43 #include PLATFORM_HEADER
44 #include "hal/error.h"
61 #define LED_ACTIVITY 0
66 #define PRINTF(...) printf(__VA_ARGS__)
68 #define PRINTF(...) do {} while (0)
72 #define LED_TX_ON() leds_on(LEDS_GREEN)
73 #define LED_TX_OFF() leds_off(LEDS_GREEN)
74 #define LED_RX_ON() leds_on(LEDS_RED)
75 #define LED_RX_OFF() leds_off(LEDS_RED)
89 int8_t mac_retries_left;
91 #define INIT_RETRY_CNT() (mac_retries_left = packetbuf_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS))
92 #define DEC_RETRY_CNT() (mac_retries_left--)
93 #define RETRY_CNT_GTZ() (mac_retries_left > 0)
97 #define INIT_RETRY_CNT()
98 #define DEC_RETRY_CNT()
99 #define RETRY_CNT_GTZ() 0
106 #ifndef RADIO_WAIT_FOR_PACKET_SENT
107 #define RADIO_WAIT_FOR_PACKET_SENT 1
110 #define TO_PREV_STATE() { \
111 if(onoroff == OFF){ \
113 ENERGEST_OFF(ENERGEST_TYPE_LISTEN); \
117 const RadioTransmitConfig radioTransmitConfig = {
130 #define RADIO_RXBUFS 1
133 static uint8_t stm32w_rxbufs[RADIO_RXBUFS][STM32W_MAX_PACKET_LEN+1];
136 static volatile int8_t first = -1, last=0;
138 static const int8_t first=0, last=0;
142 #define CLEAN_RXBUFS() do{first = -1; last = 0;}while(0)
143 #define RXBUFS_EMPTY() (first == -1)
147 int8_t first_tmp = first;
148 return first_tmp == last;
152 #define CLEAN_RXBUFS() (stm32w_rxbufs[0][0] = 0)
153 #define RXBUFS_EMPTY() (stm32w_rxbufs[0][0] == 0)
154 #define RXBUFS_FULL() (stm32w_rxbufs[0][0] != 0)
157 static uint8_t
__attribute__(( aligned(2) )) stm32w_txbuf[STM32W_MAX_PACKET_LEN+1];
160 #define CLEAN_TXBUF() (stm32w_txbuf[0] = 0)
161 #define TXBUF_EMPTY() (stm32w_txbuf[0] == 0)
163 #define CHECKSUM_LEN 2
171 static volatile uint8_t onoroff = OFF;
172 static uint8_t receiving_packet = 0;
174 static volatile StStatus last_tx_status;
177 PROCESS(stm32w_radio_process,
"STM32W radio driver");
180 static int stm32w_radio_init(
void);
181 static int stm32w_radio_prepare(
const void *payload,
unsigned short payload_len);
182 static int stm32w_radio_transmit(
unsigned short payload_len);
183 static int stm32w_radio_send(
const void *data,
unsigned short len);
184 static int stm32w_radio_read(
void *buf,
unsigned short bufsize);
185 static int stm32w_radio_channel_clear(
void);
186 static int stm32w_radio_receiving_packet(
void);
187 static int stm32w_radio_pending_packet(
void);
188 static int stm32w_radio_on(
void);
189 static int stm32w_radio_off(
void);
191 static int add_to_rxbuf(uint8_t * src);
192 static int read_from_rxbuf(
void * dest,
unsigned short len);
198 stm32w_radio_prepare,
199 stm32w_radio_transmit,
202 stm32w_radio_channel_clear,
203 stm32w_radio_receiving_packet,
204 stm32w_radio_pending_packet,
209 static int stm32w_radio_init(
void)
213 ST_RadioSetChannel(RF_CHANNEL);
217 ST_RadioInit(ST_RADIO_POWER_MODE_OFF);
219 ST_RadioSetNodeId(STM32W_NODE_ID);
220 ST_RadioSetPanId(IEEE802154_PANID);
230 int stm32w_radio_set_channel(u8_t channel)
232 if (ST_RadioSetChannel(channel) == ST_SUCCESS)
238 static int wait_for_tx(
void){
243 while(!TXBUF_EMPTY()){
245 PRINTF(
"stm32w: tx buffer full.\r\n");
254 static int stm32w_radio_prepare(
const void *payload,
unsigned short payload_len)
256 if(payload_len > STM32W_MAX_PACKET_LEN){
257 PRINTF(
"stm32w: payload length=%d is too long.\r\n", payload_len);
261 #if !RADIO_WAIT_FOR_PACKET_SENT
267 PRINTF(
"stm32w: tx buffer full.\r\n");
276 memcpy(stm32w_txbuf + 1, payload, payload_len);
282 static int stm32w_radio_transmit(
unsigned short payload_len)
284 stm32w_txbuf[0] = payload_len + CHECKSUM_LEN;
289 PRINTF(
"stm32w: Radio is off, turning it on.\r\n");
291 ENERGEST_ON(ENERGEST_TYPE_LISTEN);
295 if(ST_RadioTransmit(stm32w_txbuf)==ST_SUCCESS){
297 ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
298 ENERGEST_ON(ENERGEST_TYPE_TRANSMIT);
300 PRINTF(
"stm32w: sending %d bytes\r\n", payload_len);
303 for(u8_t c=1; c <= stm32w_txbuf[0]-2; c++){
304 PRINTF(
"%x:",stm32w_txbuf[c]);
309 #if RADIO_WAIT_FOR_PACKET_SENT
312 PRINTF(
"stm32w: unknown tx error.\r\n");
319 if(last_tx_status == ST_SUCCESS || last_tx_status == ST_PHY_ACK_RECEIVED){
337 PRINTF(
"stm32w: transmission never started.\r\n");
346 static int stm32w_radio_send(
const void *payload,
unsigned short payload_len)
348 if(stm32w_radio_prepare(payload, payload_len) == RADIO_TX_ERR)
351 return stm32w_radio_transmit(payload_len);
355 static int stm32w_radio_channel_clear(
void)
357 return ST_RadioChannelIsClear();
360 static int stm32w_radio_receiving_packet(
void)
362 return receiving_packet;
365 static int stm32w_radio_pending_packet(
void)
367 return !RXBUFS_EMPTY();
370 static int stm32w_radio_off(
void)
381 ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
387 static int stm32w_radio_on(
void)
393 ENERGEST_ON(ENERGEST_TYPE_LISTEN);
399 int stm32w_radio_is_on(
void)
401 return onoroff == ON;
407 boolean ackFramePendingSet,
413 receiving_packet = 0;
415 if(add_to_rxbuf(packet)){
425 boolean framePending)
428 ENERGEST_OFF(ENERGEST_TYPE_TRANSMIT);
429 ENERGEST_ON(ENERGEST_TYPE_LISTEN);
432 last_tx_status = status;
434 if(status == ST_SUCCESS || status == ST_PHY_ACK_RECEIVED){
442 if(ST_RadioTransmit(stm32w_txbuf)==ST_SUCCESS){
444 ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
445 ENERGEST_ON(ENERGEST_TYPE_TRANSMIT);
447 PRINTF(
"stm32w: retransmission.\r\n");
454 PRINTF(
"stm32w: retransmission failed.\r\n");
463 if(status == ST_SUCCESS || status == ST_PHY_ACK_RECEIVED){
466 else if (status == ST_MAC_NO_ACK_RECEIVED){
467 PRINTF(
"TX_END_NOACK!!!");
469 else if (status == ST_PHY_TX_CCA_FAIL){
470 PRINTF(
"TX_END_CCA!!!");
472 else if(status == ST_PHY_TX_UNDERFLOW){
473 PRINTF(
"TX_END_UFL!!!");
476 PRINTF(
"TX_END_INCOMPL!!!");
482 receiving_packet = 1;
487 receiving_packet = 1;
497 PRINTF(
"stm32w_radio_process: started\r\n");
503 PRINTF(
"stm32w_radio_process: calling receiver callback\r\n");
506 for(u8_t c=1; c <= RCVD_PACKET_LEN; c++){
507 PRINTF(
"%x",stm32w_rxbuf[c]);
516 NETSTACK_RDC.input();
528 static int stm32w_radio_read(
void *buf,
unsigned short bufsize)
530 return read_from_rxbuf(buf,bufsize);
536 PRINTF(
"OVERFLOW\r\n");
547 static int add_to_rxbuf(uint8_t * src)
553 memcpy(stm32w_rxbufs[last], src, src[0] + 1);
555 last = (last + 1) % RADIO_RXBUFS;
564 static int read_from_rxbuf(
void * dest,
unsigned short len)
571 if(stm32w_rxbufs[first][0] > len){
575 len = stm32w_rxbufs[first][0];
576 memcpy(dest,stm32w_rxbufs[first]+1,len);
577 packetbuf_set_attr(PACKETBUF_ATTR_RSSI, last_rssi);
582 first = (first + 1) % RADIO_RXBUFS;
583 int first_tmp = first;
584 if(first_tmp == last){
595 short last_packet_rssi(){