PL1167_nRF24.cpp 8.8 KB

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