[$is_event_jimu] 2 [$group_e] 元件事件库.24L01无线通讯 [$group] 元件库-通信器.$pname$ [$last_ver_update_time] 2021-04-03 [$min_need_ver] 1.23 [$p_list] [$short_show_UIname] [$code_area_type] codes,exe_per_1ms,init_code [$blocks_type] line [$name] 24L01的初始化积木 [$引脚占用列表,用于提示冲突] [($_xgm$CE),type=pin]|24L0积木里已占用CE引脚 [($_xgm$CSN),type=pin]|24L0积木里已占用CSN引脚 [($_xgm$SCK),type=pin]|24L0积木里已占用SCK引脚 [($_xgm$MOSI),type=pin]|24L0积木里已占用MOSI引脚 [($_xgm$MISO),type=pin]|24L0积木里已占用MISO引脚 [($_xgm$IRQ),type=pin]|24L0积木里已占用IRQ引脚 [$寄存器占用列表,用于提示冲突,但=>后的值没变则不提示冲突] [$中断函数号占用列表,用于提示冲突] [$interrupt_1] [$thread_do] [$cs] [$function_ref_define_in_headfile] @@file=24l01.h #define READ_REG 0x00 // Define read command to register #define WRITE_REG 0x20 // Define write command to register #define RD_RX_PLOAD 0x61 // Define RX payload register address #define WR_TX_PLOAD 0xA0 // Define TX payload register address #define FLUSH_TX 0xE1 // 命令:清除TX FIFO 寄存器 #define FLUSH_RX 0xE2 // 命令:清除RX FIFO 寄存器 #define REUSE_TX_PL 0xE3 // Define reuse TX payload register command #define NOP 0xFF // Define No Operation, might be used to read status register //***************************************************// // NRF24L01 相关寄存器地址的宏定义 #define CONFIG 0x00 // 'Config' register address #define EN_AA 0x01 // 'Enable Auto Acknowledgment' register address #define EN_RXADDR 0x02 // 'Enabled RX addresses' register address #define SETUP_AW 0x03 // 'Setup address width' register address #define SETUP_RETR 0x04 // 'Setup Auto. Retrans' register address #define RF_CH 0x05 // 'RF channel' register address #define RF_SETUP 0x06 // 'RF setup' register address #define STATUS 0x07 // 'Status' register address #define OBSERVE_TX 0x08 // 'Observe TX' register address #define CD 0x09 // 'Carrier Detect' register address #define RX_ADDR_P0 0x0A // 'RX address pipe0' register address #define RX_ADDR_P1 0x0B // 'RX address pipe1' register address #define RX_ADDR_P2 0x0C // 'RX address pipe2' register address #define RX_ADDR_P3 0x0D // 'RX address pipe3' register address #define RX_ADDR_P4 0x0E // 'RX address pipe4' register address #define RX_ADDR_P5 0x0F // 'RX address pipe5' register address #define TX_ADDR 0x10 // 'TX address' register address #define RX_PW_P0 0x11 // 'RX payload width, pipe0' register address #define RX_PW_P1 0x12 // 'RX payload width, pipe1' register address #define RX_PW_P2 0x13 // 'RX payload width, pipe2' register address #define RX_PW_P3 0x14 // 'RX payload width, pipe3' register address #define RX_PW_P4 0x15 // 'RX payload width, pipe4' register address #define RX_PW_P5 0x16 // 'RX payload width, pipe5' register address #define FIFO_STATUS 0x17 // 'FIFO Status Register' register address #define Address_Width 5 // 5 bytes TX(RX) address width #define Data_Payload_Width 12 // 30 // 20 bytes TX payload extern u8 nRF24L01_RxPacket_$rnd_varname_pre$_is_get_frame; extern u8 ret_g_TX_$rnd_varname_pre$; extern u8 RX_Address_ID_$rnd_varname_pre$[] ; // Define a static rX address extern u8 Address_ID_$rnd_varname_pre$[] ; // Define a static TX address //接收/发送时的地址 extern u8 rx_buf_$rnd_varname_pre$[]; extern u8 tx_buf_$rnd_varname_pre$[];//发送时的有效数据 extern u8 re_ok_flag_$rnd_varname_pre$; extern u8 send_ok_flag_$rnd_varname_pre$; #define CE_$rnd_varname_pre$ [($_xgm$CE),type=pin] #define CSN_$rnd_varname_pre$ [($_xgm$CSN),type=pin] #define SCK_$rnd_varname_pre$ [($_xgm$SCK),type=pin] #define MOSI_$rnd_varname_pre$ [($_xgm$MOSI),type=pin] #define MISO_$rnd_varname_pre$ [($_xgm$MISO),type=pin] #define IRQ_$rnd_varname_pre$ [($_xgm$IRQ),type=pin] void update_addr_$rnd_varname_pre$(); void RF24_Init_$rnd_varname_pre$(); void RF24_SetRfCh_$rnd_varname_pre$(u8 ch); char RX_Mode_$rnd_varname_pre$(struct pt *process_pt); char TX_Mode_$rnd_varname_pre$(struct pt *process_pt); char nRF24L01_RxPacket_$rnd_varname_pre$(struct pt *process_pt); u8 get_rx_buf_one_byte_$rnd_varname_pre$(u8 index1); void set_tx_buf_one_byte_$rnd_varname_pre$(u8 index1,u8 val1); char nRF24L01_TxPacket_$rnd_varname_pre$(struct pt *process_pt); void rx_update_addr_$rnd_varname_pre$(); [$init_code_for_define_area_once] @@file=24l01.c #define READ_REG 0x00 // Define read command to register #define WRITE_REG 0x20 // Define write command to register #define RD_RX_PLOAD 0x61 // Define RX payload register address #define WR_TX_PLOAD 0xA0 // Define TX payload register address #define FLUSH_TX 0xE1 // 命令:清除TX FIFO 寄存器 #define FLUSH_RX 0xE2 // 命令:清除RX FIFO 寄存器 #define REUSE_TX_PL 0xE3 // Define reuse TX payload register command #define NOP 0xFF // Define No Operation, might be used to read status register //***************************************************// // NRF24L01 相关寄存器地址的宏定义 #define CONFIG 0x00 // 'Config' register address #define EN_AA 0x01 // 'Enable Auto Acknowledgment' register address #define EN_RXADDR 0x02 // 'Enabled RX addresses' register address #define SETUP_AW 0x03 // 'Setup address width' register address #define SETUP_RETR 0x04 // 'Setup Auto. Retrans' register address #define RF_CH 0x05 // 'RF channel' register address #define RF_SETUP 0x06 // 'RF setup' register address #define STATUS 0x07 // 'Status' register address #define OBSERVE_TX 0x08 // 'Observe TX' register address #define CD 0x09 // 'Carrier Detect' register address #define RX_ADDR_P0 0x0A // 'RX address pipe0' register address #define RX_ADDR_P1 0x0B // 'RX address pipe1' register address #define RX_ADDR_P2 0x0C // 'RX address pipe2' register address #define RX_ADDR_P3 0x0D // 'RX address pipe3' register address #define RX_ADDR_P4 0x0E // 'RX address pipe4' register address #define RX_ADDR_P5 0x0F // 'RX address pipe5' register address #define TX_ADDR 0x10 // 'TX address' register address #define RX_PW_P0 0x11 // 'RX payload width, pipe0' register address #define RX_PW_P1 0x12 // 'RX payload width, pipe1' register address #define RX_PW_P2 0x13 // 'RX payload width, pipe2' register address #define RX_PW_P3 0x14 // 'RX payload width, pipe3' register address #define RX_PW_P4 0x15 // 'RX payload width, pipe4' register address #define RX_PW_P5 0x16 // 'RX payload width, pipe5' register address #define FIFO_STATUS 0x17 // 'FIFO Status Register' register address #define Address_Width 5 // 5 bytes TX(RX) address width #define Data_Payload_Width 12// 30 // 20 bytes TX payload /*********************************************发射功率与频率配置值***********************************************/ #define RATE_2Mbps 0x08 //2M速率 #define RATE_1Mbps 0x00 //1M速率 #define RATE_250Kbps 0x20 //250K速率 #define SI24R1_7dBm 0x07 //台产SI24R1芯片7dBm #define SI24R1_4dBm 0x06 //台产SI24R1芯片4dBm #define SI24R1_3dBm 0x05 //台产SI24R1芯片3dBm #define SI24R1_1dBm 0x04 //台产SI24R1芯片1dBm #define SI24R1_0dBm 0x03 //台产SI24R1芯片0dBm #define SI24R1_f4dBm 0x02 //台产SI24R1芯片-4dBm #define SI24R1_f6dBm 0x01 //台产SI24R1芯片-6dBm #define SI24R1_f12dBm 0x00 //台产SI24R1芯片-12dBm #define E01ML01SP4_20dBm 0x06 //成都亿佰特模块20dBm #define E01ML01SP4_14dBm 0x04 //成都亿佰特模块14dBm #define E01ML01SP4_8dBm 0x02 //成都亿佰特模块8dBm #define E01ML01SP4_2dBm 0x00 //成都亿佰特模块2dBm [$init_code_for_define_area] @@file=24l01.c #include "protothread.h" #include "app_process.h" #include "sys/timer.h" #include "Extline_Lib.h" #include "Lib_base.h" //考虑PROCESS2_WAIT_UNTIL宏 struct timer timer_at_$rnd_varname_pre$; u8 RX_Address_ID_$rnd_varname_pre$[Address_Width] = {0x34,0x43,0x10,0x10,0x02}; // Define a static rX address u8 Address_ID_$rnd_varname_pre$[Address_Width] = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address //接收/发送时的地址 u8 rx_buf_$rnd_varname_pre$[Data_Payload_Width]; u8 tx_buf_$rnd_varname_pre$[Data_Payload_Width];//发送时的有效数据 u8 re_ok_flag_$rnd_varname_pre$=0; u8 send_ok_flag_$rnd_varname_pre$=0; #define CE_$rnd_varname_pre$ [($_xgm$CE),type=pin] #define CSN_$rnd_varname_pre$ [($_xgm$CSN),type=pin] #define SCK_$rnd_varname_pre$ [($_xgm$SCK),type=pin] #define MOSI_$rnd_varname_pre$ [($_xgm$MOSI),type=pin] #define MISO_$rnd_varname_pre$ [($_xgm$MISO),type=pin] #define IRQ_$rnd_varname_pre$ [($_xgm$IRQ),type=pin] #define uchar u8 void Delay10us_$rnd_varname_pre$(u16 times1) { u16 a; u16 b; for(b=times1;b>0;b--) for(a=#get_10us_delay_num_stm32f10x(group_xml,rand1,block_name,ref_complier,extinfo)%;a>0;a--); } uchar SPI_RW_$rnd_varname_pre$(uchar byte1)//最基本的函数,完成GPIO 模拟SPI 的功能 { uchar bit_ctr; TT_disable_100us_Task_scan_for_other(1); //wait_can_run_for_uart_data_ok(); for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit { if((byte1 & 0x80)>0){ IO_Write(MOSI_$rnd_varname_pre$ ,1) ; }else{ IO_Write(MOSI_$rnd_varname_pre$ ,0) ; } byte1 = (byte1 << 1); // shift next bit into MSB.. IO_Write(SCK_$rnd_varname_pre$ ,1) ; // Set SCK high.. //Delay10us_$rnd_varname_pre$(20); byte1 |=IO_Read(MISO_$rnd_varname_pre$);// ; // capture current MISO bit IO_Write(SCK_$rnd_varname_pre$ ,0) ; } //exit_can_run_for_uart_data_ok(); TT_disable_100us_Task_scan_for_other(0); return (byte1); // return read byte } uchar SPI_RW_Reg_$rnd_varname_pre$(uchar reg, uchar value)//写寄存器 { uchar status; IO_Write(CSN_$rnd_varname_pre$ ,0) ; // CSN low, init SPI transaction status = SPI_RW_$rnd_varname_pre$(reg); // select register SPI_RW_$rnd_varname_pre$(value); // ..and write value to it.. IO_Write(CSN_$rnd_varname_pre$ ,1) ; // CSN high again return(status); // return nRF24L01 status byte } uchar SPI_Read_Reg_$rnd_varname_pre$(uchar reg)//读寄存器 { uchar reg_val; IO_Write(CSN_$rnd_varname_pre$ ,0) ; // CSN low, initialize SPI communication... SPI_RW_$rnd_varname_pre$(reg); // Select register to read from.. reg_val = SPI_RW_$rnd_varname_pre$(0); // ..then read registervalue IO_Write(CSN_$rnd_varname_pre$ ,1) ; // CSN high, terminate SPI communication return(reg_val); // return register value } uchar SPI_Read_Buf_$rnd_varname_pre$(struct pt *process_pt,uchar reg, uchar *pBuf, uchar bytes,u8 * status_g)//读取FIFO 缓冲区 { PT_BEGIN(process_pt); static u8 isok=1; static uchar status; static uchar byte_ctr; if(isok==1){ isok=0; byte_ctr=0; status=0; } IO_Write(CSN_$rnd_varname_pre$ ,0) ; // Set CSN low, init SPI tranaction status = SPI_RW_$rnd_varname_pre$(reg); // Select register to write to and read status byte for(byte_ctr=0;byte_ctrData_Payload_Width){ max_rx_len=Data_Payload_Width; } call_PT_func(SPI_Read_Buf_$rnd_varname_pre$ ,&pt_22_$rnd_varname_pre$ ,RD_RX_PLOAD,rx_buf_$rnd_varname_pre$,max_rx_len,&get_ret);// read receive payload from RX_FIFO buffer SPI_RW_Reg_$rnd_varname_pre$(FLUSH_RX,0xff); //清除RX FIFO寄存器 IO_Write(CE_$rnd_varname_pre$ ,1) ; //CE=1则使能发射或接收 timer_set(&timer_at_$rnd_varname_pre$, 1 ); timer_restart(&timer_at_$rnd_varname_pre$); PROCESS2_WAIT_UNTIL(process_pt , timer_expired(&timer_at_$rnd_varname_pre$)!=0); //Delay10us_$rnd_varname_pre$(5); revale =1; //读取数据完成标志 } nRF24L01_RxPacket_$rnd_varname_pre$_is_get_frame=revale; //*revale_g=revale; isok=1; PT_END(process_pt); //return revale; } u8 get_rx_buf_one_byte_$rnd_varname_pre$(u8 index1){ if(index1>(Data_Payload_Width-1)){return 0;} return rx_buf_$rnd_varname_pre$[index1]; } void set_tx_buf_one_byte_$rnd_varname_pre$(u8 index1,u8 val1){ if(index1>(Data_Payload_Width-1)){return ;} tx_buf_$rnd_varname_pre$[index1]=val1; } #define RX_DR 0x40 //接收成功 #define TX_DS 0x20 //发送成功 #define MAX_RT 0x10 //达到最大重发次数 u8 ret_g_TX_$rnd_varname_pre$=0; char nRF24L01_TxPacket_$rnd_varname_pre$(struct pt *process_pt) { PT_BEGIN(process_pt); //u32 max_t=0; static u8 isok=1; static u8 state=0; if(isok==1){ isok=0; state=0; } IO_Write(CE_$rnd_varname_pre$ ,0) ; //StandBy I模式 //SPI_Write_Buf_$rnd_varname_pre$(WRITE_REG + RX_ADDR_P0, Address_ID_$rnd_varname_pre$, Address_Width); // 装载接收端地址 SPI_Write_Buf_$rnd_varname_pre$(WR_TX_PLOAD, tx_buf_$rnd_varname_pre$, Data_Payload_Width); // 装载数据 // SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送 IO_Write(CE_$rnd_varname_pre$ ,1) ; //置高CE,激发数据发送 //Delay10us_$rnd_varname_pre$(7);//old=200,CE为高后延时一段时间,等待发送完成! // while(IRQ_$rnd_varname_pre$&&max_t<100){max_t++;Delay10us_$rnd_varname_pre$(10);} ; // 等待数据接收完成,old=20000,改为10,也即最大等待10ms timer_set(&timer_at_$rnd_varname_pre$, 10 );//最大等待10ms timer_restart(&timer_at_$rnd_varname_pre$); PROCESS2_WAIT_UNTIL(process_pt , timer_expired(&timer_at_$rnd_varname_pre$)!=0 && IO_Read(IRQ_$rnd_varname_pre$)==0 ); state=SPI_Read_Reg_$rnd_varname_pre$(STATUS); //读取状态寄存器的值 SPI_RW_Reg_$rnd_varname_pre$(WRITE_REG+STATUS, state); //清除TX_DS或MAX_RT中断标志 if(state&MAX_RT) //达到最大重发次数 { SPI_RW_Reg_$rnd_varname_pre$(FLUSH_TX,0xff); //清除TX FIFO寄存器 ret_g_TX_$rnd_varname_pre$=0;isok=1; return PT_ENDED; } if(state&TX_DS) { //发送完成 ret_g_TX_$rnd_varname_pre$=1;isok=1; return PT_ENDED; } ret_g_TX_$rnd_varname_pre$=0;isok=1; PT_END(process_pt); } void rx_update_addr_$rnd_varname_pre$(){ IO_Write(CE_$rnd_varname_pre$ ,0) ; //命令字:发送地址 地址数据--指针 字数5---默认的地址宽带 //SPI_Write_Buf_$rnd_varname_pre$(WRITE_REG + TX_ADDR, RX_Address_ID_$rnd_varname_pre$, Address_Width); //0x10, Writes Address_ID to nRF24L01 //设置发送地址 SPI_Write_Buf_$rnd_varname_pre$(WRITE_REG + RX_ADDR_P0, RX_Address_ID_$rnd_varname_pre$, Address_Width); //0x0a, RX_Addr0 same as TX_Adr for Auto.Ack //设置数据通道0接收地址 IO_Write(CE_$rnd_varname_pre$ ,1) ; Delay10us_$rnd_varname_pre$(1); } void tx_update_addr_$rnd_varname_pre$(){ IO_Write(CE_$rnd_varname_pre$ ,0) ; //命令字:发送地址 地址数据--指针 字数5---默认的地址宽带 SPI_Write_Buf_$rnd_varname_pre$(WRITE_REG + TX_ADDR, RX_Address_ID_$rnd_varname_pre$, Address_Width); //0x10, Writes Address_ID to nRF24L01 //设置发送地址 //SPI_Write_Buf_$rnd_varname_pre$(WRITE_REG + RX_ADDR_P0, RX_Address_ID_$rnd_varname_pre$, Address_Width); //0x0a, RX_Addr0 same as TX_Adr for Auto.Ack //设置数据通道0接收地址 IO_Write(CE_$rnd_varname_pre$ ,1) ; Delay10us_$rnd_varname_pre$(1); } void update_addr_$rnd_varname_pre$(){ IO_Write(CE_$rnd_varname_pre$ ,0) ; //命令字:发送地址 地址数据--指针 字数5---默认的地址宽带 SPI_Write_Buf_$rnd_varname_pre$(WRITE_REG + TX_ADDR, Address_ID_$rnd_varname_pre$, Address_Width); //0x10, Writes Address_ID to nRF24L01 //设置发送地址 SPI_Write_Buf_$rnd_varname_pre$(WRITE_REG + RX_ADDR_P0, Address_ID_$rnd_varname_pre$, Address_Width); //0x0a, RX_Addr0 same as TX_Adr for Auto.Ack //设置数据通道0接收地址 IO_Write(CE_$rnd_varname_pre$ ,1) ; Delay10us_$rnd_varname_pre$(1); } void RF24_SetRfCh_$rnd_varname_pre$(u8 ch){ IO_Write(CE_$rnd_varname_pre$ ,0) ; SPI_RW_Reg_$rnd_varname_pre$(RF_CH, ch); //设置变更射频通道 IO_Write(CE_$rnd_varname_pre$ ,1) ; } [$init_code_for_init_val] #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$CE),type=pin]","1","24L01的片选输入")% #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$CSN),type=pin]","1","24L01的CSN输入")% #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$SCK),type=pin]","1","24L01的SCK输入")% #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$MOSI),type=pin]","1","24L01的MOSI输入")% #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$MISO),type=pin]","0","24L01的MISO输出")% #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$IRQ),type=pin]","0","24L01的IRQ输出")% IO_Write([($_xgm$CE),type=pin] ,0) ; IO_Write([($_xgm$CSN),type=pin] ,1) ; IO_Write([($_xgm$SCK),type=pin] ,0) ; IO_Write([($_xgm$MOSI),type=pin] ,1) ; IO_Write([($_xgm$MISO),type=pin] ,1) ; IO_Write([($_xgm$IRQ),type=pin] ,1) ; Address_ID_$rnd_varname_pre$[0]=[($_xgm$addr1),type=num]; Address_ID_$rnd_varname_pre$[1]=[($_xgm$addr2),type=num]; Address_ID_$rnd_varname_pre$[2]=[($_xgm$addr3),type=num]; Address_ID_$rnd_varname_pre$[3]=[($_xgm$addr4),type=num]; Address_ID_$rnd_varname_pre$[4]=[($_xgm$addr5),type=num]; RF24_Init_$rnd_varname_pre$(); [$timer0_1ms_code] [$toolbox_block_code] [$replace_map] $_xgm$CE = $field$CE,mytype|pin, ptype|TIMES $_xgm$CSN= $field$CSN,mytype|pin, ptype|TIMES $_xgm$SCK = $field$SCK,mytype|pin, ptype|TIMES $_xgm$MOSI= $field$MOSI,mytype|pin, ptype|TIMES $_xgm$MISO = $field$MISO,mytype|pin, ptype|TIMES $_xgm$IRQ= $field$IRQ,mytype|pin, ptype|TIMES $_xgm$addr1 = $value$addr1 ,mytype|num, ptype|TIMES $_xgm$addr2 = $value$addr2 ,mytype|num, ptype|TIMES $_xgm$addr3 = $value$addr3 ,mytype|num, ptype|TIMES $_xgm$addr4 = $value$addr4 ,mytype|num, ptype|TIMES $_xgm$addr5 = $value$addr5 ,mytype|num, ptype|TIMES