This is modified program from original CC1101_Transceiver. CC1101 Test succeeded STM32-F407 platform. Feature are improved little bit.
Dependencies: mbed
TI CC1101 chip is embedded along with STM32-F407 MCU.
main.cpp@2:54c832c68208, 2015-03-31 (annotated)
- Committer:
- hillkim7
- Date:
- Tue Mar 31 08:48:00 2015 +0000
- Revision:
- 2:54c832c68208
- Parent:
- 1:e96096a7b90b
Fix misspelled word.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hillkim7 | 1:e96096a7b90b | 1 | /** |
hillkim7 | 1:e96096a7b90b | 2 | * This program prints input text to serial port of each other side. |
hillkim7 | 1:e96096a7b90b | 3 | * Test result: TX, RX work well with 433MHz on STM32-F407 platform. |
hillkim7 | 1:e96096a7b90b | 4 | * Two features are improved from the original source code. |
hillkim7 | 1:e96096a7b90b | 5 | * - Synchronous implementation of CC1101::SendPacket() |
hillkim7 | 2:54c832c68208 | 6 | * For subsequent packet transmission, now this function returns after packet transmission. |
hillkim7 | 1:e96096a7b90b | 7 | * - FIFO checking routine changed. |
hillkim7 | 2:54c832c68208 | 8 | * Original code uses timer to check FIFO, it may cause resource corruption by timer interrupt. |
hillkim7 | 1:e96096a7b90b | 9 | */ |
tmav123 | 0:9df942ea84f4 | 10 | #include "mbed.h" |
tmav123 | 0:9df942ea84f4 | 11 | #include "CC1101.h" |
tmav123 | 0:9df942ea84f4 | 12 | #include "RingBuffer.h" |
hillkim7 | 1:e96096a7b90b | 13 | |
hillkim7 | 1:e96096a7b90b | 14 | #if 0 |
hillkim7 | 1:e96096a7b90b | 15 | #define PRINTF pc.printf |
hillkim7 | 1:e96096a7b90b | 16 | #else |
hillkim7 | 1:e96096a7b90b | 17 | #define PRINTF(...) |
hillkim7 | 1:e96096a7b90b | 18 | #endif |
hillkim7 | 1:e96096a7b90b | 19 | |
hillkim7 | 1:e96096a7b90b | 20 | #define FIFO_CHECK_INTERVAL_US 500000 |
hillkim7 | 1:e96096a7b90b | 21 | |
tmav123 | 0:9df942ea84f4 | 22 | /////////////////////////////////////////////////// |
hillkim7 | 1:e96096a7b90b | 23 | const PinName mosi = PB_5; |
hillkim7 | 1:e96096a7b90b | 24 | const PinName miso = PB_4; |
hillkim7 | 1:e96096a7b90b | 25 | const PinName clk = PB_3; |
hillkim7 | 1:e96096a7b90b | 26 | const PinName csn = PA_15; |
hillkim7 | 1:e96096a7b90b | 27 | // RDmiso --> pin to detect MISO low. |
hillkim7 | 1:e96096a7b90b | 28 | const PinName RDmiso = PD_7; |
tmav123 | 0:9df942ea84f4 | 29 | |
hillkim7 | 1:e96096a7b90b | 30 | // pin connected to GDO0 pin of CC1101 for checking that received a new packet |
hillkim7 | 1:e96096a7b90b | 31 | // It related with CC1101 IOCFG0 register. |
hillkim7 | 1:e96096a7b90b | 32 | const PinName gdo0 = PE_0; |
hillkim7 | 1:e96096a7b90b | 33 | |
hillkim7 | 1:e96096a7b90b | 34 | // pin for checking that sent a packet |
hillkim7 | 2:54c832c68208 | 35 | // It related with CC1101 IOCFG02register. |
hillkim7 | 2:54c832c68208 | 36 | const PinName gdo2 = PE_1; |
hillkim7 | 2:54c832c68208 | 37 | |
hillkim7 | 2:54c832c68208 | 38 | CC1101 cc1101(mosi, miso, clk, csn, RDmiso, gdo0, gdo2); |
hillkim7 | 1:e96096a7b90b | 39 | |
hillkim7 | 2:54c832c68208 | 40 | #if 1 |
hillkim7 | 2:54c832c68208 | 41 | Serial pc(PD_8, PD_9); // tx, rx |
hillkim7 | 2:54c832c68208 | 42 | DigitalOut led1(PD_12); // FIFO led |
hillkim7 | 2:54c832c68208 | 43 | DigitalOut led2(PD_13); // RX led |
hillkim7 | 2:54c832c68208 | 44 | DigitalOut led3(PD_14); // TX led |
hillkim7 | 2:54c832c68208 | 45 | #else |
hillkim7 | 1:e96096a7b90b | 46 | Serial pc(USBTX, USBRX); |
hillkim7 | 1:e96096a7b90b | 47 | DigitalOut led1(LED1); // FIFO led |
tmav123 | 0:9df942ea84f4 | 48 | DigitalOut led2(LED2); // RX led |
tmav123 | 0:9df942ea84f4 | 49 | DigitalOut led3(LED3); // TX led |
hillkim7 | 2:54c832c68208 | 50 | #endif |
tmav123 | 0:9df942ea84f4 | 51 | RingBuffer pcRX(512); // ring buffer for the pc RX data |
tmav123 | 0:9df942ea84f4 | 52 | RingBuffer pcTX(512); // ring buffer for the pc TX data |
tmav123 | 0:9df942ea84f4 | 53 | Timeout pcRXtimeout; |
tmav123 | 0:9df942ea84f4 | 54 | Timeout led2timeout; |
tmav123 | 0:9df942ea84f4 | 55 | Timeout led3timeout; |
hillkim7 | 1:e96096a7b90b | 56 | unsigned char buffer[64]; |
hillkim7 | 1:e96096a7b90b | 57 | |
hillkim7 | 1:e96096a7b90b | 58 | /////////////////////////////////////////////////// |
hillkim7 | 1:e96096a7b90b | 59 | static uint32_t compute_elapse_us(uint32_t prev_tick, uint32_t cur_tick) |
hillkim7 | 1:e96096a7b90b | 60 | { |
hillkim7 | 1:e96096a7b90b | 61 | if (prev_tick <= cur_tick) |
hillkim7 | 1:e96096a7b90b | 62 | return cur_tick - prev_tick; |
hillkim7 | 1:e96096a7b90b | 63 | |
hillkim7 | 1:e96096a7b90b | 64 | // tick wrap around happens, it assumes tick start from 0. |
hillkim7 | 1:e96096a7b90b | 65 | return cur_tick; |
hillkim7 | 1:e96096a7b90b | 66 | } |
tmav123 | 0:9df942ea84f4 | 67 | /////////////////////////////////////////////////// |
tmav123 | 0:9df942ea84f4 | 68 | void led2timeout_func() |
tmav123 | 0:9df942ea84f4 | 69 | { |
tmav123 | 0:9df942ea84f4 | 70 | led2 = 0; |
tmav123 | 0:9df942ea84f4 | 71 | led2timeout.detach(); |
tmav123 | 0:9df942ea84f4 | 72 | } |
tmav123 | 0:9df942ea84f4 | 73 | /////////////////////////////////////////////////// |
tmav123 | 0:9df942ea84f4 | 74 | void led3timeout_func() |
tmav123 | 0:9df942ea84f4 | 75 | { |
tmav123 | 0:9df942ea84f4 | 76 | led3 = 0; |
tmav123 | 0:9df942ea84f4 | 77 | led3timeout.detach(); |
tmav123 | 0:9df942ea84f4 | 78 | } |
tmav123 | 0:9df942ea84f4 | 79 | /////////////////////////////////////////////////// |
tmav123 | 0:9df942ea84f4 | 80 | void pcRXtimeout_func() // function for transmiting the RF packets - empty the pcRX ring buffer |
tmav123 | 0:9df942ea84f4 | 81 | { |
tmav123 | 0:9df942ea84f4 | 82 | unsigned char txlength; |
hillkim7 | 1:e96096a7b90b | 83 | |
tmav123 | 0:9df942ea84f4 | 84 | txlength = 0; |
hillkim7 | 1:e96096a7b90b | 85 | while(pcRX.use() > 0) { |
hillkim7 | 1:e96096a7b90b | 86 | led2 = 1; |
hillkim7 | 1:e96096a7b90b | 87 | buffer[txlength] = pcRX.getc(); |
hillkim7 | 1:e96096a7b90b | 88 | txlength++; |
hillkim7 | 1:e96096a7b90b | 89 | led2timeout.attach(&led2timeout_func, 0.050); // for switch off the led |
tmav123 | 0:9df942ea84f4 | 90 | } |
hillkim7 | 1:e96096a7b90b | 91 | if (txlength) { |
hillkim7 | 1:e96096a7b90b | 92 | cc1101.SendPacket(buffer, txlength); // tx packet |
hillkim7 | 1:e96096a7b90b | 93 | } |
hillkim7 | 1:e96096a7b90b | 94 | |
tmav123 | 0:9df942ea84f4 | 95 | pcRXtimeout.detach(); |
tmav123 | 0:9df942ea84f4 | 96 | } |
tmav123 | 0:9df942ea84f4 | 97 | /////////////////////////////////////////////////// |
hillkim7 | 1:e96096a7b90b | 98 | void check_FIFO() // check the status of the CC1101 to see FIFO error |
hillkim7 | 1:e96096a7b90b | 99 | { |
tmav123 | 0:9df942ea84f4 | 100 | unsigned char chip_status_rx, chip_status_tx; |
hillkim7 | 1:e96096a7b90b | 101 | |
tmav123 | 0:9df942ea84f4 | 102 | led1 = !led1; |
hillkim7 | 1:e96096a7b90b | 103 | chip_status_rx = cc1101.ReadChipStatusRX(); // check the rx status |
hillkim7 | 1:e96096a7b90b | 104 | if ((chip_status_rx & CHIP_STATE_MASK) == CHIP_STATE_RXFIFO_OVERFLOW) { // if rx overflow flush the rx fifo |
hillkim7 | 1:e96096a7b90b | 105 | PRINTF("*RXFIFO_OVERFLOW->Flush\r\n"); |
hillkim7 | 1:e96096a7b90b | 106 | cc1101.FlushRX(); |
hillkim7 | 1:e96096a7b90b | 107 | } |
hillkim7 | 1:e96096a7b90b | 108 | if ((chip_status_rx & CHIP_STATE_MASK) == CHIP_STATE_IDLE) { // if state is idle go to rx state again |
hillkim7 | 1:e96096a7b90b | 109 | // Basically we don't need to set RXMode here because RXOFF_MODE and TXOFF_MODE of CC1101 are RX mode. |
hillkim7 | 1:e96096a7b90b | 110 | // But we still need to check IDLE because it can be IDEL by flushing FIFO. |
hillkim7 | 1:e96096a7b90b | 111 | PRINTF("*RX MODE\r\n"); |
hillkim7 | 1:e96096a7b90b | 112 | cc1101.RXMode(); |
hillkim7 | 1:e96096a7b90b | 113 | } |
hillkim7 | 1:e96096a7b90b | 114 | chip_status_tx = cc1101.ReadChipStatusTX(); // check the tx status |
hillkim7 | 1:e96096a7b90b | 115 | if ((chip_status_tx & CHIP_STATE_MASK) == CHIP_STATE_TXFIFO_UNDERFLOW) { // if tx underflow flush the tx fifo |
hillkim7 | 1:e96096a7b90b | 116 | PRINTF("*XFIFO_UNDERFLOW->Flush\r\n"); |
hillkim7 | 1:e96096a7b90b | 117 | cc1101.FlushTX(); |
hillkim7 | 1:e96096a7b90b | 118 | } |
hillkim7 | 1:e96096a7b90b | 119 | PRINTF("[%8u]Chip RX=%02x TX=%02x\r\n", us_ticker_read()/1000, chip_status_rx, chip_status_tx); |
tmav123 | 0:9df942ea84f4 | 120 | } |
tmav123 | 0:9df942ea84f4 | 121 | /////////////////////////////////////////////////// |
hillkim7 | 1:e96096a7b90b | 122 | int main() |
tmav123 | 0:9df942ea84f4 | 123 | { |
tmav123 | 0:9df942ea84f4 | 124 | unsigned char rxlength, i; |
hillkim7 | 1:e96096a7b90b | 125 | uint32_t saved_tick = 0, cur_tick; |
hillkim7 | 1:e96096a7b90b | 126 | |
hillkim7 | 1:e96096a7b90b | 127 | pc.baud(115200); |
hillkim7 | 1:e96096a7b90b | 128 | printf("build at " __TIME__ "\r\n"); |
tmav123 | 0:9df942ea84f4 | 129 | pcRX.clear(); |
tmav123 | 0:9df942ea84f4 | 130 | pcTX.clear(); |
tmav123 | 0:9df942ea84f4 | 131 | cc1101.init(); |
hillkim7 | 1:e96096a7b90b | 132 | while(1) { |
hillkim7 | 1:e96096a7b90b | 133 | cur_tick = us_ticker_read(); |
hillkim7 | 1:e96096a7b90b | 134 | if (compute_elapse_us(saved_tick, cur_tick) > FIFO_CHECK_INTERVAL_US) { |
hillkim7 | 1:e96096a7b90b | 135 | check_FIFO(); |
hillkim7 | 1:e96096a7b90b | 136 | saved_tick = cur_tick; |
hillkim7 | 1:e96096a7b90b | 137 | } |
hillkim7 | 1:e96096a7b90b | 138 | |
hillkim7 | 1:e96096a7b90b | 139 | if(cc1101.GetGDO0()) { // rx finished and CRC OK read the new packet |
tmav123 | 0:9df942ea84f4 | 140 | rxlength = sizeof(buffer); |
hillkim7 | 1:e96096a7b90b | 141 | if (cc1101.ReceivePacket(buffer, &rxlength) == 1) { // read the rx packet |
tmav123 | 0:9df942ea84f4 | 142 | led3 = 1; |
tmav123 | 0:9df942ea84f4 | 143 | for (i = 0; i < rxlength; i++) |
hillkim7 | 1:e96096a7b90b | 144 | pcTX.putc(buffer[i]); // store the packet to the pcTX ring buffer |
tmav123 | 0:9df942ea84f4 | 145 | led3timeout.attach(&led3timeout_func, 0.050); // for switch off the led |
tmav123 | 0:9df942ea84f4 | 146 | } |
tmav123 | 0:9df942ea84f4 | 147 | } |
tmav123 | 0:9df942ea84f4 | 148 | if (pcTX.use() > 0) // check if we have data to transmit to pc |
tmav123 | 0:9df942ea84f4 | 149 | pc.putc(pcTX.getc()); // get the data from the ring buffer and transmit it to the pc |
hillkim7 | 1:e96096a7b90b | 150 | if (pc.readable()) { // check if we received new data from the pc |
tmav123 | 0:9df942ea84f4 | 151 | 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 |
hillkim7 | 1:e96096a7b90b | 152 | if (pcRX.use() > 20) // if more than 20 bytes received then tx the packet in RF |
hillkim7 | 1:e96096a7b90b | 153 | pcRXtimeout_func(); |
hillkim7 | 1:e96096a7b90b | 154 | else |
hillkim7 | 1:e96096a7b90b | 155 | pcRXtimeout.attach(&pcRXtimeout_func, 0.020); |
tmav123 | 0:9df942ea84f4 | 156 | } |
hillkim7 | 1:e96096a7b90b | 157 | } |
tmav123 | 0:9df942ea84f4 | 158 | } |
tmav123 | 0:9df942ea84f4 | 159 | /////////////////////////////////////////////////// |
hillkim7 | 1:e96096a7b90b | 160 |