From 591758ec5834e27517afce7d2b2dc3f14e162523 Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Thu, 17 Sep 2015 20:56:49 +0200 Subject: [PATCH] Simple CC1101 driver modifications - Add comments - Add cc1101_set_channel() function - Place the CC1101 chip in idle state when changing configuration registers - Add cc1101_read_pkt_status() function - Remove niternal functions from header file --- extdrv/cc1101.c | 91 +++++++++++++++++++++++++++-------------- include/extdrv/cc1101.h | 34 ++++++++------- 2 files changed, 80 insertions(+), 45 deletions(-) diff --git a/extdrv/cc1101.c b/extdrv/cc1101.c index 3b30dde..939dba3 100644 --- a/extdrv/cc1101.c +++ b/extdrv/cc1101.c @@ -76,14 +76,40 @@ uint8_t cc1101_spi_transfer(uint8_t addr, uint8_t* out, uint8_t* in, uint8_t siz } -/***************************************************************************** */ -/* SPI registers and commands access Wrappers */ +/* Read and return single register value */ +static uint8_t cc1101_read_reg(uint8_t addr) +{ + uint8_t ret = 0; + cc1101_spi_transfer((addr | CC1101_READ_OFFSET), NULL, &ret, 1); + return ret; +} +/* Read nb registers from start_addr into buffer. Return the global status byte. */ +static uint8_t cc1101_read_burst_reg(uint8_t start_addr, uint8_t* buffer, uint8_t nb) +{ + uint8_t addr = (start_addr | CC1101_READ_OFFSET | CC1101_BURST_MODE); + return cc1101_spi_transfer(addr, NULL, buffer, nb); +} +/* Write single register value. Return the global status byte */ +static uint8_t cc1101_write_reg(uint8_t addr, uint8_t val) +{ + return cc1101_spi_transfer((addr | CC1101_WRITE_OFFSET), &val, NULL, 1); +} +static uint8_t cc1101_write_burst_reg(uint8_t start_addr, uint8_t* buffer, uint8_t nb) +{ + uint8_t addr = (start_addr | CC1101_WRITE_OFFSET | CC1101_BURST_MODE); + return cc1101_spi_transfer(addr, buffer, NULL, nb); +} + /* Send command and return global status byte */ -uint8_t cc1101_send_cmd(uint8_t addr) +static uint8_t cc1101_send_cmd(uint8_t addr) { return cc1101_spi_transfer((addr | CC1101_WRITE_OFFSET), NULL, NULL, 0); } + + +/***************************************************************************** */ +/* SPI registers and commands access Wrappers */ void cc1101_reset(void) { cc1101_send_cmd(CC1101_CMD(reset)); @@ -103,6 +129,22 @@ void cc1101_flush_rx_fifo(void) cc1101_send_cmd(CC1101_CMD(flush_rx)); } + +/***************************************************************************** */ +/* Read global status byte */ +uint8_t cc1101_read_status(void) +{ + return cc1101_send_cmd(CC1101_CMD(no_op)); +} +/* Read packet status byte */ +uint8_t cc1101_read_pkt_status(void) +{ + return cc1101_send_cmd(CC1101_STATUS(packet_status)); +} + + +/***************************************************************************** */ +/* Change Current mode / status to RX */ void cc1101_enter_rx_mode(void) { cc1101_send_cmd(CC1101_CMD(state_idle)); @@ -132,38 +174,10 @@ static uint8_t cc1101_enter_tx_mode(void) return 0; } -/* Read global status byte */ -uint8_t cc1101_read_status(void) -{ - return cc1101_send_cmd(CC1101_CMD(no_op)); -} - -/* Read and return single register value */ -uint8_t cc1101_read_reg(uint8_t addr) -{ - uint8_t ret = 0; - cc1101_spi_transfer((addr | CC1101_READ_OFFSET), NULL, &ret, 1); - return ret; -} -/* Read nb registers from start_addr into buffer. Return the global status byte. */ -uint8_t cc1101_read_burst_reg(uint8_t start_addr, uint8_t* buffer, uint8_t nb) -{ - uint8_t addr = (start_addr | CC1101_READ_OFFSET | CC1101_BURST_MODE); - return cc1101_spi_transfer(addr, NULL, buffer, nb); -} -/* Write single register value. Return the global status byte */ -uint8_t cc1101_write_reg(uint8_t addr, uint8_t val) { - return cc1101_spi_transfer((addr | CC1101_WRITE_OFFSET), &val, NULL, 1); -} -uint8_t cc1101_write_burst_reg(uint8_t start_addr, uint8_t* buffer, uint8_t nb) -{ - uint8_t addr = (start_addr | CC1101_WRITE_OFFSET | CC1101_BURST_MODE); - return cc1101_spi_transfer(addr, buffer, NULL, nb); } - /***************************************************************************** */ /* Signal strength and link quality */ @@ -347,6 +361,17 @@ void cc1101_set_address(uint8_t address) cc1101_write_reg(CC1101_REGS(device_addr), address); } +/* Set current channel to use. + * The caller is responsible for checking that the channel spacing and channel bandwith are configures + * correctly to prevent overlaping channels, or to use only non-overlaping channel numbers. + * This function places the CC1101 chip in idle state. + */ +void cc1101_set_channel(uint8_t chan) +{ + cc1101_send_cmd(CC1101_CMD(state_idle)); + cc1101_write_reg(CC1101_REGS(channel_number), chan); +} + /* Change a configuration byte. * This function places the CC1101 chip in idle state. */ @@ -466,6 +491,7 @@ void cc1101_config(void) /* Update CC1101 config * Arguments are the settings table which is a table of address and value pairs, * and the table length, which must be even. + * Puts the CC1101 chip in idle state */ void cc1101_update_config(uint8_t* settings, uint8_t len) { @@ -473,6 +499,9 @@ void cc1101_update_config(uint8_t* settings, uint8_t len) if (len & 0x01) { return; } + /* Chip must be in idle state when modifying any of the Frequency or channel registers. + * Move to idle state for all cases, easier. */ + cc1101_send_cmd(CC1101_CMD(state_idle)); for (i = 0; i < len; i += 2) { cc1101_write_reg(settings[i], settings[i + 1]); } diff --git a/include/extdrv/cc1101.h b/include/extdrv/cc1101.h index 6527903..396e721 100644 --- a/include/extdrv/cc1101.h +++ b/include/extdrv/cc1101.h @@ -137,6 +137,8 @@ struct cc1101_command_strobes { #define CC1101_FIFO_SIZE 64 #define CC1101_CRC_OK 0x80 +#define CC1101_CARIER_SENSE 0x40 +#define CC1101_CHANNEL_CLEAR 0x10 /* Errors definitions */ @@ -166,8 +168,7 @@ struct cc1101_command_strobes { /***************************************************************************** */ /* SPI registers and commands access Wrappers */ -/* Send command and return global status byte */ -uint8_t cc1101_send_cmd(uint8_t addr); +/* Reset CC1101 chip */ void cc1101_reset(void); /* Power Up Reset is the same as a usual reset, the function will only wait longer for * the "chip ready" signal before starting SPI transfers .... */ @@ -176,20 +177,15 @@ void cc1101_power_up_reset(void); void cc1101_flush_tx_fifo(void); void cc1101_flush_rx_fifo(void); -void cc1101_enter_rx_mode(void); - +/***************************************************************************** */ /* Read global status byte */ uint8_t cc1101_read_status(void); +/* Read packet status byte */ +uint8_t cc1101_read_pkt_status(void); -/* Read and return single register value */ -uint8_t cc1101_read_reg(uint8_t addr); -/* Read nb registers from start_addr into buffer. Return the global status byte. */ -uint8_t cc1101_read_burst_reg(uint8_t start_addr, uint8_t* buffer, uint8_t nb); - -/* Write single register value. Return the global status byte */ -uint8_t cc1101_write_reg(uint8_t addr, uint8_t val); -uint8_t cc1101_write_burst_reg(uint8_t start_addr, uint8_t* buffer, uint8_t nb); - +/***************************************************************************** */ +/* Enter Rx mode */ +void cc1101_enter_rx_mode(void); /***************************************************************************** */ @@ -265,6 +261,13 @@ int cc1101_receive_packet(uint8_t* buffer, uint8_t size, uint8_t* status); */ void cc1101_set_address(uint8_t address); +/* Set current channel to use. + * The caller is responsible for checking that the channel spacing and channel bandwith are configures + * correctly to prevent overlaping channels, or to use only non-overlaping channel numbers. + * This function places the CC1101 chip in idle state. + */ +void cc1101_set_channel(uint8_t chan); + /* Change a configuration byte. * This function places the CC1101 chip in idle state. */ @@ -273,12 +276,15 @@ void cc1101_set_config(uint8_t byte_addr, uint8_t value); /* Configure pins, reset the CC1101, and put the CC1101 chip in idle state */ void cc1101_init(uint8_t ssp_num, const struct pio* cs_pin, const struct pio* miso_pin); -/* Write / send all the configuration register values to the CC1101 chip */ +/* Write / send all the configuration register values to the CC1101 chip + * This function places the CC1101 chip in idle state. + */ void cc1101_config(void); /* Update CC1101 config * Arguments are the settings table which is a table of address and value pairs, * and the table length, which must be even. + * This function places the CC1101 chip in idle state. */ void cc1101_update_config(uint8_t* settings, uint8_t len); -- 2.43.0