PL1167_nRF24.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. internal_receive();
  123. }
  124. if(_received) {
  125. return _packet_length;
  126. } else {
  127. return 0;
  128. }
  129. }
  130. int PL1167_nRF24::readFIFO(uint8_t data[], size_t &data_length)
  131. {
  132. if (data_length > _packet_length) {
  133. data_length = _packet_length;
  134. }
  135. memcpy(data, _packet, data_length);
  136. _packet_length -= data_length;
  137. if (_packet_length) {
  138. memmove(_packet, _packet + data_length, _packet_length);
  139. }
  140. return _packet_length;
  141. }
  142. int PL1167_nRF24::writeFIFO(const uint8_t data[], size_t data_length)
  143. {
  144. if (data_length > sizeof(_packet)) {
  145. data_length = sizeof(_packet);
  146. }
  147. memcpy(_packet, data, data_length);
  148. _packet_length = data_length;
  149. _received = false;
  150. return data_length;
  151. }
  152. int PL1167_nRF24::transmit(uint8_t channel)
  153. {
  154. if (channel != _channel) {
  155. _channel = channel;
  156. int retval = recalc_parameters();
  157. if (retval < 0) {
  158. return retval;
  159. }
  160. }
  161. _radio.stopListening();
  162. uint8_t tmp[sizeof(_packet)];
  163. uint8_t trailer = (_packet[0] & 1) ? 0x55 : 0xAA; // NOTE: This is a guess, it might also be based upon the last
  164. // syncword bit, or fixed
  165. int outp = 0;
  166. for (; outp < _receive_length; outp++) {
  167. uint8_t outbyte = 0;
  168. if (outp + 1 + _nrf_pipe_length < _preambleLength) {
  169. outbyte = _preamble;
  170. } else if (outp + 1 + _nrf_pipe_length < _preambleLength + _syncwordLength) {
  171. int syncp = outp - _preambleLength + 1 + _nrf_pipe_length;
  172. switch (syncp) {
  173. case 0:
  174. outbyte = _syncword0 & 0xFF;
  175. break;
  176. case 1:
  177. outbyte = (_syncword0 >> 8) & 0xFF;
  178. break;
  179. case 2:
  180. outbyte = _syncword3 & 0xFF;
  181. break;
  182. case 3:
  183. outbyte = (_syncword3 >> 8) & 0xFF;
  184. break;
  185. }
  186. } else if (outp + 1 + _nrf_pipe_length < _preambleLength + _syncwordLength + (_trailerLength / 8) ) {
  187. outbyte = trailer;
  188. } else {
  189. break;
  190. }
  191. tmp[outp] = reverse_bits(outbyte);
  192. }
  193. int buffer_fill;
  194. bool last_round = false;
  195. uint16_t buffer = 0;
  196. uint16_t crc;
  197. if (_crc) {
  198. crc = calc_crc(_packet, _packet_length);
  199. }
  200. buffer = trailer >> (8 - (_trailerLength % 8));
  201. buffer_fill = _trailerLength % 8;
  202. for (int inp = 0; inp < _packet_length + (_crc ? 2 : 0) + 1; inp++) {
  203. if (inp < _packet_length) {
  204. buffer |= _packet[inp] << buffer_fill;
  205. buffer_fill += 8;
  206. } else if (_crc && inp < _packet_length + 2) {
  207. buffer |= ((crc >> ( (inp - _packet_length) * 8)) & 0xff) << buffer_fill;
  208. buffer_fill += 8;
  209. } else {
  210. last_round = true;
  211. }
  212. while (buffer_fill > (last_round ? 0 : 8)) {
  213. if (outp >= sizeof(tmp)) {
  214. return -1;
  215. }
  216. tmp[outp++] = reverse_bits(buffer & 0xff);
  217. buffer >>= 8;
  218. buffer_fill -= 8;
  219. }
  220. }
  221. _radio.write(tmp, outp);
  222. return 0;
  223. }
  224. int PL1167_nRF24::internal_receive()
  225. {
  226. uint8_t tmp[sizeof(_packet)];
  227. int outp = 0;
  228. _radio.read(tmp, _receive_length);
  229. // HACK HACK HACK: Reset radio
  230. open();
  231. uint8_t shift_amount = _trailerLength % 8;
  232. uint16_t buffer = 0;
  233. #ifdef DEBUG_PRINTF
  234. printf("Packet received: ");
  235. for (int i = 0; i < _receive_length; i++) {
  236. printf("%02X", reverse_bits(tmp[i]));
  237. }
  238. printf("\n");
  239. #endif
  240. for (int inp = 0; inp < _receive_length; inp++) {
  241. uint8_t inbyte = reverse_bits(tmp[inp]);
  242. buffer = (buffer >> 8) | (inbyte << 8);
  243. if (inp + 1 + _nrf_pipe_length < _preambleLength) {
  244. if (inbyte != _preamble) {
  245. #ifdef DEBUG_PRINTF
  246. printf("Preamble fail (%i: %02X)\n", inp, inbyte);
  247. #endif
  248. return 0;
  249. }
  250. } else if (inp + 1 + _nrf_pipe_length < _preambleLength + _syncwordLength) {
  251. int syncp = inp - _preambleLength + 1 + _nrf_pipe_length;
  252. switch (syncp) {
  253. case 0:
  254. if (inbyte != _syncword0 & 0xFF) {
  255. #ifdef DEBUG_PRINTF
  256. printf("Sync 0l fail (%i: %02X)\n", inp, inbyte);
  257. #endif
  258. return 0;
  259. } break;
  260. case 1:
  261. if (inbyte != (_syncword0 >> 8) & 0xFF) {
  262. #ifdef DEBUG_PRINTF
  263. printf("Sync 0h fail (%i: %02X)\n", inp, inbyte);
  264. #endif
  265. return 0;
  266. } break;
  267. case 2:
  268. if ((_syncwordLength == 4) && (inbyte != _syncword3 & 0xFF)) {
  269. #ifdef DEBUG_PRINTF
  270. printf("Sync 3l fail (%i: %02X)\n", inp, inbyte);
  271. #endif
  272. return 0;
  273. } break;
  274. case 3:
  275. if ((_syncwordLength == 4) && (inbyte != (_syncword3 >> 8) & 0xFF)) {
  276. #ifdef DEBUG_PRINTF
  277. printf("Sync 3h fail (%i: %02X)\n", inp, inbyte);
  278. #endif
  279. return 0;
  280. } break;
  281. }
  282. } else if (inp + 1 + _nrf_pipe_length < _preambleLength + _syncwordLength + ((_trailerLength + 7) / 8) ) {
  283. } else {
  284. tmp[outp++] = buffer >> shift_amount;
  285. }
  286. }
  287. #ifdef DEBUG_PRINTF
  288. printf("Packet transformed: ");
  289. for (int i = 0; i < outp; i++) {
  290. printf("%02X", tmp[i]);
  291. }
  292. printf("\n");
  293. #endif
  294. if (_crc) {
  295. if (outp < 2) {
  296. return 0;
  297. }
  298. uint16_t crc = calc_crc(tmp, outp - 2);
  299. if ( ((crc & 0xff) != tmp[outp - 2]) || (((crc >> 8) & 0xff) != tmp[outp - 1]) ) {
  300. return 0;
  301. }
  302. outp -= 2;
  303. }
  304. memcpy(_packet, tmp, outp);
  305. _packet_length = outp;
  306. _received = true;
  307. return outp;
  308. }
  309. #define CRC_POLY 0x8408
  310. static uint16_t calc_crc(uint8_t *data, size_t data_length) {
  311. uint16_t state = 0;
  312. for (size_t i = 0; i < data_length; i++) {
  313. uint8_t byte = data[i];
  314. for (int j = 0; j < 8; j++) {
  315. if ((byte ^ state) & 0x01) {
  316. state = (state >> 1) ^ CRC_POLY;
  317. } else {
  318. state = state >> 1;
  319. }
  320. byte = byte >> 1;
  321. }
  322. }
  323. return state;
  324. }
  325. static uint8_t reverse_bits(uint8_t data) {
  326. uint8_t result = 0;
  327. for (int i = 0; i < 8; i++) {
  328. result <<= 1;
  329. result |= data & 1;
  330. data >>= 1;
  331. }
  332. return result;
  333. }