add test nrf_rx nrf_tx test code

This commit is contained in:
acevest
2020-04-15 20:32:26 +08:00
parent 7e519fba6a
commit c3204264e9
5 changed files with 102 additions and 129 deletions

View File

@@ -14,8 +14,6 @@ void board_init() {
gpio_bit_set(LEDG_GPIO_PORT, LEDG_PIN); gpio_bit_set(LEDG_GPIO_PORT, LEDG_PIN);
gpio_bit_set(LEDB_GPIO_PORT, LEDB_PIN); gpio_bit_set(LEDB_GPIO_PORT, LEDB_PIN);
#if 1
LCD_Init(); // init LCD LCD_Init(); // init LCD
LCD_Clear(BLACK); LCD_Clear(BLACK);
#endif
} }

View File

@@ -3,19 +3,12 @@
#include "lcd.h" #include "lcd.h"
#include "nrf24.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 #define LCD_TASK_SIZE 1024
k_task_t lcd_handle; k_task_t lcd_handle;
uint8_t lcd_stk[LCD_TASK_SIZE]; uint8_t lcd_stk[LCD_TASK_SIZE];
#define LED_TASK_SIZE 1024 #define LED_TASK_SIZE 1024
k_task_t led_handle; k_task_t led_handle;
uint8_t led_stk[LED_TASK_SIZE]; uint8_t led_stk[LED_TASK_SIZE];
@@ -38,7 +31,7 @@ void task_led(void *arg)
{ {
int task_cnt1 = 0; int task_cnt1 = 0;
while (1) { 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); 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) void task_lcd(void *arg)
{ {
@@ -88,7 +72,7 @@ void task_lcd(void *arg)
void main(void) { void main(void) {
board_init(); board_init();
//usart0_init(115200); usart0_init(115200);
tos_knl_init(); tos_knl_init();
@@ -97,9 +81,8 @@ void main(void) {
tos_sem_create(&sem_led, 1); 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(&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(&lcd_handle, "lcd", task_lcd, NULL, 6, lcd_stk, LCD_TASK_SIZE, 0);
tos_knl_start(); tos_knl_start();

View File

@@ -5,23 +5,24 @@
#define USE_SPI1 #define USE_SPI1
extern k_sem_t sem_led; extern k_sem_t sem_led;
k_sem_t sem_nrf_recv; k_sem_t sem_nrf;
int flag = 0;
#define TASK_SIZE (8*1024) #define TASK_SIZE (8*1024)
k_task_t task_nrf24_handle; k_task_t task_nrf24_handle;
uint8_t task_nrf24_stk[TASK_SIZE]; uint8_t task_nrf24_stk[TASK_SIZE];
#define CE_GPIO_PORT GPIOA #define CE_GPIO_PORT GPIOA
#define CE_PIN GPIO_PIN_3 #define CE_PIN GPIO_PIN_3
#define CSN_GPIO_PORT GPIOB #define CSN_GPIO_PORT GPIOB
#define CSN_PIN GPIO_PIN_12 #define CSN_PIN GPIO_PIN_12
#define IRQ_GPIO_PORT GPIOB #define IRQ_GPIO_PORT GPIOB
#define IRQ_PIN GPIO_PIN_5 #define IRQ_PIN GPIO_PIN_5
void task_nrf24(); void task_nrf24();
void nrf24l01_init() { void nrf24l01_init() {
rcu_periph_clock_enable(RCU_GPIOA); rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB); rcu_periph_clock_enable(RCU_GPIOB);
@@ -98,7 +99,7 @@ void EXTI5_9_IRQHandler(void)
nrf_hal_read_reg_byte(REG_STATUS, &status); nrf_hal_read_reg_byte(REG_STATUS, &status);
if(status & _BV(RX_DR)) { 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"); printf("\n");
} }
void task_nrf24() {
if(1) void init_nrf24l01_irq() {
{ // nrf24 irq pin
tos_sem_create(&sem_nrf_recv, 1); gpio_init(IRQ_GPIO_PORT, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, IRQ_PIN);
// nrf24 irq pin gpio_bit_set(IRQ_GPIO_PORT, 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_priority_group_set(ECLIC_PRIGROUP_LEVEL3_PRIO1); eclic_irq_enable(EXTI5_9_IRQn, 1, 1);
eclic_irq_enable(EXTI5_9_IRQn, 1, 1);
/* connect EXTI line to GPIO pin */ /* connect EXTI line to GPIO pin */
gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_5); gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_5);
exti_init(EXTI_5, EXTI_INTERRUPT, EXTI_TRIG_FALLING); exti_init(EXTI_5, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
exti_interrupt_flag_clear(EXTI_5); exti_interrupt_flag_clear(EXTI_5);
}
} void test_nrf24l01_irq_rx()
{
tos_sem_create(&sem_nrf, 1);
init_nrf24l01_irq();
nrf_delay(200); nrf_delay(200);
nrf_csn(1); nrf_csn(1);
@@ -150,6 +152,7 @@ void task_nrf24() {
nrf_set_receive_mode(); nrf_set_receive_mode();
nrf_enable_rx_irq(); nrf_enable_rx_irq();
nrf_set_rf_channel(64); nrf_set_rf_channel(64);
nrf_set_datarate(NRF_2Mbps); nrf_set_datarate(NRF_2Mbps);
uint8_t rxaddr[ADDRLEN] = { 0xAA, 0xCC, 0xEE, 0x00, 0x00 }; uint8_t rxaddr[ADDRLEN] = { 0xAA, 0xCC, 0xEE, 0x00, 0x00 };
@@ -158,17 +161,18 @@ void task_nrf24() {
nrf_enable_rxaddr(0); nrf_enable_rxaddr(0);
while(1) { while(1) {
tos_sem_pend(&sem_nrf_recv, ~0); tos_sem_pend(&sem_nrf, ~0);
uint8_t buf[32]; uint8_t buf[32];
uint8_t len = 0; uint8_t len = 0;
uint8_t pipe = 0xFF; uint8_t pipe = 0xFF;
nrf_read_payload(buf, &len, &pipe); nrf_read_payload(buf, &len, &pipe);
printf("received %u bytes from pipe %u: ", len, pipe);
tos_sem_post(&sem_led); tos_sem_post(&sem_led);
printf("received %u bytes from pipe %u: ", len, pipe);
for(int i=0; i<len; i++) { for(int i=0; i<len; i++) {
printf("%x ", buf[i]); printf("%x ", buf[i]);
} }
@@ -176,20 +180,13 @@ void task_nrf24() {
} }
} }
#if 0 void test_nrf24l01_rx() {
uint8_t nrf_hal_test_rx_old() {
uint8_t data = 0;
nrf_delay(200); nrf_delay(200);
nrf_hal_csn(1); nrf_hal_csn(1);
nrf_hal_ce(0); nrf_hal_ce(0);
nrf_delay(200); nrf_delay(200);
nrf_set_standby_mode(); nrf_set_standby_mode();
nrf_set_receive_mode(); nrf_set_receive_mode();
nrf_disable_rx_irq(); nrf_disable_rx_irq();
@@ -197,72 +194,31 @@ uint8_t nrf_hal_test_rx_old() {
nrf_set_datarate(NRF_2Mbps); nrf_set_datarate(NRF_2Mbps);
uint8_t rxaddr[ADDRLEN] = { 0xAA, 0xCC, 0xEE, 0x00, 0x00 }; uint8_t rxaddr[ADDRLEN] = { 0xAA, 0xCC, 0xEE, 0x00, 0x00 };
nrf_set_rxaddr(0, rxaddr, ADDRLEN); nrf_set_rxaddr(0, rxaddr, ADDRLEN);
nrf_enable_dynamic_payload(0); nrf_enable_dynamic_payload(0);
nrf_enable_dynamic_payload(1);
nrf_enable_rxaddr(0); nrf_enable_rxaddr(0);
nrf_enable_rxaddr(1);
print_rxaddr(0);
print_rxaddr(1);
print_rxaddr(2);
nrf_flush_rx(); nrf_flush_rx();
while(1) { while(1) {
uint8_t buf[32]; uint8_t buf[36];
uint8_t len = 0; uint8_t len = 0;
uint8_t status = 0;
nrf_hal_read_reg_byte(REG_STATUS, &status);
if((status & _BV(RX_DR)) == 0) {
printf("nodata %x\n", status);
nrf_delay(100);
}
uint8_t pipe = 0xFF; uint8_t pipe = 0xFF;
uint32_t loop_cnt = 0;
while(1) {
nrf_hal_read_reg_byte(REG_STATUS, &status);
pipe = status;
pipe >>= 1;
pipe &= 0x07;
if(pipe < 6) {
break;
}
nrf_delay(1);
loop_cnt++;
}
nrf_read_payload(buf, &len);
if(loop_cnt > 0) { nrf_poll_read_payload(buf, &len, &pipe);
printf("loopcnt %u\n", loop_cnt);
}
nrf_hal_set_reg_bit(REG_STATUS, _BV(RX_DR)); tos_sem_post(&sem_led);
nrf_hal_read_reg_byte(REG_STATUS, &status);
nrf_flush_rx();
printf("received %u bytes from pipe %u: ", len, pipe); printf("received %u bytes from pipe %u: ", len, pipe);
for(int i=0; i<len; i++) { for(int i=0; i<len; i++) {
printf("%x ", buf[i]); printf("%x ", buf[i]);
} }
printf("\n"); printf("\n");
} }
return data;
} }
uint8_t nrf_hal_test_tx() { void test_nrf24l01_tx() {
uint8_t data = 0;
nrf_delay(200); nrf_delay(200);
nrf_hal_csn(1); nrf_hal_csn(1);
nrf_hal_ce(0); nrf_hal_ce(0);
@@ -270,45 +226,28 @@ uint8_t nrf_hal_test_tx() {
nrf_set_standby_mode(); nrf_set_standby_mode();
nrf_set_send_mode(); nrf_set_send_mode();
nrf_disable_tx_irq();
nrf_set_rf_channel(100); nrf_set_rf_channel(100);
nrf_set_datarate(NRF_2Mbps); nrf_set_datarate(NRF_2Mbps);
nrf_enable_dynamic_payload(0); nrf_enable_dynamic_payload(0);
uint8_t txaddr[] = { 1, 2, 3, 4, 0 }; uint8_t txaddr[] = { 0xAA, 0xCC, 0xEE, 0x00, 0x01 };
nrf_set_txaddr(txaddr, 5); nrf_set_txaddr(txaddr, 5);
nrf_flush_rx(); nrf_flush_rx();
nrf_flush_tx(); nrf_flush_tx();
uint32_t cnt = 0; uint8_t cnt = 0;
while(1) { while(1) {
nrf_flush_rx(); nrf_flush_rx();
nrf_flush_tx(); nrf_flush_tx();
uint8_t buf[] = {0x0A, 0x0C, 0x0E, cnt++ }; uint8_t buf[] = {0x0A, 0x0C, 0x0E, cnt++ };
nrf_write_payload(buf, sizeof(buf)); nrf_write_payload(buf, sizeof(buf));
tos_sem_post(&sem_led);
while(1) {
uint8_t status = 0;
nrf_hal_read_reg_byte(REG_STATUS, &status);
printf("status %x\n", status);
if(status & _BV(MAX_RT)) {
printf("send error....\n");
nrf_hal_set_reg_bit(REG_STATUS, _BV(MAX_RT));
nrf_flush_tx();
break;
}else if(status & _BV(TX_DS)) {
printf("sended....\n");
nrf_hal_set_reg_bit(REG_STATUS, _BV(TX_DS));
break;
} else {
printf("sending....\n");
}
}
nrf_delay(100); nrf_delay(100);
} }
return data;
} }
#endif
void task_nrf24() {
test_nrf24l01_irq_rx();
//test_nrf24l01_rx();
//test_nrf24l01_tx();
}

View File

@@ -283,7 +283,6 @@ int nrf_enable_dynamic_payload(uint8_t pipe) {
return 0; return 0;
} }
int nrf_read_payload(uint8_t *buf, uint8_t *len, uint8_t *pipe) { int nrf_read_payload(uint8_t *buf, uint8_t *len, uint8_t *pipe) {
// 读数据通道 // 读数据通道
uint8_t status = 0; uint8_t status = 0;
@@ -302,11 +301,63 @@ int nrf_read_payload(uint8_t *buf, uint8_t *len, uint8_t *pipe) {
// 清空接收缓冲区 // 清空接收缓冲区
nrf_flush_rx(); nrf_flush_rx();
if(*pipe >= 6) {
*len = 0;
}
return 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) { 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;
} }

View File

@@ -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_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); int nrf_write_payload(uint8_t *buf, uint8_t len);
void nrf_ce(uint8_t mode); void nrf_ce(uint8_t mode);