Codebase from CC1101_Transceiver, ported to LPC1114/LPC824/STM32F103 and other micros, will be merged with panStamp project to replace AVR/MSP.

Dependencies:   mbed

Fork of CC1101_Transceiver_LPC1114 by Kai Liu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002     Author: Allan K Liu
00003     Original: Athanassios Mavrogeorgiadis
00004     
00005     Changes:
00006     - Optimization for Low Power Consumption Operations
00007     - GD0 as external interrupt, to bring MCU from deep sleep mode.
00008     - GPIO setup
00009     - Clock setup
00010     - Event driven design
00011     - ALOHA transceiver for channel access with RTS/CTS/ACK
00012     - S-MAC
00013     - Cloned high level designs from SimpliciTI.
00014     - New CircularBuffer from mbed 5
00015     
00016     Status:
00017     - CC1101 SPI access to regsiters from PARTNUM/VERSION to RCCTRL0 not stable
00018     - PARTNUM ranges from 0x00/0xFF
00019     - VERSION ranges from 0x0F/0x1F/0x4F
00020  */
00021 #include "mbed.h"
00022 #include "CC1101.h"
00023 #include "RingBuffer.h"
00024  
00025 
00026 #if defined(TARGET_NUCLEO_F103RB) || defined(TARGET_LPC824)
00027 // RDmiso is actually the MISO, 
00028 // SPI only works after MISO turns into low from Hi-Z state of CC1101
00029 // Therefore we need a dedicated input to detect.
00030 CC1101 cc1101(D11, D12, D13, D10, D8);    // MOSI, MISO, SCK, nCS, RDmiso
00031 DigitalIn gdo0(D7);     // InterruptIn is better than DigitalIn
00032 InterruptIn gdo2(D9);   // InterruptIn is better than DigitalIn
00033 
00034 DigitalOut led1(A0);  // timer blink led
00035 DigitalOut led2(A1);  // RX led
00036 DigitalOut led3(A2);  // TX led
00037 
00038 #elif defined(TARGET_LPC1114)
00039 // Legacy platform, to be updated
00040 CC1101 cc1101(p5, p6, p7, p8, p10);
00041 DigitalIn gdo0(p9);     // pin connected to gdo0 pin of CC1101 for checking that received a new packet
00042 
00043 DigitalOut led1(LED1);  // timer blink led
00044 DigitalOut led2(LED2);  // RX led
00045 DigitalOut led3(LED3);  // TX led
00046 
00047 #else
00048 
00049 #warning "YOU HAVE TO DEFINE A H/W."
00050 #endif
00051 
00052 
00053 Ticker timer;
00054 
00055 
00056 Serial pc(USBTX, USBRX); // tx, rx, to be replaced by USB CDC interface in a hub/sink node.
00057 RingBuffer rbRX(512);   // ring buffer for the pc RX data
00058 RingBuffer rbTX(512);   // ring buffer for the pc TX data
00059 
00060 Timeout rbRXtimeout;
00061 Timeout led2timeout;
00062 Timeout led3timeout;
00063 
00064 unsigned char buffer[128];
00065 
00066 static unsigned char sil = 0;
00067 static unsigned char ver = 0;
00068 static unsigned char sta = 0;
00069 static unsigned char val = 0;
00070 
00071 void led2timeout_func()
00072 {
00073     led2 = 0;
00074     led2timeout.detach();
00075 }
00076 
00077 void led3timeout_func()
00078 {
00079     led3 = 0;
00080     led3timeout.detach();
00081 }
00082 
00083 void rbRXtimeout_func()         // function for transmiting the RF packets - empty the rbRX ring buffer
00084 {
00085     unsigned char txlength;
00086     
00087     txlength = 0;
00088     while(rbRX.use() > 0)
00089     {
00090       led2 = 1;
00091       buffer[txlength] = rbRX.getc();
00092       txlength++;
00093       led2timeout.attach(&led2timeout_func, 0.050);  // for switch off the led
00094     }
00095     if (txlength)
00096       cc1101.SendPacket(buffer, txlength);    // tx packet
00097     
00098     rbRXtimeout.detach();
00099 }
00100 
00101 void timer_func()           // check the status of the CC1101 every 100ms
00102 {    
00103     unsigned char chip_status_rx, chip_status_tx;
00104     
00105     led1 = !led1;
00106     chip_status_rx = cc1101.ReadChipStatusRX();  // check the rx status 
00107     if ((chip_status_rx & CHIP_STATE_MASK) == CHIP_STATE_RXFIFO_OVERFLOW)   // if rx overflow flush the rx fifo
00108       cc1101.FlushRX();  
00109     if ((chip_status_rx & CHIP_STATE_MASK) == CHIP_STATE_IDLE)              // if state is idle go to rx state again
00110       cc1101.RXMode();  
00111     chip_status_tx = cc1101.ReadChipStatusTX();  // check the tx sttus
00112     if ((chip_status_tx & CHIP_STATE_MASK) == CHIP_STATE_TXFIFO_UNDERFLOW)  // if tx underflow flush the tx fifo
00113       cc1101.FlushTX();
00114 }
00115 
00116 int main() 
00117 {
00118     unsigned char rxlength, i;
00119         unsigned char buf[128];
00120     
00121     rbRX.clear();
00122     rbTX.clear();
00123     cc1101.init();
00124 
00125         // test routines
00126 
00127 #define REG_PART_DBG 1  
00128 #if defined(REG_PART_DBG)
00129     
00130         for (int i=0; i<10; i++){
00131             buf[i] = cc1101.ReadChipStatusRX();
00132         }
00133         cc1101.ReadBurstReg(CCxxx0_PARTNUM, buf, CCxxx0_PATABLE-CCxxx0_PARTNUM+1);
00134 
00135     // Read them one by one
00136         for (int i=CCxxx0_PARTNUM; i<CCxxx0_PATABLE; i++){
00137             buf[i] = cc1101.ReadReg(i);
00138         }
00139     
00140         // Try to read back all of values from registers ranges from 0x30~0x3D
00141         // Read them one by one
00142         sil = cc1101.ReadReg(CCxxx0_PARTNUM);
00143       ver = cc1101.ReadReg(CCxxx0_VERSION);
00144         sta = cc1101.ReadChipStatusRX();
00145     
00146         for (int i=0; i<10; i++){
00147             buf[i] = cc1101.ReadReg(CCxxx0_PARTNUM);
00148         }
00149 
00150         for (int i=0; i<10; i++){
00151             buf[i] = cc1101.ReadReg(CCxxx0_VERSION);
00152         }
00153 
00154         for (int i=0; i<10; i++){
00155             buf[i] = cc1101.ReadChipStatusRX();
00156         }
00157 
00158         // Read them in a burst reading
00159         cc1101.ReadBurstReg(CCxxx0_PARTNUM, buf, CCxxx0_PATABLE-CCxxx0_PARTNUM+1);
00160         // Read them in a burst reading twice
00161         cc1101.ReadBurstReg(CCxxx0_PARTNUM, buf, CCxxx0_PATABLE-CCxxx0_PARTNUM+1);
00162         // Read them one by one
00163         for (int i=CCxxx0_PARTNUM; i<CCxxx0_PATABLE; i++){
00164             buf[i] = cc1101.ReadReg(i);
00165         }
00166         // Read them in a burst reading
00167         cc1101.ReadBurstReg(CCxxx0_PARTNUM, buf, CCxxx0_PATABLE-CCxxx0_PARTNUM+1);
00168         
00169 #endif
00170 
00171 //#define REG_RW_DBG 1
00172 #if defined(REG_RW_DBG)
00173         // Try to read back all of values from registers ranges from 0x00~0x2E
00174         cc1101.ReadBurstReg(CCxxx0_IOCFG2, buf, CCxxx0_TEST0+1);
00175         
00176         for (int i=CCxxx0_IOCFG2; i<(CCxxx0_TEST0+1); i++){
00177             buf[i] = cc1101.ReadReg(i);
00178         }
00179         
00180         const unsigned char params[0x2F] = \
00181             {0x06,0x2E,0x07,0x07,0xD3,0x91,0xFF,0x04, \
00182              0x05,0x00,0x00,0x06,0x00,0x10,0xB1,0x3B, \
00183              0xF8,0x83,0x13,0x22,0xF8,0x15,0x07,0x3F, \
00184              0x18,0x16,0x6C,0x03,0x40,0x91,0x87,0x6B, \
00185              0xF8,0x56,0x10,0xE9,0x2A,0x00,0x1F,0x41, \
00186              0x00,0x59,0x7F,0x63,0x88,0x31,0x09};
00187             
00188         for (int i=CCxxx0_IOCFG2; i<(CCxxx0_TEST0+1); i++){
00189             cc1101.WriteReg(i, params[i]);
00190         }
00191 
00192         for (int i=CCxxx0_IOCFG2; i<(CCxxx0_TEST0+1); i++){
00193             buf[i] = cc1101.ReadReg(i);
00194         }
00195 
00196     cc1101.ReadBurstReg(CCxxx0_IOCFG2, buf, CCxxx0_TEST0+1);
00197 
00198 #endif      
00199         
00200         // end of test
00201     
00202     timer.attach(&timer_func, 0.1);
00203     while(1)
00204     {
00205         if(gdo0)      // rx finished and CRC OK read the new packet
00206         {
00207             rxlength = sizeof(buffer);
00208             if (cc1101.ReceivePacket(buffer, &rxlength) == 1)   // read the rx packet
00209             {
00210                 led3 = 1;
00211                 for (i = 0; i < rxlength; i++)
00212                     rbTX.putc(buffer[i]);                       // store the packet to the rbTX ring buffer 
00213                 led3timeout.attach(&led3timeout_func, 0.050);   // for switch off the led
00214             }
00215         }
00216         if (rbTX.use() > 0)         // check if we have data to transmit to pc
00217             pc.putc(rbTX.getc());   // get the data from the ring buffer and transmit it to the pc
00218         if (pc.readable())          // check if we received new data from the pc
00219         {
00220             rbRX.putc(pc.getc());   // put the data to the rbRX buffer and wait until 20ms passed till the last byte before tx the packet in RF
00221             rbRXtimeout.attach(&rbRXtimeout_func, 0.020);
00222         }
00223         if (rbRX.use() > 20)        // if more than 20 bytes received then tx the packet in RF
00224             rbRXtimeout_func();
00225     }     
00226 }