master/fsm_change.c
author Florian Pose <fp@igh-essen.com>
Wed, 25 Oct 2006 07:41:14 +0000
changeset 442 6607875255d9
parent 439 0f5a3d983908
child 443 0746236dd032
permissions -rw-r--r--
SDO dictionary and SDO access only in IDLE mode, because of kmalloc()s.
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
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
void ec_fsm_change_start(ec_fsm_change_t *);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
void ec_fsm_change_check(ec_fsm_change_t *);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
void ec_fsm_change_status(ec_fsm_change_t *);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
void ec_fsm_change_code(ec_fsm_change_t *);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
void ec_fsm_change_ack(ec_fsm_change_t *);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
void ec_fsm_change_check_ack(ec_fsm_change_t *);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
void ec_fsm_change_end(ec_fsm_change_t *);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
void ec_fsm_change_error(ec_fsm_change_t *);
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
/*****************************************************************************/
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
   Constructor.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
*/
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
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
    63
                        ec_datagram_t *datagram /**< datagram */
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
    fsm->state = NULL;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
    fsm->datagram = datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
}
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
   Destructor.
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
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
    77
{
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
   Resets the state machine.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
void ec_fsm_change(ec_fsm_change_t *fsm, /**< finite state machine */
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
                   ec_slave_t *slave, /**< EtherCAT slave */
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
                   ec_slave_state_t state /**< requested state */
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
                   )
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
    fsm->slave = slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
    fsm->requested_state = state;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
    fsm->state = ec_fsm_change_start;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
   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
   100
   \return false, if the state machine has terminated
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 434
diff changeset
   101
*/
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 434
diff changeset
   102
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 434
diff changeset
   103
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
   104
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
    fsm->state(fsm);
435
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 434
diff changeset
   106
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
    return fsm->state != ec_fsm_change_end
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
        && fsm->state != ec_fsm_change_error;
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
/*****************************************************************************/
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
   Returns, if the state machine terminated with success.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
   \return non-zero if successful.
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
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
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
   119
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
    return fsm->state == ec_fsm_change_end;
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
 *  state change state machine
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
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
   Change state: START.
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
void ec_fsm_change_start(ec_fsm_change_t *fsm /**< finite state machine */)
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
    ec_slave_t *slave = fsm->slave;
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
    fsm->take_time = 1;
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
    // write new state to slave
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
    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
   140
    EC_WRITE_U16(datagram->data, fsm->requested_state);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
    ec_master_queue_datagram(fsm->slave->master, datagram);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
    fsm->state = ec_fsm_change_check;
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
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
   Change state: CHECK.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   150
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
void ec_fsm_change_check(ec_fsm_change_t *fsm /**< finite state machine */)
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   153
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   154
    ec_slave_t *slave = fsm->slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   155
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   156
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   157
        fsm->state = ec_fsm_change_error;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   158
        EC_ERR("Failed to send state datagram to slave %i!\n",
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
               fsm->slave->ring_position);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   160
        return;
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
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
    if (fsm->take_time) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
        fsm->take_time = 0;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   165
        fsm->jiffies_start = datagram->jiffies_sent;
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
    if (datagram->working_counter != 1) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
        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
   170
            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
   171
            ec_state_string(fsm->requested_state, state_str);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   172
            fsm->state = ec_fsm_change_error;
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   173
            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
   174
                   " 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
   175
            return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
        }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   177
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   178
        // repeat writing new state to slave
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   179
        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
   180
        EC_WRITE_U16(datagram->data, fsm->requested_state);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   181
        ec_master_queue_datagram(fsm->slave->master, datagram);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   182
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
    fsm->take_time = 1;
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
    // read AL status from slave
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
    ec_datagram_nprd(datagram, slave->station_address, 0x0130, 2);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   189
    ec_master_queue_datagram(fsm->slave->master, datagram);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   190
    fsm->state = ec_fsm_change_status;
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
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
   Change state: STATUS.
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
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   199
void ec_fsm_change_status(ec_fsm_change_t *fsm /**< finite state machine */)
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
    ec_slave_t *slave = fsm->slave;
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
    if (datagram->state != EC_DATAGRAM_RECEIVED
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
        || datagram->working_counter != 1) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
        fsm->state = ec_fsm_change_error;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
        EC_ERR("Failed to check state 0x%02X on slave %i.\n",
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   208
               fsm->requested_state, slave->ring_position);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
    }
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
    if (fsm->take_time) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   213
        fsm->take_time = 0;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   214
        fsm->jiffies_start = datagram->jiffies_sent;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
    }
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
    slave->current_state = EC_READ_U8(datagram->data);
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
    if (slave->current_state == fsm->requested_state) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
        // state has been set successfully
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   221
        fsm->state = ec_fsm_change_end;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   222
        return;
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
    if (slave->current_state & EC_SLAVE_STATE_ACK_ERR) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   226
        // state change error
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   227
        char req_state[EC_STATE_STRING_SIZE], cur_state[EC_STATE_STRING_SIZE];
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   228
        ec_state_string(fsm->requested_state, req_state);
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   229
        ec_state_string(slave->current_state, cur_state);
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   230
        EC_ERR("Failed to set %s state, slave %i refused state change (%s).\n",
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   231
               req_state, slave->ring_position, cur_state);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   232
        // fetch AL status error code
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   233
        ec_datagram_nprd(datagram, slave->station_address, 0x0134, 2);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   234
        ec_master_queue_datagram(fsm->slave->master, datagram);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   235
        fsm->state = ec_fsm_change_code;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   236
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   237
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   238
439
0f5a3d983908 Increased state change checking timeout to 1s.
Florian Pose <fp@igh-essen.com>
parents: 438
diff changeset
   239
    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
   240
        // timeout while checking
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   241
        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
   242
        ec_state_string(fsm->requested_state, state_str);
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   243
        fsm->state = ec_fsm_change_error;
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   244
        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
   245
               state_str, slave->ring_position);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   246
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   247
    }
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
    // still old state: check again
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   250
    ec_datagram_nprd(datagram, slave->station_address, 0x0130, 2);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   251
    ec_master_queue_datagram(fsm->slave->master, datagram);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   252
}
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
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
   Application layer status messages.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   258
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   259
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   260
const ec_code_msg_t al_status_messages[] = {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   261
    {0x0001, "Unspecified error"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   262
    {0x0011, "Invalud requested state change"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   263
    {0x0012, "Unknown requested state"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   264
    {0x0013, "Bootstrap not supported"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   265
    {0x0014, "No valid firmware"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   266
    {0x0015, "Invalid mailbox configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   267
    {0x0016, "Invalid mailbox configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   268
    {0x0017, "Invalid sync manager configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   269
    {0x0018, "No valid inputs available"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   270
    {0x0019, "No valid outputs"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   271
    {0x001A, "Synchronisation error"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   272
    {0x001B, "Sync manager watchdog"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   273
    {0x001C, "Invalid sync manager types"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   274
    {0x001D, "Invalid output configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   275
    {0x001E, "Invalid input configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   276
    {0x001F, "Invalid watchdog configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   277
    {0x0020, "Slave needs cold start"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   278
    {0x0021, "Slave needs INIT"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   279
    {0x0022, "Slave needs PREOP"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   280
    {0x0023, "Slave needs SAVEOP"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   281
    {0x0030, "Invalid DC SYNCH configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   282
    {0x0031, "Invalid DC latch configuration"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   283
    {0x0032, "PLL error"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   284
    {0x0033, "Invalid DC IO error"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   285
    {0x0034, "Invalid DC timeout error"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   286
    {0x0042, "MBOX EOE"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   287
    {0x0043, "MBOX COE"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   288
    {0x0044, "MBOX FOE"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   289
    {0x0045, "MBOX SOE"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   290
    {0x004F, "MBOX VOE"},
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   291
    {}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   292
};
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   293
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   294
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   295
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   296
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   297
   Change state: CODE.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   298
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   299
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   300
void ec_fsm_change_code(ec_fsm_change_t *fsm /**< finite state machine */)
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   301
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   302
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   303
    ec_slave_t *slave = fsm->slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   304
    uint32_t code;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   305
    const ec_code_msg_t *al_msg;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   306
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   307
    if (datagram->state != EC_DATAGRAM_RECEIVED
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   308
        || datagram->working_counter != 1) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   309
        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
   310
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   311
    else {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   312
        if ((code = EC_READ_U16(datagram->data))) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   313
            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
   314
                if (al_msg->code != code) continue;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   315
                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
   316
                       al_msg->code, al_msg->message);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   317
                break;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   318
            }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   319
            if (!al_msg->code)
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   320
                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
   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
    // acknowledge "old" slave state
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   325
    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
   326
    EC_WRITE_U16(datagram->data, slave->current_state);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   327
    ec_master_queue_datagram(fsm->slave->master, datagram);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   328
    fsm->state = ec_fsm_change_ack;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   329
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   330
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   331
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   332
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   333
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   334
   Change state: ACK.
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   335
*/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   336
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   337
void ec_fsm_change_ack(ec_fsm_change_t *fsm /**< finite state machine */)
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   338
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   339
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   340
    ec_slave_t *slave = fsm->slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   341
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   342
    if (datagram->state != EC_DATAGRAM_RECEIVED
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   343
        || datagram->working_counter != 1) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   344
        fsm->state = ec_fsm_change_error;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   345
        EC_ERR("Reception of state ack datagram failed.\n");
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   346
        return;
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
    fsm->take_time = 1;
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
    // read new AL status
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   352
    ec_datagram_nprd(datagram, slave->station_address, 0x0130, 2);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   353
    ec_master_queue_datagram(fsm->slave->master, datagram);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   354
    fsm->state = ec_fsm_change_check_ack;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   355
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   356
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   357
/*****************************************************************************/
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
   Change state: CHECK ACK.
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
void ec_fsm_change_check_ack(ec_fsm_change_t *fsm /**< finite state machine */)
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   364
{
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   365
    ec_datagram_t *datagram = fsm->datagram;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   366
    ec_slave_t *slave = fsm->slave;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   367
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   368
    if (datagram->state != EC_DATAGRAM_RECEIVED
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   369
        || datagram->working_counter != 1) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   370
        fsm->state = ec_fsm_change_error;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   371
        EC_ERR("Reception of state ack check datagram failed.\n");
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   372
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   373
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   374
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   375
    if (fsm->take_time) {
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   376
        fsm->take_time = 0;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   377
        fsm->jiffies_start = datagram->jiffies_sent;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   378
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   379
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   380
    slave->current_state = EC_READ_U8(datagram->data);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   381
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   382
    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
   383
        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
   384
        ec_state_string(slave->current_state, state_str);
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   385
        fsm->state = ec_fsm_change_error;
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   386
        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
   387
                state_str, slave->ring_position);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   388
        return;
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
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   391
    if (datagram->jiffies_received
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   392
        - fsm->jiffies_start >= 100 * HZ / 1000) { // 100ms
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   393
        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
   394
        ec_state_string(fsm->requested_state, state_str);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   395
        // timeout while checking
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   396
        slave->current_state = EC_SLAVE_STATE_UNKNOWN;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   397
        fsm->state = ec_fsm_change_error;
438
64edce89fe9b Output state names instead of codes in change FSM.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   398
        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
   399
               state_str, slave->ring_position);
434
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   400
        return;
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   401
    }
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   402
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   403
    // reread new AL status
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   404
    ec_datagram_nprd(datagram, slave->station_address, 0x0130, 2);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   405
    ec_master_queue_datagram(fsm->slave->master, datagram);
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   406
}
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   407
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   408
/*****************************************************************************/
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   409
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
   State: ERROR.
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
void ec_fsm_change_error(ec_fsm_change_t *fsm /**< finite state machine */)
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   415
{
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
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   418
/*****************************************************************************/
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
/**
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   421
   State: END.
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
void ec_fsm_change_end(ec_fsm_change_t *fsm /**< finite state machine */)
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   425
{
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
0180d8277311 Layed out state change state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   428
/*****************************************************************************/