master/mailbox.c
changeset 1338 eb31b5a135da
parent 1337 0253c74d0940
child 1363 11c0b2caa253
equal deleted inserted replaced
1337:0253c74d0940 1338:eb31b5a135da
    51                                     size_t size /**< size of the data */
    51                                     size_t size /**< size of the data */
    52                                     )
    52                                     )
    53 {
    53 {
    54     size_t total_size;
    54     size_t total_size;
    55     int ret;
    55     int ret;
    56     uint16_t mailbox_offset, mailbox_size;
       
    57 
    56 
    58     if (unlikely(!slave->sii.mailbox_protocols)) {
    57     if (unlikely(!slave->sii.mailbox_protocols)) {
    59         EC_ERR("Slave %u does not support mailbox communication!\n",
    58         EC_ERR("Slave %u does not support mailbox communication!\n",
    60                slave->ring_position);
    59                slave->ring_position);
    61         return ERR_PTR(-EPROTONOSUPPORT);
    60         return ERR_PTR(-EPROTONOSUPPORT);
    62     }
    61     }
    63 
    62 
    64     total_size = EC_MBOX_HEADER_SIZE + size;
    63     total_size = EC_MBOX_HEADER_SIZE + size;
    65 
    64 
    66     if (slave->current_state != EC_SLAVE_STATE_BOOT) {
    65     if (unlikely(total_size > slave->configured_rx_mailbox_size)) {
    67         mailbox_offset = slave->sii.std_rx_mailbox_offset;
    66         EC_ERR("Data size (%u) does not fit in mailbox (%u)!\n",
    68         mailbox_size = slave->sii.std_rx_mailbox_size;
    67                 total_size, slave->configured_rx_mailbox_size);
    69     } else {
       
    70         mailbox_offset = slave->sii.boot_rx_mailbox_offset;
       
    71         mailbox_size = slave->sii.boot_rx_mailbox_size;
       
    72     }
       
    73 
       
    74     if (unlikely(total_size > mailbox_size)) {
       
    75         EC_ERR("Data size does not fit in mailbox!\n");
       
    76         return ERR_PTR(-EOVERFLOW);
    68         return ERR_PTR(-EOVERFLOW);
    77     }
    69     }
    78 
    70 
    79     ret = ec_datagram_fpwr(datagram, slave->station_address,
    71     ret = ec_datagram_fpwr(datagram, slave->station_address,
    80             mailbox_offset, mailbox_size);
    72             slave->configured_rx_mailbox_offset,
       
    73             slave->configured_rx_mailbox_size);
    81     if (ret)
    74     if (ret)
    82         return ERR_PTR(ret);
    75         return ERR_PTR(ret);
    83 
    76 
    84     EC_WRITE_U16(datagram->data,     size); // mailbox service data length
    77     EC_WRITE_U16(datagram->data,     size); // mailbox service data length
    85     EC_WRITE_U16(datagram->data + 2, slave->station_address); // station addr.
    78     EC_WRITE_U16(datagram->data + 2, slave->station_address); // station addr.
   130 
   123 
   131 int ec_slave_mbox_prepare_fetch(const ec_slave_t *slave, /**< slave */
   124 int ec_slave_mbox_prepare_fetch(const ec_slave_t *slave, /**< slave */
   132                                 ec_datagram_t *datagram /**< datagram */
   125                                 ec_datagram_t *datagram /**< datagram */
   133                                 )
   126                                 )
   134 {
   127 {
   135     int ret;
   128     int ret = ec_datagram_fprd(datagram, slave->station_address,
   136     uint16_t mailbox_offset, mailbox_size;
   129             slave->configured_tx_mailbox_offset,
   137 
   130             slave->configured_tx_mailbox_size);
   138     if (slave->current_state != EC_SLAVE_STATE_BOOT) {
       
   139         mailbox_offset = slave->sii.std_tx_mailbox_offset;
       
   140         mailbox_size = slave->sii.std_tx_mailbox_size;
       
   141     } else {
       
   142         mailbox_offset = slave->sii.boot_tx_mailbox_offset;
       
   143         mailbox_size = slave->sii.boot_tx_mailbox_size;
       
   144     }
       
   145 
       
   146     ret = ec_datagram_fprd(datagram, slave->station_address,
       
   147             mailbox_offset, mailbox_size);
       
   148     if (ret)
   131     if (ret)
   149         return ret;
   132         return ret;
   150 
   133 
   151     ec_datagram_zero(datagram);
   134     ec_datagram_zero(datagram);
   152     return 0;
   135     return 0;
   181                              uint8_t *type, /**< expected mailbox protocol */
   164                              uint8_t *type, /**< expected mailbox protocol */
   182                              size_t *size /**< size of the received data */
   165                              size_t *size /**< size of the received data */
   183                              )
   166                              )
   184 {
   167 {
   185     size_t data_size;
   168     size_t data_size;
   186     uint16_t mailbox_offset, mailbox_size;
       
   187 
       
   188     if (slave->current_state != EC_SLAVE_STATE_BOOT) {
       
   189         mailbox_offset = slave->sii.std_tx_mailbox_offset;
       
   190         mailbox_size = slave->sii.std_tx_mailbox_size;
       
   191     } else {
       
   192         mailbox_offset = slave->sii.boot_tx_mailbox_offset;
       
   193         mailbox_size = slave->sii.boot_tx_mailbox_size;
       
   194     }
       
   195 
   169 
   196     data_size = EC_READ_U16(datagram->data);
   170     data_size = EC_READ_U16(datagram->data);
   197 
   171 
   198     if (data_size + EC_MBOX_HEADER_SIZE > mailbox_size) {
   172     if (data_size + EC_MBOX_HEADER_SIZE > slave->configured_tx_mailbox_size) {
   199         EC_ERR("Corrupt mailbox response received from slave %u!\n",
   173         EC_ERR("Corrupt mailbox response received from slave %u!\n",
   200                 slave->ring_position);
   174                 slave->ring_position);
   201         ec_print_data(datagram->data, mailbox_size);
   175         ec_print_data(datagram->data, slave->configured_tx_mailbox_size);
   202         return ERR_PTR(-EPROTO);
   176         return ERR_PTR(-EPROTO);
   203     }
   177     }
   204 
   178 
   205     *type = EC_READ_U8(datagram->data + 5) & 0x0F;
   179     *type = EC_READ_U8(datagram->data + 5) & 0x0F;
   206     *size = data_size;
   180     *size = data_size;