fp@409: /****************************************************************************** fp@409: * fp@409: * $Id$ fp@409: * fp@409: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@409: * fp@409: * This file is part of the IgH EtherCAT Master. fp@409: * fp@409: * The IgH EtherCAT Master is free software; you can redistribute it fp@409: * and/or modify it under the terms of the GNU General Public License fp@409: * as published by the Free Software Foundation; either version 2 of the fp@409: * License, or (at your option) any later version. fp@409: * fp@409: * The IgH EtherCAT Master is distributed in the hope that it will be fp@409: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@409: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@409: * GNU General Public License for more details. fp@409: * fp@409: * You should have received a copy of the GNU General Public License fp@409: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@409: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@409: * fp@409: * The right to use EtherCAT Technology is granted and comes free of fp@409: * charge under condition of compatibility of product made by fp@409: * Licensee. People intending to distribute/sell products based on the fp@409: * code, have to sign an agreement to guarantee that products using fp@409: * software based on IgH EtherCAT master stay compatible with the actual fp@409: * EtherCAT specification (which are released themselves as an open fp@409: * standard) as the (only) precondition to have the right to use EtherCAT fp@409: * Technology, IP and trade marks. fp@409: * fp@409: *****************************************************************************/ fp@409: fp@409: /** fp@409: \file fp@409: Canopen-over-EtherCAT functions. fp@409: */ fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: #include fp@409: fp@409: #include "canopen.h" fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: ssize_t ec_show_sdo_attribute(struct kobject *, struct attribute *, char *); fp@409: ssize_t ec_show_sdo_entry_attribute(struct kobject *, struct attribute *, fp@409: char *); fp@409: void ec_sdo_clear(struct kobject *); fp@409: void ec_sdo_entry_clear(struct kobject *); fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: /** \cond */ fp@409: fp@409: EC_SYSFS_READ_ATTR(info); fp@409: fp@409: static struct attribute *sdo_def_attrs[] = { fp@409: &attr_info, fp@409: NULL, fp@409: }; fp@409: fp@409: static struct sysfs_ops sdo_sysfs_ops = { fp@409: .show = &ec_show_sdo_attribute, fp@409: .store = NULL fp@409: }; fp@409: fp@409: static struct kobj_type ktype_ec_sdo = { fp@409: .release = ec_sdo_clear, fp@409: .sysfs_ops = &sdo_sysfs_ops, fp@409: .default_attrs = sdo_def_attrs fp@409: }; fp@409: fp@409: static struct attribute *sdo_entry_def_attrs[] = { fp@409: &attr_info, fp@409: NULL, fp@409: }; fp@409: fp@409: static struct sysfs_ops sdo_entry_sysfs_ops = { fp@409: .show = &ec_show_sdo_entry_attribute, fp@409: .store = NULL fp@409: }; fp@409: fp@409: static struct kobj_type ktype_ec_sdo_entry = { fp@409: .release = ec_sdo_entry_clear, fp@409: .sysfs_ops = &sdo_entry_sysfs_ops, fp@409: .default_attrs = sdo_entry_def_attrs fp@409: }; fp@409: fp@409: /** \endcond */ fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: /** fp@409: SDO constructor. fp@409: */ fp@409: fp@409: int ec_sdo_init(ec_sdo_t *sdo, /**< SDO */ fp@409: uint16_t index, /**< SDO index */ fp@409: ec_slave_t *slave /**< parent slave */ fp@409: ) fp@409: { fp@409: sdo->index = index; fp@409: sdo->object_code = 0x00; fp@409: sdo->name = NULL; fp@409: sdo->subindices = 0; fp@409: INIT_LIST_HEAD(&sdo->entries); fp@409: fp@409: // init kobject and add it to the hierarchy fp@409: memset(&sdo->kobj, 0x00, sizeof(struct kobject)); fp@409: kobject_init(&sdo->kobj); fp@409: sdo->kobj.ktype = &ktype_ec_sdo; fp@409: sdo->kobj.parent = &slave->kobj; fp@409: if (kobject_set_name(&sdo->kobj, "sdo%4X", sdo->index)) { fp@409: EC_ERR("Failed to set kobj name.\n"); fp@409: return -1; fp@409: } fp@409: fp@409: return 0; fp@409: } fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: /** fp@409: SDO destructor. fp@409: */ fp@409: fp@409: void ec_sdo_clear(struct kobject *kobj /**< SDO's kobject */) fp@409: { fp@409: ec_sdo_t *sdo = container_of(kobj, ec_sdo_t, kobj); fp@409: ec_sdo_entry_t *entry, *next; fp@409: fp@409: // free all entries fp@409: list_for_each_entry_safe(entry, next, &sdo->entries, list) { fp@409: list_del(&entry->list); fp@409: kobject_del(&entry->kobj); fp@409: kobject_put(&entry->kobj); fp@409: } fp@409: fp@409: if (sdo->name) kfree(sdo->name); fp@409: fp@409: kfree(sdo); fp@409: } fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: ssize_t ec_sdo_info(ec_sdo_t *sdo, /**< SDO */ fp@409: char *buffer /**< target buffer */ fp@409: ) fp@409: { fp@409: off_t off = 0; fp@409: fp@409: off += sprintf(buffer + off, "Index: 0x%04X\n", sdo->index); fp@409: off += sprintf(buffer + off, "Name: %s\n", sdo->name ? sdo->name : ""); fp@409: off += sprintf(buffer + off, "Subindices: %i\n", sdo->subindices); fp@409: fp@409: return off; fp@409: } fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: ssize_t ec_show_sdo_attribute(struct kobject *kobj, /**< kobject */ fp@409: struct attribute *attr, fp@409: char *buffer fp@409: ) fp@409: { fp@409: ec_sdo_t *sdo = container_of(kobj, ec_sdo_t, kobj); fp@409: fp@409: if (attr == &attr_info) { fp@409: return ec_sdo_info(sdo, buffer); fp@409: } fp@409: fp@409: return 0; fp@409: } fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: /** fp@409: SDO entry constructor. fp@409: */ fp@409: fp@409: int ec_sdo_entry_init(ec_sdo_entry_t *entry, /**< SDO entry */ fp@409: uint8_t subindex, /**< SDO entry subindex */ fp@409: ec_sdo_t *sdo /**< parent SDO */ fp@409: ) fp@409: { fp@409: entry->subindex = subindex; fp@409: entry->data_type = 0x0000; fp@409: entry->bit_length = 0; fp@409: entry->description = NULL; fp@409: fp@409: // init kobject and add it to the hierarchy fp@409: memset(&entry->kobj, 0x00, sizeof(struct kobject)); fp@409: kobject_init(&entry->kobj); fp@409: entry->kobj.ktype = &ktype_ec_sdo_entry; fp@409: entry->kobj.parent = &sdo->kobj; fp@409: if (kobject_set_name(&entry->kobj, "entry%02X", entry->subindex)) { fp@409: EC_ERR("Failed to set kobj name.\n"); fp@409: return -1; fp@409: } fp@409: fp@409: return 0; fp@409: } fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: /** fp@409: SDO destructor. fp@409: */ fp@409: fp@409: void ec_sdo_entry_clear(struct kobject *kobj /**< SDO entry's kobject */) fp@409: { fp@409: ec_sdo_entry_t *entry = container_of(kobj, ec_sdo_entry_t, kobj); fp@409: fp@409: if (entry->description) kfree(entry->description); fp@409: fp@409: kfree(entry); fp@409: } fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: ssize_t ec_sdo_entry_info(ec_sdo_entry_t *entry, /**< SDO entry */ fp@409: char *buffer /**< target buffer */ fp@409: ) fp@409: { fp@409: off_t off = 0; fp@409: fp@409: off += sprintf(buffer + off, "Subindex: 0x%02X\n", entry->subindex); fp@409: off += sprintf(buffer + off, "Description: %s\n", fp@409: entry->description ? entry->description : ""); fp@409: off += sprintf(buffer + off, "Data type: 0x%04X\n", entry->data_type); fp@409: off += sprintf(buffer + off, "Bit length: %i\n", entry->bit_length); fp@409: fp@409: return off; fp@409: } fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: ssize_t ec_show_sdo_entry_attribute(struct kobject *kobj, /**< kobject */ fp@409: struct attribute *attr, fp@409: char *buffer fp@409: ) fp@409: { fp@409: ec_sdo_entry_t *entry = container_of(kobj, ec_sdo_entry_t, kobj); fp@409: fp@409: if (attr == &attr_info) { fp@409: return ec_sdo_entry_info(entry, buffer); fp@409: } fp@409: fp@409: return 0; fp@409: } fp@409: fp@409: /*****************************************************************************/ fp@409: fp@409: #if 0 fp@409: int ecrt_slave_sdo_read(ec_slave_t *slave, /**< EtherCAT slave */ fp@409: uint16_t sdo_index, /**< SDO index */ fp@409: uint8_t sdo_subindex, /**< SDO subindex */ fp@409: uint8_t *target, /**< memory for value */ fp@409: size_t *size /**< target memory size */ fp@409: ) fp@409: { fp@409: uint8_t *data; fp@409: size_t rec_size, data_size; fp@409: uint32_t complete_size; fp@409: ec_datagram_t datagram; fp@409: fp@409: ec_datagram_init(&datagram); fp@409: fp@409: if (!(data = ec_slave_mbox_prepare_send(slave, &datagram, 0x03, 6))) fp@409: goto err; fp@409: fp@409: EC_WRITE_U16(data, 0x2 << 12); // SDO request fp@409: EC_WRITE_U8 (data + 2, 0x2 << 5); // initiate upload request fp@409: EC_WRITE_U16(data + 3, sdo_index); fp@409: EC_WRITE_U8 (data + 5, sdo_subindex); fp@409: fp@409: if (!(data = ec_slave_mbox_simple_io(slave, &datagram, &rec_size))) fp@409: goto err; fp@409: fp@409: if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request fp@409: EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request fp@409: EC_ERR("SDO upload 0x%04X:%X aborted on slave %i.\n", fp@409: sdo_index, sdo_subindex, slave->ring_position); fp@409: ec_canopen_abort_msg(EC_READ_U32(data + 6)); fp@409: goto err; fp@409: } fp@409: fp@409: if (EC_READ_U16(data) >> 12 != 0x3 || // SDO response fp@409: EC_READ_U8 (data + 2) >> 5 != 0x2 || // initiate upload response fp@409: EC_READ_U16(data + 3) != sdo_index || // index fp@409: EC_READ_U8 (data + 5) != sdo_subindex) { // subindex fp@409: EC_ERR("SDO upload 0x%04X:%X failed:\n", sdo_index, sdo_subindex); fp@409: EC_ERR("Invalid SDO upload response at slave %i!\n", fp@409: slave->ring_position); fp@409: ec_print_data(data, rec_size); fp@409: goto err; fp@409: } fp@409: fp@409: if (rec_size < 10) { fp@409: EC_ERR("Received currupted SDO upload response!\n"); fp@409: ec_print_data(data, rec_size); fp@409: goto err; fp@409: } fp@409: fp@409: if ((complete_size = EC_READ_U32(data + 6)) > *size) { fp@409: EC_ERR("SDO data does not fit into buffer (%i / %i)!\n", fp@409: complete_size, *size); fp@409: goto err; fp@409: } fp@409: fp@409: data_size = rec_size - 10; fp@409: fp@409: if (data_size != complete_size) { fp@409: EC_ERR("SDO data incomplete - Fragmenting not implemented.\n"); fp@409: goto err; fp@409: } fp@409: fp@409: memcpy(target, data + 10, data_size); fp@409: fp@409: ec_datagram_clear(&datagram); fp@409: return 0; fp@409: err: fp@409: ec_datagram_clear(&datagram); fp@409: return -1; fp@409: } fp@409: #endif fp@409: fp@409: /*****************************************************************************/