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

Committer:
allankliu
Date:
Thu Aug 24 10:37:31 2017 +0000
Revision:
2:0e79d58be0f6
Parent:
0:9df942ea84f4
Init version, integrated CC1101_Transceiver with STM32F103RB/LPC824, publish to public for further debugging. Current SPI access 0x30 to 0x3D registers are not stable.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
allankliu 2:0e79d58be0f6 1 /*
allankliu 2:0e79d58be0f6 2 Author: Allan K Liu
allankliu 2:0e79d58be0f6 3 Original: Athanassios Mavrogeorgiadis
allankliu 2:0e79d58be0f6 4
allankliu 2:0e79d58be0f6 5 Changes:
allankliu 2:0e79d58be0f6 6 - Optimization for Low Power Consumption Operations
allankliu 2:0e79d58be0f6 7 - GD0 as external interrupt, to bring MCU from deep sleep mode.
allankliu 2:0e79d58be0f6 8 - GPIO setup
allankliu 2:0e79d58be0f6 9 - Clock setup
allankliu 2:0e79d58be0f6 10 - Event driven design
allankliu 2:0e79d58be0f6 11 - ALOHA transceiver for channel access with RTS/CTS/ACK
allankliu 2:0e79d58be0f6 12 - S-MAC
allankliu 2:0e79d58be0f6 13 - Cloned high level designs from SimpliciTI.
allankliu 2:0e79d58be0f6 14 - New CircularBuffer from mbed 5
allankliu 2:0e79d58be0f6 15
allankliu 2:0e79d58be0f6 16 Status:
allankliu 2:0e79d58be0f6 17 - CC1101 SPI access to regsiters from PARTNUM/VERSION to RCCTRL0 not stable
allankliu 2:0e79d58be0f6 18 - PARTNUM ranges from 0x00/0xFF
allankliu 2:0e79d58be0f6 19 - VERSION ranges from 0x0F/0x1F/0x4F
allankliu 2:0e79d58be0f6 20 */
tmav123 0:9df942ea84f4 21 #include "mbed.h"
tmav123 0:9df942ea84f4 22 #include "CC1101.h"
tmav123 0:9df942ea84f4 23 #include "RingBuffer.h"
tmav123 0:9df942ea84f4 24
allankliu 2:0e79d58be0f6 25
allankliu 2:0e79d58be0f6 26 #if defined(TARGET_NUCLEO_F103RB) || defined(TARGET_LPC824)
allankliu 2:0e79d58be0f6 27 // RDmiso is actually the MISO,
allankliu 2:0e79d58be0f6 28 // SPI only works after MISO turns into low from Hi-Z state of CC1101
allankliu 2:0e79d58be0f6 29 // Therefore we need a dedicated input to detect.
allankliu 2:0e79d58be0f6 30 CC1101 cc1101(D11, D12, D13, D10, D8); // MOSI, MISO, SCK, nCS, RDmiso
allankliu 2:0e79d58be0f6 31 DigitalIn gdo0(D7); // InterruptIn is better than DigitalIn
allankliu 2:0e79d58be0f6 32 InterruptIn gdo2(D9); // InterruptIn is better than DigitalIn
allankliu 2:0e79d58be0f6 33
allankliu 2:0e79d58be0f6 34 DigitalOut led1(A0); // timer blink led
allankliu 2:0e79d58be0f6 35 DigitalOut led2(A1); // RX led
allankliu 2:0e79d58be0f6 36 DigitalOut led3(A2); // TX led
allankliu 2:0e79d58be0f6 37
allankliu 2:0e79d58be0f6 38 #elif defined(TARGET_LPC1114)
allankliu 2:0e79d58be0f6 39 // Legacy platform, to be updated
tmav123 0:9df942ea84f4 40 CC1101 cc1101(p5, p6, p7, p8, p10);
tmav123 0:9df942ea84f4 41 DigitalIn gdo0(p9); // pin connected to gdo0 pin of CC1101 for checking that received a new packet
tmav123 0:9df942ea84f4 42
tmav123 0:9df942ea84f4 43 DigitalOut led1(LED1); // timer blink led
tmav123 0:9df942ea84f4 44 DigitalOut led2(LED2); // RX led
tmav123 0:9df942ea84f4 45 DigitalOut led3(LED3); // TX led
allankliu 2:0e79d58be0f6 46
allankliu 2:0e79d58be0f6 47 #else
allankliu 2:0e79d58be0f6 48
allankliu 2:0e79d58be0f6 49 #warning "YOU HAVE TO DEFINE A H/W."
allankliu 2:0e79d58be0f6 50 #endif
allankliu 2:0e79d58be0f6 51
allankliu 2:0e79d58be0f6 52
allankliu 2:0e79d58be0f6 53 Ticker timer;
allankliu 2:0e79d58be0f6 54
allankliu 2:0e79d58be0f6 55
allankliu 2:0e79d58be0f6 56 Serial pc(USBTX, USBRX); // tx, rx, to be replaced by USB CDC interface in a hub/sink node.
allankliu 2:0e79d58be0f6 57 RingBuffer rbRX(512); // ring buffer for the pc RX data
allankliu 2:0e79d58be0f6 58 RingBuffer rbTX(512); // ring buffer for the pc TX data
allankliu 2:0e79d58be0f6 59
allankliu 2:0e79d58be0f6 60 Timeout rbRXtimeout;
tmav123 0:9df942ea84f4 61 Timeout led2timeout;
tmav123 0:9df942ea84f4 62 Timeout led3timeout;
allankliu 2:0e79d58be0f6 63
tmav123 0:9df942ea84f4 64 unsigned char buffer[128];
allankliu 2:0e79d58be0f6 65
allankliu 2:0e79d58be0f6 66 static unsigned char sil = 0;
allankliu 2:0e79d58be0f6 67 static unsigned char ver = 0;
allankliu 2:0e79d58be0f6 68 static unsigned char sta = 0;
allankliu 2:0e79d58be0f6 69 static unsigned char val = 0;
allankliu 2:0e79d58be0f6 70
tmav123 0:9df942ea84f4 71 void led2timeout_func()
tmav123 0:9df942ea84f4 72 {
tmav123 0:9df942ea84f4 73 led2 = 0;
tmav123 0:9df942ea84f4 74 led2timeout.detach();
tmav123 0:9df942ea84f4 75 }
allankliu 2:0e79d58be0f6 76
tmav123 0:9df942ea84f4 77 void led3timeout_func()
tmav123 0:9df942ea84f4 78 {
tmav123 0:9df942ea84f4 79 led3 = 0;
tmav123 0:9df942ea84f4 80 led3timeout.detach();
tmav123 0:9df942ea84f4 81 }
allankliu 2:0e79d58be0f6 82
allankliu 2:0e79d58be0f6 83 void rbRXtimeout_func() // function for transmiting the RF packets - empty the rbRX ring buffer
tmav123 0:9df942ea84f4 84 {
tmav123 0:9df942ea84f4 85 unsigned char txlength;
tmav123 0:9df942ea84f4 86
tmav123 0:9df942ea84f4 87 txlength = 0;
allankliu 2:0e79d58be0f6 88 while(rbRX.use() > 0)
tmav123 0:9df942ea84f4 89 {
tmav123 0:9df942ea84f4 90 led2 = 1;
allankliu 2:0e79d58be0f6 91 buffer[txlength] = rbRX.getc();
tmav123 0:9df942ea84f4 92 txlength++;
tmav123 0:9df942ea84f4 93 led2timeout.attach(&led2timeout_func, 0.050); // for switch off the led
tmav123 0:9df942ea84f4 94 }
tmav123 0:9df942ea84f4 95 if (txlength)
tmav123 0:9df942ea84f4 96 cc1101.SendPacket(buffer, txlength); // tx packet
tmav123 0:9df942ea84f4 97
allankliu 2:0e79d58be0f6 98 rbRXtimeout.detach();
tmav123 0:9df942ea84f4 99 }
allankliu 2:0e79d58be0f6 100
tmav123 0:9df942ea84f4 101 void timer_func() // check the status of the CC1101 every 100ms
tmav123 0:9df942ea84f4 102 {
tmav123 0:9df942ea84f4 103 unsigned char chip_status_rx, chip_status_tx;
tmav123 0:9df942ea84f4 104
tmav123 0:9df942ea84f4 105 led1 = !led1;
tmav123 0:9df942ea84f4 106 chip_status_rx = cc1101.ReadChipStatusRX(); // check the rx status
tmav123 0:9df942ea84f4 107 if ((chip_status_rx & CHIP_STATE_MASK) == CHIP_STATE_RXFIFO_OVERFLOW) // if rx overflow flush the rx fifo
tmav123 0:9df942ea84f4 108 cc1101.FlushRX();
tmav123 0:9df942ea84f4 109 if ((chip_status_rx & CHIP_STATE_MASK) == CHIP_STATE_IDLE) // if state is idle go to rx state again
tmav123 0:9df942ea84f4 110 cc1101.RXMode();
tmav123 0:9df942ea84f4 111 chip_status_tx = cc1101.ReadChipStatusTX(); // check the tx sttus
tmav123 0:9df942ea84f4 112 if ((chip_status_tx & CHIP_STATE_MASK) == CHIP_STATE_TXFIFO_UNDERFLOW) // if tx underflow flush the tx fifo
tmav123 0:9df942ea84f4 113 cc1101.FlushTX();
tmav123 0:9df942ea84f4 114 }
allankliu 2:0e79d58be0f6 115
tmav123 0:9df942ea84f4 116 int main()
tmav123 0:9df942ea84f4 117 {
tmav123 0:9df942ea84f4 118 unsigned char rxlength, i;
allankliu 2:0e79d58be0f6 119 unsigned char buf[128];
tmav123 0:9df942ea84f4 120
allankliu 2:0e79d58be0f6 121 rbRX.clear();
allankliu 2:0e79d58be0f6 122 rbTX.clear();
tmav123 0:9df942ea84f4 123 cc1101.init();
allankliu 2:0e79d58be0f6 124
allankliu 2:0e79d58be0f6 125 // test routines
allankliu 2:0e79d58be0f6 126
allankliu 2:0e79d58be0f6 127 #define REG_PART_DBG 1
allankliu 2:0e79d58be0f6 128 #if defined(REG_PART_DBG)
allankliu 2:0e79d58be0f6 129
allankliu 2:0e79d58be0f6 130 for (int i=0; i<10; i++){
allankliu 2:0e79d58be0f6 131 buf[i] = cc1101.ReadChipStatusRX();
allankliu 2:0e79d58be0f6 132 }
allankliu 2:0e79d58be0f6 133 cc1101.ReadBurstReg(CCxxx0_PARTNUM, buf, CCxxx0_PATABLE-CCxxx0_PARTNUM+1);
allankliu 2:0e79d58be0f6 134
allankliu 2:0e79d58be0f6 135 // Read them one by one
allankliu 2:0e79d58be0f6 136 for (int i=CCxxx0_PARTNUM; i<CCxxx0_PATABLE; i++){
allankliu 2:0e79d58be0f6 137 buf[i] = cc1101.ReadReg(i);
allankliu 2:0e79d58be0f6 138 }
allankliu 2:0e79d58be0f6 139
allankliu 2:0e79d58be0f6 140 // Try to read back all of values from registers ranges from 0x30~0x3D
allankliu 2:0e79d58be0f6 141 // Read them one by one
allankliu 2:0e79d58be0f6 142 sil = cc1101.ReadReg(CCxxx0_PARTNUM);
allankliu 2:0e79d58be0f6 143 ver = cc1101.ReadReg(CCxxx0_VERSION);
allankliu 2:0e79d58be0f6 144 sta = cc1101.ReadChipStatusRX();
allankliu 2:0e79d58be0f6 145
allankliu 2:0e79d58be0f6 146 for (int i=0; i<10; i++){
allankliu 2:0e79d58be0f6 147 buf[i] = cc1101.ReadReg(CCxxx0_PARTNUM);
allankliu 2:0e79d58be0f6 148 }
allankliu 2:0e79d58be0f6 149
allankliu 2:0e79d58be0f6 150 for (int i=0; i<10; i++){
allankliu 2:0e79d58be0f6 151 buf[i] = cc1101.ReadReg(CCxxx0_VERSION);
allankliu 2:0e79d58be0f6 152 }
allankliu 2:0e79d58be0f6 153
allankliu 2:0e79d58be0f6 154 for (int i=0; i<10; i++){
allankliu 2:0e79d58be0f6 155 buf[i] = cc1101.ReadChipStatusRX();
allankliu 2:0e79d58be0f6 156 }
allankliu 2:0e79d58be0f6 157
allankliu 2:0e79d58be0f6 158 // Read them in a burst reading
allankliu 2:0e79d58be0f6 159 cc1101.ReadBurstReg(CCxxx0_PARTNUM, buf, CCxxx0_PATABLE-CCxxx0_PARTNUM+1);
allankliu 2:0e79d58be0f6 160 // Read them in a burst reading twice
allankliu 2:0e79d58be0f6 161 cc1101.ReadBurstReg(CCxxx0_PARTNUM, buf, CCxxx0_PATABLE-CCxxx0_PARTNUM+1);
allankliu 2:0e79d58be0f6 162 // Read them one by one
allankliu 2:0e79d58be0f6 163 for (int i=CCxxx0_PARTNUM; i<CCxxx0_PATABLE; i++){
allankliu 2:0e79d58be0f6 164 buf[i] = cc1101.ReadReg(i);
allankliu 2:0e79d58be0f6 165 }
allankliu 2:0e79d58be0f6 166 // Read them in a burst reading
allankliu 2:0e79d58be0f6 167 cc1101.ReadBurstReg(CCxxx0_PARTNUM, buf, CCxxx0_PATABLE-CCxxx0_PARTNUM+1);
allankliu 2:0e79d58be0f6 168
allankliu 2:0e79d58be0f6 169 #endif
allankliu 2:0e79d58be0f6 170
allankliu 2:0e79d58be0f6 171 //#define REG_RW_DBG 1
allankliu 2:0e79d58be0f6 172 #if defined(REG_RW_DBG)
allankliu 2:0e79d58be0f6 173 // Try to read back all of values from registers ranges from 0x00~0x2E
allankliu 2:0e79d58be0f6 174 cc1101.ReadBurstReg(CCxxx0_IOCFG2, buf, CCxxx0_TEST0+1);
allankliu 2:0e79d58be0f6 175
allankliu 2:0e79d58be0f6 176 for (int i=CCxxx0_IOCFG2; i<(CCxxx0_TEST0+1); i++){
allankliu 2:0e79d58be0f6 177 buf[i] = cc1101.ReadReg(i);
allankliu 2:0e79d58be0f6 178 }
allankliu 2:0e79d58be0f6 179
allankliu 2:0e79d58be0f6 180 const unsigned char params[0x2F] = \
allankliu 2:0e79d58be0f6 181 {0x06,0x2E,0x07,0x07,0xD3,0x91,0xFF,0x04, \
allankliu 2:0e79d58be0f6 182 0x05,0x00,0x00,0x06,0x00,0x10,0xB1,0x3B, \
allankliu 2:0e79d58be0f6 183 0xF8,0x83,0x13,0x22,0xF8,0x15,0x07,0x3F, \
allankliu 2:0e79d58be0f6 184 0x18,0x16,0x6C,0x03,0x40,0x91,0x87,0x6B, \
allankliu 2:0e79d58be0f6 185 0xF8,0x56,0x10,0xE9,0x2A,0x00,0x1F,0x41, \
allankliu 2:0e79d58be0f6 186 0x00,0x59,0x7F,0x63,0x88,0x31,0x09};
allankliu 2:0e79d58be0f6 187
allankliu 2:0e79d58be0f6 188 for (int i=CCxxx0_IOCFG2; i<(CCxxx0_TEST0+1); i++){
allankliu 2:0e79d58be0f6 189 cc1101.WriteReg(i, params[i]);
allankliu 2:0e79d58be0f6 190 }
allankliu 2:0e79d58be0f6 191
allankliu 2:0e79d58be0f6 192 for (int i=CCxxx0_IOCFG2; i<(CCxxx0_TEST0+1); i++){
allankliu 2:0e79d58be0f6 193 buf[i] = cc1101.ReadReg(i);
allankliu 2:0e79d58be0f6 194 }
allankliu 2:0e79d58be0f6 195
allankliu 2:0e79d58be0f6 196 cc1101.ReadBurstReg(CCxxx0_IOCFG2, buf, CCxxx0_TEST0+1);
allankliu 2:0e79d58be0f6 197
allankliu 2:0e79d58be0f6 198 #endif
allankliu 2:0e79d58be0f6 199
allankliu 2:0e79d58be0f6 200 // end of test
allankliu 2:0e79d58be0f6 201
tmav123 0:9df942ea84f4 202 timer.attach(&timer_func, 0.1);
tmav123 0:9df942ea84f4 203 while(1)
tmav123 0:9df942ea84f4 204 {
tmav123 0:9df942ea84f4 205 if(gdo0) // rx finished and CRC OK read the new packet
tmav123 0:9df942ea84f4 206 {
tmav123 0:9df942ea84f4 207 rxlength = sizeof(buffer);
tmav123 0:9df942ea84f4 208 if (cc1101.ReceivePacket(buffer, &rxlength) == 1) // read the rx packet
tmav123 0:9df942ea84f4 209 {
tmav123 0:9df942ea84f4 210 led3 = 1;
tmav123 0:9df942ea84f4 211 for (i = 0; i < rxlength; i++)
allankliu 2:0e79d58be0f6 212 rbTX.putc(buffer[i]); // store the packet to the rbTX ring buffer
tmav123 0:9df942ea84f4 213 led3timeout.attach(&led3timeout_func, 0.050); // for switch off the led
tmav123 0:9df942ea84f4 214 }
tmav123 0:9df942ea84f4 215 }
allankliu 2:0e79d58be0f6 216 if (rbTX.use() > 0) // check if we have data to transmit to pc
allankliu 2:0e79d58be0f6 217 pc.putc(rbTX.getc()); // get the data from the ring buffer and transmit it to the pc
tmav123 0:9df942ea84f4 218 if (pc.readable()) // check if we received new data from the pc
tmav123 0:9df942ea84f4 219 {
allankliu 2:0e79d58be0f6 220 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
allankliu 2:0e79d58be0f6 221 rbRXtimeout.attach(&rbRXtimeout_func, 0.020);
tmav123 0:9df942ea84f4 222 }
allankliu 2:0e79d58be0f6 223 if (rbRX.use() > 20) // if more than 20 bytes received then tx the packet in RF
allankliu 2:0e79d58be0f6 224 rbRXtimeout_func();
tmav123 0:9df942ea84f4 225 }
tmav123 0:9df942ea84f4 226 }