master/sync.c
author Florian Pose <fp@igh-essen.com>
Wed, 07 Mar 2007 17:27:25 +0000
changeset 634 ae5fb70b359f
parent 628 e94a16bc52ce
child 635 d304ef4af542
permissions -rw-r--r--
Updated TODO.
628
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/******************************************************************************
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 *  $Id$
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
 *
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
 *  This file is part of the IgH EtherCAT Master.
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
 *
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
 *  The IgH EtherCAT Master is free software; you can redistribute it
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
 *  and/or modify it under the terms of the GNU General Public License
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
 *  as published by the Free Software Foundation; either version 2 of the
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *  License, or (at your option) any later version.
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
 *  The IgH EtherCAT Master is distributed in the hope that it will be
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *  GNU General Public License for more details.
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *  You should have received a copy of the GNU General Public License
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *  along with the IgH EtherCAT Master; if not, write to the Free Software
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
 *  The right to use EtherCAT Technology is granted and comes free of
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *  charge under condition of compatibility of product made by
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *  Licensee. People intending to distribute/sell products based on the
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
 *  code, have to sign an agreement to guarantee that products using
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
 *  software based on IgH EtherCAT master stay compatible with the actual
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 *  EtherCAT specification (which are released themselves as an open
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
 *  standard) as the (only) precondition to have the right to use EtherCAT
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
 *  Technology, IP and trade marks.
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
 *
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
 *****************************************************************************/
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
/**
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
   \file
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
   EtherCAT sync manager methods.
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
*/
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
/*****************************************************************************/
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
#include "globals.h"
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
#include "slave.h"
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
#include "master.h"
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
#include "sync.h"
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
/*****************************************************************************/
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
/**
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
 * Constructor.
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
 */
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
void ec_sync_init(
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
        ec_sync_t *sync, /**< EtherCAT sync manager */
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
        const ec_slave_t *slave, /**< EtherCAT slave */
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
        unsigned int index /**< sync manager index */
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
        )
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
{
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
    sync->slave = slave;
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
    sync->index = index;    
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
    sync->est_length = 0;
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
}
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
/*****************************************************************************/
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
/**
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
 * Destructor.
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
 */
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
void ec_sync_clear(
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
        ec_sync_t *sync /**< EtherCAT sync manager */
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
        )
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
{
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
}
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
/*****************************************************************************/
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
/**
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
   Initializes a sync manager configuration page with EEPROM data.
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
   The referenced memory (\a data) must be at least EC_SYNC_SIZE bytes.
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
*/
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
void ec_sync_config(
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
        const ec_sync_t *sync, /**< sync manager */
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
        uint8_t *data /**> configuration memory */
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
        )
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
{
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
    size_t sync_size;
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
    sync_size = ec_slave_calc_sync_size(sync->slave, sync);
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
    if (sync->slave->master->debug_level) {
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
        EC_DBG("SM%i: Addr 0x%04X, Size %3i, Ctrl 0x%02X, En %i\n",
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
               sync->index, sync->physical_start_address,
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
               sync_size, sync->control_register, sync->enable);
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
    }
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
    EC_WRITE_U16(data,     sync->physical_start_address);
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
    EC_WRITE_U16(data + 2, sync_size);
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
    EC_WRITE_U8 (data + 4, sync->control_register);
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
    EC_WRITE_U8 (data + 5, 0x00); // status byte (read only)
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
    EC_WRITE_U16(data + 6, sync->enable ? 0x0001 : 0x0000); // enable
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
}
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
/*****************************************************************************/