SX1276.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /**
  2. * Author Wojciech Domski <Wojciech.Domski@gmail.com>
  3. * www: www.Domski.pl
  4. *
  5. * work based on DORJI.COM sample code and
  6. * https://github.com/realspinner/SX1276_LoRa
  7. */
  8. #include "SX1276.h"
  9. #include <string.h>
  10. //////////////////////////////////
  11. // logic
  12. //////////////////////////////////
  13. __weak void SX1276_hw_init(SX1276_hw_t * hw) {
  14. SX1276_hw_SetNSS(hw, 1);
  15. HAL_GPIO_WritePin(hw->reset.port, hw->reset.pin, GPIO_PIN_SET);
  16. }
  17. __weak void SX1276_hw_SetNSS(SX1276_hw_t * hw, int value) {
  18. HAL_GPIO_WritePin(hw->nss.port, hw->nss.pin,
  19. (value == 1) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  20. }
  21. __weak void SX1276_hw_Reset(SX1276_hw_t * hw) {
  22. SX1276_hw_SetNSS(hw, 1);
  23. HAL_GPIO_WritePin(hw->reset.port, hw->reset.pin, GPIO_PIN_RESET);
  24. SX1276_hw_DelayMs(1);
  25. HAL_GPIO_WritePin(hw->reset.port, hw->reset.pin, GPIO_PIN_SET);
  26. SX1276_hw_DelayMs(100);
  27. }
  28. #if 0 // PYJ.2019.04.01_BEGIN --
  29. __weak void SX1276_hw_SPICommand(SX1276_hw_t * hw, uint8_t cmd) {
  30. SX1276_hw_SetNSS(hw, 0);
  31. HAL_SPI_Transmit(hw->spi, &cmd, 1, 1000);
  32. while (HAL_SPI_GetState(hw->spi) != HAL_SPI_STATE_READY)
  33. ;
  34. }
  35. #endif // PYJ.2019.04.01_END --
  36. void SX1276_hw_SPICommand(SX1276_hw_t * hw, uint8_t cmd) {
  37. SX1276_hw_SetNSS(hw, 0);
  38. BLUECELL_SPI_Transmit(cmd);
  39. }
  40. #if 0 // PYJ.2019.04.01_BEGIN --
  41. __weak uint8_t SX1276_hw_SPIReadByte(SX1276_hw_t * hw) {
  42. uint8_t txByte = 0x00;
  43. uint8_t rxByte = 0x00;
  44. SX1276_hw_SetNSS(hw, 0);
  45. HAL_SPI_TransmitReceive(hw->spi, &txByte, &rxByte, 1, 1000);
  46. return rxByte;
  47. }
  48. #endif // PYJ.2019.04.01_END --
  49. uint8_t SX1276_hw_SPIReadByte(SX1276_hw_t * hw) {
  50. //uint8_t txByte = 0x00;
  51. uint8_t rxByte = 0x00;
  52. SX1276_hw_SetNSS(hw, 0);
  53. // HAL_SPI_TransmitReceive(hw->spi, &txByte, &rxByte, 1, 1000);
  54. rxByte = SpiRead();
  55. return rxByte;
  56. }
  57. __weak void SX1276_hw_DelayMs(uint32_t msec) {
  58. HAL_Delay(msec);
  59. }
  60. __weak int SX1276_hw_GetDIO0(SX1276_hw_t * hw) {
  61. return (HAL_GPIO_ReadPin(hw->dio0.port, hw->dio0.pin) == GPIO_PIN_SET);
  62. }
  63. //////////////////////////////////
  64. // logic
  65. //////////////////////////////////
  66. uint8_t SX1276_SPIRead(SX1276_t * module, uint8_t addr) {
  67. uint8_t tmp;
  68. SX1276_hw_SPICommand(module->hw, addr);
  69. tmp = SX1276_hw_SPIReadByte(module->hw);
  70. SX1276_hw_SetNSS(module->hw, 1);
  71. return tmp;
  72. }
  73. uint8_t SX1276_SPIIDRead(SX1276_t * module, uint8_t addr) {
  74. uint8_t tmp;
  75. SX1276_hw_SPICommand(module->hw, addr);
  76. tmp = SX1276_hw_SPIReadByte(module->hw);
  77. SX1276_hw_SetNSS(module->hw, 1);
  78. return tmp;
  79. }
  80. void SX1276_SPIWrite(SX1276_t * module, uint8_t addr, uint8_t cmd) {
  81. SX1276_hw_SetNSS(module->hw, 0);
  82. SX1276_hw_SPICommand(module->hw, addr | 0x80);
  83. SX1276_hw_SPICommand(module->hw, cmd);
  84. // SPIGPIOTxRx(addr | 0x80,cmd);
  85. SX1276_hw_SetNSS(module->hw, 1);
  86. }
  87. void SX1276_SPIBurstRead(SX1276_t * module, uint8_t addr, uint8_t* rxBuf,
  88. uint8_t length) {
  89. uint8_t i;
  90. if (length <= 1) {
  91. return;
  92. } else {
  93. SX1276_hw_SetNSS(module->hw, 0);
  94. SX1276_hw_SPICommand(module->hw, addr);
  95. //printf("Test Data:");
  96. for (i = 0; i < length; i++) {
  97. rxBuf[i] = SX1276_hw_SPIReadByte(module->hw);
  98. //printf("%02x ",rxBuf[i]);
  99. }
  100. //printf("\n");
  101. SX1276_hw_SetNSS(module->hw, 1);
  102. }
  103. }
  104. void SX1276_SPIBurstWrite(SX1276_t * module, uint8_t addr, uint8_t* txBuf,
  105. uint8_t length) {
  106. uint8_t i;
  107. if (length <= 1) {
  108. return;
  109. } else {
  110. SX1276_hw_SetNSS(module->hw, 0);
  111. SX1276_hw_SPICommand(module->hw, addr | 0x80);
  112. // printf("Test Data:");
  113. for (i = 0; i < length; i++) {
  114. SX1276_hw_SPICommand(module->hw, txBuf[i]);
  115. // printf("%02x ",txBuf[i]);
  116. }
  117. // printf("\n");
  118. SX1276_hw_SetNSS(module->hw, 1);
  119. }
  120. }
  121. /*
  122. RegPaConfig - 0x09
  123. PaSelect Mode Power Range Pout Formula
  124. 0 PA_HF or PA_LF on RFO_HF or RFO_LF -4 to +15dBm Pout=Pmax-(15-OutputPower), Pmax=10.8+0.6*MaxPower [dBm]
  125. 1 PA_HP on PA_BOOST, any frequency +2 to +17dBm Pout=17-(15-OutputPower) [dBm]
  126. PA_LF option doesn't seem to transmit anything. Not connected on these boards, perhaps?
  127. Use only PA_BOOST mode.
  128. PA_LF Pmax
  129. Pout 0 1 2 3 4 5 6 7
  130. 0 -4.2 -3.6 -3 -2.4 -1.8 -1.2 -0.6 0
  131. 1 -3.2 -2.6 -2 -1.4 -0.8 -0.2 0.4 1
  132. 2 -2.2 -1.6 -1 -0.4 0.2 0.8 1.4 2
  133. 3 -1.2 -0.6 0 0.6 1.2 1.8 2.4 3
  134. 4 -0.2 0.4 1 1.6 2.2 2.8 3.4 4
  135. 5 0.8 1.4 2 2.6 3.2 3.8 4.4 5
  136. 6 1.8 2.4 3 3.6 4.2 4.8 5.4 6
  137. 7 2.8 3.4 4 4.6 5.2 5.8 6.4 7
  138. 8 3.8 4.4 5 5.6 6.2 6.8 7.4 8
  139. 9 4.8 5.4 6 6.6 7.2 7.8 8.4 9
  140. 10 5.8 6.4 7 7.6 8.2 8.8 9.4 10
  141. 11 6.8 7.4 8 8.6 9.2 9.8 10.4 11
  142. 12 7.8 8.4 9 9.6 10.2 10.8 11.4 12
  143. 13 8.8 9.4 10 10.6 11.2 11.8 12.4 13
  144. 14 9.8 10.4 11 11.6 12.2 12.8 13.4 14
  145. 15 10.8 11.4 12 12.6 13.2 13.8 14.4 15 [dBm]
  146. PA_BOOST
  147. Pout OutputPower
  148. 0 2 dBm
  149. 1 3 dBm
  150. 2 4 dBm
  151. 3 5 dBm
  152. 4 6 dBm
  153. 5 7 dBm
  154. 6 8 dBm
  155. 7 9 dBm
  156. 8 10 dBm
  157. 9 11 dBm
  158. 10 12 dBm
  159. 11 13 dBm
  160. 12 14 dBm
  161. 13 15 dBm
  162. 14 16 dBm
  163. 15 17 dBm
  164. PaSelect
  165. 0 RFO pin. Maximum power of +14 dBm
  166. 1 PA_BOOST pin. Maximum power of +20 dBm
  167. MaxPower
  168. Select max output power: Pmax=10.8+0.6*MaxPower [dBm]
  169. OutputPower
  170. Pout=Pmax-(15-OutputPower) if PaSelect = 0 (RFO pins)
  171. Pout=17-(15-OutputPower) if PaSelect = 1 (PA_BOOST pin)
  172. PA_HF and PA_LF are high efficiency amplifiers capable of yielding RF power programmable in 1 dB steps
  173. from -4 to +14dBm directly into a 50 ohm load with low current consumption. PA_LF covers the lower bands
  174. (up to 525 MHz), whilst PA_HF will cover the upper bands (from 860 MHz).
  175. PA_HP (High Power), connected to the PA_BOOST pin, covers all frequency bands that the chip addresses.
  176. It permits continuous operation at up to +17 dBm and duty cycled operation at up to +20dBm.
  177. RegOcp - 0x0B
  178. OcpTrim IMAX Imax Formula
  179. 0 to 15 45 to 120 mA 45 + 5*OcpTrim [mA]
  180. 16 to 27 130 to 240 mA -30 + 10*OcpTrim [mA]
  181. 27+ 240 mA 240 mA
  182. The power amplifiers of RFM95/96/97/98(W) are protected against current over supply in adverse RF load
  183. conditions by the over current protection block. The current limiter value is controlled by the OcpTrim
  184. bits in RegOcp.
  185. Imax sets a limit on the current drain of the Power Amplifier only, hence the maximum current drain of the
  186. RFM96/77/78 is equal to Imax + IFS.
  187. Default 100mA changed to 240mA.
  188. */
  189. void SX1276_set_power(SX1276_t * module)
  190. {
  191. // SX1276_SPIWrite(LR_RegPaConfig, (PaSelect << 7) | ((MaxPower & 0x07) << 4) | (OutputPower & 0x0F));
  192. SX1276_SPIWrite(module,LR_RegPaConfig, (module->LoRa_Pa_boost << 7) | ((0x07) << 4) | (module->power & 0x0F));
  193. //RFO -> 0 ~ 15 / PA_BOOST -> 2 ~ 17
  194. }
  195. void SX1276_defaultConfig(SX1276_t * module) {
  196. SX1276_config(module, module->frequency, module->power, module->LoRa_Rate,
  197. module->LoRa_BW,module->LoRa_Lna,module->LoRa_Pa_boost);
  198. }
  199. void SX1276_config(SX1276_t * module, uint8_t frequency, uint8_t power,
  200. uint8_t LoRa_Rate, uint8_t LoRa_BW,uint8_t LoRa_Lna,uint8_t LoRa_PaBoost) {
  201. SX1276_sleep(module); //Change modem mode Must in Sleep mode
  202. SX1276_hw_DelayMs(15);
  203. SX1276_entryLoRa(module);
  204. //SX1276_SPIWrite(module, 0x5904); //?? Change digital regulator form 1.6V to 1.47V: see errata note
  205. SX1276_SPIBurstWrite(module, LR_RegFrMsb,
  206. (uint8_t*) SX1276_Frequency[frequency], 3); //setting frequency parameter
  207. //setting base parameter
  208. SX1276_SPIWrite(module, LR_RegPaConfig, SX1276_Power[power]); //Setting output power parameter
  209. SX1276_SPIWrite(module, LR_RegOcp, 0x0B); //RegOcp,Close Ocp
  210. // SX1276_SPIWrite(module, LR_RegLna, 0x20); //RegLNA,High & LNA Enable
  211. SX1276_set_power(module);
  212. SX1276_SPIWrite(module, LR_RegLna, LoRa_Lna); //RegLNA,High & LNA Enable
  213. if (SX1276_SpreadFactor[LoRa_Rate] == 6) { //SFactor=6
  214. uint8_t tmp;
  215. SX1276_SPIWrite(module,
  216. LR_RegModemConfig1,
  217. ((SX1276_LoRaBandwidth[LoRa_BW] << 4) + (SX1276_CR << 1) + 0x01)); //Implicit Enable CRC Enable(0x02) & Error Coding rate 4/5(0x01), 4/6(0x02), 4/7(0x03), 4/8(0x04)
  218. SX1276_SPIWrite(module,LR_RegModemConfig2,((SX1276_SpreadFactor[LoRa_Rate] << 4) + (SX1276_CRC << 2) + 0x03));
  219. tmp = SX1276_SPIRead(module, 0x31);
  220. tmp &= 0xF8;
  221. tmp |= 0x05;
  222. SX1276_SPIWrite(module, 0x31, tmp);
  223. SX1276_SPIWrite(module, 0x37, 0x0C);
  224. } else {
  225. SX1276_SPIWrite(module,
  226. LR_RegModemConfig1,
  227. ((SX1276_LoRaBandwidth[LoRa_BW] << 4) + (SX1276_CR << 1) + 0x00)); //Explicit Enable CRC Enable(0x02) & Error Coding rate 4/5(0x01), 4/6(0x02), 4/7(0x03), 4/8(0x04)
  228. SX1276_SPIWrite(module,
  229. LR_RegModemConfig2,
  230. ((SX1276_SpreadFactor[LoRa_Rate] << 4) + (SX1276_CRC << 2)
  231. + 0x03)); //SFactor & LNA gain set by the internal AGC loop
  232. }
  233. SX1276_SPIWrite(module, LR_RegSymbTimeoutLsb, 0xFF); //RegSymbTimeoutLsb Timeout = 0x3FF(Max)
  234. SX1276_SPIWrite(module, LR_RegPreambleMsb, 0x00); //RegPreambleMsb
  235. SX1276_SPIWrite(module, LR_RegPreambleLsb, 12); //RegPreambleLsb 8+4=12byte Preamble
  236. SX1276_SPIWrite(module, REG_LR_DIOMAPPING2, 0x01); //RegDioMapping2 DIO5=00, DIO4=01
  237. module->readBytes = 0;
  238. SX1276_standby(module); //Entry standby mode
  239. }
  240. void SX1276_standby(SX1276_t * module) {
  241. // SX1276_SPIWrite(module, LR_RegOpMode, 0x09);
  242. SX1276_SPIWrite(module, LR_RegOpMode, 0x01);
  243. module->status = STANDBY;
  244. }
  245. void SX1276_sleep(SX1276_t * module) {
  246. // SX1276_SPIWrite(module, LR_RegOpMode, 0x08);
  247. SX1276_SPIWrite(module, LR_RegOpMode, 0x80);
  248. module->status = SLEEP;
  249. }
  250. void SX1276_entryLoRa(SX1276_t * module) {
  251. // SX1276_SPIWrite(module, LR_RegOpMode, 0x88);
  252. SX1276_SPIWrite(module, LR_RegOpMode, 0x80);
  253. }
  254. void SX1276_clearLoRaIrq(SX1276_t * module) {
  255. SX1276_SPIWrite(module, LR_RegIrqFlags, 0xFF);
  256. }
  257. int SX1276_LoRaEntryRx(SX1276_t * module, uint8_t length, uint32_t timeout) {
  258. uint8_t addr;
  259. module->packetLength = length;
  260. SX1276_defaultConfig(module); //Setting base parameter
  261. SX1276_SPIWrite(module, REG_LR_PADAC, 0x84); //Normal and RX
  262. SX1276_SPIWrite(module, LR_RegHopPeriod, 0xFF); //No FHSS
  263. SX1276_SPIWrite(module, REG_LR_DIOMAPPING1, 0x01);//DIO=00,DIO1=00,DIO2=00, DIO3=01
  264. SX1276_SPIWrite(module, LR_RegIrqFlagsMask, 0x3F);//Open RxDone interrupt & Timeout
  265. SX1276_clearLoRaIrq(module);
  266. SX1276_SPIWrite(module, LR_RegPayloadLength, length);//Payload Length 21byte(this register must difine when the data long of one byte in SF is 6)
  267. addr = SX1276_SPIRead(module, LR_RegFifoRxBaseAddr); //Read RxBaseAddr
  268. SX1276_SPIWrite(module, LR_RegFifoAddrPtr, addr); //RxBaseAddr->FiFoAddrPtr
  269. SX1276_SPIWrite(module, LR_RegOpMode, 0x85); //Mode//Low Frequency Mode
  270. //SX1276_SPIWrite(module, LR_RegOpMode,0x05); //Continuous Rx Mode //High Frequency Mode
  271. module->readBytes = 0;
  272. while (1) {
  273. if ((SX1276_SPIRead(module, LR_RegModemStat) & 0x04) == 0x04) { //Rx-on going RegModemStat
  274. module->status = RX;
  275. return 1;
  276. }
  277. if (--timeout == 0) {
  278. printf( "Function : %s Line : %d \n",__func__,__LINE__);
  279. NVIC_SystemReset();
  280. SX1276_hw_Reset(module->hw);
  281. SX1276_defaultConfig(module);
  282. return 0;
  283. }
  284. SX1276_hw_DelayMs(1);
  285. }
  286. }
  287. uint8_t SX1276_LoRaRxPacket(SX1276_t * module) {
  288. unsigned char addr;
  289. unsigned char packet_size;
  290. if (SX1276_hw_GetDIO0(module->hw)) {
  291. memset(module->rxBuffer, 0x00, SX1276_MAX_PACKET);
  292. addr = SX1276_SPIRead(module, LR_RegFifoRxCurrentaddr); //last packet addr
  293. SX1276_SPIWrite(module, LR_RegFifoAddrPtr, addr); //RxBaseAddr -> FiFoAddrPtr
  294. if (module->LoRa_Rate == SX1276_LORA_SF_6) { //When SpreadFactor is six,will used Implicit Header mode(Excluding internal packet length)
  295. packet_size = module->packetLength;
  296. } else {
  297. packet_size = SX1276_SPIRead(module, LR_RegRxNbBytes); //Number for received bytes
  298. }
  299. SX1276_SPIBurstRead(module, 0x00, module->rxBuffer, packet_size);
  300. module->readBytes = packet_size;
  301. SX1276_clearLoRaIrq(module);
  302. }
  303. return module->readBytes;
  304. }
  305. int SX1276_LoRaEntryTx(SX1276_t * module, uint8_t length, uint32_t timeout) {
  306. uint8_t addr;
  307. uint8_t temp;
  308. module->packetLength = length;
  309. SX1276_defaultConfig(module); //setting base parameter
  310. SX1276_SPIWrite(module, REG_LR_PADAC, 0x87); //Tx for 20dBm
  311. SX1276_SPIWrite(module, LR_RegHopPeriod, 0x00); //RegHopPeriod NO FHSS
  312. SX1276_SPIWrite(module, REG_LR_DIOMAPPING1, 0x41); //DIO0=01, DIO1=00,DIO2=00, DIO3=01
  313. SX1276_clearLoRaIrq(module);
  314. SX1276_SPIWrite(module, LR_RegIrqFlagsMask, 0xF7); //Open TxDone interrupt
  315. SX1276_SPIWrite(module, LR_RegPayloadLength, length); //RegPayloadLength 21byte
  316. addr = SX1276_SPIRead(module, LR_RegFifoTxBaseAddr); //RegFiFoTxBaseAddr
  317. SX1276_SPIWrite(module, LR_RegFifoAddrPtr, addr); //RegFifoAddrPtr
  318. while (1) {
  319. temp = SX1276_SPIRead(module, LR_RegPayloadLength);
  320. if (temp == length) {
  321. module->status = TX;
  322. return 1;
  323. }
  324. if (--timeout == 0) {
  325. printf( "Function : %s Line : %d \n",__func__,__LINE__);
  326. NVIC_SystemReset();
  327. SX1276_hw_Reset(module->hw);
  328. SX1276_defaultConfig(module);
  329. return 0;
  330. }
  331. }
  332. }
  333. int SX1276_LoRaTxPacket(SX1276_t * module, uint8_t* txBuffer, uint8_t length,
  334. uint32_t timeout) {
  335. SX1276_SPIBurstWrite(module, 0x00, txBuffer, length);
  336. // SX1276_SPIWrite(module, LR_RegOpMode, 0x8b); //Tx Mode
  337. SX1276_SPIWrite(module, LR_RegOpMode, 0x83); //Tx Mode
  338. while (1) {
  339. if (SX1276_hw_GetDIO0(module->hw)) { //if(Get_NIRQ()) //Packet send over
  340. SX1276_SPIRead(module, LR_RegIrqFlags);
  341. SX1276_clearLoRaIrq(module); //Clear irq
  342. SX1276_standby(module); //Entry Standby mode
  343. return 1;
  344. }
  345. if (--timeout == 0) {
  346. printf( "Function : %s Line : %d \n",__func__,__LINE__);
  347. NVIC_SystemReset();
  348. SX1276_hw_Reset(module->hw);
  349. SX1276_defaultConfig(module);
  350. return 0;
  351. }
  352. SX1276_hw_DelayMs(1);
  353. }
  354. }
  355. void SX1276_begin(SX1276_t * module, uint8_t frequency, uint8_t power,
  356. uint8_t LoRa_Rate, uint8_t LoRa_BW, uint8_t packetLength,uint8_t LoRa_Lna,uint8_t LoRa_PaBoost) {
  357. SX1276_hw_init(module->hw);
  358. module->frequency = frequency;
  359. module->power = power;
  360. module->LoRa_Rate = LoRa_Rate;
  361. module->LoRa_BW = LoRa_BW;
  362. module->packetLength = packetLength;
  363. module->LoRa_Lna = LoRa_Lna;
  364. module->LoRa_Pa_boost = LoRa_PaBoost;
  365. SX1276_defaultConfig(module);
  366. }
  367. int SX1276_transmit(SX1276_t * module, uint8_t* txBuf, uint8_t length,
  368. uint32_t timeout) {
  369. if (SX1276_LoRaEntryTx(module, length, timeout)) {
  370. return SX1276_LoRaTxPacket(module, txBuf, length, timeout);
  371. }
  372. return 0;
  373. }
  374. int SX1276_receive(SX1276_t * module, uint8_t length, uint32_t timeout) {
  375. return SX1276_LoRaEntryRx(module, length, timeout);
  376. }
  377. uint8_t SX1276_available(SX1276_t * module) {
  378. return SX1276_LoRaRxPacket(module);
  379. }
  380. uint8_t SX1276_read(SX1276_t * module, uint8_t* rxBuf, uint8_t length) {
  381. if (length != module->readBytes)
  382. length = module->readBytes;
  383. memcpy(rxBuf, module->rxBuffer, length);
  384. rxBuf[length] = '\0';
  385. module->readBytes = 0;
  386. return length;
  387. }
  388. uint8_t SX1276_RSSI_LoRa(SX1276_t * module) {
  389. uint32_t temp = 10;
  390. temp = SX1276_SPIRead(module, LR_RegRssiValue); //Read RegRssiValue, Rssi value
  391. temp = temp + 127 - 137; //127:Max RSSI, 137:RSSI offset
  392. return (uint8_t) temp;
  393. }
  394. uint8_t SX1276_RSSI(SX1276_t * module) {
  395. uint8_t temp = 0xff;
  396. temp = SX1276_SPIRead(module, RegRssiValue);
  397. temp = 127 - (temp >> 1); //127:Max RSSI
  398. return temp;
  399. }