myRadio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. #include "board.h"
  2. #include "myRadio.h"
  3. #include "myRadio_gpio.h"
  4. /**-------------------------radio include----------------------------------**/
  5. #include "radio.h"
  6. #include "sx1280.h"
  7. #include "sx1280-hal.h"
  8. /**-------------------------radio include end----------------------------------**/
  9. static int8_t rfTxPower;
  10. static uint32_t rfFrequence;
  11. static uint32_t rfBaudrate;
  12. static rfRxCallBack rxCb;
  13. static uint8_t rfRxBuffer[255];
  14. static uint32_t rf_handle;
  15. static uint8_t rf_workProcess;
  16. static uint8_t chipType;
  17. static uint8_t rfModuleMode = RF_MM_MODE_FLRC;
  18. /**-------------------------radio params----------------------------------**/
  19. ModulationParams_t modulationParams;
  20. rfRxPacket_ts rfRxPacket;
  21. const loraBaudrateFrame_ts loraBaudrateFrame[MAX_RF_BAUDRATE_LR_COUNT] =
  22. {
  23. {//216.264204545455bps,SF=12,BW=203kHz,CR=7
  24. .SpreadingFactor = LORA_SF12,
  25. .SignalBw = LORA_BW_0200,
  26. .ErrorCoding = LORA_CR_LI_4_7,
  27. },
  28. {//991.2109375bps,SF=10,BW=203kHz,CR=4
  29. .SpreadingFactor = LORA_SF10,
  30. .SignalBw = LORA_BW_0200,
  31. .ErrorCoding = LORA_CR_4_8,
  32. },
  33. {//4987.44419642857bps,SF=11,BW=1625kHz,CR=3
  34. .SpreadingFactor = LORA_SF11,
  35. .SignalBw = LORA_BW_1600,
  36. .ErrorCoding = LORA_CR_4_7,
  37. },
  38. {//10150bps,SF=8,BW=812kHz,CR=6
  39. .SpreadingFactor = LORA_SF8,
  40. .SignalBw = LORA_BW_0800,
  41. .ErrorCoding = LORA_CR_LI_4_6,
  42. },
  43. {//20300bps,SF=8,BW=812kHz,CR=1
  44. .SpreadingFactor = LORA_SF8,
  45. .SignalBw = LORA_BW_0800,
  46. .ErrorCoding = LORA_CR_4_5,
  47. },
  48. {//60900bps,SF=6,BW=812kHz,CR=1
  49. .SpreadingFactor = LORA_SF6,
  50. .SignalBw = LORA_BW_0800,
  51. .ErrorCoding = LORA_CR_4_5,
  52. },
  53. {//126953.125bps,SF=5,BW=1625kHz,CR=4
  54. .SpreadingFactor = LORA_SF5,
  55. .SignalBw = LORA_BW_1600,
  56. .ErrorCoding = LORA_CR_4_8,
  57. },
  58. {//203125bps,SF=5,BW=1625kHz,CR=1
  59. .SpreadingFactor = LORA_SF5,
  60. .SignalBw = LORA_BW_1600,
  61. .ErrorCoding = LORA_CR_4_5,
  62. },
  63. };
  64. const fastloraBaudrateFrame_ts fastLoraConfigList[] =
  65. {
  66. {//0.13Mbps
  67. .BitrateBandwidth =FLRC_BR_0_260_BW_0_3,
  68. .CodingRate =FLRC_CR_1_2,
  69. .ModulationShaping =RADIO_MOD_SHAPING_BT_1_0,
  70. },
  71. {//0.26Mbps
  72. .BitrateBandwidth =FLRC_BR_0_520_BW_0_6,
  73. .CodingRate =FLRC_CR_1_2,
  74. .ModulationShaping =RADIO_MOD_SHAPING_BT_1_0,
  75. },
  76. {//0.52Mbps
  77. .BitrateBandwidth =FLRC_BR_1_040_BW_1_2,
  78. .CodingRate =FLRC_CR_1_2,
  79. .ModulationShaping =RADIO_MOD_SHAPING_BT_1_0,
  80. },
  81. {//1.04Mbps
  82. .BitrateBandwidth =FLRC_BR_1_040_BW_1_2,
  83. .CodingRate =FLRC_CR_1_0,
  84. .ModulationShaping =RADIO_MOD_SHAPING_BT_1_0,
  85. },
  86. };
  87. #define RF_BL_ADV_CHANNEL_38 2478000000 // Hz
  88. /*!
  89. * \brief Defines the nominal frequency
  90. */
  91. #define RF_FREQUENCY RF_BL_ADV_CHANNEL_38 // Hz
  92. /*!
  93. * \brief Defines the output power in dBm
  94. *
  95. * \remark The range of the output power is [-18..+13] dBm
  96. */
  97. #define TX_OUTPUT_POWER 13
  98. /*!
  99. * \brief Defines the buffer size, i.e. the payload size
  100. */
  101. #define BUFFER_SIZE 64
  102. /*!
  103. * \brief Number of tick size steps for tx timeout
  104. */
  105. #define TX_TIMEOUT_VALUE 3000 // ms
  106. /*!
  107. * \brief Number of tick size steps for rx timeout
  108. */
  109. #define RX_TIMEOUT_VALUE 0//1000 // ms
  110. /*!
  111. * \brief Size of ticks (used for Tx and Rx timeout)
  112. */
  113. #define RX_TIMEOUT_TICK_SIZE RADIO_TICK_SIZE_1000_US
  114. /*!
  115. * \brief Defines the size of the token defining message type in the payload
  116. */
  117. #define PINGPONGSIZE 4
  118. /*!
  119. * \brief Function to be executed on Radio Tx Done event
  120. */
  121. void OnTxDone( void );
  122. /*!
  123. * \brief Function to be executed on Radio Rx Done event
  124. */
  125. void OnRxDone( void );
  126. /*!
  127. * \brief Function executed on Radio Tx Timeout event
  128. */
  129. void OnTxTimeout( void );
  130. /*!
  131. * \brief Function executed on Radio Rx Timeout event
  132. */
  133. void OnRxTimeout( void );
  134. /*!
  135. * \brief Function executed on Radio Rx Error event
  136. */
  137. void OnRxError( IrqErrorCode_t );
  138. RadioCallbacks_t Callbacks =
  139. {
  140. &OnTxDone, // txDone
  141. &OnRxDone, // rxDone
  142. NULL, // syncWordDone
  143. NULL, // headerDone
  144. &OnTxTimeout, // txTimeout
  145. &OnRxTimeout, // rxTimeout
  146. &OnRxError, // rxError
  147. NULL, // rangingDone
  148. NULL, // cadDone
  149. };
  150. /*!
  151. * \brief Define the possible message type for this application
  152. */
  153. const uint8_t PingMsg[] = "PING";
  154. const uint8_t PongMsg[] = "PONG";
  155. typedef enum
  156. {
  157. APP_LOWPOWER,
  158. APP_RUNNING,
  159. APP_RX,
  160. APP_RX_TIMEOUT,
  161. APP_RX_ERROR,
  162. APP_TX,
  163. APP_TX_TIMEOUT,
  164. }AppStates_t;
  165. /*!
  166. * \brief The size of the buffer
  167. */
  168. uint8_t BufferSize = BUFFER_SIZE;
  169. /*!
  170. * \brief The buffer
  171. */
  172. uint8_t Buffer[BUFFER_SIZE];
  173. /*!
  174. * \brief Mask of IRQs to listen to in rx mode
  175. */
  176. uint16_t RxIrqMask = IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT | IRQ_CRC_ERROR;
  177. /*!
  178. * \brief Mask of IRQs to listen to in tx mode
  179. */
  180. uint16_t TxIrqMask = IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT | IRQ_CRC_ERROR;
  181. /*!
  182. * \brief The State of the application
  183. */
  184. AppStates_t AppState = APP_LOWPOWER;
  185. // #if defined( MODE_BLE )
  186. /*!
  187. * \brief In case of BLE, the payload must contain the header
  188. */
  189. typedef union
  190. {
  191. struct BleAdvHeaderField_s
  192. {
  193. uint8_t pduType: 4;
  194. uint8_t rfu1:2;
  195. uint8_t txAddr:1;
  196. uint8_t rxAddr:1;
  197. uint8_t length:6;
  198. uint8_t rfu2:2;
  199. } Fields;
  200. uint8_t Serial[ 2 ];
  201. }BleAdvHeaders_t;
  202. BleAdvHeaders_t ble_header_adv;
  203. // #endif // MODE_BLE
  204. PacketParams_t packetParams;
  205. PacketStatus_t packetStatus;
  206. /**-------------------------radio params end----------------------------------**/
  207. void myRadio_delay(uint32_t time_ms)
  208. {
  209. delay1ms(time_ms);
  210. }
  211. /**
  212. * @brief IO口中断回调
  213. * IO口产生中断后会执行该函数
  214. * 用于接收射频工作的中断响应
  215. *
  216. * @param index
  217. */
  218. void myRadio_gpioCallback(uint8_t index)
  219. {
  220. SX1280OnDioIrq();
  221. }
  222. void myRadio_setRfModuleMode(uint8_t mode)
  223. {
  224. rfModuleMode = mode;
  225. }
  226. /**
  227. * @brief 射频初始化
  228. *
  229. * @param agr0
  230. * @param agr1_ptr 无线工作状态响应回调
  231. * 产生回调给外部使用,@rfRxCallBack
  232. */
  233. void myRadio_init(int agr0, void *agr1_ptr)
  234. {
  235. myRadio_gpio_init(myRadio_gpioCallback);
  236. /**-------------------------radio init----------------------------------**/
  237. uint16_t ver = 0;
  238. myRadio_delay(10);
  239. Radio.Init( &Callbacks );
  240. Radio.SetRegulatorMode( USE_DCDC ); // Can also be set in LDO mode but consume more power
  241. {
  242. ver = Radio.GetFirmwareVersion();//获取芯片信息 0xA9B5 可用于测试SPI的读写情况
  243. }
  244. if (rfModuleMode == RF_MM_MODE_BLE)
  245. {
  246. modulationParams.PacketType = PACKET_TYPE_BLE;
  247. modulationParams.Params.Ble.BitrateBandwidth = GFS_BLE_BR_1_000_BW_1_2;
  248. modulationParams.Params.Ble.ModulationIndex = GFS_BLE_MOD_IND_0_50;
  249. modulationParams.Params.Ble.ModulationShaping = RADIO_MOD_SHAPING_BT_0_5;
  250. packetParams.PacketType = PACKET_TYPE_BLE;
  251. packetParams.Params.Ble.BlePacketType = BLE_EYELONG_1_0;
  252. packetParams.Params.Ble.ConnectionState = BLE_ADVERTISER;
  253. packetParams.Params.Ble.CrcField = BLE_CRC_3B;
  254. packetParams.Params.Ble.Whitening = RADIO_WHITENING_ON;
  255. }
  256. else if(rfModuleMode == RF_MM_MODE_LORA)
  257. {
  258. modulationParams.PacketType = PACKET_TYPE_LORA;
  259. modulationParams.Params.LoRa.SpreadingFactor = LORA_SF5;//
  260. modulationParams.Params.LoRa.Bandwidth = LORA_BW_0400;
  261. modulationParams.Params.LoRa.CodingRate = LORA_CR_LI_4_5;
  262. packetParams.PacketType = PACKET_TYPE_LORA;
  263. packetParams.Params.LoRa.PreambleLength = 12;
  264. packetParams.Params.LoRa.HeaderType = LORA_PACKET_VARIABLE_LENGTH;
  265. packetParams.Params.LoRa.PayloadLength = BUFFER_SIZE;
  266. packetParams.Params.LoRa.CrcMode = LORA_CRC_ON;
  267. packetParams.Params.LoRa.InvertIQ = LORA_IQ_NORMAL;
  268. }
  269. else if(rfModuleMode == RF_MM_MODE_GFSK)
  270. {
  271. modulationParams.PacketType = PACKET_TYPE_GFSK;
  272. modulationParams.Params.Gfsk.BitrateBandwidth = GFS_BLE_BR_0_125_BW_0_3;
  273. modulationParams.Params.Gfsk.ModulationIndex = GFS_BLE_MOD_IND_1_00;
  274. modulationParams.Params.Gfsk.ModulationShaping = RADIO_MOD_SHAPING_BT_1_0;
  275. packetParams.PacketType = PACKET_TYPE_GFSK;
  276. packetParams.Params.Gfsk.PreambleLength = PREAMBLE_LENGTH_24_BITS;
  277. packetParams.Params.Gfsk.SyncWordLength = GFS_SYNCWORD_LENGTH_5_BYTE;
  278. packetParams.Params.Gfsk.SyncWordMatch = RADIO_RX_MATCH_SYNCWORD_1_2_3;
  279. packetParams.Params.Gfsk.HeaderType = RADIO_PACKET_FIXED_LENGTH;
  280. packetParams.Params.Gfsk.PayloadLength = BUFFER_SIZE;
  281. packetParams.Params.Gfsk.CrcLength = RADIO_CRC_OFF;
  282. packetParams.Params.Gfsk.Whitening = RADIO_WHITENING_OFF;
  283. // packetParams.PacketType = PACKET_TYPE_GFSK;
  284. // packetParams.Params.Gfsk.PreambleLength = PREAMBLE_LENGTH_32_BITS;
  285. // packetParams.Params.Gfsk.SyncWordLength = GFS_SYNCWORD_LENGTH_5_BYTE;
  286. // packetParams.Params.Gfsk.SyncWordMatch = RADIO_RX_MATCH_SYNCWORD_1;
  287. // packetParams.Params.Gfsk.HeaderType = RADIO_PACKET_VARIABLE_LENGTH;
  288. // packetParams.Params.Gfsk.PayloadLength = BUFFER_SIZE;
  289. // packetParams.Params.Gfsk.CrcLength = RADIO_CRC_3_BYTES;
  290. // packetParams.Params.Gfsk.Whitening = RADIO_WHITENING_ON;
  291. }
  292. else if(rfModuleMode == RF_MM_MODE_FLRC)
  293. {
  294. modulationParams.PacketType = PACKET_TYPE_FLRC;
  295. modulationParams.Params.Flrc.BitrateBandwidth = FLRC_BR_0_325_BW_0_3;
  296. modulationParams.Params.Flrc.CodingRate = FLRC_CR_1_2;
  297. modulationParams.Params.Flrc.ModulationShaping = RADIO_MOD_SHAPING_BT_1_0;
  298. packetParams.PacketType = PACKET_TYPE_FLRC;
  299. packetParams.Params.Flrc.PreambleLength = PREAMBLE_LENGTH_32_BITS;
  300. packetParams.Params.Flrc.SyncWordLength = FLRC_SYNCWORD_LENGTH_4_BYTE;
  301. packetParams.Params.Flrc.SyncWordMatch = RADIO_RX_MATCH_SYNCWORD_1;
  302. packetParams.Params.Flrc.HeaderType = RADIO_PACKET_VARIABLE_LENGTH;
  303. packetParams.Params.Flrc.PayloadLength = BUFFER_SIZE;
  304. packetParams.Params.Flrc.CrcLength = RADIO_CRC_3_BYTES;
  305. packetParams.Params.Flrc.Whitening = RADIO_WHITENING_OFF;
  306. }
  307. else
  308. {
  309. modulationParams.PacketType = PACKET_TYPE_FLRC;
  310. modulationParams.Params.Flrc.BitrateBandwidth = FLRC_BR_0_325_BW_0_3;
  311. modulationParams.Params.Flrc.CodingRate = FLRC_CR_1_2;
  312. modulationParams.Params.Flrc.ModulationShaping = RADIO_MOD_SHAPING_BT_1_0;
  313. packetParams.PacketType = PACKET_TYPE_FLRC;
  314. packetParams.Params.Flrc.PreambleLength = PREAMBLE_LENGTH_32_BITS;
  315. packetParams.Params.Flrc.SyncWordLength = FLRC_SYNCWORD_LENGTH_4_BYTE;
  316. packetParams.Params.Flrc.SyncWordMatch = RADIO_RX_MATCH_SYNCWORD_1;
  317. packetParams.Params.Flrc.HeaderType = RADIO_PACKET_VARIABLE_LENGTH;
  318. packetParams.Params.Flrc.PayloadLength = BUFFER_SIZE;
  319. packetParams.Params.Flrc.CrcLength = RADIO_CRC_3_BYTES;
  320. packetParams.Params.Flrc.Whitening = RADIO_WHITENING_OFF;
  321. }
  322. // #elif defined( MODE_GFSK )
  323. // #elif defined( MODE_LORA )
  324. // #elif defined( MODE_FLRC )
  325. // #else
  326. // #error "Please select the mode of operation for the Ping Ping demo"
  327. // #endif
  328. Radio.SetStandby( STDBY_RC );
  329. Radio.SetPacketType( modulationParams.PacketType );
  330. Radio.SetModulationParams( &modulationParams );
  331. Radio.SetPacketParams( &packetParams );
  332. Radio.SetRfFrequency( RF_FREQUENCY );//频点设置
  333. Radio.SetBufferBaseAddresses( 0x00, 0x00 );
  334. Radio.SetTxParams( TX_OUTPUT_POWER, RADIO_RAMP_02_US );//功率设置
  335. // Radio.SetInterruptMode();
  336. Radio.SetPollingMode();
  337. if(rfModuleMode == RF_MM_MODE_GFSK)
  338. {
  339. //接收灵敏度测试
  340. Radio.SetSyncWord( 1, ( uint8_t[] ){ 0xDD, 0xA0, 0x96, 0x69, 0xDD } );
  341. }
  342. if(rfModuleMode == RF_MM_MODE_BLE)
  343. {
  344. // only used in GENERIC and BLE mode
  345. Radio.WriteRegister(0x9c7, 0x55 );
  346. Radio.WriteRegister(0x9c8, 0x55 );
  347. Radio.WriteRegister(0x9c9, 0x55 );
  348. //Radio.WriteRegister( 0x9c5, 0x33 );
  349. Radio.SetBleAdvertizerAccessAddress( );
  350. Radio.SetWhiteningSeed( 0x33 );
  351. ble_header_adv.Fields.length = PINGPONGSIZE + 2;
  352. ble_header_adv.Fields.pduType = 2;
  353. }
  354. AppState = APP_LOWPOWER;
  355. /**-------------------------radio init end----------------------------------**/
  356. RF_EXT_PA_TO_IDLE();
  357. if ((rfRxCallBack )agr1_ptr)
  358. {
  359. rxCb = (rfRxCallBack )agr1_ptr;
  360. }
  361. rf_handle = 0xe5;
  362. }
  363. /**
  364. * @brief 射频底层执行程序
  365. * 要放在主循环中执行
  366. *
  367. */
  368. void myRadio_process(void)
  369. {
  370. if (rf_handle == 0)
  371. {
  372. return;
  373. }
  374. SX1280ProcessIrqs();
  375. }
  376. /**
  377. * @brief 退出射频进入休眠
  378. *
  379. */
  380. void myRadio_abort(void)
  381. {
  382. if (rf_handle == 0)
  383. {
  384. return;
  385. }
  386. RF_EXT_PA_TO_IDLE();
  387. SleepParams_t SleepParams;
  388. SleepParams.DataBufferRetention = 1;
  389. SleepParams.DataRamRetention = 1;
  390. SleepParams.InstructionRamRetention = 1;
  391. SleepParams.WakeUpRTC = 0;
  392. // Radio.SetSleep( SleepParams );
  393. Radio.SetStandby( STDBY_RC );
  394. }
  395. /**
  396. * @brief 获取射频工作中心频率
  397. *
  398. * @return uint32_t
  399. */
  400. uint32_t myRadio_getFrequency(void)
  401. {
  402. if (rf_handle == 0)
  403. {
  404. return 0;
  405. }
  406. return rfFrequence;
  407. }
  408. /**
  409. * @brief 设置射频工作中心频率
  410. *
  411. * @param freq
  412. * 具体频点,单位:Hz
  413. */
  414. void myRadio_setFrequency(uint32_t freq)
  415. {
  416. if (rf_handle == 0)
  417. {
  418. return;
  419. }
  420. rfFrequence = freq;
  421. Radio.SetRfFrequency(freq);
  422. }
  423. /**
  424. * @brief 获取发射功率
  425. *
  426. * @return int8_t
  427. */
  428. int8_t myRadio_getTxPower(void)
  429. {
  430. if (rf_handle == 0)
  431. {
  432. return 0;
  433. }
  434. return rfTxPower;
  435. }
  436. /**
  437. * @brief 设置发射功率
  438. *
  439. * @param power
  440. * 单位:dbm
  441. */
  442. void myRadio_setTxPower(int8_t power)
  443. {
  444. if (rf_handle == 0)
  445. {
  446. return;
  447. }
  448. rfTxPower = power;
  449. Radio.SetTxParams( rfTxPower, RADIO_RAMP_02_US );
  450. }
  451. /**
  452. * 获取射频波特率
  453. * @param : br->
  454. */
  455. uint32_t myRadio_getBaudrate(void)
  456. {
  457. if (rf_handle == 0)
  458. {
  459. return 0;
  460. }
  461. return rfBaudrate;
  462. }
  463. /**
  464. * 设置射频波特率
  465. * @param : br->
  466. */
  467. void myRadio_setBaudrate(uint32_t br)
  468. {
  469. if (rf_handle == 0)
  470. {
  471. return;
  472. }
  473. rfBaudrate = br;
  474. if(rfModuleMode == RF_MM_MODE_GFSK)
  475. {
  476. }
  477. else if (rfModuleMode == RF_MM_MODE_LORA)
  478. {
  479. modulationParams.Params.LoRa.SpreadingFactor = loraBaudrateFrame[rfBaudrate].SpreadingFactor;//
  480. modulationParams.Params.LoRa.Bandwidth = loraBaudrateFrame[rfBaudrate].SignalBw;
  481. modulationParams.Params.LoRa.CodingRate = loraBaudrateFrame[rfBaudrate].ErrorCoding;
  482. }
  483. else if (rfModuleMode == RF_MM_MODE_FLRC)
  484. {
  485. modulationParams.Params.Flrc.BitrateBandwidth = fastLoraConfigList[rfBaudrate].BitrateBandwidth;//
  486. modulationParams.Params.Flrc.CodingRate = fastLoraConfigList[rfBaudrate].CodingRate;
  487. modulationParams.Params.Flrc.ModulationShaping = fastLoraConfigList[rfBaudrate].ModulationShaping;
  488. }
  489. else
  490. {
  491. modulationParams.Params.Flrc.BitrateBandwidth = fastLoraConfigList[rfBaudrate].BitrateBandwidth;//
  492. modulationParams.Params.Flrc.CodingRate = fastLoraConfigList[rfBaudrate].CodingRate;
  493. modulationParams.Params.Flrc.ModulationShaping = fastLoraConfigList[rfBaudrate].ModulationShaping;
  494. }
  495. Radio.SetModulationParams( &modulationParams );
  496. }
  497. /**
  498. * @brief 设置模组型号
  499. *
  500. * @param type
  501. */
  502. void myRadio_setChipType(uint8_t type)
  503. {
  504. chipType = type;
  505. }
  506. /**
  507. * @brief 获取模组型号
  508. *
  509. * @return uint8_t
  510. */
  511. uint8_t myRadio_getChipType(void)
  512. {
  513. return chipType;
  514. }
  515. int16_t myRadio_getRssi(void)
  516. {
  517. return Radio.GetRssiInst();;
  518. }
  519. /**
  520. * @brief 无线发送数据包
  521. *
  522. * @param packet
  523. */
  524. void myRadio_transmit(rfTxPacket_ts *packet)
  525. {
  526. if (rf_handle == 0)
  527. {
  528. return;
  529. }
  530. RF_EXT_PA_TO_TX();
  531. if (Radio.GetOpMode() == MODE_SLEEP)
  532. {
  533. myRadio_delay(1);
  534. }
  535. TickTime_t TickTime ={ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE };
  536. Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
  537. if(rfModuleMode == RF_MM_MODE_GFSK)
  538. {
  539. packetParams.Params.Gfsk.PayloadLength = packet->len;
  540. }
  541. else if (rfModuleMode == RF_MM_MODE_LORA)
  542. {
  543. packetParams.Params.LoRa.PayloadLength = packet->len;
  544. }
  545. else if (rfModuleMode == RF_MM_MODE_FLRC)
  546. {
  547. packetParams.Params.Flrc.PayloadLength = packet->len;
  548. }
  549. else
  550. {
  551. packetParams.Params.Flrc.PayloadLength = packet->len;
  552. }
  553. Radio.SetPacketParams( &packetParams );
  554. Radio.SendPayload( packet->payload, packet->len, TickTime);
  555. }
  556. /**
  557. * @brief 进入无线接收
  558. *
  559. */
  560. void myRadio_receiver(void)
  561. {
  562. if (rf_handle == 0)
  563. {
  564. return;
  565. }
  566. RF_EXT_PA_TO_RX();
  567. TickTime_t TickTime ={ RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE };
  568. Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
  569. Radio.SetRx( TickTime );
  570. }
  571. void myRadio_setCtrl(controlMode_te mode, uint32_t value)
  572. {
  573. if (rf_handle == 0)
  574. {
  575. return;
  576. }
  577. myRadio_setRfModuleMode(RF_MM_MODE_GFSK);
  578. myRadio_init(0, 0);
  579. myRadio_setFrequency(rfFrequence);
  580. myRadio_setTxPower(rfTxPower);
  581. myRadio_setBaudrate(rfBaudrate);
  582. switch (mode)
  583. {
  584. case RADIO_EXT_CONTROL_TX_UNMODULATED:
  585. {
  586. RF_EXT_PA_TO_TX();
  587. Radio.SetTxContinuousWave();
  588. }
  589. break;
  590. case RADIO_EXT_CONTROL_TX_MODULATED:
  591. {
  592. RF_EXT_PA_TO_TX();
  593. Radio.SetTxContinuousWave();
  594. }
  595. break;
  596. case RADIO_EXT_CONTROL_RX_SENSITIVITY:
  597. {
  598. RF_EXT_PA_TO_RX();
  599. myRadio_receiver();
  600. }
  601. break;
  602. default:
  603. break;
  604. }
  605. }
  606. /**-------------------------radio funtion----------------------------------**/
  607. void OnTxDone( void )
  608. {
  609. AppState = APP_TX;
  610. RF_EXT_PA_TO_IDLE();
  611. if (rxCb)
  612. {
  613. rxCb(TX_STA_SECCESS, rfRxPacket);
  614. }
  615. }
  616. void OnRxDone( void )
  617. {
  618. AppState = APP_RX;
  619. memset(rfRxPacket.payload, 0, sizeof(rfRxPacket.payload));
  620. Radio.GetPayload( rfRxPacket.payload, &rfRxPacket.len, sizeof(rfRxPacket.payload) );
  621. rfRxPacket.rssi = Radio.GetRssiInst();
  622. if (rxCb)
  623. {
  624. rxCb(RX_STA_SECCESS, rfRxPacket);
  625. }
  626. }
  627. void OnTxTimeout( void )
  628. {
  629. AppState = APP_TX_TIMEOUT;
  630. }
  631. void OnRxTimeout( void )
  632. {
  633. rfRxPacket_ts rfRxPacket;
  634. AppState = APP_RX_TIMEOUT;
  635. if (rxCb)
  636. {
  637. rxCb(RX_STA_TIMEOUT, rfRxPacket);
  638. }
  639. }
  640. void OnRxError( IrqErrorCode_t errorCode )
  641. {
  642. rfRxPacket_ts rfRxPacket;
  643. AppState = APP_RX_ERROR;
  644. if (rxCb)
  645. {
  646. rxCb(RX_STA_PAYLOAD_ERROR, rfRxPacket);
  647. }
  648. }
  649. void OnRangingDone( IrqRangingCode_t val )
  650. {
  651. }
  652. void OnCadDone( bool channelActivityDetected )
  653. {
  654. }
  655. /**-------------------------radio funtion end----------------------------------**/