Guest User

cc1101.cpp

a guest
Jul 27th, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.77 KB | None | 0 0
  1. #include "cc1101.h"
  2.  
  3. // Select (SPI) CC1101
  4. #define cc1101_Select() digitalWrite(chipSelectPin, LOW)
  5. // Deselect (SPI) CC1101
  6. #define cc1101_Deselect() digitalWrite(chipSelectPin, HIGH)
  7. // Wait until SPI MISO line goes low
  8. #define wait_Miso() while (digitalRead(MISO) > 0)
  9.  
  10. CC1101::CC1101(void) {}
  11.  
  12. void CC1101::writeReg(byte regAddr, byte value)
  13. {
  14. cc1101_Select(); // Select CC1101
  15. wait_Miso(); // Wait until MISO goes low
  16. SPI.transfer(regAddr); // Send register address
  17. SPI.transfer(value); // Send value
  18. cc1101_Deselect(); // Deselect CC1101
  19. }
  20.  
  21. void CC1101::writeBurstReg(byte regAddr, byte *buffer, byte len)
  22. {
  23. byte addr, i;
  24.  
  25. addr = regAddr | WRITE_BURST; // Enable burst transfer
  26. cc1101_Select(); // Select CC1101
  27. wait_Miso(); // Wait until MISO goes low
  28. SPI.transfer(addr); // Send register address
  29.  
  30. for (i = 0; i < len; i++)
  31. SPI.transfer(buffer[i]); // Send value
  32.  
  33. cc1101_Deselect(); // Deselect CC1101
  34. }
  35.  
  36. void CC1101::cmdStrobe(byte cmd)
  37. {
  38. cc1101_Select(); // Select CC1101
  39. wait_Miso(); // Wait until MISO goes low
  40. SPI.transfer(cmd); // Send strobe command
  41. cc1101_Deselect(); // Deselect CC1101
  42. }
  43.  
  44. byte CC1101::readReg(byte regAddr, byte regType)
  45. {
  46. byte addr, val;
  47.  
  48. addr = regAddr | regType;
  49. cc1101_Select(); // Select CC1101
  50. wait_Miso(); // Wait until MISO goes low
  51. SPI.transfer(addr); // Send register address
  52. val = SPI.transfer(0x00); // Read result
  53. cc1101_Deselect(); // Deselect CC1101
  54.  
  55. return val;
  56. }
  57.  
  58. void CC1101::readBurstReg(byte *buffer, byte regAddr, byte len)
  59. {
  60. byte addr, i;
  61.  
  62. addr = regAddr | READ_BURST;
  63. cc1101_Select(); // Select CC1101
  64. wait_Miso(); // Wait until MISO goes low
  65. SPI.transfer(addr); // Send register address
  66. for (i = 0; i < len; i++)
  67. buffer[i] = SPI.transfer(0x00); // Read result byte by byte
  68. cc1101_Deselect(); // Deselect CC1101
  69. }
  70.  
  71. void CC1101::reset(void)
  72. {
  73. cc1101_Deselect(); // Deselect CC1101
  74. delayMicroseconds(5);
  75. cc1101_Select(); // Select CC1101
  76. delayMicroseconds(10);
  77. cc1101_Deselect(); // Deselect CC1101
  78. delayMicroseconds(41);
  79. cc1101_Select(); // Select CC1101
  80.  
  81. wait_Miso(); // Wait until MISO goes low
  82. SPI.transfer(CC1101_SRES); // Send reset command strobe
  83. wait_Miso(); // Wait until MISO goes low
  84.  
  85. cc1101_Deselect(); // Deselect CC1101
  86. }
  87.  
  88. void CC1101::config_registers(void)
  89. {
  90. reset();
  91.  
  92. // ########## BEGIN FAN RF15 chip config ##########
  93. setCarrierFreq(CFREQ_868);
  94. writeReg(CC1101_IOCFG0, CC1101_DEFVAL_IOCFG0); // High impdance 3-state
  95. writeReg(CC1101_IOCFG2, CC1101_DEFVAL_IOCFG2); // Lock detector
  96. writeReg(CC1101_FSCTRL1, CC1101_DEFVAL_FSCTRL1);
  97. writeReg(CC1101_FSCTRL0, CC1101_DEFVAL_FSCTRL0);
  98. writeReg(CC1101_MDMCFG4, CC1101_DEFVAL_MDMCFG4);
  99. writeReg(CC1101_MDMCFG3, CC1101_DEFVAL_MDMCFG3);
  100. writeReg(CC1101_MDMCFG2, CC1101_DEFVAL_MDMCFG2);
  101. writeReg(CC1101_MDMCFG1, CC1101_DEFVAL_MDMCFG1);
  102. writeReg(CC1101_MDMCFG0, CC1101_DEFVAL_MDMCFG0);
  103. writeReg(CC1101_CHANNR, CC1101_DEFVAL_CHANNR);
  104. writeReg(CC1101_DEVIATN, CC1101_DEFVAL_DEVIATN);
  105. writeReg(CC1101_FREND1, CC1101_DEFVAL_FREND1);
  106. writeReg(CC1101_FREND0, CC1101_DEFVAL_FREND0);
  107. writeReg(CC1101_MCSM0, CC1101_DEFVAL_MCSM0);
  108. writeReg(CC1101_FOCCFG, CC1101_DEFVAL_FOCCFG);
  109. writeReg(CC1101_BSCFG, CC1101_DEFVAL_BSCFG);
  110. writeReg(CC1101_AGCCTRL2, CC1101_DEFVAL_AGCCTRL2);
  111. writeReg(CC1101_AGCCTRL1, CC1101_DEFVAL_AGCCTRL1);
  112. writeReg(CC1101_AGCCTRL0, CC1101_DEFVAL_AGCCTRL0);
  113. writeReg(CC1101_WORCTRL, CC1101_DEFVAL_WORCTRL);
  114. writeReg(CC1101_FSCAL3, CC1101_DEFVAL_FSCAL3);
  115. writeReg(CC1101_FSCAL2, CC1101_DEFVAL_FSCAL2);
  116. writeReg(CC1101_FSCAL1, CC1101_DEFVAL_FSCAL1);
  117. writeReg(CC1101_FSCAL0, CC1101_DEFVAL_FSCAL0);
  118. writeReg(CC1101_TEST0, CC1101_DEFVAL_TEST0);
  119. writeReg(CC1101_PKTCTRL1, CC1101_DEFVAL_PKTCTRL1);
  120. writeReg(CC1101_PKTCTRL0, CC1101_DEFVAL_PKTCTRL0);
  121.  
  122. uint8_t tx_buf[8] = { 0x6F, 0x26, 0x2E, 0x7F, 0x8A, 0x84, 0xCA, 0xC4
  123. };
  124. writeBurstReg(CC1101_UNKNOWNFIFO, tx_buf, 8);
  125. cmdStrobe(CC1101_SIDLE);
  126. cmdStrobe(CC1101_SIDLE);
  127.  
  128. }
  129.  
  130. void CC1101::init(void)
  131. {
  132. pinMode(chipSelectPin, OUTPUT); // Make sure that the SS Pin is declared as an Output
  133. SPI.begin(); // Initialize SPI interface
  134. pinMode(CC1101_GDO2, INPUT); // Config GDO0 as input
  135. reset(); // Reset CC1101
  136.  
  137. // Back to idle/power down
  138. setPowerDownState();
  139.  
  140. // Constants
  141. msg_id[indoor_hum].code_id = 0x12A0;
  142. msg_id[fan_speed].code_id = 0x22F1;
  143. msg_id[fan_info].code_id = 0x31DA;
  144.  
  145. }
  146.  
  147. uint8_t CC1101::calc_crc(uint8_t dataframe[], uint8_t len)
  148. {
  149. int crc_calc = 0;
  150.  
  151. for (uint8_t i = 0; i < len - 1; i++)
  152. crc_calc += dataframe[i];
  153.  
  154. while (crc_calc > 256)
  155. crc_calc -= 256;
  156. crc_calc = 256 - crc_calc;
  157.  
  158. return crc_calc;
  159. }
  160.  
  161. bool CC1101::clone_mode(void)
  162. {
  163. const uint8_t buff_lenght = 75;
  164. uint8_t rx_buffer[buff_lenght];
  165. uint8_t rbuf[buff_lenght];
  166. int rlen = 0;
  167. bool rf15_frame_valid = false;
  168. unsigned long previousMillis = 0;
  169. unsigned long currentMillis = 0;
  170. bool rx_abort_flag = false;
  171. bool header_detected_flag = false;
  172.  
  173. Serial1.setTimeout(RX_TIME_OUT);
  174.  
  175. // Clear input buffer
  176. uint8_t temp_var;
  177. while (Serial1.available())
  178. temp_var = Serial1.read();
  179.  
  180. previousMillis = millis();
  181.  
  182. // Process RX data
  183. while (!rx_abort_flag) // RX complete or timeout
  184. {
  185. if (!header_detected_flag)
  186. {
  187. while (Serial1.available() > 0)
  188. {
  189. for (uint8_t i = 0; i < 5; i++)
  190. rx_buffer[i] = rx_buffer[i + 1];
  191. rx_buffer[5] = Serial1.read();
  192.  
  193. if ( (rx_buffer[5] == 0x53) && (rx_buffer[4] == 0x55) && (rx_buffer[3] == 0x33) && (rx_buffer[2] == 0x00) && (rx_buffer[0] == 0x55))
  194. {
  195. header_detected_flag = true;
  196. break;
  197. }
  198. else
  199. {
  200. currentMillis = millis();
  201. if (currentMillis - previousMillis > PAIR_TIME_OUT)
  202. {
  203. rx_abort_flag = true;
  204. break;
  205. }
  206. }
  207. }
  208. }
  209. else
  210. {
  211. rlen = Serial1.readBytesUntil(0x35, rbuf, sizeof(rbuf) - 1); // read until 0x35 received or timeout
  212. rx_abort_flag = true;
  213. }
  214. }
  215.  
  216. // Process data
  217. if ( (rlen == 0) || (rlen == 100)) // No valid data
  218. {
  219. #ifdef DEBUG_MODE
  220. if (!header_detected_flag)
  221. Serial.println("> Error: no header detected!");
  222. else
  223. Serial.println("> header detected, but no valid data!");
  224. #endif
  225.  
  226. }
  227. else
  228. {
  229. // check if encoded number of bytes is even number
  230. if ((rlen % 2) != 0)
  231. {
  232. #ifdef DEBUG_MODE
  233. Serial.println("> Error: not even number, problaby corrupted msg, problematic for decoding");
  234. #endif
  235. }
  236. else
  237. {
  238. uint8_t dataframe_decoded[rlen / 2];
  239. manchester_decode(rbuf, rlen, dataframe_decoded);
  240.  
  241. #ifdef DEBUG_MODE
  242. Serial.print("> RX decoded data used for cloning: ");
  243. for (int i = 0; i < rlen / 2; i++)
  244. {
  245. Serial.print(dataframe_decoded[i], HEX);
  246. Serial.print(" ");
  247. }
  248. Serial.print(" | ");
  249. #endif
  250.  
  251. // CRC check
  252. if (calc_crc(dataframe_decoded, rlen / 2) == dataframe_decoded[(rlen / 2) - 1])
  253. {
  254. // Clone and store RF15 address in EEPROM
  255. for (uint8_t i = 1; i < 7; i++)
  256. {
  257. new_fan_state.address[i - 1] = dataframe_decoded[i];
  258. }
  259.  
  260. rf15_frame_valid = true;
  261. }
  262. }
  263.  
  264. }
  265.  
  266. return rf15_frame_valid;
  267.  
  268. }
  269.  
  270. uint8_t *CC1101::manchester_decode(uint8_t rx_buff[], uint8_t len, uint8_t *rx_payload)
  271. {
  272. uint8_t payload_cntr = 0;
  273. const uint8_t mch_lookup[16] = {0xAA, 0xA9, 0xA6, 0xA5, 0x9A, 0x99, 0x96, 0x95, 0x6A, 0x69, 0x66, 0x65, 0x5A, 0x59, 0x56, 0x55};
  274.  
  275. for (uint8_t i = 0; i < len; i++)
  276. {
  277. for (uint8_t x = 0; x < 16; x++)
  278. {
  279. if (rx_buff[i + 1] == mch_lookup[x])
  280. rx_payload[payload_cntr] = x;
  281. }
  282. for (uint8_t x = 0; x < 16; x++)
  283. {
  284. if (rx_buff[i] == mch_lookup[x])
  285. rx_payload[payload_cntr] |= x << 4;
  286. }
  287. payload_cntr++;
  288. i++;
  289. }
  290.  
  291. return rx_payload;
  292. }
  293.  
  294. uint8_t *CC1101::manchester_encode(uint8_t tx_buff[], uint8_t len, uint8_t *payload_encoded)
  295. {
  296. const uint8_t mch_lookup[16] = {0xAA, 0xA9, 0xA6, 0xA5, 0x9A, 0x99, 0x96, 0x95, 0x6A, 0x69, 0x66, 0x65, 0x5A, 0x59, 0x56, 0x55};
  297.  
  298. // Manchester encode
  299. uint8_t tx_cntr = 0;
  300. uint8_t temp_var = 0;
  301.  
  302. for (int i = 0; i < len; i++)
  303. {
  304. temp_var = tx_buff[i] & B11110000;
  305. payload_encoded[tx_cntr] = mch_lookup[temp_var >> 4];
  306. tx_cntr++;
  307. temp_var = tx_buff[i] & B00001111;
  308. payload_encoded[tx_cntr] = mch_lookup[temp_var];
  309. tx_cntr++;
  310. }
  311.  
  312. return payload_encoded;
  313. }
  314.  
  315. // Return 0 if successful, (last) non-zero exit code if not.
  316. uint8_t CC1101::transmit_data(uint8_t payload[], uint8_t len)
  317. {
  318. bool fan_frame_valid = false;
  319. uint8_t error_code = -1;
  320. const uint8_t buff_lenght = 100;
  321. uint8_t rx_buffer[6];
  322. uint8_t rbuf[buff_lenght];
  323. int rlen = 0;
  324. bool header_detected_flag = false;
  325. uint8_t tx_frame_lenght = (len * 2) + 15; // 15 bytes overhead, len*2 data
  326. uint8_t tx_buffer[tx_frame_lenght];
  327. uint8_t tx_payload_encoded[len * 2];
  328.  
  329. unsigned long previousMillis = 0;
  330. unsigned long currentMillis = 0;
  331. bool rx_abort_flag = false;
  332.  
  333. // Manchester encode payload
  334. manchester_encode(payload, len, tx_payload_encoded);
  335.  
  336. //PREAMBLE
  337. for (int i = 0; i < 9; i++)
  338. {
  339. tx_buffer[i] = 0x55;
  340. }
  341.  
  342. // BOF
  343. tx_buffer[9] = 0xFF;
  344. tx_buffer[10] = 0x00;
  345. tx_buffer[11] = 0x33;
  346. tx_buffer[12] = 0x55;
  347. tx_buffer[13] = 0x53;
  348.  
  349. // Insert payload (manchester encoded)
  350. for (int i = 0; i < tx_frame_lenght - 1; i++)
  351. {
  352. tx_buffer[i + 14] = tx_payload_encoded[i];
  353. }
  354.  
  355. // EOF
  356. tx_buffer[tx_frame_lenght - 1] = 0x35;
  357.  
  358. Serial1.setTimeout(RX_TIME_OUT);
  359.  
  360. config_registers(); // Reconfigure CC1101
  361.  
  362. setTxState();
  363. Serial1.write(tx_buffer, tx_frame_lenght);
  364. Serial1.flush(); // wait for Serial TX data completed
  365.  
  366. // Clear input buffer, might be filled with TX data, since RX and TX pin are connected
  367. uint8_t temp_var;
  368. while (Serial1.available())
  369. {
  370. temp_var = Serial1.read();
  371. }
  372.  
  373. // Config GDO0 to ouput
  374. cmdStrobe(CC1101_SIDLE);
  375. setRxState();
  376.  
  377. // Read serial data, variable frame lenght
  378. previousMillis = millis();
  379.  
  380. // Process RX data
  381. while (!rx_abort_flag) // RX complete or timeout
  382. {
  383. if (!header_detected_flag)
  384. {
  385. while (Serial1.available() > 0)
  386. {
  387. for (uint8_t i = 0; i < 5; i++)
  388. {
  389. rx_buffer[i] = rx_buffer[i + 1];
  390. }
  391.  
  392. rx_buffer[5] = Serial1.read();
  393.  
  394. if ( (rx_buffer[5] == 0x53) && (rx_buffer[4] == 0x55) && (rx_buffer[3] == 0x33) && (rx_buffer[2] == 0x00) && (rx_buffer[0] == 0x55))
  395. {
  396. header_detected_flag = true;
  397. break;
  398. }
  399. else
  400. {
  401. currentMillis = millis();
  402. if (currentMillis - previousMillis > RX_TIME_OUT)
  403. {
  404. rx_abort_flag = true;
  405. error_code = 10;
  406. break;
  407. }
  408. }
  409. }
  410. }
  411. else
  412. {
  413. rlen = Serial1.readBytesUntil(0x35, rbuf, sizeof(rbuf) - 1); // read until 0x35 received or timeout
  414. rx_abort_flag = true;
  415. error_code = 11;
  416. }
  417. }
  418.  
  419. // Process data
  420. if ( (rlen == 0) || (rlen == 100)) // No valid data
  421. {
  422. #ifdef DEBUG_MODE
  423. if (!header_detected_flag)
  424. {
  425. Serial.println("> Error: no header detected!");
  426. }
  427. else
  428. {
  429. Serial.println("> header detected, but no valid data!");
  430. }
  431. #endif
  432. }
  433. else
  434. {
  435. // check if encoded number of bytes is even number
  436. if ((rlen % 2) != 0)
  437. {
  438. #ifdef DEBUG_MODE
  439. Serial.println("> Error: not even number, problaby corrupted msg, problematic for decoding");
  440. #endif
  441. error_code =13;
  442. }
  443. else
  444. {
  445. uint8_t dataframe_decoded[rlen / 2];
  446. manchester_decode(rbuf, rlen, dataframe_decoded);
  447.  
  448. #ifdef DEBUG_MODE
  449. Serial.print("> RX decoded data: ");
  450. for (int i = 0; i < rlen / 2; i++)
  451. {
  452. Serial.print(dataframe_decoded[i], HEX);
  453. Serial.print(" ");
  454. }
  455. Serial.print(" | ");
  456. #endif
  457.  
  458. // CRC check
  459. if (calc_crc(dataframe_decoded, rlen / 2) == dataframe_decoded[(rlen / 2) - 1])
  460. {
  461. // Check address!
  462. fan_frame_valid = true;
  463. for (uint8_t i = 1; i < 4; i++)
  464. {
  465. if (dataframe_decoded[i] != new_fan_state.address[i + 2])
  466. {
  467. fan_frame_valid = false;
  468. error_code = 14;
  469.  
  470. #ifdef DEBUG_MODE
  471. Serial.print("> Address is not valid!");
  472. #endif
  473. }
  474. }
  475.  
  476. if (fan_frame_valid) // Update states
  477. {
  478. uint16_t param_type = ((uint16_t)dataframe_decoded[7]<<8) | (uint16_t)(dataframe_decoded[8]);
  479. uint8_t param_lenght = dataframe_decoded[9];
  480.  
  481. if ( (param_type == 0x22F1) && (param_lenght == 0x03) ) // FAN SPEED
  482. {
  483. new_fan_state.fan_speed = dataframe_decoded[11];
  484. msg_id[fan_speed].rx_flag = true;
  485. }
  486.  
  487. if ( (param_type == 0x22F7) && (param_lenght == 0x03) ) // BYPASS
  488. {
  489. // This is:
  490. // 0xC8 (open), 0x00 (close), 0xFF (auto)
  491. new_fan_state.bypass_mode = dataframe_decoded[11];
  492. msg_id[bypass_mode].rx_flag = true;
  493.  
  494. #ifdef DEBUG_MODE
  495. Serial.print("> Setting for bypass while transmitting");
  496. #endif
  497. }
  498.  
  499. if (param_type == 0x31D9) // FAN SPEED, don't check for lenght, might differ from MVS and HRC
  500. {
  501. new_fan_state.fan_speed = dataframe_decoded[12];
  502. msg_id[fan_speed].rx_flag = true;
  503. }
  504.  
  505. if ( (param_type == 0x12A0) && (param_lenght == 0x02) ) // Humidity
  506. {
  507. new_fan_state.indoor_humidity = dataframe_decoded[11];
  508. msg_id[indoor_hum].rx_flag = true;
  509. }
  510.  
  511. if ( (param_type == 0x31DA) && (param_lenght == 0x1E) ) // EXTENDED 31DA INFO
  512. {
  513. new_fan_state.indoor_humidity = dataframe_decoded[15];
  514. new_fan_state.outdoor_humidity = dataframe_decoded[16];
  515. new_fan_state.exhaust_temperature = (dataframe_decoded[17]*256)+dataframe_decoded[18];
  516. new_fan_state.supply_temperature = (dataframe_decoded[19]*256)+dataframe_decoded[20];
  517. new_fan_state.indoor_temperature = (dataframe_decoded[21]*256)+dataframe_decoded[22];
  518. new_fan_state.outdoor_temperature = (dataframe_decoded[23]*256)+dataframe_decoded[24];
  519. new_fan_state.bypass_position = dataframe_decoded[27];
  520. new_fan_state.exhaust_fanspeed = dataframe_decoded[29];
  521. new_fan_state.supply_fanspeed = dataframe_decoded[30];
  522. new_fan_state.supply_flow = (dataframe_decoded[35]*256)+dataframe_decoded[36];
  523. new_fan_state.exhaust_flow = (dataframe_decoded[37]*256)+dataframe_decoded[38];
  524.  
  525. msg_id[fan_info].rx_flag = true;
  526. }
  527.  
  528. #ifdef DEBUG_MODE
  529. Serial.println("Data ok!");
  530. #endif
  531. }
  532. else
  533. {
  534. error_code = 15;
  535. #ifdef DEBUG_MODE
  536. Serial.println("Valid message, wrong address!");
  537. #endif
  538.  
  539. }
  540. }
  541. else
  542. {
  543. error_code = 16;
  544. #ifdef DEBUG_MODE
  545. Serial.println("CRC ERROR!");
  546. #endif
  547. }
  548. }
  549. }
  550.  
  551. // Back to idle/power down
  552. cmdStrobe(CC1101_SIDLE);
  553. writeReg(CC1101_IOCFG0, CC1101_DEFVAL_IOCFG0);
  554. setPowerDownState();
  555.  
  556. if(fan_frame_valid)
  557. {
  558. return 0;
  559. }
  560.  
  561. return error_code;
  562. }
  563.  
  564. bool CC1101::set_bypass(uint8_t bypass_mode)
  565. {
  566. uint8_t payload[14];
  567. uint8_t ARR_SIZE = sizeof(payload) / sizeof(payload[0]);
  568.  
  569. // header[RQ = 0x0C, I = 0x1C, W = 0x2C, RP = 3C]
  570. payload[0] = 0x2C;
  571.  
  572. // Get souce and target address
  573. for (uint8_t i = 1; i < 7; i++)
  574. payload[i] = new_fan_state.address[i - 1];
  575.  
  576. // Opcode [ByPass]
  577. payload[7] = 0x22;
  578. payload[8] = 0xF7;
  579.  
  580. // Command lenght
  581. payload[9] = 0x03;
  582.  
  583. // Payload
  584.  
  585. payload[10] = 0x00;
  586. payload[11] = bypass_mode;
  587. payload[12] = 0xEF;
  588.  
  589. payload[ARR_SIZE - 1] = calc_crc(payload, ARR_SIZE);
  590.  
  591. #ifdef DEBUG_MODE
  592. Serial.print("Set bypass mode: ");
  593. Serial.println(bypass_mode);
  594. #endif
  595.  
  596. // Returns error code
  597. return transmit_data(payload, ARR_SIZE) == 0;
  598.  
  599. }
  600.  
  601. bool CC1101::tx_fanspeed(uint8_t fan_speed)
  602. {
  603. uint8_t payload[14];
  604. uint8_t ARR_SIZE = sizeof(payload) / sizeof(payload[0]);
  605.  
  606. // header[RQ = 0x0C, I = 0x1C, W = 0x2C, RP = 3C]
  607. payload[0] = 0x1C;
  608.  
  609. // Get souce and target address
  610. for (uint8_t i = 1; i < 7; i++)
  611. payload[i] = new_fan_state.address[i - 1];
  612.  
  613. // Opcode[FAN speed status]
  614. payload[7] = 0x22;
  615. payload[8] = 0xF1;
  616.  
  617. // Command lenght
  618. payload[9] = 0x03;
  619.  
  620. // Payload
  621. payload[10] = 0x00;
  622. payload[11] = fan_speed;
  623. payload[12] = 0x04;
  624.  
  625. payload[ARR_SIZE - 1] = calc_crc(payload, ARR_SIZE);
  626.  
  627. #ifdef DEBUG_MODE
  628. Serial.println("Set fan speed");
  629. #endif
  630.  
  631. // Returns bool
  632. return transmit_data(payload, ARR_SIZE) == 0;
  633.  
  634. }
  635.  
  636. uint8_t CC1101::request_fan_state(void)
  637. {
  638. // header[RQ = 0x0C, W = 0x1C, I = 0x2C, RP = 3C]
  639. uint8_t payload[12] = {0x0C, new_fan_state.address[0], new_fan_state.address[1], new_fan_state.address[2], new_fan_state.address[3], new_fan_state.address[4], new_fan_state.address[5], 0x00, 0x00, 0x01, 0x00, 0x00};
  640. uint8_t ARR_SIZE = sizeof(payload) / sizeof(payload[0]);
  641. static uint8_t tx_cntr = 0;
  642. static uint8_t req_cntr = 0;
  643.  
  644. static uint8_t req_pointer[3] = {fan_speed, indoor_hum, fan_info}; // Init request array
  645.  
  646. payload[7] = (msg_id[req_pointer[tx_cntr]].code_id >> 8);
  647. payload[8] = (msg_id[req_pointer[tx_cntr]].code_id & 0xFF);
  648.  
  649. payload[ARR_SIZE - 1] = calc_crc(payload, ARR_SIZE);
  650.  
  651. // Determine next tx msg
  652. if (req_cntr < TX_REQ_CNTS)
  653. {
  654. if (tx_cntr < sizeof(enum codes_enum))
  655. tx_cntr++;
  656. else
  657. tx_cntr = 0;
  658.  
  659. req_cntr++;
  660. }
  661. else
  662. {
  663. req_pointer[0] = fan_speed;
  664. if(msg_id[fan_info].rx_flag)
  665. req_pointer[1] = fan_info;
  666. else
  667. req_pointer[1] = indoor_hum;
  668.  
  669. if (tx_cntr < sizeof(enum codes_enum)-1)
  670. tx_cntr++;
  671. else
  672. tx_cntr = 0;
  673. }
  674.  
  675. // Returns bool
  676. return transmit_data(payload, ARR_SIZE) == 0;
  677.  
  678. }
  679.  
  680. void CC1101::set_rx_mode(void)
  681. {
  682. config_registers(); // Reconfigure CC1101
  683. setRxState();
  684. }
  685.  
  686. void CC1101::setCarrierFreq(byte freq)
  687. {
  688. switch (freq)
  689. {
  690. case CFREQ_915:
  691. writeReg(CC1101_FREQ2, CC1101_DEFVAL_FREQ2_915);
  692. writeReg(CC1101_FREQ1, CC1101_DEFVAL_FREQ1_915);
  693. writeReg(CC1101_FREQ0, CC1101_DEFVAL_FREQ0_915);
  694. break;
  695. case CFREQ_433:
  696. writeReg(CC1101_FREQ2, CC1101_DEFVAL_FREQ2_433);
  697. writeReg(CC1101_FREQ1, CC1101_DEFVAL_FREQ1_433);
  698. writeReg(CC1101_FREQ0, CC1101_DEFVAL_FREQ0_433);
  699. break;
  700. case CFREQ_918:
  701. writeReg(CC1101_FREQ2, CC1101_DEFVAL_FREQ2_918);
  702. writeReg(CC1101_FREQ1, CC1101_DEFVAL_FREQ1_918);
  703. writeReg(CC1101_FREQ0, CC1101_DEFVAL_FREQ0_918);
  704. break;
  705. default:
  706. writeReg(CC1101_FREQ2, CC1101_DEFVAL_FREQ2_868);
  707. writeReg(CC1101_FREQ1, CC1101_DEFVAL_FREQ1_868);
  708. writeReg(CC1101_FREQ0, CC1101_DEFVAL_FREQ0_868);
  709. break;
  710. }
  711.  
  712. //carrierFreq = freq;
  713. }
  714.  
  715. void CC1101::setPowerDownState()
  716. {
  717. // Comming from RX state, we need to enter the IDLE state first
  718. cmdStrobe(CC1101_SIDLE);
  719. // Enter Power-down state
  720. cmdStrobe(CC1101_SPWD);
  721. }
  722.  
  723. void CC1101::setRxState(void)
  724. {
  725. writeReg(CC1101_MDMCFG2, CC1101_DEFVAL_MDMCFG2);
  726. writeReg(CC1101_IOCFG0, CC1101_SERIAL_OUT_IOCFG0); // Serial data output
  727. cmdStrobe(CC1101_SRX);
  728. //rfState = RFSTATE_RX;
  729. }
  730.  
  731. void CC1101::setTxState(void)
  732. {
  733. writeReg(CC1101_IOCFG0, CC1101_DEFVAL_IOCFG0);
  734. writeReg(CC1101_MDMCFG2, CC1101_DEFVAL_MDMCFG2);
  735. cmdStrobe(CC1101_STX);
  736. //rfState = RFSTATE_TX;
  737. }
Advertisement
Add Comment
Please, Sign In to add comment