Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 6:4a3dbfbc30f1, committed 2015-01-15
- Comitter:
- jjones646
- Date:
- Thu Jan 15 07:15:33 2015 +0000
- Parent:
- 5:146523a0d1f4
- Commit message:
- socket interface confirmed working.
Changed in this revision
--- a/drivers/CC1101/CC1101.cpp Wed Jan 14 17:46:44 2015 +0000
+++ b/drivers/CC1101/CC1101.cpp Thu Jan 15 07:15:33 2015 +0000
@@ -11,12 +11,19 @@
CC1101::CC1101(PinName mosi, PinName miso, PinName sck, PinName cs, PinName int_pin) :
CommLink(mosi, miso, sck, cs, int_pin)
{
- // [X] - 1 - Setup the chip
- setup();
-
- // [X] - 2 - Call the base class method for beginning full class operation with threads
- // =================
- CommLink::ready();
+ // [X] - 1 - Setup the chip and only signal the base class if device is tested and confirmed to be electrically connected
+ if(powerUp()) {
+
+ LOG("CC1101 Failure");
+
+ } else {
+
+ // [X] - 2 - Call the base class method for beginning full class operation with threads
+ // =================
+ CommLink::ready();
+ LOG("CC1101 Ready!");
+
+ }
}
// Deconstructor
@@ -31,41 +38,21 @@
}
-void CC1101::setup(void)
+int32_t CC1101::powerUp(void)
{
- // [X] - 1 - Initialize the CC1101 radio transceiver for operation
- // =================
+
+#if CCXXX1_DEBUG_MODE > 0
+ EVENT("CC1101 RADIO TRANSCEIVER INITILIZATION");
+#endif
+
set_init_vars();
- assign_if_freq(CCXXX1_IF_FREQUENCY);
freq(CCXXX1_BASE_FREQUENCY);
+ interface_freq(CCXXX1_IF_FREQUENCY);
assign_channel_spacing(200000); // 200kHz
datarate(250000); // 250 kBaud
set_rf_settings();
init();
-
- // [] - 2 - Test the interrupt pin for proper operation
- // =================
-
- // [] - 3 - Check the CC1101's version register to ensure the chip is what we believe it to be
- // =================
-
-}
-
-
-int32_t CC1101::powerUp(void)
-{
-#if CCXXX1_DEBUG_MODE > 0
- std::printf("[CC1101 RADIO TRANSCEIVER INITILIZATION]\r\n");
-#endif
-
- // power_on_reset();
-
- // now send the assigned rfSettings struct to the CC1101 for full inililization
- // init();
-
- // scan();
-
- return 0;
+ return selfTest();
}
@@ -73,7 +60,7 @@
{
// [] - 1 - Perform a check to ensure the CC1101 can provide communication with a secondary base station link
// =================
- // Note: This does not necessarily mean the link must be reliable, this only needs to determine: `Can the perheripial provide communication?`
+ // Note: This does not necessarily mean the link must be reliable, this only needs to determine: `Can the perheripial provide communication with a 2nd initialized CC1101?`
// [] - 2 - Return true/false for indicating a connected communication link
// =================
@@ -82,16 +69,15 @@
}
-uint32_t CC1101::reset(void)
+void CC1101::reset(void)
{
// [X] - 1 - Perform a soft reset for the CC1101 transceiver
// =================
strobe(CCXXX1_SRES);
- return 0; // success
}
-uint32_t CC1101::selfTest(void)
+int32_t CC1101::selfTest(void)
{
// [X] - 1 - Get the chip's version number and fail if different from what was expected.
_chip_version = status(CCXXX1_VERSION);
@@ -99,8 +85,8 @@
if (_chip_version != CCXXX1_EXPECTED_VERSION_NUMBER) {
// send message over serial port
- std::printf(
- "[FATAL ERROR]\r\n"
+ WARNING(
+ "FATAL ERROR\r\n"
" Wrong version number returned from chip's 'VERSION' register (Addr: 0x%02X)\r\n"
"\r\n"
" Expected: 0x%02X\r\n"
@@ -111,8 +97,9 @@
" - Determine if chip is newer version & update firmware\r\n"
, CCXXX1_VERSION, CCXXX1_EXPECTED_VERSION_NUMBER, _chip_version);
- return 1;
- }
+ return -1; // negative numbers mean error occurred
+ }
+
return 0; // success
}
@@ -124,9 +111,6 @@
_channel = 1;
_address = 0x00;
- // frequency that radio links with another CC1101 over the air
- _base_freq = CCXXX1_BASE_FREQUENCY;
-
// turn off address packet filtering and assign 0 (broadcast address) to the address value
_pck_control.addr_check = ADDR_OFF;
@@ -148,7 +132,7 @@
// setup how the payload of the packet is transmitted - default to a fixed length of 61 bytes
_pck_control.length_type = PACKET_VARIABLE;
//_pck_control.length_type = PACKET_FIXED;
- //_pck_control.size = 61;
+ _pck_control.size = 61; // indicates max packet size when using variable packet types
// this is a preamble threshold for determining when a packet should be accepted
_pck_control.preamble_thresh = 2;
@@ -167,17 +151,14 @@
_modem.mod_type = MOD_GFSK;
_modem.sync_mode = SYNC_HIGH_ALLOW_TWO;
_modem.preamble_bytes = PREAM_FOUR;
-
- // the values assigned here are used for the frequency synthesizer control
- // assign_if_freq(CCXXX1_IF_FREQUENCY);
-
- // set all the configuration values into the rfSettings struct
- // set_rf_settings();
}
void CC1101::put_rf_settings()
{
+#if DEBUG_MODE > 0
+ LOG("Writing configuration registers...");
+#endif
write_reg(CCXXX1_IOCFG2, rfSettings.IOCFG2);
write_reg(CCXXX1_IOCFG1, rfSettings.IOCFG1);
write_reg(CCXXX1_IOCFG0, rfSettings.IOCFG0);
@@ -188,17 +169,17 @@
write_reg(CCXXX1_PCKCTRL1, rfSettings.PCKCTRL1);
write_reg(CCXXX1_PCKCTRL0, rfSettings.PCKCTRL0);
write_reg(CCXXX1_ADDR, rfSettings.ADDR);
-
+
write_reg(CCXXX1_CHANNR, rfSettings.CHANNR);
write_reg(CCXXX1_FSCTRL1, rfSettings.FSCTRL1);
- write_reg(CCXXX1_FSCTRL0, rfSettings.FSCTRL0);
+// write_reg(CCXXX1_FSCTRL0, rfSettings.FSCTRL0);
write_reg(CCXXX1_FREQ2, rfSettings.FREQ2);
-
+
write_reg(CCXXX1_FREQ1, rfSettings.FREQ1);
write_reg(CCXXX1_FREQ0, rfSettings.FREQ0);
write_reg(CCXXX1_MDMCFG4, rfSettings.MDMCFG4);
write_reg(CCXXX1_MDMCFG3, rfSettings.MDMCFG3);
-
+
write_reg(CCXXX1_MDMCFG2, rfSettings.MDMCFG2);
write_reg(CCXXX1_MDMCFG1, rfSettings.MDMCFG1);
write_reg(CCXXX1_MDMCFG0, rfSettings.MDMCFG0);
@@ -228,19 +209,22 @@
//write_reg(CCXXX1_TEST2, rfSettings.TEST2);
//write_reg(CCXXX1_TEST1, rfSettings.TEST1);
//write_reg(CCXXX1_TEST0, rfSettings.TEST0);
-} // put_rf_settings
-
+#if DEBUG_MODE > 0
+ LOG("Configurations registers ready");
+#endif
+}
void CC1101::power_on_reset(void)
{
#if DEBUG_MODE > 0
- std::printf(" Starting Power-on-Reset procedure...");
+ LOG("Beginning Power-on-Reset routine...");
#endif
+
delete _spi;
// make sure chip is not selected
- toggle_cs();
+ *_cs = 1;
DigitalOut *SI = new DigitalOut(_mosi_pin);
DigitalOut *SCK = new DigitalOut(_sck_pin);
@@ -259,7 +243,6 @@
// pull CSn low & wait for the serial out line to go low
*_cs = 0;
-
while(*SO);
// cleanup everything before the mbed's SPI library calls take back over
@@ -284,62 +267,38 @@
setup_spi();
#if DEBUG_MODE > 0
- std::printf("==============done\r\n");
+ LOG("CC1101 Power-on-Reset complete");
#endif
}
// 2nd ighest level of initilization routines behind CC1101::setup();
void CC1101::init(void)
{
-
+
power_on_reset();
-
- // send all configuration values to the CC1101 registers
-#if DEBUG_MODE > 0
- std::printf(" Writing configuration registers...");
-#endif
+ // Send all configuration values to the CC1101 registers
put_rf_settings();
-#if DEBUG_MODE > 0
- std::printf("done\r\n");
-#endif
uint8_t paTable[] = {0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // 10dB
write_reg(CCXXX1_PATABLE, paTable[0]);
- // flush TX and RX buffers before beginning
-#if DEBUG_MODE > 0
- std::printf(" Clearing TX and RX buffers...");
-#endif
-
+ // Flush TX and RX buffers before beginning
flush_rx();
flush_tx();
-#if DEBUG_MODE > 0
- std::printf("done\r\n");
-#endif
-
-#if DEBUG_MODE > 0
- std::printf(" Configuring frequency offset estimate...");
-#endif
-
- //write_reg(CCXXX1_FSCTRL0, status(CCXXX1_FREQEST));
+ // Set the initial offset frequency estimate
-#if DEBUG_MODE > 0
- std::printf("done\r\n");
-#endif
+ LOG("Configuring frequency offset estimate...");
+ write_reg(CCXXX1_FSCTRL0, status(CCXXX1_FREQEST));
+ LOG("Frequency offset estimate configured");
- // Enter RX mode
-#if DEBUG_MODE > 0
- std::printf(" Entering RX mode...");
-#endif
+ calibrate();
+
rx_mode();
-#if DEBUG_MODE > 0
- std::printf("done\r\n");
-#endif
}
-// returns the current mode that the CC1101 is operating in
+// Returns the current mode that the CC1101 is operating in
uint8_t CC1101::mode(void)
{
return status(CCXXX1_MARCSTATE);
@@ -347,10 +306,10 @@
int32_t CC1101::sendData(uint8_t *buf, uint8_t size)
-{
+{
// [X] - 1 - Move all values down by 1 to make room for the packet's size value.
// =================
- for (uint8_t i = size; i > 0; i--)
+ for (int i = size; i > 0; i--)
buf[i] = buf[i-1];
@@ -360,7 +319,7 @@
#if CCXXX1_DEBUG_MODE > 0
- std::printf("\r\n[PACKET TRANSMITTED]\r\n Bytes: %u\r\n", size);
+ LOG("PACKET TRANSMITTED\r\n Bytes: %u", size);
#endif
@@ -391,11 +350,11 @@
// [X] - 5 - Wait until radio enters back to the RX state.
// =================
// *Note: Takes very few cycles, so might as well wait before returning to elimate querky errors.
- while(mode() != 0x0D);
+ //while(mode() != 0x0D);
#if CCXXX1_DEBUG_MODE > 1
t1.stop();
- std::printf(" Time: %02.4f ms\r\n", (t1.read() - ti)*1000);
+ LOG("Time: %02.4f ms", (t1.read() - ti)*1000);
#endif
// [] - 6 - Return any error codes if necessary.
@@ -444,9 +403,13 @@
_lqi = status_bytes[1] & CCXXX1_RXFIFO_MASK; // MSB of LQI is the CRC_OK bit - The interrupt is only triggered if CRC is OK, so no need to check again.
#if CCXXX1_DEBUG_MODE > 0
- std::printf("\r\n[PACKET RECEIVED]\r\n Bytes: %u\r\n", *length);
- std::printf(" RSSI: %ddBm\r\n", _rssi);
- std::printf(" LQI: %u\r\n", _lqi);
+ LOG(
+ "PACKET RECEIVED\r\n"
+ " Bytes: %u\r\n"
+ " RSSI: %ddBm\r\n"
+ " LQI: %u"
+ , *length, _rssi, _lqi
+ );
#endif
// [X] - 3.2c - Go back to the receiving state since CC1101 is configured for transitioning to IDLE on receiving a packet.
@@ -470,29 +433,26 @@
_spi->frequency(8000000);
toggle_cs();
_spi->write(addr | CCXXX1_READ_SINGLE);
- //tiny_delay();
uint8_t x = _spi->write(0);
toggle_cs();
#if CCXXX1_DEBUG_MODE > 1
- std::printf("\r\n== Single Register Read ==\r\n Address: 0x%02X\r\n Value: 0x%02X\r\n", addr, x);
+ LOG("== Single Register Read ==\r\n Address: 0x%02X\r\n Value: 0x%02X", addr, x);
#endif
return x;
} // read_reg
void CC1101::read_reg(uint8_t addr, uint8_t *buffer, uint8_t count)
{
- _spi->frequency(4500000);
+ _spi->frequency(5000000);
toggle_cs();
_spi->write(addr | CCXXX1_READ_BURST);
- //tiny_delay();
for (uint8_t i = 0; i < count; i++) {
buffer[i] = _spi->write(0);
- //tiny_delay();
}
toggle_cs();
#if CCXXX1_DEBUG_MODE > 1
- std::printf("\r\n== Burst Register Read ==\r\n Address: 0x%02X\r\n Bytes: %u\r\n", addr, count);
+ LOG("== Burst Register Read ==\r\n Address: 0x%02X\r\n Bytes: %u", addr, count);
#endif
} // read_reg
@@ -509,12 +469,12 @@
toggle_cs();
#if CCXXX1_DEBUG_MODE > 1
- std::printf("\r\n== Single Register Write ==\r\n Address: 0x%02X\r\n Value: 0x%02X\r\n", addr, value);
+ LOG("== Single Register Write ==\r\n Address: 0x%02X\r\n Value: 0x%02X", addr, value);
#endif
} // write_reg
void CC1101::write_reg(uint8_t addr, uint8_t *buffer, uint8_t count)
{
- _spi->frequency(4500000);
+ _spi->frequency(5000000);
toggle_cs();
_spi->write(addr | CCXXX1_WRITE_BURST);
for (uint8_t i = 0; i < count; i++) {
@@ -523,7 +483,7 @@
toggle_cs();
#if CCXXX1_DEBUG_MODE > 1
- std::printf("\r\n== Burst Register Write ==\r\n Address: 0x%02X\r\n Bytes: %u\r\n", addr, count);
+ LOG("== Burst Register Write ==\r\n Address: 0x%02X\r\n Bytes: %u", addr, count);
#endif
} // write_reg
@@ -531,7 +491,7 @@
// Strobe
uint8_t CC1101::strobe(uint8_t addr)
{
- _spi->frequency(4500000);
+ _spi->frequency(8000000);
toggle_cs();
uint8_t x = _spi->write(addr);
toggle_cs();
@@ -542,17 +502,27 @@
// Macro to calibrate the frequency synthesizer
void CC1101::calibrate(void)
{
+#if DEBUG_MODE > 0
+ LOG("Calibrating frequency synthesizer");
+#endif
+
+ idle();
+
// Send the calibration strobe
strobe(CCXXX1_SCAL);
// Wait for the radio to leave the calibration step
-// while( (mode() == 0x04) | (mode() == 0x05) );
+ while( (mode() == 0x04) | (mode() == 0x05) );
// The radio is now is IDLE mode, so go to RX mode
rx_mode();
// Wait for the radio to enter back into RX mode
- //while( mode() != 0x0D );
+ while( mode() != 0x0D );
+
+#if DEBUG_MODE > 0
+ LOG("Frequency synthesizer calibrated");
+#endif
}
@@ -584,13 +554,16 @@
void CC1101::channel(uint16_t chan)
{
if ( chan != _channel ) {
+#if DEBUG_MODE > 0
+ LOG("Updating channel from %02u to %02u", _channel, chan);
+#endif
+
_channel = chan;
write_reg(CCXXX1_CHANNR, _channel);
-/*
-#if CCXXX1_DEBUG_MODE > 0
- std::printf("\r\n[CHANNEL UPDATED]\r\n Channel: %02u\r\n Freq: %3.2f MHz\r\n", _channel, static_cast<float>(_final_freq)/1000000);
+
+#if DEBUG_MODE > 0
+ LOG("Channel updated: %02u", _channel);
#endif
-*/
}
}
@@ -614,6 +587,10 @@
void CC1101::flush_rx(void)
{
+#if DEBUG_MODE > 0
+ LOG("Clearing RX buffer...");
+#endif
+
// Make sure that the radio is in IDLE state before flushing the FIFO
idle();
@@ -622,10 +599,19 @@
// Enter back into a RX state
rx_mode();
+
+#if DEBUG_MODE > 0
+ LOG("RX buffer cleared");
+#endif
}
+
void CC1101::flush_tx(void)
{
+#if DEBUG_MODE > 0
+ LOG("Clearing TX buffer...");
+#endif
+
// Make sure that the radio is in IDLE state before flushing the FIFO
idle();
@@ -634,30 +620,58 @@
// Enter back into a RX state
rx_mode();
+
+#if DEBUG_MODE > 0
+ LOG("TX buffer cleared");
+#endif
}
void CC1101::rx_mode(void)
{
+#if DEBUG_MODE > 0
+ LOG("Sending RX_MODE strobe to CC1101");
+#endif
+
//strobe(CCXXX1_SIDLE);
strobe(CCXXX1_SRX);
//while(mode() != 0x0D);
+
+#if DEBUG_MODE > 0
+ LOG("RX_MODE strobe sent to CC1101");
+#endif
}
void CC1101::tx_mode(void)
{
+#if DEBUG_MODE > 0
+ LOG("Sending TX_MODE strobe to CC1101");
+#endif
+
//strobe(CCXXX1_SIDLE);
strobe(CCXXX1_STX);
// while(mode() != 0x13);
+
+#if DEBUG_MODE > 0
+ LOG("TX_MODE strobe sent to CC1101");
+#endif
}
void CC1101::idle(void)
{
+#if DEBUG_MODE > 0
+ LOG("Sending IDLE strobe to CC1101");
+#endif
+
// Send the IDLE strobe
strobe(CCXXX1_SIDLE);
// Wait before returning
//while( mode() != 0x01);
+
+#if DEBUG_MODE > 0
+ LOG("IDLE strobe sent to CC1101");
+#endif
}
@@ -669,10 +683,10 @@
uint8_t CC1101::status(uint8_t addr)
{
- _spi->frequency(800000);
+ _spi->frequency(8000000);
toggle_cs();
_spi->write(addr | CCXXX1_READ_BURST);
- tiny_delay();
+ //tiny_delay();
uint8_t x = _spi->write(0);
toggle_cs();
return x;
@@ -688,7 +702,7 @@
*/
// this is split into 3 bytes that are written to 3 different registers on the CC1101
- uint32_t reg_freq = _base_freq / (CCXXX1_CRYSTAL_FREQUENCY>>16);
+ uint32_t reg_freq = freq/(CCXXX1_CRYSTAL_FREQUENCY>>16);
rfSettings.FREQ2 = (reg_freq>>16) & 0xFF; // high byte, bits 7..6 are always 0 for this register
rfSettings.FREQ1 = (reg_freq>>8) & 0xFF; // middle byte
@@ -710,6 +724,14 @@
// ===============
+void CC1101::interface_freq(uint32_t if_freq)
+{
+ // The desired IF frequency for RX. Subtracted from FS base frequency in RX.
+ // bits 7..5 are always 0
+ rfSettings.FSCTRL1 = (if_freq/(CCXXX1_CRYSTAL_FREQUENCY>>10)) & 0x1F;
+ rfSettings.FSCTRL0 = 0x00; // set the initial freq calibration to 0
+}
+// ===============
void CC1101::assign_modem_params()
{
rfSettings.MDMCFG4 = (_modem.channel_bw_exp & 0x03)<<6 | (_modem.channel_bw & 0x03)<<4 | (_modem.data_rate_exp & 0x0F);
@@ -724,14 +746,6 @@
rfSettings.PCKLEN = _pck_control.size;
}
// ===============
-void CC1101::assign_if_freq(uint32_t freq)
-{
- // The desired IF frequency for RX. Subtracted from FS base frequency in RX.
- // bits 7..5 are always 0
- rfSettings.FSCTRL1 = (freq/(CCXXX1_CRYSTAL_FREQUENCY>>10)) & 0x1F;
- rfSettings.FSCTRL0 = 0x00; // set the initial freq calibration to 0
-}
-// ===============
void CC1101::assign_channel_spacing(uint32_t spacing)
{
// have to be careful with bit shifting here since it requires a large amount of shifts
@@ -754,9 +768,6 @@
// there can be 16 different channel numbers. The bandwidth and spacing are defined in other registers
rfSettings.CHANNR = _channel;
- // compute the final adjusted frequency so that it will be correct after the constructor
- // compute_freq();
-
// disable GDO0
rfSettings.IOCFG0 = 0x2E;
@@ -771,32 +782,32 @@
rfSettings.FREND1 = 0xB6;
rfSettings.FREND0 = 0x10;
+
bool RX_TIME_RSSI = false;
bool RX_TIME_QUAL = false;
uint8_t RX_TIME = 0x07; // no timeout
+ rfSettings.MCSM2 = (RX_TIME_RSSI<<4) | (RX_TIME_QUAL<<3) | (RX_TIME & 0x07);
- rfSettings.MCSM2 = (RX_TIME_RSSI<<4) | (RX_TIME_QUAL<<3) | (RX_TIME & 0x07);
uint8_t CCA_MODE = 0x00;
uint8_t RXOFF_MODE = 0x00; // go directly to IDLE when existing RX
//uint8_t RXOFF_MODE = 0x03; // stay in RX when existing RX
uint8_t TXOFF_MODE = 0x03; // go directly to RX when existing TX
// uint8_t TXOFF_MODE = 0x00; // go directly to IDLE when existing TX
+ rfSettings.MCSM1 = ((CCA_MODE & 0x03)<<4) | ((RXOFF_MODE & 0x03)<<2) | (TXOFF_MODE & 0x03);
- rfSettings.MCSM1 = ((CCA_MODE & 0x03)<<4) | ((RXOFF_MODE & 0x03)<<2) | (TXOFF_MODE & 0x03);
uint8_t FS_AUTOCAL = 0x01; // calibrate when going from IDLE to RX or TX
uint8_t PO_TIMEOUT = 0x02;
bool PIN_CTRL_EN = false;
bool XOSC_FORCE_ON = false;
+ rfSettings.MCSM0 = ((FS_AUTOCAL & 0x03)<<4) | ((PO_TIMEOUT & 0x03)<<2) | (PIN_CTRL_EN<<1) | (XOSC_FORCE_ON);
- rfSettings.MCSM0 = ((FS_AUTOCAL & 0x03)<<4) | ((PO_TIMEOUT & 0x03)<<2) | (PIN_CTRL_EN<<1) | (XOSC_FORCE_ON);
bool FOC_BS_CS_GATE = false;
uint8_t FOC_PRE_K = 0x03;
bool FOC_POST_K = true;
uint8_t FOC_LIMIT = 0x01;
-
rfSettings.FOCCFG = 0x40 | (FOC_BS_CS_GATE<<5) | ((FOC_PRE_K & 0x03)<<3) | (FOC_POST_K<<2) | (FOC_LIMIT & 0x03);
rfSettings.BSCFG = 0x1C;
--- a/drivers/CC1101/CC1101.h Wed Jan 14 17:46:44 2015 +0000
+++ b/drivers/CC1101/CC1101.h Thu Jan 15 07:15:33 2015 +0000
@@ -5,7 +5,6 @@
#include "CommLink.h"
#include "cmsis_os.h"
#include "RTP.h"
-//#include "ThreadHelper.h"
#include "CC1101-Defines.h"
#define CCXXX1_DEBUG_MODE 0
@@ -21,19 +20,21 @@
virtual ~CC1101();
// These must have implementations in the CC1101 class since it is a derived class of the base class CommLink
- virtual uint32_t reset(void);
- virtual uint32_t selfTest(void);
+ virtual void reset(void);
+ virtual int32_t selfTest(void);
virtual bool isConnected(void);
// Set/Get the chip's operating channel
void channel(uint16_t);
uint16_t channel(void);
- // Set/Get the chip's datarate (baudrate)
+ // Set/Get the chip's address if applicable
+ void address(uint8_t);
+ uint8_t address(void);
+
+ // Get the chip's datarate (baudrate)
uint16_t datarate(void);
- void address(uint8_t);
-
// The NOP command used to get the CC1101's status byte
uint8_t status(void);
uint8_t status(uint8_t);
@@ -44,6 +45,7 @@
int32_t powerUp(void);
protected:
+ // These must have implementations in the CC1101 class since it is a derived class of the base class CommLink
virtual int32_t sendData(uint8_t*, uint8_t);
virtual int32_t getData(uint8_t*, uint8_t*);
@@ -53,23 +55,25 @@
uint8_t read_reg(uint8_t);
void read_reg(uint8_t, uint8_t*, uint8_t);
+ // Send a command strobe to the CC1101 (1 byte SPI transfer)
uint8_t strobe(uint8_t);
+ // Send the TX or RX command strobe for placing the CC1101 in the respective state
void tx_mode(void);
void rx_mode(void);
+ void idle(void);
+ // Send the command strobe to flush the TX or RX buffers on the CC1101
void flush_tx(void);
void flush_rx(void);
- void idle(void);
-
void freq(uint32_t);
void datarate(uint32_t); // set data rate
void put_rf_settings(void);
void init(void);
private:
- void setup(void);
+
void calibrate(void);
void tiny_delay(void);
@@ -78,7 +82,7 @@
void assign_modem_params(void);
void assign_packet_params(void);
- void assign_if_freq(uint32_t);
+ void interface_freq(uint32_t);
void assign_channel_spacing(uint32_t);
void set_rf_settings(void);
void set_init_vars(void);
--- a/main.cpp Wed Jan 14 17:46:44 2015 +0000
+++ b/main.cpp Thu Jan 15 07:15:33 2015 +0000
@@ -4,7 +4,6 @@
#if RJ_BOOT_LOG
LocalFileSystem local("local"); // Create the local filesystem object
#endif
-DigitalOut ll(p5, 1); // trigger for logic analyzer
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
@@ -12,20 +11,6 @@
Serial pc(USBTX, USBRX);
-// Dummy function
-void primary_radio_rx_handle(void)
-{
- led3 != led3;
-}
-/*
-// Dummy function
-void primary_radio_tx_handle(RTP_t* p)
-{
- led4 = !led4;
-}
-*/
-
-
// Sets the mbed's baudrate for debugging purposes
void baud(int baudrate)
{
@@ -36,7 +21,6 @@
// Main program operations =======================
int main()
{
-
led4 = 0;
// Set the baud rate
baud(57600);
@@ -54,9 +38,7 @@
#endif
-DigitalOut temppp(RJ_PRIMARY_RADIO_INT, 1);
-temppp = 0;
-temppp = 1;
+ DigitalOut temppp(RJ_PRIMARY_RADIO_INT, 1);
// Create a new physical hardware communication link
CC1101 radio_900(
@@ -64,16 +46,13 @@
RJ_PRIMARY_RADIO_CS,
RJ_PRIMARY_RADIO_INT
);
-
- radio_900.selfTest();
-
- radio_900.status(CCXXX1_MARCSTATE);
// Create a Communication Module Object
CommModule comm;
- radio_900.setCommModule(comm);
+
+ radio_900.setModule(comm);
+
comm.TxHandler((CommLink*)&radio_900, &CommLink::sendPacket, 8);
- //comm.RxHandler((CommLink*)&radio_900, &CommLink::ISR, 8);
comm.openSocket(8);
// Open a socket for the Communication Module. Give it a port number and a function to call when a packet of that port number is received
@@ -81,10 +60,13 @@
// Create a dummy packet that is set to send out from socket connection 8
RTP_t dummy_packet;
+ for (int i=0; i<20; i++)
+ dummy_packet.data[i] = i;
+
+ dummy_packet.data_size = 20;
dummy_packet.port = 8;
dummy_packet.subclass = 1;
- for (int i=0; i<20; i++)
- dummy_packet.data[0] = i;
+
// Enable watchdog timer
//Watchdog watchdog;
@@ -94,6 +76,7 @@
while(1) {
led1 = !led1;
+ //radio_900.status();
//comm.send(dummy_packet);
osDelay(500);
--- a/modules/CommLink/CommLink.cpp Wed Jan 14 17:46:44 2015 +0000
+++ b/modules/CommLink/CommLink.cpp Thu Jan 15 07:15:33 2015 +0000
@@ -10,8 +10,6 @@
// Default constructor
CommLink::CommLink()
{
- setup_pins();
- setup();
}
CommLink::CommLink(PinName mosi, PinName miso, PinName sck, PinName cs, PinName int_pin) :
@@ -35,22 +33,13 @@
setup_cs();
setup_interrupt();
-
- // [X] - 2.1 - Define the TX & RX queues.
+ // [X] - 2 - Define the thread tasks for controlling the data queues
// =================
- // The size allocations for each queue are dependent upon what interface that communication is occuring on (radio/serial/usb/etc.)
-
- // [X] - 2.2 - Create small TX & RX queues.
- // =================
-
-
- // [X] - 3.1 - define the thread tasks for controlling the data queues
- // =================
- // Outline the thread definitions
define_thread(_txDef, &CommLink::txThread);
define_thread(_rxDef, &CommLink::rxThread);
- // [X] - 3.2 - Create the threads and pass them a pointer to the created object
+ // [X] - 3 - Create the threads and pass them a pointer to the created object
+ // =================
_txID = osThreadCreate(&_txDef, (void*)this);
_rxID = osThreadCreate(&_rxDef, (void*)this);
}
@@ -69,7 +58,6 @@
void CommLink::setup_spi(void)
{
if (_mosi_pin != NC & _miso_pin != NC & _sck_pin != NC) {
- // Setup the spi for 8 bit data, high steady state clock, second edge capture
_spi = new SPI(_mosi_pin, _miso_pin, _sck_pin); // DON'T FORGET TO DELETE IN DERIVED CLASS
_spi->format(8,0);
_spi->frequency(5000000);
@@ -104,7 +92,7 @@
DigitalOut tx_led(LED1, 1);
while(1) {
- // [] - 1 - Wait until the CommModule class sends a signal to begin operation on new data being placed in its txQueue
+ // [X] - 1 - Wait until the CommModule class sends a signal to begin operation on new data being placed in its txQueue
// =================
osSignalWait(COMM_LINK_SIGNAL_TX_TRIGGER, osWaitForever);
@@ -136,20 +124,19 @@
// Set the function to call on an interrupt trigger
inst->_int_in->rise(inst, &CommLink::ISR);
- DigitalOut rx_led(LED2, 0);
+ DigitalOut rx_led(LED2, 1);
while(1) {
- // [X] - 1 - Wait until new data has arrived
+ // [X] - 1 - Wait until new data has arrived - this is interrupt triggered by CommLink::ISR()
// =================
- LOG("WAITING FOR TRIGGER");
osSignalWait(COMM_LINK_SIGNAL_RX_TRIGGER, osWaitForever);
- LOG("TRIGGERED");
+
+
// [X] - 2 - Get the received data from the external chip
// =================
uint8_t rec_bytes = COMM_LINK_BUFFER_SIZE;
RTP_t p;
- //uint8_t *buf_ptr = p.data;
inst->getData(p.data, &rec_bytes);
//p.port = p.data[0] & 0xF0;
p.port = 8;
@@ -158,7 +145,7 @@
// [X] - 3 - Write the data to the CommModule object's rxQueue
// =================
- inst->_comm_module_ptr->receive(p);
+ inst->_comm_module->receive(p);
// [~] - 4 - Blink the RX LED for the hardware link
@@ -179,52 +166,27 @@
}
-void CommLink::write_tx_queue(RTP_t *p)
+void CommLink::sendPacket(RTP_t *p)
{
- // [] - 1 - Write the data to the RX queue
- // =================
-
- // [] - 2 - Signal the TX thread of new data
- // =================
-
+ sendData(p->raw, p->data_size+1);
}
-void CommLink::sendPacket(RTP_t *p)
-{
- sendData(p->data, sizeof(p->data));
-}
-
-
-
-void CommLink::receivePacket(RTP_t *p)
-{
-
-}
-
// Interrupt Service Routine - KEEP OPERATIONS TO ABOSOLUTE MINIMUM HERE AND IN ANY OVERRIDEN BASE CLASS IMPLEMENTATIONS OF THIS CLASS METHOD
void CommLink::ISR(void)
{
osSignalSet(_rxID , COMM_LINK_SIGNAL_RX_TRIGGER);
}
+
void CommLink::toggle_cs(void)
{
*_cs = !*_cs;
}
-void CommLink::triggerReceive(void)
-{
- CommLink::ISR();
-}
-void CommLink::triggerTransmit(void)
+void CommLink::setModule(CommModule& com)
{
- osSignalSet(_txID , COMM_LINK_SIGNAL_TX_TRIGGER);
-}
-
-void CommLink::setCommModule(CommModule& com)
-{
- _comm_module_ptr = &com;
+ _comm_module = &com;
osSignalSet(_rxID , COMM_LINK_SIGNAL_MODULE_LINKED);
}
\ No newline at end of file
--- a/modules/CommLink/CommLink.h Wed Jan 14 17:46:44 2015 +0000
+++ b/modules/CommLink/CommLink.h Thu Jan 15 07:15:33 2015 +0000
@@ -22,29 +22,25 @@
class CommLink
{
public:
- // Default constructor
+ // Constructors
CommLink();
-
CommLink(PinName, PinName, PinName, PinName = NC, PinName = NC);
// Deconstructor
- virtual ~CommLink() {};
+ virtual ~CommLink() {}; // Don't forget to include deconstructor implementation in derived classes that frees memory
// Class constants for the data queue sizes
static const int TX_QUEUE_SIZE;
static const int RX_QUEUE_SIZE;
// The pure virtual methods for making CommLink an abstract class
- virtual uint32_t reset(void) = 0;
- virtual uint32_t selfTest(void) = 0;
+ virtual void reset(void) = 0;
+ virtual int32_t selfTest(void) = 0;
virtual bool isConnected(void) = 0;
-
- void module(CommModule&);
- void triggerReceive(void);
- void triggerTransmit(void);
- void write_tx_queue(RTP_t*);
- void setCommModule(CommModule&);
+ void setModule(CommModule&);
+ void sendPacket(RTP_t*);
+ void receivePacket(RTP_t*);
protected:
virtual int32_t sendData(uint8_t*, uint8_t) = 0; // write data out to the radio device using SPI
@@ -52,7 +48,7 @@
void ISR(void);
void toggle_cs(void);
- void ready(void);
+ void ready(void); // Always call CommLink::ready() after derived class is ready for communication
void setup_spi(void);
// The data queues for temporarily holding received packets and packets that need to be transmitted
@@ -76,38 +72,36 @@
// Used to help define the class's threads in the constructor
friend void define_thread(osThreadDef_t&, void(*task)(void const *arg), osPriority, uint32_t, unsigned char*);
+ // Data queues
MailHelper<RTP_t, COMM_LINK_TX_QUEUE_SIZE> _txQueueHelper;
MailHelper<RTP_t, COMM_LINK_RX_QUEUE_SIZE> _rxQueueHelper;
-
- // The working threads for handeling rx and tx data queues
- static void txThread(void const*);
- static void rxThread(void const*);
-
- // methods for initializing the pins
- void setup(void);
- void setup_pins(PinName = NC, PinName = NC, PinName = NC, PinName = NC, PinName = NC);
- void setup_cs(void);
- void setup_interrupt(void);
-
- typedef void(*func_p)(void const*);
- func_p _rx_handle;
-
- static unsigned int _nbr_links;
-
- uint8_t buf[COMM_LINK_BUFFER_SIZE];
-
// Thread definitions and IDs
osThreadDef_t _txDef;
osThreadDef_t _rxDef;
osThreadId _txID;
osThreadId _rxID;
-
- CommModule* _comm_module_ptr;
+
+ // The working threads for handeling RX/TX data queue operations
+ static void txThread(void const*);
+ static void rxThread(void const*);
-public:
- void sendPacket(RTP_t*);
- void receivePacket(RTP_t*);
+ // Methods for initializing a transceiver's pins for communication
+ void setup(void);
+ void setup_pins(PinName = NC, PinName = NC, PinName = NC, PinName = NC, PinName = NC);
+ void setup_cs(void);
+ void setup_interrupt(void);
+
+ // Used for tracking the number of link-level communication interfaces
+ static unsigned int _nbr_links;
+
+ //typedef void(*func_p)(void const*);
+ //nc_p _rx_handle;
+
+ uint8_t buf[COMM_LINK_BUFFER_SIZE];
+
+ CommModule *_comm_module;
+
};
#endif // COMMUNICATION_LINK_H
--- a/modules/CommModule/CommModule.cpp Wed Jan 14 17:46:44 2015 +0000
+++ b/modules/CommModule/CommModule.cpp Thu Jan 15 07:15:33 2015 +0000
@@ -83,17 +83,11 @@
evt = osMailGet(inst->_rxQueue, osWaitForever);
if (evt.status == osEventMail) {
+ DigitalOut led(LED4, 1);
// get a pointer to where the data is stored
p = (RTP_t*)evt.value.p;
- /*
- if (inst->p.port) {
- // write the received packet to it's respective port's queue
- inst->rxQueue[p->port]
- }
- */
-
// If there is an open socket for the port, call it.
if (std::binary_search(inst->_open_ports->begin(), inst->_open_ports->end(), p->port)) {
inst->_rx_handles[p->port].call();
@@ -102,7 +96,7 @@
EVENT("Reception: \r\n Port: %u\r\n Subclass: %u\r\n", p->port, p->subclass);
osMailFree(inst->_rxQueue, p); // free memory allocated for mail
-
+
}
}
}
@@ -180,14 +174,16 @@
{
static bool isReady = false;
- if (!isReady) {
- isReady = true;
+ if (isReady) {
+ return;
+ }
- _open_ports = new std::vector<uint8_t>;
+ isReady = true;
- osSignalSet(_txID, COMM_MODULE_SIGNAL_START_THREAD);
- osSignalSet(_rxID, COMM_MODULE_SIGNAL_START_THREAD);
- }
+ _open_ports = new std::vector<uint8_t>;
+
+ osSignalSet(_txID, COMM_MODULE_SIGNAL_START_THREAD);
+ osSignalSet(_rxID, COMM_MODULE_SIGNAL_START_THREAD);
}
@@ -204,6 +200,7 @@
// =================
p->port = packet.port;
p->subclass = packet.subclass;
+ p->data_size = packet.data_size;
for (int i=0; i<(sizeof(packet)/sizeof(uint8_t)); i++)
p->data[i] = packet.data[i];
@@ -231,7 +228,7 @@
p->port = packet.port;
p->subclass = packet.subclass;
- for (int i=0; i<(sizeof(packet)/sizeof(uint8_t)); i++)
+ for (int i=0; i<packet.data_size; i++)
p->data[i] = packet.data[i];
// [X] - 1.3 - Place the passed packet into the txQueue.
--- a/modules/CommModule/CommModule.h Wed Jan 14 17:46:44 2015 +0000
+++ b/modules/CommModule/CommModule.h Thu Jan 15 07:15:33 2015 +0000
@@ -50,6 +50,7 @@
void TxHandler(void(*)(RTP_t*), uint8_t);
void RxHandler(void(*)(RTP_t*), uint8_t);
+
void RxHandler(void(*)(void), uint8_t);
void openSocket(uint8_t);
@@ -61,7 +62,6 @@
// Send a RTP packet. The details of exactly how the packet will be sent are determined from the RTP packet's port and subclass values
void send(RTP_t&);
-
void receive(RTP_t&);
//osThreadId rxID(void);
--- a/utils/RTP.h Wed Jan 14 17:46:44 2015 +0000
+++ b/utils/RTP.h Thu Jan 15 07:15:33 2015 +0000
@@ -6,14 +6,14 @@
#define RTP_MAX_DATA_SIZE 32
typedef struct _RTP_t {
- uint8_t size;
+ uint8_t data_size;
union {
struct {
union {
uint8_t header;
struct {
- uint8_t port;
- uint8_t subclass;
+ uint8_t port : 4;
+ uint8_t subclass : 4;
};
};
uint8_t data[RTP_MAX_DATA_SIZE];
