master/fsm_change.c
author Florian Pose <fp@igh-essen.com>
Mon, 26 Jan 2009 16:35:50 +0000
changeset 1344 e5756d4ed12c
parent 1337 0253c74d0940
child 1363 11c0b2caa253
permissions -rw-r--r--
Debug message.
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
 *
1326
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1225
diff changeset
     5
 *  Copyright (C) 2006-2008  Florian Pose, Ingenieurgemeinschaft IgH
434
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
 *
1326
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1225
diff changeset
     9
 *  The IgH EtherCAT Master is free software; you can redistribute it and/or
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1225
diff changeset
    10
 *  modify it under the terms of the GNU General Public License version 2, as
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1225
diff changeset
    11
 *  published by the Free Software Foundation.
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1225
diff changeset
    12
 *
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1225
diff changeset
    13
 *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1225
diff changeset
    14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1225
diff changeset
    15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1225
diff changeset
    16
 *  Public License for more details.
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1225
diff changeset
    17
 *
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1225
diff changeset
    18
 *  You should have received a copy of the GNU General Public License along
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1225
diff changeset
    19
 *  with the IgH EtherCAT Master; if not, write to the Free Software
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *  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
    21
 *
1326
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1225
diff changeset
    22
 *  Using the EtherCAT technology and brand is permitted in compliance with
ef907b0b5125 merge -c1603 branches/stable-1.4: Changed licence headers to avoid conflicts with the GPL; restricted licence to GPLv2 only.
Florian Pose <fp@igh-essen.com>
parents: 1225
diff changeset
    23
 *  the industrial property and similar rights of Beckhoff Automation GmbH.
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
   \file
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
   EtherCAT state change FSM.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
*/
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
#include "globals.h"
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
#include "master.h"
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#include "fsm_change.h"
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
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    40
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
    41
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
    42
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
    43
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
    44
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
    45
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
    46
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
    47
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
    48
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
    49
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
   Constructor.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
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
    57
                        ec_datagram_t *datagram /**< datagram */
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
    fsm->state = NULL;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
    fsm->datagram = datagram;
738
880c6153101f Improved handling for spontaneous AL state changes.
Florian Pose <fp@igh-essen.com>
parents: 713
diff changeset
    62
    fsm->spontaneous_change = 0;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
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
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
   Destructor.
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
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
    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
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
/**
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    78
   Starts the change state machine.
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    79
*/
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    80
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    81
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
    82
                         ec_slave_t *slave, /**< EtherCAT slave */
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    83
                         ec_slave_state_t state /**< requested state */
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    84
                         )
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
    fsm->mode = EC_FSM_CHANGE_MODE_FULL;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
    fsm->slave = slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
    fsm->requested_state = state;
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    89
    fsm->state = ec_fsm_change_state_start;
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
/*****************************************************************************/
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    93
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    94
/**
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
    95
   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
    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
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
    99
                       ec_slave_t *slave /**< EtherCAT slave */
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
{
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   102
    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
   103
    fsm->slave = slave;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   104
    fsm->requested_state = EC_SLAVE_STATE_UNKNOWN;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   105
    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
   106
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
   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
   112
   \return false, if the state machine has terminated
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 434
diff changeset
   113
*/
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 434
diff changeset
   114
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 434
diff changeset
   115
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
   116
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
    fsm->state(fsm);
435
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 434
diff changeset
   118
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   119
    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
   120
        && fsm->state != ec_fsm_change_state_error;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
}
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
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
   Returns, if the state machine terminated with success.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
   \return non-zero if successful.
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
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
   131
{
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   132
    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
   133
}
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
 *  state change state machine
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
 *****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
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
   Change state: START.
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
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   143
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
   144
                               /**< finite state machine */)
434
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
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
    ec_slave_t *slave = fsm->slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
    fsm->take_time = 1;
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   150
    fsm->old_state = fsm->slave->current_state;
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
    // write new state to slave
815
002fe9ec778f Implemented missing datagram types.
Florian Pose <fp@igh-essen.com>
parents: 813
diff changeset
   153
    ec_datagram_fpwr(datagram, slave->station_address, 0x0120, 2);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   154
    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
   155
    fsm->retries = EC_FSM_RETRIES;
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   156
    fsm->state = ec_fsm_change_state_check;
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
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   160
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   161
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   162
   Change state: CHECK.
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
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   165
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
   166
                               /**< finite state machine */)
434
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
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
    ec_slave_t *slave = fsm->slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   171
    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
   172
        return;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   173
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   175
        fsm->state = ec_fsm_change_state_error;
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   176
        EC_ERR("Failed to receive state datagram from slave %u"
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   177
                " (datagram state %u)!\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   178
               fsm->slave->ring_position, datagram->state);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   179
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   180
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   181
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   182
    if (fsm->take_time) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
        fsm->take_time = 0;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
        fsm->jiffies_start = datagram->jiffies_sent;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   187
    if (datagram->working_counter == 0) {
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
        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
   189
            char state_str[EC_STATE_STRING_SIZE];
1337
0253c74d0940 merge -c1619 branches/1.4-foe: Implemented going to bootstrap state BOOT.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   190
            ec_state_string(fsm->requested_state, state_str, 0);
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   191
            fsm->state = ec_fsm_change_state_error;
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   192
            EC_ERR("Failed to set state %s on slave %u: ",
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   193
                    state_str, fsm->slave->ring_position);
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   194
            ec_datagram_print_wc_error(datagram);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
            return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
        }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   198
        // repeat writing new state to slave
815
002fe9ec778f Implemented missing datagram types.
Florian Pose <fp@igh-essen.com>
parents: 813
diff changeset
   199
        ec_datagram_fpwr(datagram, slave->station_address, 0x0120, 2);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
        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
   201
        fsm->retries = EC_FSM_RETRIES;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   205
    if (unlikely(datagram->working_counter > 1)) {
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   206
        char state_str[EC_STATE_STRING_SIZE];
1337
0253c74d0940 merge -c1619 branches/1.4-foe: Implemented going to bootstrap state BOOT.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   207
        ec_state_string(fsm->requested_state, state_str, 0);
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   208
        fsm->state = ec_fsm_change_state_error;
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   209
        EC_ERR("Failed to set state %s on slave %u: ",
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   210
                state_str, fsm->slave->ring_position);
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   211
        ec_datagram_print_wc_error(datagram);
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   212
        return;
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   213
    }
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   214
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
    fsm->take_time = 1;
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
    // read AL status from slave
815
002fe9ec778f Implemented missing datagram types.
Florian Pose <fp@igh-essen.com>
parents: 813
diff changeset
   218
    ec_datagram_fprd(datagram, slave->station_address, 0x0130, 2);
1225
4d5b739edcb5 Introduced ec_datagram_zero().
Florian Pose <fp@igh-essen.com>
parents: 986
diff changeset
   219
    ec_datagram_zero(datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   220
    fsm->retries = EC_FSM_RETRIES;
738
880c6153101f Improved handling for spontaneous AL state changes.
Florian Pose <fp@igh-essen.com>
parents: 713
diff changeset
   221
    fsm->spontaneous_change = 0;
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   222
    fsm->state = ec_fsm_change_state_status;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   223
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   224
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   225
/*****************************************************************************/
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
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   228
   Change state: STATUS.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   229
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   230
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   231
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
   232
                                /**< finite state machine */)
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   233
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   234
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   235
    ec_slave_t *slave = fsm->slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   236
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   237
    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
   238
        return;
505
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
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   241
        fsm->state = ec_fsm_change_state_error;
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   242
        EC_ERR("Failed to receive state checking datagram from slave %u"
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   243
                " (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   244
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   245
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   246
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   247
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   248
    if (datagram->working_counter != 1) {
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   249
        char req_state[EC_STATE_STRING_SIZE];
1337
0253c74d0940 merge -c1619 branches/1.4-foe: Implemented going to bootstrap state BOOT.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   250
        ec_state_string(fsm->requested_state, req_state, 0);
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   251
        fsm->state = ec_fsm_change_state_error;
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   252
        EC_ERR("Failed to check state %s on slave %u: ",
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   253
               req_state, slave->ring_position);
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   254
        ec_datagram_print_wc_error(datagram);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   255
        return;
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
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   258
    if (fsm->take_time) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   259
        fsm->take_time = 0;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   260
        fsm->jiffies_start = datagram->jiffies_sent;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   261
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   262
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   263
    slave->current_state = EC_READ_U8(datagram->data);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   264
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   265
    if (slave->current_state == fsm->requested_state) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   266
        // state has been set successfully
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   267
        fsm->state = ec_fsm_change_state_end;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   268
        return;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   269
    }
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   270
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   271
    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
   272
        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
   273
1337
0253c74d0940 merge -c1619 branches/1.4-foe: Implemented going to bootstrap state BOOT.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   274
        ec_state_string(slave->current_state, cur_state, 0);
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   275
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   276
        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
   277
            // 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
   278
            // 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
   279
            // state change
738
880c6153101f Improved handling for spontaneous AL state changes.
Florian Pose <fp@igh-essen.com>
parents: 713
diff changeset
   280
            fsm->spontaneous_change = 1;
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   281
            fsm->old_state = slave->current_state;
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   282
            EC_WARN("Slave %u changed to %s in the meantime.\n",
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   283
                    slave->ring_position, cur_state);
738
880c6153101f Improved handling for spontaneous AL state changes.
Florian Pose <fp@igh-essen.com>
parents: 713
diff changeset
   284
            goto check_again;
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   285
        }
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   286
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   287
        // state change error
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   288
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   289
        slave->error_flag = 1;
1337
0253c74d0940 merge -c1619 branches/1.4-foe: Implemented going to bootstrap state BOOT.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   290
        ec_state_string(fsm->requested_state, req_state, 0);
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   291
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   292
        EC_ERR("Failed to set %s state, slave %u refused state change (%s).\n",
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   293
               req_state, slave->ring_position, cur_state);
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   294
        // fetch AL status error code
815
002fe9ec778f Implemented missing datagram types.
Florian Pose <fp@igh-essen.com>
parents: 813
diff changeset
   295
        ec_datagram_fprd(datagram, slave->station_address, 0x0134, 2);
1225
4d5b739edcb5 Introduced ec_datagram_zero().
Florian Pose <fp@igh-essen.com>
parents: 986
diff changeset
   296
        ec_datagram_zero(datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   297
        fsm->retries = EC_FSM_RETRIES;
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   298
        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
   299
        return;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   300
    }
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   301
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   302
    // still old state
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   303
439
0f5a3d983908 Increased state change checking timeout to 1s.
Florian Pose <fp@igh-essen.com>
parents: 438
diff changeset
   304
    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
   305
        // timeout while checking
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   306
        char state_str[EC_STATE_STRING_SIZE];
1337
0253c74d0940 merge -c1619 branches/1.4-foe: Implemented going to bootstrap state BOOT.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   307
        ec_state_string(fsm->requested_state, state_str, 0);
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   308
        fsm->state = ec_fsm_change_state_error;
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   309
        EC_ERR("Timeout while setting state %s on slave %u.\n",
738
880c6153101f Improved handling for spontaneous AL state changes.
Florian Pose <fp@igh-essen.com>
parents: 713
diff changeset
   310
                state_str, slave->ring_position);
880c6153101f Improved handling for spontaneous AL state changes.
Florian Pose <fp@igh-essen.com>
parents: 713
diff changeset
   311
        return;
880c6153101f Improved handling for spontaneous AL state changes.
Florian Pose <fp@igh-essen.com>
parents: 713
diff changeset
   312
    }
880c6153101f Improved handling for spontaneous AL state changes.
Florian Pose <fp@igh-essen.com>
parents: 713
diff changeset
   313
880c6153101f Improved handling for spontaneous AL state changes.
Florian Pose <fp@igh-essen.com>
parents: 713
diff changeset
   314
 check_again:
469
a9c7991dca6b Fixed state change FSM again.
Florian Pose <fp@igh-essen.com>
parents: 454
diff changeset
   315
    // no timeout yet. check again
815
002fe9ec778f Implemented missing datagram types.
Florian Pose <fp@igh-essen.com>
parents: 813
diff changeset
   316
    ec_datagram_fprd(datagram, slave->station_address, 0x0130, 2);
1225
4d5b739edcb5 Introduced ec_datagram_zero().
Florian Pose <fp@igh-essen.com>
parents: 986
diff changeset
   317
    ec_datagram_zero(datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   318
    fsm->retries = EC_FSM_RETRIES;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   319
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   320
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   321
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   322
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   323
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   324
   Application layer status messages.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   325
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   326
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   327
const ec_code_msg_t al_status_messages[] = {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   328
    {0x0001, "Unspecified error"},
1337
0253c74d0940 merge -c1619 branches/1.4-foe: Implemented going to bootstrap state BOOT.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   329
    {0x0011, "Invalid requested state change"},
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   330
    {0x0012, "Unknown requested state"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   331
    {0x0013, "Bootstrap not supported"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   332
    {0x0014, "No valid firmware"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   333
    {0x0015, "Invalid mailbox configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   334
    {0x0016, "Invalid mailbox configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   335
    {0x0017, "Invalid sync manager configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   336
    {0x0018, "No valid inputs available"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   337
    {0x0019, "No valid outputs"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   338
    {0x001A, "Synchronisation error"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   339
    {0x001B, "Sync manager watchdog"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   340
    {0x001C, "Invalid sync manager types"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   341
    {0x001D, "Invalid output configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   342
    {0x001E, "Invalid input configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   343
    {0x001F, "Invalid watchdog configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   344
    {0x0020, "Slave needs cold start"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   345
    {0x0021, "Slave needs INIT"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   346
    {0x0022, "Slave needs PREOP"},
813
bfc3f1ab52de Fixed typo SAVEOP -> SAFEOP.
Florian Pose <fp@igh-essen.com>
parents: 798
diff changeset
   347
    {0x0023, "Slave needs SAFEOP"},
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   348
    {0x0030, "Invalid DC SYNCH configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   349
    {0x0031, "Invalid DC latch configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   350
    {0x0032, "PLL error"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   351
    {0x0033, "Invalid DC IO error"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   352
    {0x0034, "Invalid DC timeout error"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   353
    {0x0042, "MBOX EOE"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   354
    {0x0043, "MBOX COE"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   355
    {0x0044, "MBOX FOE"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   356
    {0x0045, "MBOX SOE"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   357
    {0x004F, "MBOX VOE"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   358
    {}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   359
};
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   360
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   361
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   362
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   363
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   364
   Change state: CODE.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   365
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   366
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   367
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
   368
                              /**< finite state machine */)
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   369
{
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   370
    ec_datagram_t *datagram = fsm->datagram;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   371
    uint32_t code;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   372
    const ec_code_msg_t *al_msg;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   373
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   374
    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
   375
        return;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   376
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   377
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   378
        fsm->state = ec_fsm_change_state_error;
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   379
        EC_ERR("Failed to receive AL status code datagram from slave %u"
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   380
                " (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   381
               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
   382
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   383
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   384
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   385
    if (datagram->working_counter != 1) {
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   386
        EC_WARN("Reception of AL status code datagram failed: ");
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   387
        ec_datagram_print_wc_error(datagram);
798
5a58606726f3 Output AL status code zero.
Florian Pose <fp@igh-essen.com>
parents: 738
diff changeset
   388
    } else {
5a58606726f3 Output AL status code zero.
Florian Pose <fp@igh-essen.com>
parents: 738
diff changeset
   389
        code = EC_READ_U16(datagram->data);
5a58606726f3 Output AL status code zero.
Florian Pose <fp@igh-essen.com>
parents: 738
diff changeset
   390
        for (al_msg = al_status_messages; al_msg->code; al_msg++) {
5a58606726f3 Output AL status code zero.
Florian Pose <fp@igh-essen.com>
parents: 738
diff changeset
   391
            if (al_msg->code != code) continue;
5a58606726f3 Output AL status code zero.
Florian Pose <fp@igh-essen.com>
parents: 738
diff changeset
   392
            EC_ERR("AL status message 0x%04X: \"%s\".\n",
5a58606726f3 Output AL status code zero.
Florian Pose <fp@igh-essen.com>
parents: 738
diff changeset
   393
                    al_msg->code, al_msg->message);
5a58606726f3 Output AL status code zero.
Florian Pose <fp@igh-essen.com>
parents: 738
diff changeset
   394
            break;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   395
        }
798
5a58606726f3 Output AL status code zero.
Florian Pose <fp@igh-essen.com>
parents: 738
diff changeset
   396
        if (!al_msg->code)
5a58606726f3 Output AL status code zero.
Florian Pose <fp@igh-essen.com>
parents: 738
diff changeset
   397
            EC_ERR("Unknown AL status code 0x%04X.\n", code);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   398
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   399
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   400
    // acknowledge "old" slave state
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   401
    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
   402
}
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   403
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   404
/*****************************************************************************/
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   405
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   406
/**
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   407
   Change state: START ACK.
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   408
*/
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   409
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   410
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
   411
                                   /**< finite state machine */)
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   412
{
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   413
    ec_slave_t *slave = fsm->slave;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   414
    ec_datagram_t *datagram = fsm->datagram;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   415
815
002fe9ec778f Implemented missing datagram types.
Florian Pose <fp@igh-essen.com>
parents: 813
diff changeset
   416
    ec_datagram_fpwr(datagram, slave->station_address, 0x0120, 2);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   417
    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
   418
    fsm->retries = EC_FSM_RETRIES;
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   419
    fsm->state = ec_fsm_change_state_ack;
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   420
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   421
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   422
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   423
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   424
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   425
   Change state: ACK.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   426
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   427
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   428
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
   429
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   430
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   431
    ec_slave_t *slave = fsm->slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   432
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   433
    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
   434
        return;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   435
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   436
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   437
        fsm->state = ec_fsm_change_state_error;
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   438
        EC_ERR("Failed to receive state ack datagram for slave %u"
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   439
                " (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   440
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   441
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   442
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   443
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   444
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   445
        fsm->state = ec_fsm_change_state_error;
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   446
        EC_ERR("Reception of state ack datagram failed on slave %u: ",
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   447
                slave->ring_position);
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   448
        ec_datagram_print_wc_error(datagram);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   449
        return;
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
    fsm->take_time = 1;
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
    // read new AL status
815
002fe9ec778f Implemented missing datagram types.
Florian Pose <fp@igh-essen.com>
parents: 813
diff changeset
   455
    ec_datagram_fprd(datagram, slave->station_address, 0x0130, 2);
1225
4d5b739edcb5 Introduced ec_datagram_zero().
Florian Pose <fp@igh-essen.com>
parents: 986
diff changeset
   456
    ec_datagram_zero(datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   457
    fsm->retries = EC_FSM_RETRIES;
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   458
    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
   459
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   460
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   461
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   462
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   463
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   464
   Change state: CHECK ACK.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   465
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   466
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   467
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
   468
                                   /**< finite state machine */)
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   469
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   470
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   471
    ec_slave_t *slave = fsm->slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   472
637
d5d04c868e0e Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents: 594
diff changeset
   473
    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
   474
        return;
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   475
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   476
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   477
        fsm->state = ec_fsm_change_state_error;
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   478
        EC_ERR("Failed to receive state ack check datagram from slave %u"
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   479
                " (datagram state %u).\n",
594
07dd2a7df66e Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents: 505
diff changeset
   480
               slave->ring_position, datagram->state);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   481
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   482
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   483
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   484
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   485
        fsm->state = ec_fsm_change_state_error;
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   486
        EC_ERR("Reception of state ack check datagram failed on slave %u: ",
713
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   487
                slave->ring_position);
ae41cadd25b6 Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents: 637
diff changeset
   488
        ec_datagram_print_wc_error(datagram);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   489
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   490
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   491
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   492
    if (fsm->take_time) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   493
        fsm->take_time = 0;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   494
        fsm->jiffies_start = datagram->jiffies_sent;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   495
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   496
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   497
    slave->current_state = EC_READ_U8(datagram->data);
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
    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
   500
        char state_str[EC_STATE_STRING_SIZE];
1337
0253c74d0940 merge -c1619 branches/1.4-foe: Implemented going to bootstrap state BOOT.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   501
        ec_state_string(slave->current_state, state_str, 0);
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   502
        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
   503
            fsm->state = ec_fsm_change_state_error;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   504
        }
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   505
        else { // EC_FSM_CHANGE_MODE_ACK_ONLY
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   506
            fsm->state = ec_fsm_change_state_end;
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   507
        }
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   508
        EC_INFO("Acknowledged state %s on slave %u.\n",
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   509
                state_str, slave->ring_position);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   510
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   511
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   512
443
0746236dd032 Fixes in state change FSM.
Florian Pose <fp@igh-essen.com>
parents: 439
diff changeset
   513
    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
   514
        // timeout while checking
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   515
        char state_str[EC_STATE_STRING_SIZE];
1337
0253c74d0940 merge -c1619 branches/1.4-foe: Implemented going to bootstrap state BOOT.
Florian Pose <fp@igh-essen.com>
parents: 1326
diff changeset
   516
        ec_state_string(slave->current_state, state_str, 0);
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   517
        fsm->state = ec_fsm_change_state_error;
986
a486591ba86b Replaced misleading %i by %u.
Florian Pose <fp@igh-essen.com>
parents: 815
diff changeset
   518
        EC_ERR("Timeout while acknowledging state %s on slave %u.\n",
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   519
               state_str, slave->ring_position);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   520
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   521
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   522
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   523
    // reread new AL status
815
002fe9ec778f Implemented missing datagram types.
Florian Pose <fp@igh-essen.com>
parents: 813
diff changeset
   524
    ec_datagram_fprd(datagram, slave->station_address, 0x0130, 2);
1225
4d5b739edcb5 Introduced ec_datagram_zero().
Florian Pose <fp@igh-essen.com>
parents: 986
diff changeset
   525
    ec_datagram_zero(datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 469
diff changeset
   526
    fsm->retries = EC_FSM_RETRIES;
434
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
/*****************************************************************************/
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
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   532
   State: ERROR.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   533
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   534
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   535
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
   536
                               /**< finite state machine */)
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   537
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   538
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   539
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   540
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   541
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   542
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   543
   State: END.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   544
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   545
454
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   546
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
   547
                             /**< finite state machine */)
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   548
{
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   549
}
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   550
25cc4a3b9e0a State acknowledgement in master state machine.
Florian Pose <fp@igh-essen.com>
parents: 443
diff changeset
   551
/*****************************************************************************/