LT8900MiLightRadio.cpp 17 KB

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