PL1167_nRF24.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. {
  30. int packet_length = _maxPacketLength + 2;
  31. int nrf_address_pos = _syncwordLength;
  32. if (packet_length > sizeof(_packet) || nrf_address_length < 3) {
  33. return -1;
  34. }
  35. if (_syncwordBytes != nullptr) {
  36. _radio.openWritingPipe(_syncwordBytes);
  37. _radio.openReadingPipe(1, _syncwordBytes);
  38. }
  39. _receive_length = packet_length;
  40. _radio.setChannel(2 + _channel);
  41. _radio.setPayloadSize( packet_length );
  42. return 0;
  43. }
  44. int PL1167_nRF24::setSyncword(const uint8_t syncword[], size_t syncwordLength) {
  45. _syncwordLength = syncwordLength;
  46. _syncwordBytes = syncword;
  47. return recalc_parameters();
  48. }
  49. int PL1167_nRF24::setMaxPacketLength(uint8_t maxPacketLength) {
  50. _maxPacketLength = maxPacketLength;
  51. return recalc_parameters();
  52. }
  53. int PL1167_nRF24::receive(uint8_t channel) {
  54. if (channel != _channel) {
  55. _channel = channel;
  56. int retval = recalc_parameters();
  57. if (retval < 0) {
  58. return retval;
  59. }
  60. }
  61. _radio.startListening();
  62. if (_radio.available()) {
  63. #ifdef DEBUG_PRINTF
  64. printf("Radio is available\n");
  65. #endif
  66. internal_receive();
  67. }
  68. if(_received) {
  69. #ifdef DEBUG_PRINTF
  70. if (_packet_length > 0) {
  71. printf("Received packet (len = %d)!\n", _packet_length);
  72. }
  73. #endif
  74. return _packet_length;
  75. } else {
  76. return 0;
  77. }
  78. }
  79. int PL1167_nRF24::readFIFO(uint8_t data[], size_t &data_length)
  80. {
  81. if (data_length > _packet_length) {
  82. data_length = _packet_length;
  83. }
  84. memcpy(data, _packet, data_length);
  85. _packet_length -= data_length;
  86. if (_packet_length) {
  87. memmove(_packet, _packet + data_length, _packet_length);
  88. }
  89. return _packet_length;
  90. }
  91. int PL1167_nRF24::writeFIFO(const uint8_t data[], size_t data_length)
  92. {
  93. if (data_length > sizeof(_packet)) {
  94. data_length = sizeof(_packet);
  95. }
  96. memcpy(_packet, data, data_length);
  97. _packet_length = data_length;
  98. _received = false;
  99. return data_length;
  100. }
  101. int PL1167_nRF24::transmit(uint8_t channel) {
  102. if (channel != _channel) {
  103. _channel = channel;
  104. int retval = recalc_parameters();
  105. if (retval < 0) {
  106. return retval;
  107. }
  108. yield();
  109. }
  110. _radio.stopListening();
  111. uint8_t tmp[sizeof(_packet)];
  112. int outp=0;
  113. uint16_t crc;
  114. if (_crc) {
  115. crc = calc_crc(_packet, _packet_length);
  116. }
  117. for (int inp = 0; inp < _packet_length + (_crc ? 2 : 0) + 1; inp++) {
  118. if (inp < _packet_length) {
  119. tmp[outp++] = reverse_bits(_packet[inp]);}
  120. else if (_crc && inp < _packet_length + 2) {
  121. tmp[outp++] = reverse_bits((crc >> ( (inp - _packet_length) * 8)) & 0xff);
  122. }
  123. }
  124. yield();
  125. _radio.write(tmp, outp);
  126. return 0;
  127. }
  128. /**
  129. * The over-the-air packet structure sent by the PL1167 is as follows (lengths
  130. * measured in bits)
  131. *
  132. * Preamble ( 8) | Syncword (32) | Trailer ( 4) | Packet Len ( 8) | Packet (...)
  133. *
  134. * Note that because the Trailer is 4 bits, the remaining data is not byte-aligned.
  135. *
  136. * Bit-order is reversed.
  137. *
  138. */
  139. int PL1167_nRF24::internal_receive() {
  140. uint8_t tmp[sizeof(_packet)];
  141. int outp = 0;
  142. _radio.read(tmp, _receive_length);
  143. // HACK HACK HACK: Reset radio
  144. open();
  145. #ifdef DEBUG_PRINTF
  146. printf("Packet received: ");
  147. for (int i = 0; i < _receive_length; i++) {
  148. printf("%02X", tmp[i]);
  149. }
  150. printf("\n");
  151. #endif
  152. for (int inp = 0; inp < _receive_length; inp++) {
  153. tmp[outp++] = reverse_bits(tmp[inp]);
  154. }
  155. #ifdef DEBUG_PRINTF
  156. printf("Packet transformed: ");
  157. for (int i = 0; i < outp; i++) {
  158. printf("%02X", tmp[i]);
  159. }
  160. printf("\n");
  161. #endif
  162. if (_crc) {
  163. if (outp < 2) {
  164. #ifdef DEBUG_PRINTF
  165. printf("Failed CRC: outp < 2\n");
  166. #endif
  167. return 0;
  168. }
  169. uint16_t crc = calc_crc(tmp, outp - 2);
  170. if ( ((crc & 0xff) != tmp[outp - 2]) || (((crc >> 8) & 0xff) != tmp[outp - 1]) ) {
  171. #ifdef DEBUG_PRINTF
  172. uint16_t recv_crc = ((tmp[outp - 2] & 0xFF) << 8) | (tmp[outp - 1] & 0xFF);
  173. printf("Failed CRC: expected %d, got %d\n", crc, recv_crc);
  174. #endif
  175. return 0;
  176. }
  177. outp -= 2;
  178. }
  179. memcpy(_packet, tmp, outp);
  180. _packet_length = outp;
  181. _received = true;
  182. #ifdef DEBUG_PRINTF
  183. Serial.printf_P(PSTR("Successfully parsed packet of length %d\n"), _packet_length);
  184. #endif
  185. return outp;
  186. }
  187. #define CRC_POLY 0x8408
  188. static uint16_t calc_crc(uint8_t *data, size_t data_length) {
  189. uint16_t state = 0;
  190. for (size_t i = 0; i < data_length; i++) {
  191. uint8_t byte = data[i];
  192. for (int j = 0; j < 8; j++) {
  193. if ((byte ^ state) & 0x01) {
  194. state = (state >> 1) ^ CRC_POLY;
  195. } else {
  196. state = state >> 1;
  197. }
  198. byte = byte >> 1;
  199. }
  200. }
  201. return state;
  202. }
  203. static uint8_t reverse_bits(uint8_t data) {
  204. uint8_t result = 0;
  205. for (int i = 0; i < 8; i++) {
  206. result <<= 1;
  207. result |= data & 1;
  208. data >>= 1;
  209. }
  210. return result;
  211. }