TI CC1101 Transceiver

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "CC1101.h"
00003 #include "RingBuffer.h"
00004  
00005 ///////////////////////////////////////////////////
00006 Ticker timer;
00007 CC1101 cc1101(p5, p6, p7, p8, p10);
00008 DigitalIn gdo0(p9);     // pin connected to gdo0 pin of CC1101 for checking that received a new packet
00009 
00010 DigitalOut led1(LED1);  // timer blink led
00011 DigitalOut led2(LED2);  // RX led
00012 DigitalOut led3(LED3);  // TX led
00013 Serial pc(USBTX, USBRX); // tx, rx
00014 RingBuffer pcRX(512);   // ring buffer for the pc RX data
00015 RingBuffer pcTX(512);   // ring buffer for the pc TX data
00016 Timeout pcRXtimeout;
00017 Timeout led2timeout;
00018 Timeout led3timeout;
00019 unsigned char buffer[128];
00020 ///////////////////////////////////////////////////
00021 void led2timeout_func()
00022 {
00023     led2 = 0;
00024     led2timeout.detach();
00025 }
00026 ///////////////////////////////////////////////////
00027 void led3timeout_func()
00028 {
00029     led3 = 0;
00030     led3timeout.detach();
00031 }
00032 ///////////////////////////////////////////////////
00033 void pcRXtimeout_func()         // function for transmiting the RF packets - empty the pcRX ring buffer
00034 {
00035     unsigned char txlength;
00036     
00037     txlength = 0;
00038     while(pcRX.use() > 0)
00039     {
00040       led2 = 1;
00041       buffer[txlength] = pcRX.getc();
00042       txlength++;
00043       led2timeout.attach(&led2timeout_func, 0.050);  // for switch off the led
00044     }
00045     if (txlength)
00046       cc1101.SendPacket(buffer, txlength);    // tx packet
00047     
00048     pcRXtimeout.detach();
00049 }
00050 ///////////////////////////////////////////////////
00051 void timer_func()           // check the status of the CC1101 every 100ms
00052 {    
00053     unsigned char chip_status_rx, chip_status_tx;
00054     
00055     led1 = !led1;
00056     chip_status_rx = cc1101.ReadChipStatusRX();  // check the rx status 
00057     if ((chip_status_rx & CHIP_STATE_MASK) == CHIP_STATE_RXFIFO_OVERFLOW)   // if rx overflow flush the rx fifo
00058       cc1101.FlushRX();  
00059     if ((chip_status_rx & CHIP_STATE_MASK) == CHIP_STATE_IDLE)              // if state is idle go to rx state again
00060       cc1101.RXMode();  
00061     chip_status_tx = cc1101.ReadChipStatusTX();  // check the tx sttus
00062     if ((chip_status_tx & CHIP_STATE_MASK) == CHIP_STATE_TXFIFO_UNDERFLOW)  // if tx underflow flush the tx fifo
00063       cc1101.FlushTX();
00064 }
00065 ///////////////////////////////////////////////////
00066 int main() 
00067 {
00068     unsigned char rxlength, i;
00069     
00070     pcRX.clear();
00071     pcTX.clear();
00072     cc1101.init();
00073     timer.attach(&timer_func, 0.1);
00074     while(1)
00075     {
00076         if(gdo0)      // rx finished and CRC OK read the new packet
00077         {
00078             rxlength = sizeof(buffer);
00079             if (cc1101.ReceivePacket(buffer, &rxlength) == 1)   // read the rx packet
00080             {
00081                 led3 = 1;
00082                 for (i = 0; i < rxlength; i++)
00083                     pcTX.putc(buffer[i]);                       // store the packet to the pcTX ring buffer 
00084                 led3timeout.attach(&led3timeout_func, 0.050);   // for switch off the led
00085             }
00086         }
00087         if (pcTX.use() > 0)         // check if we have data to transmit to pc
00088             pc.putc(pcTX.getc());   // get the data from the ring buffer and transmit it to the pc
00089         if (pc.readable())          // check if we received new data from the pc
00090         {
00091             pcRX.putc(pc.getc());   // put the data to the pcRX buffer and wait until 20ms passed till the last byte before tx the packet in RF
00092             pcRXtimeout.attach(&pcRXtimeout_func, 0.020);
00093         }
00094         if (pcRX.use() > 20)        // if more than 20 bytes received then tx the packet in RF
00095             pcRXtimeout_func();
00096     }     
00097 }
00098 ///////////////////////////////////////////////////