Advertisement
Guest User

ELECHOUSE_CC1101.cpp

a guest
Oct 24th, 2022
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.45 KB | None | 0 0
  1. /*
  2.     ELECHOUSE_CC1101.cpp - CC1101 module library
  3.     Copyright (c) 2010 Michael.
  4.     Author: Michael, <www.elechouse.com>
  5.     Version: November 12, 2010
  6.  
  7.     This library is designed to use CC1101/CC1100 module on Arduino platform.
  8.     CC1101/CC1100 module is an useful wireless module.Using the functions of the
  9.     library, you can easily send and receive data by the CC1101/CC1100 module.
  10.     Just have fun!
  11.     For the details, please refer to the datasheet of CC1100/CC1101.
  12. */
  13. #include "ELECHOUSE_CC1101.h"
  14. #include <Arduino.h>
  15.  
  16. /****************************************************************/
  17. #define     WRITE_BURST         0x40                        //write burst
  18. #define     READ_SINGLE         0x80                        //read single
  19. #define     READ_BURST          0xC0                        //read burst
  20. #define     BYTES_IN_RXFIFO     0x7F                        //byte number in RXfifo
  21.  
  22. /****************************************************************/
  23. byte PaTabel[8] = {0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60};
  24.  
  25. /****************************************************************
  26. *FUNCTION NAME:SpiInit
  27. *FUNCTION     :spi communication initialization
  28. *INPUT        :none
  29. *OUTPUT       :none
  30. ****************************************************************/
  31. void ELECHOUSE_CC1101::SpiInit(void)
  32. {
  33.   // initialize the SPI pins
  34.   pinMode(SCK_PIN, OUTPUT);
  35.   pinMode(MOSI_PIN, OUTPUT);
  36.   pinMode(MISO_PIN, INPUT);
  37.   pinMode(SS_PIN, OUTPUT);
  38.  
  39.   // enable SPI Master, MSB, SPI mode 0, FOSC/4
  40.   SpiMode(0);
  41. }
  42. /****************************************************************
  43. *FUNCTION NAME:SpiMode
  44. *FUNCTION     :set spi mode
  45. *INPUT        :        config               mode
  46.                (0<<CPOL) | (0 << CPHA)       0
  47.                (0<<CPOL) | (1 << CPHA)       1
  48.                (1<<CPOL) | (0 << CPHA)       2
  49.                (1<<CPOL) | (1 << CPHA)       3
  50. *OUTPUT       :none
  51. ****************************************************************/
  52. void ELECHOUSE_CC1101::SpiMode(byte config)
  53. {
  54.   byte tmp;
  55.  
  56.   // enable SPI master with configuration byte specified
  57.   SPCR = 0;
  58.   SPCR = (config & 0x7F) | (1<<SPE) | (1<<MSTR);
  59.   tmp = SPSR;
  60.   tmp = SPDR;
  61. }
  62.  
  63. /****************************************************************
  64. *FUNCTION NAME:SpiTransfer
  65. *FUNCTION     :spi transfer
  66. *INPUT        :value: data to send
  67. *OUTPUT       :data to receive
  68. ****************************************************************/
  69. byte ELECHOUSE_CC1101::SpiTransfer(byte value)
  70. {
  71.   SPDR = value;
  72.   while (!(SPSR & (1<<SPIF))) ;
  73.   return SPDR;
  74. }
  75.  
  76. /****************************************************************
  77. *FUNCTION NAME: GDO_Set()
  78. *FUNCTION     : set GDO0,GDO2 pin
  79. *INPUT        : none
  80. *OUTPUT       : none
  81. ****************************************************************/
  82. void ELECHOUSE_CC1101::GDO_Set (void)
  83. {
  84.     pinMode(GDO0, INPUT);
  85.     pinMode(GDO2, INPUT);
  86. }
  87.  
  88. /****************************************************************
  89. *FUNCTION NAME:Reset
  90. *FUNCTION     :CC1101 reset //details refer datasheet of CC1101/CC1100//
  91. *INPUT        :none
  92. *OUTPUT       :none
  93. ****************************************************************/
  94. void ELECHOUSE_CC1101::Reset (void)
  95. {
  96.     digitalWrite(SS_PIN, LOW);
  97.     delay(1);
  98.     digitalWrite(SS_PIN, HIGH);
  99.     delay(1);
  100.     digitalWrite(SS_PIN, LOW);
  101.     while(digitalRead(MISO_PIN));
  102.     SpiTransfer(CC1101_SRES);
  103.     while(digitalRead(MISO_PIN));
  104.     digitalWrite(SS_PIN, HIGH);
  105. }
  106.  
  107. /****************************************************************
  108. *FUNCTION NAME:Init
  109. *FUNCTION     :CC1101 initialization
  110. *INPUT        :none
  111. *OUTPUT       :none
  112. ****************************************************************/
  113. void ELECHOUSE_CC1101::Init(void)
  114. {
  115.     SpiInit();                                      //spi initialization
  116.     GDO_Set();                                      //GDO set
  117.     digitalWrite(SS_PIN, HIGH);
  118.     digitalWrite(SCK_PIN, HIGH);
  119.     digitalWrite(MOSI_PIN, LOW);
  120.     Reset();                                        //CC1101 reset
  121.     RegConfigSettings();                            //CC1101 register config
  122.     SpiWriteBurstReg(CC1101_PATABLE,PaTabel,8);     //CC1101 PATABLE config
  123. }
  124.  
  125.  
  126. /****************************************************************
  127. *FUNCTION NAME:SpiWriteReg
  128. *FUNCTION     :CC1101 write data to register
  129. *INPUT        :addr: register address; value: register value
  130. *OUTPUT       :none
  131. ****************************************************************/
  132. void ELECHOUSE_CC1101::SpiWriteReg(byte addr, byte value)
  133. {
  134.     digitalWrite(SS_PIN, LOW);
  135.     while(digitalRead(MISO_PIN));
  136.     SpiTransfer(addr);
  137.     SpiTransfer(value);
  138.     digitalWrite(SS_PIN, HIGH);
  139. }
  140.  
  141. /****************************************************************
  142. *FUNCTION NAME:SpiWriteBurstReg
  143. *FUNCTION     :CC1101 write burst data to register
  144. *INPUT        :addr: register address; buffer:register value array; num:number to write
  145. *OUTPUT       :none
  146. ****************************************************************/
  147. void ELECHOUSE_CC1101::SpiWriteBurstReg(byte addr, byte *buffer, byte num)
  148. {
  149.     byte i, temp;
  150.  
  151.     temp = addr | WRITE_BURST;
  152.     digitalWrite(SS_PIN, LOW);
  153.     while(digitalRead(MISO_PIN));
  154.     SpiTransfer(temp);
  155.     for (i = 0; i < num; i++)
  156.     {
  157.         SpiTransfer(buffer[i]);
  158.     }
  159.     digitalWrite(SS_PIN, HIGH);
  160. }
  161.  
  162. /****************************************************************
  163. *FUNCTION NAME:SpiStrobe
  164. *FUNCTION     :CC1101 Strobe
  165. *INPUT        :strobe: command; //refer define in CC1101.h//
  166. *OUTPUT       :none
  167. ****************************************************************/
  168. void ELECHOUSE_CC1101::SpiStrobe(byte strobe)
  169. {
  170.     digitalWrite(SS_PIN, LOW);
  171.     while(digitalRead(MISO_PIN));
  172.     SpiTransfer(strobe);
  173.     digitalWrite(SS_PIN, HIGH);
  174. }
  175.  
  176. /****************************************************************
  177. *FUNCTION NAME:SpiReadReg
  178. *FUNCTION     :CC1101 read data from register
  179. *INPUT        :addr: register address
  180. *OUTPUT       :register value
  181. ****************************************************************/
  182. byte ELECHOUSE_CC1101::SpiReadReg(byte addr)
  183. {
  184.     byte temp, value;
  185.  
  186.     temp = addr|READ_SINGLE;
  187.     digitalWrite(SS_PIN, LOW);
  188.     while(digitalRead(MISO_PIN));
  189.     SpiTransfer(temp);
  190.     value=SpiTransfer(0);
  191.     digitalWrite(SS_PIN, HIGH);
  192.  
  193.     return value;
  194. }
  195.  
  196. /****************************************************************
  197. *FUNCTION NAME:SpiReadBurstReg
  198. *FUNCTION     :CC1101 read burst data from register
  199. *INPUT        :addr: register address; buffer:array to store register value; num: number to read
  200. *OUTPUT       :none
  201. ****************************************************************/
  202. void ELECHOUSE_CC1101::SpiReadBurstReg(byte addr, byte *buffer, byte num)
  203. {
  204.     byte i,temp;
  205.  
  206.     temp = addr | READ_BURST;
  207.     digitalWrite(SS_PIN, LOW);
  208.     while(digitalRead(MISO_PIN));
  209.     SpiTransfer(temp);
  210.     for(i=0;i<num;i++)
  211.     {
  212.         buffer[i]=SpiTransfer(0);
  213.     }
  214.     digitalWrite(SS_PIN, HIGH);
  215. }
  216.  
  217. /****************************************************************
  218. *FUNCTION NAME:SpiReadStatus
  219. *FUNCTION     :CC1101 read status register
  220. *INPUT        :addr: register address
  221. *OUTPUT       :status value
  222. ****************************************************************/
  223. byte ELECHOUSE_CC1101::SpiReadStatus(byte addr)
  224. {
  225.     byte value,temp;
  226.  
  227.     temp = addr | READ_BURST;
  228.     digitalWrite(SS_PIN, LOW);
  229.     while(digitalRead(MISO_PIN));
  230.     SpiTransfer(temp);
  231.     value=SpiTransfer(0);
  232.     digitalWrite(SS_PIN, HIGH);
  233.  
  234.     return value;
  235. }
  236.  
  237. /****************************************************************
  238. *FUNCTION NAME:RegConfigSettings
  239. *FUNCTION     :CC1101 register config //details refer datasheet of CC1101/CC1100//
  240. *INPUT        :none
  241. *OUTPUT       :none
  242. ****************************************************************/
  243. void ELECHOUSE_CC1101::RegConfigSettings(void)
  244. {
  245.     SpiWriteReg(CC1101_FSCTRL1,  0x08);
  246.     SpiWriteReg(CC1101_FSCTRL0,  0x00);
  247.     SpiWriteReg(CC1101_FREQ2,    0x10);
  248.     SpiWriteReg(CC1101_FREQ1,    0xA7);
  249.     SpiWriteReg(CC1101_FREQ0,    0x62);
  250.     SpiWriteReg(CC1101_MDMCFG4,  0x5B);
  251.     SpiWriteReg(CC1101_MDMCFG3,  0xF8);
  252.     SpiWriteReg(CC1101_MDMCFG2,  0x03);
  253.     SpiWriteReg(CC1101_MDMCFG1,  0x22);
  254.     SpiWriteReg(CC1101_MDMCFG0,  0xF8);
  255.     SpiWriteReg(CC1101_CHANNR,   0x00);
  256.     SpiWriteReg(CC1101_DEVIATN,  0x47);
  257.     SpiWriteReg(CC1101_FREND1,   0xB6);
  258.     SpiWriteReg(CC1101_FREND0,   0x10);
  259.     SpiWriteReg(CC1101_MCSM0 ,   0x18);
  260.     SpiWriteReg(CC1101_FOCCFG,   0x1D);
  261.     SpiWriteReg(CC1101_BSCFG,    0x1C);
  262.     SpiWriteReg(CC1101_AGCCTRL2, 0xC7);
  263.     SpiWriteReg(CC1101_AGCCTRL1, 0x00);
  264.     SpiWriteReg(CC1101_AGCCTRL0, 0xB2);
  265.     SpiWriteReg(CC1101_FSCAL3,   0xEA);
  266.     SpiWriteReg(CC1101_FSCAL2,   0x2A);
  267.     SpiWriteReg(CC1101_FSCAL1,   0x00);
  268.     SpiWriteReg(CC1101_FSCAL0,   0x11);
  269.     SpiWriteReg(CC1101_FSTEST,   0x59);
  270.     SpiWriteReg(CC1101_TEST2,    0x81);
  271.     SpiWriteReg(CC1101_TEST1,    0x35);
  272.     SpiWriteReg(CC1101_TEST0,    0x09);
  273.     SpiWriteReg(CC1101_IOCFG2,   0x0B);     //serial clock.synchronous to the data in synchronous serial mode
  274.     SpiWriteReg(CC1101_IOCFG0,   0x06);     //asserts when sync word has been sent/received, and de-asserts at the end of the packet
  275.     SpiWriteReg(CC1101_PKTCTRL1, 0x04);     //two status bytes will be appended to the payload of the packet,including RSSI LQI and CRC OK
  276.                                             //No address check
  277.     SpiWriteReg(CC1101_PKTCTRL0, 0x05);     //whitening off;CRC Enable��variable length packets, packet length configured by the first byte after sync word
  278.     SpiWriteReg(CC1101_ADDR,     0x00);     //address used for packet filtration.
  279.     SpiWriteReg(CC1101_PKTLEN,   0x3D);     //61 bytes max length
  280. }
  281.  
  282. /****************************************************************
  283. *FUNCTION NAME:SendData
  284. *FUNCTION     :use CC1101 send data
  285. *INPUT        :txBuffer: data array to send; size: number of data to send, no more than 61
  286. *OUTPUT       :none
  287. ****************************************************************/
  288. void ELECHOUSE_CC1101::SendData(byte *txBuffer,byte size)
  289. {
  290.     SpiWriteReg(CC1101_TXFIFO,size);
  291.     SpiWriteBurstReg(CC1101_TXFIFO,txBuffer,size);          //write data to send
  292.     SpiStrobe(CC1101_STX);                                  //start send   
  293.     while (!digitalRead(GDO0));                             // Wait for GDO0 to be set -> sync transmitted  
  294.     while (digitalRead(GDO0));                              // Wait for GDO0 to be cleared -> end of packet
  295.     SpiStrobe(CC1101_SFTX);                                 //flush TXfifo
  296. }
  297.  
  298. /****************************************************************
  299. *FUNCTION NAME:SetReceive
  300. *FUNCTION     :set CC1101 to receive state
  301. *INPUT        :none
  302. *OUTPUT       :none
  303. ****************************************************************/
  304. void ELECHOUSE_CC1101::SetReceive(void)
  305. {
  306.     SpiStrobe(CC1101_SRX);
  307. }
  308.  
  309. /****************************************************************
  310. *FUNCTION NAME:CheckReceiveFlag
  311. *FUNCTION     :check receive data or not
  312. *INPUT        :none
  313. *OUTPUT       :flag: 0 no data; 1 receive data
  314. ****************************************************************/
  315. byte ELECHOUSE_CC1101::CheckReceiveFlag(void)
  316. {
  317.     if(digitalRead(GDO0))           //receive data
  318.     {
  319.         while (digitalRead(GDO0));
  320.         return 1;
  321.     }
  322.     else                            // no data
  323.     {
  324.         return 0;
  325.     }
  326. }
  327.  
  328.  
  329. /****************************************************************
  330. *FUNCTION NAME:ReceiveData
  331. *FUNCTION     :read data received from RXfifo
  332. *INPUT        :rxBuffer: buffer to store data
  333. *OUTPUT       :size of data received
  334. ****************************************************************/
  335. byte ELECHOUSE_CC1101::ReceiveData(byte *rxBuffer)
  336. {
  337.     byte size;
  338.     byte status[2];
  339.  
  340.     if(SpiReadStatus(CC1101_RXBYTES) & BYTES_IN_RXFIFO)
  341.     {
  342.         size=SpiReadReg(CC1101_RXFIFO);
  343.         SpiReadBurstReg(CC1101_RXFIFO,rxBuffer,size);
  344.         SpiReadBurstReg(CC1101_RXFIFO,status,2);
  345.         SpiStrobe(CC1101_SFRX);
  346.         return size;
  347.     }
  348.     else
  349.     {
  350.         SpiStrobe(CC1101_SFRX);
  351.         return 0;
  352.     }
  353.    
  354. }
  355.  
  356. ELECHOUSE_CC1101 ELECHOUSE_cc1101;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement