master/fsm_change.c
author Florian Pose <fp@igh-essen.com>
Thu, 09 Aug 2007 14:38:18 +0000
changeset 689 880b556a6efc
parent 637 d5d04c868e0e
child 713 ae41cadd25b6
permissions -rw-r--r--
Removed checking for NULL slave pointer in EoE handler; added comments.
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/******************************************************************************
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 *  $Id$
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
 *
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
 *  This file is part of the IgH EtherCAT Master.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
 *
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
 *  The IgH EtherCAT Master is free software; you can redistribute it
0180d8277311 Layed out state change 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
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
 *  as published by the Free Software Foundation; either version 2 of the
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *  License, or (at your option) any later version.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *
0180d8277311 Layed out state change 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
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *  GNU General Public License for more details.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *  You should have received a copy of the GNU General Public License
0180d8277311 Layed out state change 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
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
 *  The right to use EtherCAT Technology is granted and comes free of
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *  charge under condition of compatibility of product made by
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *  Licensee. People intending to distribute/sell products based on the
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
 *  code, have to sign an agreement to guarantee that products using
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
 *  software based on IgH EtherCAT master stay compatible with the actual
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 *  EtherCAT specification (which are released themselves as an open
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
 *  standard) as the (only) precondition to have the right to use EtherCAT
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
 *  Technology, IP and trade marks.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
 *
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
 *****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
   \file
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
   EtherCAT state change FSM.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
#include "globals.h"
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
#include "master.h"
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
#include "fsm_change.h"
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    47
void ec_fsm_change_state_start(ec_fsm_change_t *);
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    48
void ec_fsm_change_state_check(ec_fsm_change_t *);
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    49
void ec_fsm_change_state_status(ec_fsm_change_t *);
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    50
void ec_fsm_change_state_code(ec_fsm_change_t *);
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    51
void ec_fsm_change_state_start_ack(ec_fsm_change_t *);
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    52
void ec_fsm_change_state_ack(ec_fsm_change_t *);
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    53
void ec_fsm_change_state_check_ack(ec_fsm_change_t *);
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    54
void ec_fsm_change_state_end(ec_fsm_change_t *);
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    55
void ec_fsm_change_state_error(ec_fsm_change_t *);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
   Constructor.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
void ec_fsm_change_init(ec_fsm_change_t *fsm, /**< finite state machine */
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
                        ec_datagram_t *datagram /**< datagram */
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
                        )
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
    fsm->state = NULL;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
    fsm->datagram = datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
   Destructor.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
void ec_fsm_change_clear(ec_fsm_change_t *fsm /**< finite state machine */)
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
/**
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    84
   Starts the change state machine.
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    85
*/
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    86
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    87
void ec_fsm_change_start(ec_fsm_change_t *fsm, /**< finite state machine */
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    88
                         ec_slave_t *slave, /**< EtherCAT slave */
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    89
                         ec_slave_state_t state /**< requested state */
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    90
                         )
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    91
{
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    92
    fsm->mode = EC_FSM_CHANGE_MODE_FULL;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
    fsm->slave = slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
    fsm->requested_state = state;
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    95
    fsm->state = ec_fsm_change_state_start;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    96
}
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    97
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    98
/*****************************************************************************/
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    99
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   100
/**
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   101
   Starts the change state machine to only acknowlegde a slave's state.
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   102
*/
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   103
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   104
void ec_fsm_change_ack(ec_fsm_change_t *fsm, /**< finite state machine */
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   105
                       ec_slave_t *slave /**< EtherCAT slave */
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   106
                       )
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   107
{
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   108
    fsm->mode = EC_FSM_CHANGE_MODE_ACK_ONLY;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   109
    fsm->slave = slave;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   110
    fsm->requested_state = EC_SLAVE_STATE_UNKNOWN;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   111
    fsm->state = ec_fsm_change_state_start_ack;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
   Executes the current state of the state machine.
435
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 434
diff changeset
   118
   \return false, if the state machine has terminated
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 434
diff changeset
   119
*/
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 434
diff changeset
   120
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 434
diff changeset
   121
int ec_fsm_change_exec(ec_fsm_change_t *fsm /**< finite state machine */)
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
    fsm->state(fsm);
435
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 434
diff changeset
   124
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   125
    return fsm->state != ec_fsm_change_state_end
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   126
        && fsm->state != ec_fsm_change_state_error;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
   Returns, if the state machine terminated with success.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
   \return non-zero if successful.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
int ec_fsm_change_success(ec_fsm_change_t *fsm /**< Finite state machine */)
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
{
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   138
    return fsm->state == ec_fsm_change_state_end;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
/******************************************************************************
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
 *  state change state machine
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   143
 *****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
   Change state: START.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   149
void ec_fsm_change_state_start(ec_fsm_change_t *fsm
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   150
                               /**< finite state machine */)
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   153
    ec_slave_t *slave = fsm->slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   154
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   155
    fsm->take_time = 1;
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   156
    fsm->old_state = fsm->slave->current_state;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   157
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   158
    // write new state to slave
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
    ec_datagram_npwr(datagram, slave->station_address, 0x0120, 2);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   160
    EC_WRITE_U16(datagram->data, fsm->requested_state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   161
    fsm->retries = EC_FSM_RETRIES;
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   162
    fsm->state = ec_fsm_change_state_check;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   165
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   166
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   167
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   168
   Change state: CHECK.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   171
void ec_fsm_change_state_check(ec_fsm_change_t *fsm
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   172
                               /**< finite state machine */)
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   173
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   175
    ec_slave_t *slave = fsm->slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   177
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   178
        return;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   179
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   180
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   181
        fsm->state = ec_fsm_change_state_error;
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   182
        EC_ERR("Failed to receive state datagram from slave %i"
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   183
                " (datagram state %i)!\n",
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   184
               fsm->slave->ring_position, datagram->state);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
    if (fsm->take_time) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   189
        fsm->take_time = 0;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   190
        fsm->jiffies_start = datagram->jiffies_sent;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   191
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   192
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   193
    if (datagram->working_counter != 1) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
        if (datagram->jiffies_received - fsm->jiffies_start >= 3 * HZ) {
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   195
            char state_str[EC_STATE_STRING_SIZE];
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   196
            ec_state_string(fsm->requested_state, state_str);
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   197
            fsm->state = ec_fsm_change_state_error;
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   198
            EC_ERR("Failed to set state %s on slave %i: Slave did not"
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   199
                   " respond.\n", state_str, fsm->slave->ring_position);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
            return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
        }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
        // repeat writing new state to slave
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
        ec_datagram_npwr(datagram, slave->station_address, 0x0120, 2);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
        EC_WRITE_U16(datagram->data, fsm->requested_state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   206
        fsm->retries = EC_FSM_RETRIES;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   208
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
    fsm->take_time = 1;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   211
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   212
    // read AL status from slave
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   213
    ec_datagram_nprd(datagram, slave->station_address, 0x0130, 2);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   214
    fsm->retries = EC_FSM_RETRIES;
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   215
    fsm->state = ec_fsm_change_state_status;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   217
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   218
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   219
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   221
   Change state: STATUS.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   222
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   223
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   224
void ec_fsm_change_state_status(ec_fsm_change_t *fsm
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   225
                                /**< finite state machine */)
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   226
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   227
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   228
    ec_slave_t *slave = fsm->slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   229
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   230
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   231
        return;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   232
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   233
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   234
        fsm->state = ec_fsm_change_state_error;
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   235
        EC_ERR("Failed to receive state checking datagram from slave %i"
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   236
                " (datagram state %i).\n",
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   237
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   238
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   239
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   240
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   241
    if (datagram->working_counter != 1) {
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   242
        char req_state[EC_STATE_STRING_SIZE];
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   243
        ec_state_string(fsm->requested_state, req_state);
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   244
        fsm->state = ec_fsm_change_state_error;
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   245
        EC_ERR("Failed to check state %s on slave %i.\n",
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   246
               req_state, slave->ring_position);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   247
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   248
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   249
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   250
    if (fsm->take_time) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   251
        fsm->take_time = 0;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   252
        fsm->jiffies_start = datagram->jiffies_sent;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   253
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   254
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   255
    slave->current_state = EC_READ_U8(datagram->data);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   256
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   257
    if (slave->current_state == fsm->requested_state) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   258
        // state has been set successfully
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   259
        fsm->state = ec_fsm_change_state_end;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   260
        return;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   261
    }
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   262
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   263
    if (slave->current_state != fsm->old_state) { // state changed
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   264
        char req_state[EC_STATE_STRING_SIZE], cur_state[EC_STATE_STRING_SIZE];
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   265
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   266
        ec_state_string(slave->current_state, cur_state);
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   267
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   268
        if ((slave->current_state & 0x0F) != (fsm->old_state & 0x0F)) {
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   269
            // Slave spontaneously changed its state just before the new state
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   270
            // was written. Accept current state as old state and wait for
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   271
            // state change
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   272
            fsm->old_state = slave->current_state;
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   273
            EC_WARN("Slave %i changed to %s in the meantime.\n",
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   274
                    slave->ring_position, cur_state);
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   275
            goto again;
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   276
        }
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   277
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   278
        // state change error
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   279
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   280
        slave->error_flag = 1;
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   281
        ec_state_string(fsm->requested_state, req_state);
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   282
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   283
        EC_ERR("Failed to set %s state, slave %i refused state change (%s).\n",
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   284
               req_state, slave->ring_position, cur_state);
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   285
        // fetch AL status error code
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   286
        ec_datagram_nprd(datagram, slave->station_address, 0x0134, 2);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   287
        fsm->retries = EC_FSM_RETRIES;
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   288
        fsm->state = ec_fsm_change_state_code;
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   289
        return;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   290
    }
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   291
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   292
    // still old state
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   293
439
0f5a3d983908 Increased state change checking timeout to 1s.
Florian Pose <fp@igh-essen.com>
parents: 438
diff changeset
   294
    if (datagram->jiffies_received - fsm->jiffies_start >= HZ) { // 1s
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   295
        // timeout while checking
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   296
        char state_str[EC_STATE_STRING_SIZE];
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   297
        ec_state_string(fsm->requested_state, state_str);
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   298
        fsm->state = ec_fsm_change_state_error;
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   299
        EC_ERR("Timeout while setting state %s on slave %i.\n",
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   300
               state_str, slave->ring_position);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   301
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   302
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   303
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   304
 again:
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   305
    // no timeout yet. check again
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   306
    ec_datagram_nprd(datagram, slave->station_address, 0x0130, 2);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   307
    fsm->retries = EC_FSM_RETRIES;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   308
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   309
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   310
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   311
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   312
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   313
   Application layer status messages.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   314
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   315
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   316
const ec_code_msg_t al_status_messages[] = {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   317
    {0x0001, "Unspecified error"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   318
    {0x0011, "Invalud requested state change"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   319
    {0x0012, "Unknown requested state"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   320
    {0x0013, "Bootstrap not supported"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   321
    {0x0014, "No valid firmware"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   322
    {0x0015, "Invalid mailbox configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   323
    {0x0016, "Invalid mailbox configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   324
    {0x0017, "Invalid sync manager configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   325
    {0x0018, "No valid inputs available"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   326
    {0x0019, "No valid outputs"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   327
    {0x001A, "Synchronisation error"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   328
    {0x001B, "Sync manager watchdog"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   329
    {0x001C, "Invalid sync manager types"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   330
    {0x001D, "Invalid output configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   331
    {0x001E, "Invalid input configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   332
    {0x001F, "Invalid watchdog configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   333
    {0x0020, "Slave needs cold start"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   334
    {0x0021, "Slave needs INIT"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   335
    {0x0022, "Slave needs PREOP"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   336
    {0x0023, "Slave needs SAVEOP"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   337
    {0x0030, "Invalid DC SYNCH configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   338
    {0x0031, "Invalid DC latch configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   339
    {0x0032, "PLL error"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   340
    {0x0033, "Invalid DC IO error"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   341
    {0x0034, "Invalid DC timeout error"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   342
    {0x0042, "MBOX EOE"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   343
    {0x0043, "MBOX COE"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   344
    {0x0044, "MBOX FOE"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   345
    {0x0045, "MBOX SOE"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   346
    {0x004F, "MBOX VOE"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   347
    {}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   348
};
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   349
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   350
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   351
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   352
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   353
   Change state: CODE.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   354
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   355
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   356
void ec_fsm_change_state_code(ec_fsm_change_t *fsm
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   357
                              /**< finite state machine */)
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   358
{
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   359
    ec_datagram_t *datagram = fsm->datagram;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   360
    uint32_t code;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   361
    const ec_code_msg_t *al_msg;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   362
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   363
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   364
        return;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   365
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   366
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   367
        fsm->state = ec_fsm_change_state_error;
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   368
        EC_ERR("Failed to receive AL status code datagram from slave %i"
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   369
                " (datagram state %i).\n",
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   370
               fsm->slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   371
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   372
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   373
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   374
    if (datagram->working_counter != 1) {
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   375
        EC_WARN("Reception of AL status code datagram failed.\n");
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   376
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   377
    else {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   378
        if ((code = EC_READ_U16(datagram->data))) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   379
            for (al_msg = al_status_messages; al_msg->code; al_msg++) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   380
                if (al_msg->code != code) continue;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   381
                EC_ERR("AL status message 0x%04X: \"%s\".\n",
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   382
                       al_msg->code, al_msg->message);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   383
                break;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   384
            }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   385
            if (!al_msg->code)
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   386
                EC_ERR("Unknown AL status code 0x%04X.\n", code);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   387
        }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   388
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   389
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   390
    // acknowledge "old" slave state
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   391
    ec_fsm_change_state_start_ack(fsm); // execute immediately
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   392
}
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   393
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   394
/*****************************************************************************/
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   395
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   396
/**
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   397
   Change state: START ACK.
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   398
*/
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   399
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   400
void ec_fsm_change_state_start_ack(ec_fsm_change_t *fsm
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   401
                                   /**< finite state machine */)
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   402
{
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   403
    ec_slave_t *slave = fsm->slave;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   404
    ec_datagram_t *datagram = fsm->datagram;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   405
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   406
    ec_datagram_npwr(datagram, slave->station_address, 0x0120, 2);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   407
    EC_WRITE_U16(datagram->data, slave->current_state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   408
    fsm->retries = EC_FSM_RETRIES;
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   409
    fsm->state = ec_fsm_change_state_ack;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   410
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   411
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   412
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   413
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   414
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   415
   Change state: ACK.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   416
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   417
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   418
void ec_fsm_change_state_ack(ec_fsm_change_t *fsm /**< finite state machine */)
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   419
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   420
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   421
    ec_slave_t *slave = fsm->slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   422
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   423
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   424
        return;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   425
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   426
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   427
        fsm->state = ec_fsm_change_state_error;
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   428
        EC_ERR("Failed to receive state ack datagram for slave %i"
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   429
                " (datagram state %i).\n",
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   430
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   431
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   432
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   433
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   434
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   435
        fsm->state = ec_fsm_change_state_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   436
        EC_ERR("Reception of state ack datagram failed - slave %i did not"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   437
               " respond.\n", slave->ring_position);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   438
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   439
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   440
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   441
    fsm->take_time = 1;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   442
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   443
    // read new AL status
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   444
    ec_datagram_nprd(datagram, slave->station_address, 0x0130, 2);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   445
    fsm->retries = EC_FSM_RETRIES;
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   446
    fsm->state = ec_fsm_change_state_check_ack;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   447
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   448
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   449
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   450
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   451
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   452
   Change state: CHECK ACK.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   453
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   454
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   455
void ec_fsm_change_state_check_ack(ec_fsm_change_t *fsm
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   456
                                   /**< finite state machine */)
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   457
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   458
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   459
    ec_slave_t *slave = fsm->slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   460
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   461
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   462
        return;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   463
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   464
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   465
        fsm->state = ec_fsm_change_state_error;
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   466
        EC_ERR("Failed to receive state ack check datagram from slave %i"
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   467
                " (datagram state %i).\n",
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   468
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   469
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   470
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   471
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   472
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   473
        fsm->state = ec_fsm_change_state_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   474
        EC_ERR("Reception of state ack check datagram failed - slave %i did"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   475
               " not respond.\n", slave->ring_position);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   476
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   477
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   478
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   479
    if (fsm->take_time) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   480
        fsm->take_time = 0;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   481
        fsm->jiffies_start = datagram->jiffies_sent;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   482
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   483
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   484
    slave->current_state = EC_READ_U8(datagram->data);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   485
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   486
    if (!(slave->current_state & EC_SLAVE_STATE_ACK_ERR)) {
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   487
        char state_str[EC_STATE_STRING_SIZE];
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   488
        ec_state_string(slave->current_state, state_str);
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   489
        if (fsm->mode == EC_FSM_CHANGE_MODE_FULL) {
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   490
            fsm->state = ec_fsm_change_state_error;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   491
        }
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   492
        else { // EC_FSM_CHANGE_MODE_ACK_ONLY
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   493
            fsm->state = ec_fsm_change_state_end;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   494
        }
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   495
        EC_INFO("Acknowledged state %s on slave %i.\n",
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   496
                state_str, slave->ring_position);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   497
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   498
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   499
443
0746236dd032 Fixes in state change FSM.
Florian Pose <fp@igh-essen.com>
parents: 439
diff changeset
   500
    if (datagram->jiffies_received - fsm->jiffies_start >= HZ) { // 1s
0746236dd032 Fixes in state change FSM.
Florian Pose <fp@igh-essen.com>
parents: 439
diff changeset
   501
        // timeout while checking
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   502
        char state_str[EC_STATE_STRING_SIZE];
443
0746236dd032 Fixes in state change FSM.
Florian Pose <fp@igh-essen.com>
parents: 439
diff changeset
   503
        ec_state_string(slave->current_state, state_str);
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   504
        fsm->state = ec_fsm_change_state_error;
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   505
        EC_ERR("Timeout while acknowledging state %s on slave %i.\n",
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   506
               state_str, slave->ring_position);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   507
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   508
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   509
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   510
    // reread new AL status
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   511
    ec_datagram_nprd(datagram, slave->station_address, 0x0130, 2);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   512
    fsm->retries = EC_FSM_RETRIES;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   513
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   514
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   515
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   516
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   517
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   518
   State: ERROR.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   519
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   520
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   521
void ec_fsm_change_state_error(ec_fsm_change_t *fsm
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   522
                               /**< finite state machine */)
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   523
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   524
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   525
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   526
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   527
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   528
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   529
   State: END.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   530
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   531
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   532
void ec_fsm_change_state_end(ec_fsm_change_t *fsm
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   533
                             /**< finite state machine */)
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   534
{
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   535
}
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   536
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   537
/*****************************************************************************/