PL1167_nRF24.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * PL1167_nRF24.cpp
  3. *
  4. * Adapted from work by henryk:
  5. * https://github.com/henryk/openmili
  6. * Created on: 29 May 2015
  7. * Author: henryk
  8. * Optimizations by khamann:
  9. * https://github.com/khmann/esp8266_milight_hub/blob/e3600cef75b102ff3be51a7afdb55ab7460fe712/lib/MiLight/PL1167_nRF24.cpp
  10. *
  11. */
  12. #include "PL1167_nRF24.h"
  13. #include <RadioUtils.h>
  14. #include <MiLightRadioConfig.h>
  15. static uint16_t calc_crc(uint8_t *data, size_t data_length);
  16. PL1167_nRF24::PL1167_nRF24(RF24 &radio)
  17. : _radio(radio)
  18. { }
  19. int PL1167_nRF24::open() {
  20. _radio.begin();
  21. _radio.setAutoAck(false);
  22. _radio.setDataRate(RF24_1MBPS);
  23. _radio.disableCRC();
  24. _syncwordLength = MiLightRadioConfig::SYNCWORD_LENGTH;
  25. _radio.setAddressWidth(_syncwordLength);
  26. return recalc_parameters();
  27. }
  28. int PL1167_nRF24::recalc_parameters() {
  29. size_t nrf_address_length = _syncwordLength;
  30. // +2 for CRC
  31. size_t packet_length = _maxPacketLength + 2;
  32. // Read an extra byte if we don't include the trailer in the syncword
  33. if (_syncwordLength < 5) {
  34. ++packet_length;
  35. }
  36. if (packet_length > sizeof(_packet) || nrf_address_length < 3) {
  37. return -1;
  38. }
  39. if (_syncwordBytes != nullptr) {
  40. _radio.openWritingPipe(_syncwordBytes);
  41. _radio.openReadingPipe(1, _syncwordBytes);
  42. }
  43. _receive_length = packet_length;
  44. _radio.setChannel(2 + _channel);
  45. _radio.setPayloadSize( packet_length );
  46. return 0;
  47. }
  48. int PL1167_nRF24::setSyncword(const uint8_t syncword[], size_t syncwordLength) {
  49. _syncwordLength = syncwordLength;
  50. _syncwordBytes = syncword;
  51. return recalc_parameters();
  52. }
  53. int PL1167_nRF24::setMaxPacketLength(uint8_t maxPacketLength) {
  54. _maxPacketLength = maxPacketLength;
  55. return recalc_parameters();
  56. }
  57. int PL1167_nRF24::receive(uint8_t channel) {
  58. if (channel != _channel) {
  59. _channel = channel;
  60. int retval = recalc_parameters();
  61. if (retval < 0) {
  62. return retval;
  63. }
  64. }
  65. _radio.startListening();
  66. if (_radio.available()) {
  67. #ifdef DEBUG_PRINTF
  68. printf("Radio is available\n");
  69. #endif
  70. internal_receive();
  71. }
  72. if(_received) {
  73. #ifdef DEBUG_PRINTF
  74. if (_packet_length > 0) {
  75. printf("Received packet (len = %d)!\n", _packet_length);
  76. }
  77. #endif
  78. return _packet_length;
  79. } else {
  80. return 0;
  81. }
  82. }
  83. int PL1167_nRF24::readFIFO(uint8_t data[], size_t &data_length)
  84. {
  85. if (data_length > _packet_length) {
  86. data_length = _packet_length;
  87. }
  88. memcpy(data, _packet, data_length);
  89. _packet_length -= data_length;
  90. if (_packet_length) {
  91. memmove(_packet, _packet + data_length, _packet_length);
  92. }
  93. return _packet_length;
  94. }
  95. int PL1167_nRF24::writeFIFO(const uint8_t data[], size_t data_length)
  96. {
  97. if (data_length > sizeof(_packet)) {
  98. data_length = sizeof(_packet);
  99. }
  100. memcpy(_packet, data, data_length);
  101. _packet_length = data_length;
  102. _received = false;
  103. return data_length;
  104. }
  105. int PL1167_nRF24::transmit(uint8_t channel) {
  106. if (channel != _channel) {
  107. _channel = channel;
  108. int retval = recalc_parameters();
  109. if (retval < 0) {
  110. return retval;
  111. }
  112. yield();
  113. }
  114. _radio.stopListening();
  115. uint8_t tmp[sizeof(_packet)];
  116. int outp=0;
  117. uint16_t crc = calc_crc(_packet, _packet_length);
  118. // +1 for packet length
  119. // +2 for crc
  120. // = 3
  121. for (int inp = 0; inp < _packet_length + 3; inp++) {
  122. if (inp < _packet_length) {
  123. tmp[outp++] = reverseBits(_packet[inp]);}
  124. else if (inp < _packet_length + 2) {
  125. tmp[outp++] = reverseBits((crc >> ( (inp - _packet_length) * 8)) & 0xff);
  126. }
  127. }
  128. yield();
  129. _radio.write(tmp, outp);
  130. return 0;
  131. }
  132. /**
  133. * The over-the-air packet structure sent by the PL1167 is as follows (lengths
  134. * measured in bits)
  135. *
  136. * Preamble ( 8) | Syncword (32) | Trailer ( 4) | Packet Len ( 8) | Packet (...)
  137. *
  138. * Note that because the Trailer is 4 bits, the remaining data is not byte-aligned.
  139. *
  140. * Bit-order is reversed.
  141. *
  142. */
  143. int PL1167_nRF24::internal_receive() {
  144. uint8_t tmp[sizeof(_packet)];
  145. int outp = 0;
  146. _radio.read(tmp, _receive_length);
  147. // HACK HACK HACK: Reset radio
  148. open();
  149. // Currently, the syncword width is set to 5 in order to include the
  150. // PL1167 trailer. The trailer is 4 bits, which pushes packet data
  151. // out of byte-alignment.
  152. //
  153. // The following code reads un-byte-aligned packet data.
  154. //
  155. // #ifdef DEBUG_PRINTF
  156. // Serial.printf_P(PSTR("Packet received (%d bytes) RAW: "), outp);
  157. // for (int i = 0; i < _receive_length; i++) {
  158. // Serial.printf_P(PSTR("%02X "), tmp[i]);
  159. // }
  160. // Serial.print(F("\n"));
  161. // #endif
  162. //
  163. // uint16_t buffer = tmp[0];
  164. //
  165. // for (int inp = 1; inp < _receive_length; inp++) {
  166. // uint8_t currentByte = tmp[inp];
  167. // tmp[outp++] = reverseBits((buffer << 4) | (currentByte >> 4));
  168. // buffer = (buffer << 8) | currentByte;
  169. // }
  170. for (int inp = 0; inp < _receive_length; inp++) {
  171. tmp[outp++] = reverseBits(tmp[inp]);
  172. }
  173. #ifdef DEBUG_PRINTF
  174. Serial.printf_P(PSTR("Packet received (%d bytes): "), outp);
  175. for (int i = 0; i < outp; i++) {
  176. Serial.printf_P(PSTR("%02X "), tmp[i]);
  177. }
  178. Serial.print(F("\n"));
  179. #endif
  180. if (outp < 2) {
  181. #ifdef DEBUG_PRINTF
  182. Serial.println(F("Failed CRC: outp < 2"));
  183. #endif
  184. return 0;
  185. }
  186. uint16_t crc = calc_crc(tmp, outp - 2);
  187. uint16_t recvCrc = (tmp[outp - 1] << 8) | tmp[outp - 2];
  188. if ( crc != recvCrc ) {
  189. #ifdef DEBUG_PRINTF
  190. Serial.printf_P(PSTR("Failed CRC: expected %04X, got %04X\n"), crc, recvCrc);
  191. #endif
  192. return 0;
  193. }
  194. outp -= 2;
  195. memcpy(_packet, tmp, outp);
  196. _packet_length = outp;
  197. _received = true;
  198. #ifdef DEBUG_PRINTF
  199. Serial.printf_P(PSTR("Successfully parsed packet of length %d\n"), _packet_length);
  200. #endif
  201. return outp;
  202. }
  203. #define CRC_POLY 0x8408
  204. static uint16_t calc_crc(uint8_t *data, size_t data_length) {
  205. uint16_t state = 0;
  206. for (size_t i = 0; i < data_length; i++) {
  207. uint8_t byte = data[i];
  208. for (int j = 0; j < 8; j++) {
  209. if ((byte ^ state) & 0x01) {
  210. state = (state >> 1) ^ CRC_POLY;
  211. } else {
  212. state = state >> 1;
  213. }
  214. byte = byte >> 1;
  215. }
  216. }
  217. return state;
  218. }