cc1101.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. #include "flipper_v2.h"
  2. #include "cc1101-workaround/cc1101.h"
  3. #include "spi.h"
  4. #include <math.h>
  5. // ******************************************************************************
  6. #define WRITE_BURST 0x40
  7. #define READ_SINGLE 0x80
  8. #define READ_BURST 0xC0
  9. #define BYTES_IN_FIFO 0x7F //used to detect FIFO underflow or overflow
  10. /*********************ss_pin as global variable****************************** */
  11. /* cc1101 */
  12. /******************************************************************************/
  13. GpioPin ss_pin;
  14. CC1101::CC1101(GpioPin* ss_pin) {
  15. /*
  16. pinMode(gdo0_pin, OUTPUT); //GDO0 as asynchronous serial mode input
  17. pinMode(gdo2_pin, INPUT); //GDO2 as asynchronous serial mode output
  18. */
  19. gpio_init(ss_pin, GpioModeOutputPushPull);
  20. this->ss_pin = ss_pin;
  21. // TODO open record
  22. this->miso_pin = MISO_PIN;
  23. this->miso_pin_record = &this->miso_pin;
  24. }
  25. //******************************************************************************
  26. //SpiInit
  27. /******************************************************************************/
  28. extern SPI_HandleTypeDef SPI_R;
  29. void CC1101::SpiInit(void) {
  30. //initialize spi pins
  31. //Enable spi master, MSB, SPI mode 0, FOSC/4
  32. SpiMode(0);
  33. CC1101_SPI_Reconfigure();
  34. }
  35. void CC1101::SpiEnd(void) {
  36. /*
  37. SPCR = ((0<<SPE) | // SPI Enable
  38. (0<<SPIE)| // SPI Interupt Enable
  39. (0<<DORD)| // Data Order (0:MSB first / 1:LSB first)
  40. (1<<MSTR)| // Master/Slave select
  41. (0<<SPR1)|(0<<SPR0)| // SPI Clock Rate ( 0 0 = osc/4; 0 1 = osc/16; 1 0 = osc/64; 1 1= 0sc/128)
  42. (0<<CPOL)| // Clock Polarity (0:SCK low / 1:SCK hi when idle)
  43. (0<<CPHA)); // Clock Phase (0:leading / 1:trailing edge sampling)
  44. //SPSR = (0<<SPI2X); // Double Clock Rate
  45. */
  46. }
  47. /******************************************************************************
  48. Function: SpiMode
  49. *INPUT : config mode
  50. (0<<CPOL) | (0 << CPHA) 0
  51. (0<<CPOL) | (1 << CPHA) 1
  52. (1<<CPOL) | (0 << CPHA) 2
  53. (1<<CPOL) | (1 << CPHA) 3
  54. *OUTPUT :none
  55. ******************************************************************************/
  56. void CC1101::SpiMode(uint8_t config) {
  57. /*
  58. uint8_t tmp;
  59. // enable SPI master with configuration byte specified
  60. SPCR = 0;
  61. SPCR = (config & 0x7F) | (1<<SPE) | (1<<MSTR);
  62. tmp = SPSR;
  63. tmp = SPDR;
  64. */
  65. }
  66. /****************************************************************
  67. *FUNCTION NAME:SpiTransfer
  68. *FUNCTION :spi transfer
  69. *INPUT :value: data to send
  70. *OUTPUT :data to receive
  71. ****************************************************************/
  72. uint8_t CC1101::SpiTransfer(uint8_t value) {
  73. uint8_t buf[1] = {value};
  74. uint8_t rxbuf[1] = {0};
  75. HAL_SPI_TransmitReceive(&SPI_R, buf, rxbuf, 1, HAL_MAX_DELAY);
  76. return rxbuf[0];
  77. }
  78. uint8_t last_status;
  79. /****************************************************************
  80. *FUNCTION NAME:SpiWriteReg
  81. *FUNCTION :CC1101 write data to register
  82. *INPUT :addr: register address; value: register value
  83. *OUTPUT :none
  84. ****************************************************************/
  85. void CC1101::SpiWriteReg(uint8_t addr, uint8_t value) {
  86. gpio_write(ss_pin, false);
  87. while(gpio_read(this->miso_pin_record))
  88. ;
  89. last_status = SpiTransfer(addr);
  90. last_status = SpiTransfer(value);
  91. gpio_write(ss_pin, true);
  92. }
  93. /****************************************************************
  94. *FUNCTION NAME:SpiWriteBurstReg
  95. *FUNCTION :CC1101 write burst data to register
  96. *INPUT :addr: register address; buffer:register value array; num:number to write
  97. *OUTPUT :none
  98. ****************************************************************/
  99. void CC1101::SpiWriteBurstReg(uint8_t addr, uint8_t* buffer, uint8_t num) {
  100. gpio_write(ss_pin, false);
  101. while(gpio_read(this->miso_pin_record))
  102. ;
  103. last_status = SpiTransfer(addr | WRITE_BURST);
  104. for(uint8_t i = 0; i < num; i++) {
  105. last_status = SpiTransfer(buffer[i]);
  106. }
  107. gpio_write(ss_pin, true);
  108. }
  109. /****************************************************************
  110. *FUNCTION NAME:SpiStrobe
  111. *FUNCTION :CC1101 Strobe
  112. *INPUT :strobe: command; //refer define in CC1101.h//
  113. *OUTPUT :none
  114. ****************************************************************/
  115. void CC1101::SpiStrobe(uint8_t strobe) {
  116. gpio_write(ss_pin, false);
  117. while(gpio_read(this->miso_pin_record))
  118. ;
  119. last_status = SpiTransfer(strobe);
  120. gpio_write(ss_pin, true);
  121. }
  122. /****************************************************************
  123. *FUNCTION NAME:SpiReadReg
  124. *FUNCTION :CC1101 read data from register
  125. *INPUT :addr: register address
  126. *OUTPUT :register value
  127. ****************************************************************/
  128. uint8_t CC1101::SpiReadReg(uint8_t addr) {
  129. gpio_write(ss_pin, false);
  130. while(gpio_read(this->miso_pin_record))
  131. ;
  132. last_status = SpiTransfer(addr | READ_SINGLE);
  133. uint8_t value = SpiTransfer(0);
  134. gpio_write(ss_pin, true);
  135. return value;
  136. }
  137. /****************************************************************
  138. *FUNCTION NAME:SpiReadBurstReg
  139. *FUNCTION :CC1101 read burst data from register
  140. *INPUT :addr: register address; buffer:array to store register value; num: number to read
  141. *OUTPUT :none
  142. ****************************************************************/
  143. void CC1101::SpiReadBurstReg(uint8_t addr, uint8_t* buffer, uint8_t num) {
  144. gpio_write(ss_pin, false);
  145. while(gpio_read(this->miso_pin_record))
  146. ;
  147. last_status = SpiTransfer(addr | READ_BURST);
  148. for(uint8_t i = 0; i < num; i++) {
  149. buffer[i] = SpiTransfer(0);
  150. }
  151. gpio_write(ss_pin, true);
  152. }
  153. /****************************************************************
  154. *FUNCTION NAME:SpiReadStatus
  155. *FUNCTION :CC1101 read status register
  156. *INPUT :addr: register address
  157. *OUTPUT :status value
  158. ****************************************************************/
  159. uint8_t CC1101::SpiReadStatus(uint8_t addr) {
  160. gpio_write(ss_pin, false);
  161. while(gpio_read(this->miso_pin_record))
  162. ;
  163. last_status = SpiTransfer(addr | READ_BURST);
  164. uint8_t value = SpiTransfer(0);
  165. gpio_write(ss_pin, true);
  166. return value;
  167. }
  168. /****************************************************************
  169. *FUNCTION NAME:Reset
  170. *FUNCTION :CC1101 reset //details refer datasheet of CC1101/CC1100//
  171. *INPUT :none
  172. *OUTPUT :none
  173. ****************************************************************/
  174. void CC1101::Reset(void) {
  175. gpio_write(ss_pin, false);
  176. delay(1);
  177. gpio_write(ss_pin, true);
  178. delay(1);
  179. gpio_write(ss_pin, false);
  180. while(gpio_read(this->miso_pin_record))
  181. ;
  182. last_status = SpiTransfer(CC1101_SRES);
  183. while(gpio_read(this->miso_pin_record))
  184. ;
  185. gpio_write(ss_pin, true);
  186. }
  187. bool CC1101::SpiSetRegValue(uint8_t reg, uint8_t value, uint8_t msb, uint8_t lsb) {
  188. if((msb > 7) || (lsb > 7) || (lsb > msb)) {
  189. return false;
  190. }
  191. uint8_t current_value = SpiReadReg(reg);
  192. uint8_t mask = ~((0b11111111 << (msb + 1)) | (0b11111111 >> (8 - lsb)));
  193. uint8_t new_value = (current_value & ~mask) | (value & mask);
  194. SpiWriteReg(reg, new_value);
  195. return true;
  196. }
  197. /****************************************************************
  198. *FUNCTION NAME:Init
  199. *FUNCTION :CC1101 initialization
  200. *INPUT :none
  201. *OUTPUT :none
  202. ****************************************************************/
  203. uint8_t CC1101::Init(void) {
  204. #ifdef CC1101_DEBUG
  205. printf("Init SPI...\n");
  206. #endif
  207. SpiInit(); //spi initialization
  208. gpio_write(ss_pin, true);
  209. // gpio_write(SCK_PIN, true);
  210. // gpio_write(MOSI_PIN, false);
  211. #ifdef CC1101_DEBUG
  212. printf("Reset CC1101...\n");
  213. #endif
  214. Reset(); // CC1101 reset
  215. osDelay(150);
  216. uint8_t partnum __attribute__((unused));
  217. uint8_t version;
  218. partnum = SpiReadStatus(CC1101_PARTNUM);
  219. version = SpiReadStatus(CC1101_VERSION);
  220. #ifdef CC1101_DEBUG
  221. printf("Partnum:0x%02X, Version:0x%02X\n", partnum, version);
  222. #endif
  223. #ifdef CC1101_DEBUG
  224. printf("Init CC1101...");
  225. #endif
  226. // RegConfigSettings(); //CC1101 register config
  227. #ifdef CC1101_DEBUG
  228. printf("Done!\n");
  229. #endif
  230. return version;
  231. }
  232. /****************************************************************
  233. *FUNCTION NAME:SetMod
  234. *FUNCTION :CC1101 modulation type
  235. *INPUT :byte mode
  236. *OUTPUT :none
  237. ****************************************************************/
  238. void CC1101::SetMod(uint8_t mode) {
  239. SpiWriteReg(CC1101_MDMCFG2, mode); //no sync/preamble; ASK/OOK only support up to -1dbm
  240. if((mode | 0x30) == ASK) {
  241. SpiWriteReg(CC1101_FREND0, 0x11); //use first up to PATABLE(0)
  242. uint8_t PaTabel[8] = {0x00, POWER, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  243. SpiWriteBurstReg(CC1101_PATABLE, PaTabel, 8); //CC1101 PATABLE config
  244. } else {
  245. SpiWriteReg(CC1101_FREND0, 0x10); //use first up to PATABLE(0)
  246. uint8_t PaTabel[8] = {POWER, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  247. SpiWriteBurstReg(CC1101_PATABLE, PaTabel, 8); //CC1101 PATABLE config
  248. }
  249. #ifdef CC1101_DEBUG
  250. switch(mode | 0x30) {
  251. case GFSK: {
  252. printf("CC1101 Modulation: GFSK");
  253. break;
  254. }
  255. case MSK: {
  256. printf("CC1101 Modulation: MSK");
  257. break;
  258. }
  259. case ASK: {
  260. printf("CC1101 Modulation: ASK/OOK");
  261. break;
  262. }
  263. case FSK2: {
  264. printf("CC1101 Modulation: 2-FSK");
  265. break;
  266. }
  267. case FSK4: {
  268. printf("CC1101 Modulation: 4-FSK");
  269. break;
  270. }
  271. default: //default to GFSK
  272. {
  273. printf("Modulation mode not supported");
  274. break;
  275. }
  276. }
  277. printf("\n");
  278. #endif
  279. }
  280. /****************************************************************
  281. *FUNCTION NAME:RegConfigSettings
  282. *FUNCTION :CC1101 register config //details refer datasheet of CC1101/CC1100//
  283. *INPUT :none
  284. *OUTPUT :none
  285. ****************************************************************/
  286. void CC1101::RegConfigSettings(void) {
  287. SpiWriteReg(CC1101_FSCTRL1, 0x06); //IF frequency
  288. SpiWriteReg(CC1101_FSCTRL0, 0x00); //frequency offset before synthesizer
  289. SpiWriteReg(CC1101_MDMCFG4, 0xCC); // RX filter bandwidth 100k(0xcc)
  290. SpiWriteReg(
  291. CC1101_MDMCFG3, 0x43); //datarate config 512kBaud for the purpose of fast rssi measurement
  292. SpiWriteReg(CC1101_MDMCFG1, 0x21); //FEC preamble etc. last 2 bits for channel spacing
  293. SpiWriteReg(CC1101_MDMCFG0, 0xF8); //100khz channel spacing
  294. //CC1101_CHANNR moved to SetChannel func
  295. //SpiWriteReg(CC1101_DEVIATN, 0x47);
  296. SpiWriteReg(
  297. CC1101_MCSM0, 0x18); // calibrate when going from IDLE to RX or TX ; 149 - 155 μs timeout
  298. SpiWriteReg(CC1101_FOCCFG, 0x16); //frequency compensation
  299. //SpiWriteReg(CC1101_BSCFG, 0x1C); //bit synchronization config
  300. SpiWriteReg(CC1101_AGCCTRL2, 0x43);
  301. SpiWriteReg(CC1101_AGCCTRL1, 0x49);
  302. SpiWriteReg(CC1101_AGCCTRL0, 0x91);
  303. //freq synthesizer calibration
  304. SpiWriteReg(CC1101_FSCAL3, 0xEA);
  305. SpiWriteReg(CC1101_FSCAL2, 0x2A);
  306. SpiWriteReg(CC1101_FSCAL1, 0x00);
  307. SpiWriteReg(CC1101_FSCAL0, 0x1F);
  308. SpiWriteReg(CC1101_TEST2, 0x81);
  309. SpiWriteReg(CC1101_TEST1, 0x35);
  310. SpiWriteReg(CC1101_TEST0, 0x0B); //should be 0x0B for lower than 430.6MHz and 0x09 for higher
  311. //SpiWriteReg(CC1101_FREND1, 0x56);
  312. //SpiWriteReg(CC1101_IOCFG2, 0x0B); //serial clock.synchronous to the data in synchronous serial mode
  313. //SpiWriteReg(CC1101_IOCFG0, 0x06); //asserts when sync word has been sent/received, and de-asserts at the end of the packet
  314. SpiWriteReg(CC1101_IOCFG2, 0x0D); //data output pin for asynchronous mode
  315. SpiWriteReg(
  316. CC1101_IOCFG0,
  317. 0x2E); //High impedance (3-state), GDO0 configed as data input for asynchronous mode
  318. //SpiWriteReg(CC1101_PKTCTRL0, 0x05); //whitening off;CRC Enable;variable length packets, packet length configured by the first byte after sync word
  319. SpiWriteReg(
  320. CC1101_PKTCTRL0, 0x33); //whitening off; asynchronous serial mode; CRC diable;reserved
  321. //SpiWriteReg(CC1101_PKTLEN, 0x3D); //61 bytes max length
  322. SpiWriteReg(
  323. CC1101_FIFOTHR,
  324. 0x47); //Adc_retention enabled for RX filter bandwidth less than 325KHz; defalut fifo threthold.
  325. }
  326. /****************************************************************
  327. *FUNCTION NAME:SetFreq
  328. *FUNCTION :SetFreq
  329. *INPUT :Freq2, Freq1, Freq0
  330. *OUTPUT :none
  331. ****************************************************************/
  332. void CC1101::SetFreq(uint8_t freq2, uint8_t freq1, uint8_t freq0) {
  333. SpiWriteReg(CC1101_FREQ2, freq2);
  334. SpiWriteReg(CC1101_FREQ1, freq1);
  335. SpiWriteReg(CC1101_FREQ0, freq0);
  336. }
  337. /****************************************************************
  338. *FUNCTION NAME:SetChannel
  339. *FUNCTION :SetChannel
  340. *INPUT :int channel
  341. *OUTPUT :none
  342. ****************************************************************/
  343. void CC1101::SetChannel(int channel) {
  344. #ifdef CC1101_DEBUG
  345. printf("Set CC1101 channel to: %d \n", channel);
  346. #endif
  347. SpiWriteReg(CC1101_CHANNR, (uint8_t)channel); //related to channel numbers
  348. }
  349. /****************************************************************
  350. *FUNCTION NAME:SetReceive
  351. *FUNCTION :SetReceive
  352. *INPUT :none
  353. *OUTPUT :none
  354. ****************************************************************/
  355. void CC1101::SetReceive(void) {
  356. SpiStrobe(CC1101_SRX);
  357. while(SpiReadStatus(CC1101_MARCSTATE) ^ CC1101_STATUS_RX) {
  358. // delay(1);
  359. // printf("wait status\n");
  360. }
  361. }
  362. /****************************************************************
  363. *FUNCTION NAME:SetTransmit
  364. *FUNCTION :
  365. *INPUT :none
  366. *OUTPUT :none
  367. ****************************************************************/
  368. void CC1101::SetTransmit(void) {
  369. SpiStrobe(CC1101_STX);
  370. while(SpiReadStatus(CC1101_MARCSTATE) ^ CC1101_STATUS_TX)
  371. ;
  372. }
  373. //cc1101 cc1101;
  374. bool CC1101::setRxBandwidth(float bandwidth) {
  375. if(bandwidth < 58.0 || bandwidth > 821.0) return false;
  376. // set mode to standby
  377. SpiStrobe(CC1101_SIDLE);
  378. // calculate exponent and mantissa values
  379. for(int8_t e = 3; e >= 0; e--) {
  380. for(int8_t m = 3; m >= 0; m--) {
  381. float point = (F_OSC) / (8 * (m + 4) * ((uint32_t)1 << e));
  382. if(fabs((bandwidth * 1000.0) - point) <= 1000) {
  383. // set Rx channel filter bandwidth
  384. SpiSetRegValue(CC1101_MDMCFG4, (e << 6) | (m << 4), 7, 4);
  385. return true;
  386. }
  387. }
  388. }
  389. return false;
  390. }
  391. static void getExpMant(
  392. float target,
  393. uint16_t mantOffset,
  394. uint8_t divExp,
  395. uint8_t expMax,
  396. uint8_t& exp,
  397. uint8_t& mant) {
  398. // get table origin point (exp = 0, mant = 0)
  399. float origin = (mantOffset * F_OSC) / ((uint32_t)1 << divExp);
  400. // iterate over possible exponent values
  401. for(int8_t e = expMax; e >= 0; e--) {
  402. // get table column start value (exp = e, mant = 0);
  403. float intervalStart = ((uint32_t)1 << e) * origin;
  404. // check if target value is in this column
  405. if(target >= intervalStart) {
  406. // save exponent value
  407. exp = e;
  408. // calculate size of step between table rows
  409. float stepSize = intervalStart / (float)mantOffset;
  410. // get target point position (exp = e, mant = m)
  411. mant = ((target - intervalStart) / stepSize);
  412. // we only need the first match, terminate
  413. return;
  414. }
  415. }
  416. }
  417. bool CC1101::setBitRate(float bitrate) {
  418. if(bitrate < 0.6 || bitrate > 500.0) return false;
  419. // set mode to standby
  420. SpiStrobe(CC1101_SIDLE);
  421. // calculate exponent and mantissa values
  422. uint8_t e = 0;
  423. uint8_t m = 0;
  424. getExpMant(bitrate * 1000.0, 256, 28, 14, e, m);
  425. // set bit rate value
  426. SpiSetRegValue(CC1101_MDMCFG4, e, 3, 0);
  427. SpiSetRegValue(CC1101_MDMCFG3, m, 7, 0);
  428. return true;
  429. }