master/fsm_coe.c
author Florian Pose <fp@igh-essen.com>
Thu, 31 Jul 2008 16:13:29 +0000
changeset 1174 235f34ca50e2
parent 1169 cf0b5293eea6
child 1219 aa030fb1e980
permissions -rw-r--r--
Major Pdo configuration change: Read mapping of unknown Pdos during
configuration.
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/******************************************************************************
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 *  $Id$
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
 *
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
 *  This file is part of the IgH EtherCAT Master.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
 *
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
 *  The IgH EtherCAT Master is free software; you can redistribute it
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
 *  and/or modify it under the terms of the GNU General Public License
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
 *  as published by the Free Software Foundation; either version 2 of the
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *  License, or (at your option) any later version.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
 *  The IgH EtherCAT Master is distributed in the hope that it will be
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *  GNU General Public License for more details.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *  You should have received a copy of the GNU General Public License
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *  along with the IgH EtherCAT Master; if not, write to the Free Software
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
 *  The right to use EtherCAT Technology is granted and comes free of
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *  charge under condition of compatibility of product made by
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *  Licensee. People intending to distribute/sell products based on the
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
 *  code, have to sign an agreement to guarantee that products using
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
 *  software based on IgH EtherCAT master stay compatible with the actual
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 *  EtherCAT specification (which are released themselves as an open
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
 *  standard) as the (only) precondition to have the right to use EtherCAT
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
 *  Technology, IP and trade marks.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
 *
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
 *****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
   \file
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
   EtherCAT CoE state machines.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
#include "globals.h"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
#include "master.h"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
#include "mailbox.h"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
#include "fsm_coe.h"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
    48
/** Maximum time in ms to wait for responses when reading out the dictionary.
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
    49
 */
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
    50
#define EC_FSM_COE_DICT_TIMEOUT 3000
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
    51
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
    52
/*****************************************************************************/
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
    53
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
void ec_fsm_coe_dict_start(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
void ec_fsm_coe_dict_request(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
void ec_fsm_coe_dict_check(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
void ec_fsm_coe_dict_response(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
void ec_fsm_coe_dict_desc_request(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
void ec_fsm_coe_dict_desc_check(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
void ec_fsm_coe_dict_desc_response(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
void ec_fsm_coe_dict_entry_request(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
void ec_fsm_coe_dict_entry_check(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
void ec_fsm_coe_dict_entry_response(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
void ec_fsm_coe_down_start(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
void ec_fsm_coe_down_request(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
void ec_fsm_coe_down_check(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
void ec_fsm_coe_down_response(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
void ec_fsm_coe_up_start(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
void ec_fsm_coe_up_request(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
void ec_fsm_coe_up_check(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
void ec_fsm_coe_up_response(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
void ec_fsm_coe_up_seg_request(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
void ec_fsm_coe_up_seg_check(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
void ec_fsm_coe_up_seg_response(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
void ec_fsm_coe_end(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
void ec_fsm_coe_error(ec_fsm_coe_t *);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
/**
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
    84
   Sdo abort messages.
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
    85
   The "abort Sdo transfer request" supplies an abort code,
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
   which can be translated to clear text. This table does
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
   the mapping of the codes and messages.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
const ec_code_msg_t sdo_abort_messages[] = {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
    {0x05030000, "Toggle bit not changed"},
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
    92
    {0x05040000, "Sdo protocol timeout"},
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
    {0x05040001, "Client/Server command specifier not valid or unknown"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
    {0x05040005, "Out of memory"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
    {0x06010000, "Unsupported access to an object"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
    {0x06010001, "Attempt to read a write-only object"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
    {0x06010002, "Attempt to write a read-only object"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
    {0x06020000, "This object does not exist in the object directory"},
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
    99
    {0x06040041, "The object cannot be mapped into the Pdo"},
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
    {0x06040042, "The number and length of the objects to be mapped would"
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   101
     " exceed the Pdo length"},
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
    {0x06040043, "General parameter incompatibility reason"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
    {0x06040047, "Gerneral internal incompatibility in device"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
    {0x06060000, "Access failure due to a hardware error"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
    {0x06070010, "Data type does not match, length of service parameter does"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
     " not match"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
    {0x06070012, "Data type does not match, length of service parameter too"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
     " high"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
    {0x06070013, "Data type does not match, length of service parameter too"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
     " low"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
    {0x06090011, "Subindex does not exist"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
    {0x06090030, "Value range of parameter exceeded"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
    {0x06090031, "Value of parameter written too high"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
    {0x06090032, "Value of parameter written too low"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
    {0x06090036, "Maximum value is less than minimum value"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
    {0x08000000, "General error"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
    {0x08000020, "Data cannot be transferred or stored to the application"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
    {0x08000021, "Data cannot be transferred or stored to the application"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
     " because of local control"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
    {0x08000022, "Data cannot be transferred or stored to the application"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
     " because of the present device state"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
    {0x08000023, "Object dictionary dynamic generation fails or no object"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
     " dictionary is present"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
    {}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
};
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
/**
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   130
   Outputs an Sdo abort message.
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
void ec_canopen_abort_msg(uint32_t abort_code)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
    const ec_code_msg_t *abort_msg;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
    for (abort_msg = sdo_abort_messages; abort_msg->code; abort_msg++) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
        if (abort_msg->code == abort_code) {
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   139
            EC_ERR("Sdo abort message 0x%08X: \"%s\".\n",
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
                   abort_msg->code, abort_msg->message);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   143
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   145
    EC_ERR("Unknown Sdo abort code 0x%08X.\n", abort_code);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   150
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
   Constructor.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   153
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   154
void ec_fsm_coe_init(ec_fsm_coe_t *fsm, /**< finite state machine */
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   155
                     ec_datagram_t *datagram /**< datagram */
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   156
                     )
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   157
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   158
    fsm->state = NULL;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
    fsm->datagram = datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   160
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   161
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   162
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   165
   Destructor.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   166
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   167
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   168
void ec_fsm_coe_clear(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   171
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   172
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   173
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
/**
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   175
   Starts reading a slaves' Sdo dictionary.
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   177
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   178
void ec_fsm_coe_dictionary(ec_fsm_coe_t *fsm, /**< finite state machine */
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   179
                           ec_slave_t *slave /**< EtherCAT slave */
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   180
                           )
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   181
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   182
    fsm->slave = slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
    fsm->state = ec_fsm_coe_dict_start;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
/**
859
233e32f428e1 Replaced ec_fsm_coe_\(upload\|download\)() with ec_fsm_coe_transfer();
Florian Pose <fp@igh-essen.com>
parents: 854
diff changeset
   189
   Starts to transfer an Sdo to/from a slave.
233e32f428e1 Replaced ec_fsm_coe_\(upload\|download\)() with ec_fsm_coe_transfer();
Florian Pose <fp@igh-essen.com>
parents: 854
diff changeset
   190
*/
233e32f428e1 Replaced ec_fsm_coe_\(upload\|download\)() with ec_fsm_coe_transfer();
Florian Pose <fp@igh-essen.com>
parents: 854
diff changeset
   191
233e32f428e1 Replaced ec_fsm_coe_\(upload\|download\)() with ec_fsm_coe_transfer();
Florian Pose <fp@igh-essen.com>
parents: 854
diff changeset
   192
void ec_fsm_coe_transfer(
854
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
   193
        ec_fsm_coe_t *fsm, /**< State machine. */
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
   194
        ec_slave_t *slave, /**< EtherCAT slave. */
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
   195
        ec_sdo_request_t *request /**< Sdo request. */
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
   196
        )
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   198
    fsm->slave = slave;
854
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
   199
    fsm->request = request;
859
233e32f428e1 Replaced ec_fsm_coe_\(upload\|download\)() with ec_fsm_coe_transfer();
Florian Pose <fp@igh-essen.com>
parents: 854
diff changeset
   200
    if (request->dir == EC_DIR_OUTPUT)
233e32f428e1 Replaced ec_fsm_coe_\(upload\|download\)() with ec_fsm_coe_transfer();
Florian Pose <fp@igh-essen.com>
parents: 854
diff changeset
   201
        fsm->state = ec_fsm_coe_down_start;
233e32f428e1 Replaced ec_fsm_coe_\(upload\|download\)() with ec_fsm_coe_transfer();
Florian Pose <fp@igh-essen.com>
parents: 854
diff changeset
   202
    else
233e32f428e1 Replaced ec_fsm_coe_\(upload\|download\)() with ec_fsm_coe_transfer();
Florian Pose <fp@igh-essen.com>
parents: 854
diff changeset
   203
        fsm->state = ec_fsm_coe_up_start;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   208
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
   Executes the current state of the state machine.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
   \return false, if state machine has terminated
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   211
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   212
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   213
int ec_fsm_coe_exec(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   214
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
    fsm->state(fsm);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   217
    return fsm->state != ec_fsm_coe_end && fsm->state != ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   218
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   219
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   221
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   222
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   223
   Returns, if the state machine terminated with success.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   224
   \return non-zero if successful.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   225
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   226
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   227
int ec_fsm_coe_success(ec_fsm_coe_t *fsm /**< Finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   228
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   229
    return fsm->state == ec_fsm_coe_end;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   230
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   231
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   232
/*****************************************************************************/
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   233
1092
69393cf60399 Improved source code doc.
Florian Pose <fp@igh-essen.com>
parents: 1053
diff changeset
   234
/** Check if the received data are a CoE emergency request.
69393cf60399 Improved source code doc.
Florian Pose <fp@igh-essen.com>
parents: 1053
diff changeset
   235
 *
69393cf60399 Improved source code doc.
Florian Pose <fp@igh-essen.com>
parents: 1053
diff changeset
   236
 * If the check is positive, the emergency request is output.
69393cf60399 Improved source code doc.
Florian Pose <fp@igh-essen.com>
parents: 1053
diff changeset
   237
 *
69393cf60399 Improved source code doc.
Florian Pose <fp@igh-essen.com>
parents: 1053
diff changeset
   238
 * \return The data were an emergency request.
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   239
 */
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   240
int ec_fsm_coe_check_emergency(
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   241
        ec_fsm_coe_t *fsm, /**< Finite state machine */
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   242
        const uint8_t *data, /**< CoE mailbox data. */
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   243
        size_t size /**< CoE mailbox data size. */
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   244
        )
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   245
{
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   246
    if (size < 2 || ((EC_READ_U16(data) >> 12) & 0x0F) != 0x01)
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   247
        return 0;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   248
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   249
    if (size < 10) {
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   250
        EC_WARN("Received incomplete CoE Emergency request from slave %u:\n",
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   251
                fsm->slave->ring_position);
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   252
        ec_print_data(data, size);
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   253
        return 1;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   254
    }
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   255
    
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   256
    EC_INFO("CoE Emergency Request received from slave %u:\n",
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   257
            fsm->slave->ring_position);
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   258
    EC_INFO("Error code 0x%04X, Error register 0x%02X, data:\n",
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   259
            EC_READ_U16(data + 2), EC_READ_U8(data + 4));
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   260
    ec_print_data(data + 5, 5);
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   261
    return 1;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   262
}
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   263
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   264
/******************************************************************************
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   265
 *  CoE dictionary state machine
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   266
 *****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   267
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   268
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   269
   CoE state: DICT START.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   270
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   271
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   272
void ec_fsm_coe_dict_start(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   273
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   274
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   275
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   276
    uint8_t *data;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   277
834
0791aac03180 Separated SII data from slave.
Florian Pose <fp@igh-essen.com>
parents: 831
diff changeset
   278
    if (!(slave->sii.mailbox_protocols & EC_MBOX_COE)) {
825
3771f5a0ed90 CoE state machines raising error, if the slave does not support CoE.
Florian Pose <fp@igh-essen.com>
parents: 814
diff changeset
   279
        EC_ERR("Slave %u does not support CoE!\n", slave->ring_position);
3771f5a0ed90 CoE state machines raising error, if the slave does not support CoE.
Florian Pose <fp@igh-essen.com>
parents: 814
diff changeset
   280
        fsm->state = ec_fsm_coe_error;
3771f5a0ed90 CoE state machines raising error, if the slave does not support CoE.
Florian Pose <fp@igh-essen.com>
parents: 814
diff changeset
   281
        return;
3771f5a0ed90 CoE state machines raising error, if the slave does not support CoE.
Florian Pose <fp@igh-essen.com>
parents: 814
diff changeset
   282
    }
3771f5a0ed90 CoE state machines raising error, if the slave does not support CoE.
Florian Pose <fp@igh-essen.com>
parents: 814
diff changeset
   283
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   284
    if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   285
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   286
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   287
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   288
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   289
    EC_WRITE_U16(data, 0x8 << 12); // Sdo information
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   290
    EC_WRITE_U8 (data + 2, 0x01); // Get OD List Request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   291
    EC_WRITE_U8 (data + 3, 0x00);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   292
    EC_WRITE_U16(data + 4, 0x0000);
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   293
    EC_WRITE_U16(data + 6, 0x0001); // deliver all Sdos!
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   294
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   295
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   296
    fsm->state = ec_fsm_coe_dict_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   297
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   298
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   299
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   300
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   301
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   302
   CoE state: DICT REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   303
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   304
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   305
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   306
void ec_fsm_coe_dict_request(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   307
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   308
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   309
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   310
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   311
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   312
        return; // FIXME: request again?
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   313
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   314
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   315
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   316
        EC_ERR("Failed to receive CoE dictionary request datagram for"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
   317
               " slave %u (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   318
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   319
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   320
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   321
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   322
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   323
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   324
        EC_ERR("Reception of CoE dictionary request failed on slave %u: ",
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   325
                slave->ring_position);
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   326
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   327
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   328
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   329
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
   330
    fsm->jiffies_start = datagram->jiffies_sent;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   331
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   332
    ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   333
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   334
    fsm->state = ec_fsm_coe_dict_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   335
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   336
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   337
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   338
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   339
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   340
   CoE state: DICT CHECK.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   341
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   342
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   343
void ec_fsm_coe_dict_check(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   344
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   345
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   346
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   347
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   348
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   349
        return;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   350
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   351
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   352
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   353
        EC_ERR("Failed to receive CoE mailbox check datagram for slave %u"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
   354
                " (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   355
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   356
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   357
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   358
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   359
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   360
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   361
        EC_ERR("Reception of CoE mailbox check datagram failed on slave %u: ",
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   362
               slave->ring_position);
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   363
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   364
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   365
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   366
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   367
    if (!ec_slave_mbox_check(datagram)) {
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
   368
        unsigned long diff_ms =
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
   369
            (datagram->jiffies_received - fsm->jiffies_start) * 1000 / HZ;
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   370
        if (diff_ms >= EC_FSM_COE_DICT_TIMEOUT) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   371
            fsm->state = ec_fsm_coe_error;
1111
6aaf6a0c375c Improved timeout error messages.
Florian Pose <fp@igh-essen.com>
parents: 1092
diff changeset
   372
            EC_ERR("Timeout while waiting for Sdo dictionary list response "
6aaf6a0c375c Improved timeout error messages.
Florian Pose <fp@igh-essen.com>
parents: 1092
diff changeset
   373
                    "on slave %u.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   374
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   375
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   376
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   377
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   378
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   379
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   380
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   381
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   382
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   383
    ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   384
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   385
    fsm->state = ec_fsm_coe_dict_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   386
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   387
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   388
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   389
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   390
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   391
   CoE state: DICT RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   392
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   393
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   394
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   395
void ec_fsm_coe_dict_response(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   396
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   397
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   398
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   399
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   400
    size_t rec_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   401
    unsigned int sdo_count, i;
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
   402
    uint16_t sdo_index, fragments_left;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   403
    ec_sdo_t *sdo;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   404
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   405
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   406
        return; // FIXME: request again?
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   407
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   408
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   409
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   410
        EC_ERR("Failed to receive CoE dictionary response datagram for"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
   411
               " slave %u (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   412
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   413
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   414
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   415
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   416
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   417
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   418
        EC_ERR("Reception of CoE dictionary response failed on slave %u: ",
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   419
                slave->ring_position);
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   420
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   421
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   422
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   423
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   424
    if (!(data = ec_slave_mbox_fetch(slave, datagram,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   425
				     &mbox_prot, &rec_size))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   426
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   427
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   428
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   429
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   430
    if (mbox_prot != 0x03) { // CoE
446
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
   431
        EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   432
        fsm->state = ec_fsm_coe_error;
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
   433
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   434
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   435
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   436
    if (ec_fsm_coe_check_emergency(fsm, data, rec_size)) {
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   437
        // check for CoE response again
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   438
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   439
        fsm->retries = EC_FSM_RETRIES;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   440
        fsm->state = ec_fsm_coe_dict_check;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   441
        return;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   442
    }
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   443
1037
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   444
    if (rec_size < 3) {
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   445
        EC_ERR("Received corrupted Sdo dictionary response (size %u).\n",
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   446
                rec_size);
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   447
        fsm->state = ec_fsm_coe_error;
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   448
        return;
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   449
    }
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   450
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   451
    if (EC_READ_U16(data) >> 12 == 0x8 && // Sdo information
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   452
        (EC_READ_U8(data + 2) & 0x7F) == 0x07) { // error response
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   453
        EC_ERR("Sdo information error response at slave %u!\n",
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   454
               slave->ring_position);
1037
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   455
        if (rec_size < 10) {
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   456
            EC_ERR("Incomplete Sdo information error response:\n");
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   457
            ec_print_data(data, rec_size);
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   458
        } else {
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   459
            ec_canopen_abort_msg(EC_READ_U32(data + 6));
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   460
        }
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   461
        fsm->state = ec_fsm_coe_error;
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
   462
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   463
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   464
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   465
    if (EC_READ_U16(data) >> 12 != 0x8 || // Sdo information
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   466
        (EC_READ_U8 (data + 2) & 0x7F) != 0x02) { // Get OD List response
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   467
        if (fsm->slave->master->debug_level) {
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   468
            EC_DBG("Invalid Sdo list response at slave %u! Retrying...\n",
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   469
                    slave->ring_position);
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   470
            ec_print_data(data, rec_size);
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   471
        }
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   472
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   473
        fsm->retries = EC_FSM_RETRIES;
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   474
        fsm->state = ec_fsm_coe_dict_check;
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
   475
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   476
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   477
1037
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   478
    if (rec_size < 8 || rec_size % 2) {
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   479
        EC_ERR("Invalid data size %u!\n", rec_size);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   480
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   481
        fsm->state = ec_fsm_coe_error;
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
   482
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   483
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   484
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   485
    sdo_count = (rec_size - 8) / 2;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   486
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   487
    for (i = 0; i < sdo_count; i++) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   488
        sdo_index = EC_READ_U16(data + 8 + i * 2);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   489
        if (!sdo_index) {
461
b9eda9235173 Print certain warnings only at debug_level.
Florian Pose <fp@igh-essen.com>
parents: 459
diff changeset
   490
            if (slave->master->debug_level)
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   491
                EC_WARN("Sdo dictionary of slave %u contains index 0x0000.\n",
461
b9eda9235173 Print certain warnings only at debug_level.
Florian Pose <fp@igh-essen.com>
parents: 459
diff changeset
   492
                        slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   493
            continue;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   494
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   495
998
05a24788f3b2 Replaced a few GFP_ATOMIC allocations with GFP_KERNEL ones.
Florian Pose <fp@igh-essen.com>
parents: 992
diff changeset
   496
        if (!(sdo = (ec_sdo_t *) kmalloc(sizeof(ec_sdo_t), GFP_KERNEL))) {
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   497
            EC_ERR("Failed to allocate memory for Sdo!\n");
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   498
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   499
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   500
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   501
992
50a44cbd30af Removed kobjects from sdo and slave.
Florian Pose <fp@igh-essen.com>
parents: 991
diff changeset
   502
        ec_sdo_init(sdo, slave, sdo_index);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   503
        list_add_tail(&sdo->list, &slave->sdo_dictionary);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   504
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   505
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
   506
    fragments_left = EC_READ_U16(data + 4);
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
   507
    if (slave->master->debug_level && fragments_left) {
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   508
        EC_DBG("Sdo list fragments left: %u\n", fragments_left);
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
   509
    }
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
   510
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
   511
    if (EC_READ_U8(data + 2) & 0x80 || fragments_left) { // more messages waiting. check again.
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
   512
        fsm->jiffies_start = datagram->jiffies_sent;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   513
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   514
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   515
        fsm->state = ec_fsm_coe_dict_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   516
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   517
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   518
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   519
    if (list_empty(&slave->sdo_dictionary)) {
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   520
        // no Sdos in dictionary. finished.
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   521
        fsm->state = ec_fsm_coe_end; // success
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   522
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   523
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   524
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   525
    // fetch Sdo descriptions
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   526
    fsm->sdo = list_entry(slave->sdo_dictionary.next, ec_sdo_t, list);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   527
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   528
    if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   529
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   530
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   531
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   532
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   533
    EC_WRITE_U16(data, 0x8 << 12); // Sdo information
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   534
    EC_WRITE_U8 (data + 2, 0x03); // Get object description request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   535
    EC_WRITE_U8 (data + 3, 0x00);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   536
    EC_WRITE_U16(data + 4, 0x0000);
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   537
    EC_WRITE_U16(data + 6, fsm->sdo->index); // Sdo index
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   538
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   539
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   540
    fsm->state = ec_fsm_coe_dict_desc_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   541
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   542
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   543
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   544
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   545
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   546
   CoE state: DICT DESC REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   547
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   548
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   549
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   550
void ec_fsm_coe_dict_desc_request(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   551
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   552
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   553
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   554
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   555
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   556
        return; // FIXME: check for response first?
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   557
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   558
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   559
        fsm->state = ec_fsm_coe_error;
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   560
        EC_ERR("Failed to receive CoE Sdo description request datagram for"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
   561
               " slave %u (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   562
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   563
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   564
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   565
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   566
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   567
        fsm->state = ec_fsm_coe_error;
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   568
        EC_ERR("Reception of CoE Sdo description"
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   569
                " request failed on slave %u: ", slave->ring_position);
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   570
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   571
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   572
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   573
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
   574
    fsm->jiffies_start = datagram->jiffies_sent;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   575
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   576
    ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   577
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   578
    fsm->state = ec_fsm_coe_dict_desc_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   579
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   580
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   581
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   582
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   583
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   584
   CoE state: DICT DESC CHECK.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   585
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   586
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   587
void ec_fsm_coe_dict_desc_check(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   588
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   589
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   590
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   591
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   592
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   593
        return;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   594
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   595
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   596
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   597
        EC_ERR("Failed to receive CoE mailbox check datagram from slave %u"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
   598
                " (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   599
                slave->ring_position, datagram->state);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   600
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   601
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   602
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   603
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   604
        fsm->state = ec_fsm_coe_error;
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   605
        EC_ERR("Reception of CoE mailbox check"
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   606
                " datagram failed on slave %u: ", slave->ring_position);
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   607
        ec_datagram_print_wc_error(datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   608
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   609
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   610
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   611
    if (!ec_slave_mbox_check(datagram)) {
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
   612
        unsigned long diff_ms =
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
   613
            (datagram->jiffies_received - fsm->jiffies_start) * 1000 / HZ;
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   614
        if (diff_ms >= EC_FSM_COE_DICT_TIMEOUT) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   615
            fsm->state = ec_fsm_coe_error;
1111
6aaf6a0c375c Improved timeout error messages.
Florian Pose <fp@igh-essen.com>
parents: 1092
diff changeset
   616
            EC_ERR("Timeout while waiting for Sdo object description "
6aaf6a0c375c Improved timeout error messages.
Florian Pose <fp@igh-essen.com>
parents: 1092
diff changeset
   617
                    "response on slave %u.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   618
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   619
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   620
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   621
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   622
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   623
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   624
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   625
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   626
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   627
    ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   628
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   629
    fsm->state = ec_fsm_coe_dict_desc_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   630
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   631
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   632
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   633
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   634
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   635
   CoE state: DICT DESC RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   636
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   637
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   638
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   639
void ec_fsm_coe_dict_desc_response(ec_fsm_coe_t *fsm
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   640
                                   /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   641
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   642
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   643
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   644
    ec_sdo_t *sdo = fsm->sdo;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   645
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   646
    size_t rec_size, name_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   647
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   648
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   649
        return; // FIXME: request again?
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   650
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   651
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   652
        fsm->state = ec_fsm_coe_error;
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   653
        EC_ERR("Failed to receive CoE Sdo description response datagram from"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
   654
               " slave %u (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   655
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   656
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   657
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   658
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   659
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   660
        fsm->state = ec_fsm_coe_error;
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   661
        EC_ERR("Reception of CoE Sdo description"
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   662
                " response failed on slave %u: ", slave->ring_position);
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   663
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   664
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   665
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   666
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   667
    if (!(data = ec_slave_mbox_fetch(slave, datagram,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   668
				     &mbox_prot, &rec_size))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   669
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   670
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   671
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   672
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   673
    if (mbox_prot != 0x03) { // CoE
446
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
   674
        EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   675
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   676
        return;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   677
    }
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   678
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   679
    if (ec_fsm_coe_check_emergency(fsm, data, rec_size)) {
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   680
        // check for CoE response again
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   681
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   682
        fsm->retries = EC_FSM_RETRIES;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   683
        fsm->state = ec_fsm_coe_dict_desc_check;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   684
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   685
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   686
1037
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   687
    if (rec_size < 3) {
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   688
        EC_ERR("Received corrupted Sdo description response (size %u).\n",
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   689
                rec_size);
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   690
        fsm->state = ec_fsm_coe_error;
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   691
        return;
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   692
    }
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   693
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   694
    if (EC_READ_U16(data) >> 12 == 0x8 && // Sdo information
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   695
        (EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   696
        EC_ERR("Sdo information error response at slave %u while"
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   697
               " fetching Sdo 0x%04X!\n", slave->ring_position,
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   698
               sdo->index);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   699
        ec_canopen_abort_msg(EC_READ_U32(data + 6));
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   700
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   701
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   702
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   703
1037
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   704
    if (rec_size < 8) {
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   705
        EC_ERR("Received corrupted Sdo description response (size %u).\n",
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   706
                rec_size);
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   707
        fsm->state = ec_fsm_coe_error;
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   708
        return;
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   709
    }
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   710
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   711
    if (EC_READ_U16(data) >> 12 != 0x8 || // Sdo information
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   712
        (EC_READ_U8 (data + 2) & 0x7F) != 0x04 || // Object desc. response
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   713
        EC_READ_U16(data + 6) != sdo->index) { // Sdo index
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   714
        if (fsm->slave->master->debug_level) {
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   715
            EC_DBG("Invalid object description response at slave %u while"
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   716
                    " fetching Sdo 0x%04X!\n", slave->ring_position,
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   717
                    sdo->index);
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   718
            ec_print_data(data, rec_size);
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   719
        }
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   720
        // check for CoE response again
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   721
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   722
        fsm->retries = EC_FSM_RETRIES;
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   723
        fsm->state = ec_fsm_coe_dict_desc_check;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   724
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   725
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   726
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   727
    if (rec_size < 12) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   728
        EC_ERR("Invalid data size!\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   729
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   730
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   731
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   732
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   733
962
ea83a7aab57a Renamed subindices to max_subindex.
Florian Pose <fp@igh-essen.com>
parents: 926
diff changeset
   734
    sdo->max_subindex = EC_READ_U8(data + 10);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   735
    sdo->object_code = EC_READ_U8(data + 11);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   736
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   737
    name_size = rec_size - 12;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   738
    if (name_size) {
998
05a24788f3b2 Replaced a few GFP_ATOMIC allocations with GFP_KERNEL ones.
Florian Pose <fp@igh-essen.com>
parents: 992
diff changeset
   739
        if (!(sdo->name = kmalloc(name_size + 1, GFP_KERNEL))) {
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   740
            EC_ERR("Failed to allocate Sdo name!\n");
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   741
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   742
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   743
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   744
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   745
        memcpy(sdo->name, data + 12, name_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   746
        sdo->name[name_size] = 0;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   747
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   748
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   749
    if (EC_READ_U8(data + 2) & 0x80) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   750
        EC_ERR("Fragment follows (not implemented)!\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   751
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   752
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   753
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   754
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   755
    // start fetching entries
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   756
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   757
    fsm->subindex = 0;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   758
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   759
    if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   760
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   761
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   762
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   763
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   764
    EC_WRITE_U16(data, 0x8 << 12); // Sdo information
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   765
    EC_WRITE_U8 (data + 2, 0x05); // Get entry description request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   766
    EC_WRITE_U8 (data + 3, 0x00);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   767
    EC_WRITE_U16(data + 4, 0x0000);
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   768
    EC_WRITE_U16(data + 6, sdo->index); // Sdo index
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   769
    EC_WRITE_U8 (data + 8, fsm->subindex); // Sdo subindex
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   770
    EC_WRITE_U8 (data + 9, 0x00); // value info (no values)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   771
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   772
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   773
    fsm->state = ec_fsm_coe_dict_entry_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   774
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   775
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   776
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   777
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   778
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   779
   CoE state: DICT ENTRY REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   780
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   781
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   782
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   783
void ec_fsm_coe_dict_entry_request(ec_fsm_coe_t *fsm
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   784
                                   /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   785
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   786
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   787
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   788
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   789
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   790
        return; // FIXME: check for response first?
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   791
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   792
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   793
        fsm->state = ec_fsm_coe_error;
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   794
        EC_ERR("Failed to receive CoE Sdo entry request datagram for"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
   795
               " slave %u (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   796
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   797
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   798
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   799
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   800
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   801
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   802
        EC_ERR("Reception of CoE Sdo entry request failed on slave %u: ",
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   803
                slave->ring_position);
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   804
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   805
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   806
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   807
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
   808
    fsm->jiffies_start = datagram->jiffies_sent;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   809
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   810
    ec_slave_mbox_prepare_check(slave, datagram); // can not fail
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   811
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   812
    fsm->state = ec_fsm_coe_dict_entry_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   813
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   814
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   815
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   816
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   817
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   818
   CoE state: DICT ENTRY CHECK.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   819
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   820
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   821
void ec_fsm_coe_dict_entry_check(ec_fsm_coe_t *fsm
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   822
                                 /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   823
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   824
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   825
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   826
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   827
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   828
        return;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   829
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   830
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   831
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   832
        EC_ERR("Failed to receive CoE mailbox check datagram from slave %u"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
   833
                " (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   834
               slave->ring_position, datagram->state);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   835
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   836
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   837
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   838
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   839
        fsm->state = ec_fsm_coe_error;
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   840
        EC_ERR("Reception of CoE mailbox check"
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   841
                " datagram failed on slave %u: ", slave->ring_position);
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   842
        ec_datagram_print_wc_error(datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   843
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   844
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   845
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   846
    if (!ec_slave_mbox_check(datagram)) {
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
   847
        unsigned long diff_ms =
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
   848
            (datagram->jiffies_received - fsm->jiffies_start) * 1000 / HZ;
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   849
        if (diff_ms >= EC_FSM_COE_DICT_TIMEOUT) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   850
            fsm->state = ec_fsm_coe_error;
1111
6aaf6a0c375c Improved timeout error messages.
Florian Pose <fp@igh-essen.com>
parents: 1092
diff changeset
   851
            EC_ERR("Timeout while waiting for Sdo entry description response "
6aaf6a0c375c Improved timeout error messages.
Florian Pose <fp@igh-essen.com>
parents: 1092
diff changeset
   852
                    "on slave %u.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   853
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   854
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   855
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   856
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   857
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   858
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   859
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   860
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   861
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   862
    ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   863
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   864
    fsm->state = ec_fsm_coe_dict_entry_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   865
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   866
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   867
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   868
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   869
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   870
   CoE state: DICT ENTRY RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   871
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   872
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   873
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   874
void ec_fsm_coe_dict_entry_response(ec_fsm_coe_t *fsm
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   875
                                    /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   876
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   877
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   878
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   879
    ec_sdo_t *sdo = fsm->sdo;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   880
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   881
    size_t rec_size, data_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   882
    ec_sdo_entry_t *entry;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   883
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   884
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   885
        return; // FIXME: request again?
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   886
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   887
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   888
        fsm->state = ec_fsm_coe_error;
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   889
        EC_ERR("Failed to receive CoE Sdo description response datagram from"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
   890
               " slave %u (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   891
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   892
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   893
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   894
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   895
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   896
        fsm->state = ec_fsm_coe_error;
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   897
        EC_ERR("Reception of CoE Sdo description"
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   898
                " response failed on slave %u: ", slave->ring_position);
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   899
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   900
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   901
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   902
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   903
    if (!(data = ec_slave_mbox_fetch(slave, datagram,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   904
				     &mbox_prot, &rec_size))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   905
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   906
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   907
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   908
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   909
    if (mbox_prot != 0x03) { // CoE
446
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
   910
        EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   911
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   912
        return;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   913
    }
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   914
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   915
    if (ec_fsm_coe_check_emergency(fsm, data, rec_size)) {
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   916
        // check for CoE response again
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   917
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   918
        fsm->retries = EC_FSM_RETRIES;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   919
        fsm->state = ec_fsm_coe_dict_entry_check;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   920
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   921
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   922
1037
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   923
    if (rec_size < 3) {
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   924
        EC_ERR("Received corrupted Sdo entry description response "
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   925
                "(size %u).\n", rec_size);
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   926
        fsm->state = ec_fsm_coe_error;
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   927
        return;
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   928
    }
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   929
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   930
    if (EC_READ_U16(data) >> 12 == 0x8 && // Sdo information
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   931
        (EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   932
        EC_ERR("Sdo information error response at slave %u while"
926
6bb33c6ec770 Output subindices with %02X.
Florian Pose <fp@igh-essen.com>
parents: 895
diff changeset
   933
               " fetching Sdo entry 0x%04X:%02X!\n", slave->ring_position,
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   934
               sdo->index, fsm->subindex);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   935
        ec_canopen_abort_msg(EC_READ_U32(data + 6));
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   936
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   937
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   938
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   939
1037
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   940
    if (rec_size < 9) {
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   941
        EC_ERR("Received corrupted Sdo entry description response "
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   942
                "(size %u).\n", rec_size);
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   943
        fsm->state = ec_fsm_coe_error;
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   944
        return;
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   945
    }
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   946
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   947
    if (EC_READ_U16(data) >> 12 != 0x8 || // Sdo information
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   948
        (EC_READ_U8(data + 2) & 0x7F) != 0x06 || // Entry desc. response
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   949
        EC_READ_U16(data + 6) != sdo->index || // Sdo index
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   950
        EC_READ_U8(data + 8) != fsm->subindex) { // Sdo subindex
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   951
        if (fsm->slave->master->debug_level) {
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   952
            EC_DBG("Invalid entry description response at slave %u while"
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   953
                    " fetching Sdo entry 0x%04X:%02X!\n", slave->ring_position,
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   954
                    sdo->index, fsm->subindex);
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   955
            ec_print_data(data, rec_size);
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   956
        }
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   957
        // check for CoE response again
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   958
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   959
        fsm->retries = EC_FSM_RETRIES;
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   960
        fsm->state = ec_fsm_coe_dict_entry_check;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   961
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   962
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   963
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   964
    if (rec_size < 16) {
1037
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   965
        EC_ERR("Invalid data size %u!\n", rec_size);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   966
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   967
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   968
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   969
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   970
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   971
    data_size = rec_size - 16;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   972
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   973
    if (!(entry = (ec_sdo_entry_t *)
998
05a24788f3b2 Replaced a few GFP_ATOMIC allocations with GFP_KERNEL ones.
Florian Pose <fp@igh-essen.com>
parents: 992
diff changeset
   974
          kmalloc(sizeof(ec_sdo_entry_t), GFP_KERNEL))) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   975
        EC_ERR("Failed to allocate entry!\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   976
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   977
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   978
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   979
991
2548ca639b1f Removed kobject from sdo entry.
Florian Pose <fp@igh-essen.com>
parents: 986
diff changeset
   980
    ec_sdo_entry_init(entry, sdo, fsm->subindex);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   981
    entry->data_type = EC_READ_U16(data + 10);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   982
    entry->bit_length = EC_READ_U16(data + 12);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   983
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   984
    if (data_size) {
484
9fde4a17b820 kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents: 476
diff changeset
   985
        uint8_t *desc;
998
05a24788f3b2 Replaced a few GFP_ATOMIC allocations with GFP_KERNEL ones.
Florian Pose <fp@igh-essen.com>
parents: 992
diff changeset
   986
        if (!(desc = kmalloc(data_size + 1, GFP_KERNEL))) {
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
   987
            EC_ERR("Failed to allocate Sdo entry name!\n");
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   988
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   989
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   990
        }
484
9fde4a17b820 kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents: 476
diff changeset
   991
        memcpy(desc, data + 16, data_size);
9fde4a17b820 kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents: 476
diff changeset
   992
        desc[data_size] = 0;
9fde4a17b820 kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents: 476
diff changeset
   993
        entry->description = desc;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   994
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   995
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   996
    list_add_tail(&entry->list, &sdo->entries);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   997
962
ea83a7aab57a Renamed subindices to max_subindex.
Florian Pose <fp@igh-essen.com>
parents: 926
diff changeset
   998
    if (fsm->subindex < sdo->max_subindex) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   999
        fsm->subindex++;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1000
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1001
        if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1002
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1003
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1004
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1005
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1006
        EC_WRITE_U16(data, 0x8 << 12); // Sdo information
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1007
        EC_WRITE_U8 (data + 2, 0x05); // Get entry description request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1008
        EC_WRITE_U8 (data + 3, 0x00);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1009
        EC_WRITE_U16(data + 4, 0x0000);
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1010
        EC_WRITE_U16(data + 6, sdo->index); // Sdo index
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1011
        EC_WRITE_U8 (data + 8, fsm->subindex); // Sdo subindex
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1012
        EC_WRITE_U8 (data + 9, 0x00); // value info (no values)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1013
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1014
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1015
        fsm->state = ec_fsm_coe_dict_entry_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1016
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1017
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1018
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1019
    // another Sdo description to fetch?
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1020
    if (fsm->sdo->list.next != &slave->sdo_dictionary) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1021
        fsm->sdo = list_entry(fsm->sdo->list.next, ec_sdo_t, list);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1022
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1023
        if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1024
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1025
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1026
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1027
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1028
        EC_WRITE_U16(data, 0x8 << 12); // Sdo information
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1029
        EC_WRITE_U8 (data + 2, 0x03); // Get object description request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1030
        EC_WRITE_U8 (data + 3, 0x00);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1031
        EC_WRITE_U16(data + 4, 0x0000);
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1032
        EC_WRITE_U16(data + 6, fsm->sdo->index); // Sdo index
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1033
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1034
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1035
        fsm->state = ec_fsm_coe_dict_desc_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1036
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1037
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1038
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1039
    fsm->state = ec_fsm_coe_end;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1040
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1041
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1042
/******************************************************************************
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1043
 *  CoE state machine
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1044
 *****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1045
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1046
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1047
   CoE state: DOWN START.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1048
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1049
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1050
void ec_fsm_coe_down_start(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1051
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1052
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1053
    ec_slave_t *slave = fsm->slave;
854
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1054
    ec_sdo_request_t *request = fsm->request;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1055
    uint8_t *data;
1169
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1056
    uint8_t size;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1057
874
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  1058
    if (fsm->slave->master->debug_level) {
926
6bb33c6ec770 Output subindices with %02X.
Florian Pose <fp@igh-essen.com>
parents: 895
diff changeset
  1059
        EC_DBG("Downloading Sdo 0x%04X:%02X to slave %u.\n",
854
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1060
               request->index, request->subindex, slave->ring_position);
874
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  1061
        ec_print_data(request->data, request->data_size);
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  1062
    }
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1063
834
0791aac03180 Separated SII data from slave.
Florian Pose <fp@igh-essen.com>
parents: 831
diff changeset
  1064
    if (!(slave->sii.mailbox_protocols & EC_MBOX_COE)) {
825
3771f5a0ed90 CoE state machines raising error, if the slave does not support CoE.
Florian Pose <fp@igh-essen.com>
parents: 814
diff changeset
  1065
        EC_ERR("Slave %u does not support CoE!\n", slave->ring_position);
3771f5a0ed90 CoE state machines raising error, if the slave does not support CoE.
Florian Pose <fp@igh-essen.com>
parents: 814
diff changeset
  1066
        fsm->state = ec_fsm_coe_error;
3771f5a0ed90 CoE state machines raising error, if the slave does not support CoE.
Florian Pose <fp@igh-essen.com>
parents: 814
diff changeset
  1067
        return;
3771f5a0ed90 CoE state machines raising error, if the slave does not support CoE.
Florian Pose <fp@igh-essen.com>
parents: 814
diff changeset
  1068
    }
1169
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1069
	
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1070
	if (request->data_size <= 4) { // use expedited transfer type
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1071
	    if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10))) {
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1072
	        fsm->state = ec_fsm_coe_error;
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1073
	        return;
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1074
	    }
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1075
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1076
	    size = 4 - request->data_size;
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1077
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1078
	    EC_WRITE_U16(data, 0x2 << 12); // Sdo request
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1079
	    EC_WRITE_U8 (data + 2, (0x3 // size specified, expedited
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1080
								| size << 2
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1081
	                            | 0x1 << 5)); // Download request
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1082
	    EC_WRITE_U16(data + 3, request->index);
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1083
	    EC_WRITE_U8 (data + 5, request->subindex);
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1084
	    memcpy(data + 6, request->data, request->data_size);
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1085
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1086
        if (slave->master->debug_level) {
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1087
            EC_DBG("Expedited download request:\n");
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1088
            ec_print_data(data, 10 + request->data_size);
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1089
        }
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1090
	}
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1091
    else { // request->data_size > 4, use normal transfer type
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1092
	    if (slave->sii.rx_mailbox_size < 6 + 10 + request->data_size) {
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1093
	        EC_ERR("Sdo fragmenting not supported yet!\n");
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1094
	        fsm->state = ec_fsm_coe_error;
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1095
	        return;
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1096
	    }
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1097
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1098
	    if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03,
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1099
	                                            request->data_size + 10))) {
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1100
	        fsm->state = ec_fsm_coe_error;
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1101
	        return;
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1102
	    }
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1103
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1104
	    EC_WRITE_U16(data, 0x2 << 12); // Sdo request
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1105
	    EC_WRITE_U8 (data + 2, (0x1 // size indicator, normal
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1106
	                            | 0x1 << 5)); // Download request
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1107
	    EC_WRITE_U16(data + 3, request->index);
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1108
	    EC_WRITE_U8 (data + 5, request->subindex);
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1109
	    EC_WRITE_U32(data + 6, request->data_size);
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1110
	    memcpy(data + 10, request->data, request->data_size);
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1111
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1112
        if (slave->master->debug_level) {
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1113
            EC_DBG("Normal download request:\n");
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1114
            ec_print_data(data, 10 + request->data_size);
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1115
        }
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1116
	}
975
dae04112bcc7 Added debugging for sdo download.
Florian Pose <fp@igh-essen.com>
parents: 962
diff changeset
  1117
1035
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1118
    fsm->request->jiffies_sent = jiffies;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1119
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1120
    fsm->state = ec_fsm_coe_down_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1121
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1122
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1123
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1124
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1125
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1126
   CoE state: DOWN REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1127
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1128
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1129
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1130
void ec_fsm_coe_down_request(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1131
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1132
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1133
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1134
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1135
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1136
        return; // FIXME: check for response first?
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1137
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1138
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1139
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1140
        EC_ERR("Failed to receive CoE download request datagram for"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
  1141
               " slave %u (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
  1142
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1143
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1144
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1145
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1146
    if (datagram->working_counter != 1) {
1035
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1147
        if (!datagram->working_counter) {
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1148
            unsigned long diff_ms =
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1149
                (jiffies - fsm->request->jiffies_sent) * 1000 / HZ;
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1150
            if (diff_ms < fsm->request->response_timeout) {
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1151
                if (fsm->slave->master->debug_level) {
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1152
                    EC_DBG("Slave %u did not respond to Sdo download request. "
1035
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1153
                            "Retrying after %u ms...\n",
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1154
                            slave->ring_position, (u32) diff_ms);
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1155
                }
1112
d11661e1f4de Fixed CoE transfer timeout bug.
Florian Pose <fp@igh-essen.com>
parents: 1111
diff changeset
  1156
                // no response; send request datagram again
d11661e1f4de Fixed CoE transfer timeout bug.
Florian Pose <fp@igh-essen.com>
parents: 1111
diff changeset
  1157
                return;
1035
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1158
            }
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1159
        }
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1160
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1161
        EC_ERR("Reception of CoE download request failed on slave %u: ",
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1162
                slave->ring_position);
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1163
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1164
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1165
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1166
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
  1167
    fsm->jiffies_start = datagram->jiffies_sent;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1168
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1169
    ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1170
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1171
    fsm->state = ec_fsm_coe_down_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1172
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1173
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1174
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1175
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1176
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1177
   CoE state: DOWN CHECK.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1178
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1179
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1180
void ec_fsm_coe_down_check(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1181
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1182
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1183
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1184
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1185
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1186
        return;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1187
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1188
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1189
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1190
        EC_ERR("Failed to receive CoE mailbox check datagram for slave %u"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
  1191
                " (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
  1192
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1193
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1194
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1195
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1196
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1197
        fsm->state = ec_fsm_coe_error;
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1198
        EC_ERR("Reception of CoE mailbox check"
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1199
                " datagram failed on slave %u: ", slave->ring_position);
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1200
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1201
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1202
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1203
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1204
    if (!ec_slave_mbox_check(datagram)) {
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
  1205
        unsigned long diff_ms =
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
  1206
            (datagram->jiffies_received - fsm->jiffies_start) * 1000 / HZ;
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1207
        if (diff_ms >= fsm->request->response_timeout) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1208
            fsm->state = ec_fsm_coe_error;
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1209
            EC_ERR("Timeout while waiting for Sdo download response on "
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1210
                    "slave %u.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1211
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1212
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1213
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1214
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1215
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1216
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1217
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1218
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1219
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1220
    ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1221
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1222
    fsm->state = ec_fsm_coe_down_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1223
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1224
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1225
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1226
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1227
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1228
   CoE state: DOWN RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1229
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1230
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1231
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1232
void ec_fsm_coe_down_response(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1233
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1234
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1235
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1236
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1237
    size_t rec_size;
854
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1238
    ec_sdo_request_t *request = fsm->request;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1239
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1240
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1241
        return; // FIXME: request again?
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1242
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1243
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1244
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1245
        EC_ERR("Failed to receive CoE download response datagram from"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
  1246
               " slave %u (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
  1247
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1248
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1249
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1250
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1251
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1252
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1253
        EC_ERR("Reception of CoE download response failed on slave %u: ",
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1254
                slave->ring_position);
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1255
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1256
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1257
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1258
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1259
    if (!(data = ec_slave_mbox_fetch(slave, datagram,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1260
				     &mbox_prot, &rec_size))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1261
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1262
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1263
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1264
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1265
    if (mbox_prot != 0x03) { // CoE
860
ba5c38f0bb30 Minor change: Moved state assign to beginning.
Florian Pose <fp@igh-essen.com>
parents: 859
diff changeset
  1266
        fsm->state = ec_fsm_coe_error;
446
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1267
        EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
860
ba5c38f0bb30 Minor change: Moved state assign to beginning.
Florian Pose <fp@igh-essen.com>
parents: 859
diff changeset
  1268
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1269
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1270
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1271
    if (ec_fsm_coe_check_emergency(fsm, data, rec_size)) {
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1272
        // check for CoE response again
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1273
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1274
        fsm->retries = EC_FSM_RETRIES;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1275
        fsm->state = ec_fsm_coe_down_check;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1276
        return;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1277
    }
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1278
975
dae04112bcc7 Added debugging for sdo download.
Florian Pose <fp@igh-essen.com>
parents: 962
diff changeset
  1279
    if (slave->master->debug_level) {
dae04112bcc7 Added debugging for sdo download.
Florian Pose <fp@igh-essen.com>
parents: 962
diff changeset
  1280
        EC_DBG("Download response:\n");
dae04112bcc7 Added debugging for sdo download.
Florian Pose <fp@igh-essen.com>
parents: 962
diff changeset
  1281
        ec_print_data(data, rec_size);
dae04112bcc7 Added debugging for sdo download.
Florian Pose <fp@igh-essen.com>
parents: 962
diff changeset
  1282
    }
dae04112bcc7 Added debugging for sdo download.
Florian Pose <fp@igh-essen.com>
parents: 962
diff changeset
  1283
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1284
    if (rec_size < 6) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1285
        fsm->state = ec_fsm_coe_error;
1037
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
  1286
        EC_ERR("Received data are too small (%u bytes):\n", rec_size);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1287
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1288
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1289
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1290
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1291
    if (EC_READ_U16(data) >> 12 == 0x2 && // Sdo request
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1292
        EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort Sdo transfer request
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1293
        fsm->state = ec_fsm_coe_error;
926
6bb33c6ec770 Output subindices with %02X.
Florian Pose <fp@igh-essen.com>
parents: 895
diff changeset
  1294
        EC_ERR("Sdo download 0x%04X:%02X (%u bytes) aborted on slave %u.\n",
854
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1295
               request->index, request->subindex, request->data_size,
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1296
               slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1297
        if (rec_size < 10) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1298
            EC_ERR("Incomplete Abort command:\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1299
            ec_print_data(data, rec_size);
1037
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
  1300
        } else {
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
  1301
            fsm->request->abort_code = EC_READ_U32(data + 6);
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
  1302
            ec_canopen_abort_msg(fsm->request->abort_code);
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
  1303
        }
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1304
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1305
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1306
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1307
    if (EC_READ_U16(data) >> 12 != 0x3 || // Sdo response
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1308
        EC_READ_U8 (data + 2) >> 5 != 0x3 || // Download response
854
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1309
        EC_READ_U16(data + 3) != request->index || // index
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1310
        EC_READ_U8 (data + 5) != request->subindex) { // subindex
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1311
        if (slave->master->debug_level) {
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1312
            EC_DBG("Invalid Sdo download response at slave %u! Retrying...\n",
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1313
                    slave->ring_position);
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1314
            ec_print_data(data, rec_size);
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1315
        }
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1316
        // check for CoE response again
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1317
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1318
        fsm->retries = EC_FSM_RETRIES;
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1319
        fsm->state = ec_fsm_coe_down_check;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1320
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1321
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1322
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1323
    fsm->state = ec_fsm_coe_end; // success
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1324
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1325
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1326
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1327
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1328
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1329
   CoE state: UP START.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1330
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1331
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1332
void ec_fsm_coe_up_start(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1333
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1334
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1335
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1336
    ec_master_t *master = slave->master;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1337
    ec_sdo_request_t *request = fsm->request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1338
    uint8_t *data;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1339
459
f0da52cf6b5b Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents: 446
diff changeset
  1340
    if (master->debug_level)
926
6bb33c6ec770 Output subindices with %02X.
Florian Pose <fp@igh-essen.com>
parents: 895
diff changeset
  1341
        EC_DBG("Uploading Sdo 0x%04X:%02X from slave %u.\n",
831
ded9519c8d6e Reading of Pdo mapping via CoE during scan.
Florian Pose <fp@igh-essen.com>
parents: 825
diff changeset
  1342
               request->index, request->subindex, slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1343
834
0791aac03180 Separated SII data from slave.
Florian Pose <fp@igh-essen.com>
parents: 831
diff changeset
  1344
    if (!(slave->sii.mailbox_protocols & EC_MBOX_COE)) {
825
3771f5a0ed90 CoE state machines raising error, if the slave does not support CoE.
Florian Pose <fp@igh-essen.com>
parents: 814
diff changeset
  1345
        EC_ERR("Slave %u does not support CoE!\n", slave->ring_position);
3771f5a0ed90 CoE state machines raising error, if the slave does not support CoE.
Florian Pose <fp@igh-essen.com>
parents: 814
diff changeset
  1346
        fsm->state = ec_fsm_coe_error;
3771f5a0ed90 CoE state machines raising error, if the slave does not support CoE.
Florian Pose <fp@igh-essen.com>
parents: 814
diff changeset
  1347
        return;
3771f5a0ed90 CoE state machines raising error, if the slave does not support CoE.
Florian Pose <fp@igh-essen.com>
parents: 814
diff changeset
  1348
    }
3771f5a0ed90 CoE state machines raising error, if the slave does not support CoE.
Florian Pose <fp@igh-essen.com>
parents: 814
diff changeset
  1349
476
fd7c0ee645f8 Fixed CoE Upload Request, now 10 bytes of Mailbox Service Data.
Florian Pose <fp@igh-essen.com>
parents: 461
diff changeset
  1350
    if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10))) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1351
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1352
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1353
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1354
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1355
    EC_WRITE_U16(data, 0x2 << 12); // Sdo request
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1356
    EC_WRITE_U8 (data + 2, 0x2 << 5); // initiate upload request
831
ded9519c8d6e Reading of Pdo mapping via CoE during scan.
Florian Pose <fp@igh-essen.com>
parents: 825
diff changeset
  1357
    EC_WRITE_U16(data + 3, request->index);
ded9519c8d6e Reading of Pdo mapping via CoE during scan.
Florian Pose <fp@igh-essen.com>
parents: 825
diff changeset
  1358
    EC_WRITE_U8 (data + 5, request->subindex);
476
fd7c0ee645f8 Fixed CoE Upload Request, now 10 bytes of Mailbox Service Data.
Florian Pose <fp@igh-essen.com>
parents: 461
diff changeset
  1359
    memset(data + 6, 0x00, 4);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1360
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1361
    if (master->debug_level) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1362
        EC_DBG("Upload request:\n");
476
fd7c0ee645f8 Fixed CoE Upload Request, now 10 bytes of Mailbox Service Data.
Florian Pose <fp@igh-essen.com>
parents: 461
diff changeset
  1363
        ec_print_data(data, 10);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1364
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1365
1035
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1366
    fsm->request->jiffies_sent = jiffies;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1367
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1368
    fsm->state = ec_fsm_coe_up_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1369
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1370
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1371
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1372
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1373
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1374
   CoE state: UP REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1375
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1376
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1377
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1378
void ec_fsm_coe_up_request(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1379
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1380
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1381
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1382
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1383
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1384
        return; // FIXME: check for response first?
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1385
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1386
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1387
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1388
        EC_ERR("Failed to receive CoE upload request for slave %u"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
  1389
                " (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
  1390
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1391
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1392
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1393
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1394
    if (datagram->working_counter != 1) {
1035
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1395
        if (!datagram->working_counter) {
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1396
            unsigned long diff_ms =
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1397
                (jiffies - fsm->request->jiffies_sent) * 1000 / HZ;
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1398
            if (diff_ms < fsm->request->response_timeout) {
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1399
                if (fsm->slave->master->debug_level) {
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1400
                    EC_DBG("Slave %u did no respond to Sdo upload request. "
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1401
                            "Retrying after %u ms...\n",
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1402
                            slave->ring_position, (u32) diff_ms);
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1403
                }
1112
d11661e1f4de Fixed CoE transfer timeout bug.
Florian Pose <fp@igh-essen.com>
parents: 1111
diff changeset
  1404
                // no response; send request datagram again
d11661e1f4de Fixed CoE transfer timeout bug.
Florian Pose <fp@igh-essen.com>
parents: 1111
diff changeset
  1405
                return;
1035
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1406
            }
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1407
        }
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1408
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1409
        EC_ERR("Reception of CoE upload request failed on slave %u: ",
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1410
                slave->ring_position);
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1411
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1412
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1413
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1414
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
  1415
    fsm->jiffies_start = datagram->jiffies_sent;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1416
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1417
    ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1418
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1419
    fsm->state = ec_fsm_coe_up_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1420
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1421
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1422
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1423
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1424
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1425
   CoE state: UP CHECK.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1426
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1427
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1428
void ec_fsm_coe_up_check(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1429
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1430
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1431
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1432
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1433
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1434
        return;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1435
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1436
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1437
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1438
        EC_ERR("Failed to receive CoE mailbox check datagram from slave %u"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
  1439
                " (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
  1440
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1441
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1442
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1443
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1444
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1445
        fsm->state = ec_fsm_coe_error;
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1446
        EC_ERR("Reception of CoE mailbox check"
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1447
                " datagram failed on slave %u: ", slave->ring_position);
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1448
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1449
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1450
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1451
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1452
    if (!ec_slave_mbox_check(datagram)) {
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
  1453
        unsigned long diff_ms =
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
  1454
            (datagram->jiffies_received - fsm->jiffies_start) * 1000 / HZ;
1043
2abea4233a6d Response timeout for checking Sdo upload.
Florian Pose <fp@igh-essen.com>
parents: 1038
diff changeset
  1455
        if (diff_ms >= fsm->request->response_timeout) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1456
            fsm->state = ec_fsm_coe_error;
1111
6aaf6a0c375c Improved timeout error messages.
Florian Pose <fp@igh-essen.com>
parents: 1092
diff changeset
  1457
            EC_ERR("Timeout while waiting for Sdo upload response on "
6aaf6a0c375c Improved timeout error messages.
Florian Pose <fp@igh-essen.com>
parents: 1092
diff changeset
  1458
                    "slave %u.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1459
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1460
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1461
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1462
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1463
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1464
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1465
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1466
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1467
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1468
    ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1469
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1470
    fsm->state = ec_fsm_coe_up_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1471
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1472
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1473
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1474
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1475
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1476
   CoE state: UP RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1477
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1478
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1479
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1480
void ec_fsm_coe_up_response(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1481
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1482
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1483
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1484
    ec_master_t *master = slave->master;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1485
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1486
    size_t rec_size, data_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1487
    ec_sdo_request_t *request = fsm->request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1488
    uint32_t complete_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1489
    unsigned int expedited, size_specified;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1490
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1491
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1492
        return; // FIXME: request again?
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1493
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1494
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1495
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1496
        EC_ERR("Failed to receive CoE upload response datagram for"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
  1497
               " slave %u (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
  1498
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1499
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1500
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1501
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1502
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1503
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1504
        EC_ERR("Reception of CoE upload response failed on slave %u: ",
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1505
                slave->ring_position);
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1506
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1507
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1508
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1509
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1510
    if (!(data = ec_slave_mbox_fetch(slave, datagram,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1511
				     &mbox_prot, &rec_size))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1512
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1513
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1514
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1515
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1516
    if (master->debug_level) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1517
        EC_DBG("Upload response:\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1518
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1519
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1520
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1521
    if (mbox_prot != 0x03) { // CoE
860
ba5c38f0bb30 Minor change: Moved state assign to beginning.
Florian Pose <fp@igh-essen.com>
parents: 859
diff changeset
  1522
        fsm->state = ec_fsm_coe_error;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1523
        EC_WARN("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1524
        return;
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1525
    }
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1526
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1527
    if (ec_fsm_coe_check_emergency(fsm, data, rec_size)) {
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1528
        // check for CoE response again
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1529
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1530
        fsm->retries = EC_FSM_RETRIES;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1531
        fsm->state = ec_fsm_coe_up_check;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1532
        return;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1533
    }
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1534
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1535
    if (rec_size < 3) {
860
ba5c38f0bb30 Minor change: Moved state assign to beginning.
Florian Pose <fp@igh-essen.com>
parents: 859
diff changeset
  1536
        fsm->state = ec_fsm_coe_error;
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1537
        EC_ERR("Received currupted Sdo upload response (%u bytes)!\n", rec_size);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1538
        ec_print_data(data, rec_size);
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1539
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1540
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1541
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1542
    if (EC_READ_U16(data) >> 12 == 0x2 && // Sdo request
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1543
        EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort Sdo transfer request
926
6bb33c6ec770 Output subindices with %02X.
Florian Pose <fp@igh-essen.com>
parents: 895
diff changeset
  1544
        EC_ERR("Sdo upload 0x%04X:%02X aborted on slave %u.\n",
831
ded9519c8d6e Reading of Pdo mapping via CoE during scan.
Florian Pose <fp@igh-essen.com>
parents: 825
diff changeset
  1545
               request->index, request->subindex, slave->ring_position);
1037
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
  1546
        if (rec_size >= 10) {
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
  1547
            request->abort_code = EC_READ_U32(data + 6);
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
  1548
            ec_canopen_abort_msg(request->abort_code);
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
  1549
        } else {
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1550
            EC_ERR("No abort message.\n");
1037
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
  1551
        }
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1552
        fsm->state = ec_fsm_coe_error;
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1553
        return;
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1554
    }
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1555
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1556
    // normal or expedited?
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1557
    expedited = EC_READ_U8(data + 2) & 0x02;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1558
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1559
    if (expedited) {
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1560
        if (rec_size < 7) {
860
ba5c38f0bb30 Minor change: Moved state assign to beginning.
Florian Pose <fp@igh-essen.com>
parents: 859
diff changeset
  1561
            fsm->state = ec_fsm_coe_error;
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1562
            EC_ERR("Received currupted Sdo expedited upload"
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1563
                    " response (only %u bytes)!\n", rec_size);
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1564
            ec_print_data(data, rec_size);
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1565
            return;
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1566
        }
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1567
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1568
        if (EC_READ_U16(data) >> 12 != 0x3 || // Sdo response
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1569
                EC_READ_U8 (data + 2) >> 5 != 0x2 || // upload response
831
ded9519c8d6e Reading of Pdo mapping via CoE during scan.
Florian Pose <fp@igh-essen.com>
parents: 825
diff changeset
  1570
                EC_READ_U16(data + 3) != request->index || // index
ded9519c8d6e Reading of Pdo mapping via CoE during scan.
Florian Pose <fp@igh-essen.com>
parents: 825
diff changeset
  1571
                EC_READ_U8 (data + 5) != request->subindex) { // subindex
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1572
            if (fsm->slave->master->debug_level) {
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1573
                EC_DBG("Invalid Sdo upload expedited response at slave %u!\n",
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1574
                        slave->ring_position);
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1575
                ec_print_data(data, rec_size);
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1576
            }
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1577
            // check for CoE response again
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1578
            ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1579
            fsm->retries = EC_FSM_RETRIES;
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1580
            fsm->state = ec_fsm_coe_up_check;
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1581
            return;
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1582
        }
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1583
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1584
        size_specified = EC_READ_U8(data + 2) & 0x01;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1585
        if (size_specified) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1586
            complete_size = 4 - ((EC_READ_U8(data + 2) & 0x0C) >> 2);
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1587
        } else {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1588
            complete_size = 4;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1589
        }
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1590
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1591
        if (rec_size < 6 + complete_size) {
860
ba5c38f0bb30 Minor change: Moved state assign to beginning.
Florian Pose <fp@igh-essen.com>
parents: 859
diff changeset
  1592
            fsm->state = ec_fsm_coe_error;
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1593
            EC_ERR("Received currupted Sdo expedited upload"
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1594
                    " response (only %u bytes)!\n", rec_size);
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1595
            ec_print_data(data, rec_size);
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1596
            return;
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1597
        }
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1598
854
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1599
        if (ec_sdo_request_copy_data(request, data + 6, complete_size)) {
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1600
            fsm->state = ec_fsm_coe_error;
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1601
            return;
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1602
        }
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1603
    } else { // normal
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1604
        if (rec_size < 10) {
860
ba5c38f0bb30 Minor change: Moved state assign to beginning.
Florian Pose <fp@igh-essen.com>
parents: 859
diff changeset
  1605
            fsm->state = ec_fsm_coe_error;
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1606
            EC_ERR("Received currupted Sdo normal upload"
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1607
                    " response (only %u bytes)!\n", rec_size);
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1608
            ec_print_data(data, rec_size);
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1609
            return;
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1610
        }
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1611
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1612
        if (EC_READ_U16(data) >> 12 != 0x3 || // Sdo response
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1613
                EC_READ_U8 (data + 2) >> 5 != 0x2 || // upload response
831
ded9519c8d6e Reading of Pdo mapping via CoE during scan.
Florian Pose <fp@igh-essen.com>
parents: 825
diff changeset
  1614
                EC_READ_U16(data + 3) != request->index || // index
ded9519c8d6e Reading of Pdo mapping via CoE during scan.
Florian Pose <fp@igh-essen.com>
parents: 825
diff changeset
  1615
                EC_READ_U8 (data + 5) != request->subindex) { // subindex
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1616
            if (fsm->slave->master->debug_level) {
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1617
                EC_DBG("Invalid Sdo normal upload response at slave %u!\n",
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1618
                        slave->ring_position);
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1619
                ec_print_data(data, rec_size);
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1620
            }
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1621
            // check for CoE response again
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1622
            ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1623
            fsm->retries = EC_FSM_RETRIES;
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1624
            fsm->state = ec_fsm_coe_up_check;
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1625
            return;
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1626
        }
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1627
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1628
        data_size = rec_size - 10;
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1629
        complete_size = EC_READ_U32(data + 6);
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1630
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1631
        if (!complete_size) {
860
ba5c38f0bb30 Minor change: Moved state assign to beginning.
Florian Pose <fp@igh-essen.com>
parents: 859
diff changeset
  1632
            fsm->state = ec_fsm_coe_error;
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1633
            EC_ERR("No complete size supplied!\n");
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1634
            ec_print_data(data, rec_size);
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1635
            return;
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1636
        }
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  1637
854
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1638
        if (ec_sdo_request_alloc(request, complete_size)) {
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1639
            fsm->state = ec_fsm_coe_error;
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1640
            return;
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1641
        }
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1642
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1643
        if (ec_sdo_request_copy_data(request, data + 10, data_size)) {
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1644
            fsm->state = ec_fsm_coe_error;
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1645
            return;
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1646
        }
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1647
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1648
        fsm->toggle = 0;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1649
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1650
        if (data_size < complete_size) {
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
  1651
            EC_WARN("Sdo data incomplete (%u / %u).\n",
446
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1652
                    data_size, complete_size);
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1653
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1654
            if (!(data = ec_slave_mbox_prepare_send(slave, datagram,
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1655
                                                    0x03, 3))) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1656
                fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1657
                return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1658
            }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1659
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1660
            EC_WRITE_U16(data, 0x2 << 12); // Sdo request
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1661
            EC_WRITE_U8 (data + 2, (fsm->toggle << 4 // toggle
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1662
                                    | 0x3 << 5)); // upload segment request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1663
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1664
            if (master->debug_level) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1665
                EC_DBG("Upload segment request:\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1666
                ec_print_data(data, 3);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1667
            }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1668
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1669
            fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1670
            fsm->state = ec_fsm_coe_up_seg_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1671
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1672
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1673
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1674
874
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  1675
    if (master->debug_level) {
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  1676
        EC_DBG("Uploaded data:\n");
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  1677
        ec_print_data(request->data, request->data_size);
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  1678
    }
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  1679
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1680
    fsm->state = ec_fsm_coe_end; // success
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1681
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1682
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1683
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1684
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1685
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1686
   CoE state: UP REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1687
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1688
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1689
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1690
void ec_fsm_coe_up_seg_request(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1691
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1692
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1693
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1694
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1695
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1696
        return; // FIXME: check for response first?
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1697
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1698
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1699
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1700
        EC_ERR("Failed to receive CoE upload segment request datagram for"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
  1701
               " slave %u (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
  1702
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1703
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1704
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1705
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1706
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1707
        fsm->state = ec_fsm_coe_error;
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1708
        EC_ERR("Reception of CoE upload segment"
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1709
                " request failed on slave %u: ", slave->ring_position);
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1710
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1711
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1712
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1713
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
  1714
    fsm->jiffies_start = datagram->jiffies_sent;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1715
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1716
    ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1717
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1718
    fsm->state = ec_fsm_coe_up_seg_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1719
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1720
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1721
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1722
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1723
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1724
   CoE state: UP CHECK.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1725
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1726
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1727
void ec_fsm_coe_up_seg_check(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1728
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1729
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1730
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1731
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1732
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1733
        return;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1734
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1735
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1736
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1737
        EC_ERR("Failed to receive CoE mailbox check datagram for slave %u"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
  1738
                " (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
  1739
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1740
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1741
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1742
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1743
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1744
        fsm->state = ec_fsm_coe_error;
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1745
        EC_ERR("Reception of CoE mailbox check"
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1746
                " datagram failed on slave %u: ", slave->ring_position);
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1747
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1748
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1749
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1750
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1751
    if (!ec_slave_mbox_check(datagram)) {
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
  1752
        unsigned long diff_ms =
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
  1753
            (datagram->jiffies_received - fsm->jiffies_start) * 1000 / HZ;
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1754
        if (diff_ms >= fsm->request->response_timeout) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1755
            fsm->state = ec_fsm_coe_error;
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1756
            EC_ERR("Timeout while waiting for Sdo upload segment response "
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1757
                    "on slave %u.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1758
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1759
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1760
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1761
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1762
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1763
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1764
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1765
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1766
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1767
    ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1768
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1769
    fsm->state = ec_fsm_coe_up_seg_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1770
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1771
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1772
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1773
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1774
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1775
   CoE state: UP RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1776
   \todo Timeout behavior
854
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1777
   \todo Check for \a data_size exceeding \a complete_size.
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1778
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1779
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1780
void ec_fsm_coe_up_seg_response(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1781
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1782
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1783
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1784
    ec_master_t *master = slave->master;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1785
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1786
    size_t rec_size, data_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1787
    ec_sdo_request_t *request = fsm->request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1788
    uint32_t seg_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1789
    unsigned int last_segment;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1790
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1791
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
  1792
        return; // FIXME: request again?
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1793
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1794
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1795
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1796
        EC_ERR("Failed to receive CoE upload segment response datagram for"
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
  1797
               " slave %u (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
  1798
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1799
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1800
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1801
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1802
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1803
        fsm->state = ec_fsm_coe_error;
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1804
        EC_ERR("Reception of CoE upload segment"
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1805
                " response failed on slave %u: ", slave->ring_position);
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
  1806
        ec_datagram_print_wc_error(datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1807
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1808
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1809
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1810
    if (!(data = ec_slave_mbox_fetch(slave, datagram,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1811
				     &mbox_prot, &rec_size))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1812
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1813
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1814
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1815
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1816
    if (master->debug_level) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1817
        EC_DBG("Upload segment response:\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1818
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1819
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1820
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1821
    if (mbox_prot != 0x03) { // CoE
446
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1822
        EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1823
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1824
        return;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1825
    }
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1826
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1827
    if (ec_fsm_coe_check_emergency(fsm, data, rec_size)) {
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1828
        // check for CoE response again
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1829
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1830
        fsm->retries = EC_FSM_RETRIES;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1831
        fsm->state = ec_fsm_coe_up_seg_check;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1832
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1833
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1834
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1835
    if (rec_size < 10) {
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1836
        EC_ERR("Received currupted Sdo upload segment response!\n");
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1837
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1838
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1839
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1840
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1841
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1842
    if (EC_READ_U16(data) >> 12 == 0x2 && // Sdo request
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1843
        EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort Sdo transfer request
926
6bb33c6ec770 Output subindices with %02X.
Florian Pose <fp@igh-essen.com>
parents: 895
diff changeset
  1844
        EC_ERR("Sdo upload 0x%04X:%02X aborted on slave %u.\n",
831
ded9519c8d6e Reading of Pdo mapping via CoE during scan.
Florian Pose <fp@igh-essen.com>
parents: 825
diff changeset
  1845
               request->index, request->subindex, slave->ring_position);
1037
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
  1846
        request->abort_code = EC_READ_U32(data + 6);
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
  1847
        ec_canopen_abort_msg(request->abort_code);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1848
        fsm->state = ec_fsm_coe_error;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1849
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1850
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1851
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1852
    if (EC_READ_U16(data) >> 12 != 0x3 || // Sdo response
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1853
        EC_READ_U8 (data + 2) >> 5 != 0x0) { // upload segment response
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1854
        if (fsm->slave->master->debug_level) {
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1855
            EC_DBG("Invalid Sdo upload segment response at slave %u!\n",
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1856
               slave->ring_position);
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1857
            ec_print_data(data, rec_size);
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1858
        }
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1859
        // check for CoE response again
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1860
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1861
        fsm->retries = EC_FSM_RETRIES;
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1862
        fsm->state = ec_fsm_coe_up_seg_check;
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1863
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1864
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1865
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1866
    last_segment = EC_READ_U8(data + 2) & 0x01;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1867
    seg_size = (EC_READ_U8(data + 2) & 0xE) >> 1;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1868
    data_size = rec_size - 10;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1869
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1870
    if (data_size != seg_size) {
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 975
diff changeset
  1871
        EC_WARN("Sdo segment data invalid (%u / %u)"
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1872
                " - Fragmenting not implemented.\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1873
                data_size, seg_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1874
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1875
854
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1876
    memcpy(request->data + request->data_size, data + 10, data_size);
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  1877
    request->data_size += data_size;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1878
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1879
    if (!last_segment) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1880
        fsm->toggle = !fsm->toggle;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1881
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1882
        if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 3))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1883
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1884
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1885
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1886
814
a51f857b1b2d Renamed [PS]DO to [PS]do.
Florian Pose <fp@igh-essen.com>
parents: 769
diff changeset
  1887
        EC_WRITE_U16(data, 0x2 << 12); // Sdo request
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1888
        EC_WRITE_U8 (data + 2, (fsm->toggle << 4 // toggle
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1889
                                | 0x3 << 5)); // upload segment request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1890
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1891
        if (master->debug_level) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1892
            EC_DBG("Upload segment request:\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1893
            ec_print_data(data, 3);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1894
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1895
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1896
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1897
        fsm->state = ec_fsm_coe_up_seg_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1898
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1899
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1900
874
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  1901
    if (master->debug_level) {
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  1902
        EC_DBG("Uploaded data:\n");
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  1903
        ec_print_data(request->data, request->data_size);
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  1904
    }
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  1905
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1906
    fsm->state = ec_fsm_coe_end; // success
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1907
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1908
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1909
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1910
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1911
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1912
   State: ERROR.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1913
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1914
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1915
void ec_fsm_coe_error(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1916
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1917
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1918
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1919
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1920
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1921
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1922
   State: END.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1923
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1924
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1925
void ec_fsm_coe_end(ec_fsm_coe_t *fsm /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1926
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1927
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1928
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1929
/*****************************************************************************/