Skip to content
Snippets Groups Projects
slave.c 32.2 KiB
Newer Older
                             size_t *size /**< Datengre des Puffers, spter
                                             Gre der gelesenen Daten */
                             )
{
    ec_command_t command;
    size_t data_size;
    cycles_t start, end, timeout;

    // Read "written bit" of Sync-Manager
    start = get_cycles();
    timeout = (cycles_t) 100 * cpu_khz; // 100ms

    while (1)
    {
        ec_command_init_nprd(&command, slave->station_address, 0x808, 8);
        if (unlikely(ec_master_simple_io(slave->master, &command))) {
            EC_ERR("Mailbox checking failed on slave %i!\n",
                   slave->ring_position);
            return -1;
        }

        end = get_cycles();

        if (EC_READ_U8(command.data + 5) & 8) break; // Written bit is high

        if ((end - start) >= timeout) {
            EC_ERR("Mailbox check - Slave %i timed out.\n",
                   slave->ring_position);
            return -1;
        }

        udelay(100);
    }

    if (unlikely(slave->master->debug_level) > 1)
        EC_DBG("SDO download took %ius.\n", ((u32) (end - start) * 1000
                                             / cpu_khz));

    ec_command_init_nprd(&command, slave->station_address, 0x18F6, 0xF6);
    if (unlikely(ec_master_simple_io(slave->master, &command))) {
        EC_ERR("Mailbox receiving failed on slave %i!\n",
               slave->ring_position);
        return -1;
    }

    if (EC_READ_U8(command.data + 5) != type) { // nicht CoE
        EC_ERR("Invalid mailbox response (non-CoE) at slave %i!\n",
               slave->ring_position);
        return -1;
    }

    if ((data_size = EC_READ_U16(command.data)) > *size) {
        EC_ERR("CoE data does not fit in buffer (%i > %i).\n",
               data_size, *size);
        return -1;
    }

    memcpy(prot_data, command.data + 6, data_size);
    *size = data_size;
    return 0;
}

/*****************************************************************************/

/* Emacs-Konfiguration
;;; Local Variables: ***
;;; End: ***
*/