cc1101.cpp 14 KB

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