myRadio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. uint32_t i, j;
  210. i = time_ms;
  211. while (i --)
  212. {
  213. for ( j = 0; j < 1000; j++)
  214. {
  215. ;
  216. }
  217. }
  218. }
  219. /**
  220. * @brief IO口中断回调
  221. * IO口产生中断后会执行该函数
  222. * 用于接收射频工作的中断响应
  223. *
  224. * @param index
  225. */
  226. void myRadio_gpioCallback(uint8_t index)
  227. {
  228. SX1280OnDioIrq();
  229. }
  230. void myRadio_setRfModuleMode(uint8_t mode)
  231. {
  232. rfModuleMode = mode;
  233. }
  234. /**
  235. * @brief 射频初始化
  236. *
  237. * @param agr0
  238. * @param agr1_ptr 无线工作状态响应回调
  239. * 产生回调给外部使用,@rfRxCallBack
  240. */
  241. void myRadio_init(int agr0, void *agr1_ptr)
  242. {
  243. myRadio_gpio_init(myRadio_gpioCallback);
  244. /**-------------------------radio init----------------------------------**/
  245. uint16_t ver = 0;
  246. myRadio_delay(1000);
  247. Radio.Init( &Callbacks );
  248. Radio.SetRegulatorMode( USE_DCDC ); // Can also be set in LDO mode but consume more power
  249. {
  250. ver = Radio.GetFirmwareVersion();//获取芯片信息 0xA9B5 可用于测试SPI的读写情况
  251. }
  252. if (rfModuleMode == RF_MM_MODE_BLE)
  253. {
  254. modulationParams.PacketType = PACKET_TYPE_BLE;
  255. modulationParams.Params.Ble.BitrateBandwidth = GFS_BLE_BR_1_000_BW_1_2;
  256. modulationParams.Params.Ble.ModulationIndex = GFS_BLE_MOD_IND_0_50;
  257. modulationParams.Params.Ble.ModulationShaping = RADIO_MOD_SHAPING_BT_0_5;
  258. packetParams.PacketType = PACKET_TYPE_BLE;
  259. packetParams.Params.Ble.BlePacketType = BLE_EYELONG_1_0;
  260. packetParams.Params.Ble.ConnectionState = BLE_ADVERTISER;
  261. packetParams.Params.Ble.CrcField = BLE_CRC_3B;
  262. packetParams.Params.Ble.Whitening = RADIO_WHITENING_ON;
  263. }
  264. else if(rfModuleMode == RF_MM_MODE_LORA)
  265. {
  266. modulationParams.PacketType = PACKET_TYPE_LORA;
  267. modulationParams.Params.LoRa.SpreadingFactor = LORA_SF5;//
  268. modulationParams.Params.LoRa.Bandwidth = LORA_BW_0400;
  269. modulationParams.Params.LoRa.CodingRate = LORA_CR_LI_4_5;
  270. packetParams.PacketType = PACKET_TYPE_LORA;
  271. packetParams.Params.LoRa.PreambleLength = 12;
  272. packetParams.Params.LoRa.HeaderType = LORA_PACKET_VARIABLE_LENGTH;
  273. packetParams.Params.LoRa.PayloadLength = BUFFER_SIZE;
  274. packetParams.Params.LoRa.CrcMode = LORA_CRC_ON;
  275. packetParams.Params.LoRa.InvertIQ = LORA_IQ_NORMAL;
  276. }
  277. else if(rfModuleMode == RF_MM_MODE_GFSK)
  278. {
  279. modulationParams.PacketType = PACKET_TYPE_GFSK;
  280. modulationParams.Params.Gfsk.BitrateBandwidth = GFS_BLE_BR_0_125_BW_0_3;
  281. modulationParams.Params.Gfsk.ModulationIndex = GFS_BLE_MOD_IND_1_00;
  282. modulationParams.Params.Gfsk.ModulationShaping = RADIO_MOD_SHAPING_BT_1_0;
  283. packetParams.PacketType = PACKET_TYPE_GFSK;
  284. packetParams.Params.Gfsk.PreambleLength = PREAMBLE_LENGTH_24_BITS;
  285. packetParams.Params.Gfsk.SyncWordLength = GFS_SYNCWORD_LENGTH_5_BYTE;
  286. packetParams.Params.Gfsk.SyncWordMatch = RADIO_RX_MATCH_SYNCWORD_1_2_3;
  287. packetParams.Params.Gfsk.HeaderType = RADIO_PACKET_FIXED_LENGTH;
  288. packetParams.Params.Gfsk.PayloadLength = BUFFER_SIZE;
  289. packetParams.Params.Gfsk.CrcLength = RADIO_CRC_OFF;
  290. packetParams.Params.Gfsk.Whitening = RADIO_WHITENING_OFF;
  291. // packetParams.PacketType = PACKET_TYPE_GFSK;
  292. // packetParams.Params.Gfsk.PreambleLength = PREAMBLE_LENGTH_32_BITS;
  293. // packetParams.Params.Gfsk.SyncWordLength = GFS_SYNCWORD_LENGTH_5_BYTE;
  294. // packetParams.Params.Gfsk.SyncWordMatch = RADIO_RX_MATCH_SYNCWORD_1;
  295. // packetParams.Params.Gfsk.HeaderType = RADIO_PACKET_VARIABLE_LENGTH;
  296. // packetParams.Params.Gfsk.PayloadLength = BUFFER_SIZE;
  297. // packetParams.Params.Gfsk.CrcLength = RADIO_CRC_3_BYTES;
  298. // packetParams.Params.Gfsk.Whitening = RADIO_WHITENING_ON;
  299. }
  300. else if(rfModuleMode == RF_MM_MODE_FLRC)
  301. {
  302. modulationParams.PacketType = PACKET_TYPE_FLRC;
  303. modulationParams.Params.Flrc.BitrateBandwidth = FLRC_BR_0_325_BW_0_3;
  304. modulationParams.Params.Flrc.CodingRate = FLRC_CR_1_2;
  305. modulationParams.Params.Flrc.ModulationShaping = RADIO_MOD_SHAPING_BT_1_0;
  306. packetParams.PacketType = PACKET_TYPE_FLRC;
  307. packetParams.Params.Flrc.PreambleLength = PREAMBLE_LENGTH_32_BITS;
  308. packetParams.Params.Flrc.SyncWordLength = FLRC_SYNCWORD_LENGTH_4_BYTE;
  309. packetParams.Params.Flrc.SyncWordMatch = RADIO_RX_MATCH_SYNCWORD_1;
  310. packetParams.Params.Flrc.HeaderType = RADIO_PACKET_VARIABLE_LENGTH;
  311. packetParams.Params.Flrc.PayloadLength = BUFFER_SIZE;
  312. packetParams.Params.Flrc.CrcLength = RADIO_CRC_3_BYTES;
  313. packetParams.Params.Flrc.Whitening = RADIO_WHITENING_OFF;
  314. }
  315. else
  316. {
  317. modulationParams.PacketType = PACKET_TYPE_FLRC;
  318. modulationParams.Params.Flrc.BitrateBandwidth = FLRC_BR_0_325_BW_0_3;
  319. modulationParams.Params.Flrc.CodingRate = FLRC_CR_1_2;
  320. modulationParams.Params.Flrc.ModulationShaping = RADIO_MOD_SHAPING_BT_1_0;
  321. packetParams.PacketType = PACKET_TYPE_FLRC;
  322. packetParams.Params.Flrc.PreambleLength = PREAMBLE_LENGTH_32_BITS;
  323. packetParams.Params.Flrc.SyncWordLength = FLRC_SYNCWORD_LENGTH_4_BYTE;
  324. packetParams.Params.Flrc.SyncWordMatch = RADIO_RX_MATCH_SYNCWORD_1;
  325. packetParams.Params.Flrc.HeaderType = RADIO_PACKET_VARIABLE_LENGTH;
  326. packetParams.Params.Flrc.PayloadLength = BUFFER_SIZE;
  327. packetParams.Params.Flrc.CrcLength = RADIO_CRC_3_BYTES;
  328. packetParams.Params.Flrc.Whitening = RADIO_WHITENING_OFF;
  329. }
  330. // #elif defined( MODE_GFSK )
  331. // #elif defined( MODE_LORA )
  332. // #elif defined( MODE_FLRC )
  333. // #else
  334. // #error "Please select the mode of operation for the Ping Ping demo"
  335. // #endif
  336. Radio.SetStandby( STDBY_RC );
  337. Radio.SetPacketType( modulationParams.PacketType );
  338. Radio.SetModulationParams( &modulationParams );
  339. Radio.SetPacketParams( &packetParams );
  340. Radio.SetRfFrequency( RF_FREQUENCY );//频点设置
  341. Radio.SetBufferBaseAddresses( 0x00, 0x00 );
  342. Radio.SetTxParams( TX_OUTPUT_POWER, RADIO_RAMP_02_US );//功率设置
  343. // Radio.SetInterruptMode();
  344. Radio.SetPollingMode();
  345. if(rfModuleMode == RF_MM_MODE_GFSK)
  346. {
  347. //接收灵敏度测试
  348. Radio.SetSyncWord( 1, ( uint8_t[] ){ 0xDD, 0xA0, 0x96, 0x69, 0xDD } );
  349. }
  350. if(rfModuleMode == RF_MM_MODE_BLE)
  351. {
  352. // only used in GENERIC and BLE mode
  353. Radio.WriteRegister(0x9c7, 0x55 );
  354. Radio.WriteRegister(0x9c8, 0x55 );
  355. Radio.WriteRegister(0x9c9, 0x55 );
  356. //Radio.WriteRegister( 0x9c5, 0x33 );
  357. Radio.SetBleAdvertizerAccessAddress( );
  358. Radio.SetWhiteningSeed( 0x33 );
  359. ble_header_adv.Fields.length = PINGPONGSIZE + 2;
  360. ble_header_adv.Fields.pduType = 2;
  361. }
  362. AppState = APP_LOWPOWER;
  363. /**-------------------------radio init end----------------------------------**/
  364. RF_EXT_PA_TO_IDLE();
  365. if ((rfRxCallBack )agr1_ptr)
  366. {
  367. rxCb = (rfRxCallBack )agr1_ptr;
  368. }
  369. rf_handle = 0xe5;
  370. }
  371. /**
  372. * @brief 射频底层执行程序
  373. * 要放在主循环中执行
  374. *
  375. */
  376. void myRadio_process(void)
  377. {
  378. if (rf_handle == 0)
  379. {
  380. return;
  381. }
  382. SX1280ProcessIrqs();
  383. }
  384. /**
  385. * @brief 退出射频进入休眠
  386. *
  387. */
  388. void myRadio_abort(void)
  389. {
  390. if (rf_handle == 0)
  391. {
  392. return;
  393. }
  394. RF_EXT_PA_TO_IDLE();
  395. SleepParams_t SleepParams;
  396. SleepParams.DataBufferRetention = 1;
  397. SleepParams.DataRamRetention = 1;
  398. SleepParams.InstructionRamRetention = 1;
  399. SleepParams.WakeUpRTC = 0;
  400. // Radio.SetSleep( SleepParams );
  401. Radio.SetStandby( STDBY_RC );
  402. }
  403. /**
  404. * @brief 获取射频工作中心频率
  405. *
  406. * @return uint32_t
  407. */
  408. uint32_t myRadio_getFrequency(void)
  409. {
  410. if (rf_handle == 0)
  411. {
  412. return 0;
  413. }
  414. return rfFrequence;
  415. }
  416. /**
  417. * @brief 设置射频工作中心频率
  418. *
  419. * @param freq
  420. * 具体频点,单位:Hz
  421. */
  422. void myRadio_setFrequency(uint32_t freq)
  423. {
  424. if (rf_handle == 0)
  425. {
  426. return;
  427. }
  428. rfFrequence = freq;
  429. Radio.SetRfFrequency(freq);
  430. }
  431. /**
  432. * @brief 获取发射功率
  433. *
  434. * @return int8_t
  435. */
  436. int8_t myRadio_getTxPower(void)
  437. {
  438. if (rf_handle == 0)
  439. {
  440. return 0;
  441. }
  442. return rfTxPower;
  443. }
  444. /**
  445. * @brief 设置发射功率
  446. *
  447. * @param power
  448. * 单位:dbm
  449. */
  450. void myRadio_setTxPower(int8_t power)
  451. {
  452. if (rf_handle == 0)
  453. {
  454. return;
  455. }
  456. rfTxPower = power;
  457. Radio.SetTxParams( rfTxPower, RADIO_RAMP_02_US );
  458. }
  459. /**
  460. * 获取射频波特率
  461. * @param : br->
  462. */
  463. uint32_t myRadio_getBaudrate(void)
  464. {
  465. if (rf_handle == 0)
  466. {
  467. return 0;
  468. }
  469. return rfBaudrate;
  470. }
  471. /**
  472. * 设置射频波特率
  473. * @param : br->
  474. */
  475. void myRadio_setBaudrate(uint32_t br)
  476. {
  477. if (rf_handle == 0)
  478. {
  479. return;
  480. }
  481. rfBaudrate = br;
  482. if(rfModuleMode == RF_MM_MODE_GFSK)
  483. {
  484. }
  485. else if (rfModuleMode == RF_MM_MODE_LORA)
  486. {
  487. modulationParams.Params.LoRa.SpreadingFactor = loraBaudrateFrame[rfBaudrate].SpreadingFactor;//
  488. modulationParams.Params.LoRa.Bandwidth = loraBaudrateFrame[rfBaudrate].SignalBw;
  489. modulationParams.Params.LoRa.CodingRate = loraBaudrateFrame[rfBaudrate].ErrorCoding;
  490. }
  491. else if (rfModuleMode == RF_MM_MODE_FLRC)
  492. {
  493. modulationParams.Params.Flrc.BitrateBandwidth = fastLoraConfigList[rfBaudrate].BitrateBandwidth;//
  494. modulationParams.Params.Flrc.CodingRate = fastLoraConfigList[rfBaudrate].CodingRate;
  495. modulationParams.Params.Flrc.ModulationShaping = fastLoraConfigList[rfBaudrate].ModulationShaping;
  496. }
  497. else
  498. {
  499. modulationParams.Params.Flrc.BitrateBandwidth = fastLoraConfigList[rfBaudrate].BitrateBandwidth;//
  500. modulationParams.Params.Flrc.CodingRate = fastLoraConfigList[rfBaudrate].CodingRate;
  501. modulationParams.Params.Flrc.ModulationShaping = fastLoraConfigList[rfBaudrate].ModulationShaping;
  502. }
  503. Radio.SetModulationParams( &modulationParams );
  504. }
  505. /**
  506. * @brief 设置模组型号
  507. *
  508. * @param type
  509. */
  510. void myRadio_setChipType(uint8_t type)
  511. {
  512. chipType = type;
  513. }
  514. /**
  515. * @brief 获取模组型号
  516. *
  517. * @return uint8_t
  518. */
  519. uint8_t myRadio_getChipType(void)
  520. {
  521. return chipType;
  522. }
  523. int16_t myRadio_getRssi(void)
  524. {
  525. return Radio.GetRssiInst();;
  526. }
  527. /**
  528. * @brief 无线发送数据包
  529. *
  530. * @param packet
  531. */
  532. void myRadio_transmit(rfTxPacket_ts *packet)
  533. {
  534. if (rf_handle == 0)
  535. {
  536. return;
  537. }
  538. RF_EXT_PA_TO_TX();
  539. if (Radio.GetOpMode() == MODE_SLEEP)
  540. {
  541. myRadio_delay(1);
  542. }
  543. TickTime_t TickTime ={ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE };
  544. Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
  545. if(rfModuleMode == RF_MM_MODE_GFSK)
  546. {
  547. packetParams.Params.Gfsk.PayloadLength = packet->len;
  548. }
  549. else if (rfModuleMode == RF_MM_MODE_LORA)
  550. {
  551. packetParams.Params.LoRa.PayloadLength = packet->len;
  552. }
  553. else if (rfModuleMode == RF_MM_MODE_FLRC)
  554. {
  555. packetParams.Params.Flrc.PayloadLength = packet->len;
  556. }
  557. else
  558. {
  559. packetParams.Params.Flrc.PayloadLength = packet->len;
  560. }
  561. Radio.SetPacketParams( &packetParams );
  562. Radio.SendPayload( packet->payload, packet->len, TickTime);
  563. }
  564. /**
  565. * @brief 进入无线接收
  566. *
  567. */
  568. void myRadio_receiver(void)
  569. {
  570. if (rf_handle == 0)
  571. {
  572. return;
  573. }
  574. RF_EXT_PA_TO_RX();
  575. TickTime_t TickTime ={ RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE };
  576. Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
  577. Radio.SetRx( TickTime );
  578. }
  579. void myRadio_setCtrl(controlMode_te mode, uint32_t value)
  580. {
  581. if (rf_handle == 0)
  582. {
  583. return;
  584. }
  585. myRadio_setRfModuleMode(RF_MM_MODE_GFSK);
  586. myRadio_init(0, 0);
  587. switch (mode)
  588. {
  589. case RADIO_EXT_CONTROL_TX_UNMODULATED:
  590. {
  591. RF_EXT_PA_TO_TX();
  592. Radio.SetRfFrequency(rfFrequence);
  593. Radio.SetTxParams( rfTxPower, RADIO_RAMP_02_US );
  594. Radio.SetTxContinuousWave();
  595. }
  596. break;
  597. case RADIO_EXT_CONTROL_TX_MODULATED:
  598. {
  599. RF_EXT_PA_TO_TX();
  600. Radio.SetRfFrequency(rfFrequence);
  601. Radio.SetTxParams( rfTxPower, RADIO_RAMP_02_US );
  602. Radio.SetTxContinuousWave();
  603. }
  604. break;
  605. case RADIO_EXT_CONTROL_RX_SENSITIVITY:
  606. {
  607. RF_EXT_PA_TO_RX();
  608. myRadio_receiver();
  609. }
  610. break;
  611. default:
  612. break;
  613. }
  614. }
  615. /**-------------------------radio funtion----------------------------------**/
  616. void OnTxDone( void )
  617. {
  618. AppState = APP_TX;
  619. RF_EXT_PA_TO_IDLE();
  620. if (rxCb)
  621. {
  622. rxCb(TX_STA_SECCESS, rfRxPacket);
  623. }
  624. }
  625. void OnRxDone( void )
  626. {
  627. AppState = APP_RX;
  628. memset(rfRxPacket.payload, 0, sizeof(rfRxPacket.payload));
  629. Radio.GetPayload( rfRxPacket.payload, &rfRxPacket.len, sizeof(rfRxPacket.payload) );
  630. rfRxPacket.rssi = Radio.GetRssiInst();
  631. if (rxCb)
  632. {
  633. rxCb(RX_STA_SECCESS, rfRxPacket);
  634. }
  635. }
  636. void OnTxTimeout( void )
  637. {
  638. AppState = APP_TX_TIMEOUT;
  639. }
  640. void OnRxTimeout( void )
  641. {
  642. rfRxPacket_ts rfRxPacket;
  643. AppState = APP_RX_TIMEOUT;
  644. if (rxCb)
  645. {
  646. rxCb(RX_STA_TIMEOUT, rfRxPacket);
  647. }
  648. }
  649. void OnRxError( IrqErrorCode_t errorCode )
  650. {
  651. rfRxPacket_ts rfRxPacket;
  652. AppState = APP_RX_ERROR;
  653. if (rxCb)
  654. {
  655. rxCb(RX_STA_PAYLOAD_ERROR, rfRxPacket);
  656. }
  657. }
  658. void OnRangingDone( IrqRangingCode_t val )
  659. {
  660. }
  661. void OnCadDone( bool channelActivityDetected )
  662. {
  663. }
  664. /**-------------------------radio funtion end----------------------------------**/