main.c 15 KB

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