From c3204264e9bdbb97ce1194a5755679fa78a3acd0 Mon Sep 17 00:00:00 2001 From: acevest Date: Wed, 15 Apr 2020 20:32:26 +0800 Subject: [PATCH] add test nrf_rx nrf_tx test code --- board/Sipeed_LonganNano/BSP/Src/mcu_init.c | 2 - .../Sipeed_LonganNano/eclipse/nRF24L01/main.c | 25 +-- .../eclipse/nRF24L01/nrf24.c | 147 +++++------------- .../eclipse/nRF24L01/nrf24l01.c | 55 ++++++- .../eclipse/nRF24L01/nrf24l01.h | 2 + 5 files changed, 102 insertions(+), 129 deletions(-) diff --git a/board/Sipeed_LonganNano/BSP/Src/mcu_init.c b/board/Sipeed_LonganNano/BSP/Src/mcu_init.c index 073ae165..149da2f5 100644 --- a/board/Sipeed_LonganNano/BSP/Src/mcu_init.c +++ b/board/Sipeed_LonganNano/BSP/Src/mcu_init.c @@ -14,8 +14,6 @@ void board_init() { gpio_bit_set(LEDG_GPIO_PORT, LEDG_PIN); gpio_bit_set(LEDB_GPIO_PORT, LEDB_PIN); -#if 1 LCD_Init(); // init LCD LCD_Clear(BLACK); -#endif } diff --git a/board/Sipeed_LonganNano/eclipse/nRF24L01/main.c b/board/Sipeed_LonganNano/eclipse/nRF24L01/main.c index 8172e794..a0e2c794 100644 --- a/board/Sipeed_LonganNano/eclipse/nRF24L01/main.c +++ b/board/Sipeed_LonganNano/eclipse/nRF24L01/main.c @@ -3,19 +3,12 @@ #include "lcd.h" #include "nrf24.h" -#if 0 -#define TASK_SIZE 1024 -k_task_t task2_handle -uint8_t task2_stk[TASK_SIZE*1]; - -#endif #define LCD_TASK_SIZE 1024 k_task_t lcd_handle; uint8_t lcd_stk[LCD_TASK_SIZE]; - #define LED_TASK_SIZE 1024 k_task_t led_handle; uint8_t led_stk[LED_TASK_SIZE]; @@ -38,7 +31,7 @@ void task_led(void *arg) { int task_cnt1 = 0; while (1) { - printf("hello world from %s cnt: %d\n", __func__, task_cnt1++); + //printf("hello world from %s cnt: %d\n", __func__, task_cnt1++); tos_sem_pend(&sem_led, ~0); @@ -48,15 +41,6 @@ void task_led(void *arg) } } -void task2(void *arg) -{ - int task_cnt2 = 0; - - while (1) { - //printf("hello world from %s cnt: %08x\n", __func__, task_cnt2--); - tos_task_delay(200); - } -} void task_lcd(void *arg) { @@ -88,7 +72,7 @@ void task_lcd(void *arg) void main(void) { board_init(); - //usart0_init(115200); + usart0_init(115200); tos_knl_init(); @@ -97,9 +81,8 @@ void main(void) { tos_sem_create(&sem_led, 1); - tos_task_create(&led_handle, "led", task_led, NULL, 6, led_stk, LED_TASK_SIZE, 0); - //tos_task_create(&task2_handle, "task2", task2, NULL, 3, task2_stk, TASK_SIZE*1, 0); - tos_task_create(&lcd_handle, "lcd", task_lcd, NULL, 6, lcd_stk, LCD_TASK_SIZE, 0); + tos_task_create(&led_handle, "led", task_led, NULL, 6, led_stk, LED_TASK_SIZE, 0); + tos_task_create(&lcd_handle, "lcd", task_lcd, NULL, 6, lcd_stk, LCD_TASK_SIZE, 0); tos_knl_start(); diff --git a/board/Sipeed_LonganNano/eclipse/nRF24L01/nrf24.c b/board/Sipeed_LonganNano/eclipse/nRF24L01/nrf24.c index 437910fd..1c2a7898 100644 --- a/board/Sipeed_LonganNano/eclipse/nRF24L01/nrf24.c +++ b/board/Sipeed_LonganNano/eclipse/nRF24L01/nrf24.c @@ -5,23 +5,24 @@ #define USE_SPI1 extern k_sem_t sem_led; -k_sem_t sem_nrf_recv; -int flag = 0; +k_sem_t sem_nrf; + #define TASK_SIZE (8*1024) + k_task_t task_nrf24_handle; uint8_t task_nrf24_stk[TASK_SIZE]; #define CE_GPIO_PORT GPIOA #define CE_PIN GPIO_PIN_3 - #define CSN_GPIO_PORT GPIOB #define CSN_PIN GPIO_PIN_12 - #define IRQ_GPIO_PORT GPIOB #define IRQ_PIN GPIO_PIN_5 void task_nrf24(); + + void nrf24l01_init() { rcu_periph_clock_enable(RCU_GPIOA); rcu_periph_clock_enable(RCU_GPIOB); @@ -98,7 +99,7 @@ void EXTI5_9_IRQHandler(void) nrf_hal_read_reg_byte(REG_STATUS, &status); if(status & _BV(RX_DR)) { - tos_sem_post(&sem_nrf_recv); + tos_sem_post(&sem_nrf); } } @@ -120,26 +121,27 @@ void print_rxaddr(uint8_t pipe) { printf("\n"); } -void task_nrf24() { - if(1) - { - tos_sem_create(&sem_nrf_recv, 1); - // nrf24 irq pin - gpio_init(IRQ_GPIO_PORT, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, IRQ_PIN); - gpio_bit_set(IRQ_GPIO_PORT, IRQ_PIN); +void init_nrf24l01_irq() { + // nrf24 irq pin + gpio_init(IRQ_GPIO_PORT, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, IRQ_PIN); + gpio_bit_set(IRQ_GPIO_PORT, IRQ_PIN); - eclic_global_interrupt_enable(); - eclic_priority_group_set(ECLIC_PRIGROUP_LEVEL3_PRIO1); - eclic_irq_enable(EXTI5_9_IRQn, 1, 1); + eclic_priority_group_set(ECLIC_PRIGROUP_LEVEL3_PRIO1); + eclic_irq_enable(EXTI5_9_IRQn, 1, 1); - /* connect EXTI line to GPIO pin */ - gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_5); + /* connect EXTI line to GPIO pin */ + gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_5); - exti_init(EXTI_5, EXTI_INTERRUPT, EXTI_TRIG_FALLING); - exti_interrupt_flag_clear(EXTI_5); + exti_init(EXTI_5, EXTI_INTERRUPT, EXTI_TRIG_FALLING); + exti_interrupt_flag_clear(EXTI_5); +} - } +void test_nrf24l01_irq_rx() +{ + tos_sem_create(&sem_nrf, 1); + + init_nrf24l01_irq(); nrf_delay(200); nrf_csn(1); @@ -150,6 +152,7 @@ void task_nrf24() { nrf_set_receive_mode(); nrf_enable_rx_irq(); + nrf_set_rf_channel(64); nrf_set_datarate(NRF_2Mbps); uint8_t rxaddr[ADDRLEN] = { 0xAA, 0xCC, 0xEE, 0x00, 0x00 }; @@ -158,17 +161,18 @@ void task_nrf24() { nrf_enable_rxaddr(0); while(1) { - tos_sem_pend(&sem_nrf_recv, ~0); + tos_sem_pend(&sem_nrf, ~0); uint8_t buf[32]; uint8_t len = 0; uint8_t pipe = 0xFF; + nrf_read_payload(buf, &len, &pipe); - printf("received %u bytes from pipe %u: ", len, pipe); - tos_sem_post(&sem_led); + printf("received %u bytes from pipe %u: ", len, pipe); + for(int i=0; i>= 1; - pipe &= 0x07; - if(pipe < 6) { - break; - } - nrf_delay(1); - loop_cnt++; - } - nrf_read_payload(buf, &len); - if(loop_cnt > 0) { - printf("loopcnt %u\n", loop_cnt); - } + nrf_poll_read_payload(buf, &len, &pipe); - nrf_hal_set_reg_bit(REG_STATUS, _BV(RX_DR)); - nrf_hal_read_reg_byte(REG_STATUS, &status); - - nrf_flush_rx(); + tos_sem_post(&sem_led); printf("received %u bytes from pipe %u: ", len, pipe); - for(int i=0; i= 6) { + *len = 0; + } + return 0; } + +int nrf_poll_read_payload(uint8_t *buf, uint8_t *len, uint8_t *pipe) { + while(1) { + // 读数据通道 + uint8_t status = 0; + nrf_hal_read_reg_byte(REG_STATUS, &status); + if((status & _BV(RX_DR)) == 0) { + nrf_delay(1); + continue; + } + + *pipe = ((status>>1) & 0x07); + + break; + } + + // 读数据长度 + nrf_hal_cmd_read_byte(CMD_R_RX_PL_WID, len); + + // 读数据 + nrf_hal_cmd_read(CMD_R_RX_PAYLOAD, buf, *len); + + // 清除数据标志位 + _nrf_set_reg_bit(REG_STATUS, _BV(RX_DR)); + + // 清空接收缓冲区 + nrf_flush_rx(); + + if(*pipe >= 6) { + *len = 0; + } + + return 0; +} + int nrf_write_payload(uint8_t *buf, uint8_t len) { - return nrf_hal_cmd_write(CMD_W_TX_PAYLOAD_NOACK, buf, len); + nrf_hal_cmd_write(CMD_W_TX_PAYLOAD_NOACK, buf, len); + + while(1) { + uint8_t status = 0; + nrf_hal_read_reg_byte(REG_STATUS, &status); + if(status & _BV(TX_DS)) { + nrf_delay(1); + } + _nrf_set_reg_bit(REG_STATUS, _BV(MAX_RT)); + _nrf_set_reg_bit(REG_STATUS, _BV(TX_DS)); + break; + } + + return 0; } diff --git a/board/Sipeed_LonganNano/eclipse/nRF24L01/nrf24l01.h b/board/Sipeed_LonganNano/eclipse/nRF24L01/nrf24l01.h index 4443ea61..667fca72 100644 --- a/board/Sipeed_LonganNano/eclipse/nRF24L01/nrf24l01.h +++ b/board/Sipeed_LonganNano/eclipse/nRF24L01/nrf24l01.h @@ -346,6 +346,8 @@ int nrf_enable_dynamic_payload(uint8_t pipe); int nrf_read_payload(uint8_t *buf, uint8_t *len, uint8_t *pipe); +int nrf_poll_read_payload(uint8_t *buf, uint8_t *len, uint8_t *pipe); + int nrf_write_payload(uint8_t *buf, uint8_t len); void nrf_ce(uint8_t mode);