PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

736 Zeilen
24KB

  1. // RH_RF22.cpp
  2. //
  3. // Copyright (C) 2011 Mike McCauley
  4. // $Id: RH_RF22.cpp,v 1.26 2016/04/04 01:40:12 mikem Exp $
  5. #include <RH_RF22.h>
  6. // Interrupt vectors for the 2 Arduino interrupt pins
  7. // Each interrupt can be handled by a different instance of RH_RF22, allowing you to have
  8. // 2 RH_RF22s per Arduino
  9. RH_RF22* RH_RF22::_deviceForInterrupt[RH_RF22_NUM_INTERRUPTS] = {0, 0, 0};
  10. uint8_t RH_RF22::_interruptCount = 0; // Index into _deviceForInterrupt for next device
  11. // These are indexed by the values of ModemConfigChoice
  12. // Canned modem configurations generated with
  13. // http://www.hoperf.com/upload/rf/RH_RF22B%2023B%2031B%2042B%2043B%20Register%20Settings_RevB1-v5.xls
  14. // Stored in flash (program) memory to save SRAM
  15. PROGMEM static const RH_RF22::ModemConfig MODEM_CONFIG_TABLE[] =
  16. {
  17. { 0x2b, 0x03, 0xf4, 0x20, 0x41, 0x89, 0x00, 0x36, 0x40, 0x0a, 0x1d, 0x80, 0x60, 0x10, 0x62, 0x2c, 0x00, 0x08 }, // Unmodulated carrier
  18. { 0x2b, 0x03, 0xf4, 0x20, 0x41, 0x89, 0x00, 0x36, 0x40, 0x0a, 0x1d, 0x80, 0x60, 0x10, 0x62, 0x2c, 0x33, 0x08 }, // FSK, PN9 random modulation, 2, 5
  19. // All the following enable FIFO with reg 71
  20. // 1c, 1f, 20, 21, 22, 23, 24, 25, 2c, 2d, 2e, 58, 69, 6e, 6f, 70, 71, 72
  21. // FSK, No Manchester, Max Rb err <1%, Xtal Tol 20ppm
  22. { 0x2b, 0x03, 0xf4, 0x20, 0x41, 0x89, 0x00, 0x36, 0x40, 0x0a, 0x1d, 0x80, 0x60, 0x10, 0x62, 0x2c, 0x22, 0x08 }, // 2, 5
  23. { 0x1b, 0x03, 0x41, 0x60, 0x27, 0x52, 0x00, 0x07, 0x40, 0x0a, 0x1e, 0x80, 0x60, 0x13, 0xa9, 0x2c, 0x22, 0x3a }, // 2.4, 36
  24. { 0x1d, 0x03, 0xa1, 0x20, 0x4e, 0xa5, 0x00, 0x13, 0x40, 0x0a, 0x1e, 0x80, 0x60, 0x27, 0x52, 0x2c, 0x22, 0x48 }, // 4.8, 45
  25. { 0x1e, 0x03, 0xd0, 0x00, 0x9d, 0x49, 0x00, 0x45, 0x40, 0x0a, 0x20, 0x80, 0x60, 0x4e, 0xa5, 0x2c, 0x22, 0x48 }, // 9.6, 45
  26. { 0x2b, 0x03, 0x34, 0x02, 0x75, 0x25, 0x07, 0xff, 0x40, 0x0a, 0x1b, 0x80, 0x60, 0x9d, 0x49, 0x2c, 0x22, 0x0f }, // 19.2, 9.6
  27. { 0x02, 0x03, 0x68, 0x01, 0x3a, 0x93, 0x04, 0xd5, 0x40, 0x0a, 0x1e, 0x80, 0x60, 0x09, 0xd5, 0x0c, 0x22, 0x1f }, // 38.4, 19.6
  28. { 0x06, 0x03, 0x45, 0x01, 0xd7, 0xdc, 0x07, 0x6e, 0x40, 0x0a, 0x2d, 0x80, 0x60, 0x0e, 0xbf, 0x0c, 0x22, 0x2e }, // 57.6. 28.8
  29. { 0x8a, 0x03, 0x60, 0x01, 0x55, 0x55, 0x02, 0xad, 0x40, 0x0a, 0x50, 0x80, 0x60, 0x20, 0x00, 0x0c, 0x22, 0xc8 }, // 125, 125
  30. { 0x2b, 0x03, 0xa1, 0xe0, 0x10, 0xc7, 0x00, 0x09, 0x40, 0x0a, 0x1d, 0x80, 0x60, 0x04, 0x32, 0x2c, 0x22, 0x04 }, // 512 baud, FSK, 2.5 Khz fd for POCSAG compatibility
  31. { 0x27, 0x03, 0xa1, 0xe0, 0x10, 0xc7, 0x00, 0x06, 0x40, 0x0a, 0x1d, 0x80, 0x60, 0x04, 0x32, 0x2c, 0x22, 0x07 }, // 512 baud, FSK, 4.5 Khz fd for POCSAG compatibility
  32. // GFSK, No Manchester, Max Rb err <1%, Xtal Tol 20ppm
  33. // These differ from FSK only in register 71, for the modulation type
  34. { 0x2b, 0x03, 0xf4, 0x20, 0x41, 0x89, 0x00, 0x36, 0x40, 0x0a, 0x1d, 0x80, 0x60, 0x10, 0x62, 0x2c, 0x23, 0x08 }, // 2, 5
  35. { 0x1b, 0x03, 0x41, 0x60, 0x27, 0x52, 0x00, 0x07, 0x40, 0x0a, 0x1e, 0x80, 0x60, 0x13, 0xa9, 0x2c, 0x23, 0x3a }, // 2.4, 36
  36. { 0x1d, 0x03, 0xa1, 0x20, 0x4e, 0xa5, 0x00, 0x13, 0x40, 0x0a, 0x1e, 0x80, 0x60, 0x27, 0x52, 0x2c, 0x23, 0x48 }, // 4.8, 45
  37. { 0x1e, 0x03, 0xd0, 0x00, 0x9d, 0x49, 0x00, 0x45, 0x40, 0x0a, 0x20, 0x80, 0x60, 0x4e, 0xa5, 0x2c, 0x23, 0x48 }, // 9.6, 45
  38. { 0x2b, 0x03, 0x34, 0x02, 0x75, 0x25, 0x07, 0xff, 0x40, 0x0a, 0x1b, 0x80, 0x60, 0x9d, 0x49, 0x2c, 0x23, 0x0f }, // 19.2, 9.6
  39. { 0x02, 0x03, 0x68, 0x01, 0x3a, 0x93, 0x04, 0xd5, 0x40, 0x0a, 0x1e, 0x80, 0x60, 0x09, 0xd5, 0x0c, 0x23, 0x1f }, // 38.4, 19.6
  40. { 0x06, 0x03, 0x45, 0x01, 0xd7, 0xdc, 0x07, 0x6e, 0x40, 0x0a, 0x2d, 0x80, 0x60, 0x0e, 0xbf, 0x0c, 0x23, 0x2e }, // 57.6. 28.8
  41. { 0x8a, 0x03, 0x60, 0x01, 0x55, 0x55, 0x02, 0xad, 0x40, 0x0a, 0x50, 0x80, 0x60, 0x20, 0x00, 0x0c, 0x23, 0xc8 }, // 125, 125
  42. // OOK, No Manchester, Max Rb err <1%, Xtal Tol 20ppm
  43. { 0x51, 0x03, 0x68, 0x00, 0x3a, 0x93, 0x01, 0x3d, 0x2c, 0x11, 0x28, 0x80, 0x60, 0x09, 0xd5, 0x2c, 0x21, 0x08 }, // 1.2, 75
  44. { 0xc8, 0x03, 0x39, 0x20, 0x68, 0xdc, 0x00, 0x6b, 0x2a, 0x08, 0x2a, 0x80, 0x60, 0x13, 0xa9, 0x2c, 0x21, 0x08 }, // 2.4, 335
  45. { 0xc8, 0x03, 0x9c, 0x00, 0xd1, 0xb7, 0x00, 0xd4, 0x29, 0x04, 0x29, 0x80, 0x60, 0x27, 0x52, 0x2c, 0x21, 0x08 }, // 4.8, 335
  46. { 0xb8, 0x03, 0x9c, 0x00, 0xd1, 0xb7, 0x00, 0xd4, 0x28, 0x82, 0x29, 0x80, 0x60, 0x4e, 0xa5, 0x2c, 0x21, 0x08 }, // 9.6, 335
  47. { 0xa8, 0x03, 0x9c, 0x00, 0xd1, 0xb7, 0x00, 0xd4, 0x28, 0x41, 0x29, 0x80, 0x60, 0x9d, 0x49, 0x2c, 0x21, 0x08 }, // 19.2, 335
  48. { 0x98, 0x03, 0x9c, 0x00, 0xd1, 0xb7, 0x00, 0xd4, 0x28, 0x20, 0x29, 0x80, 0x60, 0x09, 0xd5, 0x0c, 0x21, 0x08 }, // 38.4, 335
  49. { 0x98, 0x03, 0x96, 0x00, 0xda, 0x74, 0x00, 0xdc, 0x28, 0x1f, 0x29, 0x80, 0x60, 0x0a, 0x3d, 0x0c, 0x21, 0x08 }, // 40, 335
  50. };
  51. RH_RF22::RH_RF22(uint8_t slaveSelectPin, uint8_t interruptPin, RHGenericSPI& spi)
  52. :
  53. RHSPIDriver(slaveSelectPin, spi)
  54. {
  55. _interruptPin = interruptPin;
  56. _idleMode = RH_RF22_XTON; // Default idle state is READY mode
  57. _polynomial = CRC_16_IBM; // Historical
  58. _myInterruptIndex = 0xff; // Not allocated yet
  59. }
  60. void RH_RF22::setIdleMode(uint8_t idleMode)
  61. {
  62. _idleMode = idleMode;
  63. }
  64. bool RH_RF22::init()
  65. {
  66. if (!RHSPIDriver::init())
  67. return false;
  68. // Determine the interrupt number that corresponds to the interruptPin
  69. int interruptNumber = digitalPinToInterrupt(_interruptPin);
  70. if (interruptNumber == NOT_AN_INTERRUPT)
  71. return false;
  72. #ifdef RH_ATTACHINTERRUPT_TAKES_PIN_NUMBER
  73. interruptNumber = _interruptPin;
  74. #endif
  75. // Software reset the device
  76. reset();
  77. // Get the device type and check it
  78. // This also tests whether we are really connected to a device
  79. _deviceType = spiRead(RH_RF22_REG_00_DEVICE_TYPE);
  80. if ( _deviceType != RH_RF22_DEVICE_TYPE_RX_TRX
  81. && _deviceType != RH_RF22_DEVICE_TYPE_TX)
  82. {
  83. return false;
  84. }
  85. // Add by Adrien van den Bossche <vandenbo@univ-tlse2.fr> for Teensy
  86. // ARM M4 requires the below. else pin interrupt doesn't work properly.
  87. // On all other platforms, its innocuous, belt and braces
  88. pinMode(_interruptPin, INPUT);
  89. // Enable interrupt output on the radio. Interrupt line will now go high until
  90. // an interrupt occurs
  91. spiWrite(RH_RF22_REG_05_INTERRUPT_ENABLE1, RH_RF22_ENTXFFAEM | RH_RF22_ENRXFFAFULL | RH_RF22_ENPKSENT | RH_RF22_ENPKVALID | RH_RF22_ENCRCERROR | RH_RF22_ENFFERR);
  92. spiWrite(RH_RF22_REG_06_INTERRUPT_ENABLE2, RH_RF22_ENPREAVAL);
  93. // Set up interrupt handler
  94. // Since there are a limited number of interrupt glue functions isr*() available,
  95. // we can only support a limited number of devices simultaneously
  96. // On some devices, notably most Arduinos, the interrupt pin passed in is actually the
  97. // interrupt number. You have to figure out the interruptnumber-to-interruptpin mapping
  98. // yourself based on knowledge of what Arduino board you are running on.
  99. if (_myInterruptIndex == 0xff)
  100. {
  101. // First run, no interrupt allocated yet
  102. if (_interruptCount <= RH_RF22_NUM_INTERRUPTS)
  103. _myInterruptIndex = _interruptCount++;
  104. else
  105. return false; // Too many devices, not enough interrupt vectors
  106. }
  107. _deviceForInterrupt[_myInterruptIndex] = this;
  108. if (_myInterruptIndex == 0)
  109. attachInterrupt(interruptNumber, isr0, FALLING);
  110. else if (_myInterruptIndex == 1)
  111. attachInterrupt(interruptNumber, isr1, FALLING);
  112. else if (_myInterruptIndex == 2)
  113. attachInterrupt(interruptNumber, isr2, FALLING);
  114. else
  115. return false; // Too many devices, not enough interrupt vectors
  116. setModeIdle();
  117. clearTxBuf();
  118. clearRxBuf();
  119. // Most of these are the POR default
  120. spiWrite(RH_RF22_REG_7D_TX_FIFO_CONTROL2, RH_RF22_TXFFAEM_THRESHOLD);
  121. spiWrite(RH_RF22_REG_7E_RX_FIFO_CONTROL, RH_RF22_RXFFAFULL_THRESHOLD);
  122. spiWrite(RH_RF22_REG_30_DATA_ACCESS_CONTROL, RH_RF22_ENPACRX | RH_RF22_ENPACTX | RH_RF22_ENCRC | (_polynomial & RH_RF22_CRC));
  123. // Configure the message headers
  124. // Here we set up the standard packet format for use by the RH_RF22 library
  125. // 8 nibbles preamble
  126. // 2 SYNC words 2d, d4
  127. // Header length 4 (to, from, id, flags)
  128. // 1 octet of data length (0 to 255)
  129. // 0 to 255 octets data
  130. // 2 CRC octets as CRC16(IBM), computed on the header, length and data
  131. // On reception the to address is check for validity against RH_RF22_REG_3F_CHECK_HEADER3
  132. // or the broadcast address of 0xff
  133. // If no changes are made after this, the transmitted
  134. // to address will be 0xff, the from address will be 0xff
  135. // and all such messages will be accepted. This permits the out-of the box
  136. // RH_RF22 config to act as an unaddresed, unreliable datagram service
  137. spiWrite(RH_RF22_REG_32_HEADER_CONTROL1, RH_RF22_BCEN_HEADER3 | RH_RF22_HDCH_HEADER3);
  138. spiWrite(RH_RF22_REG_33_HEADER_CONTROL2, RH_RF22_HDLEN_4 | RH_RF22_SYNCLEN_2);
  139. setPreambleLength(8);
  140. uint8_t syncwords[] = { 0x2d, 0xd4 };
  141. setSyncWords(syncwords, sizeof(syncwords));
  142. setPromiscuous(false);
  143. // Set some defaults. An innocuous ISM frequency, and reasonable pull-in
  144. setFrequency(434.0, 0.05);
  145. // setFrequency(900.0);
  146. // Some slow, reliable default speed and modulation
  147. setModemConfig(FSK_Rb2_4Fd36);
  148. // setModemConfig(FSK_Rb125Fd125);
  149. setGpioReversed(false);
  150. // Lowish power
  151. setTxPower(RH_RF22_TXPOW_8DBM);
  152. return true;
  153. }
  154. // C++ level interrupt handler for this instance
  155. void RH_RF22::handleInterrupt()
  156. {
  157. uint8_t _lastInterruptFlags[2];
  158. // Read the interrupt flags which clears the interrupt
  159. spiBurstRead(RH_RF22_REG_03_INTERRUPT_STATUS1, _lastInterruptFlags, 2);
  160. #if 0
  161. // DEVELOPER TESTING ONLY
  162. // Caution: Serial printing in this interrupt routine can cause mysterious crashes
  163. Serial.print("interrupt ");
  164. Serial.print(_lastInterruptFlags[0], HEX);
  165. Serial.print(" ");
  166. Serial.println(_lastInterruptFlags[1], HEX);
  167. if (_lastInterruptFlags[0] == 0 && _lastInterruptFlags[1] == 0)
  168. Serial.println("FUNNY: no interrupt!");
  169. #endif
  170. #if 0
  171. // DEVELOPER TESTING ONLY
  172. // TESTING: fake an RH_RF22_IFFERROR
  173. static int counter = 0;
  174. if (_lastInterruptFlags[0] & RH_RF22_IPKSENT && counter++ == 10)
  175. {
  176. _lastInterruptFlags[0] = RH_RF22_IFFERROR;
  177. counter = 0;
  178. }
  179. #endif
  180. if (_lastInterruptFlags[0] & RH_RF22_IFFERROR)
  181. {
  182. resetFifos(); // Clears the interrupt
  183. if (_mode == RHModeTx)
  184. restartTransmit();
  185. else if (_mode == RHModeRx)
  186. clearRxBuf();
  187. // Serial.println("IFFERROR");
  188. }
  189. // Caution, any delay here may cause a FF underflow or overflow
  190. if (_lastInterruptFlags[0] & RH_RF22_ITXFFAEM)
  191. {
  192. // See if more data has to be loaded into the Tx FIFO
  193. sendNextFragment();
  194. // Serial.println("ITXFFAEM");
  195. }
  196. if (_lastInterruptFlags[0] & RH_RF22_IRXFFAFULL)
  197. {
  198. // Caution, any delay here may cause a FF overflow
  199. // Read some data from the Rx FIFO
  200. readNextFragment();
  201. // Serial.println("IRXFFAFULL");
  202. }
  203. if (_lastInterruptFlags[0] & RH_RF22_IEXT)
  204. {
  205. // This is not enabled by the base code, but users may want to enable it
  206. handleExternalInterrupt();
  207. // Serial.println("IEXT");
  208. }
  209. if (_lastInterruptFlags[1] & RH_RF22_IWUT)
  210. {
  211. // This is not enabled by the base code, but users may want to enable it
  212. handleWakeupTimerInterrupt();
  213. // Serial.println("IWUT");
  214. }
  215. if (_lastInterruptFlags[0] & RH_RF22_IPKSENT)
  216. {
  217. // Serial.println("IPKSENT");
  218. _txGood++;
  219. // Transmission does not automatically clear the tx buffer.
  220. // Could retransmit if we wanted
  221. // RH_RF22 transitions automatically to Idle
  222. _mode = RHModeIdle;
  223. }
  224. if (_lastInterruptFlags[0] & RH_RF22_IPKVALID)
  225. {
  226. uint8_t len = spiRead(RH_RF22_REG_4B_RECEIVED_PACKET_LENGTH);
  227. // Serial.println("IPKVALID");
  228. // May have already read one or more fragments
  229. // Get any remaining unread octets, based on the expected length
  230. // First make sure we dont overflow the buffer in the case of a stupid length
  231. // or partial bad receives
  232. if ( len > RH_RF22_MAX_MESSAGE_LEN
  233. || len < _bufLen)
  234. {
  235. _rxBad++;
  236. _mode = RHModeIdle;
  237. clearRxBuf();
  238. return; // Hmmm receiver buffer overflow.
  239. }
  240. spiBurstRead(RH_RF22_REG_7F_FIFO_ACCESS, _buf + _bufLen, len - _bufLen);
  241. _rxHeaderTo = spiRead(RH_RF22_REG_47_RECEIVED_HEADER3);
  242. _rxHeaderFrom = spiRead(RH_RF22_REG_48_RECEIVED_HEADER2);
  243. _rxHeaderId = spiRead(RH_RF22_REG_49_RECEIVED_HEADER1);
  244. _rxHeaderFlags = spiRead(RH_RF22_REG_4A_RECEIVED_HEADER0);
  245. _rxGood++;
  246. _bufLen = len;
  247. _mode = RHModeIdle;
  248. _rxBufValid = true;
  249. }
  250. if (_lastInterruptFlags[0] & RH_RF22_ICRCERROR)
  251. {
  252. // Serial.println("ICRCERR");
  253. _rxBad++;
  254. clearRxBuf();
  255. resetRxFifo();
  256. _mode = RHModeIdle;
  257. setModeRx(); // Keep trying
  258. }
  259. if (_lastInterruptFlags[1] & RH_RF22_IPREAVAL)
  260. {
  261. // Serial.println("IPREAVAL");
  262. _lastRssi = (int8_t)(-120 + ((spiRead(RH_RF22_REG_26_RSSI) / 2)));
  263. _lastPreambleTime = millis();
  264. resetRxFifo();
  265. clearRxBuf();
  266. }
  267. }
  268. // These are low level functions that call the interrupt handler for the correct
  269. // instance of RH_RF22.
  270. // 3 interrupts allows us to have 3 different devices
  271. void RH_RF22::isr0()
  272. {
  273. if (_deviceForInterrupt[0])
  274. _deviceForInterrupt[0]->handleInterrupt();
  275. }
  276. void RH_RF22::isr1()
  277. {
  278. if (_deviceForInterrupt[1])
  279. _deviceForInterrupt[1]->handleInterrupt();
  280. }
  281. void RH_RF22::isr2()
  282. {
  283. if (_deviceForInterrupt[2])
  284. _deviceForInterrupt[2]->handleInterrupt();
  285. }
  286. void RH_RF22::reset()
  287. {
  288. spiWrite(RH_RF22_REG_07_OPERATING_MODE1, RH_RF22_SWRES);
  289. // Wait for it to settle
  290. delay(1); // SWReset time is nominally 100usec
  291. }
  292. uint8_t RH_RF22::statusRead()
  293. {
  294. return spiRead(RH_RF22_REG_02_DEVICE_STATUS);
  295. }
  296. uint8_t RH_RF22::adcRead(uint8_t adcsel,
  297. uint8_t adcref ,
  298. uint8_t adcgain,
  299. uint8_t adcoffs)
  300. {
  301. uint8_t configuration = adcsel | adcref | (adcgain & RH_RF22_ADCGAIN);
  302. spiWrite(RH_RF22_REG_0F_ADC_CONFIGURATION, configuration | RH_RF22_ADCSTART);
  303. spiWrite(RH_RF22_REG_10_ADC_SENSOR_AMP_OFFSET, adcoffs);
  304. // Conversion time is nominally 305usec
  305. // Wait for the DONE bit
  306. while (!(spiRead(RH_RF22_REG_0F_ADC_CONFIGURATION) & RH_RF22_ADCDONE))
  307. ;
  308. // Return the value
  309. return spiRead(RH_RF22_REG_11_ADC_VALUE);
  310. }
  311. uint8_t RH_RF22::temperatureRead(uint8_t tsrange, uint8_t tvoffs)
  312. {
  313. spiWrite(RH_RF22_REG_12_TEMPERATURE_SENSOR_CALIBRATION, tsrange | RH_RF22_ENTSOFFS);
  314. spiWrite(RH_RF22_REG_13_TEMPERATURE_VALUE_OFFSET, tvoffs);
  315. return adcRead(RH_RF22_ADCSEL_INTERNAL_TEMPERATURE_SENSOR | RH_RF22_ADCREF_BANDGAP_VOLTAGE);
  316. }
  317. uint16_t RH_RF22::wutRead()
  318. {
  319. uint8_t buf[2];
  320. spiBurstRead(RH_RF22_REG_17_WAKEUP_TIMER_VALUE1, buf, 2);
  321. return ((uint16_t)buf[0] << 8) | buf[1]; // Dont rely on byte order
  322. }
  323. // RFM-22 doc appears to be wrong: WUT for wtm = 10000, r, = 0, d = 0 is about 1 sec
  324. void RH_RF22::setWutPeriod(uint16_t wtm, uint8_t wtr, uint8_t wtd)
  325. {
  326. uint8_t period[3];
  327. period[0] = ((wtr & 0xf) << 2) | (wtd & 0x3);
  328. period[1] = wtm >> 8;
  329. period[2] = wtm & 0xff;
  330. spiBurstWrite(RH_RF22_REG_14_WAKEUP_TIMER_PERIOD1, period, sizeof(period));
  331. }
  332. // Returns true if centre + (fhch * fhs) is within limits
  333. // Caution, different versions of the RH_RF22 support different max freq
  334. // so YMMV
  335. bool RH_RF22::setFrequency(float centre, float afcPullInRange)
  336. {
  337. uint8_t fbsel = RH_RF22_SBSEL;
  338. uint8_t afclimiter;
  339. if (centre < 240.0 || centre > 960.0) // 930.0 for early silicon
  340. return false;
  341. if (centre >= 480.0)
  342. {
  343. if (afcPullInRange < 0.0 || afcPullInRange > 0.318750)
  344. return false;
  345. centre /= 2;
  346. fbsel |= RH_RF22_HBSEL;
  347. afclimiter = afcPullInRange * 1000000.0 / 1250.0;
  348. }
  349. else
  350. {
  351. if (afcPullInRange < 0.0 || afcPullInRange > 0.159375)
  352. return false;
  353. afclimiter = afcPullInRange * 1000000.0 / 625.0;
  354. }
  355. centre /= 10.0;
  356. float integerPart = floor(centre);
  357. float fractionalPart = centre - integerPart;
  358. uint8_t fb = (uint8_t)integerPart - 24; // Range 0 to 23
  359. fbsel |= fb;
  360. uint16_t fc = fractionalPart * 64000;
  361. spiWrite(RH_RF22_REG_73_FREQUENCY_OFFSET1, 0); // REVISIT
  362. spiWrite(RH_RF22_REG_74_FREQUENCY_OFFSET2, 0);
  363. spiWrite(RH_RF22_REG_75_FREQUENCY_BAND_SELECT, fbsel);
  364. spiWrite(RH_RF22_REG_76_NOMINAL_CARRIER_FREQUENCY1, fc >> 8);
  365. spiWrite(RH_RF22_REG_77_NOMINAL_CARRIER_FREQUENCY0, fc & 0xff);
  366. spiWrite(RH_RF22_REG_2A_AFC_LIMITER, afclimiter);
  367. return !(statusRead() & RH_RF22_FREQERR);
  368. }
  369. // Step size in 10kHz increments
  370. // Returns true if centre + (fhch * fhs) is within limits
  371. bool RH_RF22::setFHStepSize(uint8_t fhs)
  372. {
  373. spiWrite(RH_RF22_REG_7A_FREQUENCY_HOPPING_STEP_SIZE, fhs);
  374. return !(statusRead() & RH_RF22_FREQERR);
  375. }
  376. // Adds fhch * fhs to centre frequency
  377. // Returns true if centre + (fhch * fhs) is within limits
  378. bool RH_RF22::setFHChannel(uint8_t fhch)
  379. {
  380. spiWrite(RH_RF22_REG_79_FREQUENCY_HOPPING_CHANNEL_SELECT, fhch);
  381. return !(statusRead() & RH_RF22_FREQERR);
  382. }
  383. uint8_t RH_RF22::rssiRead()
  384. {
  385. return spiRead(RH_RF22_REG_26_RSSI);
  386. }
  387. uint8_t RH_RF22::ezmacStatusRead()
  388. {
  389. return spiRead(RH_RF22_REG_31_EZMAC_STATUS);
  390. }
  391. void RH_RF22::setOpMode(uint8_t mode)
  392. {
  393. spiWrite(RH_RF22_REG_07_OPERATING_MODE1, mode);
  394. }
  395. void RH_RF22::setModeIdle()
  396. {
  397. if (_mode != RHModeIdle)
  398. {
  399. setOpMode(_idleMode);
  400. _mode = RHModeIdle;
  401. }
  402. }
  403. bool RH_RF22::sleep()
  404. {
  405. if (_mode != RHModeSleep)
  406. {
  407. setOpMode(0);
  408. _mode = RHModeSleep;
  409. }
  410. return true;
  411. }
  412. void RH_RF22::setModeRx()
  413. {
  414. if (_mode != RHModeRx)
  415. {
  416. setOpMode(_idleMode | RH_RF22_RXON);
  417. _mode = RHModeRx;
  418. }
  419. }
  420. void RH_RF22::setModeTx()
  421. {
  422. if (_mode != RHModeTx)
  423. {
  424. setOpMode(_idleMode | RH_RF22_TXON);
  425. // Hmmm, if you dont clear the RX FIFO here, then it appears that going
  426. // to transmit mode in the middle of a receive can corrupt the
  427. // RX FIFO
  428. resetRxFifo();
  429. _mode = RHModeTx;
  430. }
  431. }
  432. void RH_RF22::setTxPower(uint8_t power)
  433. {
  434. spiWrite(RH_RF22_REG_6D_TX_POWER, power | RH_RF22_LNA_SW); // On RF23, LNA_SW must be set.
  435. }
  436. // Sets registers from a canned modem configuration structure
  437. void RH_RF22::setModemRegisters(const ModemConfig* config)
  438. {
  439. spiWrite(RH_RF22_REG_1C_IF_FILTER_BANDWIDTH, config->reg_1c);
  440. spiWrite(RH_RF22_REG_1F_CLOCK_RECOVERY_GEARSHIFT_OVERRIDE, config->reg_1f);
  441. spiBurstWrite(RH_RF22_REG_20_CLOCK_RECOVERY_OVERSAMPLING_RATE, &config->reg_20, 6);
  442. spiBurstWrite(RH_RF22_REG_2C_OOK_COUNTER_VALUE_1, &config->reg_2c, 3);
  443. spiWrite(RH_RF22_REG_58_CHARGE_PUMP_CURRENT_TRIMMING, config->reg_58);
  444. spiWrite(RH_RF22_REG_69_AGC_OVERRIDE1, config->reg_69);
  445. spiBurstWrite(RH_RF22_REG_6E_TX_DATA_RATE1, &config->reg_6e, 5);
  446. }
  447. // Set one of the canned FSK Modem configs
  448. // Returns true if its a valid choice
  449. bool RH_RF22::setModemConfig(ModemConfigChoice index)
  450. {
  451. if (index > (signed int)(sizeof(MODEM_CONFIG_TABLE) / sizeof(ModemConfig)))
  452. return false;
  453. RH_RF22::ModemConfig cfg;
  454. memcpy_P(&cfg, &MODEM_CONFIG_TABLE[index], sizeof(RH_RF22::ModemConfig));
  455. setModemRegisters(&cfg);
  456. return true;
  457. }
  458. // REVISIT: top bit is in Header Control 2 0x33
  459. void RH_RF22::setPreambleLength(uint8_t nibbles)
  460. {
  461. spiWrite(RH_RF22_REG_34_PREAMBLE_LENGTH, nibbles);
  462. }
  463. // Caution doesnt set sync word len in Header Control 2 0x33
  464. void RH_RF22::setSyncWords(const uint8_t* syncWords, uint8_t len)
  465. {
  466. spiBurstWrite(RH_RF22_REG_36_SYNC_WORD3, syncWords, len);
  467. }
  468. void RH_RF22::clearRxBuf()
  469. {
  470. ATOMIC_BLOCK_START;
  471. _bufLen = 0;
  472. _rxBufValid = false;
  473. ATOMIC_BLOCK_END;
  474. }
  475. bool RH_RF22::available()
  476. {
  477. if (!_rxBufValid)
  478. {
  479. if (_mode == RHModeTx)
  480. return false;
  481. setModeRx(); // Make sure we are receiving
  482. }
  483. return _rxBufValid;
  484. }
  485. bool RH_RF22::recv(uint8_t* buf, uint8_t* len)
  486. {
  487. if (!available())
  488. return false;
  489. if (buf && len)
  490. {
  491. ATOMIC_BLOCK_START;
  492. if (*len > _bufLen)
  493. *len = _bufLen;
  494. memcpy(buf, _buf, *len);
  495. ATOMIC_BLOCK_END;
  496. }
  497. clearRxBuf();
  498. // printBuffer("recv:", buf, *len);
  499. return true;
  500. }
  501. void RH_RF22::clearTxBuf()
  502. {
  503. ATOMIC_BLOCK_START;
  504. _bufLen = 0;
  505. _txBufSentIndex = 0;
  506. ATOMIC_BLOCK_END;
  507. }
  508. void RH_RF22::startTransmit()
  509. {
  510. sendNextFragment(); // Actually the first fragment
  511. spiWrite(RH_RF22_REG_3E_PACKET_LENGTH, _bufLen); // Total length that will be sent
  512. setModeTx(); // Start the transmitter, turns off the receiver
  513. }
  514. // Restart the transmission of a packet that had a problem
  515. void RH_RF22::restartTransmit()
  516. {
  517. _mode = RHModeIdle;
  518. _txBufSentIndex = 0;
  519. // Serial.println("Restart");
  520. startTransmit();
  521. }
  522. bool RH_RF22::send(const uint8_t* data, uint8_t len)
  523. {
  524. bool ret = true;
  525. waitPacketSent();
  526. ATOMIC_BLOCK_START;
  527. spiWrite(RH_RF22_REG_3A_TRANSMIT_HEADER3, _txHeaderTo);
  528. spiWrite(RH_RF22_REG_3B_TRANSMIT_HEADER2, _txHeaderFrom);
  529. spiWrite(RH_RF22_REG_3C_TRANSMIT_HEADER1, _txHeaderId);
  530. spiWrite(RH_RF22_REG_3D_TRANSMIT_HEADER0, _txHeaderFlags);
  531. if (!fillTxBuf(data, len))
  532. ret = false;
  533. else
  534. startTransmit();
  535. ATOMIC_BLOCK_END;
  536. // printBuffer("send:", data, len);
  537. return ret;
  538. }
  539. bool RH_RF22::fillTxBuf(const uint8_t* data, uint8_t len)
  540. {
  541. clearTxBuf();
  542. if (!len)
  543. return false;
  544. return appendTxBuf(data, len);
  545. }
  546. bool RH_RF22::appendTxBuf(const uint8_t* data, uint8_t len)
  547. {
  548. if (((uint16_t)_bufLen + len) > RH_RF22_MAX_MESSAGE_LEN)
  549. return false;
  550. ATOMIC_BLOCK_START;
  551. memcpy(_buf + _bufLen, data, len);
  552. _bufLen += len;
  553. ATOMIC_BLOCK_END;
  554. // printBuffer("txbuf:", _buf, _bufLen);
  555. return true;
  556. }
  557. // Assumption: there is currently <= RH_RF22_TXFFAEM_THRESHOLD bytes in the Tx FIFO
  558. void RH_RF22::sendNextFragment()
  559. {
  560. if (_txBufSentIndex < _bufLen)
  561. {
  562. // Some left to send?
  563. uint8_t len = _bufLen - _txBufSentIndex;
  564. // But dont send too much
  565. if (len > (RH_RF22_FIFO_SIZE - RH_RF22_TXFFAEM_THRESHOLD - 1))
  566. len = (RH_RF22_FIFO_SIZE - RH_RF22_TXFFAEM_THRESHOLD - 1);
  567. spiBurstWrite(RH_RF22_REG_7F_FIFO_ACCESS, _buf + _txBufSentIndex, len);
  568. // printBuffer("frag:", _buf + _txBufSentIndex, len);
  569. _txBufSentIndex += len;
  570. }
  571. }
  572. // Assumption: there are at least RH_RF22_RXFFAFULL_THRESHOLD in the RX FIFO
  573. // That means it should only be called after a RXFFAFULL interrupt
  574. void RH_RF22::readNextFragment()
  575. {
  576. if (((uint16_t)_bufLen + RH_RF22_RXFFAFULL_THRESHOLD) > RH_RF22_MAX_MESSAGE_LEN)
  577. return; // Hmmm receiver overflow. Should never occur
  578. // Read the RH_RF22_RXFFAFULL_THRESHOLD octets that should be there
  579. spiBurstRead(RH_RF22_REG_7F_FIFO_ACCESS, _buf + _bufLen, RH_RF22_RXFFAFULL_THRESHOLD);
  580. _bufLen += RH_RF22_RXFFAFULL_THRESHOLD;
  581. }
  582. // Clear the FIFOs
  583. void RH_RF22::resetFifos()
  584. {
  585. spiWrite(RH_RF22_REG_08_OPERATING_MODE2, RH_RF22_FFCLRRX | RH_RF22_FFCLRTX);
  586. spiWrite(RH_RF22_REG_08_OPERATING_MODE2, 0);
  587. }
  588. // Clear the Rx FIFO
  589. void RH_RF22::resetRxFifo()
  590. {
  591. spiWrite(RH_RF22_REG_08_OPERATING_MODE2, RH_RF22_FFCLRRX);
  592. spiWrite(RH_RF22_REG_08_OPERATING_MODE2, 0);
  593. _rxBufValid = false;
  594. }
  595. // CLear the TX FIFO
  596. void RH_RF22::resetTxFifo()
  597. {
  598. spiWrite(RH_RF22_REG_08_OPERATING_MODE2, RH_RF22_FFCLRTX);
  599. spiWrite(RH_RF22_REG_08_OPERATING_MODE2, 0);
  600. }
  601. // Default implmentation does nothing. Override if you wish
  602. void RH_RF22::handleExternalInterrupt()
  603. {
  604. }
  605. // Default implmentation does nothing. Override if you wish
  606. void RH_RF22::handleWakeupTimerInterrupt()
  607. {
  608. }
  609. void RH_RF22::setPromiscuous(bool promiscuous)
  610. {
  611. RHSPIDriver::setPromiscuous(promiscuous);
  612. spiWrite(RH_RF22_REG_43_HEADER_ENABLE3, promiscuous ? 0x00 : 0xff);
  613. }
  614. bool RH_RF22::setCRCPolynomial(CRCPolynomial polynomial)
  615. {
  616. if (polynomial >= CRC_CCITT &&
  617. polynomial <= CRC_Biacheva)
  618. {
  619. _polynomial = polynomial;
  620. return true;
  621. }
  622. else
  623. return false;
  624. }
  625. uint8_t RH_RF22::maxMessageLength()
  626. {
  627. return RH_RF22_MAX_MESSAGE_LEN;
  628. }
  629. void RH_RF22::setThisAddress(uint8_t thisAddress)
  630. {
  631. RHSPIDriver::setThisAddress(thisAddress);
  632. spiWrite(RH_RF22_REG_3F_CHECK_HEADER3, thisAddress);
  633. }
  634. uint32_t RH_RF22::getLastPreambleTime()
  635. {
  636. return _lastPreambleTime;
  637. }
  638. void RH_RF22::setGpioReversed(bool gpioReversed)
  639. {
  640. // Ensure the antenna can be switched automatically according to transmit and receive
  641. // This assumes GPIO0(out) is connected to TX_ANT(in) to enable tx antenna during transmit
  642. // This assumes GPIO1(out) is connected to RX_ANT(in) to enable rx antenna during receive
  643. if (gpioReversed)
  644. {
  645. // Reversed for HAB-RFM22B-BOA HAB-RFM22B-BO, also Si4432 sold by Dorji.com via Tindie.com.
  646. spiWrite(RH_RF22_REG_0B_GPIO_CONFIGURATION0, 0x15) ; // RX state
  647. spiWrite(RH_RF22_REG_0C_GPIO_CONFIGURATION1, 0x12) ; // TX state
  648. }
  649. else
  650. {
  651. spiWrite(RH_RF22_REG_0B_GPIO_CONFIGURATION0, 0x12) ; // TX state
  652. spiWrite(RH_RF22_REG_0C_GPIO_CONFIGURATION1, 0x15) ; // RX state
  653. }
  654. }