PL1167_nRF24.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. static uint8_t reverse_bits(uint8_t data);
  17. PL1167_nRF24::PL1167_nRF24(RF24 &radio)
  18. : _radio(radio)
  19. { }
  20. int PL1167_nRF24::open()
  21. {
  22. _radio.begin();
  23. _radio.setAutoAck(false);
  24. _radio.setDataRate(RF24_1MBPS);
  25. _radio.disableCRC();
  26. _syncwordLength = MiLightRadioConfig::SYNCWORD_LENGTH;
  27. _radio.setAddressWidth(_syncwordLength);
  28. return recalc_parameters();
  29. }
  30. int PL1167_nRF24::recalc_parameters()
  31. {
  32. int packet_length = _maxPacketLength + 2;
  33. int nrf_address_pos = _syncwordLength;
  34. if (packet_length > sizeof(_packet) || nrf_address_length < 3) {
  35. return -1;
  36. }
  37. if (_syncwordBytes != nullptr) {
  38. _radio.openWritingPipe(_syncwordBytes);
  39. _radio.openReadingPipe(1, _syncwordBytes);
  40. }
  41. _receive_length = packet_length;
  42. _radio.setChannel(2 + _channel);
  43. _radio.setPayloadSize( packet_length );
  44. return 0;
  45. }
  46. int PL1167_nRF24::setSyncword(const uint8_t syncword[], size_t syncwordLength) {
  47. _syncwordLength = syncwordLength;
  48. _syncwordBytes = syncword;
  49. return recalc_parameters();
  50. }
  51. int PL1167_nRF24::setMaxPacketLength(uint8_t maxPacketLength)
  52. {
  53. _maxPacketLength = maxPacketLength;
  54. return recalc_parameters();
  55. }
  56. int PL1167_nRF24::receive(uint8_t channel)
  57. {
  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. {
  107. if (channel != _channel) {
  108. _channel = channel;
  109. int retval = recalc_parameters();
  110. if (retval < 0) {
  111. return retval;
  112. }
  113. yield();
  114. }
  115. _radio.stopListening();
  116. uint8_t tmp[sizeof(_packet)];
  117. int outp=0;
  118. uint16_t crc;
  119. if (_crc) {
  120. crc = calc_crc(_packet, _packet_length);
  121. }
  122. for (int inp = 0; inp < _packet_length + (_crc ? 2 : 0) + 1; inp++) {
  123. if (inp < _packet_length) {
  124. tmp[outp++] = reverse_bits(_packet[inp]);}
  125. else if (_crc && inp < _packet_length + 2) {
  126. tmp[outp++] = reverse_bits((crc >> ( (inp - _packet_length) * 8)) & 0xff);
  127. }
  128. }
  129. yield();
  130. _radio.write(tmp, outp);
  131. return 0;
  132. }
  133. int PL1167_nRF24::internal_receive()
  134. {
  135. uint8_t tmp[sizeof(_packet)];
  136. int outp = 0;
  137. _radio.read(tmp, _receive_length);
  138. // HACK HACK HACK: Reset radio
  139. open();
  140. #ifdef DEBUG_PRINTF
  141. printf("Packet received: ");
  142. for (int i = 0; i < _receive_length; i++) {
  143. printf("%02X", tmp[i]);
  144. }
  145. printf("\n");
  146. #endif
  147. for (int inp = 0; inp < _receive_length; inp++) {
  148. tmp[outp++] = reverse_bits(tmp[inp]);
  149. }
  150. #ifdef DEBUG_PRINTF
  151. printf("Packet transformed: ");
  152. for (int i = 0; i < outp; i++) {
  153. printf("%02X", tmp[i]);
  154. }
  155. printf("\n");
  156. #endif
  157. if (_crc) {
  158. if (outp < 2) {
  159. #ifdef DEBUG_PRINTF
  160. printf("Failed CRC: outp < 2\n");
  161. #endif
  162. return 0;
  163. }
  164. uint16_t crc = calc_crc(tmp, outp - 2);
  165. if ( ((crc & 0xff) != tmp[outp - 2]) || (((crc >> 8) & 0xff) != tmp[outp - 1]) ) {
  166. #ifdef DEBUG_PRINTF
  167. uint16_t recv_crc = ((tmp[outp - 2] & 0xFF) << 8) | (tmp[outp - 1] & 0xFF);
  168. printf("Failed CRC: expected %d, got %d\n", crc, recv_crc);
  169. #endif
  170. return 0;
  171. }
  172. outp -= 2;
  173. }
  174. memcpy(_packet, tmp, outp);
  175. _packet_length = outp;
  176. _received = true;
  177. #ifdef DEBUG_PRINTF
  178. printf("Successfully parsed packet of length %d\n", _packet_length);
  179. #endif
  180. return outp;
  181. }
  182. #define CRC_POLY 0x8408
  183. static uint16_t calc_crc(uint8_t *data, size_t data_length) {
  184. uint16_t state = 0;
  185. for (size_t i = 0; i < data_length; i++) {
  186. uint8_t byte = data[i];
  187. for (int j = 0; j < 8; j++) {
  188. if ((byte ^ state) & 0x01) {
  189. state = (state >> 1) ^ CRC_POLY;
  190. } else {
  191. state = state >> 1;
  192. }
  193. byte = byte >> 1;
  194. }
  195. }
  196. return state;
  197. }
  198. static uint8_t reverse_bits(uint8_t data) {
  199. uint8_t result = 0;
  200. for (int i = 0; i < 8; i++) {
  201. result <<= 1;
  202. result |= data & 1;
  203. data >>= 1;
  204. }
  205. return result;
  206. }