LT8900MiLightRadio.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * MiLightRadioPL1167_LT89000.cpp
  3. *
  4. * Created on: 31 March 2017
  5. * Author: WoodsterDK
  6. *
  7. * Very inspired by:
  8. * https://github.com/pmoscetta/authometion-milight/tree/master/Authometion-MiLight
  9. * https://bitbucket.org/robvanderveer/lt8900lib
  10. */
  11. #include "LT8900MiLightRadio.h"
  12. #include <SPI.h>
  13. /**************************************************************************/
  14. // Constructor
  15. /**************************************************************************/
  16. LT8900MiLightRadio::LT8900MiLightRadio(byte byCSPin, byte byResetPin, byte byPktFlag, const MiLightRadioConfig& config)
  17. : _config(config),
  18. _channel(0),
  19. _currentPacketLen(0),
  20. _currentPacketPos(0)
  21. {
  22. _csPin = byCSPin;
  23. _pin_pktflag = byPktFlag;
  24. pinMode(_pin_pktflag, INPUT);
  25. if (byResetPin > 0) // If zero then bypass hardware reset
  26. {
  27. pinMode(byResetPin, OUTPUT);
  28. digitalWrite(byResetPin, LOW);
  29. delay(200);
  30. digitalWrite(byResetPin, HIGH);
  31. delay(200);
  32. }
  33. pinMode(_csPin, OUTPUT);
  34. digitalWrite(_csPin, HIGH);
  35. SPI.begin();
  36. SPI.setDataMode(SPI_MODE1);
  37. // The following speed settings depends upon the wiring and PCB
  38. //SPI.setFrequency(8000000);
  39. SPI.setFrequency(4000000);
  40. SPI.setBitOrder(MSBFIRST);
  41. //Initialize transceiver with correct settings
  42. vInitRadioModule();
  43. delay(50);
  44. // Check if HW is connected
  45. _bConnected = bCheckRadioConnection();
  46. //Reset SPI MODE to default
  47. SPI.setDataMode(SPI_MODE0);
  48. _waiting = false;
  49. }
  50. /**************************************************************************/
  51. // Checks the connection to the radio module by verifying a register setting
  52. /**************************************************************************/
  53. bool LT8900MiLightRadio::bCheckRadioConnection(void)
  54. {
  55. bool bRetValue = false;
  56. uint16_t value_0 = uiReadRegister(0);
  57. uint16_t value_1 = uiReadRegister(1);
  58. if ((value_0 == 0x6fe0) && (value_1 == 0x5681))
  59. {
  60. #ifdef DEBUG_PRINTF
  61. Serial.println(F("Radio module running correctly..."));
  62. #endif
  63. bRetValue = true;
  64. }
  65. else
  66. {
  67. #ifdef DEBUG_PRINTF
  68. Serial.println(F("Failed initializing the radio module..."));
  69. #endif
  70. }
  71. return bRetValue;
  72. }
  73. /**************************************************************************/
  74. // Initialize radio module
  75. /**************************************************************************/
  76. void LT8900MiLightRadio::vInitRadioModule() {
  77. bool bWriteDefaultDefault = true; // Is it okay to use the default power up values, without setting them
  78. regWrite16(0x00, 0x6F, 0xE0, 7); // Recommended value by PMmicro
  79. regWrite16(0x02, 0x66, 0x17, 7); // Recommended value by PMmicro
  80. regWrite16(0x04, 0x9C, 0xC9, 7); // Recommended value by PMmicro
  81. regWrite16(0x05, 0x66, 0x37, 7); // Recommended value by PMmicro
  82. regWrite16(0x07, 0x00, 0x4C, 7); // PL1167's TX/RX Enable and Channel Register, Default channel 76
  83. regWrite16(0x08, 0x6C, 0x90, 7); // Recommended value by PMmicro
  84. regWrite16(0x09, 0x48, 0x00, 7); // PA Control register
  85. regWrite16(0x0B, 0x00, 0x08, 7); // Recommended value by PMmicro
  86. regWrite16(0x0D, 0x48, 0xBD, 7); // Recommended value by PMmicro
  87. regWrite16(0x16, 0x00, 0xFF, 7); // Recommended value by PMmicro
  88. regWrite16(0x18, 0x00, 0x67, 7); // Recommended value by PMmicro
  89. regWrite16(0x1A, 0x19, 0xE0, 7); // Recommended value by PMmicro
  90. regWrite16(0x1B, 0x13, 0x00, 7); // Recommended value by PMmicro
  91. regWrite16(0x20, 0x48, 0x00, 7); // Recommended value by PMmicro
  92. regWrite16(0x21, 0x3F, 0xC7, 7); // Recommended value by PMmicro
  93. regWrite16(0x22, 0x20, 0x00, 7); // Recommended value by PMmicro
  94. regWrite16(0x23, 0x03, 0x00, 7); // Recommended value by PMmicro
  95. regWrite16(0x24, 0x72, 0x36, 7); // Sync R0
  96. regWrite16(0x27, 0x18, 0x09, 7); // Sync R3
  97. regWrite16(0x28, 0x44, 0x02, 7); // Recommended value by PMmicro
  98. regWrite16(0x29, 0xB0, 0x00, 7); // Recommended value by PMmicro
  99. regWrite16(0x2A, 0xFD, 0xB0, 7); // Recommended value by PMmicro
  100. if (bWriteDefaultDefault == true) {
  101. regWrite16(0x01, 0x56, 0x81, 7); // Recommended value by PMmicro
  102. regWrite16(0x0A, 0x7F, 0xFD, 7); // Recommended value by PMmicro
  103. regWrite16(0x0C, 0x00, 0x00, 7); // Recommended value by PMmicro
  104. regWrite16(0x17, 0x80, 0x05, 7); // Recommended value by PMmicro
  105. regWrite16(0x19, 0x16, 0x59, 7); // Recommended value by PMmicro
  106. regWrite16(0x1C, 0x18, 0x00, 7); // Recommended value by PMmicro
  107. regWrite16(0x25, 0x00, 0x00, 7); // Recommended value by PMmicro
  108. regWrite16(0x26, 0x00, 0x00, 7); // Recommended value by PMmicro
  109. regWrite16(0x2B, 0x00, 0x0F, 7); // Recommended value by PMmicro
  110. }
  111. }
  112. /**************************************************************************/
  113. // Set sync word
  114. /**************************************************************************/
  115. void LT8900MiLightRadio::vSetSyncWord(uint16_t syncWord3, uint16_t syncWord2, uint16_t syncWord1, uint16_t syncWord0)
  116. {
  117. uiWriteRegister(R_SYNCWORD1, syncWord0);
  118. uiWriteRegister(R_SYNCWORD2, syncWord1);
  119. uiWriteRegister(R_SYNCWORD3, syncWord1);
  120. uiWriteRegister(R_SYNCWORD4, syncWord3);
  121. }
  122. /**************************************************************************/
  123. // Low level register write with delay
  124. /**************************************************************************/
  125. void LT8900MiLightRadio::regWrite16(byte ADDR, byte V1, byte V2, byte WAIT)
  126. {
  127. digitalWrite(_csPin, LOW);
  128. SPI.transfer(ADDR);
  129. SPI.transfer(V1);
  130. SPI.transfer(V2);
  131. digitalWrite(_csPin, HIGH);
  132. delayMicroseconds(WAIT);
  133. }
  134. /**************************************************************************/
  135. // Low level register read
  136. /**************************************************************************/
  137. uint16_t LT8900MiLightRadio::uiReadRegister(uint8_t reg)
  138. {
  139. SPI.setDataMode(SPI_MODE1);
  140. digitalWrite(_csPin, LOW);
  141. SPI.transfer(REGISTER_READ | (REGISTER_MASK & reg));
  142. uint8_t high = SPI.transfer(0x00);
  143. uint8_t low = SPI.transfer(0x00);
  144. digitalWrite(_csPin, HIGH);
  145. SPI.setDataMode(SPI_MODE0);
  146. return (high << 8 | low);
  147. }
  148. /**************************************************************************/
  149. // Low level 16bit register write
  150. /**************************************************************************/
  151. uint8_t LT8900MiLightRadio::uiWriteRegister(uint8_t reg, uint16_t data)
  152. {
  153. uint8_t high = data >> 8;
  154. uint8_t low = data & 0xFF;
  155. digitalWrite(_csPin, LOW);
  156. uint8_t result = SPI.transfer(REGISTER_WRITE | (REGISTER_MASK & reg));
  157. SPI.transfer(high);
  158. SPI.transfer(low);
  159. digitalWrite(_csPin, HIGH);
  160. return result;
  161. }
  162. /**************************************************************************/
  163. // Start listening on specified channel and syncword
  164. /**************************************************************************/
  165. void LT8900MiLightRadio::vStartListening(uint uiChannelToListenTo)
  166. {
  167. _dupes_received = 0;
  168. vSetSyncWord(_config.syncword3, 0,0,_config.syncword0);
  169. //vSetChannel(uiChannelToListenTo);
  170. _channel = uiChannelToListenTo;
  171. vResumeRX();
  172. delay(5);
  173. }
  174. /**************************************************************************/
  175. // Resume listening - without changing the channel and syncword
  176. /**************************************************************************/
  177. void LT8900MiLightRadio::vResumeRX(void)
  178. {
  179. _dupes_received = 0;
  180. uiWriteRegister(R_CHANNEL, _channel & CHANNEL_MASK); //turn off rx/tx
  181. delay(3);
  182. uiWriteRegister(R_FIFO_CONTROL, 0x0080); //flush rx
  183. uiWriteRegister(R_CHANNEL, (_channel & CHANNEL_MASK) | _BV(CHANNEL_RX_BIT)); //enable RX
  184. }
  185. /**************************************************************************/
  186. // Check if data is available using the hardware pin PKT_FLAG
  187. /**************************************************************************/
  188. bool LT8900MiLightRadio::bAvailablePin() {
  189. return digitalRead(_pin_pktflag) > 0;
  190. }
  191. /**************************************************************************/
  192. // Check if data is available using the PKT_FLAG state in the status register
  193. /**************************************************************************/
  194. bool LT8900MiLightRadio::bAvailableRegister() {
  195. //read the PKT_FLAG state; this can also be done with a hard wire.
  196. uint16_t value = uiReadRegister(R_STATUS);
  197. if (bitRead(value, STATUS_CRC_BIT) != 0) {
  198. #ifdef DEBUG_PRINTF
  199. Serial.println(F("LT8900: CRC failed"));
  200. #endif
  201. vResumeRX();
  202. return false;
  203. }
  204. return (value & STATUS_PKT_BIT_MASK) > 0;
  205. }
  206. /**************************************************************************/
  207. // Read the RX buffer
  208. /**************************************************************************/
  209. int LT8900MiLightRadio::iReadRXBuffer(uint8_t *buffer, size_t maxBuffer) {
  210. size_t bufferIx = 0;
  211. uint16_t data;
  212. if (_currentPacketLen == 0) {
  213. if (! available()) {
  214. return -1;
  215. }
  216. data = uiReadRegister(R_FIFO);
  217. _currentPacketLen = (data >> 8);
  218. _currentPacketPos = 1;
  219. buffer[bufferIx++] = (data & 0xFF);
  220. }
  221. while (_currentPacketPos < _currentPacketLen && (bufferIx+1) < maxBuffer) {
  222. data = uiReadRegister(R_FIFO);
  223. buffer[bufferIx++] = data >> 8;
  224. buffer[bufferIx++] = data & 0xFF;
  225. _currentPacketPos += 2;
  226. }
  227. #ifdef DEBUG_PRINTF
  228. printf_P(
  229. PSTR("Read %d/%d bytes in RX, read %d bytes into buffer\n"),
  230. _currentPacketPos,
  231. _currentPacketLen,
  232. bufferIx
  233. );
  234. #endif
  235. if (_currentPacketPos >= _currentPacketLen) {
  236. _currentPacketPos = 0;
  237. _currentPacketLen = 0;
  238. }
  239. return bufferIx;
  240. }
  241. /**************************************************************************/
  242. // Set the active channel for the radio module
  243. /**************************************************************************/
  244. void LT8900MiLightRadio::vSetChannel(uint8_t channel)
  245. {
  246. _channel = channel;
  247. uiWriteRegister(R_CHANNEL, (_channel & CHANNEL_MASK));
  248. }
  249. /**************************************************************************/
  250. // Startup
  251. /**************************************************************************/
  252. int LT8900MiLightRadio::begin()
  253. {
  254. vSetChannel(_config.channels[0]);
  255. configure();
  256. available();
  257. return 0;
  258. }
  259. /**************************************************************************/
  260. // Configure the module according to type, and start listening
  261. /**************************************************************************/
  262. int LT8900MiLightRadio::configure()
  263. {
  264. vInitRadioModule();
  265. vSetSyncWord(_config.syncword3, 0,0,_config.syncword0);
  266. vStartListening(_config.channels[0]);
  267. return 0;
  268. }
  269. /**************************************************************************/
  270. // Check if data is available
  271. /**************************************************************************/
  272. bool LT8900MiLightRadio::available()
  273. {
  274. if (_currentPacketPos < _currentPacketLen) {
  275. return true;
  276. }
  277. return bAvailablePin() && bAvailableRegister();
  278. }
  279. /**************************************************************************/
  280. // Read received data from buffer to upper layer
  281. /**************************************************************************/
  282. int LT8900MiLightRadio::read(uint8_t frame[], size_t &frame_length)
  283. {
  284. if (!available()) {
  285. frame_length = 0;
  286. return -1;
  287. }
  288. #ifdef DEBUG_PRINTF
  289. Serial.println(F("LT8900: Radio was available, reading packet..."));
  290. #endif
  291. uint8_t buf[MILIGHT_MAX_PACKET_LENGTH];
  292. int packetSize = iReadRXBuffer(buf, MILIGHT_MAX_PACKET_LENGTH);
  293. if (packetSize > 0) {
  294. frame_length = packetSize;
  295. memcpy(frame, buf, packetSize);
  296. }
  297. vResumeRX();
  298. return packetSize;
  299. }
  300. /**************************************************************************/
  301. // Write data
  302. /**************************************************************************/
  303. int LT8900MiLightRadio::write(uint8_t frame[], size_t frame_length)
  304. {
  305. if (frame_length > sizeof(_out_packet) - 1) {
  306. return -1;
  307. }
  308. memcpy(_out_packet + 1, frame, frame_length);
  309. _out_packet[0] = frame_length;
  310. SPI.setDataMode(SPI_MODE1);
  311. int retval = resend();
  312. yield();
  313. SPI.setDataMode(SPI_MODE0);
  314. if (retval < 0) {
  315. return retval;
  316. }
  317. return frame_length;
  318. }
  319. /**************************************************************************/
  320. // Handle the transmission to regarding to freq diversity and repeats
  321. /**************************************************************************/
  322. int LT8900MiLightRadio::resend()
  323. {
  324. byte Length = _out_packet[0];
  325. for (size_t i = 0; i < MiLightRadioConfig::NUM_CHANNELS; i++)
  326. {
  327. sendPacket(_out_packet, Length, _config.channels[i]);
  328. delayMicroseconds(DEFAULT_TIME_BETWEEN_RETRANSMISSIONS_uS);
  329. }
  330. return 0;
  331. }
  332. /**************************************************************************/
  333. // The actual transmit happens here
  334. /**************************************************************************/
  335. bool LT8900MiLightRadio::sendPacket(uint8_t *data, size_t packetSize, byte byChannel)
  336. {
  337. if(_bConnected) // Must be connected to module otherwise it might lookup waiting for _pin_pktflag
  338. {
  339. if (packetSize < 1 || packetSize > 255)
  340. {
  341. return false;
  342. }
  343. uiWriteRegister(R_CHANNEL, 0x0000);
  344. uiWriteRegister(R_FIFO_CONTROL, 0x8080); //flush tx and RX
  345. digitalWrite(_csPin, LOW); // Enable PL1167 SPI transmission
  346. SPI.transfer(R_FIFO); // Start writing PL1167's FIFO Data register
  347. SPI.transfer(packetSize); // Length of data buffer: x bytes
  348. for (byte iCounter = 0; iCounter < packetSize; iCounter++)
  349. {
  350. SPI.transfer((data[1+iCounter]));
  351. }
  352. digitalWrite(_csPin, HIGH); // Disable PL1167 SPI transmission
  353. delayMicroseconds(10);
  354. uiWriteRegister(R_CHANNEL, (byChannel & CHANNEL_MASK) | _BV(CHANNEL_TX_BIT)); //enable RX
  355. //Wait until the packet is sent.
  356. while (digitalRead(_pin_pktflag) == 0)
  357. {
  358. //do nothing.
  359. }
  360. return true;
  361. }
  362. }
  363. const MiLightRadioConfig& LT8900MiLightRadio::config() {
  364. return _config;
  365. }