diff --git a/Examples/LL/SPI/nRF24L01_Wireless/main.c b/Examples/LL/SPI/nRF24L01_Wireless/main.c index 3a01d47..c683bfc 100644 --- a/Examples/LL/SPI/nRF24L01_Wireless/main.c +++ b/Examples/LL/SPI/nRF24L01_Wireless/main.c @@ -17,13 +17,15 @@ #include "py32f0xx_bsp_printf.h" #include "nrf24l01.h" +/* MODE_TX, MODE_RX, MODE_RX_INT */ +#define MODE_TX 0 +#define MODE_RX 2 +#define MODE_RX_INT 3 +#define NRF24_MODE MODE_TX + uint8_t RX_ADDRESS[NRF24L01_ADDR_WIDTH] = {0x32,0x4E,0x6F,0x64,0x65}; uint8_t TX_ADDRESS[NRF24L01_ADDR_WIDTH] = {0x32,0x4E,0x6F,0x64,0x22}; -/*-----------------------------------------------------*/ -/* Mode: sending:1 or receiving:0 */ -uint8_t Mode = 1; -/*-----------------------------------------------------*/ extern uint8_t RX_BUF[]; extern uint8_t TX_BUF[]; @@ -50,42 +52,58 @@ int main(void) while (NRF24L01_Check() != 0) { - printf("nRF24L01 check failed\r\n"); + printf("nRF24L01 check: error\r\n"); LL_mDelay(2000); } - printf("nRF24L01 check succeeded\r\n"); + printf("nRF24L01 check: succ\r\n"); - if (Mode == 1) +#if (NRF24_MODE == MODE_RX) + printf("nRF24L01 in RX polling mode\r\n"); + NRF24L01_RX_Mode(TX_ADDRESS, RX_ADDRESS); + NRF24L01_DumpConfig(); + while(1) { - printf("nRF24L01 in SEND mode\r\n"); - NRF24L01_TX_Mode(RX_ADDRESS, TX_ADDRESS); - } - else if (Mode == 0) - { - printf("nRF24L01 in RECEIVE mode\r\n"); - NRF24L01_RX_Mode(TX_ADDRESS, RX_ADDRESS); + NRF24L01_RxPacket(RX_BUF); } - while (1) - { - NRF24L01_DumpConfig(); +#elif (NRF24_MODE == MODE_RX_INT) + printf("nRF24L01 in RX interrupt mode\r\n"); + NRF24L01_RX_Mode(TX_ADDRESS, RX_ADDRESS); + NRF24L01_ClearIRQFlags(); + NRF24L01_DumpConfig(); - if (Mode == 1) - { - uint8_t tmp[] = {0x1f, - 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, - 0x21, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x28, - 0x31, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x38, - 0x41, 0x12, 0x13, 0x14, 0x15, 0x16, 0x47, 0x48}; - NRF24L01_TxPacket(tmp, 32); - LL_mDelay(1000); - } - else if (Mode == 0) - { - NRF24L01_RxPacket(RX_BUF); - } + + while(1); + +#elif (NRF24_MODE == MODE_TX) + uint8_t tmp[] = {0x1f, + 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, + 0x21, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x28, + 0x31, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x38, + 0x41, 0x12, 0x13, 0x14, 0x15, 0x16, 0x47, 0x48}; + printf("nRF24L01 in TX mode\r\n"); + NRF24L01_TX_Mode(RX_ADDRESS, TX_ADDRESS); + NRF24L01_DumpConfig(); + + while(1) + { + NRF24L01_TxPacket(tmp, 32); + LL_mDelay(500); + } + +#endif +} + +#if (NRF24_MODE == MODE_RX_INT) +void EXTI4_15_IRQHandler(void) +{ + if(LL_EXTI_IsActiveFlag(LL_EXTI_LINE_4)) + { + NRF24L01_IntRxPacket(RX_BUF); + LL_EXTI_ClearFlag(LL_EXTI_LINE_4); } } +#endif uint8_t SPI_TxRxByte(uint8_t data) { @@ -108,6 +126,10 @@ uint8_t SPI_TxRxByte(uint8_t data) static void APP_GPIOConfig(void) { LL_GPIO_InitTypeDef GPIO_InitStruct; +#if (NRF24_MODE == MODE_RX_INT) + LL_EXTI_InitTypeDef EXTI_InitStruct; +#endif + // PA6 CSN LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_6, LL_GPIO_MODE_OUTPUT); // PA5 CE @@ -117,6 +139,18 @@ static void APP_GPIOConfig(void) GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT; GPIO_InitStruct.Pull = LL_GPIO_PULL_UP; LL_GPIO_Init(GPIOA, &GPIO_InitStruct); + +#if (NRF24_MODE == MODE_RX_INT) + /* Triggerred by falling edge */ + EXTI_InitStruct.Line = LL_EXTI_LINE_4; + EXTI_InitStruct.LineCommand = ENABLE; + EXTI_InitStruct.Mode = LL_EXTI_MODE_IT; + EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_FALLING; + LL_EXTI_Init(&EXTI_InitStruct); + + NVIC_SetPriority(EXTI4_15_IRQn, 1); + NVIC_EnableIRQ(EXTI4_15_IRQn); +#endif } /** diff --git a/Examples/LL/SPI/nRF24L01_Wireless/nrf24l01.c b/Examples/LL/SPI/nRF24L01_Wireless/nrf24l01.c index 4ce40ba..061cf8c 100644 --- a/Examples/LL/SPI/nRF24L01_Wireless/nrf24l01.c +++ b/Examples/LL/SPI/nRF24L01_Wireless/nrf24l01.c @@ -190,6 +190,7 @@ void NRF24L01_RX_Mode(uint8_t *rx_addr, uint8_t *tx_addr) */ NRF24L01_Write_Reg(NRF24L01_CMD_REGISTER_W + NRF24L01_REG_CONFIG, 0x0f); //RX,PWR_UP,CRC16,EN_CRC CE(1); + NRF24L01_FlushRX(); } /** @@ -215,15 +216,17 @@ uint8_t NRF24L01_RxPacket(uint8_t *rx_buf) while(IRQ); CE(0); status = NRF24L01_Read_Reg(NRF24L01_REG_STATUS); - printf("Interrupted, status: %02X\r\n", status); + BSP_UART_TxHex8(status); + BSP_UART_TxChar(':'); if(status & NRF24L01_FLAG_RX_DREADY) { NRF24L01_Read_To_Buf(NRF24L01_CMD_RX_PLOAD_R, rx_buf, NRF24L01_PLOAD_WIDTH); for (int i = 0; i < 32; i++) { - printf("%02X ", RX_BUF[i]); + BSP_UART_TxHex8(RX_BUF[i]); } + BSP_UART_TxString("\r\n"); result = 1; NRF24L01_ClearIRQFlag(NRF24L01_FLAG_RX_DREADY); } @@ -231,6 +234,32 @@ uint8_t NRF24L01_RxPacket(uint8_t *rx_buf) return result; } +/** +* Read in interrupt +*/ +void NRF24L01_IntRxPacket(uint8_t *rx_buf) +{ + uint8_t status; + CE(0); + status = NRF24L01_Read_Reg(NRF24L01_REG_STATUS); + BSP_UART_TxHex8(status); + BSP_UART_TxChar(':'); + + if(status & NRF24L01_FLAG_RX_DREADY) + { + NRF24L01_Read_To_Buf(NRF24L01_CMD_RX_PLOAD_R, rx_buf, NRF24L01_PLOAD_WIDTH); + for (int i = 0; i < 32; i++) + { + BSP_UART_TxHex8(RX_BUF[i]); + } + BSP_UART_TxString("\r\n"); + NRF24L01_ClearIRQFlag(NRF24L01_FLAG_RX_DREADY); + } + status |= NRF24L01_MASK_STATUS_IRQ; + NRF24L01_Write_Reg(NRF24L01_REG_STATUS, status); + CE(1); +} + /** * Send data in tx_buf and wait till data is sent or max re-tr reached */ @@ -245,20 +274,20 @@ uint8_t NRF24L01_TxPacket(uint8_t *tx_buf, uint8_t len) CE(0); status = NRF24L01_Read_Reg(NRF24L01_REG_STATUS); - printf("Interrupted, status: %02X\r\n", status); + BSP_UART_TxHex8(status); + BSP_UART_TxChar(':'); if(status & NRF24L01_FLAG_TX_DSENT) { - printf("Data sent: "); + BSP_UART_TxString("Data sent: "); for (uint8_t i = 0; i < len; i++) { - printf("%02X ", tx_buf[i]); + BSP_UART_TxHex8(tx_buf[i]); } - printf("\r\n"); + BSP_UART_TxString("\r\n"); NRF24L01_ClearIRQFlag(NRF24L01_FLAG_TX_DSENT); - } else if(status & NRF24L01_FLAG_MAX_RT) { - printf("Sending exceeds max retries\r\n"); + BSP_UART_TxString("Sending exceeds max retries\r\n"); NRF24L01_FlushTX(); NRF24L01_ClearIRQFlag(NRF24L01_FLAG_MAX_RT); } diff --git a/Examples/LL/SPI/nRF24L01_Wireless/nrf24l01.h b/Examples/LL/SPI/nRF24L01_Wireless/nrf24l01.h index 3ee2fb4..0093c10 100644 --- a/Examples/LL/SPI/nRF24L01_Wireless/nrf24l01.h +++ b/Examples/LL/SPI/nRF24L01_Wireless/nrf24l01.h @@ -124,6 +124,7 @@ uint8_t NRF24L01_Write_From_Buf(uint8_t reg,uint8_t *pBuf,uint8_t len); * Hold till data received and written to rx_buf */ uint8_t NRF24L01_RxPacket(uint8_t *rx_buf); +void NRF24L01_IntRxPacket(uint8_t *rx_buf); /** * Send data in tx_buf and wait till data is sent or max re-tr reached