PL1167_nRF24.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * PL1167_nRF24.cpp
  3. *
  4. * Created on: 29 May 2015
  5. * Author: henryk
  6. */
  7. #include "PL1167_nRF24.h"
  8. static uint16_t calc_crc(uint8_t *data, size_t data_length);
  9. static uint8_t reverse_bits(uint8_t data);
  10. static void demangle_packet(uint8_t *in, uint8_t *out) ;
  11. PL1167_nRF24::PL1167_nRF24(RF24 &radio)
  12. : _radio(radio)
  13. { }
  14. static const uint8_t pipe[] = {0xd1, 0x28, 0x5e, 0x55, 0x55};
  15. int PL1167_nRF24::open()
  16. {
  17. _radio.begin();
  18. return recalc_parameters();
  19. }
  20. int PL1167_nRF24::recalc_parameters()
  21. {
  22. int nrf_address_length = _preambleLength - 1 + _syncwordLength;
  23. int address_overflow = 0;
  24. if (nrf_address_length > 5) {
  25. address_overflow = nrf_address_length - 5;
  26. nrf_address_length = 5;
  27. }
  28. int packet_length = address_overflow + ( (_trailerLength + 7) / 8) + _maxPacketLength;
  29. if (_crc) {
  30. packet_length += 2;
  31. }
  32. if (packet_length > sizeof(_packet) || nrf_address_length < 3) {
  33. return -1;
  34. }
  35. uint8_t preamble = 0;
  36. if (_syncword0 & 0x01) {
  37. preamble = 0x55;
  38. } else {
  39. preamble = 0xAA;
  40. }
  41. int nrf_address_pos = nrf_address_length;
  42. for (int i = 0; i < _preambleLength - 1; i++) {
  43. _nrf_pipe[ --nrf_address_pos ] = reverse_bits(preamble);
  44. }
  45. if (nrf_address_pos) {
  46. _nrf_pipe[ --nrf_address_pos ] = reverse_bits(_syncword0 & 0xff);
  47. }
  48. if (nrf_address_pos) {
  49. _nrf_pipe[ --nrf_address_pos ] = reverse_bits( (_syncword0 >> 8) & 0xff);
  50. }
  51. if (_syncwordLength == 4) {
  52. if (nrf_address_pos) {
  53. _nrf_pipe[ --nrf_address_pos ] = reverse_bits(_syncword3 & 0xff);
  54. }
  55. if (nrf_address_pos) {
  56. _nrf_pipe[ --nrf_address_pos ] = reverse_bits( (_syncword3 >> 8) & 0xff);
  57. }
  58. }
  59. _receive_length = packet_length;
  60. _preamble = preamble;
  61. _nrf_pipe_length = nrf_address_length;
  62. _radio.setAddressWidth(_nrf_pipe_length);
  63. _radio.openWritingPipe(_nrf_pipe);
  64. _radio.openReadingPipe(1, _nrf_pipe);
  65. _radio.setChannel(2 + _channel);
  66. _radio.setPayloadSize( packet_length );
  67. _radio.setAutoAck(false);
  68. _radio.setPALevel(RF24_PA_MAX);
  69. _radio.setDataRate(RF24_1MBPS);
  70. _radio.disableCRC();
  71. return 0;
  72. }
  73. int PL1167_nRF24::setPreambleLength(uint8_t preambleLength)
  74. {
  75. if (preambleLength > 8) {
  76. return -1;
  77. }
  78. _preambleLength = preambleLength;
  79. return recalc_parameters();
  80. }
  81. int PL1167_nRF24::setSyncword(uint16_t syncword0, uint16_t syncword3)
  82. {
  83. _syncwordLength = 4;
  84. _syncword0 = syncword0;
  85. _syncword3 = syncword3;
  86. return recalc_parameters();
  87. }
  88. int PL1167_nRF24::setTrailerLength(uint8_t trailerLength)
  89. {
  90. if (trailerLength < 4) {
  91. return -1;
  92. }
  93. if (trailerLength > 18) {
  94. return -1;
  95. }
  96. if (trailerLength & 0x01) {
  97. return -1;
  98. }
  99. _trailerLength = trailerLength;
  100. return recalc_parameters();
  101. }
  102. int PL1167_nRF24::setCRC(bool crc)
  103. {
  104. _crc = crc;
  105. return recalc_parameters();
  106. }
  107. int PL1167_nRF24::setMaxPacketLength(uint8_t maxPacketLength)
  108. {
  109. _maxPacketLength = maxPacketLength;
  110. return recalc_parameters();
  111. }
  112. int PL1167_nRF24::receive(uint8_t channel)
  113. {
  114. if (channel != _channel) {
  115. _channel = channel;
  116. int retval = recalc_parameters();
  117. if (retval < 0) {
  118. return retval;
  119. }
  120. }
  121. _radio.startListening();
  122. if (_radio.available()) {
  123. #ifdef DEBUG_PRINTF
  124. printf("Radio is available\n");
  125. #endif
  126. internal_receive();
  127. }
  128. if(_received) {
  129. #ifdef DEBUG_PRINTF
  130. if (_packet_length > 0) {
  131. printf("Received packet (len = %d)!\n", _packet_length);
  132. }
  133. #endif
  134. return _packet_length;
  135. } else {
  136. return 0;
  137. }
  138. }
  139. int PL1167_nRF24::readFIFO(uint8_t data[], size_t &data_length)
  140. {
  141. if (data_length > _packet_length) {
  142. data_length = _packet_length;
  143. }
  144. memcpy(data, _packet, data_length);
  145. _packet_length -= data_length;
  146. if (_packet_length) {
  147. memmove(_packet, _packet + data_length, _packet_length);
  148. }
  149. return _packet_length;
  150. }
  151. int PL1167_nRF24::writeFIFO(const uint8_t data[], size_t data_length)
  152. {
  153. if (data_length > sizeof(_packet)) {
  154. data_length = sizeof(_packet);
  155. }
  156. memcpy(_packet, data, data_length);
  157. _packet_length = data_length;
  158. _received = false;
  159. return data_length;
  160. }
  161. int PL1167_nRF24::transmit(uint8_t channel)
  162. {
  163. if (channel != _channel) {
  164. _channel = channel;
  165. int retval = recalc_parameters();
  166. if (retval < 0) {
  167. return retval;
  168. }
  169. yield();
  170. }
  171. _radio.stopListening();
  172. uint8_t tmp[sizeof(_packet)];
  173. uint8_t trailer = (_packet[0] & 1) ? 0x55 : 0xAA; // NOTE: This is a guess, it might also be based upon the last
  174. // syncword bit, or fixed
  175. int outp = 0;
  176. for (; outp < _receive_length; outp++) {
  177. uint8_t outbyte = 0;
  178. if (outp + 1 + _nrf_pipe_length < _preambleLength) {
  179. outbyte = _preamble;
  180. } else if (outp + 1 + _nrf_pipe_length < _preambleLength + _syncwordLength) {
  181. int syncp = outp - _preambleLength + 1 + _nrf_pipe_length;
  182. switch (syncp) {
  183. case 0:
  184. outbyte = _syncword0 & 0xFF;
  185. break;
  186. case 1:
  187. outbyte = (_syncword0 >> 8) & 0xFF;
  188. break;
  189. case 2:
  190. outbyte = _syncword3 & 0xFF;
  191. break;
  192. case 3:
  193. outbyte = (_syncword3 >> 8) & 0xFF;
  194. break;
  195. }
  196. } else if (outp + 1 + _nrf_pipe_length < _preambleLength + _syncwordLength + (_trailerLength / 8) ) {
  197. outbyte = trailer;
  198. } else {
  199. break;
  200. }
  201. tmp[outp] = reverse_bits(outbyte);
  202. }
  203. int buffer_fill;
  204. bool last_round = false;
  205. uint16_t buffer = 0;
  206. uint16_t crc;
  207. if (_crc) {
  208. crc = calc_crc(_packet, _packet_length);
  209. }
  210. buffer = trailer >> (8 - (_trailerLength % 8));
  211. buffer_fill = _trailerLength % 8;
  212. for (int inp = 0; inp < _packet_length + (_crc ? 2 : 0) + 1; inp++) {
  213. if (inp < _packet_length) {
  214. buffer |= _packet[inp] << buffer_fill;
  215. buffer_fill += 8;
  216. } else if (_crc && inp < _packet_length + 2) {
  217. buffer |= ((crc >> ( (inp - _packet_length) * 8)) & 0xff) << buffer_fill;
  218. buffer_fill += 8;
  219. } else {
  220. last_round = true;
  221. }
  222. while (buffer_fill > (last_round ? 0 : 8)) {
  223. if (outp >= sizeof(tmp)) {
  224. return -1;
  225. }
  226. tmp[outp++] = reverse_bits(buffer & 0xff);
  227. buffer >>= 8;
  228. buffer_fill -= 8;
  229. }
  230. }
  231. yield();
  232. _radio.write(tmp, outp);
  233. return 0;
  234. }
  235. int PL1167_nRF24::internal_receive()
  236. {
  237. uint8_t tmp[sizeof(_packet)];
  238. int outp = 0;
  239. _radio.read(tmp, _receive_length);
  240. // HACK HACK HACK: Reset radio
  241. open();
  242. uint8_t shift_amount = _trailerLength % 8;
  243. uint16_t buffer = 0;
  244. #ifdef DEBUG_PRINTF
  245. printf("Packet received: ");
  246. for (int i = 0; i < _receive_length; i++) {
  247. printf("%02X", tmp[i]);
  248. }
  249. printf("\n");
  250. #endif
  251. for (int inp = 0; inp < _receive_length; inp++) {
  252. uint8_t inbyte = reverse_bits(tmp[inp]);
  253. buffer = (buffer >> 8) | (inbyte << 8);
  254. if (inp + 1 + _nrf_pipe_length < _preambleLength) {
  255. if (inbyte != _preamble) {
  256. #ifdef DEBUG_PRINTF
  257. printf("Preamble fail (%i: %02X)\n", inp, inbyte);
  258. #endif
  259. return 0;
  260. }
  261. } else if (inp + 1 + _nrf_pipe_length < _preambleLength + _syncwordLength) {
  262. int syncp = inp - _preambleLength + 1 + _nrf_pipe_length;
  263. switch (syncp) {
  264. case 0:
  265. if (inbyte != _syncword0 & 0xFF) {
  266. #ifdef DEBUG_PRINTF
  267. printf("Sync 0l fail (%i: %02X)\n", inp, inbyte);
  268. #endif
  269. return 0;
  270. } break;
  271. case 1:
  272. if (inbyte != (_syncword0 >> 8) & 0xFF) {
  273. #ifdef DEBUG_PRINTF
  274. printf("Sync 0h fail (%i: %02X)\n", inp, inbyte);
  275. #endif
  276. return 0;
  277. } break;
  278. case 2:
  279. if ((_syncwordLength == 4) && (inbyte != _syncword3 & 0xFF)) {
  280. #ifdef DEBUG_PRINTF
  281. printf("Sync 3l fail (%i: %02X)\n", inp, inbyte);
  282. #endif
  283. return 0;
  284. } break;
  285. case 3:
  286. if ((_syncwordLength == 4) && (inbyte != (_syncword3 >> 8) & 0xFF)) {
  287. #ifdef DEBUG_PRINTF
  288. printf("Sync 3h fail (%i: %02X)\n", inp, inbyte);
  289. #endif
  290. return 0;
  291. } break;
  292. }
  293. } else if (inp + 1 + _nrf_pipe_length < _preambleLength + _syncwordLength + ((_trailerLength + 7) / 8) ) {
  294. } else {
  295. tmp[outp++] = buffer >> shift_amount;
  296. }
  297. }
  298. #ifdef DEBUG_PRINTF
  299. printf("Packet transformed: ");
  300. for (int i = 0; i < outp; i++) {
  301. printf("%02X", tmp[i]);
  302. }
  303. printf("\n");
  304. #endif
  305. if (_crc) {
  306. if (outp < 2) {
  307. #ifdef DEBUG_PRINTF
  308. printf("Failed CRC: outp < 2\n");
  309. #endif
  310. return 0;
  311. }
  312. uint16_t crc = calc_crc(tmp, outp - 2);
  313. if ( ((crc & 0xff) != tmp[outp - 2]) || (((crc >> 8) & 0xff) != tmp[outp - 1]) ) {
  314. #ifdef DEBUG_PRINTF
  315. uint16_t recv_crc = ((tmp[outp - 2] & 0xFF) << 8) | (tmp[outp - 1] & 0xFF);
  316. printf("Failed CRC: expected %d, got %d\n", crc, recv_crc);
  317. #endif
  318. return 0;
  319. }
  320. outp -= 2;
  321. }
  322. memcpy(_packet, tmp, outp);
  323. _packet_length = outp;
  324. _received = true;
  325. #ifdef DEBUG_PRINTF
  326. printf("Successfully parsed packet of length %d\n", _packet_length);
  327. #endif
  328. return outp;
  329. }
  330. #define CRC_POLY 0x8408
  331. static uint16_t calc_crc(uint8_t *data, size_t data_length) {
  332. uint16_t state = 0;
  333. for (size_t i = 0; i < data_length; i++) {
  334. uint8_t byte = data[i];
  335. for (int j = 0; j < 8; j++) {
  336. if ((byte ^ state) & 0x01) {
  337. state = (state >> 1) ^ CRC_POLY;
  338. } else {
  339. state = state >> 1;
  340. }
  341. byte = byte >> 1;
  342. }
  343. }
  344. return state;
  345. }
  346. static uint8_t reverse_bits(uint8_t data) {
  347. uint8_t result = 0;
  348. for (int i = 0; i < 8; i++) {
  349. result <<= 1;
  350. result |= data & 1;
  351. data >>= 1;
  352. }
  353. return result;
  354. }