50 uint8_t type, /**< mailbox protocol */ |
50 uint8_t type, /**< mailbox protocol */ |
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 uint16_t mailbox_offset, mailbox_size; |
|
56 |
55 |
57 if (unlikely(!slave->sii.mailbox_protocols)) { |
56 if (unlikely(!slave->sii.mailbox_protocols)) { |
58 EC_ERR("Slave %u does not support mailbox communication!\n", |
57 EC_ERR("Slave %u does not support mailbox communication!\n", |
59 slave->ring_position); |
58 slave->ring_position); |
60 return NULL; |
59 return NULL; |
61 } |
60 } |
62 |
61 |
63 total_size = size + 6; |
62 total_size = size + 6; |
64 |
63 |
65 if (slave->current_state != EC_SLAVE_STATE_BOOT) { |
64 if (unlikely(total_size > slave->configured_rx_mailbox_size)) { |
66 mailbox_offset = slave->sii.std_rx_mailbox_offset; |
65 EC_ERR("Data size (%u) does not fit in mailbox (%u)!\n", |
67 mailbox_size = slave->sii.std_rx_mailbox_size; |
66 total_size, slave->configured_rx_mailbox_size); |
68 } else { |
|
69 mailbox_offset = slave->sii.boot_rx_mailbox_offset; |
|
70 mailbox_size = slave->sii.boot_rx_mailbox_size; |
|
71 } |
|
72 |
|
73 if (unlikely(total_size > mailbox_size)) { |
|
74 EC_ERR("Data size does not fit in mailbox!\n"); |
|
75 return NULL; |
67 return NULL; |
76 } |
68 } |
77 |
69 |
78 if (ec_datagram_fpwr(datagram, slave->station_address, |
70 if (ec_datagram_fpwr(datagram, slave->station_address, |
79 mailbox_offset, mailbox_size)) |
71 slave->configured_rx_mailbox_offset, |
|
72 slave->configured_rx_mailbox_size)) |
80 return NULL; |
73 return NULL; |
81 |
74 |
82 EC_WRITE_U16(datagram->data, size); // mailbox service data length |
75 EC_WRITE_U16(datagram->data, size); // mailbox service data length |
83 EC_WRITE_U16(datagram->data + 2, slave->station_address); // station addr. |
76 EC_WRITE_U16(datagram->data + 2, slave->station_address); // station addr. |
84 EC_WRITE_U8 (datagram->data + 4, 0x00); // channel & priority |
77 EC_WRITE_U8 (datagram->data + 4, 0x00); // channel & priority |
126 |
119 |
127 int ec_slave_mbox_prepare_fetch(const ec_slave_t *slave, /**< slave */ |
120 int ec_slave_mbox_prepare_fetch(const ec_slave_t *slave, /**< slave */ |
128 ec_datagram_t *datagram /**< datagram */ |
121 ec_datagram_t *datagram /**< datagram */ |
129 ) |
122 ) |
130 { |
123 { |
131 uint16_t mailbox_offset, mailbox_size; |
|
132 |
|
133 if (slave->current_state != EC_SLAVE_STATE_BOOT) { |
|
134 mailbox_offset = slave->sii.std_tx_mailbox_offset; |
|
135 mailbox_size = slave->sii.std_tx_mailbox_size; |
|
136 } else { |
|
137 mailbox_offset = slave->sii.boot_tx_mailbox_offset; |
|
138 mailbox_size = slave->sii.boot_tx_mailbox_size; |
|
139 } |
|
140 |
|
141 if (ec_datagram_fprd(datagram, slave->station_address, |
124 if (ec_datagram_fprd(datagram, slave->station_address, |
142 mailbox_offset, mailbox_size)) |
125 slave->configured_tx_mailbox_offset, |
|
126 slave->configured_tx_mailbox_size)) |
143 return -1; |
127 return -1; |
144 |
128 |
145 return 0; |
129 return 0; |
146 } |
130 } |
147 |
131 |
175 uint8_t *type, /**< expected mailbox protocol */ |
159 uint8_t *type, /**< expected mailbox protocol */ |
176 size_t *size /**< size of the received data */ |
160 size_t *size /**< size of the received data */ |
177 ) |
161 ) |
178 { |
162 { |
179 size_t data_size; |
163 size_t data_size; |
180 uint16_t mailbox_offset, mailbox_size; |
|
181 |
|
182 if (slave->current_state != EC_SLAVE_STATE_BOOT) { |
|
183 mailbox_offset = slave->sii.std_tx_mailbox_offset; |
|
184 mailbox_size = slave->sii.std_tx_mailbox_size; |
|
185 } else { |
|
186 mailbox_offset = slave->sii.boot_tx_mailbox_offset; |
|
187 mailbox_size = slave->sii.boot_tx_mailbox_size; |
|
188 } |
|
189 |
164 |
190 data_size = EC_READ_U16(datagram->data); |
165 data_size = EC_READ_U16(datagram->data); |
191 |
166 |
192 if (data_size > mailbox_size - 6) { |
167 if (data_size > slave->configured_tx_mailbox_size - 6) { |
193 EC_ERR("Corrupt mailbox response received from slave %u!\n", |
168 EC_ERR("Corrupt mailbox response received from slave %u!\n", |
194 slave->ring_position); |
169 slave->ring_position); |
195 ec_print_data(datagram->data, mailbox_size); |
170 ec_print_data(datagram->data, slave->configured_tx_mailbox_size); |
196 return NULL; |
171 return NULL; |
197 } |
172 } |
198 |
173 |
199 *type = EC_READ_U8(datagram->data + 5) & 0x0F; |
174 *type = EC_READ_U8(datagram->data + 5) & 0x0F; |
200 *size = data_size; |
175 *size = data_size; |