master/mailbox.c
changeset 154 94d780887be0
parent 145 11a82e4fd31b
child 195 674071846ee3
equal deleted inserted replaced
153:32446a24c322 154:94d780887be0
   104    Verarbeitet empfangene Mailbox-Daten.
   104    Verarbeitet empfangene Mailbox-Daten.
   105  */
   105  */
   106 
   106 
   107 uint8_t *ec_slave_mbox_fetch(ec_slave_t *slave, /**< Slave */
   107 uint8_t *ec_slave_mbox_fetch(ec_slave_t *slave, /**< Slave */
   108                              uint8_t type, /**< Protokoll */
   108                              uint8_t type, /**< Protokoll */
   109                              size_t *size /**< Größe der empfangenen
   109                              size_t *size /**< Größe der empfangenen Daten */
   110                                              Daten */
       
   111                              )
   110                              )
   112 {
   111 {
   113     ec_command_t *command = &slave->mbox_command;
   112     ec_command_t *command = &slave->mbox_command;
   114     size_t data_size;
   113     size_t data_size;
   115 
   114 
   211 
   210 
   212     return ec_slave_mbox_fetch(slave, type, size);
   211     return ec_slave_mbox_fetch(slave, type, size);
   213 }
   212 }
   214 
   213 
   215 /*****************************************************************************/
   214 /*****************************************************************************/
   216 
       
   217 #if 0
       
   218 /**
       
   219    Sendet ein Mailbox-Kommando.
       
   220  */
       
   221 
       
   222 int ec_slave_mbox_send(ec_slave_t *slave, /**< EtherCAT-Slave */
       
   223                        uint8_t type, /**< Unterliegendes Protokoll */
       
   224                        const uint8_t *prot_data, /**< Protokoll-Daten */
       
   225                        size_t size /**< Datengröße */
       
   226                        )
       
   227 {
       
   228     uint8_t *data;
       
   229     ec_command_t command;
       
   230 
       
   231 
       
   232     }
       
   233 
       
   234     if (!(data = kmalloc(slave->sii_rx_mailbox_size, GFP_KERNEL))) {
       
   235         EC_ERR("Failed to allocate %i bytes of memory for mailbox data!\n",
       
   236                slave->sii_rx_mailbox_size);
       
   237         return -1;
       
   238     }
       
   239 
       
   240     memset(data, 0x00, slave->sii_rx_mailbox_size);
       
   241     EC_WRITE_U16(data,      size); // Length of the Mailbox service data
       
   242     EC_WRITE_U16(data + 2,  slave->station_address); // Station address
       
   243     EC_WRITE_U8 (data + 4,  0x00); // Channel & priority
       
   244     EC_WRITE_U8 (data + 5,  type); // Underlying protocol type
       
   245     memcpy(data + 6, prot_data, size);
       
   246 
       
   247     ec_command_init_npwr(&command, slave->station_address,
       
   248                          slave->sii_rx_mailbox_offset,
       
   249                          slave->sii_rx_mailbox_size, data);
       
   250     if (unlikely(ec_master_simple_io(slave->master, &command))) {
       
   251         EC_ERR("Mailbox sending failed on slave %i!\n", slave->ring_position);
       
   252         kfree(data);
       
   253         return -1;
       
   254     }
       
   255 
       
   256     kfree(data);
       
   257     return 0;
       
   258 }
       
   259 
       
   260 /*****************************************************************************/
       
   261 
       
   262 /**
       
   263    Sendet ein Mailbox-Kommando.
       
   264  */
       
   265 
       
   266 int ec_slave_mailbox_receive(ec_slave_t *slave, /**< EtherCAT-Slave */
       
   267                              uint8_t type, /**< Unterliegendes Protokoll */
       
   268                              uint8_t *prot_data, /**< Protokoll-Daten */
       
   269                              size_t *size /**< Datengröße des Puffers, später
       
   270                                              Größe der gelesenen Daten */
       
   271                              )
       
   272 {
       
   273     ec_command_t command;
       
   274     size_t data_size;
       
   275     cycles_t start, end, timeout;
       
   276 
       
   277     // Read "written bit" of Sync-Manager
       
   278     start = get_cycles();
       
   279     timeout = (cycles_t) 100 * cpu_khz; // 100ms
       
   280 
       
   281     while (1)
       
   282     {
       
   283         // FIXME: Zweiter Sync-Manager nicht immer TX-Mailbox?
       
   284         ec_command_init_nprd(&command, slave->station_address, 0x808, 8);
       
   285         if (unlikely(ec_master_simple_io(slave->master, &command))) {
       
   286             EC_ERR("Mailbox checking failed on slave %i!\n",
       
   287                    slave->ring_position);
       
   288             return -1;
       
   289         }
       
   290 
       
   291         end = get_cycles();
       
   292 
       
   293         if (EC_READ_U8(command.data + 5) & 8)
       
   294             break; // Proceed with received data
       
   295 
       
   296         if ((end - start) >= timeout) {
       
   297             EC_ERR("Mailbox check - Slave %i timed out.\n",
       
   298                    slave->ring_position);
       
   299             return -1;
       
   300         }
       
   301 
       
   302         udelay(100);
       
   303     }
       
   304 
       
   305     ec_command_init_nprd(&command, slave->station_address,
       
   306                          slave->sii_tx_mailbox_offset,
       
   307                          slave->sii_tx_mailbox_size);
       
   308     if (unlikely(ec_master_simple_io(slave->master, &command))) {
       
   309         EC_ERR("Mailbox receiving failed on slave %i!\n",
       
   310                slave->ring_position);
       
   311         return -1;
       
   312     }
       
   313 
       
   314     if ((EC_READ_U8(command.data + 5) & 0x0F) != type) {
       
   315         EC_ERR("Unexpected mailbox protocol 0x%02X (exp.: 0x%02X) at"
       
   316                " slave %i!\n", EC_READ_U8(command.data + 5), type,
       
   317                slave->ring_position);
       
   318         return -1;
       
   319     }
       
   320 
       
   321     if (unlikely(slave->master->debug_level) > 1)
       
   322         EC_DBG("Mailbox receive took %ius.\n", ((u32) (end - start) * 1000
       
   323                                                 / cpu_khz));
       
   324 
       
   325     if ((data_size = EC_READ_U16(command.data)) > *size) {
       
   326         EC_ERR("Mailbox service data does not fit into buffer (%i > %i).\n",
       
   327                data_size, *size);
       
   328         return -1;
       
   329     }
       
   330 
       
   331     if (data_size > slave->sii_tx_mailbox_size - 6) {
       
   332         EC_ERR("Currupt mailbox response detected!\n");
       
   333         return -1;
       
   334     }
       
   335 
       
   336     memcpy(prot_data, command.data + 6, data_size);
       
   337     *size = data_size;
       
   338     return 0;
       
   339 }
       
   340 
       
   341 /*****************************************************************************/
       
   342 
       
   343 uint8_t *ec_slave_init_mbox_send_cmd(ec_slave_t *slave, /**< EtherCAT-Slave */
       
   344                                      ec_command_t *command, /**< Kommando */
       
   345                                      uint8_t type, /**< Protokolltyp */
       
   346                                      size_t size /**< Datengröße */
       
   347                                      )
       
   348 {
       
   349     size_t total_size;
       
   350     uint8_t *data;
       
   351 
       
   352     if (unlikely(!slave->sii_mailbox_protocols)) {
       
   353         EC_ERR("Slave %i does not support mailbox communication!\n",
       
   354                slave->ring_position);
       
   355         return NULL;
       
   356     }
       
   357 
       
   358     total_size = size + 6;
       
   359     if (unlikely(total_size > slave->sii_rx_mailbox_size)) {
       
   360         EC_ERR("Data size does not fit into mailbox of slave %i!\n",
       
   361                slave->ring_position);
       
   362         return NULL;
       
   363     }
       
   364 
       
   365     data = command->data;
       
   366 
       
   367     memset(data, 0x00, slave->sii_rx_mailbox_size);
       
   368     EC_WRITE_U16(data,     size); // Length of the Mailbox service data
       
   369     EC_WRITE_U16(data + 2, slave->station_address); // Station address
       
   370     EC_WRITE_U8 (data + 4, 0x00); // Channel & priority
       
   371     EC_WRITE_U8 (data + 5, type); // Underlying protocol type
       
   372 
       
   373     return data + 6;
       
   374 }
       
   375 #endif
       
   376 
       
   377 /*****************************************************************************/