Merge branch 'retry'
This commit is contained in:
@@ -23,6 +23,8 @@ typedef struct {
|
|||||||
|
|
||||||
extern MQueue usb_inq;
|
extern MQueue usb_inq;
|
||||||
|
|
||||||
|
void mq_reset(MQueue *mq);
|
||||||
|
|
||||||
void mq_init(MQueue *mq);
|
void mq_init(MQueue *mq);
|
||||||
|
|
||||||
bool mq_can_post(MQueue *mq);
|
bool mq_can_post(MQueue *mq);
|
||||||
|
|||||||
@@ -79,6 +79,16 @@ uint8_t NRF_IsModeTX(void);
|
|||||||
*/
|
*/
|
||||||
uint8_t NRF_IsModeRx(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
|
* Add a pipe to the next free slot
|
||||||
*
|
*
|
||||||
|
|||||||
+34
-1
@@ -71,6 +71,8 @@ void start_slave_cmd(uint8_t slave_addr, uint16_t frame_len, uint8_t cksum)
|
|||||||
txmsg_cksum = cksum;
|
txmsg_cksum = cksum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define MAX_RETRY 20
|
||||||
|
|
||||||
void handle_txframe_chunk(const uint8_t *buffer, uint16_t size)
|
void handle_txframe_chunk(const uint8_t *buffer, uint16_t size)
|
||||||
{
|
{
|
||||||
uint32_t wanted = MIN(txmsg_len - txmsg_collected, 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;
|
uint32_t remain = txmsg_len;
|
||||||
for (int i = 0; i <= txmsg_len/32; i++) {
|
for (int i = 0; i <= txmsg_len/32; i++) {
|
||||||
uint8_t chunk = (uint8_t) MIN(remain, 32);
|
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;
|
remain -= chunk;
|
||||||
|
|
||||||
if (!suc) {
|
if (!suc) {
|
||||||
dbg("Sending failed, discard rest");
|
dbg("Sending failed, discard rest");
|
||||||
|
|
||||||
|
NRF_PowerDown();
|
||||||
|
NRF_FlushRx();
|
||||||
|
NRF_FlushTx();
|
||||||
|
NRF_ModeRX();
|
||||||
break; // skip rest of the frame
|
break; // skip rest of the frame
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -179,7 +204,15 @@ void gw_handle_usb_out(uint8_t *buffer)
|
|||||||
|
|
||||||
void handle_cmd_reset(void)
|
void handle_cmd_reset(void)
|
||||||
{
|
{
|
||||||
|
// mq_reset(&usb_inq);
|
||||||
|
// TODO this may be causing problems??
|
||||||
|
|
||||||
NRF_ResetPipes();
|
NRF_ResetPipes();
|
||||||
|
|
||||||
|
NRF_PowerDown();
|
||||||
|
NRF_FlushRx();
|
||||||
|
NRF_FlushTx();
|
||||||
|
NRF_ModeRX();
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_cmd_addnodes(PayloadParser *pp)
|
void handle_cmd_addnodes(PayloadParser *pp)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <msg_queue.h>
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "msg_queue.h"
|
#include "msg_queue.h"
|
||||||
|
|
||||||
@@ -13,6 +14,7 @@ MQueue usb_inq;
|
|||||||
|
|
||||||
void mq_init(MQueue *mq)
|
void mq_init(MQueue *mq)
|
||||||
{
|
{
|
||||||
|
mq->nw = 0;
|
||||||
mq->lr = MQ_LEN-1;
|
mq->lr = MQ_LEN-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,6 +23,12 @@ bool mq_can_post(MQueue *mq)
|
|||||||
return mq->nw != mq->lr;
|
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)
|
bool mq_post(MQueue *mq, const uint8_t *buffer, uint32_t len)
|
||||||
{
|
{
|
||||||
if (!mq_can_post(mq)) {
|
if (!mq_can_post(mq)) {
|
||||||
|
|||||||
@@ -387,9 +387,7 @@ uint8_t NRF_ReceivePacket(uint8_t *Packet, uint8_t *PipeNum)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (pw > 32) {
|
if (pw > 32) {
|
||||||
CHIPSELECT {
|
NRF_FlushRx();
|
||||||
spi(CMD_FLUSH_RX);
|
|
||||||
}
|
|
||||||
pw = 0;
|
pw = 0;
|
||||||
} else {
|
} else {
|
||||||
// Read the reception pipe number
|
// 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_ModeTX(); // Make sure in TX mode
|
||||||
NRF_SetTxAddress(nrf_pipe_addr[PipeNum]); // this sets the Tx addr and also pipe 0 addr for ACK
|
NRF_SetTxAddress(nrf_pipe_addr[PipeNum]); // this sets the Tx addr and also pipe 0 addr for ACK
|
||||||
|
|
||||||
CHIPSELECT {
|
NRF_FlushTx();
|
||||||
spi(CMD_FLUSH_TX);
|
|
||||||
};
|
|
||||||
|
|
||||||
CHIPSELECT {
|
CHIPSELECT {
|
||||||
spi(CMD_WR_TX_PLD);
|
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)
|
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++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
nrf_pipe_addr[i] = 0;
|
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)
|
void NRF_Init(uint8_t pSpeed)
|
||||||
{
|
{
|
||||||
// Set the required output pins
|
// Set the required output pins
|
||||||
@@ -491,17 +501,17 @@ void NRF_Init(uint8_t pSpeed)
|
|||||||
|
|
||||||
// clear flags etc
|
// clear flags etc
|
||||||
NRF_PowerDown();
|
NRF_PowerDown();
|
||||||
CHIPSELECT { spi(CMD_FLUSH_RX); }
|
NRF_FlushRx();
|
||||||
CHIPSELECT { spi(CMD_FLUSH_TX); }
|
NRF_FlushTx();
|
||||||
NRF_WriteRegister(RG_STATUS, 0x70);
|
NRF_WriteRegister(RG_STATUS, 0x70);
|
||||||
|
|
||||||
NRF_WriteRegister(RG_CONFIG, ModeBits);
|
NRF_WriteRegister(RG_CONFIG, ModeBits);
|
||||||
NRF_WriteRegister(RG_SETUP_AW, 0b11); // 5 byte addresses
|
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_SETUP_RETR, 0x18); // 8 retries, 500 ms
|
||||||
NRF_WriteRegister(RG_RF_CH, 7); // channel 2 NO HIGHER THAN 83 in USA!
|
NRF_WriteRegister(RG_RF_CH, 76); // channel
|
||||||
|
|
||||||
NRF_WriteRegister(RG_RF_SETUP, pSpeed);
|
NRF_WriteRegister(RG_RF_SETUP, pSpeed);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user