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