master/slave.c
changeset 89 e91ef35c36db
parent 84 b4ae98855cea
child 91 0120d6214948
equal deleted inserted replaced
88:98335157e54e 89:e91ef35c36db
    66 
    66 
    67     // Read base data
    67     // Read base data
    68     ec_frame_init_nprd(&frame, slave->master, slave->station_address,
    68     ec_frame_init_nprd(&frame, slave->master, slave->station_address,
    69                        0x0000, 6);
    69                        0x0000, 6);
    70 
    70 
    71     if (unlikely(ec_frame_send_receive(&frame))) return -1;
    71     if (unlikely(ec_frame_send_receive(&frame))) {
    72 
    72         EC_ERR("Reading base datafrom slave %i failed!\n",
    73     if (unlikely(frame.working_counter != 1)) {
       
    74         EC_ERR("Slave %i did not respond while reading base data!\n",
       
    75                slave->ring_position);
    73                slave->ring_position);
    76         return -1;
    74         return -1;
    77     }
    75     }
    78 
    76 
    79     slave->base_type =       EC_READ_U8 (frame.data);
    77     slave->base_type =       EC_READ_U8 (frame.data);
   139     EC_WRITE_U8 (data,     0x00);
   137     EC_WRITE_U8 (data,     0x00);
   140     EC_WRITE_U8 (data + 1, 0x01);
   138     EC_WRITE_U8 (data + 1, 0x01);
   141     EC_WRITE_U16(data + 2, offset);
   139     EC_WRITE_U16(data + 2, offset);
   142     EC_WRITE_U16(data + 4, 0x0000);
   140     EC_WRITE_U16(data + 4, 0x0000);
   143 
   141 
   144     ec_frame_init_npwr(&frame, slave->master, slave->station_address, 0x502, 6,
   142     ec_frame_init_npwr(&frame, slave->master, slave->station_address,
   145                        data);
   143                        0x502, 6, data);
   146 
   144 
   147     if (unlikely(ec_frame_send_receive(&frame))) return -1;
   145     if (unlikely(ec_frame_send_receive(&frame))) {
   148 
   146         EC_ERR("SII-read failed on slave %i!\n", slave->ring_position);
   149     if (unlikely(frame.working_counter != 1)) {
       
   150         EC_ERR("SII-read - Slave %i did not respond!\n", slave->ring_position);
       
   151         return -1;
   147         return -1;
   152     }
   148     }
   153 
   149 
   154     // Der Slave legt die Informationen des Slave-Information-Interface
   150     // Der Slave legt die Informationen des Slave-Information-Interface
   155     // in das Datenregister und löscht daraufhin ein Busy-Bit. Solange
   151     // in das Datenregister und löscht daraufhin ein Busy-Bit. Solange
   158     tries_left = 100;
   154     tries_left = 100;
   159     while (likely(tries_left))
   155     while (likely(tries_left))
   160     {
   156     {
   161         udelay(10);
   157         udelay(10);
   162 
   158 
   163         ec_frame_init_nprd(&frame, slave->master, slave->station_address, 0x502,
   159         ec_frame_init_nprd(&frame, slave->master, slave->station_address,
   164                            10);
   160                            0x502, 10);
   165 
   161 
   166         if (unlikely(ec_frame_send_receive(&frame))) return -1;
   162         if (unlikely(ec_frame_send_receive(&frame))) {
   167 
   163             EC_ERR("Getting SII-read status failed on slave %i!\n",
   168         if (unlikely(frame.working_counter != 1)) {
       
   169             EC_ERR("SII-read status - Slave %i did not respond!\n",
       
   170                    slave->ring_position);
   164                    slave->ring_position);
   171             return -1;
   165             return -1;
   172         }
   166         }
   173 
   167 
   174         if (likely((EC_READ_U8(frame.data + 1) & 0x81) == 0)) {
   168         if (likely((EC_READ_U8(frame.data + 1) & 0x81) == 0)) {
   205     unsigned char data[2];
   199     unsigned char data[2];
   206     unsigned int tries_left;
   200     unsigned int tries_left;
   207 
   201 
   208     EC_WRITE_U16(data, state | EC_ACK);
   202     EC_WRITE_U16(data, state | EC_ACK);
   209 
   203 
   210     ec_frame_init_npwr(&frame, slave->master, slave->station_address, 0x0120,
   204     ec_frame_init_npwr(&frame, slave->master, slave->station_address,
   211                        2, data);
   205                        0x0120, 2, data);
   212 
   206 
   213     if (unlikely(ec_frame_send_receive(&frame) != 0)) {
   207     if (unlikely(ec_frame_send_receive(&frame))) {
   214         EC_WARN("Could no acknowledge state %02X - Unable to send!\n", state);
   208         EC_WARN("State %02X acknowledge failed on slave %i!\n",
   215         return;
   209                 state, slave->ring_position);
   216     }
       
   217 
       
   218     if (unlikely(frame.working_counter != 1)) {
       
   219         EC_WARN("Could not acknowledge state %02X - Slave %i did not"
       
   220                 " respond!\n", state, slave->ring_position);
       
   221         return;
   210         return;
   222     }
   211     }
   223 
   212 
   224     tries_left = 100;
   213     tries_left = 100;
   225     while (likely(tries_left))
   214     while (likely(tries_left))
   227         udelay(10);
   216         udelay(10);
   228 
   217 
   229         ec_frame_init_nprd(&frame, slave->master, slave->station_address,
   218         ec_frame_init_nprd(&frame, slave->master, slave->station_address,
   230                            0x0130, 2);
   219                            0x0130, 2);
   231 
   220 
   232         if (unlikely(ec_frame_send_receive(&frame) != 0)) {
   221         if (unlikely(ec_frame_send_receive(&frame))) {
   233             EC_WARN("Could not check state acknowledgement %02X - Unable to"
   222             EC_WARN("State %02X acknowledge checking failed on slave %i!\n",
   234                     " send!\n", state);
   223                     state, slave->ring_position);
   235             return;
       
   236         }
       
   237 
       
   238         if (unlikely(frame.working_counter != 1)) {
       
   239             EC_WARN("Could not check state acknowledgement %02X - Slave %i did"
       
   240                     " not respond!\n", state, slave->ring_position);
       
   241             return;
   224             return;
   242         }
   225         }
   243 
   226 
   244         if (unlikely(EC_READ_U8(frame.data) != state)) {
   227         if (unlikely(EC_READ_U8(frame.data) != state)) {
   245             EC_WARN("Could not acknowledge state %02X on slave %i (code"
   228             EC_WARN("Could not acknowledge state %02X on slave %i (code"
   282     unsigned char data[2];
   265     unsigned char data[2];
   283     unsigned int tries_left;
   266     unsigned int tries_left;
   284 
   267 
   285     EC_WRITE_U16(data, state);
   268     EC_WRITE_U16(data, state);
   286 
   269 
   287     ec_frame_init_npwr(&frame, slave->master, slave->station_address, 0x0120,
   270     ec_frame_init_npwr(&frame, slave->master, slave->station_address,
   288                        2, data);
   271                        0x0120, 2, data);
   289 
   272 
   290     if (unlikely(ec_frame_send_receive(&frame) != 0)) {
   273     if (unlikely(ec_frame_send_receive(&frame))) {
   291         EC_ERR("Could not set state %02X - Unable to send!\n", state);
   274         EC_ERR("Failed to set state %02X on slave %i!\n",
   292         return -1;
   275                state, slave->ring_position);
   293     }
       
   294 
       
   295     if (unlikely(frame.working_counter != 1)) {
       
   296         EC_ERR("Could not set state %02X - Slave %i did not respond!\n", state,
       
   297                slave->ring_position);
       
   298         return -1;
   276         return -1;
   299     }
   277     }
   300 
   278 
   301     tries_left = 100;
   279     tries_left = 100;
   302     while (likely(tries_left))
   280     while (likely(tries_left))
   304         udelay(10);
   282         udelay(10);
   305 
   283 
   306         ec_frame_init_nprd(&frame, slave->master, slave->station_address,
   284         ec_frame_init_nprd(&frame, slave->master, slave->station_address,
   307                            0x0130, 2);
   285                            0x0130, 2);
   308 
   286 
   309         if (unlikely(ec_frame_send_receive(&frame) != 0)) {
   287         if (unlikely(ec_frame_send_receive(&frame))) {
   310             EC_ERR("Could not check state %02X - Unable to send!\n", state);
   288             EC_ERR("Failed to check state %02X on slave %i!\n",
   311             return -1;
       
   312         }
       
   313 
       
   314         if (unlikely(frame.working_counter != 1)) {
       
   315             EC_ERR("Could not check state %02X - Slave %i did not respond!\n",
       
   316                    state, slave->ring_position);
   289                    state, slave->ring_position);
   317             return -1;
   290             return -1;
   318         }
   291         }
   319 
   292 
   320         if (unlikely(EC_READ_U8(frame.data) & 0x10)) { // State change error
   293         if (unlikely(EC_READ_U8(frame.data) & 0x10)) { // State change error
   429 int ec_slave_check_crc(ec_slave_t *slave /**< EtherCAT-Slave */)
   402 int ec_slave_check_crc(ec_slave_t *slave /**< EtherCAT-Slave */)
   430 {
   403 {
   431     ec_frame_t frame;
   404     ec_frame_t frame;
   432     uint8_t data[4];
   405     uint8_t data[4];
   433 
   406 
   434     ec_frame_init_nprd(&frame, slave->master, slave->station_address, 0x0300,
   407     ec_frame_init_nprd(&frame, slave->master, slave->station_address,
   435                        4);
   408                        0x0300, 4);
   436 
   409 
   437     if (unlikely(ec_frame_send_receive(&frame))) {
   410     if (unlikely(ec_frame_send_receive(&frame))) {
   438         EC_WARN("Reading CRC fault counters failed on slave %i - Could not"
   411         EC_WARN("Reading CRC fault counters failed on slave %i!\n",
   439                 " send command!\n", slave->ring_position);
       
   440         return -1;
       
   441     }
       
   442 
       
   443     if (unlikely(frame.working_counter != 1)) {
       
   444         EC_WARN("Reading CRC fault counters - Slave %i did not respond!\n",
       
   445                 slave->ring_position);
   412                 slave->ring_position);
   446         return -1;
   413         return -1;
   447     }
   414     }
   448 
   415 
   449     // No CRC faults.
   416     // No CRC faults.
   453             EC_READ_U16(frame.data), EC_READ_U16(frame.data + 2));
   420             EC_READ_U16(frame.data), EC_READ_U16(frame.data + 2));
   454 
   421 
   455     // Reset CRC counters
   422     // Reset CRC counters
   456     EC_WRITE_U16(data,     0x0000);
   423     EC_WRITE_U16(data,     0x0000);
   457     EC_WRITE_U16(data + 2, 0x0000);
   424     EC_WRITE_U16(data + 2, 0x0000);
   458     ec_frame_init_npwr(&frame, slave->master, slave->station_address, 0x0300,
   425     ec_frame_init_npwr(&frame, slave->master, slave->station_address,
   459                        4, data);
   426                        0x0300, 4, data);
   460 
   427 
   461     if (unlikely(ec_frame_send_receive(&frame))) return -1;
   428     if (unlikely(ec_frame_send_receive(&frame))) {
   462 
   429         EC_WARN("Resetting CRC fault counters failed on slave %i!\n",
   463     if (unlikely(frame.working_counter != 1)) {
       
   464         EC_WARN("Resetting CRC fault counters - Slave %i did not respond!\n",
       
   465                 slave->ring_position);
   430                 slave->ring_position);
   466         return -1;
   431         return -1;
   467     }
   432     }
   468 
   433 
   469     return 0;
   434     return 0;