PL1167_nRF24.cpp 6.1 KB

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