C51 COMPILER V9.60.7.0 MAIN 11/21/2025 17:21:11 PAGE 1 C51 COMPILER V9.60.7.0, COMPILATION OF MODULE MAIN OBJECT MODULE PLACED IN .\Objects\main.obj COMPILER INVOKED BY: d:\Keil_v5\C51\BIN\C51.EXE ..\source\main.c OBJECTADVANCED OPTIMIZE(9,SPEED) BROWSE ORDER NOAREGS M -ODC2 INCDIR(..\..\..\..\driver\inc;..\..\..\..\mcu;..\..\..\..\middleware\log;..\..\..\..\middleware\delay;..\..\..\..\m -iddleware\rf_basis) DEBUG PRINT(.\Listings\main.lst) TABS(2) OBJECT(.\Objects\main.obj) line level source 1 /** 2 ************************************************************************ 3 * @file main.c 4 * @brief Example for RF tx function. 5 * @version V0.6 6 * @date 2024-11-20 7 * @author Panchip Team 8 * Copyright (C) 2024 Panchip Technology Corp. All rights reserved. 9 **************************************************************************** 10 */ 11 12 #include 13 #include "stdlib.h" 14 #include 15 #include 16 #include "gpio.h" 17 #include "rcc.h" 18 #include "rf.h" 19 #include "uart.h" 20 #include "timer.h" 21 #include "log.h" 22 23 #ifndef IS_CLIENT_BOARD 24 #define IS_CLIENT_BOARD 0 25 #endif 26 27 #if IS_CLIENT_BOARD #define USER_KEY_PIN GPIO_P32 #define USER_KEY_PIN_BIT P3_2 #define USER_KEY_PIN_MUX GPIO_P32_MUX_IO #else 32 #define USER_KEY_PIN GPIO_P33 33 #define USER_KEY_PIN_BIT P3_3 34 #define USER_KEY_PIN_MUX GPIO_P33_MUX_IO 35 #endif 36 37 #define RF_ADRESS_SIZE 5 38 #define RF_TX_BUF_SIZE 64 // The length of PAYLOAD/ACK PAYLOAD to be sent each time 39 #define RF_RX_BUF_SIZE 64 // Receive payload length is set by RF_SetRxPayloadLen 40 RF_ITStatus_t rf_status; 41 42 typedef struct 43 { 44 /* radio param */ 45 u16 channel; // 2400~2480 46 RF_TxPower_t txPower; // 0dbm~13dbm 47 RF_DataRate_t rate; // 1Mbps, 2Mbps and 250Kbps 48 49 /* radio config */ 50 RF_ChipMode_t ChipMode; // 297L、BLE and 24L01 51 RF_WorkMode_t WorkMode; // normal mode and enhance mode 52 bool EnAPL; // ack payload enable or disable only used in enhancde mode 53 bool EnDPL; // dynamic payload enable or disable only used in enhancde mode C51 COMPILER V9.60.7.0 MAIN 11/21/2025 17:21:11 PAGE 2 54 bool EnTxNoAck; // tx noack enable or disable only used in normal mode 55 bool EnWhite; // whiten enable or disable 56 RF_Crc_t crc; 57 u8 AddrWidth; 58 u8 TRxAddr[RF_ADRESS_SIZE]; 59 60 u16 RxTimeoutUs; // RF Wait for ack timeout time in enhance mode or receive a packet with timeo -ut in normal mode 61 u16 TxSetupTimeUs; // RF tx setup time, the time from the end of the last bit of received packet -to the start of the first bit of the ack packet 62 63 u8 TxBuf[RF_TX_BUF_SIZE]; 64 u8 RxBuf[RF_RX_BUF_SIZE]; 65 66 u8 TxLen; 67 u8 RxLen; 68 } RFConfig_t; 69 70 RFConfig_t xdata gRfConfig = 71 { 72 /*.Channel = */ 2418, 73 /*.TxPower = */ 7, 74 /*.DataRate = */ RF_DR_1Mbps, 75 /*.ChipMode = */ RF_CHIPMODE_BLE, 76 /*.WorkMode = */ RF_WORKMODE_NORMAL, 77 /*.EnAPL = */ ENABLE, 78 /*.EnDPL = */ ENABLE, 79 /*.EnTxNoAck = */ DISABLE, 80 /*.EnWhite = */ ENABLE, 81 /*.Crc = */ RF_CRC_2BYTE, 82 /*.AddrWidth = */ RF_ADRESS_SIZE, 83 /*.TRxAddr = */ {0x10, 0x22, 0x55, 0x0F, 0x71}, // If you want to test multi-pipe0, you can u -se this address 84 // /*.TRxAddr = */ {0x11, 0x22, 0x55, 0x0F, 0x71}, // If you want to test multi-pipe1, you can -use this address 85 // /*.TRxAddr = */ {0x12, 0x22, 0x55, 0x0F, 0x71}, // If you want to test multi-pipe2, you can -use this address 86 // /*.TRxAddr = */ {0x13, 0x22, 0x55, 0x0F, 0x71}, // If you want to test multi-pipe3, you can -use this address 87 // /*.TRxAddr = */ {0x14, 0x22, 0x55, 0x0F, 0x71}, // If you want to test multi-pipe4, you can -use this address 88 // /*.TRxAddr = */ {0x15, 0x22, 0x55, 0x0F, 0x71}, // If you want to test multi-pipe5, you can -use this address 89 /*.RxTimeoutUs = */ 1000, 90 /*.TxSetupTimeUs = */ 200, 91 /*.TxBuf = */ {0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55, 92 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55, 93 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55, 94 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55, 95 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55, 96 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55, 97 0x55,0x55,0x55,0x55}, 98 /*.RxBuf = */ {0}, 99 /*.TxLen = */ 0, 100 /*.RxLen = */ 0, 101 }; 102 103 u16 xdata Timer2IntCnt = 0; // The interrupt period of timer2's setting is about 50ms 104 105 void APP_UartInit(void) 106 { 107 1 RCC_PeriphClockCmd(RCC_PERIPH_UART, ENABLE); C51 COMPILER V9.60.7.0 MAIN 11/21/2025 17:21:11 PAGE 3 108 1 RCC_PeriphClockCmd(RCC_PERIPH_PORT, ENABLE); 109 1 RCC_PeriphClockCmd(RCC_PERIPH_TIMER1, ENABLE); 110 1 111 1 /** Initialize uart params(if baudrate is 115200, timer1 will be used by uart) */ 112 1 GPIO_Init(GPIO_P31, GPIO_P31_MUX_TXD0, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL); 113 1 UART_Init(LENGTH_8, DISABLE_RX, DISABLE_PAR, BAUD_115200); 114 1 } 115 116 void APP_Timer2Init(void) 117 { 118 1 RCC_PeriphClockCmd(RCC_PERIPH_TIMER2, ENABLE); 119 1 120 1 TIM_TimeBaseInit(TIMER2, TIM2_Mode0_16BitAutoReload, TIM_CLK_DIV_12T, 0xFACB); 121 1 TIM_EnableIRQ(TIMER2); /**< Enable timer0 interrupt */ 122 1 TIM_Cmd(TIMER2, ENABLE); /**< Start timer0 */ 123 1 } 124 125 void APP_RfInit(RFConfig_t *pCfg) 126 { 127 1 RCC_PeriphClockCmd(RCC_PERIPH_RF, ENABLE); 128 1 129 1 RF_Init(); 130 1 131 1 RF_CarrierOffset(0x00); 132 1 RF_SetChipMode(pCfg->ChipMode); 133 1 RF_SetDataRate(pCfg->rate); 134 1 RF_SetChannel(pCfg->channel-2400); 135 1 RF_SetCrc(pCfg->crc); 136 1 RF_SetTxPower(pCfg->txPower); 137 1 138 1 RF_SetFifoLenType(RF_FIFOLEN_TYPE_64); 139 1 140 1 RF_SetTxAddr(pCfg->TRxAddr, pCfg->AddrWidth); 141 1 RF_SetRxAddr(RF_PIPE0, pCfg->TRxAddr, pCfg->AddrWidth); 142 1 RF_SetRxPayloadLen(RF_PIPE0, sizeof(gRfConfig.RxBuf)); 143 1 144 1 RF_SetWorkMode(pCfg->WorkMode); 145 1 146 1 /* RF tx setup time: 112us is minimum value */ 147 1 RF_SetTxSetupTime(pCfg->TxSetupTimeUs); 148 1 149 1 if(pCfg->WorkMode == RF_WORKMODE_ENHANCE) 150 1 { 151 2 RF_SetTRxAckTimeout(pCfg->RxTimeoutUs); 152 2 RF_SetAutoRetrans(250, 3); // 250us delay, 3 times retransmit 153 2 RF_SetNoAck(pCfg->EnTxNoAck); 154 2 RF_SetAckPayload(pCfg->EnAPL); 155 2 RF_EnableDynamicPayload(RF_PIPE0, pCfg->EnDPL); 156 2 } 157 1 else 158 1 { 159 2 RF_SetAutoRetrans(250, 0); // You must disable the auto retransmit function, when chip is in norma -l mode 160 2 RF_SetNoAck(ENABLE); // You must enable the Tx noack bit, when chip is in normal mode 161 2 RF_SetAckPayload(DISABLE); 162 2 RF_EnableDynamicPayload(RF_PIPE0, DISABLE); 163 2 } 164 1 165 1 RF_ITConfig(RF_ITCONF_TX|RF_ITCONF_RX|RF_ITCONF_TX_MAX, ENABLE); 166 1 RF_EnableIRQ(); 167 1 } 168 void DelayMs(u16 Ms) C51 COMPILER V9.60.7.0 MAIN 11/21/2025 17:21:11 PAGE 4 169 { 170 1 u16 i, j; 171 1 172 1 for (i = 0; i < Ms; i++) 173 1 { 174 2 for (j = 0; j < 1000; j++) 175 2 { 176 3 ; 177 3 _nop_(); 178 3 } 179 2 } 180 1 } 181 static unsigned char key_flag = 0; 182 unsigned char key_scan(void) 183 { 184 1 uint8_t P10Sta; 185 1 if (USER_KEY_PIN_BIT) 186 1 { 187 2 P10Sta = 1; 188 2 } 189 1 else 190 1 { 191 2 P10Sta = 0; 192 2 } 193 1 // printf("USER_KEY_PIN_BIT=[%bu]\r\n", P10Sta); 194 1 if(P10Sta==0) 195 1 { 196 2 if (key_flag== 0) 197 2 { 198 3 DelayMs(300); 199 3 if(P10Sta==0) 200 3 { 201 4 key_flag = 1; 202 4 } 203 3 } 204 2 205 2 } 206 1 else 207 1 { 208 2 if(P10Sta) 209 2 { 210 3 if(key_flag==1) 211 3 { 212 4 DelayMs(300); 213 4 if(P10Sta) 214 4 { 215 5 if(key_flag==1) 216 5 { 217 6 key_flag=0; 218 6 return 1; 219 6 } 220 5 key_flag=0; 221 5 } 222 4 } 223 3 } 224 2 } 225 1 226 1 return 0; 227 1 } 228 int main(void) 229 { 230 1 uint8_t txrx_done = 0; C51 COMPILER V9.60.7.0 MAIN 11/21/2025 17:21:11 PAGE 5 231 1 uint8_t mode = 250; 232 1 uint8_t mode_set = 0; 233 1 uint8_t rf_ch = 2; 234 1 uint8_t get_rand_num = 0; 235 1 uint8_t autoMode = 0; 236 1 int sendPacketCount = 0; 237 1 /** System clock initialize */ 238 1 RCC_SysClkInit(); 239 1 DelayMs(1000); 240 1 APP_UartInit(); 241 1 APP_Timer2Init(); 242 1 APP_RfInit(&gRfConfig); 243 1 244 1 RF_EnterTxMode(); 245 1 GPIO_Init(USER_KEY_PIN, USER_KEY_PIN_MUX, GPIO_MODE_INPUT, GPIO_PULLUP); 246 1 EA = 1; 247 1 248 1 printf("RF Tx Test.\r\n"); 249 1 250 1 while (1) 251 1 { 252 2 // static u8 Count = 0; 253 2 if (rf_status) 254 2 { 255 3 switch (rf_status) 256 3 { 257 4 case RF_IT_TX_RX_DONE: 258 4 printf("Tx&Rx done.\n"); 259 4 break; 260 4 case RF_IT_TX_DONE: 261 4 // printf("Tx done.\n"); 262 4 txrx_done = 1; 263 4 break; 264 4 case RF_IT_RX_DONE: 265 4 // printf("Rx done.\n"); 266 4 break; 267 4 case RF_IT_TX_MAX_RT: 268 4 printf("Tx fail.\n"); 269 4 break; 270 4 default: 271 4 break; 272 4 } 273 3 rf_status = RF_IT_NONE; 274 3 } 275 2 if(key_scan()) 276 2 { 277 3 txrx_done = 1; 278 3 mode_set = 1; 279 3 mode++; 280 3 if(mode>=9)mode = 0; 281 3 printf("mode=[%bu]\r\n", mode); 282 3 } 283 2 284 2 switch(mode) 285 2 { 286 3 case 0: 287 3 if(mode_set) 288 3 { 289 4 mode_set = 0; 290 4 txrx_done = 1; 291 4 RF_Deinit(); 292 4 APP_RfInit(&gRfConfig); C51 COMPILER V9.60.7.0 MAIN 11/21/2025 17:21:11 PAGE 6 293 4 RF_SetChannel(02); 294 4 RF_EnterTxMode(); 295 4 } 296 3 if (txrx_done) 297 3 { 298 4 txrx_done = 0; 299 4 // DelayMs(50); 300 4 RF_FlushTxFifo(); 301 4 if(gRfConfig.EnTxNoAck) 302 4 { 303 5 RF_WriteTxPayloadByXDATA(rf_tx_noack, gRfConfig.TxBuf, sizeof(gRfConfig.TxBuf)); 304 5 } 305 4 else 306 4 { 307 5 RF_WriteTxPayloadByXDATA(rf_tx, gRfConfig.TxBuf, sizeof(gRfConfig.TxBuf)); 308 5 } 309 4 // printf("TxLen[%bu]:\n", sizeof(gRfConfig.TxBuf)); 310 4 // panlog_hexdump(16, gRfConfig.TxBuf, sizeof(gRfConfig.TxBuf)); 311 4 } 312 3 313 3 break; 314 3 case 1: 315 3 if(mode_set) 316 3 { 317 4 mode_set = 0; 318 4 txrx_done = 1; 319 4 RF_SetChannel(40); 320 4 } 321 3 if (txrx_done) 322 3 { 323 4 txrx_done = 0; 324 4 // DelayMs(50); 325 4 RF_FlushTxFifo(); 326 4 RF_WriteTxPayloadByXDATA(rf_tx_noack, gRfConfig.TxBuf, sizeof(gRfConfig.TxBuf)); 327 4 // printf("start to send data\r\n"); 328 4 } 329 3 break; 330 3 case 2: 331 3 if(mode_set) 332 3 { 333 4 mode_set = 0; 334 4 txrx_done = 1; 335 4 RF_SetChannel(80); 336 4 } 337 3 if (txrx_done) 338 3 { 339 4 txrx_done = 0; 340 4 // DelayMs(50); 341 4 RF_FlushTxFifo(); 342 4 RF_WriteTxPayloadByXDATA(rf_tx_noack, gRfConfig.TxBuf, sizeof(gRfConfig.TxBuf)); 343 4 // printf("start to send data\r\n"); 344 4 } 345 3 break; 346 3 case 3: 347 3 if(mode_set) 348 3 { 349 4 mode_set = 0; 350 4 RF_SetChannel(2); 351 4 RF_EnterRxMode(); 352 4 printf("RF_EnterRxMode\r\n"); 353 4 } 354 3 break; C51 COMPILER V9.60.7.0 MAIN 11/21/2025 17:21:11 PAGE 7 355 3 case 4: 356 3 if(mode_set) 357 3 { 358 4 mode_set = 0; 359 4 txrx_done=0; 360 4 RF_Deinit(); 361 4 APP_RfInit(&gRfConfig); 362 4 RF_SetChannel(2); 363 4 RF_EnterTxMode(); 364 4 // DelayMs(50); 365 4 366 4 RF_Carrier(); 367 4 } 368 3 break; 369 3 case 5: 370 3 if(mode_set) 371 3 { 372 4 mode_set = 0; 373 4 RF_SetChannel(40); 374 4 } 375 3 break; 376 3 case 6: 377 3 if(mode_set) 378 3 { 379 4 380 4 mode_set = 0; 381 4 RF_SetChannel(80); 382 4 } 383 3 break; 384 3 case 7: 385 3 if(mode_set) 386 3 { 387 4 mode_set = 0; 388 4 RF_Deinit(); 389 4 APP_RfInit(&gRfConfig); 390 4 RF_EnterTxMode(); 391 4 txrx_done=1; 392 4 sendPacketCount = 0; 393 4 } 394 3 if (sendPacketCount >= 300) 395 3 { 396 4 sendPacketCount = 0; 397 4 // flag_200ms = 0; 398 4 get_rand_num = rand()%41; 399 4 rf_ch = get_rand_num*2; 400 4 if(rf_ch>80)rf_ch=80; 401 4 RF_SetChannel(rf_ch); 402 4 printf("rand_num=[%bu]\r\n", rf_ch); 403 4 } 404 3 if (txrx_done) 405 3 { 406 4 sendPacketCount++; 407 4 txrx_done=0; 408 4 RF_FlushTxFifo(); 409 4 RF_WriteTxPayloadByXDATA(rf_tx_noack, gRfConfig.TxBuf, sizeof(gRfConfig.TxBuf)); 410 4 } 411 3 412 3 break; 413 3 case 8: 414 3 if(mode_set) 415 3 { 416 4 mode_set = 0; C51 COMPILER V9.60.7.0 MAIN 11/21/2025 17:21:11 PAGE 8 417 4 rf_ch = 0; 418 4 txrx_done=1; 419 4 sendPacketCount = 0; 420 4 } 421 3 422 3 if (sendPacketCount >= 300) 423 3 { 424 4 sendPacketCount = 0; 425 4 txrx_done=0; 426 4 rf_ch+=2; 427 4 if(rf_ch>80)rf_ch=2; 428 4 RF_SetChannel(rf_ch); 429 4 printf("rf_ch=[%bu]\r\n", rf_ch); 430 4 } 431 3 if (txrx_done) 432 3 { 433 4 txrx_done=0; 434 4 sendPacketCount++; 435 4 RF_FlushTxFifo(); 436 4 RF_WriteTxPayloadByXDATA(rf_tx_noack, gRfConfig.TxBuf, sizeof(gRfConfig.TxBuf)); 437 4 } 438 3 break; 439 3 } 440 2 // if(Timer2IntCnt >= 500) 441 2 // { 442 2 // Timer2IntCnt = 0; 443 2 // memset(gRfConfig.TxBuf, Count++, sizeof(gRfConfig.TxBuf)); 444 2 // RF_FlushTxFifo(); 445 2 // if(gRfConfig.EnTxNoAck) 446 2 // { 447 2 // RF_WriteTxPayloadByXDATA(rf_tx_noack, gRfConfig.TxBuf, sizeof(gRfConfig.TxBuf)); 448 2 // } 449 2 // else 450 2 // { 451 2 // RF_WriteTxPayloadByXDATA(rf_tx, gRfConfig.TxBuf, sizeof(gRfConfig.TxBuf)); 452 2 // } 453 2 // printf("TxLen[%bu]:\n", sizeof(gRfConfig.TxBuf)); 454 2 // panlog_hexdump(16, gRfConfig.TxBuf, sizeof(gRfConfig.TxBuf)); 455 2 // } 456 2 457 2 // if(gRfConfig.RxLen > 0) 458 2 // { 459 2 // printf("RxLen[%bu]:\n", gRfConfig.RxLen); 460 2 // panlog_hexdump(16, gRfConfig.RxBuf, gRfConfig.RxLen); 461 2 // gRfConfig.RxLen = 0; 462 2 // } 463 2 } 464 1 } 465 466 /** 467 * @brief 468 * @param None 469 * @retval None 470 */ 471 void RF_InterruptHandler(void) interrupt RF_COM3_VECTOR 472 { 473 1 474 1 rf_status = RF_GetIRQFlags(); 475 1 switch (rf_status) 476 1 { 477 2 case RF_IT_TX_RX_DONE: 478 2 if(gRfConfig.EnAPL != RF_APL_DISABLE) C51 COMPILER V9.60.7.0 MAIN 11/21/2025 17:21:11 PAGE 9 479 2 { 480 3 gRfConfig.RxLen = RF_ReadRxPayloadByXDATA(gRfConfig.RxBuf, sizeof(gRfConfig.RxBuf)); 481 3 } 482 2 //printf("Tx&Rx done.\n"); 483 2 break; 484 2 case RF_IT_TX_DONE: 485 2 //printf("Tx done.\n"); 486 2 break; 487 2 case RF_IT_RX_DONE: 488 2 //printf("Rx done.\n"); 489 2 break; 490 2 case RF_IT_TX_MAX_RT: 491 2 //printf("Tx fail.\n"); 492 2 break; 493 2 default: 494 2 break; 495 2 } 496 1 RF_FlushRxFifo(); 497 1 RF_ClearIRQFlags(); 498 1 } 499 500 void TIMER2_InterruptHandler(void) interrupt TIMER2_VECTOR 501 { 502 1 /* mode1:Tmax = (0x10000)*(1/(16M/div_12)) = 49.152ms */ 503 1 /* Clear timer2 global interrupt falg */ 504 1 TF2 = 0; 505 1 Timer2IntCnt++; 506 1 } MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 1476 ---- CONSTANT SIZE = 98 ---- XDATA SIZE = 153 ---- PDATA SIZE = ---- ---- DATA SIZE = 2 11 IDATA SIZE = ---- ---- BIT SIZE = ---- ---- EDATA SIZE = ---- ---- HDATA SIZE = ---- ---- XDATA CONST SIZE = ---- ---- FAR CONST SIZE = ---- ---- END OF MODULE INFORMATION. C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)