master/sync.c
author Florian Pose <fp@igh-essen.com>
Thu, 09 Aug 2007 14:38:18 +0000
changeset 689 880b556a6efc
parent 635 d304ef4af542
child 745 3f4e5a50733e
permissions -rw-r--r--
Removed checking for NULL slave pointer in EoE handler; added comments.
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"
635
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    44
#include "pdo.h"
628
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
#include "sync.h"
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
/**
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
 * Constructor.
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
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
void ec_sync_init(
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
        ec_sync_t *sync, /**< EtherCAT sync manager */
635
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    55
        ec_slave_t *slave, /**< EtherCAT slave */
628
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
        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
    57
        )
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
{
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
    sync->slave = slave;
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
    sync->index = index;    
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
    sync->est_length = 0;
635
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    63
    INIT_LIST_HEAD(&sync->pdos);
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    64
    sync->alt_mapping = 0;
628
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
/*****************************************************************************/
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
 * Destructor.
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
 */
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
void ec_sync_clear(
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
        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
    75
        )
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
{
635
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    77
    ec_sync_clear_pdos(sync);
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    78
}
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    79
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    80
/*****************************************************************************/
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    81
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    82
/**
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    83
 * Calculates the size of a sync manager by evaluating PDO sizes.
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    84
 * \return sync manager size
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    85
 */
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    86
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    87
uint16_t ec_sync_size(
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    88
        const ec_sync_t *sync /**< sync manager */
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    89
        )
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    90
{
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    91
    const ec_pdo_t *pdo;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    92
    const ec_pdo_entry_t *pdo_entry;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    93
    unsigned int bit_size, byte_size;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    94
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    95
    if (sync->length) return sync->length;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    96
    if (sync->est_length) return sync->est_length;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    97
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    98
    bit_size = 0;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
    99
    list_for_each_entry(pdo, &sync->pdos, list) {
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   100
        list_for_each_entry(pdo_entry, &pdo->entries, list) {
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   101
            bit_size += pdo_entry->bit_length;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   102
        }
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   103
    }
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   104
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   105
    if (bit_size % 8) // round up to full bytes
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   106
        byte_size = bit_size / 8 + 1;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   107
    else
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   108
        byte_size = bit_size / 8;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   109
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   110
    return byte_size;
628
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
}
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
/*****************************************************************************/
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
/**
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
   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
   117
   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
   118
*/
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
void ec_sync_config(
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
        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
   122
        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
   123
        )
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
{
635
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   125
    size_t sync_size = ec_sync_size(sync);
628
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
    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
   128
        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
   129
               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
   130
               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
   131
    }
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
    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
   134
    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
   135
    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
   136
    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
   137
    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
   138
}
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
e94a16bc52ce Layed out sync manager structures and methods into own files.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
/*****************************************************************************/
635
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   141
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   142
/**
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   143
 */
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   144
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   145
int ec_sync_add_pdo(
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   146
        ec_sync_t *sync, /**< EtherCAT sync manager */
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   147
        const ec_pdo_t *pdo /**< PDO to map */
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   148
        )
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   149
{
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   150
    ec_pdo_t *mapped_pdo;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   151
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   152
    // PDO already mapped?
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   153
    list_for_each_entry(mapped_pdo, &sync->pdos, list) {
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   154
        if (mapped_pdo->index != pdo->index) continue;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   155
        EC_ERR("PDO 0x%04X is already mapped!\n", pdo->index);
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   156
        return -1;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   157
    }
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   158
    
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   159
    if (!(mapped_pdo = kmalloc(sizeof(ec_pdo_t), GFP_KERNEL))) {
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   160
        EC_ERR("Failed to allocate memory for PDO mapping.\n");
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   161
        return -1;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   162
    }
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   163
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   164
    ec_pdo_init(mapped_pdo);
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   165
    if (ec_pdo_copy(mapped_pdo, pdo)) {
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   166
        ec_pdo_clear(mapped_pdo);
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   167
        kfree(mapped_pdo);
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   168
        return -1;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   169
    }
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   170
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   171
    // set appropriate sync manager index
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   172
    mapped_pdo->sync_index = sync->index;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   173
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   174
    list_add_tail(&mapped_pdo->list, &sync->pdos);
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   175
    sync->alt_mapping = 1;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   176
    return 0;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   177
}
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   178
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   179
/*****************************************************************************/
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   180
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   181
/**
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   182
 */
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   183
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   184
void ec_sync_clear_pdos(
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   185
        ec_sync_t *sync /**< EtherCAT sync manager */
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   186
        )
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   187
{
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   188
    ec_pdo_t *pdo, *next;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   189
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   190
    // free all mapped PDOs
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   191
    list_for_each_entry_safe(pdo, next, &sync->pdos, list) {
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   192
        list_del(&pdo->list);
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   193
        ec_pdo_clear(pdo);
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   194
        kfree(pdo);
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   195
    }
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   196
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   197
    sync->alt_mapping = 1;
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   198
}
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   199
d304ef4af542 Implemented alternative PDO mapping configuration interface.
Florian Pose <fp@igh-essen.com>
parents: 628
diff changeset
   200
/*****************************************************************************/