nrf logging

retry
Ondřej Hruška 7 years ago
parent 62c6665114
commit 5f563e9038
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 4
      Inc/nrf.h
  2. 4
      Inc/usbd_cdc_if.h
  3. 4
      Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc.c
  4. 183
      Src/gex_gateway.c
  5. 4
      Src/gpio.c
  6. 10
      Src/main.c
  7. 21
      Src/nrf.c
  8. 9
      Src/usbd_cdc_if.c

@ -102,9 +102,9 @@ uint8_t NRF_PipeNum2Addr(uint8_t pipe_num);
uint8_t NRF_Addr2PipeNum(uint8_t addr);
/**
* Reset as much as possible (incl. removing pipes)
* Reset pipes, so we can add them again
*/
void NRF_Reset(void);
void NRF_ResetPipes(void);
/**
* Send a packet (takes care of mode switching etc)

@ -118,6 +118,8 @@
/** CDC Interface callback. */
extern USBD_CDC_ItfTypeDef USBD_Interface_fops_FS;
extern volatile bool usb_tx_busy;
/* USER CODE BEGIN EXPORTED_VARIABLES */
/* USER CODE END EXPORTED_VARIABLES */
@ -133,6 +135,8 @@ extern USBD_CDC_ItfTypeDef USBD_Interface_fops_FS;
uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len);
void USBD_CDC_TransmitDone(USBD_HandleTypeDef *pdev);
/* USER CODE BEGIN EXPORTED_FUNCTIONS */
/* USER CODE END EXPORTED_FUNCTIONS */

@ -654,6 +654,8 @@ static uint8_t USBD_CDC_Setup (USBD_HandleTypeDef *pdev,
return USBD_OK;
}
extern void USBD_CDC_TransmitDone(USBD_HandleTypeDef *pdev);
/**
* @brief USBD_CDC_DataIn
* Data sent on non-control IN endpoint
@ -670,6 +672,8 @@ static uint8_t USBD_CDC_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum)
hcdc->TxState = 0;
USBD_CDC_TransmitDone(pdev);
return USBD_OK;
}
else

@ -13,6 +13,23 @@
#include "crc32.h"
#include "payload_builder.h"
enum MsgTypes {
MSG_TYPE_NETWORK_ID = 1,
MSG_TYPE_DATA = 2,
};
struct msg_network_id {
uint8_t msg_type;
uint8_t bytes[4];
};
struct msg_data {
uint8_t msg_type;
uint8_t dev_addr;
uint8_t length;
uint8_t data[32];
};
static uint8_t gex_network[4];
// USB RX
@ -36,13 +53,13 @@ static uint8_t txmsg_cksum = 0;
enum GW_CMD {
CMD_GET_ID = 'i', // 105 - get network ID
CMD_RESET = 'r', // reset the radio and network
CMD_ADD_NODE = 'n', // add a node by address byte
CMD_ADD_NODES = 'n', // add a node by address byte
CMD_TXMSG = 'm', // 109 - send a message
};
void respond_gw_id(void);
void handle_cmd_addnode(PayloadParser *pp);
void handle_cmd_addnodes(PayloadParser *pp);
void handle_cmd_reset(void);
@ -54,10 +71,52 @@ void start_slave_cmd(uint8_t slave_addr, uint16_t frame_len, uint8_t cksum)
txmsg_cksum = cksum;
}
void handle_txframe_chunk(const uint8_t *buffer, uint16_t size)
{
uint32_t wanted = MIN(txmsg_len - txmsg_collected, size);
memcpy(&txmsg_payload[txmsg_collected], buffer, wanted);
txmsg_collected += wanted;
if (wanted < size) {
// this was the end - simple checksum to verify it's a valid frame
uint8_t ck = 0;
for (int i = 0; i < txmsg_len; i++) {
ck ^= txmsg_payload[i];
}
ck = ~ck;
if (ck != txmsg_cksum) {
dbg("Checksum mismatch!");
}
else {
dbg("Verified, sending a %d B frame to slave.", (int) txmsg_len);
uint8_t pipe = NRF_Addr2PipeNum(txmsg_addr);
if (pipe == 0xFF) {
dbg("Bad slave num!");
} else {
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);
remain -= chunk;
if (!suc) {
dbg("Sending failed."); // (even with retransmission)
break; // skip rest of the frame
}
}
}
}
cmd_state = CMD_STATE_IDLE;
}
}
void gw_handle_usb_out(uint8_t *buffer)
{
if (cmd_state == CMD_STATE_IDLE) {
PayloadParser pp = pp_start(buffer, MQ_SLOT_LEN, NULL);
PayloadParser pp = pp_start(buffer, 64, NULL);
// handle binary commands for the gateway
@ -75,11 +134,14 @@ void gw_handle_usb_out(uint8_t *buffer)
handle_cmd_reset();
break;
case CMD_ADD_NODE:
handle_cmd_addnode(&pp);
case CMD_ADD_NODES:
// payload is: u8-count, u8[] node addresses
handle_cmd_addnodes(&pp);
break;
case CMD_TXMSG:;
// u8-slave-addr, u16-len, u8-checksum
// the message is sent in the following frames.
uint8_t slave_addr = pp_u8(&pp);
uint16_t frame_len = pp_u16(&pp);
uint8_t cksum = pp_u8(&pp);
@ -92,6 +154,11 @@ void gw_handle_usb_out(uint8_t *buffer)
start_slave_cmd(slave_addr, frame_len, cksum);
dbg("Collecting frame for slave %02x: %d bytes", (int)slave_addr, (int)frame_len);
cmd_state = CMD_STATE_TXMSG;
// handle the rest as payload
uint32_t len;
const uint8_t *tail = pp_tail(&pp, &len);
handle_txframe_chunk(tail, (uint16_t) len);
break;
default:
@ -103,65 +170,47 @@ void gw_handle_usb_out(uint8_t *buffer)
}
}
else if (cmd_state == CMD_STATE_TXMSG) {
uint32_t wanted = MIN(txmsg_len - txmsg_collected, MQ_SLOT_LEN);
memcpy(&txmsg_payload[txmsg_collected], buffer, wanted);
txmsg_collected += wanted;
if (wanted < MQ_SLOT_LEN) {
// this was the end - simple checksum to verify it's a valid frame
uint8_t ck = 0;
for (int i = 0; i < txmsg_len; i++) {
ck ^= txmsg_payload[i];
}
ck = ~ck;
if (ck != txmsg_cksum) {
dbg("Checksum mismatch!");
}
else {
dbg("Verified, sending a %d B frame to slave.", (int) txmsg_len);
uint8_t pipe = NRF_Addr2PipeNum(txmsg_addr);
if (pipe == 0xFF) {
dbg("Bad slave num!");
} else {
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);
remain -= chunk;
if (!suc) {
dbg("Sending failed."); // (even with retransmission)
break; // skip rest of the frame
}
}
}
}
cmd_state = CMD_STATE_IDLE;
}
handle_txframe_chunk(buffer, 64);
}
}
void handle_cmd_reset(void)
{
NRF_Reset();
// TODO also clear queues?
NRF_ResetPipes();
}
void handle_cmd_addnode(PayloadParser *pp)
void handle_cmd_addnodes(PayloadParser *pp)
{
uint8_t node = pp_u8(pp);
uint8_t pipenum;
bool suc = NRF_AddPipe(node, &pipenum);
if (!suc) dbg("Failed to add node.");
// TODO response
uint8_t count = pp_u8(pp);
for(int i = 0; i < count; i++) {
uint8_t node = pp_u8(pp);
uint8_t pipenum;
bool suc = NRF_AddPipe(node, &pipenum);
if (!suc) {
dbg("Failed to add node.");
} else {
dbg("Bound node %02x to pipe %d", node, pipenum);
}
}
}
void respond_gw_id(void)
{
// TODO implement (after response system is added)
dbg("> respond_gw_id");
struct msg_network_id m = {
.msg_type = MSG_TYPE_NETWORK_ID,
.bytes = {
gex_network[0],
gex_network[1],
gex_network[2],
gex_network[3],
}
};
bool suc = mq_post(&usb_inq, (uint8_t *) &m, sizeof(m));
if (!suc) {
dbg("IN que overflow!!");
}
}
/**
@ -206,8 +255,32 @@ void gw_setup_radio(void)
NRF_ModeRX(); // base state is RX
dbg("Send a packet");
// dbg("Send a packet");
//
// suc = NRF_SendPacket(pipenum, (uint8_t *) "AHOJ", 5);
// dbg("Suc? %d", (int)suc);
}
void EXTI2_IRQHandler(void)
{
struct msg_data m;
m.msg_type = MSG_TYPE_DATA;
LL_EXTI_ClearFlag_0_31(LL_EXTI_LINE_2);
suc = NRF_SendPacket(pipenum, (uint8_t *) "AHOJ", 5);
dbg("Suc? %d", (int)suc);
uint8_t pipenum;
m.length = NRF_ReceivePacket(m.data, &pipenum);
if (m.length == 0) {
dbg("IRQ but no msg!");
}
else {
dbg("Msg RXd from nordic!");
m.dev_addr = NRF_PipeNum2Addr(pipenum);
bool suc = mq_post(&usb_inq, (uint8_t *) &m, sizeof(m));
if (!suc) {
dbg("IN que overflow!!");
}
}
}

@ -98,7 +98,7 @@ void MX_GPIO_Init(void)
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
#if 0
#if 1
/**/
LL_GPIO_AF_SetEXTISource(LL_GPIO_AF_EXTI_PORTA, LL_GPIO_AF_EXTI_LINE2);
@ -109,6 +109,8 @@ void MX_GPIO_Init(void)
EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_RISING;
LL_EXTI_Init(&EXTI_InitStruct);
NVIC_EnableIRQ(EXTI2_IRQn);
/**/
LL_GPIO_SetPinMode(NRF_IRQ_GPIO_Port, NRF_IRQ_Pin, LL_GPIO_MODE_FLOATING);
#endif

@ -57,6 +57,7 @@
#include "debug.h"
#include "gex_gateway.h"
#include "msg_queue.h"
#include "usbd_cdc_if.h"
/* USER CODE BEGIN Includes */
@ -130,11 +131,16 @@ int main(void)
/* USER CODE BEGIN WHILE */
int cnt = 0;
while (1) {
if (cnt++ > 4000000) {
if (cnt++ > 500000) {
LL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
// GPIOC->ODR ^= (1 << 13);
cnt = 0;
}
uint8_t buff[MQ_SLOT_LEN];
if (!usb_tx_busy && mq_can_read(&usb_inq)) {
mq_read(&usb_inq, buff);
CDC_Transmit_FS(buff, MQ_SLOT_LEN);
}
}
/* USER CODE END 3 */

@ -169,6 +169,7 @@ static uint8_t NRF_WriteRegister(uint8_t reg, uint8_t value)
status = spi(CMD_WRITE_REG | reg);
spi(value);
}
dbg("Wr[0x%02x] := 0x%02x", (int)reg, (int) value);
return status;
}
@ -179,6 +180,7 @@ static uint8_t NRF_ReadRegister(uint8_t reg)
spi(CMD_READ_REG | reg);
reg_val = spi(0);
}
dbg("Rd[0x%02x] = 0x%02x", (int)reg, (int) reg_val);
return reg_val;
}
@ -232,10 +234,13 @@ void NRF_SetRxAddress(uint8_t pipenum, uint8_t AddrByte)
nrf_pipe_addr[pipenum] = AddrByte;
dbg("Set Rx addr (pipe %d) = 0x%02x", (int)pipenum, AddrByte);
if (pipenum == 0) {
dbg("W ADDR_PA0: %02X-%02X-%02X-%02X-%02X", nrf_base_address[0], nrf_base_address[1], nrf_base_address[2], nrf_base_address[3], nrf_base_address[4]);
NRF_WriteBuffer(RG_RX_ADDR_P0, nrf_base_address, 5);
}
else if (pipenum == 1) {
dbg("W ADDR_PA1: %02X-%02X-%02X-%02X-%02X", nrf_base_address[0], nrf_base_address[1], nrf_base_address[2], nrf_base_address[3], nrf_base_address[4]);
NRF_WriteBuffer(RG_RX_ADDR_P1, nrf_base_address, 5);
}
else {
@ -292,18 +297,23 @@ void NRF_DisablePipe(uint8_t pipenum)
static void NRF_SetTxAddress(uint8_t SendTo)
{
nrf_base_address[4] = SendTo;
dbg("W Tx_ADDR + Rx0: %02X-%02X-%02X-%02X-%02X", nrf_base_address[0], nrf_base_address[1], nrf_base_address[2], nrf_base_address[3], nrf_base_address[4]);
NRF_WriteBuffer(RG_TX_ADDR, nrf_base_address, 5);
NRF_WriteBuffer(RG_RX_ADDR_P0, nrf_base_address, 5); // the ACK will come to pipe 0
}
void NRF_PowerDown(void)
{
dbg("PDn");
CELOW;
NRF_WriteRegister(RG_CONFIG, ModeBits);
}
void NRF_ModeTX(void)
{
dbg("Tx Mode");
CELOW;
uint8_t m = NRF_ReadRegister(RG_CONFIG);
NRF_WriteRegister(RG_CONFIG, ModeBits | RD_CONFIG_PWR_UP);
@ -312,6 +322,7 @@ void NRF_ModeTX(void)
void NRF_ModeRX(void)
{
dbg("Rx Mode");
NRF_WriteRegister(RG_CONFIG, ModeBits | RD_CONFIG_PWR_UP | RD_CONFIG_PRIM_RX);
NRF_SetRxAddress(0, nrf_pipe_addr[0]); // set the P0 address - it was changed during Rx for ACK reception
CEHIGH;
@ -414,24 +425,24 @@ bool NRF_SendPacket(uint8_t PipeNum, const uint8_t *Packet, uint8_t Length)
st = NRF_ReadStatus(); // Packet acked or timed out
}
dbg("Send status: MAX_RT %d, SENT %d", (st&RD_STATUS_MAX_RT) != 0, (st&RD_STATUS_TX_DS) != 0);
NRF_WriteRegister(RG_STATUS, st & (RD_STATUS_MAX_RT|RD_STATUS_TX_DS)); // Clear the bit
if ((orig_conf & RD_CONFIG_PWR_UP) == 0) {
dbg("going back PwrDn");
NRF_PowerDown();
}
else if ((orig_conf & RD_CONFIG_PRIM_RX) == RD_CONFIG_PRIM_RX) {
dbg("going back PwrUp+Rx");
NRF_ModeRX();
}
return 0 != (st & RD_STATUS_TX_DS); // success
}
void NRF_Reset(void)
void NRF_ResetPipes(void)
{
NSS(1);
CELOW;
NRF_PowerDown();
NRF_WriteRegister(RG_EN_RXADDR, 0); // disable all pipes
for (int i = 0; i < 6; i++) {

@ -117,6 +117,8 @@
* @}
*/
volatile bool usb_tx_busy = false;
/** @defgroup USBD_CDC_IF_Private_Variables USBD_CDC_IF_Private_Variables
* @brief Private variables.
* @{
@ -331,6 +333,8 @@ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
if (hcdc->TxState != 0){
return USBD_BUSY;
}
usb_tx_busy = true;
USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
/* USER CODE END 7 */
@ -341,7 +345,10 @@ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
/* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
// TODO add Tx Done notify
void USBD_CDC_TransmitDone(USBD_HandleTypeDef *pdev)
{
usb_tx_busy = false;
}
/**
* @}

Loading…
Cancel
Save