![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
TI CC1101 Transceiver
main.cpp@0:9df942ea84f4, 2010-11-21 (annotated)
- Committer:
- tmav123
- Date:
- Sun Nov 21 11:37:56 2010 +0000
- Revision:
- 0:9df942ea84f4
initial revision
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tmav123 | 0:9df942ea84f4 | 1 | #include "mbed.h" |
tmav123 | 0:9df942ea84f4 | 2 | #include "CC1101.h" |
tmav123 | 0:9df942ea84f4 | 3 | #include "RingBuffer.h" |
tmav123 | 0:9df942ea84f4 | 4 | |
tmav123 | 0:9df942ea84f4 | 5 | /////////////////////////////////////////////////// |
tmav123 | 0:9df942ea84f4 | 6 | Ticker timer; |
tmav123 | 0:9df942ea84f4 | 7 | CC1101 cc1101(p5, p6, p7, p8, p10); |
tmav123 | 0:9df942ea84f4 | 8 | DigitalIn gdo0(p9); // pin connected to gdo0 pin of CC1101 for checking that received a new packet |
tmav123 | 0:9df942ea84f4 | 9 | |
tmav123 | 0:9df942ea84f4 | 10 | DigitalOut led1(LED1); // timer blink led |
tmav123 | 0:9df942ea84f4 | 11 | DigitalOut led2(LED2); // RX led |
tmav123 | 0:9df942ea84f4 | 12 | DigitalOut led3(LED3); // TX led |
tmav123 | 0:9df942ea84f4 | 13 | Serial pc(USBTX, USBRX); // tx, rx |
tmav123 | 0:9df942ea84f4 | 14 | RingBuffer pcRX(512); // ring buffer for the pc RX data |
tmav123 | 0:9df942ea84f4 | 15 | RingBuffer pcTX(512); // ring buffer for the pc TX data |
tmav123 | 0:9df942ea84f4 | 16 | Timeout pcRXtimeout; |
tmav123 | 0:9df942ea84f4 | 17 | Timeout led2timeout; |
tmav123 | 0:9df942ea84f4 | 18 | Timeout led3timeout; |
tmav123 | 0:9df942ea84f4 | 19 | unsigned char buffer[128]; |
tmav123 | 0:9df942ea84f4 | 20 | /////////////////////////////////////////////////// |
tmav123 | 0:9df942ea84f4 | 21 | void led2timeout_func() |
tmav123 | 0:9df942ea84f4 | 22 | { |
tmav123 | 0:9df942ea84f4 | 23 | led2 = 0; |
tmav123 | 0:9df942ea84f4 | 24 | led2timeout.detach(); |
tmav123 | 0:9df942ea84f4 | 25 | } |
tmav123 | 0:9df942ea84f4 | 26 | /////////////////////////////////////////////////// |
tmav123 | 0:9df942ea84f4 | 27 | void led3timeout_func() |
tmav123 | 0:9df942ea84f4 | 28 | { |
tmav123 | 0:9df942ea84f4 | 29 | led3 = 0; |
tmav123 | 0:9df942ea84f4 | 30 | led3timeout.detach(); |
tmav123 | 0:9df942ea84f4 | 31 | } |
tmav123 | 0:9df942ea84f4 | 32 | /////////////////////////////////////////////////// |
tmav123 | 0:9df942ea84f4 | 33 | void pcRXtimeout_func() // function for transmiting the RF packets - empty the pcRX ring buffer |
tmav123 | 0:9df942ea84f4 | 34 | { |
tmav123 | 0:9df942ea84f4 | 35 | unsigned char txlength; |
tmav123 | 0:9df942ea84f4 | 36 | |
tmav123 | 0:9df942ea84f4 | 37 | txlength = 0; |
tmav123 | 0:9df942ea84f4 | 38 | while(pcRX.use() > 0) |
tmav123 | 0:9df942ea84f4 | 39 | { |
tmav123 | 0:9df942ea84f4 | 40 | led2 = 1; |
tmav123 | 0:9df942ea84f4 | 41 | buffer[txlength] = pcRX.getc(); |
tmav123 | 0:9df942ea84f4 | 42 | txlength++; |
tmav123 | 0:9df942ea84f4 | 43 | led2timeout.attach(&led2timeout_func, 0.050); // for switch off the led |
tmav123 | 0:9df942ea84f4 | 44 | } |
tmav123 | 0:9df942ea84f4 | 45 | if (txlength) |
tmav123 | 0:9df942ea84f4 | 46 | cc1101.SendPacket(buffer, txlength); // tx packet |
tmav123 | 0:9df942ea84f4 | 47 | |
tmav123 | 0:9df942ea84f4 | 48 | pcRXtimeout.detach(); |
tmav123 | 0:9df942ea84f4 | 49 | } |
tmav123 | 0:9df942ea84f4 | 50 | /////////////////////////////////////////////////// |
tmav123 | 0:9df942ea84f4 | 51 | void timer_func() // check the status of the CC1101 every 100ms |
tmav123 | 0:9df942ea84f4 | 52 | { |
tmav123 | 0:9df942ea84f4 | 53 | unsigned char chip_status_rx, chip_status_tx; |
tmav123 | 0:9df942ea84f4 | 54 | |
tmav123 | 0:9df942ea84f4 | 55 | led1 = !led1; |
tmav123 | 0:9df942ea84f4 | 56 | chip_status_rx = cc1101.ReadChipStatusRX(); // check the rx status |
tmav123 | 0:9df942ea84f4 | 57 | if ((chip_status_rx & CHIP_STATE_MASK) == CHIP_STATE_RXFIFO_OVERFLOW) // if rx overflow flush the rx fifo |
tmav123 | 0:9df942ea84f4 | 58 | cc1101.FlushRX(); |
tmav123 | 0:9df942ea84f4 | 59 | if ((chip_status_rx & CHIP_STATE_MASK) == CHIP_STATE_IDLE) // if state is idle go to rx state again |
tmav123 | 0:9df942ea84f4 | 60 | cc1101.RXMode(); |
tmav123 | 0:9df942ea84f4 | 61 | chip_status_tx = cc1101.ReadChipStatusTX(); // check the tx sttus |
tmav123 | 0:9df942ea84f4 | 62 | if ((chip_status_tx & CHIP_STATE_MASK) == CHIP_STATE_TXFIFO_UNDERFLOW) // if tx underflow flush the tx fifo |
tmav123 | 0:9df942ea84f4 | 63 | cc1101.FlushTX(); |
tmav123 | 0:9df942ea84f4 | 64 | } |
tmav123 | 0:9df942ea84f4 | 65 | /////////////////////////////////////////////////// |
tmav123 | 0:9df942ea84f4 | 66 | int main() |
tmav123 | 0:9df942ea84f4 | 67 | { |
tmav123 | 0:9df942ea84f4 | 68 | unsigned char rxlength, i; |
tmav123 | 0:9df942ea84f4 | 69 | |
tmav123 | 0:9df942ea84f4 | 70 | pcRX.clear(); |
tmav123 | 0:9df942ea84f4 | 71 | pcTX.clear(); |
tmav123 | 0:9df942ea84f4 | 72 | cc1101.init(); |
tmav123 | 0:9df942ea84f4 | 73 | timer.attach(&timer_func, 0.1); |
tmav123 | 0:9df942ea84f4 | 74 | while(1) |
tmav123 | 0:9df942ea84f4 | 75 | { |
tmav123 | 0:9df942ea84f4 | 76 | if(gdo0) // rx finished and CRC OK read the new packet |
tmav123 | 0:9df942ea84f4 | 77 | { |
tmav123 | 0:9df942ea84f4 | 78 | rxlength = sizeof(buffer); |
tmav123 | 0:9df942ea84f4 | 79 | if (cc1101.ReceivePacket(buffer, &rxlength) == 1) // read the rx packet |
tmav123 | 0:9df942ea84f4 | 80 | { |
tmav123 | 0:9df942ea84f4 | 81 | led3 = 1; |
tmav123 | 0:9df942ea84f4 | 82 | for (i = 0; i < rxlength; i++) |
tmav123 | 0:9df942ea84f4 | 83 | pcTX.putc(buffer[i]); // store the packet to the pcTX ring buffer |
tmav123 | 0:9df942ea84f4 | 84 | led3timeout.attach(&led3timeout_func, 0.050); // for switch off the led |
tmav123 | 0:9df942ea84f4 | 85 | } |
tmav123 | 0:9df942ea84f4 | 86 | } |
tmav123 | 0:9df942ea84f4 | 87 | if (pcTX.use() > 0) // check if we have data to transmit to pc |
tmav123 | 0:9df942ea84f4 | 88 | pc.putc(pcTX.getc()); // get the data from the ring buffer and transmit it to the pc |
tmav123 | 0:9df942ea84f4 | 89 | if (pc.readable()) // check if we received new data from the pc |
tmav123 | 0:9df942ea84f4 | 90 | { |
tmav123 | 0:9df942ea84f4 | 91 | 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 |
tmav123 | 0:9df942ea84f4 | 92 | pcRXtimeout.attach(&pcRXtimeout_func, 0.020); |
tmav123 | 0:9df942ea84f4 | 93 | } |
tmav123 | 0:9df942ea84f4 | 94 | if (pcRX.use() > 20) // if more than 20 bytes received then tx the packet in RF |
tmav123 | 0:9df942ea84f4 | 95 | pcRXtimeout_func(); |
tmav123 | 0:9df942ea84f4 | 96 | } |
tmav123 | 0:9df942ea84f4 | 97 | } |
tmav123 | 0:9df942ea84f4 | 98 | /////////////////////////////////////////////////// |