mbed_controller / Mbed 2 deprecated CC1101_Transceiver_STM32F4

Dependencies:   mbed

Committer:
hillkim7
Date:
Tue Mar 31 07:48:52 2015 +0000
Revision:
1:e96096a7b90b
Parent:
0:9df942ea84f4
Child:
2:54c832c68208
CC1101 Test with STM32-F407 platform

Who changed what in which revision?

UserRevisionLine numberNew 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 1:e96096a7b90b 6 * For subsequent packet transmission, now this function returns after packet transmission.
hillkim7 1:e96096a7b90b 7 * - FIFO checking routine changed.
hillkim7 1:e96096a7b90b 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 1:e96096a7b90b 35 // It related with CC1101 IOCFG01register.
hillkim7 1:e96096a7b90b 36 const PinName gdo1 = PE_1;
hillkim7 1:e96096a7b90b 37
hillkim7 1:e96096a7b90b 38 CC1101 cc1101(mosi, miso, clk, csn, RDmiso, gdo0, gdo1);
hillkim7 1:e96096a7b90b 39
hillkim7 1:e96096a7b90b 40 Serial pc(USBTX, USBRX);
hillkim7 1:e96096a7b90b 41 DigitalOut led1(LED1); // FIFO led
tmav123 0:9df942ea84f4 42 DigitalOut led2(LED2); // RX led
tmav123 0:9df942ea84f4 43 DigitalOut led3(LED3); // TX led
hillkim7 1:e96096a7b90b 44
tmav123 0:9df942ea84f4 45 RingBuffer pcRX(512); // ring buffer for the pc RX data
tmav123 0:9df942ea84f4 46 RingBuffer pcTX(512); // ring buffer for the pc TX data
tmav123 0:9df942ea84f4 47 Timeout pcRXtimeout;
tmav123 0:9df942ea84f4 48 Timeout led2timeout;
tmav123 0:9df942ea84f4 49 Timeout led3timeout;
hillkim7 1:e96096a7b90b 50 unsigned char buffer[64];
hillkim7 1:e96096a7b90b 51
hillkim7 1:e96096a7b90b 52 ///////////////////////////////////////////////////
hillkim7 1:e96096a7b90b 53 static uint32_t compute_elapse_us(uint32_t prev_tick, uint32_t cur_tick)
hillkim7 1:e96096a7b90b 54 {
hillkim7 1:e96096a7b90b 55 if (prev_tick <= cur_tick)
hillkim7 1:e96096a7b90b 56 return cur_tick - prev_tick;
hillkim7 1:e96096a7b90b 57
hillkim7 1:e96096a7b90b 58 // tick wrap around happens, it assumes tick start from 0.
hillkim7 1:e96096a7b90b 59 return cur_tick;
hillkim7 1:e96096a7b90b 60 }
tmav123 0:9df942ea84f4 61 ///////////////////////////////////////////////////
tmav123 0:9df942ea84f4 62 void led2timeout_func()
tmav123 0:9df942ea84f4 63 {
tmav123 0:9df942ea84f4 64 led2 = 0;
tmav123 0:9df942ea84f4 65 led2timeout.detach();
tmav123 0:9df942ea84f4 66 }
tmav123 0:9df942ea84f4 67 ///////////////////////////////////////////////////
tmav123 0:9df942ea84f4 68 void led3timeout_func()
tmav123 0:9df942ea84f4 69 {
tmav123 0:9df942ea84f4 70 led3 = 0;
tmav123 0:9df942ea84f4 71 led3timeout.detach();
tmav123 0:9df942ea84f4 72 }
tmav123 0:9df942ea84f4 73 ///////////////////////////////////////////////////
tmav123 0:9df942ea84f4 74 void pcRXtimeout_func() // function for transmiting the RF packets - empty the pcRX ring buffer
tmav123 0:9df942ea84f4 75 {
tmav123 0:9df942ea84f4 76 unsigned char txlength;
hillkim7 1:e96096a7b90b 77
tmav123 0:9df942ea84f4 78 txlength = 0;
hillkim7 1:e96096a7b90b 79 while(pcRX.use() > 0) {
hillkim7 1:e96096a7b90b 80 led2 = 1;
hillkim7 1:e96096a7b90b 81 buffer[txlength] = pcRX.getc();
hillkim7 1:e96096a7b90b 82 txlength++;
hillkim7 1:e96096a7b90b 83 led2timeout.attach(&led2timeout_func, 0.050); // for switch off the led
tmav123 0:9df942ea84f4 84 }
hillkim7 1:e96096a7b90b 85 if (txlength) {
hillkim7 1:e96096a7b90b 86 cc1101.SendPacket(buffer, txlength); // tx packet
hillkim7 1:e96096a7b90b 87 }
hillkim7 1:e96096a7b90b 88
tmav123 0:9df942ea84f4 89 pcRXtimeout.detach();
tmav123 0:9df942ea84f4 90 }
tmav123 0:9df942ea84f4 91 ///////////////////////////////////////////////////
hillkim7 1:e96096a7b90b 92 void check_FIFO() // check the status of the CC1101 to see FIFO error
hillkim7 1:e96096a7b90b 93 {
tmav123 0:9df942ea84f4 94 unsigned char chip_status_rx, chip_status_tx;
hillkim7 1:e96096a7b90b 95
tmav123 0:9df942ea84f4 96 led1 = !led1;
hillkim7 1:e96096a7b90b 97 chip_status_rx = cc1101.ReadChipStatusRX(); // check the rx status
hillkim7 1:e96096a7b90b 98 if ((chip_status_rx & CHIP_STATE_MASK) == CHIP_STATE_RXFIFO_OVERFLOW) { // if rx overflow flush the rx fifo
hillkim7 1:e96096a7b90b 99 PRINTF("*RXFIFO_OVERFLOW->Flush\r\n");
hillkim7 1:e96096a7b90b 100 cc1101.FlushRX();
hillkim7 1:e96096a7b90b 101 }
hillkim7 1:e96096a7b90b 102 if ((chip_status_rx & CHIP_STATE_MASK) == CHIP_STATE_IDLE) { // if state is idle go to rx state again
hillkim7 1:e96096a7b90b 103 // Basically we don't need to set RXMode here because RXOFF_MODE and TXOFF_MODE of CC1101 are RX mode.
hillkim7 1:e96096a7b90b 104 // But we still need to check IDLE because it can be IDEL by flushing FIFO.
hillkim7 1:e96096a7b90b 105 PRINTF("*RX MODE\r\n");
hillkim7 1:e96096a7b90b 106 cc1101.RXMode();
hillkim7 1:e96096a7b90b 107 }
hillkim7 1:e96096a7b90b 108 chip_status_tx = cc1101.ReadChipStatusTX(); // check the tx status
hillkim7 1:e96096a7b90b 109 if ((chip_status_tx & CHIP_STATE_MASK) == CHIP_STATE_TXFIFO_UNDERFLOW) { // if tx underflow flush the tx fifo
hillkim7 1:e96096a7b90b 110 PRINTF("*XFIFO_UNDERFLOW->Flush\r\n");
hillkim7 1:e96096a7b90b 111 cc1101.FlushTX();
hillkim7 1:e96096a7b90b 112 }
hillkim7 1:e96096a7b90b 113 PRINTF("[%8u]Chip RX=%02x TX=%02x\r\n", us_ticker_read()/1000, chip_status_rx, chip_status_tx);
tmav123 0:9df942ea84f4 114 }
tmav123 0:9df942ea84f4 115 ///////////////////////////////////////////////////
hillkim7 1:e96096a7b90b 116 int main()
tmav123 0:9df942ea84f4 117 {
tmav123 0:9df942ea84f4 118 unsigned char rxlength, i;
hillkim7 1:e96096a7b90b 119 uint32_t saved_tick = 0, cur_tick;
hillkim7 1:e96096a7b90b 120
hillkim7 1:e96096a7b90b 121 pc.baud(115200);
hillkim7 1:e96096a7b90b 122 printf("build at " __TIME__ "\r\n");
tmav123 0:9df942ea84f4 123 pcRX.clear();
tmav123 0:9df942ea84f4 124 pcTX.clear();
tmav123 0:9df942ea84f4 125 cc1101.init();
hillkim7 1:e96096a7b90b 126 while(1) {
hillkim7 1:e96096a7b90b 127 cur_tick = us_ticker_read();
hillkim7 1:e96096a7b90b 128 if (compute_elapse_us(saved_tick, cur_tick) > FIFO_CHECK_INTERVAL_US) {
hillkim7 1:e96096a7b90b 129 check_FIFO();
hillkim7 1:e96096a7b90b 130 saved_tick = cur_tick;
hillkim7 1:e96096a7b90b 131 }
hillkim7 1:e96096a7b90b 132
hillkim7 1:e96096a7b90b 133 if(cc1101.GetGDO0()) { // rx finished and CRC OK read the new packet
tmav123 0:9df942ea84f4 134 rxlength = sizeof(buffer);
hillkim7 1:e96096a7b90b 135 if (cc1101.ReceivePacket(buffer, &rxlength) == 1) { // read the rx packet
tmav123 0:9df942ea84f4 136 led3 = 1;
tmav123 0:9df942ea84f4 137 for (i = 0; i < rxlength; i++)
hillkim7 1:e96096a7b90b 138 pcTX.putc(buffer[i]); // store the packet to the pcTX ring buffer
tmav123 0:9df942ea84f4 139 led3timeout.attach(&led3timeout_func, 0.050); // for switch off the led
tmav123 0:9df942ea84f4 140 }
tmav123 0:9df942ea84f4 141 }
tmav123 0:9df942ea84f4 142 if (pcTX.use() > 0) // check if we have data to transmit to pc
tmav123 0:9df942ea84f4 143 pc.putc(pcTX.getc()); // get the data from the ring buffer and transmit it to the pc
hillkim7 1:e96096a7b90b 144 if (pc.readable()) { // check if we received new data from the pc
tmav123 0:9df942ea84f4 145 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 146 if (pcRX.use() > 20) // if more than 20 bytes received then tx the packet in RF
hillkim7 1:e96096a7b90b 147 pcRXtimeout_func();
hillkim7 1:e96096a7b90b 148 else
hillkim7 1:e96096a7b90b 149 pcRXtimeout.attach(&pcRXtimeout_func, 0.020);
tmav123 0:9df942ea84f4 150 }
hillkim7 1:e96096a7b90b 151 }
tmav123 0:9df942ea84f4 152 }
tmav123 0:9df942ea84f4 153 ///////////////////////////////////////////////////
hillkim7 1:e96096a7b90b 154