PL1167_nRF24.cpp 6.1 KB

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