PL1167_nRF24.cpp 5.8 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. static uint16_t calc_crc(uint8_t *data, size_t data_length);
  14. static uint8_t reverse_bits(uint8_t data);
  15. PL1167_nRF24::PL1167_nRF24(RF24 &radio)
  16. : _radio(radio)
  17. { }
  18. int PL1167_nRF24::open()
  19. {
  20. _radio.begin();
  21. _radio.setAutoAck(false);
  22. _radio.setDataRate(RF24_1MBPS);
  23. _radio.disableCRC();
  24. _syncwordLength = 5;
  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 (_syncword0 & 0x01) {
  33. _nrf_pipe[ --nrf_address_pos ] = reverse_bits( ( (_syncword0 << 4) & 0xf0 ) + 0x05 );
  34. } else {
  35. _nrf_pipe[ --nrf_address_pos ] = reverse_bits( ( (_syncword0 << 4) & 0xf0 ) + 0x0a );
  36. }
  37. _nrf_pipe[ --nrf_address_pos ] = reverse_bits( (_syncword0 >> 4) & 0xff);
  38. _nrf_pipe[ --nrf_address_pos ] = reverse_bits( ( (_syncword0 >> 12) & 0x0f ) + ( (_syncword3 << 4) & 0xf0) );
  39. _nrf_pipe[ --nrf_address_pos ] = reverse_bits( (_syncword3 >> 4) & 0xff);
  40. _nrf_pipe[ --nrf_address_pos ] = reverse_bits( ( (_syncword3 >> 12) & 0x0f ) + 0x50 ); // kh: spi says trailer is always "5" ?
  41. _receive_length = packet_length;
  42. _radio.openWritingPipe(_nrf_pipe);
  43. _radio.openReadingPipe(1, _nrf_pipe);
  44. _radio.setChannel(2 + _channel);
  45. _radio.setPayloadSize( packet_length );
  46. return 0;
  47. }
  48. int PL1167_nRF24::setTrailerLength(uint8_t trailerLength)
  49. { return 0; }
  50. /* kh- no thanks, I'll take care of that.
  51. One could argue there is potential value to "defining" the trailer - such that
  52. we can use those "values" for internal (repeateR?) functions since they are
  53. ignored by the real PL1167.. But there is no value in _this_ implementation...
  54. */
  55. int PL1167_nRF24::setCRC(bool crc)
  56. {
  57. _crc = crc;
  58. return recalc_parameters();
  59. }
  60. int PL1167_nRF24::setMaxPacketLength(uint8_t maxPacketLength)
  61. {
  62. _maxPacketLength = maxPacketLength;
  63. return recalc_parameters();
  64. }
  65. int PL1167_nRF24::receive(uint8_t channel)
  66. {
  67. if (channel != _channel) {
  68. _channel = channel;
  69. int retval = recalc_parameters();
  70. if (retval < 0) {
  71. return retval;
  72. }
  73. }
  74. _radio.startListening();
  75. if (_radio.available()) {
  76. #ifdef DEBUG_PRINTF
  77. printf("Radio is available\n");
  78. #endif
  79. internal_receive();
  80. }
  81. if(_received) {
  82. #ifdef DEBUG_PRINTF
  83. if (_packet_length > 0) {
  84. printf("Received packet (len = %d)!\n", _packet_length);
  85. }
  86. #endif
  87. return _packet_length;
  88. } else {
  89. return 0;
  90. }
  91. }
  92. int PL1167_nRF24::readFIFO(uint8_t data[], size_t &data_length)
  93. {
  94. if (data_length > _packet_length) {
  95. data_length = _packet_length;
  96. }
  97. memcpy(data, _packet, data_length);
  98. _packet_length -= data_length;
  99. if (_packet_length) {
  100. memmove(_packet, _packet + data_length, _packet_length);
  101. }
  102. return _packet_length;
  103. }
  104. int PL1167_nRF24::writeFIFO(const uint8_t data[], size_t data_length)
  105. {
  106. if (data_length > sizeof(_packet)) {
  107. data_length = sizeof(_packet);
  108. }
  109. memcpy(_packet, data, data_length);
  110. _packet_length = data_length;
  111. _received = false;
  112. return data_length;
  113. }
  114. int PL1167_nRF24::transmit(uint8_t channel)
  115. {
  116. if (channel != _channel) {
  117. _channel = channel;
  118. int retval = recalc_parameters();
  119. if (retval < 0) {
  120. return retval;
  121. }
  122. yield();
  123. }
  124. _radio.stopListening();
  125. uint8_t tmp[sizeof(_packet)];
  126. int outp=0;
  127. uint16_t crc;
  128. if (_crc) {
  129. crc = calc_crc(_packet, _packet_length);
  130. }
  131. for (int inp = 0; inp < _packet_length + (_crc ? 2 : 0) + 1; inp++) {
  132. if (inp < _packet_length) {
  133. tmp[outp++] = reverse_bits(_packet[inp]);}
  134. else if (_crc && inp < _packet_length + 2) {
  135. tmp[outp++] = reverse_bits((crc >> ( (inp - _packet_length) * 8)) & 0xff);
  136. }
  137. }
  138. yield();
  139. _radio.write(tmp, outp);
  140. return 0;
  141. }
  142. int PL1167_nRF24::internal_receive()
  143. {
  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. #ifdef DEBUG_PRINTF
  150. printf("Packet received: ");
  151. for (int i = 0; i < _receive_length; i++) {
  152. printf("%02X", tmp[i]);
  153. }
  154. printf("\n");
  155. #endif
  156. for (int inp = 0; inp < _receive_length; inp++) {
  157. tmp[outp++] = reverse_bits(tmp[inp]);
  158. }
  159. #ifdef DEBUG_PRINTF
  160. printf("Packet transformed: ");
  161. for (int i = 0; i < outp; i++) {
  162. printf("%02X", tmp[i]);
  163. }
  164. printf("\n");
  165. #endif
  166. if (_crc) {
  167. if (outp < 2) {
  168. #ifdef DEBUG_PRINTF
  169. printf("Failed CRC: outp < 2\n");
  170. #endif
  171. return 0;
  172. }
  173. uint16_t crc = calc_crc(tmp, outp - 2);
  174. if ( ((crc & 0xff) != tmp[outp - 2]) || (((crc >> 8) & 0xff) != tmp[outp - 1]) ) {
  175. #ifdef DEBUG_PRINTF
  176. uint16_t recv_crc = ((tmp[outp - 2] & 0xFF) << 8) | (tmp[outp - 1] & 0xFF);
  177. printf("Failed CRC: expected %d, got %d\n", crc, recv_crc);
  178. #endif
  179. return 0;
  180. }
  181. outp -= 2;
  182. }
  183. memcpy(_packet, tmp, outp);
  184. _packet_length = outp;
  185. _received = true;
  186. #ifdef DEBUG_PRINTF
  187. printf("Successfully parsed packet of length %d\n", _packet_length);
  188. #endif
  189. return outp;
  190. }
  191. #define CRC_POLY 0x8408
  192. static uint16_t calc_crc(uint8_t *data, size_t data_length) {
  193. uint16_t state = 0;
  194. for (size_t i = 0; i < data_length; i++) {
  195. uint8_t byte = data[i];
  196. for (int j = 0; j < 8; j++) {
  197. if ((byte ^ state) & 0x01) {
  198. state = (state >> 1) ^ CRC_POLY;
  199. } else {
  200. state = state >> 1;
  201. }
  202. byte = byte >> 1;
  203. }
  204. }
  205. return state;
  206. }
  207. static uint8_t reverse_bits(uint8_t data) {
  208. uint8_t result = 0;
  209. for (int i = 0; i < 8; i++) {
  210. result <<= 1;
  211. result |= data & 1;
  212. data >>= 1;
  213. }
  214. return result;
  215. }