LT8900MiLightRadio.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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(config.type);
  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(MiLightRadioType type)
  77. {
  78. if (type == RGB_CCT)
  79. {
  80. bool bWriteDefaultDefault = true; // Is it okay to use the default power up values, without setting them
  81. regWrite16(0x00, 0x6F, 0xE0, 7); // Recommended value by PMmicro
  82. regWrite16(0x02, 0x66, 0x17, 7); // Recommended value by PMmicro
  83. regWrite16(0x04, 0x9C, 0xC9, 7); // Recommended value by PMmicro
  84. regWrite16(0x05, 0x66, 0x37, 7); // Recommended value by PMmicro
  85. regWrite16(0x07, 0x00, 0x4C, 7); // PL1167's TX/RX Enable and Channel Register, Default channel 76
  86. regWrite16(0x08, 0x6C, 0x90, 7); // Recommended value by PMmicro
  87. regWrite16(0x09, 0x48, 0x00, 7); // PA Control register
  88. regWrite16(0x0B, 0x00, 0x08, 7); // Recommended value by PMmicro
  89. regWrite16(0x0D, 0x48, 0xBD, 7); // Recommended value by PMmicro
  90. regWrite16(0x16, 0x00, 0xFF, 7); // Recommended value by PMmicro
  91. regWrite16(0x18, 0x00, 0x67, 7); // Recommended value by PMmicro
  92. regWrite16(0x1A, 0x19, 0xE0, 7); // Recommended value by PMmicro
  93. regWrite16(0x1B, 0x13, 0x00, 7); // Recommended value by PMmicro
  94. regWrite16(0x20, 0x48, 0x00, 7); // Recommended value by PMmicro
  95. regWrite16(0x21, 0x3F, 0xC7, 7); // Recommended value by PMmicro
  96. regWrite16(0x22, 0x20, 0x00, 7); // Recommended value by PMmicro
  97. regWrite16(0x23, 0x03, 0x00, 7); // Recommended value by PMmicro
  98. regWrite16(0x24, 0x72, 0x36, 7); // Sync R0
  99. regWrite16(0x27, 0x18, 0x09, 7); // Sync R3
  100. regWrite16(0x28, 0x44, 0x02, 7); // Recommended value by PMmicro
  101. regWrite16(0x29, 0xB0, 0x00, 7); // Recommended value by PMmicro
  102. regWrite16(0x2A, 0xFD, 0xB0, 7); // Recommended value by PMmicro
  103. if (bWriteDefaultDefault == true)
  104. {
  105. regWrite16(0x01, 0x56, 0x81, 7); // Recommended value by PMmicro
  106. regWrite16(0x0A, 0x7F, 0xFD, 7); // Recommended value by PMmicro
  107. regWrite16(0x0C, 0x00, 0x00, 7); // Recommended value by PMmicro
  108. regWrite16(0x17, 0x80, 0x05, 7); // Recommended value by PMmicro
  109. regWrite16(0x19, 0x16, 0x59, 7); // Recommended value by PMmicro
  110. regWrite16(0x1C, 0x18, 0x00, 7); // Recommended value by PMmicro
  111. regWrite16(0x25, 0x00, 0x00, 7); // Recommended value by PMmicro
  112. regWrite16(0x26, 0x00, 0x00, 7); // Recommended value by PMmicro
  113. regWrite16(0x2B, 0x00, 0x0F, 7); // Recommended value by PMmicro
  114. }
  115. }
  116. else if((type == RGBW) || (type == CCT) || (type == RGB) )
  117. {
  118. regWrite16(0, 111, 224, 7); // Recommended value by PMmicro
  119. regWrite16(1, 86, 129, 7); // Recommended value by PMmicro
  120. regWrite16(2, 102, 23, 7); // Recommended value by PMmicro
  121. regWrite16(4, 156, 201, 7); // Recommended value by PMmicro
  122. regWrite16(5, 102, 55, 7); // Recommended value by PMmicro
  123. regWrite16(7, 0, 76, 7); // PL1167's TX/RX Enable and Channel Register
  124. regWrite16(8, 108, 144, 7); // Recommended value by PMmicro
  125. regWrite16(9, 72, 0, 7); // PL1167's PA Control Register
  126. regWrite16(10, 127, 253, 7); // Recommended value by PMmicro
  127. regWrite16(11, 0, 8, 7); // PL1167's RSSI OFF Control Register -- ???
  128. regWrite16(12, 0, 0, 7); // Recommended value by PMmicro
  129. regWrite16(13, 72, 189, 7); // Recommended value by PMmicro
  130. regWrite16(22, 0, 255, 7); // Recommended value by PMmicro
  131. regWrite16(23, 128, 5, 7); // PL1167's VCO Calibration Enable Register
  132. regWrite16(24, 0, 103, 7); // Recommended value by PMmicro
  133. regWrite16(25, 22, 89, 7); // Recommended value by PMmicro
  134. regWrite16(26, 25, 224, 7); // Recommended value by PMmicro
  135. regWrite16(27, 19, 0, 7); // Recommended value by PMmicro
  136. regWrite16(28, 24, 0, 7); // Recommended value by PMmicro
  137. regWrite16(32, 72, 0, 7); // PL1167's Data Configure Register: LEN_PREAMBLE = 010 -> (0xAAAAAA) 3 bytes, LEN_SYNCWORD = 01 -> 32 bits, LEN_TRAILER = 000 -> (0x05) 4 bits, TYPE_PKT_DAT = 00 -> NRZ law data, TYPE_FEC = 00 -> No FEC
  138. regWrite16(33, 63, 199, 7); // PL1167's Delay Time Control Register 0
  139. regWrite16(34, 32, 0, 7); // PL1167's Delay Time Control Register 1
  140. regWrite16(35, 3, 0, 7); // PL1167's Power Management and Miscellaneous Register
  141. regWrite16(40, 68, 2, 7); // PL1167's FIFO and SYNCWORD Threshold Register
  142. regWrite16(41, 176, 0, 7); // PL1167's Miscellaneous Register: CRC_ON = 1 -> ON, SCR_ON = 0 -> OFF, EN_PACK_LEN = 1 -> ON, FW_TERM_TX = 1 -> ON, AUTO_ACK = 0 -> OFF, PKT_LEVEL = 0 -> PKT active high, CRC_INIT_DAT = 0
  143. regWrite16(42, 253, 176, 7); // PL1167's SCAN RSSI Register 0
  144. regWrite16(43, 0, 15, 7); // PL1167's SCAN RSSI Register 1
  145. delay(200);
  146. regWrite16(128, 0, 0, 7);
  147. regWrite16(129, 255, 255, 7);
  148. regWrite16(130, 0, 0, 7);
  149. regWrite16(132, 0, 0, 7);
  150. regWrite16(133, 255, 255, 7);
  151. regWrite16(135, 255, 255, 7);
  152. regWrite16(136, 0, 0, 7);
  153. regWrite16(137, 255, 255, 7);
  154. regWrite16(138, 0, 0, 7);
  155. regWrite16(139, 255, 255, 7);
  156. regWrite16(140, 0, 0, 7);
  157. regWrite16(141, 255, 255, 7);
  158. regWrite16(150, 0, 0, 7);
  159. regWrite16(151, 255, 255, 7);
  160. regWrite16(152, 0, 0, 7);
  161. regWrite16(153, 255, 255, 7);
  162. regWrite16(154, 0, 0, 7);
  163. regWrite16(155, 255, 255, 7);
  164. regWrite16(156, 0, 0, 7);
  165. regWrite16(160, 0, 0, 7);
  166. regWrite16(161, 255, 255, 7);
  167. regWrite16(162, 0, 0, 7);
  168. regWrite16(163, 255, 255, 7);
  169. regWrite16(168, 0, 0, 7);
  170. regWrite16(169, 255, 255, 7);
  171. regWrite16(170, 0, 0, 7);
  172. regWrite16(171, 255, 255, 7);
  173. regWrite16(7, 0, 0, 7); // Disable TX/RX and set radio channel to 0
  174. }
  175. }
  176. /**************************************************************************/
  177. // Set sync word
  178. /**************************************************************************/
  179. void LT8900MiLightRadio::vSetSyncWord(uint16_t syncWord3, uint16_t syncWord2, uint16_t syncWord1, uint16_t syncWord0)
  180. {
  181. uiWriteRegister(R_SYNCWORD1, syncWord0);
  182. uiWriteRegister(R_SYNCWORD2, syncWord1);
  183. uiWriteRegister(R_SYNCWORD3, syncWord1);
  184. uiWriteRegister(R_SYNCWORD4, syncWord3);
  185. }
  186. /**************************************************************************/
  187. // Low level register write with delay
  188. /**************************************************************************/
  189. void LT8900MiLightRadio::regWrite16(byte ADDR, byte V1, byte V2, byte WAIT)
  190. {
  191. digitalWrite(_csPin, LOW);
  192. SPI.transfer(ADDR);
  193. SPI.transfer(V1);
  194. SPI.transfer(V2);
  195. digitalWrite(_csPin, HIGH);
  196. delayMicroseconds(WAIT);
  197. }
  198. /**************************************************************************/
  199. // Low level register read
  200. /**************************************************************************/
  201. uint16_t LT8900MiLightRadio::uiReadRegister(uint8_t reg)
  202. {
  203. SPI.setDataMode(SPI_MODE1);
  204. digitalWrite(_csPin, LOW);
  205. SPI.transfer(REGISTER_READ | (REGISTER_MASK & reg));
  206. uint8_t high = SPI.transfer(0x00);
  207. uint8_t low = SPI.transfer(0x00);
  208. digitalWrite(_csPin, HIGH);
  209. SPI.setDataMode(SPI_MODE0);
  210. return (high << 8 | low);
  211. }
  212. /**************************************************************************/
  213. // Low level 16bit register write
  214. /**************************************************************************/
  215. uint8_t LT8900MiLightRadio::uiWriteRegister(uint8_t reg, uint16_t data)
  216. {
  217. uint8_t high = data >> 8;
  218. uint8_t low = data & 0xFF;
  219. digitalWrite(_csPin, LOW);
  220. uint8_t result = SPI.transfer(REGISTER_WRITE | (REGISTER_MASK & reg));
  221. SPI.transfer(high);
  222. SPI.transfer(low);
  223. digitalWrite(_csPin, HIGH);
  224. return result;
  225. }
  226. /**************************************************************************/
  227. // Start listening on specified channel and syncword
  228. /**************************************************************************/
  229. void LT8900MiLightRadio::vStartListening(uint uiChannelToListenTo)
  230. {
  231. _dupes_received = 0;
  232. vSetSyncWord(_config.syncword3, 0,0,_config.syncword0);
  233. //vSetChannel(uiChannelToListenTo);
  234. _channel = uiChannelToListenTo;
  235. vResumeRX();
  236. delay(5);
  237. }
  238. /**************************************************************************/
  239. // Resume listening - without changing the channel and syncword
  240. /**************************************************************************/
  241. void LT8900MiLightRadio::vResumeRX(void)
  242. {
  243. _dupes_received = 0;
  244. uiWriteRegister(R_CHANNEL, _channel & CHANNEL_MASK); //turn off rx/tx
  245. delay(3);
  246. uiWriteRegister(R_FIFO_CONTROL, 0x0080); //flush rx
  247. uiWriteRegister(R_CHANNEL, (_channel & CHANNEL_MASK) | _BV(CHANNEL_RX_BIT)); //enable RX
  248. }
  249. /**************************************************************************/
  250. // Check if data is available using the hardware pin PKT_FLAG
  251. /**************************************************************************/
  252. bool LT8900MiLightRadio::bAvailablePin() {
  253. return digitalRead(_pin_pktflag) > 0;
  254. }
  255. /**************************************************************************/
  256. // Check if data is available using the PKT_FLAG state in the status register
  257. /**************************************************************************/
  258. bool LT8900MiLightRadio::bAvailableRegister() {
  259. //read the PKT_FLAG state; this can also be done with a hard wire.
  260. uint16_t value = uiReadRegister(R_STATUS);
  261. if (bitRead(value, STATUS_CRC_BIT) != 0) {
  262. Serial.println(F("LT8900: CRC failed"));
  263. vResumeRX();
  264. return false;
  265. }
  266. return (value & STATUS_PKT_BIT_MASK) > 0;
  267. }
  268. /**************************************************************************/
  269. // Read the RX buffer
  270. /**************************************************************************/
  271. int LT8900MiLightRadio::iReadRXBuffer(uint8_t *buffer, size_t maxBuffer) {
  272. size_t bufferIx = 0;
  273. uint16_t data;
  274. if (_currentPacketLen == 0) {
  275. if (! available()) {
  276. return -1;
  277. }
  278. data = uiReadRegister(R_FIFO);
  279. _currentPacketLen = (data >> 8);
  280. _currentPacketPos = 1;
  281. buffer[bufferIx++] = (data & 0xFF);
  282. }
  283. while (_currentPacketPos < _currentPacketLen && (bufferIx+1) < maxBuffer) {
  284. data = uiReadRegister(R_FIFO);
  285. buffer[bufferIx++] = data >> 8;
  286. buffer[bufferIx++] = data & 0xFF;
  287. _currentPacketPos += 2;
  288. }
  289. printf("Read %d/%d bytes in RX, read %d bytes into buffer\n", _currentPacketPos, _currentPacketLen, bufferIx);
  290. if (_currentPacketPos >= _currentPacketLen) {
  291. _currentPacketPos = 0;
  292. _currentPacketLen = 0;
  293. }
  294. return bufferIx;
  295. }
  296. /**************************************************************************/
  297. // Set the active channel for the radio module
  298. /**************************************************************************/
  299. void LT8900MiLightRadio::vSetChannel(uint8_t channel)
  300. {
  301. _channel = channel;
  302. uiWriteRegister(R_CHANNEL, (_channel & CHANNEL_MASK));
  303. }
  304. /**************************************************************************/
  305. // Startup
  306. /**************************************************************************/
  307. int LT8900MiLightRadio::begin()
  308. {
  309. vSetChannel(_config.channels[0]);
  310. configure();
  311. available();
  312. return 0;
  313. }
  314. /**************************************************************************/
  315. // Configure the module according to type, and start listening
  316. /**************************************************************************/
  317. int LT8900MiLightRadio::configure()
  318. {
  319. vInitRadioModule(_config.type);
  320. vSetSyncWord(_config.syncword3, 0,0,_config.syncword0);
  321. vStartListening(_config.channels[0]);
  322. return 0;
  323. }
  324. /**************************************************************************/
  325. // Check if data is available
  326. /**************************************************************************/
  327. bool LT8900MiLightRadio::available()
  328. {
  329. if (_currentPacketPos < _currentPacketLen) {
  330. return true;
  331. }
  332. return bAvailablePin() && bAvailableRegister();
  333. }
  334. /**************************************************************************/
  335. // Read received data from buffer to upper layer
  336. /**************************************************************************/
  337. int LT8900MiLightRadio::read(uint8_t frame[], size_t &frame_length)
  338. {
  339. if (!available()) {
  340. frame_length = 0;
  341. return -1;
  342. }
  343. #ifdef DEBUG_PRINTF
  344. Serial.println(F("LT8900: Radio was available, reading packet..."));
  345. #endif
  346. uint8_t buf[_config.getPacketLength()];
  347. int packetSize = iReadRXBuffer(buf, _config.getPacketLength());
  348. if (packetSize > 0) {
  349. frame_length = packetSize;
  350. memcpy(frame, buf, packetSize);
  351. }
  352. vResumeRX();
  353. return packetSize;
  354. }
  355. /**************************************************************************/
  356. // Write data
  357. /**************************************************************************/
  358. int LT8900MiLightRadio::write(uint8_t frame[], size_t frame_length)
  359. {
  360. if (frame_length > sizeof(_out_packet) - 1) {
  361. return -1;
  362. }
  363. memcpy(_out_packet + 1, frame, frame_length);
  364. _out_packet[0] = frame_length;
  365. SPI.setDataMode(SPI_MODE1);
  366. int retval = resend();
  367. yield();
  368. SPI.setDataMode(SPI_MODE0);
  369. if (retval < 0) {
  370. return retval;
  371. }
  372. return frame_length;
  373. }
  374. /**************************************************************************/
  375. // Handle the transmission to regarding to freq diversity and repeats
  376. /**************************************************************************/
  377. int LT8900MiLightRadio::resend()
  378. {
  379. byte Length = _out_packet[0];
  380. for (size_t i = 0; i < MiLightRadioConfig::NUM_CHANNELS; i++)
  381. {
  382. sendPacket(_out_packet, Length, _config.channels[i]);
  383. delayMicroseconds(DEFAULT_TIME_BETWEEN_RETRANSMISSIONS_uS);
  384. }
  385. return 0;
  386. }
  387. /**************************************************************************/
  388. // The actual transmit happens here
  389. /**************************************************************************/
  390. bool LT8900MiLightRadio::sendPacket(uint8_t *data, size_t packetSize, byte byChannel)
  391. {
  392. if(_bConnected) // Must be connected to module otherwise it might lookup waiting for _pin_pktflag
  393. {
  394. if (packetSize < 1 || packetSize > 255)
  395. {
  396. return false;
  397. }
  398. uiWriteRegister(R_CHANNEL, 0x0000);
  399. uiWriteRegister(R_FIFO_CONTROL, 0x8080); //flush tx and RX
  400. digitalWrite(_csPin, LOW); // Enable PL1167 SPI transmission
  401. SPI.transfer(R_FIFO); // Start writing PL1167's FIFO Data register
  402. SPI.transfer(packetSize); // Length of data buffer: x bytes
  403. for (byte iCounter = 0; iCounter < packetSize; iCounter++)
  404. {
  405. SPI.transfer((data[1+iCounter]));
  406. }
  407. digitalWrite(_csPin, HIGH); // Disable PL1167 SPI transmission
  408. delayMicroseconds(10);
  409. uiWriteRegister(R_CHANNEL, (byChannel & CHANNEL_MASK) | _BV(CHANNEL_TX_BIT)); //enable RX
  410. //Wait until the packet is sent.
  411. while (digitalRead(_pin_pktflag) == 0)
  412. {
  413. //do nothing.
  414. }
  415. return true;
  416. }
  417. }
  418. const MiLightRadioConfig& LT8900MiLightRadio::config() {
  419. return _config;
  420. }