00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00039
00040
00041 #include <linux/slab.h>
00042 #include <linux/delay.h>
00043
00044 #include "mailbox.h"
00045 #include "datagram.h"
00046 #include "master.h"
00047
00048
00049
00055 uint8_t *ec_slave_mbox_prepare_send(const ec_slave_t *slave,
00056 ec_datagram_t *datagram,
00057 uint8_t type,
00058 size_t size
00059 )
00060 {
00061 size_t total_size;
00062
00063 if (unlikely(!slave->sii_mailbox_protocols)) {
00064 EC_ERR("Slave %i does not support mailbox communication!\n",
00065 slave->ring_position);
00066 return NULL;
00067 }
00068
00069 total_size = size + 6;
00070 if (unlikely(total_size > slave->sii_rx_mailbox_size)) {
00071 EC_ERR("Data size does not fit in mailbox!\n");
00072 return NULL;
00073 }
00074
00075 if (ec_datagram_npwr(datagram, slave->station_address,
00076 slave->sii_rx_mailbox_offset,
00077 slave->sii_rx_mailbox_size))
00078 return NULL;
00079
00080 EC_WRITE_U16(datagram->data, size);
00081 EC_WRITE_U16(datagram->data + 2, slave->station_address);
00082 EC_WRITE_U8 (datagram->data + 4, 0x00);
00083 EC_WRITE_U8 (datagram->data + 5, type);
00084
00085 return datagram->data + 6;
00086 }
00087
00088
00089
00095 int ec_slave_mbox_prepare_check(const ec_slave_t *slave,
00096 ec_datagram_t *datagram
00097 )
00098 {
00099
00100 if (ec_datagram_nprd(datagram, slave->station_address, 0x808, 8))
00101 return -1;
00102
00103 return 0;
00104 }
00105
00106
00107
00113 int ec_slave_mbox_check(const ec_datagram_t *datagram )
00114 {
00115 return EC_READ_U8(datagram->data + 5) & 8 ? 1 : 0;
00116 }
00117
00118
00119
00125 int ec_slave_mbox_prepare_fetch(const ec_slave_t *slave,
00126 ec_datagram_t *datagram
00127 )
00128 {
00129 if (ec_datagram_nprd(datagram, slave->station_address,
00130 slave->sii_tx_mailbox_offset,
00131 slave->sii_tx_mailbox_size)) return -1;
00132 return 0;
00133 }
00134
00135
00136
00142 uint8_t *ec_slave_mbox_fetch(const ec_slave_t *slave,
00143 ec_datagram_t *datagram,
00144 uint8_t type,
00145 size_t *size
00146 )
00147 {
00148 size_t data_size;
00149
00150 if ((EC_READ_U8(datagram->data + 5) & 0x0F) != type) {
00151 EC_ERR("Unexpected mailbox protocol 0x%02X (exp.: 0x%02X) at"
00152 " slave %i!\n", EC_READ_U8(datagram->data + 5), type,
00153 slave->ring_position);
00154 return NULL;
00155 }
00156
00157 if ((data_size = EC_READ_U16(datagram->data)) >
00158 slave->sii_tx_mailbox_size - 6) {
00159 EC_ERR("Currupt mailbox response detected!\n");
00160 return NULL;
00161 }
00162
00163 *size = data_size;
00164 return datagram->data + 6;
00165 }
00166
00167