master/fsm_coe.c
author Florian Pose <fp@igh-essen.com>
Tue, 12 Feb 2013 11:52:03 +0100
branchstable-1.5
changeset 2518 e5640f5e10a2
parent 2498 9cdd7669dc0b
child 2522 ec403cf308eb
permissions -rw-r--r--
TTY: termios structure was included in 'struct tty' with kernel 3.7.
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
 *
1326
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1313
diff changeset
     5
 *  Copyright (C) 2006-2008  Florian Pose, Ingenieurgemeinschaft IgH
436
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
 *
1326
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1313
diff changeset
     9
 *  The IgH EtherCAT Master is free software; you can redistribute it and/or
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1313
diff changeset
    10
 *  modify it under the terms of the GNU General Public License version 2, as
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1313
diff changeset
    11
 *  published by the Free Software Foundation.
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *
1326
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1313
diff changeset
    13
 *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1313
diff changeset
    14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1313
diff changeset
    15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1313
diff changeset
    16
 *  Public License for more details.
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *
1326
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1313
diff changeset
    18
 *  You should have received a copy of the GNU General Public License along
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1313
diff changeset
    19
 *  with the IgH EtherCAT Master; if not, write to the Free Software
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *  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
    21
 *
1363
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1339
diff changeset
    22
 *  ---
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1339
diff changeset
    23
 *
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1339
diff changeset
    24
 *  The license mentioned above concerns the source code only. Using the
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1339
diff changeset
    25
 *  EtherCAT technology and brand is only permitted in compliance with the
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1339
diff changeset
    26
 *  industrial property and similar rights of Beckhoff Automation GmbH.
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
 *
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 *****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    30
/** \file EtherCAT CoE state machines.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    31
 */
436
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
#include "globals.h"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#include "master.h"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
#include "mailbox.h"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
#include "fsm_coe.h"
2438
9c3e629a220c Added API for querying CoE emergency requests.
Florian Pose <fp@igh-essen.com>
parents: 2197
diff changeset
    39
#include "slave_config.h"
436
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
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
    43
/** 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
    44
 */
1794
94e0e1756dec Reduced CoE timeouts to 1000 ms.
Florian Pose <fp@igh-essen.com>
parents: 1793
diff changeset
    45
#define EC_FSM_COE_DICT_TIMEOUT 1000
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
    46
1907
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    47
/** CoE download request header size.
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    48
 */
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    49
#define EC_COE_DOWN_REQ_HEADER_SIZE 10
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    50
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    51
/** CoE download segment request header size.
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    52
 */
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    53
#define EC_COE_DOWN_SEG_REQ_HEADER_SIZE 3
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    54
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    55
/** Minimum size of download segment.
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    56
 */
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    57
#define EC_COE_DOWN_SEG_MIN_DATA_SIZE 7
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    58
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    59
/** Enable debug output for CoE retries.
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    60
 */
1791
27030c6b2de3 Do not output CoE retries.
Florian Pose <fp@igh-essen.com>
parents: 1563
diff changeset
    61
#define DEBUG_RETRIES 0
1907
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    62
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    63
/** Enable warning output if transfers take too long.
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
    64
 */
1792
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
    65
#define DEBUG_LONG 0
1791
27030c6b2de3 Do not output CoE retries.
Florian Pose <fp@igh-essen.com>
parents: 1563
diff changeset
    66
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
    67
/*****************************************************************************/
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
    68
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    69
void ec_fsm_coe_dict_start(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    70
void ec_fsm_coe_dict_request(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    71
void ec_fsm_coe_dict_check(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    72
void ec_fsm_coe_dict_response(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    73
void ec_fsm_coe_dict_desc_request(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    74
void ec_fsm_coe_dict_desc_check(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    75
void ec_fsm_coe_dict_desc_response(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    76
void ec_fsm_coe_dict_entry_request(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    77
void ec_fsm_coe_dict_entry_check(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    78
void ec_fsm_coe_dict_entry_response(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    79
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    80
void ec_fsm_coe_down_start(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    81
void ec_fsm_coe_down_request(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    82
void ec_fsm_coe_down_check(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    83
void ec_fsm_coe_down_response(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    84
void ec_fsm_coe_down_seg_check(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    85
void ec_fsm_coe_down_seg_response(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    86
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    87
void ec_fsm_coe_up_start(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    88
void ec_fsm_coe_up_request(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    89
void ec_fsm_coe_up_check(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    90
void ec_fsm_coe_up_response(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    91
void ec_fsm_coe_up_seg_request(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    92
void ec_fsm_coe_up_seg_check(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    93
void ec_fsm_coe_up_seg_response(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    94
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    95
void ec_fsm_coe_end(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    96
void ec_fsm_coe_error(ec_fsm_coe_t *, ec_datagram_t *);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    97
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    98
/*****************************************************************************/
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
    99
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   100
/** SDO abort messages.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   101
 *
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   102
 * The "abort SDO transfer request" supplies an abort code, which can be
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   103
 * translated to clear text. This table does the mapping of the codes and
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   104
 * messages.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   105
 */
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
const ec_code_msg_t sdo_abort_messages[] = {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
    {0x05030000, "Toggle bit not changed"},
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   108
    {0x05040000, "SDO protocol timeout"},
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
    {0x05040001, "Client/Server command specifier not valid or unknown"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
    {0x05040005, "Out of memory"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
    {0x06010000, "Unsupported access to an object"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
    {0x06010001, "Attempt to read a write-only object"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
    {0x06010002, "Attempt to write a read-only object"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
    {0x06020000, "This object does not exist in the object directory"},
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   115
    {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
   116
    {0x06040042, "The number and length of the objects to be mapped would"
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   117
     " exceed the PDO length"},
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
    {0x06040043, "General parameter incompatibility reason"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
    {0x06040047, "Gerneral internal incompatibility in device"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
    {0x06060000, "Access failure due to a hardware error"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
    {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
   122
     " not match"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
    {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
   124
     " high"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
    {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
   126
     " low"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
    {0x06090011, "Subindex does not exist"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
    {0x06090030, "Value range of parameter exceeded"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
    {0x06090031, "Value of parameter written too high"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
    {0x06090032, "Value of parameter written too low"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
    {0x06090036, "Maximum value is less than minimum value"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
    {0x08000000, "General error"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
    {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
   134
    {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
   135
     " because of local control"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
    {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
   137
     " because of the present device state"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
    {0x08000023, "Object dictionary dynamic generation fails or no object"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
     " dictionary is present"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
    {}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
};
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
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   145
/** Outputs an SDO abort message.
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   146
 */
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   147
void ec_canopen_abort_msg(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   148
        const ec_slave_t *slave, /**< Slave. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   149
        uint32_t abort_code /**< Abort code to search for. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   150
        )
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
    const ec_code_msg_t *abort_msg;
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
    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
   155
        if (abort_msg->code == abort_code) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   156
            EC_SLAVE_ERR(slave, "SDO abort message 0x%08X: \"%s\".\n",
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   157
                   abort_msg->code, abort_msg->message);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   158
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
        }
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
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   162
    EC_SLAVE_ERR(slave, "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
   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
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   166
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   167
/** Constructor.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   168
 */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   169
void ec_fsm_coe_init(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   170
        ec_fsm_coe_t *fsm /**< Finite state machine */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   171
        )
436
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
    fsm->state = NULL;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   174
    fsm->datagram = NULL;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   175
}
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   176
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   177
/*****************************************************************************/
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   178
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   179
/** Destructor.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   180
 */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   181
void ec_fsm_coe_clear(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   182
        ec_fsm_coe_t *fsm /**< Finite state machine */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   183
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   184
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   185
}
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   186
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   187
/*****************************************************************************/
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   188
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   189
/** Starts reading a slaves' SDO dictionary.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   190
 */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   191
void ec_fsm_coe_dictionary(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   192
        ec_fsm_coe_t *fsm, /**< Finite state machine */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   193
        ec_slave_t *slave /**< EtherCAT slave */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   194
        )
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
    fsm->slave = slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
    fsm->state = ec_fsm_coe_dict_start;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   198
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   199
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   202
/** Starts to transfer an SDO to/from a slave.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   203
 */
859
233e32f428e1 Replaced ec_fsm_coe_\(upload\|download\)() with ec_fsm_coe_transfer();
Florian Pose <fp@igh-essen.com>
parents: 854
diff changeset
   204
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
   205
        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
   206
        ec_slave_t *slave, /**< EtherCAT slave. */
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   207
        ec_sdo_request_t *request /**< SDO request. */
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
   208
        )
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
    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
   211
    fsm->request = request;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   212
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   213
    if (request->dir == EC_DIR_OUTPUT) {
859
233e32f428e1 Replaced ec_fsm_coe_\(upload\|download\)() with ec_fsm_coe_transfer();
Florian Pose <fp@igh-essen.com>
parents: 854
diff changeset
   214
        fsm->state = ec_fsm_coe_down_start;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   215
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   216
    else {
859
233e32f428e1 Replaced ec_fsm_coe_\(upload\|download\)() with ec_fsm_coe_transfer();
Florian Pose <fp@igh-essen.com>
parents: 854
diff changeset
   217
        fsm->state = ec_fsm_coe_up_start;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   218
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   219
}
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   220
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   221
/*****************************************************************************/
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   222
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   223
/** Executes the current state of the state machine.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   224
 *
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   225
 * \return 1 if the datagram was used, else 0.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   226
 */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   227
int ec_fsm_coe_exec(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   228
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   229
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   230
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   231
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   232
    int datagram_used = 0;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   233
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   234
    if (fsm->datagram &&
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   235
            (fsm->datagram->state == EC_DATAGRAM_INIT ||
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   236
             fsm->datagram->state == EC_DATAGRAM_QUEUED ||
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   237
             fsm->datagram->state == EC_DATAGRAM_SENT)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   238
        // datagram not received yet
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   239
        return datagram_used;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   240
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   241
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   242
    fsm->state(fsm, datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   243
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   244
    datagram_used =
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   245
        fsm->state != ec_fsm_coe_end && fsm->state != ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   246
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   247
    if (datagram_used) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   248
        fsm->datagram = datagram;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   249
    } else {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   250
        fsm->datagram = NULL;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   251
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   252
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   253
    return datagram_used;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   254
}
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   255
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   256
/*****************************************************************************/
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   257
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   258
/** Returns, if the state machine terminated with success.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   259
 * \return non-zero if successful.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   260
 */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   261
int ec_fsm_coe_success(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   262
        const ec_fsm_coe_t *fsm /**< Finite state machine */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
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
    return fsm->state == ec_fsm_coe_end;
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
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   268
/*****************************************************************************/
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   269
1092
69393cf60399 Improved source code doc.
Florian Pose <fp@igh-essen.com>
parents: 1053
diff changeset
   270
/** 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
   271
 *
69393cf60399 Improved source code doc.
Florian Pose <fp@igh-essen.com>
parents: 1053
diff changeset
   272
 * 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
   273
 *
69393cf60399 Improved source code doc.
Florian Pose <fp@igh-essen.com>
parents: 1053
diff changeset
   274
 * \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
   275
 */
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   276
int ec_fsm_coe_check_emergency(
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   277
        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
   278
        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
   279
        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
   280
        )
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   281
{
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   282
    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
   283
        return 0;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   284
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   285
    if (size < 10) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   286
        EC_SLAVE_WARN(fsm->slave, "Received incomplete CoE Emergency"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   287
                " request:\n");
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   288
        ec_print_data(data, size);
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   289
        return 1;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   290
    }
2196
6b481c7b3b34 Allow CoE OD List Segments to have 6 bytes instead of 8.
Florian Pose <fp@igh-essen.com>
parents: 1948
diff changeset
   291
2438
9c3e629a220c Added API for querying CoE emergency requests.
Florian Pose <fp@igh-essen.com>
parents: 2197
diff changeset
   292
    {
9c3e629a220c Added API for querying CoE emergency requests.
Florian Pose <fp@igh-essen.com>
parents: 2197
diff changeset
   293
        ec_slave_config_t *sc = fsm->slave->config;
9c3e629a220c Added API for querying CoE emergency requests.
Florian Pose <fp@igh-essen.com>
parents: 2197
diff changeset
   294
        if (sc) {
9c3e629a220c Added API for querying CoE emergency requests.
Florian Pose <fp@igh-essen.com>
parents: 2197
diff changeset
   295
            ec_coe_emerg_ring_push(&sc->emerg_ring, data + 2);
9c3e629a220c Added API for querying CoE emergency requests.
Florian Pose <fp@igh-essen.com>
parents: 2197
diff changeset
   296
        }
9c3e629a220c Added API for querying CoE emergency requests.
Florian Pose <fp@igh-essen.com>
parents: 2197
diff changeset
   297
    }
9c3e629a220c Added API for querying CoE emergency requests.
Florian Pose <fp@igh-essen.com>
parents: 2197
diff changeset
   298
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   299
    EC_SLAVE_WARN(fsm->slave, "CoE Emergency Request received:\n"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   300
            "Error code 0x%04X, Error register 0x%02X, data:\n",
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   301
            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
   302
    ec_print_data(data + 5, 5);
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   303
    return 1;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   304
}
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   305
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   306
/******************************************************************************
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   307
 *  CoE dictionary state machine
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   308
 *****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   309
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   310
int ec_fsm_coe_prepare_dict(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   311
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   312
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   313
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   314
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   315
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   316
    uint8_t *data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8);
1313
ed15eef57d5c Improved error case return codes of many functions.
Florian Pose <fp@igh-essen.com>
parents: 1310
diff changeset
   317
    if (IS_ERR(data)) {
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   318
        return PTR_ERR(data);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   319
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   320
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   321
    EC_WRITE_U16(data, 0x8 << 12); // SDO information
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   322
    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
   323
    EC_WRITE_U8 (data + 3, 0x00);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   324
    EC_WRITE_U16(data + 4, 0x0000);
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   325
    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
   326
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   327
    fsm->state = ec_fsm_coe_dict_request;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   328
    return 0;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   329
}
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   330
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   331
/*****************************************************************************/
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   332
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   333
/** CoE state: DICT START.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   334
 */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   335
void ec_fsm_coe_dict_start(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   336
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   337
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   338
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   339
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   340
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   341
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   342
    if (!(slave->sii.mailbox_protocols & EC_MBOX_COE)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   343
        EC_SLAVE_ERR(slave, "Slave does not support CoE!\n");
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   344
        fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   345
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   346
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   347
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   348
    if (slave->sii.has_general && !slave->sii.coe_details.enable_sdo_info) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   349
        EC_SLAVE_ERR(slave, "Slave does not support"
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   350
                " SDO information service!\n");
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   351
        fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   352
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   353
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   354
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   355
    fsm->retries = EC_FSM_RETRIES;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   356
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   357
    if (ec_fsm_coe_prepare_dict(fsm, datagram)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   358
        fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   359
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   360
}
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   361
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   362
/*****************************************************************************/
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   363
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   364
/** CoE state: DICT REQUEST.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   365
 * \todo Timeout behavior
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   366
 */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   367
void ec_fsm_coe_dict_request(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   368
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   369
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   370
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   371
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   372
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   373
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   374
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   375
        if (ec_fsm_coe_prepare_dict(fsm, datagram)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   376
            fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   377
        }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   378
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   379
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   380
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   381
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   382
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   383
        EC_SLAVE_ERR(slave, "Failed to receive CoE dictionary"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   384
                " request datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   385
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   386
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   387
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   388
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   389
    if (fsm->datagram->working_counter != 1) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   390
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   391
        EC_SLAVE_ERR(slave, "Reception of CoE dictionary request failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   392
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   393
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   394
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   395
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   396
    fsm->jiffies_start = fsm->datagram->jiffies_sent;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   397
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   398
    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
   399
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   400
    fsm->state = ec_fsm_coe_dict_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   401
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   402
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   403
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   404
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   405
/** CoE state: DICT CHECK.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   406
 */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   407
void ec_fsm_coe_dict_check(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   408
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   409
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   410
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   411
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   412
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   413
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   414
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   415
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   416
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   417
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   418
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   419
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   420
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   421
        EC_SLAVE_ERR(slave, "Failed to receive CoE mailbox check datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   422
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   423
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   424
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   425
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   426
    if (fsm->datagram->working_counter != 1) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   427
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   428
        EC_SLAVE_ERR(slave,"Reception of CoE mailbox check"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   429
                " datagram failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   430
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   431
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   432
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   433
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   434
    if (!ec_slave_mbox_check(fsm->datagram)) {
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
   435
        unsigned long diff_ms =
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   436
            (fsm->datagram->jiffies_received - fsm->jiffies_start) *
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   437
            1000 / HZ;
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   438
        if (diff_ms >= EC_FSM_COE_DICT_TIMEOUT) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   439
            fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   440
            EC_SLAVE_ERR(slave, "Timeout while waiting for"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   441
                    " SDO dictionary list response.\n");
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   442
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   443
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   444
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   445
        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
   446
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   447
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   448
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   449
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   450
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   451
    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
   452
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   453
    fsm->state = ec_fsm_coe_dict_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   454
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   455
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   456
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   457
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   458
int ec_fsm_coe_dict_prepare_desc(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   459
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   460
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   461
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   462
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   463
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   464
    u8 *data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   465
    if (IS_ERR(data)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   466
        return PTR_ERR(data);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   467
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   468
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   469
    EC_WRITE_U16(data, 0x8 << 12); // SDO information
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   470
    EC_WRITE_U8 (data + 2, 0x03); // Get object description request
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   471
    EC_WRITE_U8 (data + 3, 0x00);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   472
    EC_WRITE_U16(data + 4, 0x0000);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   473
    EC_WRITE_U16(data + 6, fsm->sdo->index); // SDO index
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   474
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   475
    fsm->state = ec_fsm_coe_dict_desc_request;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   476
    return 0;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   477
}
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   478
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   479
/*****************************************************************************/
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   480
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   481
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   482
   CoE state: DICT RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   483
   \todo Timeout behavior
436
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
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   486
void ec_fsm_coe_dict_response(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   487
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   488
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   489
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   490
{
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   491
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   492
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   493
    size_t rec_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   494
    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
   495
    uint16_t sdo_index, fragments_left;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   496
    ec_sdo_t *sdo;
2196
6b481c7b3b34 Allow CoE OD List Segments to have 6 bytes instead of 8.
Florian Pose <fp@igh-essen.com>
parents: 1948
diff changeset
   497
    bool first_segment;
6b481c7b3b34 Allow CoE OD List Segments to have 6 bytes instead of 8.
Florian Pose <fp@igh-essen.com>
parents: 1948
diff changeset
   498
    size_t index_list_offset;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   499
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   500
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   501
        ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   502
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   503
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   504
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   505
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   506
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   507
        EC_SLAVE_ERR(slave, "Failed to receive CoE dictionary"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   508
                " response datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   509
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   510
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   511
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   512
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   513
    if (fsm->datagram->working_counter != 1) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   514
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   515
        EC_SLAVE_ERR(slave, "Reception of CoE dictionary response failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   516
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   517
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   518
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   519
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   520
    data = ec_slave_mbox_fetch(slave, fsm->datagram, &mbox_prot, &rec_size);
1313
ed15eef57d5c Improved error case return codes of many functions.
Florian Pose <fp@igh-essen.com>
parents: 1310
diff changeset
   521
    if (IS_ERR(data)) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   522
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   523
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   524
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   525
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   526
    if (mbox_prot != 0x03) { // CoE
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   527
        EC_SLAVE_ERR(slave, "Received mailbox protocol 0x%02X as response.\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   528
                mbox_prot);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   529
        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
   530
        return;
436
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
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   533
    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
   534
        // check for CoE response again
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   535
        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
   536
        fsm->retries = EC_FSM_RETRIES;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   537
        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
   538
        return;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   539
    }
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   540
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
   541
    if (rec_size < 3) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   542
        EC_SLAVE_ERR(slave, "Received corrupted SDO dictionary response"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   543
                " (size %zu).\n", 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
   544
        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
   545
        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
   546
    }
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   547
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   548
    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
   549
        (EC_READ_U8(data + 2) & 0x7F) == 0x07) { // error response
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   550
        EC_SLAVE_ERR(slave, "SDO information error response!\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
   551
        if (rec_size < 10) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   552
            EC_SLAVE_ERR(slave, "Incomplete SDO information"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   553
                    " error response:\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
   554
            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
   555
        } else {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   556
            ec_canopen_abort_msg(slave, EC_READ_U32(data + 6));
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
   557
        }
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   558
        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
   559
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   560
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   561
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   562
    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
   563
        (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
   564
        if (fsm->slave->master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   565
            EC_SLAVE_DBG(slave, 1, "Invalid SDO list response!"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   566
                    " Retrying...\n");
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   567
            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
   568
        }
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   569
        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
   570
        fsm->retries = EC_FSM_RETRIES;
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   571
        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
   572
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   573
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   574
2196
6b481c7b3b34 Allow CoE OD List Segments to have 6 bytes instead of 8.
Florian Pose <fp@igh-essen.com>
parents: 1948
diff changeset
   575
    first_segment = list_empty(&slave->sdo_dictionary) ? true : false;
6b481c7b3b34 Allow CoE OD List Segments to have 6 bytes instead of 8.
Florian Pose <fp@igh-essen.com>
parents: 1948
diff changeset
   576
    index_list_offset = first_segment ? 8 : 6;
6b481c7b3b34 Allow CoE OD List Segments to have 6 bytes instead of 8.
Florian Pose <fp@igh-essen.com>
parents: 1948
diff changeset
   577
6b481c7b3b34 Allow CoE OD List Segments to have 6 bytes instead of 8.
Florian Pose <fp@igh-essen.com>
parents: 1948
diff changeset
   578
    if (rec_size < index_list_offset || rec_size % 2) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   579
        EC_SLAVE_ERR(slave, "Invalid data size %zu!\n", rec_size);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   580
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   581
        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
   582
        return;
436
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
2196
6b481c7b3b34 Allow CoE OD List Segments to have 6 bytes instead of 8.
Florian Pose <fp@igh-essen.com>
parents: 1948
diff changeset
   585
    sdo_count = (rec_size - index_list_offset) / 2;
436
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
    for (i = 0; i < sdo_count; i++) {
2196
6b481c7b3b34 Allow CoE OD List Segments to have 6 bytes instead of 8.
Florian Pose <fp@igh-essen.com>
parents: 1948
diff changeset
   588
        sdo_index = EC_READ_U16(data + index_list_offset + i * 2);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   589
        if (!sdo_index) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   590
            EC_SLAVE_DBG(slave, 1, "SDO dictionary contains index 0x0000.\n");
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   591
            continue;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   592
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   593
998
05a24788f3b2 Replaced a few GFP_ATOMIC allocations with GFP_KERNEL ones.
Florian Pose <fp@igh-essen.com>
parents: 992
diff changeset
   594
        if (!(sdo = (ec_sdo_t *) kmalloc(sizeof(ec_sdo_t), GFP_KERNEL))) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   595
            EC_SLAVE_ERR(slave, "Failed to allocate memory for SDO!\n");
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   596
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   597
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   598
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   599
992
50a44cbd30af Removed kobjects from sdo and slave.
Florian Pose <fp@igh-essen.com>
parents: 991
diff changeset
   600
        ec_sdo_init(sdo, slave, sdo_index);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   601
        list_add_tail(&sdo->list, &slave->sdo_dictionary);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   602
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   603
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
   604
    fragments_left = EC_READ_U16(data + 4);
1923
Florian Pose <fp@igh-essen.com>
parents: 1921
diff changeset
   605
    if (fragments_left) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   606
        EC_SLAVE_DBG(slave, 1, "SDO list fragments left: %u\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   607
                fragments_left);
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   608
    }
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   609
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   610
    if (EC_READ_U8(data + 2) & 0x80 || fragments_left) {
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   611
        // more messages waiting. check again.
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   612
        fsm->jiffies_start = fsm->datagram->jiffies_sent;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   613
        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
   614
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   615
        fsm->state = ec_fsm_coe_dict_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   616
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   617
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   618
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   619
    if (list_empty(&slave->sdo_dictionary)) {
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   620
        // no SDOs in dictionary. finished.
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   621
        fsm->state = ec_fsm_coe_end; // success
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   622
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   623
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   624
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   625
    // fetch SDO descriptions
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   626
    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
   627
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;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   629
    if (ec_fsm_coe_dict_prepare_desc(fsm, datagram)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   630
        fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   631
    }
436
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   636
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   637
   CoE state: DICT DESC REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   638
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   639
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   640
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   641
void ec_fsm_coe_dict_desc_request(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   642
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   643
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   644
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   645
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   646
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   647
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   648
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   649
        if (ec_fsm_coe_dict_prepare_desc(fsm, datagram)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   650
            fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   651
        }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   652
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   653
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   654
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   655
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   656
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   657
        EC_SLAVE_ERR(slave, "Failed to receive CoE SDO"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   658
                " description request datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   659
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   660
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   661
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   662
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   663
    if (fsm->datagram->working_counter != 1) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   664
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   665
        EC_SLAVE_ERR(slave, "Reception of CoE SDO description"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   666
                " request failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   667
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   668
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   669
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   670
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   671
    fsm->jiffies_start = fsm->datagram->jiffies_sent;
436
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
    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
   674
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   675
    fsm->state = ec_fsm_coe_dict_desc_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   676
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   677
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   678
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   679
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   680
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   681
   CoE state: DICT DESC CHECK.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   682
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   683
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   684
void ec_fsm_coe_dict_desc_check(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   685
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   686
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   687
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   688
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   689
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   690
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   691
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   692
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   693
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   694
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   695
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   696
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   697
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   698
        EC_SLAVE_ERR(slave, "Failed to receive CoE mailbox check datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   699
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   700
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   701
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   702
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   703
    if (fsm->datagram->working_counter != 1) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   704
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   705
        EC_SLAVE_ERR(slave, "Reception of CoE mailbox check"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   706
                " datagram failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   707
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   708
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   709
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   710
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   711
    if (!ec_slave_mbox_check(fsm->datagram)) {
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
   712
        unsigned long diff_ms =
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   713
            (fsm->datagram->jiffies_received - fsm->jiffies_start) *
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   714
            1000 / HZ;
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   715
        if (diff_ms >= EC_FSM_COE_DICT_TIMEOUT) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   716
            fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   717
            EC_SLAVE_ERR(slave, "Timeout while waiting for"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   718
                    " SDO 0x%04x object description response.\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   719
                    fsm->sdo->index);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   720
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   721
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   722
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   723
        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
   724
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   725
        return;
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   728
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   729
    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
   730
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   731
    fsm->state = ec_fsm_coe_dict_desc_response;
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   734
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   735
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   736
int ec_fsm_coe_dict_prepare_entry(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   737
        ec_fsm_coe_t *fsm, /**< Finite state machine */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   738
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   739
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   740
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   741
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   742
    u8 *data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   743
    if (IS_ERR(data)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   744
        return PTR_ERR(data);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   745
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   746
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   747
    EC_WRITE_U16(data, 0x8 << 12); // SDO information
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   748
    EC_WRITE_U8 (data + 2, 0x05); // Get entry description request
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   749
    EC_WRITE_U8 (data + 3, 0x00);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   750
    EC_WRITE_U16(data + 4, 0x0000);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   751
    EC_WRITE_U16(data + 6, fsm->sdo->index); // SDO index
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   752
    EC_WRITE_U8 (data + 8, fsm->subindex); // SDO subindex
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   753
    EC_WRITE_U8 (data + 9, 0x01); // value info (access rights only)
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   754
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   755
    fsm->state = ec_fsm_coe_dict_entry_request;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   756
    return 0;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   757
}
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   758
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   759
/*****************************************************************************/
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   760
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   761
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   762
   CoE state: DICT DESC RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   763
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   764
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   765
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   766
void ec_fsm_coe_dict_desc_response(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   767
        ec_fsm_coe_t *fsm, /**< Finite state machine */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   768
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   769
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   770
{
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   771
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   772
    ec_sdo_t *sdo = fsm->sdo;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   773
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   774
    size_t rec_size, name_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   775
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   776
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   777
        ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   778
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   779
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   780
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   781
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   782
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   783
        EC_SLAVE_ERR(slave, "Failed to receive CoE SDO description"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   784
                " response datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   785
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   786
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   787
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   788
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   789
    if (fsm->datagram->working_counter != 1) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   790
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   791
        EC_SLAVE_ERR(slave, "Reception of CoE SDO description"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   792
                " response failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   793
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   794
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   795
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   796
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   797
    data = ec_slave_mbox_fetch(slave, fsm->datagram, &mbox_prot, &rec_size);
1313
ed15eef57d5c Improved error case return codes of many functions.
Florian Pose <fp@igh-essen.com>
parents: 1310
diff changeset
   798
    if (IS_ERR(data)) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   799
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   800
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   801
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   802
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   803
    if (mbox_prot != 0x03) { // CoE
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   804
        EC_SLAVE_ERR(slave, "Received mailbox protocol 0x%02X as response.\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   805
                mbox_prot);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   806
        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
   807
        return;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   808
    }
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   809
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   810
    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
   811
        // check for CoE response again
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   812
        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
   813
        fsm->retries = EC_FSM_RETRIES;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
   814
        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
   815
        return;
436
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
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
   818
    if (rec_size < 3) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   819
        EC_SLAVE_ERR(slave, "Received corrupted SDO description response"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   820
                " (size %zu).\n", 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
   821
        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
   822
        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
   823
    }
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   824
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   825
    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
   826
        (EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   827
        EC_SLAVE_ERR(slave, "SDO information error response while"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   828
                " fetching SDO 0x%04X!\n", sdo->index);
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   829
        ec_canopen_abort_msg(slave, EC_READ_U32(data + 6));
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   830
        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
   831
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   832
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   833
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
   834
    if (rec_size < 8) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   835
        EC_SLAVE_ERR(slave, "Received corrupted SDO"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   836
                " description response (size %zu).\n", 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
   837
        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
   838
        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
   839
    }
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
   840
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   841
    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
   842
        (EC_READ_U8 (data + 2) & 0x7F) != 0x04 || // Object desc. response
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   843
        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
   844
        if (fsm->slave->master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   845
            EC_SLAVE_DBG(slave, 1, "Invalid object description response while"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   846
                    " fetching SDO 0x%04X!\n", sdo->index);
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   847
            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
   848
        }
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   849
        // check for CoE response again
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   850
        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
   851
        fsm->retries = EC_FSM_RETRIES;
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   852
        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
   853
        return;
436
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
    if (rec_size < 12) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   857
        EC_SLAVE_ERR(slave, "Invalid data size!\n");
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   858
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   859
        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
   860
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   861
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   862
962
ea83a7aab57a Renamed subindices to max_subindex.
Florian Pose <fp@igh-essen.com>
parents: 926
diff changeset
   863
    sdo->max_subindex = EC_READ_U8(data + 10);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   864
    sdo->object_code = EC_READ_U8(data + 11);
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
    name_size = rec_size - 12;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   867
    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
   868
        if (!(sdo->name = kmalloc(name_size + 1, GFP_KERNEL))) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   869
            EC_SLAVE_ERR(slave, "Failed to allocate SDO name!\n");
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   870
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   871
            return;
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
        memcpy(sdo->name, data + 12, name_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   875
        sdo->name[name_size] = 0;
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   878
    if (EC_READ_U8(data + 2) & 0x80) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   879
        EC_SLAVE_ERR(slave, "Fragment follows (not implemented)!\n");
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   880
        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
   881
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   882
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   883
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   884
    // start fetching entries
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   885
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   886
    fsm->subindex = 0;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   887
    fsm->retries = EC_FSM_RETRIES;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   888
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   889
    if (ec_fsm_coe_dict_prepare_entry(fsm, datagram)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   890
        fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   891
    }
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   892
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   893
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   894
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   895
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   896
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   897
   CoE state: DICT ENTRY REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   898
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   899
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   900
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   901
void ec_fsm_coe_dict_entry_request(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   902
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   903
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   904
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   905
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   906
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   907
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   908
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   909
        if (ec_fsm_coe_dict_prepare_entry(fsm, datagram)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   910
            fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   911
        }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   912
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   913
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   914
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   915
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   916
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   917
        EC_SLAVE_ERR(slave, "Failed to receive CoE SDO entry"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   918
                " request datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   919
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   920
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   921
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   922
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   923
    if (fsm->datagram->working_counter != 1) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   924
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   925
        EC_SLAVE_ERR(slave, "Reception of CoE SDO entry request failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   926
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   927
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   928
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   929
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   930
    fsm->jiffies_start = fsm->datagram->jiffies_sent;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   931
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   932
    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
   933
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   934
    fsm->state = ec_fsm_coe_dict_entry_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   935
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   936
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   937
/*****************************************************************************/
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
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   940
   CoE state: DICT ENTRY CHECK.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   941
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   942
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   943
void ec_fsm_coe_dict_entry_check(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   944
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   945
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   946
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   947
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   948
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   949
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   950
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   951
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   952
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   953
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   954
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   955
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   956
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   957
        EC_SLAVE_ERR(slave, "Failed to receive CoE mailbox check datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   958
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   959
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   960
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   961
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   962
    if (fsm->datagram->working_counter != 1) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   963
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   964
        EC_SLAVE_ERR(slave, "Reception of CoE mailbox check"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   965
                " datagram failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   966
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   967
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   968
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   969
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   970
    if (!ec_slave_mbox_check(fsm->datagram)) {
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
   971
        unsigned long diff_ms =
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   972
            (fsm->datagram->jiffies_received - fsm->jiffies_start) *
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
   973
            1000 / HZ;
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
   974
        if (diff_ms >= EC_FSM_COE_DICT_TIMEOUT) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   975
            fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   976
            EC_SLAVE_ERR(slave, "Timeout while waiting for"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   977
                    " SDO entry 0x%04x:%x description response.\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
   978
                    fsm->sdo->index, fsm->subindex);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   979
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   980
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   981
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   982
        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
   983
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   984
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   985
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   986
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   987
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   988
    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
   989
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   990
    fsm->state = ec_fsm_coe_dict_entry_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   991
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   992
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   993
/*****************************************************************************/
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
   CoE state: DICT ENTRY RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   997
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   998
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   999
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1000
void ec_fsm_coe_dict_entry_response(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1001
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1002
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1003
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1004
{
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1005
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1006
    ec_sdo_t *sdo = fsm->sdo;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1007
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1008
    size_t rec_size, data_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1009
    ec_sdo_entry_t *entry;
1382
cb2188d111f3 Read SDO entry access rights.
Florian Pose <fp@igh-essen.com>
parents: 1363
diff changeset
  1010
    u16 word;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1011
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1012
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1013
        ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1014
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1015
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1016
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1017
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1018
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1019
        EC_SLAVE_ERR(slave, "Failed to receive CoE SDO"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1020
                " description response datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1021
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1022
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1023
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1024
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1025
    if (fsm->datagram->working_counter != 1) {
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1026
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1027
        EC_SLAVE_ERR(slave, "Reception of CoE SDO description"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1028
                " response failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1029
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1030
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1031
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1032
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1033
    data = ec_slave_mbox_fetch(slave, fsm->datagram, &mbox_prot, &rec_size);
1313
ed15eef57d5c Improved error case return codes of many functions.
Florian Pose <fp@igh-essen.com>
parents: 1310
diff changeset
  1034
    if (IS_ERR(data)) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1035
        fsm->state = ec_fsm_coe_error;
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
    if (mbox_prot != 0x03) { // CoE
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1040
        EC_SLAVE_ERR(slave, "Received mailbox protocol"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1041
                " 0x%02X as response.\n", mbox_prot);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1042
        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
  1043
        return;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1044
    }
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1045
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1046
    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
  1047
        // check for CoE response again
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1048
        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
  1049
        fsm->retries = EC_FSM_RETRIES;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1050
        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
  1051
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1052
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1053
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
  1054
    if (rec_size < 3) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1055
        EC_SLAVE_ERR(slave, "Received corrupted SDO entry"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1056
                " description response (size %zu).\n", 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
  1057
        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
  1058
        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
  1059
    }
59f191c9185b Store abort code in sdo request; output it in user space; added some
Florian Pose <fp@igh-essen.com>
parents: 1035
diff changeset
  1060
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
  1061
    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
  1062
        (EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response
2197
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1063
        EC_SLAVE_WARN(slave, "SDO information error response while"
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1064
               " fetching SDO entry 0x%04X:%02X!\n",
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1065
               sdo->index, fsm->subindex);
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1066
        ec_canopen_abort_msg(slave, EC_READ_U32(data + 6));
2197
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1067
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1068
        /* There may be gaps in the subindices, so try to continue with next
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1069
         * subindex. */
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1070
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1071
    } else {
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1072
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1073
        if (rec_size < 9) {
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1074
            EC_SLAVE_ERR(slave, "Received corrupted SDO entry"
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1075
                    " description response (size %zu).\n", rec_size);
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1076
            fsm->state = ec_fsm_coe_error;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1077
            return;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1078
        }
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1079
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1080
        if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1081
            (EC_READ_U8(data + 2) & 0x7F) != 0x06 || // Entry desc. response
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1082
            EC_READ_U16(data + 6) != sdo->index || // SDO index
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1083
            EC_READ_U8(data + 8) != fsm->subindex) { // SDO subindex
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1084
            if (fsm->slave->master->debug_level) {
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1085
                EC_SLAVE_DBG(slave, 1, "Invalid entry description response"
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1086
                        " while fetching SDO entry 0x%04X:%02X!\n",
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1087
                        sdo->index, fsm->subindex);
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1088
                ec_print_data(data, rec_size);
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1089
            }
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1090
            // check for CoE response again
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1091
            ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1092
            fsm->retries = EC_FSM_RETRIES;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1093
            fsm->state = ec_fsm_coe_dict_entry_check;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1094
            return;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1095
        }
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1096
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1097
        if (rec_size < 16) {
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1098
            EC_SLAVE_ERR(slave, "Invalid data size %zu!\n", rec_size);
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1099
            ec_print_data(data, rec_size);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1100
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1101
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1102
        }
2197
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1103
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1104
        data_size = rec_size - 16;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1105
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1106
        if (!(entry = (ec_sdo_entry_t *)
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1107
              kmalloc(sizeof(ec_sdo_entry_t), GFP_KERNEL))) {
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1108
            EC_SLAVE_ERR(slave, "Failed to allocate entry!\n");
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1109
            fsm->state = ec_fsm_coe_error;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1110
            return;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1111
        }
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1112
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1113
        ec_sdo_entry_init(entry, sdo, fsm->subindex);
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1114
        entry->data_type = EC_READ_U16(data + 10);
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1115
        entry->bit_length = EC_READ_U16(data + 12);
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1116
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1117
        // read access rights
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1118
        word = EC_READ_U16(data + 14);
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1119
        entry->read_access[EC_SDO_ENTRY_ACCESS_PREOP] = word & 0x0001;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1120
        entry->read_access[EC_SDO_ENTRY_ACCESS_SAFEOP] =
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1121
            (word >> 1)  & 0x0001;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1122
        entry->read_access[EC_SDO_ENTRY_ACCESS_OP] = (word >> 2)  & 0x0001;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1123
        entry->write_access[EC_SDO_ENTRY_ACCESS_PREOP] = (word >> 3) & 0x0001;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1124
        entry->write_access[EC_SDO_ENTRY_ACCESS_SAFEOP] =
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1125
            (word >> 4)  & 0x0001;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1126
        entry->write_access[EC_SDO_ENTRY_ACCESS_OP] = (word >> 5)  & 0x0001;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1127
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1128
        if (data_size) {
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1129
            uint8_t *desc;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1130
            if (!(desc = kmalloc(data_size + 1, GFP_KERNEL))) {
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1131
                EC_SLAVE_ERR(slave, "Failed to allocate SDO entry name!\n");
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1132
                fsm->state = ec_fsm_coe_error;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1133
                return;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1134
            }
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1135
            memcpy(desc, data + 16, data_size);
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1136
            desc[data_size] = 0;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1137
            entry->description = desc;
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1138
        }
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1139
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1140
        list_add_tail(&entry->list, &sdo->entries);
53b92e797e64 Allow gaps between SDO subindices when fetching dictionary.
Florian Pose <fp@igh-essen.com>
parents: 2196
diff changeset
  1141
    }
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1142
962
ea83a7aab57a Renamed subindices to max_subindex.
Florian Pose <fp@igh-essen.com>
parents: 926
diff changeset
  1143
    if (fsm->subindex < sdo->max_subindex) {
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1144
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1145
        fsm->subindex++;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1146
        fsm->retries = EC_FSM_RETRIES;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1147
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1148
        if (ec_fsm_coe_dict_prepare_entry(fsm, datagram)) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1149
            fsm->state = ec_fsm_coe_error;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1150
        }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1151
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1152
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1153
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1154
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
  1155
    // another SDO description to fetch?
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1156
    if (fsm->sdo->list.next != &slave->sdo_dictionary) {
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1157
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1158
        fsm->sdo = list_entry(fsm->sdo->list.next, ec_sdo_t, list);
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1159
        fsm->retries = EC_FSM_RETRIES;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1160
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1161
        if (ec_fsm_coe_dict_prepare_desc(fsm, datagram)) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1162
            fsm->state = ec_fsm_coe_error;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1163
        }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1164
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1165
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1166
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1167
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1168
    fsm->state = ec_fsm_coe_end;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1169
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1170
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1171
/******************************************************************************
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1172
 *  CoE state machine
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
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1175
int ec_fsm_coe_prepare_down_start(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1176
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1177
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1178
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1179
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1180
    u8 *data;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1181
    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
  1182
    ec_sdo_request_t *request = fsm->request;
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1183
    uint8_t data_set_size;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1184
1507
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1185
    if (request->data_size <= 4) { // use expedited transfer type
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1186
        data = ec_slave_mbox_prepare_send(slave, datagram, 0x03,
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1187
                EC_COE_DOWN_REQ_HEADER_SIZE);
1313
ed15eef57d5c Improved error case return codes of many functions.
Florian Pose <fp@igh-essen.com>
parents: 1310
diff changeset
  1188
        if (IS_ERR(data)) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1189
            request->errno = PTR_ERR(data);
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1190
            return PTR_ERR(data);
1507
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1191
        }
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1192
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1193
        fsm->remaining = 0;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1194
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1195
        data_set_size = 4 - request->data_size;
1507
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1196
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1197
        EC_WRITE_U16(data, 0x2 << 12); // SDO request
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1198
        EC_WRITE_U8 (data + 2, (0x3 // size specified, expedited
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1199
                    | data_set_size << 2
2196
6b481c7b3b34 Allow CoE OD List Segments to have 6 bytes instead of 8.
Florian Pose <fp@igh-essen.com>
parents: 1948
diff changeset
  1200
                    | ((request->complete_access ? 1 : 0) << 4)
1507
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1201
                    | 0x1 << 5)); // Download request
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1202
        EC_WRITE_U16(data + 3, request->index);
1526
66edd310830c Implemented CompleteAccess for SDO downloads.
Florian Pose <fp@igh-essen.com>
parents: 1507
diff changeset
  1203
        EC_WRITE_U8 (data + 5,
66edd310830c Implemented CompleteAccess for SDO downloads.
Florian Pose <fp@igh-essen.com>
parents: 1507
diff changeset
  1204
                request->complete_access ? 0x00 : request->subindex);
1507
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1205
        memcpy(data + 6, request->data, request->data_size);
1457
ec3ad6641d65 Slightly improved CoE download expedited request.
Florian Pose <fp@igh-essen.com>
parents: 1382
diff changeset
  1206
        memset(data + 6 + request->data_size, 0x00, 4 - request->data_size);
1169
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1207
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1208
        if (slave->master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1209
            EC_SLAVE_DBG(slave, 1, "Expedited download request:\n");
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1210
            ec_print_data(data, EC_COE_DOWN_REQ_HEADER_SIZE);
1169
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1211
        }
1507
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1212
    }
1169
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1213
    else { // request->data_size > 4, use normal transfer type
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1214
        size_t data_size,
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1215
               max_data_size =
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1216
                   slave->configured_rx_mailbox_size - EC_MBOX_HEADER_SIZE,
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1217
               required_data_size =
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1218
                   EC_COE_DOWN_REQ_HEADER_SIZE + request->data_size;
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1219
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1220
        if (max_data_size < required_data_size) {
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1221
            // segmenting needed
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1222
            data_size = max_data_size;
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1223
        } else {
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1224
            data_size = required_data_size;
1507
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1225
        }
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1226
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1227
        data = ec_slave_mbox_prepare_send(slave, datagram, 0x03,
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1228
                data_size);
1313
ed15eef57d5c Improved error case return codes of many functions.
Florian Pose <fp@igh-essen.com>
parents: 1310
diff changeset
  1229
        if (IS_ERR(data)) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1230
            request->errno = PTR_ERR(data);
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1231
            return PTR_ERR(data);
1507
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1232
        }
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1233
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1234
        fsm->offset = 0;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1235
        fsm->remaining = request->data_size;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1236
1507
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1237
        EC_WRITE_U16(data, 0x2 << 12); // SDO request
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1238
        EC_WRITE_U8(data + 2,
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1239
                0x1 // size indicator, normal
2196
6b481c7b3b34 Allow CoE OD List Segments to have 6 bytes instead of 8.
Florian Pose <fp@igh-essen.com>
parents: 1948
diff changeset
  1240
                | ((request->complete_access ? 1 : 0) << 4)
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1241
                | 0x1 << 5); // Download request
1507
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1242
        EC_WRITE_U16(data + 3, request->index);
1526
66edd310830c Implemented CompleteAccess for SDO downloads.
Florian Pose <fp@igh-essen.com>
parents: 1507
diff changeset
  1243
        EC_WRITE_U8 (data + 5,
66edd310830c Implemented CompleteAccess for SDO downloads.
Florian Pose <fp@igh-essen.com>
parents: 1507
diff changeset
  1244
                request->complete_access ? 0x00 : request->subindex);
1507
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1245
        EC_WRITE_U32(data + 6, request->data_size);
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1246
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1247
        if (data_size > EC_COE_DOWN_REQ_HEADER_SIZE) {
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1248
            size_t segment_size = data_size - EC_COE_DOWN_REQ_HEADER_SIZE;
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1249
            memcpy(data + EC_COE_DOWN_REQ_HEADER_SIZE,
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1250
                    request->data, segment_size);
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1251
            fsm->offset += segment_size;
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1252
            fsm->remaining -= segment_size;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1253
        }
1169
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1254
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1255
        if (slave->master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1256
            EC_SLAVE_DBG(slave, 1, "Normal download request:\n");
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1257
            ec_print_data(data, data_size);
1169
cf0b5293eea6 Use expedited transfer type for Sdos <= 4 byte.
Florian Pose <fp@igh-essen.com>
parents: 1112
diff changeset
  1258
        }
1507
68e89abadd19 Removed unwanted tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1498
diff changeset
  1259
    }
975
dae04112bcc7 Added debugging for sdo download.
Florian Pose <fp@igh-essen.com>
parents: 962
diff changeset
  1260
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1261
    fsm->state = ec_fsm_coe_down_request;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1262
    return 0;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1263
}
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1264
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1265
/****************************************************************************/
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1266
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1267
/** CoE state: DOWN START.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1268
 */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1269
void ec_fsm_coe_down_start(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1270
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1271
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1272
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1273
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1274
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1275
    ec_sdo_request_t *request = fsm->request;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1276
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1277
    if (fsm->slave->master->debug_level) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1278
        char subidxstr[10];
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1279
        if (request->complete_access) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1280
            subidxstr[0] = 0x00;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1281
        } else {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1282
            sprintf(subidxstr, ":%02X", request->subindex);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1283
        }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1284
        EC_SLAVE_DBG(slave, 1, "Downloading SDO 0x%04X%s.\n",
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1285
                request->index, subidxstr);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1286
        ec_print_data(request->data, request->data_size);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1287
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1288
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1289
    if (!(slave->sii.mailbox_protocols & EC_MBOX_COE)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1290
        EC_SLAVE_ERR(slave, "Slave does not support CoE!\n");
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1291
        request->errno = EPROTONOSUPPORT;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1292
        fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1293
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1294
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1295
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1296
    if (slave->configured_rx_mailbox_size <
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1297
            EC_MBOX_HEADER_SIZE + EC_COE_DOWN_REQ_HEADER_SIZE) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1298
        EC_SLAVE_ERR(slave, "Mailbox too small!\n");
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1299
        request->errno = EOVERFLOW;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1300
        fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1301
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1302
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1303
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1304
1035
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1305
    fsm->request->jiffies_sent = jiffies;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1306
    fsm->retries = EC_FSM_RETRIES;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1307
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1308
    if (ec_fsm_coe_prepare_down_start(fsm, datagram)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1309
        fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1310
    }
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1311
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1312
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1313
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1314
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1315
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1316
   CoE state: DOWN REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1317
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1318
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1319
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1320
void ec_fsm_coe_down_request(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1321
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1322
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1323
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1324
{
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1325
    ec_slave_t *slave = fsm->slave;
1792
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1326
    unsigned long diff_ms;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1327
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1328
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1329
        if (ec_fsm_coe_prepare_down_start(fsm, datagram)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1330
            fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1331
        }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1332
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1333
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1334
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1335
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1336
        fsm->request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1337
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1338
        EC_SLAVE_ERR(slave, "Failed to receive CoE download"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1339
                " request datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1340
        ec_datagram_print_state(fsm->datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1341
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1342
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1343
1792
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1344
    diff_ms = (jiffies - fsm->request->jiffies_sent) * 1000 / HZ;
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1345
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1346
    if (fsm->datagram->working_counter != 1) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1347
        if (!fsm->datagram->working_counter) {
1035
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1348
            if (diff_ms < fsm->request->response_timeout) {
1791
27030c6b2de3 Do not output CoE retries.
Florian Pose <fp@igh-essen.com>
parents: 1563
diff changeset
  1349
#if DEBUG_RETRIES
1931
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1350
                EC_SLAVE_DBG(slave, 1, "Slave did not respond to SDO"
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1351
                        " download request. Retrying after %lu ms...\n",
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1352
                        diff_ms);
1791
27030c6b2de3 Do not output CoE retries.
Florian Pose <fp@igh-essen.com>
parents: 1563
diff changeset
  1353
#endif
1112
d11661e1f4de Fixed CoE transfer timeout bug.
Florian Pose <fp@igh-essen.com>
parents: 1111
diff changeset
  1354
                // no response; send request datagram again
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1355
                if (ec_fsm_coe_prepare_down_start(fsm, datagram)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1356
                    fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1357
                }
1112
d11661e1f4de Fixed CoE transfer timeout bug.
Florian Pose <fp@igh-essen.com>
parents: 1111
diff changeset
  1358
                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
  1359
            }
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1360
        }
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1361
        fsm->request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1362
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1363
        EC_SLAVE_ERR(slave, "Reception of CoE download request"
1931
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1364
                " for SDO 0x%04x:%x failed with timeout after %lu ms: ",
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1365
                fsm->request->index, fsm->request->subindex, diff_ms);
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1366
        ec_datagram_print_wc_error(fsm->datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1367
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1368
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1369
1792
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1370
#if DEBUG_LONG
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1371
    if (diff_ms > 200) {
1931
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1372
        EC_SLAVE_WARN(slave, "SDO 0x%04x:%x download took %lu ms.\n",
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1373
                fsm->request->index, fsm->request->subindex, diff_ms);
1792
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1374
    }
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1375
#endif
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1376
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1377
    fsm->jiffies_start = fsm->datagram->jiffies_sent;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1378
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1379
    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
  1380
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1381
    fsm->state = ec_fsm_coe_down_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1382
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1383
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1384
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1385
1907
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
  1386
/** CoE state: DOWN CHECK.
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
  1387
 */
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1388
void ec_fsm_coe_down_check(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1389
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1390
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1391
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1392
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1393
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1394
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1395
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1396
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1397
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1398
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1399
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1400
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1401
        fsm->request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1402
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1403
        EC_SLAVE_ERR(slave, "Failed to receive CoE mailbox check"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1404
                " datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1405
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1406
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1407
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1408
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1409
    if (fsm->datagram->working_counter != 1) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1410
        fsm->request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1411
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1412
        EC_SLAVE_ERR(slave, "Reception of CoE mailbox check"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1413
                " datagram failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1414
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1415
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1416
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1417
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1418
    if (!ec_slave_mbox_check(fsm->datagram)) {
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
  1419
        unsigned long diff_ms =
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1420
            (fsm->datagram->jiffies_received - fsm->jiffies_start) *
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1421
            1000 / HZ;
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1422
        if (diff_ms >= fsm->request->response_timeout) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1423
            fsm->request->errno = EIO;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1424
            fsm->state = ec_fsm_coe_error;
1931
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1425
            EC_SLAVE_ERR(slave, "Timeout after %lu ms while waiting"
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1426
                    " for SDO 0x%04x:%x download response.\n", diff_ms,
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1427
                    fsm->request->index, fsm->request->subindex);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1428
            return;
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1431
        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
  1432
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1433
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1434
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1435
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1436
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1437
    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
  1438
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1439
    fsm->state = ec_fsm_coe_down_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1440
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1441
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1442
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1443
1907
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
  1444
/** Prepare a download segment request.
dd276ae226b4 Added missing code documentation.
Florian Pose <fp@igh-essen.com>
parents: 1831
diff changeset
  1445
 */
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1446
void ec_fsm_coe_down_prepare_segment_request(
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1447
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1448
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1449
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1450
{
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1451
    ec_slave_t *slave = fsm->slave;
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1452
    ec_sdo_request_t *request = fsm->request;
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1453
    size_t max_segment_size =
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1454
        slave->configured_rx_mailbox_size
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1455
        - EC_MBOX_HEADER_SIZE
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1456
        - EC_COE_DOWN_SEG_REQ_HEADER_SIZE;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1457
    size_t data_size;
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1458
    uint8_t last_segment, seg_data_size, *data;
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1459
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1460
    if (fsm->remaining > max_segment_size) {
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1461
        fsm->segment_size = max_segment_size;
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1462
        last_segment = 0;
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1463
    } else {
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1464
        fsm->segment_size = fsm->remaining;
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1465
        last_segment = 1;
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1466
    }
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1467
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1468
    if (fsm->segment_size > EC_COE_DOWN_SEG_MIN_DATA_SIZE) {
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1469
        seg_data_size = 0x00;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1470
        data_size = EC_COE_DOWN_SEG_REQ_HEADER_SIZE + fsm->segment_size;
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1471
    } else {
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1472
        seg_data_size = EC_COE_DOWN_SEG_MIN_DATA_SIZE - fsm->segment_size;
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1473
        data_size = EC_COE_DOWN_SEG_REQ_HEADER_SIZE
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1474
            + EC_COE_DOWN_SEG_MIN_DATA_SIZE;
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1475
    }
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1476
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1477
    data = ec_slave_mbox_prepare_send(slave, datagram, 0x03,
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1478
            data_size);
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1479
    if (IS_ERR(data)) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1480
        request->errno = PTR_ERR(data);
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1481
        fsm->state = ec_fsm_coe_error;
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1482
        return;
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1483
    }
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1484
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1485
    EC_WRITE_U16(data, 0x2 << 12); // SDO request
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1486
    EC_WRITE_U8(data + 2, (last_segment ? 1 : 0)
2196
6b481c7b3b34 Allow CoE OD List Segments to have 6 bytes instead of 8.
Florian Pose <fp@igh-essen.com>
parents: 1948
diff changeset
  1487
            | (seg_data_size << 1)
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1488
            | (fsm->toggle << 4)
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1489
            | (0x00 << 5)); // Download segment request
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1490
    memcpy(data + EC_COE_DOWN_SEG_REQ_HEADER_SIZE,
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1491
            request->data + fsm->offset, fsm->segment_size);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1492
    if (fsm->segment_size < EC_COE_DOWN_SEG_MIN_DATA_SIZE) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1493
        memset(data + EC_COE_DOWN_SEG_REQ_HEADER_SIZE + fsm->segment_size,
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1494
                0x00, EC_COE_DOWN_SEG_MIN_DATA_SIZE - fsm->segment_size);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1495
    }
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1496
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1497
    if (slave->master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1498
        EC_SLAVE_DBG(slave, 1, "Download segment request:\n");
1563
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1499
        ec_print_data(data, data_size);
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1500
    }
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1501
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1502
    fsm->state = ec_fsm_coe_down_seg_check;
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1503
}
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1504
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1505
/*****************************************************************************/
ddedb71b3a85 Introduced mailbox/CoE defines; avoided duplicate code in CoE segmented
Florian Pose <fp@igh-essen.com>
parents: 1562
diff changeset
  1506
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1507
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1508
   CoE state: DOWN RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1509
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1510
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1511
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1512
void ec_fsm_coe_down_response(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1513
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1514
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1515
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1516
{
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1517
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1518
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1519
    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
  1520
    ec_sdo_request_t *request = fsm->request;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1521
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1522
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1523
        ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1524
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1525
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1526
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1527
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1528
        request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1529
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1530
        EC_SLAVE_ERR(slave, "Failed to receive CoE download"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1531
                " response datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1532
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1533
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1534
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1535
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1536
    if (fsm->datagram->working_counter != 1) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1537
        request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1538
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1539
        EC_SLAVE_ERR(slave, "Reception of CoE download response failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1540
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1541
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1542
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1543
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1544
    data = ec_slave_mbox_fetch(slave, fsm->datagram, &mbox_prot, &rec_size);
1313
ed15eef57d5c Improved error case return codes of many functions.
Florian Pose <fp@igh-essen.com>
parents: 1310
diff changeset
  1545
    if (IS_ERR(data)) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1546
        request->errno = PTR_ERR(data);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1547
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1548
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1549
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1550
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1551
    if (mbox_prot != 0x03) { // CoE
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1552
        request->errno = EIO;
860
ba5c38f0bb30 Minor change: Moved state assign to beginning.
Florian Pose <fp@igh-essen.com>
parents: 859
diff changeset
  1553
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1554
        EC_SLAVE_ERR(slave, "Received mailbox protocol 0x%02X as response.\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1555
                mbox_prot);
860
ba5c38f0bb30 Minor change: Moved state assign to beginning.
Florian Pose <fp@igh-essen.com>
parents: 859
diff changeset
  1556
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1557
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1558
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1559
    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
  1560
        // check for CoE response again
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1561
        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
  1562
        fsm->retries = EC_FSM_RETRIES;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1563
        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
  1564
        return;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1565
    }
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  1566
975
dae04112bcc7 Added debugging for sdo download.
Florian Pose <fp@igh-essen.com>
parents: 962
diff changeset
  1567
    if (slave->master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1568
        EC_SLAVE_DBG(slave, 1, "Download response:\n");
975
dae04112bcc7 Added debugging for sdo download.
Florian Pose <fp@igh-essen.com>
parents: 962
diff changeset
  1569
        ec_print_data(data, rec_size);
dae04112bcc7 Added debugging for sdo download.
Florian Pose <fp@igh-essen.com>
parents: 962
diff changeset
  1570
    }
dae04112bcc7 Added debugging for sdo download.
Florian Pose <fp@igh-essen.com>
parents: 962
diff changeset
  1571
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1572
    if (rec_size < 6) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1573
        request->errno = EIO;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1574
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1575
        EC_SLAVE_ERR(slave, "Received data are too small (%zu bytes):\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1576
                rec_size);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1577
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1578
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1579
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1580
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
  1581
    if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
  1582
        EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request
1526
66edd310830c Implemented CompleteAccess for SDO downloads.
Florian Pose <fp@igh-essen.com>
parents: 1507
diff changeset
  1583
        char subidxstr[10];
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1584
        request->errno = EIO;
1526
66edd310830c Implemented CompleteAccess for SDO downloads.
Florian Pose <fp@igh-essen.com>
parents: 1507
diff changeset
  1585
        fsm->state = ec_fsm_coe_error;
66edd310830c Implemented CompleteAccess for SDO downloads.
Florian Pose <fp@igh-essen.com>
parents: 1507
diff changeset
  1586
        if (request->complete_access) {
66edd310830c Implemented CompleteAccess for SDO downloads.
Florian Pose <fp@igh-essen.com>
parents: 1507
diff changeset
  1587
            subidxstr[0] = 0x00;
66edd310830c Implemented CompleteAccess for SDO downloads.
Florian Pose <fp@igh-essen.com>
parents: 1507
diff changeset
  1588
        } else {
66edd310830c Implemented CompleteAccess for SDO downloads.
Florian Pose <fp@igh-essen.com>
parents: 1507
diff changeset
  1589
            sprintf(subidxstr, ":%02X", request->subindex);
66edd310830c Implemented CompleteAccess for SDO downloads.
Florian Pose <fp@igh-essen.com>
parents: 1507
diff changeset
  1590
        }
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1591
        EC_SLAVE_ERR(slave, "SDO download 0x%04X%s (%zu bytes) aborted.\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1592
                request->index, subidxstr, request->data_size);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1593
        if (rec_size < 10) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1594
            EC_SLAVE_ERR(slave, "Incomplete abort command:\n");
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1595
            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
  1596
        } 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
  1597
            fsm->request->abort_code = EC_READ_U32(data + 6);
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1598
            ec_canopen_abort_msg(slave, fsm->request->abort_code);
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
  1599
        }
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1600
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1601
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1602
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
  1603
    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
  1604
        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
  1605
        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
  1606
        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
  1607
        if (slave->master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1608
            EC_SLAVE_DBG(slave, 1, "Invalid SDO download response!"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1609
                    " Retrying...\n");
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1610
            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
  1611
        }
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1612
        // check for CoE response again
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1613
        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
  1614
        fsm->retries = EC_FSM_RETRIES;
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  1615
        fsm->state = ec_fsm_coe_down_check;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1616
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1617
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1618
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1619
    if (fsm->remaining) { // more segments to download
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1620
        fsm->toggle = 0;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1621
        ec_fsm_coe_down_prepare_segment_request(fsm, datagram);
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1622
    } else {
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1623
        fsm->state = ec_fsm_coe_end; // success
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1624
    }
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1625
}
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1626
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1627
/*****************************************************************************/
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1628
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1629
/**
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1630
   CoE state: DOWN SEG CHECK.
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1631
*/
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1632
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1633
void ec_fsm_coe_down_seg_check(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1634
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1635
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1636
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1637
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1638
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1639
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1640
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1641
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1642
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1643
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1644
        fsm->request->errno = EIO;
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1645
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1646
        EC_SLAVE_ERR(slave, "Failed to receive CoE mailbox check datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1647
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1648
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1649
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1650
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1651
    if (fsm->datagram->working_counter != 1) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1652
        fsm->request->errno = EIO;
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1653
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1654
        EC_SLAVE_ERR(slave, "Reception of CoE mailbox segment check"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1655
                " datagram failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1656
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1657
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1658
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1659
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1660
    if (!ec_slave_mbox_check(fsm->datagram)) {
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1661
        unsigned long diff_ms =
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1662
            (fsm->datagram->jiffies_received - fsm->jiffies_start) *
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1663
            1000 / HZ;
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1664
        if (diff_ms >= fsm->request->response_timeout) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1665
            fsm->request->errno = EIO;
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1666
            fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1667
            EC_SLAVE_ERR(slave, "Timeout while waiting for SDO download"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1668
                    " segment response.\n");
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1669
            return;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1670
        }
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1671
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1672
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1673
        fsm->retries = EC_FSM_RETRIES;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1674
        return;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1675
    }
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1676
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1677
    // Fetch response
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1678
    ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1679
    fsm->retries = EC_FSM_RETRIES;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1680
    fsm->state = ec_fsm_coe_down_seg_response;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1681
}
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1682
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1683
/*****************************************************************************/
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1684
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1685
/**
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1686
   CoE state: DOWN SEG RESPONSE.
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1687
   \todo Timeout behavior
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1688
*/
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1689
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1690
void ec_fsm_coe_down_seg_response(
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1691
        ec_fsm_coe_t *fsm, /**< Finite state machine */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1692
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1693
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1694
{
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1695
    ec_slave_t *slave = fsm->slave;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1696
    uint8_t *data, mbox_prot;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1697
    size_t rec_size;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1698
    ec_sdo_request_t *request = fsm->request;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1699
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1700
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1701
        ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1702
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1703
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1704
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1705
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1706
        request->errno = EIO;
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1707
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1708
        EC_SLAVE_ERR(slave, "Failed to receive CoE download response"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1709
                " datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1710
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1711
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1712
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1713
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1714
    if (fsm->datagram->working_counter != 1) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1715
        request->errno = EIO;
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1716
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1717
        EC_SLAVE_ERR(slave, "Reception of CoE download response failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1718
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1719
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1720
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1721
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1722
    data = ec_slave_mbox_fetch(slave, fsm->datagram, &mbox_prot, &rec_size);
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1723
    if (IS_ERR(data)) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1724
        request->errno = PTR_ERR(data);
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1725
        fsm->state = ec_fsm_coe_error;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1726
        return;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1727
    }
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1728
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1729
    if (mbox_prot != 0x03) { // CoE
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1730
        request->errno = EIO;
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1731
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1732
        EC_SLAVE_ERR(slave, "Received mailbox protocol 0x%02X as response.\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1733
                mbox_prot);
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1734
        return;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1735
    }
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1736
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1737
    if (ec_fsm_coe_check_emergency(fsm, data, rec_size)) {
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1738
        // check for CoE response again
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1739
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1740
        fsm->retries = EC_FSM_RETRIES;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1741
        fsm->state = ec_fsm_coe_down_check;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1742
        return;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1743
    }
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1744
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1745
    if (slave->master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1746
        EC_SLAVE_DBG(slave, 1, "Download response:\n");
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1747
        ec_print_data(data, rec_size);
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1748
    }
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1749
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1750
    if (rec_size < 6) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1751
        request->errno = EIO;
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1752
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1753
        EC_SLAVE_ERR(slave, "Received data are too small (%zu bytes):\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1754
                rec_size);
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1755
        ec_print_data(data, rec_size);
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1756
        return;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1757
    }
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1758
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1759
    if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1760
        EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1761
        char subidxstr[10];
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1762
        request->errno = EIO;
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1763
        fsm->state = ec_fsm_coe_error;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1764
        if (request->complete_access) {
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1765
            subidxstr[0] = 0x00;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1766
        } else {
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1767
            sprintf(subidxstr, ":%02X", request->subindex);
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1768
        }
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1769
        EC_SLAVE_ERR(slave, "SDO download 0x%04X%s (%zu bytes) aborted.\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1770
                request->index, subidxstr, request->data_size);
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1771
        if (rec_size < 10) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1772
            EC_SLAVE_ERR(slave, "Incomplete abort command:\n");
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1773
            ec_print_data(data, rec_size);
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1774
        } else {
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1775
            fsm->request->abort_code = EC_READ_U32(data + 6);
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1776
            ec_canopen_abort_msg(slave, fsm->request->abort_code);
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1777
        }
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1778
        return;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1779
    }
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1780
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1781
    if (EC_READ_U16(data) >> 12 != 0x3 ||
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1782
            ((EC_READ_U8(data + 2) >> 5) != 0x01)) { // segment response
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1783
        if (slave->master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1784
            EC_SLAVE_DBG(slave, 1, "Invalid SDO download response!"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1785
                    " Retrying...\n");
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1786
            ec_print_data(data, rec_size);
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1787
        }
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1788
        // check for CoE response again
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1789
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1790
        fsm->retries = EC_FSM_RETRIES;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1791
        fsm->state = ec_fsm_coe_down_seg_check;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1792
        return;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1793
    }
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1794
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1795
    if (((EC_READ_U8(data + 2) >> 4) & 0x01) != fsm->toggle) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1796
        EC_SLAVE_ERR(slave, "Invalid toggle received during"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1797
                " segmented download:\n");
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1798
        ec_print_data(data, rec_size);
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1799
        request->errno = EIO;
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1800
        fsm->state = ec_fsm_coe_error;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1801
        return;
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1802
    }
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1803
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1804
    fsm->offset += fsm->segment_size;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1805
    fsm->remaining -= fsm->segment_size;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1806
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1807
    if (fsm->remaining) { // more segments to download
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1808
        fsm->toggle = !fsm->toggle;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1809
        ec_fsm_coe_down_prepare_segment_request(fsm, datagram);
1562
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1810
    } else {
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1811
        fsm->state = ec_fsm_coe_end; // success
5c249f469b81 SDO segmented download.
Florian Pose <fp@igh-essen.com>
parents: 1550
diff changeset
  1812
    }
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1813
}
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
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1817
int ec_fsm_coe_prepare_up(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1818
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1819
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1820
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1821
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1822
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1823
    ec_sdo_request_t *request = fsm->request;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1824
    ec_master_t *master = slave->master;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1825
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1826
    u8 *data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10);
1313
ed15eef57d5c Improved error case return codes of many functions.
Florian Pose <fp@igh-essen.com>
parents: 1310
diff changeset
  1827
    if (IS_ERR(data)) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1828
        request->errno = PTR_ERR(data);
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1829
        return PTR_ERR(data);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1830
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1831
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
  1832
    EC_WRITE_U16(data, 0x2 << 12); // SDO request
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1833
    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
  1834
    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
  1835
    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
  1836
    memset(data + 6, 0x00, 4);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1837
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1838
    if (master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1839
        EC_SLAVE_DBG(slave, 1, "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
  1840
        ec_print_data(data, 10);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1841
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1842
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1843
    fsm->state = ec_fsm_coe_up_request;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1844
    return 0;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1845
}
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1846
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1847
/*****************************************************************************/
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1848
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1849
/**
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1850
   CoE state: UP START.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1851
*/
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1852
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1853
void ec_fsm_coe_up_start(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1854
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1855
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1856
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1857
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1858
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1859
    ec_sdo_request_t *request = fsm->request;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1860
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1861
    EC_SLAVE_DBG(slave, 1, "Uploading SDO 0x%04X:%02X.\n",
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1862
            request->index, request->subindex);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1863
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1864
    if (!(slave->sii.mailbox_protocols & EC_MBOX_COE)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1865
        EC_SLAVE_ERR(slave, "Slave does not support CoE!\n");
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1866
        request->errno = EPROTONOSUPPORT;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1867
        fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1868
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1869
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1870
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1871
    fsm->retries = EC_FSM_RETRIES;
1035
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1872
    fsm->request->jiffies_sent = jiffies;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1873
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1874
    if (ec_fsm_coe_prepare_up(fsm, datagram)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1875
        fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1876
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1877
}
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1878
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1879
/*****************************************************************************/
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1880
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1881
   CoE state: UP REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1882
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1883
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1884
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1885
void ec_fsm_coe_up_request(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1886
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1887
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1888
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1889
{
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1890
    ec_slave_t *slave = fsm->slave;
1792
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1891
    unsigned long diff_ms;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1892
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1893
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1894
        if (ec_fsm_coe_prepare_up(fsm, datagram)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1895
            fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1896
        }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1897
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1898
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1899
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1900
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1901
        fsm->request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1902
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1903
        EC_SLAVE_ERR(slave, "Failed to receive CoE upload request: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1904
        ec_datagram_print_state(fsm->datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1905
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1906
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1907
1792
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1908
    diff_ms = (jiffies - fsm->request->jiffies_sent) * 1000 / HZ;
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1909
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1910
    if (fsm->datagram->working_counter != 1) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1911
        if (!fsm->datagram->working_counter) {
1035
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1912
            if (diff_ms < fsm->request->response_timeout) {
1791
27030c6b2de3 Do not output CoE retries.
Florian Pose <fp@igh-essen.com>
parents: 1563
diff changeset
  1913
#if DEBUG_RETRIES
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1914
                EC_SLAVE_DBG(slave, 1, "Slave did not respond to"
1931
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1915
                        " SDO upload request. Retrying after %lu ms...\n",
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1916
                        diff_ms);
1791
27030c6b2de3 Do not output CoE retries.
Florian Pose <fp@igh-essen.com>
parents: 1563
diff changeset
  1917
#endif
1112
d11661e1f4de Fixed CoE transfer timeout bug.
Florian Pose <fp@igh-essen.com>
parents: 1111
diff changeset
  1918
                // no response; send request datagram again
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1919
                if (ec_fsm_coe_prepare_up(fsm, datagram)) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1920
                    fsm->state = ec_fsm_coe_error;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1921
                }
1112
d11661e1f4de Fixed CoE transfer timeout bug.
Florian Pose <fp@igh-essen.com>
parents: 1111
diff changeset
  1922
                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
  1923
            }
7fafc1a7451e Added response timeout to sdo_request; removed timout from fsm_coe_map.
Florian Pose <fp@igh-essen.com>
parents: 998
diff changeset
  1924
        }
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1925
        fsm->request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1926
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1927
        EC_SLAVE_ERR(slave, "Reception of CoE upload request for"
1931
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1928
                " SDO 0x%04x:%x failed with timeout after %lu ms: ",
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1929
                fsm->request->index, fsm->request->subindex, diff_ms);
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1930
        ec_datagram_print_wc_error(fsm->datagram);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1931
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1932
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1933
1792
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1934
#if DEBUG_LONG
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1935
    if (diff_ms > 200) {
1931
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1936
        EC_SLAVE_WARN(slave, "SDO 0x%04x:%x upload took %lu ms.\n",
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1937
                fsm->request->index, fsm->request->subindex, diff_ms);
1792
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1938
    }
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1939
#endif
3eb466e9bad5 Improved CoE error messages.
Florian Pose <fp@igh-essen.com>
parents: 1791
diff changeset
  1940
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1941
    fsm->jiffies_start = fsm->datagram->jiffies_sent;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1942
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1943
    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
  1944
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1945
    fsm->state = ec_fsm_coe_up_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1946
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1947
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1948
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1949
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1950
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1951
   CoE state: UP CHECK.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1952
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1953
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1954
void ec_fsm_coe_up_check(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1955
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1956
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1957
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1958
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1959
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1960
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1961
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1962
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1963
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1964
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1965
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1966
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1967
        fsm->request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1968
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1969
        EC_SLAVE_ERR(slave, "Failed to receive CoE mailbox check datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1970
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1971
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1972
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1973
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1974
    if (fsm->datagram->working_counter != 1) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1975
        fsm->request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1976
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1977
        EC_SLAVE_ERR(slave, "Reception of CoE mailbox check"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1978
                " datagram failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1979
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1980
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1981
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1982
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1983
    if (!ec_slave_mbox_check(fsm->datagram)) {
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
  1984
        unsigned long diff_ms =
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1985
            (fsm->datagram->jiffies_received - fsm->jiffies_start) *
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  1986
            1000 / HZ;
1043
2abea4233a6d Response timeout for checking Sdo upload.
Florian Pose <fp@igh-essen.com>
parents: 1038
diff changeset
  1987
        if (diff_ms >= fsm->request->response_timeout) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  1988
            fsm->request->errno = EIO;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1989
            fsm->state = ec_fsm_coe_error;
1931
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1990
            EC_SLAVE_ERR(slave, "Timeout after %lu ms while waiting for"
831f2d34664c Fixed format specifiers.
Florian Pose <fp@igh-essen.com>
parents: 1923
diff changeset
  1991
                    " SDO 0x%04x:%x upload response.\n", diff_ms,
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  1992
                    fsm->request->index, fsm->request->subindex);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1993
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1994
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1995
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1996
        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
  1997
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1998
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1999
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2000
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2001
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2002
    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
  2003
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2004
    fsm->state = ec_fsm_coe_up_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2005
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2006
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2007
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2008
1498
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2009
/** Prepare an SDO upload segment request.
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2010
 */
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2011
void ec_fsm_coe_up_prepare_segment_request(
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2012
        ec_fsm_coe_t *fsm, /**< Finite state machine */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2013
        ec_datagram_t *datagram /**< Datagram to use. */
1498
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2014
        )
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2015
{
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2016
    uint8_t *data =
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2017
        ec_slave_mbox_prepare_send(fsm->slave, datagram, 0x03, 10);
1498
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2018
    if (IS_ERR(data)) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2019
        fsm->request->errno = PTR_ERR(data);
1498
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2020
        fsm->state = ec_fsm_coe_error;
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2021
        return;
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2022
    }
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2023
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2024
    EC_WRITE_U16(data, 0x2 << 12); // SDO request
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2025
    EC_WRITE_U8 (data + 2, (fsm->toggle << 4 // toggle
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2026
                | 0x3 << 5)); // upload segment request
1550
81a16ba13ae6 Avoid zeroing mailbox datagram because of VoE handlers.
Florian Pose <fp@igh-essen.com>
parents: 1544
diff changeset
  2027
    memset(data + 3, 0x00, 7);
1498
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2028
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2029
    if (fsm->slave->master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2030
        EC_SLAVE_DBG(fsm->slave, 1, "Upload segment request:\n");
1498
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2031
        ec_print_data(data, 10);
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2032
    }
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2033
}
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2034
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2035
/*****************************************************************************/
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2036
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2037
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2038
   CoE state: UP RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  2039
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2040
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2041
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2042
void ec_fsm_coe_up_response(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2043
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2044
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2045
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2046
{
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2047
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2048
    ec_master_t *master = slave->master;
1468
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2049
    uint16_t rec_index;
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2050
    uint8_t *data, mbox_prot, rec_subindex;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2051
    size_t rec_size, data_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2052
    ec_sdo_request_t *request = fsm->request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2053
    unsigned int expedited, size_specified;
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2054
    int ret;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2055
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2056
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2057
        ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2058
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2059
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2060
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2061
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2062
        request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  2063
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2064
        EC_SLAVE_ERR(slave, "Failed to receive CoE upload response"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2065
                " datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2066
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2067
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2068
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2069
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2070
    if (fsm->datagram->working_counter != 1) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2071
        request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  2072
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2073
        EC_SLAVE_ERR(slave, "Reception of CoE upload response failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2074
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2075
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2076
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2077
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2078
    data = ec_slave_mbox_fetch(slave, fsm->datagram, &mbox_prot, &rec_size);
1313
ed15eef57d5c Improved error case return codes of many functions.
Florian Pose <fp@igh-essen.com>
parents: 1310
diff changeset
  2079
    if (IS_ERR(data)) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2080
        request->errno = PTR_ERR(data);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2081
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2082
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2083
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2084
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2085
    if (master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2086
        EC_SLAVE_DBG(slave, 1, "Upload response:\n");
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2087
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2088
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2089
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2090
    if (mbox_prot != 0x03) { // CoE
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2091
        request->errno = EIO;
860
ba5c38f0bb30 Minor change: Moved state assign to beginning.
Florian Pose <fp@igh-essen.com>
parents: 859
diff changeset
  2092
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2093
        EC_SLAVE_WARN(slave, "Received mailbox protocol 0x%02X"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2094
                " 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
  2095
        return;
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2096
    }
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2097
895
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  2098
    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
  2099
        // check for CoE response again
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  2100
        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
  2101
        fsm->retries = EC_FSM_RETRIES;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  2102
        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
  2103
        return;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  2104
    }
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  2105
1468
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2106
    if (rec_size < 6) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2107
        request->errno = EIO;
860
ba5c38f0bb30 Minor change: Moved state assign to beginning.
Florian Pose <fp@igh-essen.com>
parents: 859
diff changeset
  2108
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2109
        EC_SLAVE_ERR(slave, "Received currupted SDO upload response"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2110
                " (%zu bytes)!\n", rec_size);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2111
        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
  2112
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2113
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2114
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
  2115
    if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request
1468
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2116
            EC_READ_U8(data + 2) >> 5 == 0x4) { // abort SDO transfer request
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2117
        EC_SLAVE_ERR(slave, "SDO upload 0x%04X:%02X aborted.\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2118
               request->index, request->subindex);
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
  2119
        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
  2120
            request->abort_code = EC_READ_U32(data + 6);
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2121
            ec_canopen_abort_msg(slave, request->abort_code);
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
  2122
        } else {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2123
            EC_SLAVE_ERR(slave, "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
  2124
        }
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2125
        request->errno = EIO;
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2126
        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
  2127
        return;
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2128
    }
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2129
1468
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2130
    if (EC_READ_U16(data) >> 12 != 0x3 || // SDO response
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2131
            EC_READ_U8(data + 2) >> 5 != 0x2) { // upload response
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2132
        EC_SLAVE_ERR(slave, "Received unknown response while"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2133
                " uploading SDO 0x%04X:%02X.\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2134
                request->index, request->subindex);
1468
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2135
        ec_print_data(data, rec_size);
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2136
        request->errno = EIO;
1468
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2137
        fsm->state = ec_fsm_coe_error;
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2138
        return;
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2139
    }
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2140
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2141
    rec_index = EC_READ_U16(data + 3);
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2142
    rec_subindex = EC_READ_U8(data + 5);
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2143
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2144
    if (rec_index != request->index || rec_subindex != request->subindex) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2145
        EC_SLAVE_ERR(slave, "Received upload response for wrong SDO"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2146
                " (0x%04X:%02X, requested: 0x%04X:%02X).\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2147
                rec_index, rec_subindex, request->index, request->subindex);
1468
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2148
        ec_print_data(data, rec_size);
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2149
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2150
        // check for CoE response again
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2151
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2152
        fsm->retries = EC_FSM_RETRIES;
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2153
        fsm->state = ec_fsm_coe_up_check;
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2154
        return;
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2155
    }
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2156
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2157
    // normal or expedited?
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2158
    expedited = EC_READ_U8(data + 2) & 0x02;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2159
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2160
    if (expedited) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2161
        size_specified = EC_READ_U8(data + 2) & 0x01;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2162
        if (size_specified) {
1339
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2163
            fsm->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
  2164
        } else {
1339
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2165
            fsm->complete_size = 4;
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2166
        }
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2167
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2168
        if (rec_size < 6 + fsm->complete_size) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2169
            request->errno = EIO;
860
ba5c38f0bb30 Minor change: Moved state assign to beginning.
Florian Pose <fp@igh-essen.com>
parents: 859
diff changeset
  2170
            fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2171
            EC_SLAVE_ERR(slave, "Received corrupted SDO expedited upload"
1543
373cd456dc26 Avoid warnings on x86_64 platform. Thanks to Thomas Elste.
Florian Pose <fp@igh-essen.com>
parents: 1526
diff changeset
  2172
                    " response (only %zu bytes)!\n", 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
  2173
            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
  2174
            return;
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2175
        }
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2176
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2177
        ret = ec_sdo_request_copy_data(request, data + 6, fsm->complete_size);
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2178
        if (ret) {
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2179
            request->errno = -ret;
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
  2180
            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
  2181
            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
  2182
        }
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2183
    } else { // normal
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2184
        if (rec_size < 10) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2185
            request->errno = EIO;
860
ba5c38f0bb30 Minor change: Moved state assign to beginning.
Florian Pose <fp@igh-essen.com>
parents: 859
diff changeset
  2186
            fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2187
            EC_SLAVE_ERR(slave, "Received currupted SDO normal upload"
1543
373cd456dc26 Avoid warnings on x86_64 platform. Thanks to Thomas Elste.
Florian Pose <fp@igh-essen.com>
parents: 1526
diff changeset
  2188
                    " response (only %zu bytes)!\n", 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
  2189
            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
  2190
            return;
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2191
        }
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2192
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2193
        data_size = rec_size - 10;
1339
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2194
        fsm->complete_size = EC_READ_U32(data + 6);
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2195
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2196
        if (!fsm->complete_size) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2197
            request->errno = EIO;
860
ba5c38f0bb30 Minor change: Moved state assign to beginning.
Florian Pose <fp@igh-essen.com>
parents: 859
diff changeset
  2198
            fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2199
            EC_SLAVE_ERR(slave, "No complete size supplied!\n");
769
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2200
            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
  2201
            return;
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2202
        }
2195c2ea37b4 Fixed normal/expedited bug at CoE upload; more robust behaviour, when
Florian Pose <fp@igh-essen.com>
parents: 739
diff changeset
  2203
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2204
        ret = ec_sdo_request_alloc(request, fsm->complete_size);
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2205
        if (ret) {
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2206
            request->errno = -ret;
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
  2207
            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
  2208
            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
  2209
        }
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  2210
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2211
        ret = ec_sdo_request_copy_data(request, data + 10, data_size);
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2212
        if (ret) {
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2213
            request->errno = -ret;
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
  2214
            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
  2215
            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
  2216
        }
f4f53be425ac Removed ec_sdodata_t; CoE state machines work on ec_sdo_request_t.
Florian Pose <fp@igh-essen.com>
parents: 834
diff changeset
  2217
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2218
        fsm->toggle = 0;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2219
1339
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2220
        if (data_size < fsm->complete_size) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2221
            EC_SLAVE_DBG(slave, 1, "SDO data incomplete (%zu / %u)."
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2222
                    " Segmenting...\n", data_size, fsm->complete_size);
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2223
            ec_fsm_coe_up_prepare_segment_request(fsm, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  2224
            fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2225
            fsm->state = ec_fsm_coe_up_seg_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2226
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2227
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2228
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2229
874
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  2230
    if (master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2231
        EC_SLAVE_DBG(slave, 1, "Uploaded data:\n");
874
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  2232
        ec_print_data(request->data, request->data_size);
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  2233
    }
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  2234
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2235
    fsm->state = ec_fsm_coe_end; // success
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2236
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2237
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2238
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2239
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2240
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2241
   CoE state: UP REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  2242
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2243
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2244
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2245
void ec_fsm_coe_up_seg_request(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2246
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2247
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2248
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2249
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2250
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2251
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2252
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2253
        ec_fsm_coe_up_prepare_segment_request(fsm, datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2254
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2255
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2256
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2257
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2258
        fsm->request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  2259
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2260
        EC_SLAVE_ERR(slave, "Failed to receive CoE upload segment"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2261
                " request datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2262
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2263
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2264
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2265
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2266
    if (fsm->datagram->working_counter != 1) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2267
        fsm->request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  2268
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2269
        EC_SLAVE_ERR(slave, "Reception of CoE upload segment"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2270
                " request failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2271
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2272
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2273
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2274
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2275
    fsm->jiffies_start = fsm->datagram->jiffies_sent;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2276
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2277
    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
  2278
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2279
    fsm->state = ec_fsm_coe_up_seg_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2280
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2281
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2282
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2283
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2284
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2285
   CoE state: UP CHECK.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2286
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2287
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2288
void ec_fsm_coe_up_seg_check(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2289
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2290
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2291
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2292
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2293
    ec_slave_t *slave = fsm->slave;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2294
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2295
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2296
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2297
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2298
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2299
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2300
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2301
        fsm->request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  2302
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2303
        EC_SLAVE_ERR(slave, "Failed to receive CoE mailbox check"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2304
                " datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2305
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2306
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2307
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2308
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2309
    if (fsm->datagram->working_counter != 1) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2310
        fsm->request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  2311
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2312
        EC_SLAVE_ERR(slave, "Reception of CoE mailbox check datagram"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2313
                " failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2314
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2315
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2316
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2317
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2318
    if (!ec_slave_mbox_check(fsm->datagram)) {
1038
7ca4103ba170 Use jiffies instead of cycles in CoE state machine.
Florian Pose <fp@igh-essen.com>
parents: 1037
diff changeset
  2319
        unsigned long diff_ms =
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2320
            (fsm->datagram->jiffies_received - fsm->jiffies_start) *
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2321
            1000 / HZ;
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  2322
        if (diff_ms >= fsm->request->response_timeout) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2323
            fsm->request->errno = EIO;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2324
            fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2325
            EC_SLAVE_ERR(slave, "Timeout while waiting for SDO upload"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2326
                    " segment response.\n");
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2327
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2328
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2329
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2330
        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
  2331
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2332
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2333
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2334
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2335
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2336
    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
  2337
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2338
    fsm->state = ec_fsm_coe_up_seg_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2339
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2340
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2341
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2342
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2343
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2344
   CoE state: UP RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  2345
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2346
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2347
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2348
void ec_fsm_coe_up_seg_response(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2349
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2350
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2351
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2352
{
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2353
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2354
    ec_master_t *master = slave->master;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2355
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2356
    size_t rec_size, data_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2357
    ec_sdo_request_t *request = fsm->request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2358
    unsigned int last_segment;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2359
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2360
    if (fsm->datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2361
        ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2362
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2363
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2364
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2365
    if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2366
        request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  2367
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2368
        EC_SLAVE_ERR(slave, "Failed to receive CoE upload segment"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2369
                " response datagram: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2370
        ec_datagram_print_state(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2371
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2372
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2373
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2374
    if (fsm->datagram->working_counter != 1) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2375
        request->errno = EIO;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  2376
        fsm->state = ec_fsm_coe_error;
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2377
        EC_SLAVE_ERR(slave, "Reception of CoE upload segment"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2378
                " response failed: ");
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2379
        ec_datagram_print_wc_error(fsm->datagram);
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2380
        return;
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2381
    }
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2382
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2383
    data = ec_slave_mbox_fetch(slave, fsm->datagram, &mbox_prot, &rec_size);
1313
ed15eef57d5c Improved error case return codes of many functions.
Florian Pose <fp@igh-essen.com>
parents: 1310
diff changeset
  2384
    if (IS_ERR(data)) {
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2385
        request->errno = PTR_ERR(data);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2386
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2387
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2388
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2389
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2390
    if (master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2391
        EC_SLAVE_DBG(slave, 1, "Upload segment response:\n");
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2392
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2393
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2394
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2395
    if (mbox_prot != 0x03) { // CoE
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2396
        EC_SLAVE_ERR(slave, "Received mailbox protocol 0x%02X as response.\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2397
                mbox_prot);
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2398
        request->errno = EIO;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2399
        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
  2400
        return;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  2401
    }
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  2402
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  2403
    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
  2404
        // check for CoE response again
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  2405
        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
  2406
        fsm->retries = EC_FSM_RETRIES;
5c2125247dde Check for emergency requests during CoE transfers.
Florian Pose <fp@igh-essen.com>
parents: 874
diff changeset
  2407
        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
  2408
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2409
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2410
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2411
    if (rec_size < 10) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2412
        EC_SLAVE_ERR(slave, "Received currupted SDO upload"
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2413
                " segment response!\n");
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2414
        ec_print_data(data, rec_size);
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2415
        request->errno = EIO;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2416
        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
  2417
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2418
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2419
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
  2420
    if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request
1468
0a804ddbaf93 Improved error checking on SDO upload.
Florian Pose <fp@igh-essen.com>
parents: 1457
diff changeset
  2421
            EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2422
        EC_SLAVE_ERR(slave, "SDO upload 0x%04X:%02X aborted.\n",
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2423
               request->index, request->subindex);
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
  2424
        request->abort_code = EC_READ_U32(data + 6);
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2425
        ec_canopen_abort_msg(slave, request->abort_code);
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2426
        request->errno = EIO;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2427
        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
  2428
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2429
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2430
1327
4d179b06dd3c merge -c1604 branches/stable-1.4: Fixed spelling of EtherCAT terms.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
  2431
    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
  2432
        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
  2433
        if (fsm->slave->master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2434
            EC_SLAVE_DBG(slave, 1, "Invalid SDO upload segment response!\n");
1053
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  2435
            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
  2436
        }
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  2437
        // check for CoE response again
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  2438
        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
  2439
        fsm->retries = EC_FSM_RETRIES;
0cb9604cbaa7 Central CoE timeouts; retry on CoE response mismatch.
Florian Pose <fp@igh-essen.com>
parents: 1043
diff changeset
  2440
        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
  2441
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2442
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2443
1533
bd150d066ea2 Fixed SDO upload segment response for 10 bytes mailbox length (thanks to Joerg
Florian Pose <fp@igh-essen.com>
parents: 1526
diff changeset
  2444
    data_size = rec_size - 3; /* Header of segment upload is smaller than
bd150d066ea2 Fixed SDO upload segment response for 10 bytes mailbox length (thanks to Joerg
Florian Pose <fp@igh-essen.com>
parents: 1526
diff changeset
  2445
                                 normal upload */
bd150d066ea2 Fixed SDO upload segment response for 10 bytes mailbox length (thanks to Joerg
Florian Pose <fp@igh-essen.com>
parents: 1526
diff changeset
  2446
    if (rec_size == 10) {
bd150d066ea2 Fixed SDO upload segment response for 10 bytes mailbox length (thanks to Joerg
Florian Pose <fp@igh-essen.com>
parents: 1526
diff changeset
  2447
        uint8_t seg_size = (EC_READ_U8(data + 2) & 0xE) >> 1;
bd150d066ea2 Fixed SDO upload segment response for 10 bytes mailbox length (thanks to Joerg
Florian Pose <fp@igh-essen.com>
parents: 1526
diff changeset
  2448
        data_size -= seg_size;
1339
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2449
    }
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2450
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2451
    if (request->data_size + data_size > fsm->complete_size) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2452
        EC_SLAVE_ERR(slave, "SDO upload 0x%04X:%02X failed: Fragment"
1339
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2453
                " exceeding complete size!\n",
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2454
                request->index, request->subindex);
1948
fa1c4fb0a8da Output correct errno when transferring SDOs via command-line interface.
Florian Pose <fp@igh-essen.com>
parents: 1931
diff changeset
  2455
        request->errno = EOVERFLOW;
1339
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2456
        fsm->state = ec_fsm_coe_error;
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2457
        return;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2458
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2459
1498
58532f81a456 Fixed SDO upload segment response (thanks to Christoph Peter).
Florian Pose <fp@igh-essen.com>
parents: 1495
diff changeset
  2460
    memcpy(request->data + request->data_size, data + 3, data_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
  2461
    request->data_size += data_size;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2462
1533
bd150d066ea2 Fixed SDO upload segment response for 10 bytes mailbox length (thanks to Joerg
Florian Pose <fp@igh-essen.com>
parents: 1526
diff changeset
  2463
    last_segment = EC_READ_U8(data + 2) & 0x01;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2464
    if (!last_segment) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2465
        fsm->toggle = !fsm->toggle;
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2466
        ec_fsm_coe_up_prepare_segment_request(fsm, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  2467
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2468
        fsm->state = ec_fsm_coe_up_seg_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2469
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2470
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2471
1339
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2472
    if (request->data_size != fsm->complete_size) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2473
        EC_SLAVE_WARN(slave, "SDO upload 0x%04X:%02X: Assembled data"
1543
373cd456dc26 Avoid warnings on x86_64 platform. Thanks to Thomas Elste.
Florian Pose <fp@igh-essen.com>
parents: 1526
diff changeset
  2474
                " size (%zu) does not match complete size (%u)!\n",
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2475
                request->index, request->subindex,
1339
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2476
                request->data_size, fsm->complete_size);
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2477
    }
3483999e1aba merge -c1622 branches/1.4-foe: Fixed seg_size parameter when processing an CoE Upload Segment Response.
Florian Pose <fp@igh-essen.com>
parents: 1338
diff changeset
  2478
874
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  2479
    if (master->debug_level) {
1921
d9cf40facbc4 Improved syslog macros.
Florian Pose <fp@igh-essen.com>
parents: 1907
diff changeset
  2480
        EC_SLAVE_DBG(slave, 1, "Uploaded data:\n");
874
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  2481
        ec_print_data(request->data, request->data_size);
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  2482
    }
022f869ea40f Added CoE debugging.
Florian Pose <fp@igh-essen.com>
parents: 860
diff changeset
  2483
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2484
    fsm->state = ec_fsm_coe_end; // success
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2485
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2486
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2487
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2488
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2489
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2490
   State: ERROR.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2491
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2492
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2493
void ec_fsm_coe_error(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2494
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2495
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2496
        )
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2497
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2498
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2499
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2500
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2501
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2502
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2503
   State: END.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2504
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  2505
2498
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2506
void ec_fsm_coe_end(
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2507
        ec_fsm_coe_t *fsm, /**< Finite state machine. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2508
        ec_datagram_t *datagram /**< Datagram to use. */
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2509
        )
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2510
{
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2511
}
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2512
9cdd7669dc0b Avoided io_sem locking from userspace/rtdm library.
Florian Pose <fp@igh-essen.com>
parents: 2438
diff changeset
  2513
/*****************************************************************************/