master/fsm_coe.c
author Florian Pose <fp@igh-essen.com>
Sun, 04 Mar 2007 16:15:23 +0000
changeset 608 029d70ceaaf1
parent 594 07dd2a7df66e
child 637 d5d04c868e0e
permissions -rw-r--r--
Updated 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
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
   SDO abort messages.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
   The "abort SDO transfer request" supplies an abort code,
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"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
    {0x05040000, "SDO protocol timeout"},
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"},
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
    {0x06040041, "The object cannot be mapped into the PDO"},
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"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
     " exceed the PDO length"},
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
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
   Outputs an SDO abort message.
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) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
            EC_ERR("SDO abort message 0x%08X: \"%s\".\n",
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
    EC_ERR("Unknown SDO abort code 0x%08X.\n", abort_code);
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
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
   Starts reading a slaves' SDO dictionary.
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
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
   Starts to download an SDO to a slave.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
void ec_fsm_coe_download(ec_fsm_coe_t *fsm, /**< finite state machine */
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
                         ec_slave_t *slave, /**< EtherCAT slave */
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
                         ec_sdo_data_t *sdodata /**< SDO data object */
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   189
                         )
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   190
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   191
    fsm->slave = slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   192
    fsm->sdodata = sdodata;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   193
    fsm->state = ec_fsm_coe_down_start;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
}
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
   Starts to upload an SDO from a slave.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
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
   203
                       ec_slave_t *slave, /**< EtherCAT slave */
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
                       ec_sdo_request_t *request /**< SDO request */
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
                       )
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
    fsm->slave = slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   208
    fsm->request = request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
    fsm->state = ec_fsm_coe_up_start;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
}
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
   Executes the current state of the state machine.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
   \return false, if state machine has terminated
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   217
*/
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
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
   220
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   221
    fsm->state(fsm);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   222
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   223
    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
   224
}
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
   Returns, if the state machine terminated with success.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   230
   \return non-zero if successful.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   231
*/
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
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
   234
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   235
    return fsm->state == ec_fsm_coe_end;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   236
}
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
 *  CoE dictionary state machine
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   240
 *****************************************************************************/
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
   CoE state: DICT START.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   244
*/
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
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
   247
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   248
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   249
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   250
    uint8_t *data;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   251
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   252
    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
   253
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   254
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   255
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   256
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   257
    EC_WRITE_U16(data, 0x8 << 12); // SDO information
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   258
    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
   259
    EC_WRITE_U8 (data + 3, 0x00);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   260
    EC_WRITE_U16(data + 4, 0x0000);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   261
    EC_WRITE_U16(data + 6, 0x0001); // deliver all SDOs!
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
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   264
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   265
    fsm->state = ec_fsm_coe_dict_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   266
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   267
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   268
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   269
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   270
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   271
   CoE state: DICT REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   272
   \todo Timeout behavior
436
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
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
   276
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   277
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   278
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   279
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   280
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   281
        // FIXME: request again?
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   282
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   283
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   284
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   285
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   286
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   287
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   288
        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
   289
               " 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
   290
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   291
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   292
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   293
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   294
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   295
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   296
        EC_ERR("Reception of CoE dictionary request failed - slave %i did"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   297
               " not respond.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   298
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   299
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   300
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   301
    fsm->cycles_start = datagram->cycles_sent;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   302
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   303
    ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   304
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   305
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   306
    fsm->state = ec_fsm_coe_dict_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   307
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   308
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   309
/*****************************************************************************/
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
   CoE state: DICT CHECK.
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
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
   316
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   317
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   318
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   319
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   320
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   321
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   322
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   323
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   324
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   325
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   326
        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
   327
        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
   328
                " (datagram state %i).\n",
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   329
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   330
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   331
    }
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
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   334
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   335
        EC_ERR("Reception of CoE mailbox check datagram failed - slave %i did"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   336
               " not respond.\n",
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   337
               slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   338
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   339
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   340
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   341
    if (!ec_slave_mbox_check(datagram)) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   342
        if (datagram->cycles_received
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   343
            - fsm->cycles_start >= (cycles_t) 100 * cpu_khz) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   344
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   345
            EC_ERR("Timeout while checking SDO dictionary on slave %i.\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   346
                   slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   347
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   348
        }
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
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   351
        ec_master_queue_datagram(fsm->slave->master, datagram);
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.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   358
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   359
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   360
    fsm->state = ec_fsm_coe_dict_response;
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
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   366
   CoE state: DICT RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   367
   \todo Timeout behavior
436
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   370
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
   371
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   372
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   373
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   374
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   375
    size_t rec_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   376
    unsigned int sdo_count, i;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   377
    uint16_t sdo_index;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   378
    ec_sdo_t *sdo;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   379
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   380
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   381
        // FIXME: request again?
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   382
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   383
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   384
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   385
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   386
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   387
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   388
        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
   389
               " 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
   390
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   391
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   392
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   393
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   394
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   395
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   396
        EC_ERR("Reception of CoE dictionary response failed - slave %i did"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   397
               " not respond.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   398
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   399
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   400
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   401
    if (!(data = ec_slave_mbox_fetch(slave, datagram,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   402
				     &mbox_prot, &rec_size))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   403
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   404
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   405
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   406
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   407
    if (mbox_prot != 0x03) { // CoE
446
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
   408
        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
   409
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   410
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   411
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   412
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   413
    if (EC_READ_U16(data) >> 12 == 0x8 && // SDO information
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   414
        (EC_READ_U8(data + 2) & 0x7F) == 0x07) { // error response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   415
        EC_ERR("SDO information error response at slave %i!\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   416
               slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   417
        ec_canopen_abort_msg(EC_READ_U32(data + 6));
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   418
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   419
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   420
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   421
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   422
    if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   423
        (EC_READ_U8 (data + 2) & 0x7F) != 0x02) { // Get OD List response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   424
        EC_ERR("Invalid SDO list response at slave %i!\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   425
               slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   426
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   427
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   428
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   429
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   430
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   431
    if (rec_size < 8) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   432
        EC_ERR("Invalid data size!\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   433
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   434
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   435
	return;
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   438
    sdo_count = (rec_size - 8) / 2;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   439
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   440
    for (i = 0; i < sdo_count; i++) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   441
        sdo_index = EC_READ_U16(data + 8 + i * 2);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   442
        if (!sdo_index) {
461
b9eda9235173 Print certain warnings only at debug_level.
Florian Pose <fp@igh-essen.com>
parents: 459
diff changeset
   443
            if (slave->master->debug_level)
b9eda9235173 Print certain warnings only at debug_level.
Florian Pose <fp@igh-essen.com>
parents: 459
diff changeset
   444
                EC_WARN("SDO dictionary of slave %i contains index 0x0000.\n",
b9eda9235173 Print certain warnings only at debug_level.
Florian Pose <fp@igh-essen.com>
parents: 459
diff changeset
   445
                        slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   446
            continue;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   447
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   448
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   449
        if (!(sdo = (ec_sdo_t *) kmalloc(sizeof(ec_sdo_t), GFP_ATOMIC))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   450
            EC_ERR("Failed to allocate memory for SDO!\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   451
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   452
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   453
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   454
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   455
        if (ec_sdo_init(sdo, sdo_index, slave)) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   456
            EC_ERR("Failed to init SDO!\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   457
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   458
            return;
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   461
        list_add_tail(&sdo->list, &slave->sdo_dictionary);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   462
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   463
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   464
    if (EC_READ_U8(data + 2) & 0x80) { // more messages waiting. check again.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   465
        fsm->cycles_start = datagram->cycles_sent;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   466
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   467
        ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   468
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   469
        fsm->state = ec_fsm_coe_dict_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   470
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   471
    }
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
    if (list_empty(&slave->sdo_dictionary)) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   474
        // no SDOs in dictionary. finished.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   475
        fsm->state = ec_fsm_coe_end; // success
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   476
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   477
    }
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
    // fetch SDO descriptions
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   480
    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
   481
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   482
    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
   483
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   484
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   485
    }
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
    EC_WRITE_U16(data, 0x8 << 12); // SDO information
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   488
    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
   489
    EC_WRITE_U8 (data + 3, 0x00);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   490
    EC_WRITE_U16(data + 4, 0x0000);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   491
    EC_WRITE_U16(data + 6, fsm->sdo->index); // SDO index
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   492
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   493
    ec_master_queue_datagram(fsm->slave->master, datagram);
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
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   510
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   511
        // FIXME: check for response first?
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   512
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   513
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   514
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   515
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   516
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   517
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   518
        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
   519
               " 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
   520
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   521
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   522
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   523
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   524
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   525
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   526
        EC_ERR("Reception of CoE SDO description request failed - slave %i did"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   527
               " not respond.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   528
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   529
    }
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
    fsm->cycles_start = datagram->cycles_sent;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   532
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   533
    ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   534
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   535
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   536
    fsm->state = ec_fsm_coe_dict_desc_check;
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
/*****************************************************************************/
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
   CoE state: DICT DESC CHECK.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   543
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   544
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   545
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
   546
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   547
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   548
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   549
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   550
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   551
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   552
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   553
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   554
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   555
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   556
        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
   557
        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
   558
                " (datagram state %i).\n",
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   559
                slave->ring_position, datagram->state);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   560
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   561
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   562
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   563
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   564
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   565
        EC_ERR("Reception of CoE mailbox check datagram failed - slave %i did"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   566
               " not respond.\n", slave->ring_position);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   567
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   568
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   569
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   570
    if (!ec_slave_mbox_check(datagram)) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   571
        if (datagram->cycles_received
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   572
            - fsm->cycles_start >= (cycles_t) 100 * cpu_khz) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   573
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   574
            EC_ERR("Timeout while checking SDO description on slave %i.\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   575
                   slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   576
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   577
        }
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
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   580
        ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   581
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   582
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   583
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   584
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   585
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   586
    ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   587
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   588
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   589
    fsm->state = ec_fsm_coe_dict_desc_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   590
}
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   594
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   595
   CoE state: DICT DESC RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   596
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   597
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   598
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   599
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
   600
                                   /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   601
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   602
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   603
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   604
    ec_sdo_t *sdo = fsm->sdo;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   605
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   606
    size_t rec_size, name_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   607
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   608
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   609
        // FIXME: request again?
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   610
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   611
        return;
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
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   614
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   615
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   616
        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
   617
               " 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
   618
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   619
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   620
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   621
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   622
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   623
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   624
        EC_ERR("Reception of CoE SDO description response failed - slave %i"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   625
               " did not respond.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   626
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   627
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   628
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   629
    if (!(data = ec_slave_mbox_fetch(slave, datagram,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   630
				     &mbox_prot, &rec_size))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   631
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   632
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   633
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   634
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   635
    if (mbox_prot != 0x03) { // CoE
446
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
   636
        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
   637
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   638
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   639
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   640
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   641
    if (EC_READ_U16(data) >> 12 == 0x8 && // SDO information
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   642
        (EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   643
        EC_ERR("SDO information error response at slave %i while"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   644
               " fetching SDO 0x%04X!\n", slave->ring_position,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   645
               sdo->index);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   646
        ec_canopen_abort_msg(EC_READ_U32(data + 6));
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   647
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   648
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   649
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   650
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   651
    if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   652
        (EC_READ_U8 (data + 2) & 0x7F) != 0x04 || // Object desc. response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   653
        EC_READ_U16(data + 6) != sdo->index) { // SDO index
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   654
        EC_ERR("Invalid object description response at slave %i while"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   655
               " fetching SDO 0x%04X!\n", slave->ring_position,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   656
               sdo->index);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   657
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   658
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   659
	return;
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   662
    if (rec_size < 12) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   663
        EC_ERR("Invalid data size!\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   664
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   665
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   666
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   667
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   668
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   669
    sdo->subindices = EC_READ_U8(data + 10);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   670
    sdo->object_code = EC_READ_U8(data + 11);
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
    name_size = rec_size - 12;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   673
    if (name_size) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   674
        if (!(sdo->name = kmalloc(name_size + 1, GFP_ATOMIC))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   675
            EC_ERR("Failed to allocate SDO name!\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   676
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   677
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   678
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   679
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   680
        memcpy(sdo->name, data + 12, name_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   681
        sdo->name[name_size] = 0;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   682
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   683
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   684
    if (EC_READ_U8(data + 2) & 0x80) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   685
        EC_ERR("Fragment follows (not implemented)!\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   686
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   687
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   688
    }
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
    // start fetching entries
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   691
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   692
    fsm->subindex = 0;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   693
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   694
    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
   695
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   696
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   697
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   698
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   699
    EC_WRITE_U16(data, 0x8 << 12); // SDO information
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   700
    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
   701
    EC_WRITE_U8 (data + 3, 0x00);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   702
    EC_WRITE_U16(data + 4, 0x0000);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   703
    EC_WRITE_U16(data + 6, sdo->index); // SDO index
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   704
    EC_WRITE_U8 (data + 8, fsm->subindex); // SDO subindex
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   705
    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
   706
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   707
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   708
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   709
    fsm->state = ec_fsm_coe_dict_entry_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   710
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   711
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   714
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   715
   CoE state: DICT ENTRY REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   716
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   717
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   718
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   719
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
   720
                                   /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   721
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   722
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   723
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   724
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   725
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   726
        // FIXME: check for response first?
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   727
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   728
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   729
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   730
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   731
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   732
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   733
        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
   734
               " 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
   735
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   736
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   737
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   738
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   739
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   740
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   741
        EC_ERR("Reception of CoE SDO entry request failed - slave %i did"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   742
               " not respond.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   743
        return;
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   746
    fsm->cycles_start = datagram->cycles_sent;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   747
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   748
    ec_slave_mbox_prepare_check(slave, datagram); // can not fail
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   749
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   750
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   751
    fsm->state = ec_fsm_coe_dict_entry_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   752
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   753
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   754
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   755
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   756
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   757
   CoE state: DICT ENTRY CHECK.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   758
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   759
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   760
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
   761
                                 /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   762
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   763
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   764
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   765
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   766
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   767
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   768
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   769
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   770
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   771
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   772
        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
   773
        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
   774
                " (datagram state %i).\n",
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   775
               slave->ring_position, datagram->state);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   776
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   777
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   778
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   779
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   780
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   781
        EC_ERR("Reception of CoE mailbox check datagram failed - slave %i did"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   782
               " not respond.\n", slave->ring_position);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   783
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   784
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   785
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   786
    if (!ec_slave_mbox_check(datagram)) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   787
        if (datagram->cycles_received
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   788
            - fsm->cycles_start >= (cycles_t) 100 * cpu_khz) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   789
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   790
            EC_ERR("Timeout while checking SDO entry on slave %i.\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   791
                   slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   792
            return;
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
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   796
        ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   797
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   798
        return;
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   801
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   802
    ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   803
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   804
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   805
    fsm->state = ec_fsm_coe_dict_entry_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   806
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   807
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   808
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   809
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   810
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   811
   CoE state: DICT ENTRY RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   812
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   813
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   814
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   815
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
   816
                                    /**< finite state machine */)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   817
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   818
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   819
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   820
    ec_sdo_t *sdo = fsm->sdo;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   821
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   822
    size_t rec_size, data_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   823
    ec_sdo_entry_t *entry;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   824
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   825
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   826
        // FIXME: request again?
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   827
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   828
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   829
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   830
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   831
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   832
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   833
        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
   834
               " 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
   835
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   836
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   837
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   838
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   839
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   840
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   841
        EC_ERR("Reception of CoE SDO description response failed - slave %i"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   842
               " did not respond.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   843
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   844
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   845
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   846
    if (!(data = ec_slave_mbox_fetch(slave, datagram,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   847
				     &mbox_prot, &rec_size))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   848
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   849
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   850
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   851
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   852
    if (mbox_prot != 0x03) { // CoE
446
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
   853
        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
   854
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   855
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   856
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   857
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   858
    if (EC_READ_U16(data) >> 12 == 0x8 && // SDO information
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   859
        (EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   860
        EC_ERR("SDO information error response at slave %i while"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   861
               " fetching SDO entry 0x%04X:%i!\n", slave->ring_position,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   862
               sdo->index, fsm->subindex);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   863
        ec_canopen_abort_msg(EC_READ_U32(data + 6));
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   864
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   865
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   866
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   867
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   868
    if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   869
        (EC_READ_U8(data + 2) & 0x7F) != 0x06 || // Entry desc. response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   870
        EC_READ_U16(data + 6) != sdo->index || // SDO index
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   871
        EC_READ_U8(data + 8) != fsm->subindex) { // SDO subindex
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   872
        EC_ERR("Invalid entry description response at slave %i while"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   873
               " fetching SDO entry 0x%04X:%i!\n", slave->ring_position,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   874
               sdo->index, fsm->subindex);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   875
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   876
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   877
	return;
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   880
    if (rec_size < 16) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   881
        EC_ERR("Invalid data size!\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   882
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   883
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   884
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   885
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   886
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   887
    data_size = rec_size - 16;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   888
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   889
    if (!(entry = (ec_sdo_entry_t *)
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   890
          kmalloc(sizeof(ec_sdo_entry_t), GFP_ATOMIC))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   891
        EC_ERR("Failed to allocate entry!\n");
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
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   895
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   896
    if (ec_sdo_entry_init(entry, fsm->subindex, sdo)) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   897
        EC_ERR("Failed to init entry!\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   898
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   899
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   900
    }
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
    entry->data_type = EC_READ_U16(data + 10);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   903
    entry->bit_length = EC_READ_U16(data + 12);
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_size) {
484
9fde4a17b820 kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents: 476
diff changeset
   906
        uint8_t *desc;
9fde4a17b820 kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents: 476
diff changeset
   907
        if (!(desc = kmalloc(data_size + 1, GFP_ATOMIC))) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   908
            EC_ERR("Failed to allocate SDO entry name!\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   909
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   910
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   911
        }
484
9fde4a17b820 kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents: 476
diff changeset
   912
        memcpy(desc, data + 16, data_size);
9fde4a17b820 kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents: 476
diff changeset
   913
        desc[data_size] = 0;
9fde4a17b820 kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents: 476
diff changeset
   914
        entry->description = desc;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   915
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   916
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   917
    list_add_tail(&entry->list, &sdo->entries);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   918
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   919
    if (fsm->subindex < sdo->subindices) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   920
        fsm->subindex++;
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
        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
   923
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   924
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   925
        }
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
        EC_WRITE_U16(data, 0x8 << 12); // SDO information
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   928
        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
   929
        EC_WRITE_U8 (data + 3, 0x00);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   930
        EC_WRITE_U16(data + 4, 0x0000);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   931
        EC_WRITE_U16(data + 6, sdo->index); // SDO index
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   932
        EC_WRITE_U8 (data + 8, fsm->subindex); // SDO subindex
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   933
        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
   934
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   935
        ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   936
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   937
        fsm->state = ec_fsm_coe_dict_entry_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   938
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   939
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   940
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   941
    // another SDO description to fetch?
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   942
    if (fsm->sdo->list.next != &slave->sdo_dictionary) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   943
        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
   944
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   945
        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
   946
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   947
            return;
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
        EC_WRITE_U16(data, 0x8 << 12); // SDO information
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   951
        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
   952
        EC_WRITE_U8 (data + 3, 0x00);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   953
        EC_WRITE_U16(data + 4, 0x0000);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   954
        EC_WRITE_U16(data + 6, fsm->sdo->index); // SDO index
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_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
   957
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   958
        fsm->state = ec_fsm_coe_dict_desc_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   959
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   960
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   961
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   962
    fsm->state = ec_fsm_coe_end;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   963
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   964
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   965
/******************************************************************************
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   966
 *  CoE state machine
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   967
 *****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   968
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   969
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   970
   CoE state: DOWN START.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   971
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   972
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   973
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
   974
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   975
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   976
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   977
    ec_sdo_data_t *sdodata = fsm->sdodata;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   978
    uint8_t *data;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   979
459
f0da52cf6b5b Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents: 446
diff changeset
   980
    if (fsm->slave->master->debug_level)
f0da52cf6b5b Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents: 446
diff changeset
   981
        EC_DBG("Downloading SDO 0x%04X:%i to slave %i.\n",
f0da52cf6b5b Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents: 446
diff changeset
   982
               sdodata->index, sdodata->subindex, slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   983
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   984
    if (slave->sii_rx_mailbox_size < 6 + 10 + sdodata->size) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   985
        EC_ERR("SDO fragmenting not supported yet!\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   986
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   987
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   988
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   989
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   990
    if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   991
                                            sdodata->size + 10))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   992
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   993
        return;
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
    EC_WRITE_U16(data, 0x2 << 12); // SDO request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   997
    EC_WRITE_U8 (data + 2, (0x1 // size specified
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   998
                            | 0x1 << 5)); // Download request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   999
    EC_WRITE_U16(data + 3, sdodata->index);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1000
    EC_WRITE_U8 (data + 5, sdodata->subindex);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1001
    EC_WRITE_U32(data + 6, sdodata->size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1002
    memcpy(data + 10, sdodata->data, sdodata->size);
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_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1005
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1006
    fsm->state = ec_fsm_coe_down_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1007
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1008
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1009
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1010
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1011
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1012
   CoE state: DOWN REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1013
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1014
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1015
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1016
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
  1017
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1018
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1019
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1020
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1021
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1022
        // FIXME: check for response first?
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1023
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1024
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1025
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1026
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1027
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1028
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1029
        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
  1030
               " 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
  1031
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1032
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1033
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1034
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1035
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1036
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1037
        EC_ERR("Reception of CoE download request failed - slave %i did not"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1038
               " respond.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1039
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1040
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1041
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1042
    fsm->cycles_start = datagram->cycles_sent;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1043
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1044
    ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1045
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1046
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1047
    fsm->state = ec_fsm_coe_down_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1048
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1049
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1050
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1051
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1052
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1053
   CoE state: DOWN CHECK.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1054
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1055
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1056
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
  1057
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1058
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1059
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1060
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1061
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1062
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1063
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1064
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1065
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1066
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1067
        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
  1068
        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
  1069
                " (datagram state %i).\n",
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
  1070
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1071
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1072
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1073
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1074
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1075
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1076
        EC_ERR("Reception of CoE mailbox check datagram failed - slave %i did"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1077
               " not respond.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1078
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1079
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1080
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1081
    if (!ec_slave_mbox_check(datagram)) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1082
        if (datagram->cycles_received
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1083
            - fsm->cycles_start >= (cycles_t) 100 * cpu_khz) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1084
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1085
            EC_ERR("Timeout while checking SDO configuration on slave %i.\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1086
                   slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1087
            return;
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
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1091
        ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1092
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1093
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1094
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1095
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1096
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1097
    ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1098
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1099
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1100
    fsm->state = ec_fsm_coe_down_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1101
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1102
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1103
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1104
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1105
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1106
   CoE state: DOWN RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1107
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1108
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1109
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1110
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
  1111
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1112
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1113
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1114
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1115
    size_t rec_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1116
    ec_sdo_data_t *sdodata = fsm->sdodata;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1117
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1118
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1119
        // FIXME: request again?
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1120
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1121
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1122
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1123
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1124
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1125
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1126
        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
  1127
               " 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
  1128
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1129
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1130
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1131
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1132
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1133
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1134
        EC_ERR("Reception of CoE download response failed - slave %i did not"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1135
               " respond.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1136
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1137
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1138
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1139
    if (!(data = ec_slave_mbox_fetch(slave, datagram,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1140
				     &mbox_prot, &rec_size))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1141
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1142
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1143
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1144
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1145
    if (mbox_prot != 0x03) { // CoE
446
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1146
        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
  1147
        fsm->state = ec_fsm_coe_error;
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1151
    if (rec_size < 6) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1152
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1153
        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
  1154
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1155
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1156
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1157
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1158
    if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1159
        EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1160
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1161
        EC_ERR("SDO download 0x%04X:%X (%i bytes) aborted on slave %i.\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1162
               sdodata->index, sdodata->subindex, sdodata->size,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1163
               slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1164
        if (rec_size < 10) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1165
            EC_ERR("Incomplete Abort command:\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1166
            ec_print_data(data, rec_size);
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
        else
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1169
            ec_canopen_abort_msg(EC_READ_U32(data + 6));
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1170
        return;
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
    if (EC_READ_U16(data) >> 12 != 0x3 || // SDO response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1174
        EC_READ_U8 (data + 2) >> 5 != 0x3 || // Download response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1175
        EC_READ_U16(data + 3) != sdodata->index || // index
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1176
        EC_READ_U8 (data + 5) != sdodata->subindex) { // subindex
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1177
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1178
        EC_ERR("SDO download 0x%04X:%X (%i bytes) failed:\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1179
               sdodata->index, sdodata->subindex, sdodata->size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1180
        EC_ERR("Invalid SDO download response at slave %i!\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1181
               slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1182
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1183
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1184
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1185
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1186
    fsm->state = ec_fsm_coe_end; // success
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1187
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1188
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1189
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1190
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1191
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1192
   CoE state: UP START.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1193
*/
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
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
  1196
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1197
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1198
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1199
    ec_master_t *master = slave->master;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1200
    ec_sdo_request_t *request = fsm->request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1201
    ec_sdo_t *sdo = request->sdo;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1202
    ec_sdo_entry_t *entry = request->entry;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1203
    uint8_t *data;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1204
459
f0da52cf6b5b Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents: 446
diff changeset
  1205
    if (master->debug_level)
f0da52cf6b5b Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents: 446
diff changeset
  1206
        EC_DBG("Uploading SDO 0x%04X:%i from slave %i.\n",
f0da52cf6b5b Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents: 446
diff changeset
  1207
               sdo->index, entry->subindex, slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1208
476
fd7c0ee645f8 Fixed CoE Upload Request, now 10 bytes of Mailbox Service Data.
Florian Pose <fp@igh-essen.com>
parents: 461
diff changeset
  1209
    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
  1210
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1211
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1212
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1213
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1214
    EC_WRITE_U16(data, 0x2 << 12); // SDO request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1215
    EC_WRITE_U8 (data + 2, 0x2 << 5); // initiate upload request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1216
    EC_WRITE_U16(data + 3, sdo->index);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1217
    EC_WRITE_U8 (data + 5, entry->subindex);
476
fd7c0ee645f8 Fixed CoE Upload Request, now 10 bytes of Mailbox Service Data.
Florian Pose <fp@igh-essen.com>
parents: 461
diff changeset
  1218
    memset(data + 6, 0x00, 4);
436
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
    if (master->debug_level) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1221
        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
  1222
        ec_print_data(data, 10);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1223
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1224
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1225
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1226
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1227
    fsm->state = ec_fsm_coe_up_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1228
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1229
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1230
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1231
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1232
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1233
   CoE state: UP REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1234
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1235
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1236
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1237
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
  1238
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1239
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1240
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1241
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1242
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1243
        // FIXME: check for response first?
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1244
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1245
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1246
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1247
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1248
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1249
        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
  1250
        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
  1251
                " (datagram state %i).\n",
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
  1252
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1253
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1254
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1255
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1256
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1257
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1258
        EC_ERR("Reception of CoE upload request failed - slave %i did not"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1259
               " respond.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1260
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1261
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1262
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1263
    fsm->cycles_start = datagram->cycles_sent;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1264
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1265
    ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1266
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1267
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1268
    fsm->state = ec_fsm_coe_up_check;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1269
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1270
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1271
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1272
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1273
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1274
   CoE state: UP CHECK.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1275
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1276
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1277
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
  1278
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1279
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1280
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1281
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1282
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1283
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1284
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1285
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1286
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1287
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1288
        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
  1289
        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
  1290
                " (datagram state %i).\n",
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
  1291
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1292
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1293
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1294
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1295
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1296
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1297
        EC_ERR("Reception of CoE mailbox check datagram failed - slave %i did"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1298
               " not respond.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1299
        return;
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
    if (!ec_slave_mbox_check(datagram)) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1303
        if (datagram->cycles_received
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1304
            - fsm->cycles_start >= (cycles_t) 100 * cpu_khz) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1305
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1306
            EC_ERR("Timeout while checking SDO upload on slave %i.\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1307
                   slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1308
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1309
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1310
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1311
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1312
        ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1313
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1314
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1315
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1316
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1317
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1318
    ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1319
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1320
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1321
    fsm->state = ec_fsm_coe_up_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1322
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1323
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1324
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1325
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1326
/**
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1327
   CoE state: UP RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1328
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1329
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1330
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1331
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
  1332
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1333
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1334
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1335
    ec_master_t *master = slave->master;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1336
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1337
    size_t rec_size, data_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1338
    ec_sdo_request_t *request = fsm->request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1339
    ec_sdo_t *sdo = request->sdo;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1340
    ec_sdo_entry_t *entry = request->entry;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1341
    uint32_t complete_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1342
    unsigned int expedited, size_specified;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1343
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1344
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1345
        // FIXME: request again?
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1346
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1347
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1348
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1349
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1350
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1351
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1352
        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
  1353
               " 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
  1354
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1355
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1356
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1357
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1358
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1359
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1360
        EC_ERR("Reception of CoE upload response failed - slave %i did not"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1361
               " respond.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1362
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1363
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1364
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1365
    if (!(data = ec_slave_mbox_fetch(slave, datagram,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1366
				     &mbox_prot, &rec_size))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1367
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1368
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1369
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1370
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1371
    if (master->debug_level) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1372
        EC_DBG("Upload response:\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1373
        ec_print_data(data, rec_size);
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1376
    if (mbox_prot != 0x03) { // CoE
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1377
        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
  1378
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1379
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1380
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1381
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1382
    if (rec_size < 10) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1383
        EC_ERR("Received currupted SDO upload response!\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1384
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1385
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1386
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1387
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1388
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1389
    if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1390
        EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1391
        EC_ERR("SDO upload 0x%04X:%X aborted on slave %i.\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1392
               sdo->index, entry->subindex, slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1393
        ec_canopen_abort_msg(EC_READ_U32(data + 6));
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1394
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1395
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1396
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1397
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1398
    if (EC_READ_U16(data) >> 12 != 0x3 || // SDO response
476
fd7c0ee645f8 Fixed CoE Upload Request, now 10 bytes of Mailbox Service Data.
Florian Pose <fp@igh-essen.com>
parents: 461
diff changeset
  1399
        EC_READ_U8 (data + 2) >> 5 != 0x2 || // upload response
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1400
        EC_READ_U16(data + 3) != sdo->index || // index
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1401
        EC_READ_U8 (data + 5) != entry->subindex) { // subindex
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1402
        EC_ERR("SDO upload 0x%04X:%X failed:\n", sdo->index, entry->subindex);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1403
        EC_ERR("Invalid SDO upload response at slave %i!\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1404
               slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1405
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1406
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1407
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1408
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1409
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1410
    data_size = rec_size - 10;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1411
    expedited = EC_READ_U8(data + 2) & 0x02;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1412
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1413
    if (expedited) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1414
        size_specified = EC_READ_U8(data + 2) & 0x01;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1415
        if (size_specified) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1416
            complete_size = 4 - ((EC_READ_U8(data + 2) & 0x0C) >> 2);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1417
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1418
        else {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1419
            complete_size = 4;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1420
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1421
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1422
    else {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1423
        complete_size = EC_READ_U32(data + 6);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1424
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1425
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1426
    if (request->data) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1427
        kfree(request->data);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1428
        request->data = NULL;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1429
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1430
    request->size = 0;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1431
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1432
    if (complete_size) {
446
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1433
        if (!(request->data = (uint8_t *)
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1434
              kmalloc(complete_size + 1, GFP_ATOMIC))) {
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1435
            EC_ERR("Failed to allocate %i bytes of SDO data!\n",
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1436
                   complete_size);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1437
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1438
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1439
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1440
        request->data[complete_size] = 0x00; // just to be sure...
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1441
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1442
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1443
    if (expedited) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1444
        memcpy(request->data, data + 6, complete_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1445
        request->size = complete_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1446
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1447
    else {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1448
        memcpy(request->data, data + 10, data_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1449
        request->size = data_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1450
        fsm->toggle = 0;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1451
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1452
        if (data_size < complete_size) {
446
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1453
            EC_WARN("SDO data incomplete (%i / %i).\n",
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1454
                    data_size, complete_size);
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1455
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1456
            if (!(data = ec_slave_mbox_prepare_send(slave, datagram,
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1457
                                                    0x03, 3))) {
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1458
                fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1459
                return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1460
            }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1461
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1462
            EC_WRITE_U16(data, 0x2 << 12); // SDO request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1463
            EC_WRITE_U8 (data + 2, (fsm->toggle << 4 // toggle
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1464
                                    | 0x3 << 5)); // upload segment request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1465
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1466
            if (master->debug_level) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1467
                EC_DBG("Upload segment request:\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1468
                ec_print_data(data, 3);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1469
            }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1470
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1471
            ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1472
            fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1473
            fsm->state = ec_fsm_coe_up_seg_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1474
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1475
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1476
    }
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
    fsm->state = ec_fsm_coe_end; // success
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1479
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1480
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1481
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1482
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
   CoE state: UP REQUEST.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1485
   \todo Timeout behavior
436
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
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
  1489
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1490
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1491
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1492
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1493
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1494
        // FIXME: check for response first?
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1495
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1496
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1497
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1498
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1499
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1500
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1501
        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
  1502
               " 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
  1503
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1504
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1505
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1506
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1507
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1508
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1509
        EC_ERR("Reception of CoE upload segment request failed - slave %i did"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1510
               " not respond.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1511
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1512
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1513
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1514
    fsm->cycles_start = datagram->cycles_sent;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1515
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1516
    ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1517
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1518
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1519
    fsm->state = ec_fsm_coe_up_seg_check;
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
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1522
/*****************************************************************************/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1523
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
   CoE state: UP CHECK.
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
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
  1529
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1530
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1531
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1532
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1533
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1534
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1535
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1536
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1537
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1538
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1539
        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
  1540
        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
  1541
                " (datagram state %i).\n",
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
  1542
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1543
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1544
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1545
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1546
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1547
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1548
        EC_ERR("Reception of CoE mailbox check datagram failed - slave %i did"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1549
               " not respond.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1550
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1551
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1552
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1553
    if (!ec_slave_mbox_check(datagram)) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1554
        if (datagram->cycles_received
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1555
            - fsm->cycles_start >= (cycles_t) 100 * cpu_khz) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1556
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1557
            EC_ERR("Timeout while checking SDO upload segment on slave %i.\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1558
                   slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1559
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1560
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1561
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1562
        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1563
        ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1564
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1565
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1566
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1567
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1568
    // Fetch response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1569
    ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1570
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1571
    fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1572
    fsm->state = ec_fsm_coe_up_seg_response;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1573
}
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
   CoE state: UP RESPONSE.
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1579
   \todo Timeout behavior
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1580
*/
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1581
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1582
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
  1583
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1584
    ec_datagram_t *datagram = fsm->datagram;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1585
    ec_slave_t *slave = fsm->slave;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1586
    ec_master_t *master = slave->master;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1587
    uint8_t *data, mbox_prot;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1588
    size_t rec_size, data_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1589
    ec_sdo_request_t *request = fsm->request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1590
    ec_sdo_t *sdo = request->sdo;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1591
    ec_sdo_entry_t *entry = request->entry;
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
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1595
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1596
        // FIXME: request again?
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1597
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1598
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1599
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1600
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1601
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1602
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1603
        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
  1604
               " 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
  1605
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1606
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1607
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1608
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1609
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1610
        fsm->state = ec_fsm_coe_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1611
        EC_ERR("Reception of CoE upload segment response failed - slave %i"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1612
               " did not respond.\n", slave->ring_position);
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1613
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1614
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1615
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1616
    if (!(data = ec_slave_mbox_fetch(slave, datagram,
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1617
				     &mbox_prot, &rec_size))) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1618
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1619
        return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1620
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1621
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1622
    if (master->debug_level) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1623
        EC_DBG("Upload segment response:\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1624
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1625
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1626
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1627
    if (mbox_prot != 0x03) { // CoE
446
8fede404c01f Persistent slave lists.
Florian Pose <fp@igh-essen.com>
parents: 436
diff changeset
  1628
        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
  1629
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1630
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1631
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1632
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1633
    if (rec_size < 10) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1634
        EC_ERR("Received currupted SDO upload segment response!\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1635
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1636
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1637
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1638
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1639
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1640
    if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1641
        EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1642
        EC_ERR("SDO upload 0x%04X:%X aborted on slave %i.\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1643
               sdo->index, entry->subindex, slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1644
        ec_canopen_abort_msg(EC_READ_U32(data + 6));
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1645
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1646
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1647
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1648
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1649
    if (EC_READ_U16(data) >> 12 != 0x3 || // SDO response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1650
        EC_READ_U8 (data + 2) >> 5 != 0x0) { // upload segment response
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1651
        EC_ERR("SDO upload 0x%04X:%X failed:\n", sdo->index, entry->subindex);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1652
        EC_ERR("Invalid SDO upload segment response at slave %i!\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1653
               slave->ring_position);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1654
        ec_print_data(data, rec_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1655
        fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1656
	return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1657
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1658
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1659
    last_segment = EC_READ_U8(data + 2) & 0x01;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1660
    seg_size = (EC_READ_U8(data + 2) & 0xE) >> 1;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1661
    data_size = rec_size - 10;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1662
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1663
    if (data_size != seg_size) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1664
        EC_WARN("SDO segment data invalid (%i / %i)"
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1665
                " - Fragmenting not implemented.\n",
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1666
                data_size, seg_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1667
    }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1668
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1669
    memcpy(request->data + request->size, data + 10, data_size);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1670
    request->size += data_size;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1671
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1672
    if (!last_segment) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1673
        fsm->toggle = !fsm->toggle;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1674
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1675
        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
  1676
            fsm->state = ec_fsm_coe_error;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1677
            return;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1678
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1679
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1680
        EC_WRITE_U16(data, 0x2 << 12); // SDO request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1681
        EC_WRITE_U8 (data + 2, (fsm->toggle << 4 // toggle
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1682
                                | 0x3 << 5)); // upload segment request
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1683
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1684
        if (master->debug_level) {
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1685
            EC_DBG("Upload segment request:\n");
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1686
            ec_print_data(data, 3);
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1687
        }
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1688
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1689
        ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 501
diff changeset
  1690
        fsm->retries = EC_FSM_RETRIES;
436
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1691
        fsm->state = ec_fsm_coe_up_seg_request;
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1692
        return;
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
    fsm->state = ec_fsm_coe_end; // success
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
/*****************************************************************************/
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
   State: ERROR.
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
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
  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
/*****************************************************************************/
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
   State: END.
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
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
  1715
{
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1716
}
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1717
63214beb641d Layed out CoE state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
  1718
/*****************************************************************************/