myRadio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. if ((Radio.GetOpMode() != MODE_TX) || (Radio.GetOpMode() != MODE_RX) || (Radio.GetOpMode() != MODE_CAD))
  383. {
  384. return;
  385. }
  386. SX1280ProcessIrqs();
  387. }
  388. /**
  389. * @brief 退出射频进入休眠
  390. *
  391. */
  392. void myRadio_abort(void)
  393. {
  394. if (rf_handle == 0)
  395. {
  396. return;
  397. }
  398. RF_EXT_PA_TO_IDLE();
  399. SleepParams_t SleepParams;
  400. SleepParams.DataBufferRetention = 1;
  401. SleepParams.DataRamRetention = 1;
  402. SleepParams.InstructionRamRetention = 1;
  403. SleepParams.WakeUpRTC = 0;
  404. // Radio.SetSleep( SleepParams );
  405. Radio.SetStandby( STDBY_RC );
  406. }
  407. /**
  408. * @brief 获取射频工作中心频率
  409. *
  410. * @return uint32_t
  411. */
  412. uint32_t myRadio_getFrequency(void)
  413. {
  414. if (rf_handle == 0)
  415. {
  416. return 0;
  417. }
  418. return rfFrequence;
  419. }
  420. /**
  421. * @brief 设置射频工作中心频率
  422. *
  423. * @param freq
  424. * 具体频点,单位:Hz
  425. */
  426. void myRadio_setFrequency(uint32_t freq)
  427. {
  428. if (rf_handle == 0)
  429. {
  430. return;
  431. }
  432. rfFrequence = freq;
  433. Radio.SetRfFrequency(freq);
  434. }
  435. /**
  436. * @brief 获取发射功率
  437. *
  438. * @return int8_t
  439. */
  440. int8_t myRadio_getTxPower(void)
  441. {
  442. if (rf_handle == 0)
  443. {
  444. return 0;
  445. }
  446. return rfTxPower;
  447. }
  448. /**
  449. * @brief 设置发射功率
  450. *
  451. * @param power
  452. * 单位:dbm
  453. */
  454. void myRadio_setTxPower(int8_t power)
  455. {
  456. if (rf_handle == 0)
  457. {
  458. return;
  459. }
  460. rfTxPower = power;
  461. Radio.SetTxParams( rfTxPower, RADIO_RAMP_02_US );
  462. }
  463. /**
  464. * 获取射频波特率
  465. * @param : br->
  466. */
  467. uint32_t myRadio_getBaudrate(void)
  468. {
  469. if (rf_handle == 0)
  470. {
  471. return 0;
  472. }
  473. return rfBaudrate;
  474. }
  475. /**
  476. * 设置射频波特率
  477. * @param : br->
  478. */
  479. void myRadio_setBaudrate(uint32_t br)
  480. {
  481. if (rf_handle == 0)
  482. {
  483. return;
  484. }
  485. rfBaudrate = br;
  486. if(rfModuleMode == RF_MM_MODE_GFSK)
  487. {
  488. }
  489. else if (rfModuleMode == RF_MM_MODE_LORA)
  490. {
  491. modulationParams.Params.LoRa.SpreadingFactor = loraBaudrateFrame[rfBaudrate].SpreadingFactor;//
  492. modulationParams.Params.LoRa.Bandwidth = loraBaudrateFrame[rfBaudrate].SignalBw;
  493. modulationParams.Params.LoRa.CodingRate = loraBaudrateFrame[rfBaudrate].ErrorCoding;
  494. }
  495. else if (rfModuleMode == RF_MM_MODE_FLRC)
  496. {
  497. modulationParams.Params.Flrc.BitrateBandwidth = fastLoraConfigList[rfBaudrate].BitrateBandwidth;//
  498. modulationParams.Params.Flrc.CodingRate = fastLoraConfigList[rfBaudrate].CodingRate;
  499. modulationParams.Params.Flrc.ModulationShaping = fastLoraConfigList[rfBaudrate].ModulationShaping;
  500. }
  501. else
  502. {
  503. modulationParams.Params.Flrc.BitrateBandwidth = fastLoraConfigList[rfBaudrate].BitrateBandwidth;//
  504. modulationParams.Params.Flrc.CodingRate = fastLoraConfigList[rfBaudrate].CodingRate;
  505. modulationParams.Params.Flrc.ModulationShaping = fastLoraConfigList[rfBaudrate].ModulationShaping;
  506. }
  507. Radio.SetModulationParams( &modulationParams );
  508. }
  509. /**
  510. * @brief 设置模组型号
  511. *
  512. * @param type
  513. */
  514. void myRadio_setChipType(uint8_t type)
  515. {
  516. chipType = type;
  517. }
  518. /**
  519. * @brief 获取模组型号
  520. *
  521. * @return uint8_t
  522. */
  523. uint8_t myRadio_getChipType(void)
  524. {
  525. return chipType;
  526. }
  527. int16_t myRadio_getRssi(void)
  528. {
  529. if (Radio.GetOpMode() == MODE_RX)
  530. {
  531. return Radio.GetRssiInst();
  532. }
  533. else
  534. {
  535. return -127;
  536. }
  537. }
  538. /**
  539. * @brief 无线发送数据包
  540. *
  541. * @param packet
  542. */
  543. void myRadio_transmit(rfTxPacket_ts *packet)
  544. {
  545. if (rf_handle == 0)
  546. {
  547. return;
  548. }
  549. RF_EXT_PA_TO_TX();
  550. if (Radio.GetOpMode() == MODE_SLEEP)
  551. {
  552. myRadio_delay(1);
  553. }
  554. TickTime_t TickTime ={ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE };
  555. Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
  556. if(rfModuleMode == RF_MM_MODE_GFSK)
  557. {
  558. packetParams.Params.Gfsk.PayloadLength = packet->len;
  559. }
  560. else if (rfModuleMode == RF_MM_MODE_LORA)
  561. {
  562. packetParams.Params.LoRa.PayloadLength = packet->len;
  563. }
  564. else if (rfModuleMode == RF_MM_MODE_FLRC)
  565. {
  566. packetParams.Params.Flrc.PayloadLength = packet->len;
  567. }
  568. else
  569. {
  570. packetParams.Params.Flrc.PayloadLength = packet->len;
  571. }
  572. Radio.SetPacketParams( &packetParams );
  573. Radio.SendPayload( packet->payload, packet->len, TickTime);
  574. }
  575. /**
  576. * @brief 进入无线接收
  577. *
  578. */
  579. void myRadio_receiver(void)
  580. {
  581. if (rf_handle == 0)
  582. {
  583. return;
  584. }
  585. RF_EXT_PA_TO_RX();
  586. TickTime_t TickTime ={ RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE };
  587. Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
  588. Radio.SetRx( TickTime );
  589. }
  590. void myRadio_setCtrl(controlMode_te mode, uint32_t value)
  591. {
  592. if (rf_handle == 0)
  593. {
  594. return;
  595. }
  596. myRadio_setRfModuleMode(RF_MM_MODE_GFSK);
  597. myRadio_init(0, 0);
  598. switch (mode)
  599. {
  600. case RADIO_EXT_CONTROL_TX_UNMODULATED:
  601. {
  602. RF_EXT_PA_TO_TX();
  603. Radio.SetRfFrequency(rfFrequence);
  604. Radio.SetTxParams( rfTxPower, RADIO_RAMP_02_US );
  605. Radio.SetTxContinuousWave();
  606. }
  607. break;
  608. case RADIO_EXT_CONTROL_TX_MODULATED:
  609. {
  610. RF_EXT_PA_TO_TX();
  611. Radio.SetRfFrequency(rfFrequence);
  612. Radio.SetTxParams( rfTxPower, RADIO_RAMP_02_US );
  613. Radio.SetTxContinuousWave();
  614. }
  615. break;
  616. case RADIO_EXT_CONTROL_RX_SENSITIVITY:
  617. {
  618. RF_EXT_PA_TO_RX();
  619. myRadio_receiver();
  620. }
  621. break;
  622. default:
  623. break;
  624. }
  625. }
  626. /**-------------------------radio funtion----------------------------------**/
  627. void OnTxDone( void )
  628. {
  629. AppState = APP_TX;
  630. RF_EXT_PA_TO_IDLE();
  631. if (rxCb)
  632. {
  633. rxCb(TX_STA_SECCESS, rfRxPacket);
  634. }
  635. }
  636. void OnRxDone( void )
  637. {
  638. AppState = APP_RX;
  639. memset(rfRxPacket.payload, 0, sizeof(rfRxPacket.payload));
  640. Radio.GetPayload( rfRxPacket.payload, &rfRxPacket.len, sizeof(rfRxPacket.payload) );
  641. rfRxPacket.rssi = Radio.GetRssiInst();
  642. if (rxCb)
  643. {
  644. rxCb(RX_STA_SECCESS, rfRxPacket);
  645. }
  646. }
  647. void OnTxTimeout( void )
  648. {
  649. AppState = APP_TX_TIMEOUT;
  650. }
  651. void OnRxTimeout( void )
  652. {
  653. rfRxPacket_ts rfRxPacket;
  654. AppState = APP_RX_TIMEOUT;
  655. if (rxCb)
  656. {
  657. rxCb(RX_STA_TIMEOUT, rfRxPacket);
  658. }
  659. }
  660. void OnRxError( IrqErrorCode_t errorCode )
  661. {
  662. rfRxPacket_ts rfRxPacket;
  663. AppState = APP_RX_ERROR;
  664. if (rxCb)
  665. {
  666. rxCb(RX_STA_PAYLOAD_ERROR, rfRxPacket);
  667. }
  668. }
  669. void OnRangingDone( IrqRangingCode_t val )
  670. {
  671. }
  672. void OnCadDone( bool channelActivityDetected )
  673. {
  674. }
  675. /**-------------------------radio funtion end----------------------------------**/