diff --git a/Inc/msg_queue.h b/Inc/msg_queue.h index 3790d97..ff0d96a 100644 --- a/Inc/msg_queue.h +++ b/Inc/msg_queue.h @@ -23,6 +23,8 @@ typedef struct { extern MQueue usb_inq; +void mq_reset(MQueue *mq); + void mq_init(MQueue *mq); bool mq_can_post(MQueue *mq); diff --git a/Inc/nrf.h b/Inc/nrf.h index 527b76a..27c5b54 100644 --- a/Inc/nrf.h +++ b/Inc/nrf.h @@ -79,6 +79,16 @@ uint8_t NRF_IsModeTX(void); */ uint8_t NRF_IsModeRx(void); +/** + * Flush Rx fifo + */ +void NRF_FlushRx(void); + +/** + * Flush Tx fifo + */ +void NRF_FlushTx(void); + /** * Add a pipe to the next free slot * diff --git a/Src/gex_gateway.c b/Src/gex_gateway.c index 229a982..848019d 100644 --- a/Src/gex_gateway.c +++ b/Src/gex_gateway.c @@ -71,6 +71,8 @@ void start_slave_cmd(uint8_t slave_addr, uint16_t frame_len, uint8_t cksum) txmsg_cksum = cksum; } +#define MAX_RETRY 20 + void handle_txframe_chunk(const uint8_t *buffer, uint16_t size) { uint32_t wanted = MIN(txmsg_len - txmsg_collected, size); @@ -98,11 +100,34 @@ void handle_txframe_chunk(const uint8_t *buffer, uint16_t size) uint32_t remain = txmsg_len; for (int i = 0; i <= txmsg_len/32; i++) { uint8_t chunk = (uint8_t) MIN(remain, 32); - bool suc = NRF_SendPacket(pipe, &txmsg_payload[i*32], chunk); + + bool suc = false; + uint16_t delay = 1; + + // repeated tx attempts + for (int j = 0; j < MAX_RETRY; j++) { + suc = NRF_SendPacket(pipe, &txmsg_payload[i*32], chunk); // use the pipe to retrieve the address + if (!suc) { + dbg_nrf("Retry"); + LL_mDelay(delay); + delay += 2; // longer delay next time + } else { + break; + } + } // TODO it sometimes gets through after a retry but the communication stalls?? + if(delay != 1 && suc) { + dbg_nrf(" Succ after retry"); + } + remain -= chunk; if (!suc) { dbg("Sending failed, discard rest"); + + NRF_PowerDown(); + NRF_FlushRx(); + NRF_FlushTx(); + NRF_ModeRX(); break; // skip rest of the frame } } @@ -179,7 +204,15 @@ void gw_handle_usb_out(uint8_t *buffer) void handle_cmd_reset(void) { +// mq_reset(&usb_inq); + // TODO this may be causing problems?? + NRF_ResetPipes(); + + NRF_PowerDown(); + NRF_FlushRx(); + NRF_FlushTx(); + NRF_ModeRX(); } void handle_cmd_addnodes(PayloadParser *pp) diff --git a/Src/msg_queue.c b/Src/msg_queue.c index dc839e1..26d6696 100644 --- a/Src/msg_queue.c +++ b/Src/msg_queue.c @@ -5,6 +5,7 @@ #include "main.h" #include #include +#include #include "debug.h" #include "msg_queue.h" @@ -13,6 +14,7 @@ MQueue usb_inq; void mq_init(MQueue *mq) { + mq->nw = 0; mq->lr = MQ_LEN-1; } @@ -21,6 +23,12 @@ bool mq_can_post(MQueue *mq) return mq->nw != mq->lr; } +void mq_reset(MQueue *mq) +{ + mq_init(mq); + memset(mq->slots, 0, sizeof(mq->slots)); +} + bool mq_post(MQueue *mq, const uint8_t *buffer, uint32_t len) { if (!mq_can_post(mq)) { diff --git a/Src/nrf.c b/Src/nrf.c index 73c86fd..8f7c0cb 100644 --- a/Src/nrf.c +++ b/Src/nrf.c @@ -387,9 +387,7 @@ uint8_t NRF_ReceivePacket(uint8_t *Packet, uint8_t *PipeNum) } if (pw > 32) { - CHIPSELECT { - spi(CMD_FLUSH_RX); - } + NRF_FlushRx(); pw = 0; } else { // Read the reception pipe number @@ -430,9 +428,7 @@ bool NRF_SendPacket(uint8_t PipeNum, const uint8_t *Packet, uint8_t Length) NRF_ModeTX(); // Make sure in TX mode NRF_SetTxAddress(nrf_pipe_addr[PipeNum]); // this sets the Tx addr and also pipe 0 addr for ACK - CHIPSELECT { - spi(CMD_FLUSH_TX); - }; + NRF_FlushTx(); CHIPSELECT { spi(CMD_WR_TX_PLD); @@ -468,7 +464,7 @@ bool NRF_SendPacket(uint8_t PipeNum, const uint8_t *Packet, uint8_t Length) void NRF_ResetPipes(void) { - NRF_WriteRegister(RG_EN_RXADDR, 0); // disable all pipes + NRF_WriteRegister(RG_EN_RXADDR, 1); // disable all pipes for (int i = 0; i < 6; i++) { nrf_pipe_addr[i] = 0; @@ -476,6 +472,20 @@ void NRF_ResetPipes(void) } } +void NRF_FlushRx(void) +{ + CHIPSELECT { + spi(CMD_FLUSH_RX); + } +} + +void NRF_FlushTx(void) +{ + CHIPSELECT { + spi(CMD_FLUSH_TX); + } +} + void NRF_Init(uint8_t pSpeed) { // Set the required output pins @@ -491,14 +501,14 @@ void NRF_Init(uint8_t pSpeed) // clear flags etc NRF_PowerDown(); - CHIPSELECT { spi(CMD_FLUSH_RX); } - CHIPSELECT { spi(CMD_FLUSH_TX); } + NRF_FlushRx(); + NRF_FlushTx(); NRF_WriteRegister(RG_STATUS, 0x70); NRF_WriteRegister(RG_CONFIG, ModeBits); NRF_WriteRegister(RG_SETUP_AW, 0b11); // 5 byte addresses - NRF_WriteRegister(RG_EN_RXADDR, 0x00); // disable all + NRF_WriteRegister(RG_EN_RXADDR, 0x01); // disable all except 1 which we'll assign later NRF_WriteRegister(RG_SETUP_RETR, 0x18); // 8 retries, 500 ms NRF_WriteRegister(RG_RF_CH, 7); // channel 2 NO HIGHER THAN 83 in USA!