| 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 | cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and |
| 14 | publish Wilson Shen (ELECHOUSE). |
| 15 | ---------------------------------------------------------------------------------------------------------------- |
| 16 | */ |
| 17 | #ifndef ELECHOUSE_CC1101_SRC_DRV_h |
| 18 | #define ELECHOUSE_CC1101_SRC_DRV_h |
| 19 | |
| 20 | #include <Arduino.h> |
| 21 | |
| 22 | //***************************************CC1101 |
| 23 | //define**************************************************// |
| 24 | // CC1101 CONFIG REGSITER |
| 25 | #define CC1101_IOCFG2 0x00 // GDO2 output pin configuration |
| 26 | #define CC1101_IOCFG1 0x01 // GDO1 output pin configuration |
| 27 | #define CC1101_IOCFG0 0x02 // GDO0 output pin configuration |
| 28 | #define CC1101_FIFOTHR 0x03 // RX FIFO and TX FIFO thresholds |
| 29 | #define CC1101_SYNC1 0x04 // Sync word, high INT8U |
| 30 | #define CC1101_SYNC0 0x05 // Sync word, low INT8U |
| 31 | #define CC1101_PKTLEN 0x06 // Packet length |
| 32 | #define CC1101_PKTCTRL1 0x07 // Packet automation control |
| 33 | #define CC1101_PKTCTRL0 0x08 // Packet automation control |
| 34 | #define CC1101_ADDR 0x09 // Device address |
| 35 | #define CC1101_CHANNR 0x0A // Channel number |
| 36 | #define CC1101_FSCTRL1 0x0B // Frequency synthesizer control |
| 37 | #define CC1101_FSCTRL0 0x0C // Frequency synthesizer control |
| 38 | #define CC1101_FREQ2 0x0D // Frequency control word, high INT8U |
| 39 | #define CC1101_FREQ1 0x0E // Frequency control word, middle INT8U |
| 40 | #define CC1101_FREQ0 0x0F // Frequency control word, low INT8U |
| 41 | #define CC1101_MDMCFG4 0x10 // Modem configuration |
| 42 | #define CC1101_MDMCFG3 0x11 // Modem configuration |
| 43 | #define CC1101_MDMCFG2 0x12 // Modem configuration |
| 44 | #define CC1101_MDMCFG1 0x13 // Modem configuration |
| 45 | #define CC1101_MDMCFG0 0x14 // Modem configuration |
| 46 | #define CC1101_DEVIATN 0x15 // Modem deviation setting |
| 47 | #define CC1101_MCSM2 0x16 // Main Radio Control State Machine configuration |
| 48 | #define CC1101_MCSM1 0x17 // Main Radio Control State Machine configuration |
| 49 | #define CC1101_MCSM0 0x18 // Main Radio Control State Machine configuration |
| 50 | #define CC1101_FOCCFG 0x19 // Frequency Offset Compensation configuration |
| 51 | #define CC1101_BSCFG 0x1A // Bit Synchronization configuration |
| 52 | #define CC1101_AGCCTRL2 0x1B // AGC control |
| 53 | #define CC1101_AGCCTRL1 0x1C // AGC control |
| 54 | #define CC1101_AGCCTRL0 0x1D // AGC control |
| 55 | #define CC1101_WOREVT1 0x1E // High INT8U Event 0 timeout |
| 56 | #define CC1101_WOREVT0 0x1F // Low INT8U Event 0 timeout |
| 57 | #define CC1101_WORCTRL 0x20 // Wake On Radio control |
| 58 | #define CC1101_FREND1 0x21 // Front end RX configuration |
| 59 | #define CC1101_FREND0 0x22 // Front end TX configuration |
| 60 | #define CC1101_FSCAL3 0x23 // Frequency synthesizer calibration |
| 61 | #define CC1101_FSCAL2 0x24 // Frequency synthesizer calibration |
| 62 | #define CC1101_FSCAL1 0x25 // Frequency synthesizer calibration |
| 63 | #define CC1101_FSCAL0 0x26 // Frequency synthesizer calibration |
| 64 | #define CC1101_RCCTRL1 0x27 // RC oscillator configuration |
| 65 | #define CC1101_RCCTRL0 0x28 // RC oscillator configuration |
| 66 | #define CC1101_FSTEST 0x29 // Frequency synthesizer calibration control |
| 67 | #define CC1101_PTEST 0x2A // Production test |
| 68 | #define CC1101_AGCTEST 0x2B // AGC test |
| 69 | #define CC1101_TEST2 0x2C // Various test settings |
| 70 | #define CC1101_TEST1 0x2D // Various test settings |
| 71 | #define CC1101_TEST0 0x2E // Various test settings |
| 72 | |
| 73 | // CC1101 Strobe commands |
| 74 | #define CC1101_SRES 0x30 // Reset chip. |
| 75 | #define CC1101_SFSTXON \ |
| 76 | 0x31 // Enable and calibrate frequency synthesizer (if MCSM0.FS_AUTOCAL=1). |
| 77 | // If in RX/TX: Go to a wait state where only the synthesizer is |
| 78 | // running (for quick RX / TX turnaround). |
| 79 | #define CC1101_SXOFF 0x32 // Turn off crystal oscillator. |
| 80 | #define CC1101_SCAL \ |
| 81 | 0x33 // Calibrate frequency synthesizer and turn it off |
| 82 | // (enables quick start). |
| 83 | #define CC1101_SRX \ |
| 84 | 0x34 // Enable RX. Perform calibration first if coming from IDLE and |
| 85 | // MCSM0.FS_AUTOCAL=1. |
| 86 | #define CC1101_STX \ |
| 87 | 0x35 // In IDLE state: Enable TX. Perform calibration first if |
| 88 | // MCSM0.FS_AUTOCAL=1. If in RX state and CCA is enabled: |
| 89 | // Only go to TX if channel is clear. |
| 90 | #define CC1101_SIDLE \ |
| 91 | 0x36 // Exit RX / TX, turn off frequency synthesizer and exit |
| 92 | // Wake-On-Radio mode if applicable. |
| 93 | #define CC1101_SAFC 0x37 // Perform AFC adjustment of the frequency synthesizer |
| 94 | #define CC1101_SWOR 0x38 // Start automatic RX polling sequence (Wake-on-Radio) |
| 95 | #define CC1101_SPWD 0x39 // Enter power down mode when CSn goes high. |
| 96 | #define CC1101_SFRX 0x3A // Flush the RX FIFO buffer. |
| 97 | #define CC1101_SFTX 0x3B // Flush the TX FIFO buffer. |
| 98 | #define CC1101_SWORRST 0x3C // Reset real time clock. |
| 99 | #define CC1101_SNOP \ |
| 100 | 0x3D // No operation. May be used to pad strobe commands to two |
| 101 | // INT8Us for simpler software. |
| 102 | // CC1101 STATUS REGSITER |
| 103 | #define CC1101_PARTNUM 0x30 |
| 104 | #define CC1101_VERSION 0x31 |
| 105 | #define CC1101_FREQEST 0x32 |
| 106 | #define CC1101_LQI 0x33 |
| 107 | #define CC1101_RSSI 0x34 |
| 108 | #define CC1101_MARCSTATE 0x35 |
| 109 | #define CC1101_WORTIME1 0x36 |
| 110 | #define CC1101_WORTIME0 0x37 |
| 111 | #define CC1101_PKTSTATUS 0x38 |
| 112 | #define CC1101_VCO_VC_DAC 0x39 |
| 113 | #define CC1101_TXBYTES 0x3A |
| 114 | #define CC1101_RXBYTES 0x3B |
| 115 | |
| 116 | // CC1101 PATABLE,TXFIFO,RXFIFO |
| 117 | #define CC1101_PATABLE 0x3E |
| 118 | #define CC1101_TXFIFO 0x3F |
| 119 | #define CC1101_RXFIFO 0x3F |
| 120 | |
| 121 | //************************************* class |
| 122 | //**************************************************// |
| 123 | class ELECHOUSE_CC1101 { |
| 124 | private: |
| 125 | void SpiStart(void); |
| 126 | void SpiEnd(void); |
| 127 | void GDO_Set(void); |
| 128 | void GDO0_Set(void); |
| 129 | void Reset(void); |
| 130 | void setSpi(void); |
| 131 | void RegConfigSettings(void); |
| 132 | void Calibrate(void); |
| 133 | void Split_PKTCTRL0(void); |
| 134 | void Split_PKTCTRL1(void); |
| 135 | void Split_MDMCFG1(void); |
| 136 | void Split_MDMCFG2(void); |
| 137 | void Split_MDMCFG4(void); |
| 138 | |
| 139 | public: |
| 140 | void Init(void); |
| 141 | byte SpiReadStatus(byte addr); |
| 142 | void setSpiPin(byte sck, byte miso, byte mosi, byte ss); |
| 143 | void addSpiPin(byte sck, byte miso, byte mosi, byte ss, byte modul); |
| 144 | void setGDO(byte gdo0, byte gdo2); |
| 145 | void setGDO0(byte gdo0); |
| 146 | void addGDO(byte gdo0, byte gdo2, byte modul); |
| 147 | void addGDO0(byte gdo0, byte modul); |
| 148 | void setModul(byte modul); |
| 149 | void setCCMode(bool s); |
| 150 | void setModulation(byte m); |
| 151 | void setPA(int p); |
| 152 | void setMHZ(float mhz); |
| 153 | void setChannel(byte chnl); |
| 154 | void setChsp(float f); |
| 155 | void setRxBW(float f); |
| 156 | void setDRate(float d); |
| 157 | void setDeviation(float d); |
| 158 | void SetTx(void); |
| 159 | void SetRx(void); |
| 160 | void SetTx(float mhz); |
| 161 | void SetRx(float mhz); |
| 162 | int getRssi(void); |
| 163 | byte getLqi(void); |
| 164 | void setSres(void); |
| 165 | void setSidle(void); |
| 166 | void goSleep(void); |
| 167 | void SendData(byte *txBuffer, byte size); |
| 168 | void SendData(char *txchar); |
| 169 | void SendData(byte *txBuffer, byte size, int t); |
| 170 | void SendData(char *txchar, int t); |
| 171 | byte CheckReceiveFlag(void); |
| 172 | byte ReceiveData(byte *rxBuffer); |
| 173 | bool CheckCRC(void); |
| 174 | void SpiStrobe(byte strobe); |
| 175 | void SpiWriteReg(byte addr, byte value); |
| 176 | void SpiWriteBurstReg(byte addr, byte *buffer, byte num); |
| 177 | byte SpiReadReg(byte addr); |
| 178 | void SpiReadBurstReg(byte addr, byte *buffer, byte num); |
| 179 | void setClb(byte b, byte s, byte e); |
| 180 | bool getCC1101(void); |
| 181 | byte getMode(void); |
| 182 | void setSyncWord(byte sh, byte sl); |
| 183 | void setAddr(byte v); |
| 184 | void setWhiteData(bool v); |
| 185 | void setPktFormat(byte v); |
| 186 | void setCrc(bool v); |
| 187 | void setLengthConfig(byte v); |
| 188 | void setPacketLength(byte v); |
| 189 | void setDcFilterOff(bool v); |
| 190 | void setManchester(bool v); |
| 191 | void setSyncMode(byte v); |
| 192 | void setFEC(bool v); |
| 193 | void setPRE(byte v); |
| 194 | void setPQT(byte v); |
| 195 | void setCRC_AF(bool v); |
| 196 | void setAppendStatus(bool v); |
| 197 | void setAdrChk(byte v); |
| 198 | bool CheckRxFifo(int t); |
| 199 | }; |
| 200 | |
| 201 | extern ELECHOUSE_CC1101 ELECHOUSE_cc1101; |
| 202 | |
| 203 | #endif |