master/fsm_sii.c
author Florian Pose <fp@igh-essen.com>
Wed, 21 Feb 2007 11:40:40 +0000
changeset 583 172d42146930
parent 505 bc443ca0077f
child 594 07dd2a7df66e
permissions -rw-r--r--
Added testing version for forcedeth for 2.6.19 kernel.
433
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/******************************************************************************
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 *  $Id$
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
 *
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
 *  This file is part of the IgH EtherCAT Master.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
 *
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
 *  The IgH EtherCAT Master is free software; you can redistribute it
100f51f28cf2 Layed out SII 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
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
 *  as published by the Free Software Foundation; either version 2 of the
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *  License, or (at your option) any later version.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *
100f51f28cf2 Layed out SII 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
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *  GNU General Public License for more details.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *  You should have received a copy of the GNU General Public License
100f51f28cf2 Layed out SII 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
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
 *  The right to use EtherCAT Technology is granted and comes free of
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *  charge under condition of compatibility of product made by
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *  Licensee. People intending to distribute/sell products based on the
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
 *  code, have to sign an agreement to guarantee that products using
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
 *  software based on IgH EtherCAT master stay compatible with the actual
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 *  EtherCAT specification (which are released themselves as an open
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
 *  standard) as the (only) precondition to have the right to use EtherCAT
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
 *  Technology, IP and trade marks.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
 *
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
 *****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
/**
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
   \file
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
   EtherCAT slave information interface FSM.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
*/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
/*****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
#include "globals.h"
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
#include "mailbox.h"
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
#include "master.h"
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
#include "fsm_sii.h"
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
/*****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
void ec_fsm_sii_start_reading(ec_fsm_sii_t *);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
void ec_fsm_sii_read_check(ec_fsm_sii_t *);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
void ec_fsm_sii_read_fetch(ec_fsm_sii_t *);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
void ec_fsm_sii_start_writing(ec_fsm_sii_t *);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
void ec_fsm_sii_write_check(ec_fsm_sii_t *);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
void ec_fsm_sii_write_check2(ec_fsm_sii_t *);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
void ec_fsm_sii_end(ec_fsm_sii_t *);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
void ec_fsm_sii_error(ec_fsm_sii_t *);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
/*****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
/**
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
   Constructor.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
*/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
void ec_fsm_sii_init(ec_fsm_sii_t *fsm, /**< finite state machine */
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
                     ec_datagram_t *datagram /**< datagram structure to use */
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
                     )
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
{
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
    fsm->state = NULL;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
    fsm->datagram = datagram;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
}
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
/*****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
/**
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
   Destructor.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
*/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
void ec_fsm_sii_clear(ec_fsm_sii_t *fsm /**< finite state machine */)
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
{
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
}
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
/*****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
/**
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
   Initializes the SII read state machine.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
*/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
void ec_fsm_sii_read(ec_fsm_sii_t *fsm, /**< finite state machine */
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
                     ec_slave_t *slave, /**< slave to read from */
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
                     uint16_t offset, /**< offset to read from */
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
                     ec_fsm_sii_addressing_t mode /**< addressing scheme */
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
                     )
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
{
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
    fsm->state = ec_fsm_sii_start_reading;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
    fsm->slave = slave;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
    fsm->offset = offset;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
    fsm->mode = mode;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
}
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
/*****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
/**
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
   Initializes the SII write state machine.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
*/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
void ec_fsm_sii_write(ec_fsm_sii_t *fsm, /**< finite state machine */
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
                      ec_slave_t *slave, /**< slave to read from */
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
                      uint16_t offset, /**< offset to read from */
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
                      uint16_t *value, /**< pointer to 2 bytes of data */
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
                      ec_fsm_sii_addressing_t mode /**< addressing scheme */
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
                      )
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
{
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
    fsm->state = ec_fsm_sii_start_writing;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
    fsm->slave = slave;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
    fsm->offset = offset;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
    fsm->mode = mode;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
    memcpy(fsm->value, value, 2);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
}
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
/*****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
/**
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
   Executes the SII state machine.
435
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 433
diff changeset
   123
   \return false, if the state machine has terminated
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 433
diff changeset
   124
*/
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 433
diff changeset
   125
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 433
diff changeset
   126
int ec_fsm_sii_exec(ec_fsm_sii_t *fsm /**< finite state machine */)
433
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
{
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
    fsm->state(fsm);
435
779a18d12e6c Removed state machine running() methods.
Florian Pose <fp@igh-essen.com>
parents: 433
diff changeset
   129
433
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
    return fsm->state != ec_fsm_sii_end && fsm->state != ec_fsm_sii_error;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
}
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
/*****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
/**
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
   Returns, if the master startup state machine terminated with success.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
   \return non-zero if successful.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
*/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
int ec_fsm_sii_success(ec_fsm_sii_t *fsm /**< Finite state machine */)
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
{
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
    return fsm->state == ec_fsm_sii_end;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   143
}
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
/******************************************************************************
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
 *  SII state machine
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
 *****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
/**
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   150
   SII state: START READING.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
   Starts reading the slave information interface.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
*/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   153
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   154
void ec_fsm_sii_start_reading(ec_fsm_sii_t *fsm /**< finite state machine */)
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   155
{
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   156
    ec_datagram_t *datagram = fsm->datagram;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   157
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   158
    // initiate read operation
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
    switch (fsm->mode) {
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   160
        case EC_FSM_SII_POSITION:
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   161
            ec_datagram_apwr(datagram, fsm->slave->ring_position, 0x502, 4);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   162
            break;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
        case EC_FSM_SII_NODE:
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
            ec_datagram_npwr(datagram, fsm->slave->station_address, 0x502, 4);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   165
            break;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   166
    }
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   167
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   168
    EC_WRITE_U8 (datagram->data,     0x00); // read-only access
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
    EC_WRITE_U8 (datagram->data + 1, 0x01); // request read operation
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
    EC_WRITE_U16(datagram->data + 2, fsm->offset);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   171
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   172
    fsm->retries = EC_FSM_RETRIES;
433
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   173
    fsm->state = ec_fsm_sii_read_check;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
}
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   175
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
/*****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   177
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   178
/**
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   179
   SII state: READ CHECK.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   180
   Checks, if the SII-read-datagram has been sent and issues a fetch datagram.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   181
*/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   182
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
void ec_fsm_sii_read_check(ec_fsm_sii_t *fsm /**< finite state machine */)
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
{
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
    ec_datagram_t *datagram = fsm->datagram;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   187
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   188
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   189
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   190
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   191
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   192
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   193
        fsm->state = ec_fsm_sii_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   194
        EC_ERR("Failed to receive SII read datagram from slave %i.\n",
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   195
               fsm->slave->ring_position);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   196
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   197
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   198
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   199
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   200
        fsm->state = ec_fsm_sii_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   201
        EC_ERR("Reception of SII read datagram failed - slave %i did not"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   202
               " respond.\n", fsm->slave->ring_position);
433
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
        return;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
    }
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
    fsm->cycles_start = datagram->cycles_sent;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
    fsm->check_once_more = 1;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   208
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
    // issue check/fetch datagram
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
    switch (fsm->mode) {
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   211
        case EC_FSM_SII_POSITION:
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   212
            ec_datagram_aprd(datagram, fsm->slave->ring_position, 0x502, 10);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   213
            break;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   214
        case EC_FSM_SII_NODE:
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
            ec_datagram_nprd(datagram, fsm->slave->station_address, 0x502, 10);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
            break;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   217
    }
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   218
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   219
    fsm->retries = EC_FSM_RETRIES;
433
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
    fsm->state = ec_fsm_sii_read_fetch;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   221
}
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   222
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   223
/*****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   224
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   225
/**
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   226
   SII state: READ FETCH.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   227
   Fetches the result of an SII-read datagram.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   228
*/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   229
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   230
void ec_fsm_sii_read_fetch(ec_fsm_sii_t *fsm /**< finite state machine */)
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   231
{
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   232
    ec_datagram_t *datagram = fsm->datagram;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   233
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   234
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   235
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   236
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   237
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   238
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   239
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   240
        fsm->state = ec_fsm_sii_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   241
        EC_ERR("Failed to receive SII check/fetch datagram from slave %i.\n",
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   242
               fsm->slave->ring_position);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   243
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   244
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   245
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   246
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   247
        fsm->state = ec_fsm_sii_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   248
        EC_ERR("Reception of SII check/fetch datagram failed - slave %i did"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   249
               " not respond\n", fsm->slave->ring_position);
433
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   250
        return;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   251
    }
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   252
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   253
    // check "busy bit"
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   254
    if (EC_READ_U8(datagram->data + 1) & 0x81) {
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   255
        // still busy... timeout?
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   256
        if (datagram->cycles_received
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   257
            - fsm->cycles_start >= (cycles_t) 10 * cpu_khz) {
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   258
            if (!fsm->check_once_more) {
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   259
                EC_ERR("SII: Read timeout.\n");
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   260
                fsm->state = ec_fsm_sii_error;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   261
#if 0
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   262
                EC_DBG("SII busy: %02X %02X %02X %02X\n",
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   263
                       EC_READ_U8(datagram->data + 0),
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   264
                       EC_READ_U8(datagram->data + 1),
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   265
                       EC_READ_U8(datagram->data + 2),
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   266
                       EC_READ_U8(datagram->data + 3));
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   267
#endif
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   268
                return;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   269
            }
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   270
            fsm->check_once_more = 0;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   271
        }
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   272
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   273
        // issue check/fetch datagram again
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   274
        switch (fsm->mode) {
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   275
            case EC_FSM_SII_POSITION:
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   276
                ec_datagram_aprd(datagram, fsm->slave->ring_position, 0x502, 10);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   277
                break;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   278
            case EC_FSM_SII_NODE:
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   279
                ec_datagram_nprd(datagram, fsm->slave->station_address, 0x502, 10);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   280
                break;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   281
        }
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   282
        ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   283
        fsm->retries = EC_FSM_RETRIES;
433
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   284
        return;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   285
    }
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   286
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   287
#if 0
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   288
    EC_DBG("SII rec: %02X %02X %02X %02X - %02X %02X %02X %02X\n",
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   289
           EC_READ_U8(datagram->data + 0), EC_READ_U8(datagram->data + 1),
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   290
           EC_READ_U8(datagram->data + 2), EC_READ_U8(datagram->data + 3),
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   291
           EC_READ_U8(datagram->data + 6), EC_READ_U8(datagram->data + 7),
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   292
           EC_READ_U8(datagram->data + 8), EC_READ_U8(datagram->data + 9));
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   293
#endif
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   294
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   295
    // SII value received.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   296
    memcpy(fsm->value, datagram->data + 6, 4);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   297
    fsm->state = ec_fsm_sii_end;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   298
}
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   299
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   300
/*****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   301
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   302
/**
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   303
   SII state: START WRITING.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   304
   Starts reading the slave information interface.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   305
*/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   306
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   307
void ec_fsm_sii_start_writing(ec_fsm_sii_t *fsm /**< finite state machine */)
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   308
{
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   309
    ec_datagram_t *datagram = fsm->datagram;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   310
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   311
    // initiate write operation
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   312
    ec_datagram_npwr(datagram, fsm->slave->station_address, 0x502, 8);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   313
    EC_WRITE_U8 (datagram->data,     0x01); // enable write access
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   314
    EC_WRITE_U8 (datagram->data + 1, 0x02); // request write operation
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   315
    EC_WRITE_U32(datagram->data + 2, fsm->offset);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   316
    memcpy(datagram->data + 6, fsm->value, 2);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   317
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   318
    fsm->retries = EC_FSM_RETRIES;
433
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   319
    fsm->state = ec_fsm_sii_write_check;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   320
}
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   321
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   322
/*****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   323
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   324
/**
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   325
   SII state: WRITE CHECK.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   326
*/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   327
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   328
void ec_fsm_sii_write_check(ec_fsm_sii_t *fsm /**< finite state machine */)
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   329
{
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   330
    ec_datagram_t *datagram = fsm->datagram;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   331
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   332
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   333
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   334
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   335
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   336
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   337
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   338
        fsm->state = ec_fsm_sii_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   339
        EC_ERR("Failed to receive SII write datagram for slave %i.\n",
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   340
               fsm->slave->ring_position);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   341
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   342
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   343
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   344
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   345
        fsm->state = ec_fsm_sii_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   346
        EC_ERR("Reception of SII write datagram failed - slave %i did not"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   347
               " respond.\n", fsm->slave->ring_position);
433
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   348
        return;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   349
    }
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   350
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   351
    fsm->cycles_start = datagram->cycles_sent;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   352
    fsm->check_once_more = 1;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   353
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   354
    // issue check/fetch datagram
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   355
    ec_datagram_nprd(datagram, fsm->slave->station_address, 0x502, 2);
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   356
    ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   357
    fsm->retries = EC_FSM_RETRIES;
433
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   358
    fsm->state = ec_fsm_sii_write_check2;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   359
}
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   360
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   361
/*****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   362
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   363
/**
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   364
   SII state: WRITE CHECK 2.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   365
*/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   366
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   367
void ec_fsm_sii_write_check2(ec_fsm_sii_t *fsm /**< finite state machine */)
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   368
{
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   369
    ec_datagram_t *datagram = fsm->datagram;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   370
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   371
    if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   372
        ec_master_queue_datagram(fsm->slave->master, datagram);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   373
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   374
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   375
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   376
    if (datagram->state != EC_DATAGRAM_RECEIVED) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   377
        fsm->state = ec_fsm_sii_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   378
        EC_ERR("Failed to receive SII write check datagram from slave %i.\n",
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   379
               fsm->slave->ring_position);
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   380
        return;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   381
    }
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   382
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   383
    if (datagram->working_counter != 1) {
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   384
        fsm->state = ec_fsm_sii_error;
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   385
        EC_ERR("Reception of SII write check datagram failed - slave %i did"
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   386
               " not respond.\n", fsm->slave->ring_position);
433
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   387
        return;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   388
    }
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   389
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   390
    if (EC_READ_U8(datagram->data + 1) & 0x82) {
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   391
        // still busy... timeout?
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   392
        if (datagram->cycles_received
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   393
            - fsm->cycles_start >= (cycles_t) 10 * cpu_khz) {
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   394
            if (!fsm->check_once_more) {
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   395
                EC_ERR("SII: Write timeout.\n");
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   396
                fsm->state = ec_fsm_sii_error;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   397
                return;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   398
            }
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   399
            fsm->check_once_more = 0;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   400
        }
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   401
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   402
        // issue check/fetch datagram again
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   403
        ec_master_queue_datagram(fsm->slave->master, datagram);
505
bc443ca0077f State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents: 435
diff changeset
   404
        fsm->retries = EC_FSM_RETRIES;
433
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   405
        return;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   406
    }
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   407
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   408
    if (EC_READ_U8(datagram->data + 1) & 0x40) {
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   409
        EC_ERR("SII: Write operation failed!\n");
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   410
        fsm->state = ec_fsm_sii_error;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   411
        return;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   412
    }
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   413
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   414
    // success
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   415
    fsm->state = ec_fsm_sii_end;
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   416
}
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   417
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   418
/*****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   419
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   420
/**
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   421
   State: ERROR.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   422
*/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   423
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   424
void ec_fsm_sii_error(ec_fsm_sii_t *fsm /**< finite state machine */)
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   425
{
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   426
}
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   427
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   428
/*****************************************************************************/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   429
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   430
/**
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   431
   State: END.
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   432
*/
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   433
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   434
void ec_fsm_sii_end(ec_fsm_sii_t *fsm /**< finite state machine */)
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   435
{
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   436
}
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   437
100f51f28cf2 Layed out SII state machine.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   438
/*****************************************************************************/