From 94a21c38b291c4e174c1f9b2cfd3d3281cd85fa7 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 24 Feb 2015 01:57:01 +0100 Subject: hhd70: added internal functions for bulk read and write --- software/avr.lib/cc1101.c | 100 ++++++++++++++++++++++++++++++++++---- software/avr.lib/cc1101_defines.h | 5 +- 2 files changed, 93 insertions(+), 12 deletions(-) diff --git a/software/avr.lib/cc1101.c b/software/avr.lib/cc1101.c index 6072dfd..20a2bda 100644 --- a/software/avr.lib/cc1101.c +++ b/software/avr.lib/cc1101.c @@ -35,6 +35,20 @@ static cc1101_driver_conf_t drv = { .freq_corr = 1.0 }; +static uint8_t const pa_table_values_[] = { 0x00, 0x30, 0x20, 0x10, 0x01, 0x02, 0x11, 0x03, + 0x12, 0x04, 0x05, 0x13, 0x06, 0x07, 0x21, 0x14, 0x08, 0x09, 0x0A, 0x15, + 0x0B, 0x31, 0x16, 0x0C, 0x0D, 0x0E, 0x17, 0x0F, 0x22, 0x18, 0x19, 0x1A, + 0x1B, 0x32, 0x23, 0x1C, 0x6F, 0x1D, 0x1E, 0x1F, 0x24, 0x33, 0x25, 0x34, + 0x26, 0x6E, 0x27, 0x35, 0x28, 0x6D, 0x6C, 0x29, 0x36, 0x6B, 0x2A, 0x6A, + 0x37, 0x69, 0x2B, 0x68, 0x38, 0x2C, 0x8F, 0x67, 0x2D, 0x57, 0x39, 0x66, + 0x2E, 0x56, 0x3A, 0x2F, 0x65, 0x55, 0x3B, 0x64, 0x54, 0x3C, 0x63, 0x3D, + 0x53, 0x3E, 0x62, 0x3F, 0x52, 0x40, 0x61, 0x51, 0x60, 0x50, 0x8E, 0x8D, + 0x8C, 0xCF, 0x8B, 0x8A, 0x89, 0x88, 0x87, 0x86, 0x85, 0xCE, 0x84, 0x83, + 0xCD, 0x82, 0xCC, 0x81, 0xCB, 0x80, 0xCA, 0xC9, 0xC8, 0xC7, 0xC6, 0xC5, + 0xC4, 0xC3, 0xC2, 0xC1, 0xC0 }; + +#define PA_TABLE_VALUES_MAX ((sizeof(pa_table_values_)/sizeof(uint8_t))-1) + /* * internal functions */ @@ -44,7 +58,7 @@ static inline void cc1101_spi_wait_rdy(void) while(drv.spi_read_miso()); // wait for CHP_RDY to go low, TODO: timeout... } -static uint8_t cc1101_spi_strobe_command(uint8_t cmd) +static uint8_t cc1101_spi_strobe_command(const uint8_t cmd) { if(cmd < CC1101_CMD_MIN || cmd > CC1101_CMD_MAX) return -1; @@ -58,29 +72,30 @@ static uint8_t cc1101_spi_strobe_command(uint8_t cmd) return status; } -static uint8_t cc1101_spi_read_register(uint8_t addr) +static uint8_t cc1101_spi_read_register(const uint8_t addr) { - if(addr > CC1101_ADDR_MAX) + if(addr > CC1101_ADDR_MAX || (addr >= CC1101_CMD_MIN && addr <= CC1101_CMD_MAX)) return 0xFF; - if(addr > CC1101_REG_RW_MAX) - addr |= CC1101_HEADER_READONLY; + uint8_t hdr = addr; + if(addr >= CC1101_REG_RO_MIN && addr <= CC1101_REG_RO_MAX) + hdr |= CC1101_HEADER_READONLY; else - addr |= CC1101_HEADER_READ; + hdr |= CC1101_HEADER_READ; drv.spi_cs_enable(); cc1101_spi_wait_rdy(); - drv.spi_write_byte(addr); + drv.spi_write_byte(hdr); uint8_t data = drv.spi_read_byte(); drv.spi_cs_disable(); return data; } -static void cc1101_spi_write_register(uint8_t addr, uint8_t data) +static void cc1101_spi_write_register(const uint8_t addr, const uint8_t data) { - if(addr > CC1101_REG_RW_MAX) + if(addr > CC1101_REG_RW_MAX && addr != CC1101_REG_PATABLE && addr != CC1101_REG_FIFO) return; drv.spi_cs_enable(); @@ -92,6 +107,73 @@ static void cc1101_spi_write_register(uint8_t addr, uint8_t data) drv.spi_cs_disable(); } +// this is an internal-internal function - never use this outside of internal functions +static uint8_t cc1101_spi_read_register_burst_raw(const uint8_t addr, uint8_t* data, const uint8_t l) +{ + drv.spi_cs_enable(); + cc1101_spi_wait_rdy(); + + drv.spi_write_byte(CC1101_HEADER_READ | CC1101_HEADER_BURST | addr); + uint8_t i; + for(i = 0; i < l; ++i) + data[i] = drv.spi_read_byte(); + + drv.spi_cs_disable(); + return i; +} + +static uint8_t cc1101_spi_read_register_burst(const uint8_t addr, uint8_t* data, const uint8_t len) +{ + if(addr <= CC1101_REG_RW_MAX) { + uint8_t l = (CC1101_REG_RW_MAX - addr) + 1; + l = (len > l) ? l : len; + return cc1101_spi_read_register_burst_raw(addr, data, l); + + } else if(CC1101_REG_PATABLE) { + uint8_t l = (len > CC1101_PATABLE_SIZE) ? CC1101_PATABLE_SIZE : len; + return cc1101_spi_read_register_burst_raw(addr, data, l); + + } else if(CC1101_REG_FIFO) { + uint8_t l = (len > CC1101_FIFO_MAX_LEN) ? CC1101_FIFO_MAX_LEN : len; + return cc1101_spi_read_register_burst_raw(addr, data, l); + } + return 0; +} + +// this is an internal-internal function - never use this outside of internal functions +static uint8_t cc1101_spi_write_register_burst_raw(const uint8_t addr, uint8_t* data, const uint8_t l) +{ + drv.spi_cs_enable(); + cc1101_spi_wait_rdy(); + + drv.spi_write_byte(CC1101_HEADER_WRITE | CC1101_HEADER_BURST | addr); + uint8_t i; + for(i = 0; i < l; ++i) + drv.spi_write_byte(data[i]); + + drv.spi_cs_disable(); + return i; +} + +static uint8_t cc1101_spi_write_register_burst(const uint8_t addr, uint8_t* data, const uint8_t len) +{ + if(addr <= CC1101_REG_RW_MAX) { + uint8_t l = (CC1101_REG_RW_MAX - addr) + 1; + l = (len > l) ? l : len; + return cc1101_spi_write_register_burst_raw(addr, data, l); + + } else if(CC1101_REG_PATABLE) { + uint8_t l = (len > CC1101_PATABLE_SIZE) ? CC1101_PATABLE_SIZE : len; + return cc1101_spi_write_register_burst_raw(addr, data, l); + + } else if(CC1101_REG_FIFO) { + uint8_t l = (len > CC1101_FIFO_MAX_LEN) ? CC1101_FIFO_MAX_LEN : len; + return cc1101_spi_write_register_burst_raw(addr, data, l); + } + return 0; +} + + /* * EXTERNAL INTERFACE */ diff --git a/software/avr.lib/cc1101_defines.h b/software/avr.lib/cc1101_defines.h index f1e63aa..6018e60 100644 --- a/software/avr.lib/cc1101_defines.h +++ b/software/avr.lib/cc1101_defines.h @@ -120,14 +120,15 @@ //power amplifier table #define CC1101_REG_PATABLE 0x3E +#define CC1101_PATABLE_SIZE 8 //data FIFOs #define CC1101_REG_FIFO 0x3F +#define CC1101_FIFO_MAX_LEN 64 #define CC1101_ADDR_MAX 0x3F - ////status byte: #define CC1101_STATUS_CHIP_NOT_RDY(x) (x & 0b10000000) #define CC1101_STATUS_IDLE(x) ((x & 0b01110000) == 0b00000000) @@ -140,8 +141,6 @@ #define CC1101_STATUS_TXFIFO_OVERFLOW(x) ((x & 0b01110000) == 0b01110000) #define CC1101_STATUS_FIFO_BYTES_AVAILABLE(x) (x & 0b00001111) -#define CC1101_FIFO_MAX_LEN 64 - #define CC1101_MARCSTATE_SLEEP 0x00 #define CC1101_MARCSTATE_IDLE 0x01 -- cgit v1.2.3