123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- /*
- * Project: Arrow
- * Author: curiousmuch
- */
- #include <stdio.h>
- #include <math.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "freertos/semphr.h"
- #include "freertos/portmacro.h"
- #include "driver/gpio.h"
- #include "sdkconfig.h"
- #include "driver/spi_master.h"
- #include "esp_err.h"
- #include "cc1200.h"
- #include "cc1200_protocol.h"
- #include "board.h"
- #include "esp_task_wdt.h"
- #include "freertos/ringbuf.h"
- #include "esp_log.h"
- #include "driver/timer.h"
- /* Debugging Tag for ESP_LOG */
- #define CC_TAG "CC1200 Driver"
- /* SPI Constants */
- #define CC1200_WRITE_BIT 0
- #define CC1200_READ_BIT BIT(1)
- #define CC1200_BURST_BIT BIT(0)
- /* CC1200 SPI Driver Configuration */
- spi_bus_config_t bus_config =
- {
- .miso_io_num = CC1200_MISO,
- .mosi_io_num = CC1200_MOSI,
- .sclk_io_num = CC1200_SCLK,
- .quadwp_io_num = -1,
- .quadhd_io_num = -1,
- .max_transfer_sz = 150,
- .flags = SPICOMMON_BUSFLAG_MASTER,
- .intr_flags = ESP_INTR_FLAG_IRAM
- };
- spi_device_interface_config_t interface_config =
- {
- .command_bits = 2,
- .address_bits = 6,
- .dummy_bits = 0,
- .mode = 0,
- .spics_io_num = CC1200_CS,
- .clock_speed_hz = (APB_CLK_FREQ/16),
- .flags = 0,
- .queue_size = 20
- };
- spi_device_handle_t spi;
- /* Private Functions */
- static void cc1200_gpio_init(void)
- {
- gpio_config_t reset_pin_config =
- {
- .pin_bit_mask = (uint64_t)(BIT64(CC1200_RESET)),
- .mode = GPIO_MODE_OUTPUT,
- .pull_up_en = GPIO_PULLUP_DISABLE,
- .pull_down_en = GPIO_PULLDOWN_DISABLE,
- .intr_type = GPIO_INTR_DISABLE
- };
- gpio_config_t gpio_pin_config =
- {
- .pin_bit_mask = (uint64_t) (BIT64(CC1200_GPIO0)|BIT64(CC1200_GPIO2)|BIT64(CC1200_GPIO3)),
- .mode = GPIO_MODE_INPUT,
- .pull_up_en = GPIO_PULLUP_DISABLE,
- .pull_down_en = GPIO_PULLDOWN_DISABLE,
- .intr_type = GPIO_INTR_DISABLE
- };
- gpio_config(&reset_pin_config);
- gpio_config(&gpio_pin_config);
- gpio_set_level(CC1200_RESET, 1);
- }
- static void cc1200_spi_init(void)
- {
- esp_err_t ret;
- ret = spi_bus_initialize(VSPI_HOST, &bus_config, 0); // this uses DMA channel 1
- ESP_ERROR_CHECK(ret);
- ret = spi_bus_add_device(VSPI_HOST, &interface_config, &spi);
- ESP_ERROR_CHECK(ret);
- }
- static void IRAM_ATTR cc1200_spi_write_byte(uint16_t addr, uint8_t data)
- {
- esp_err_t ret;
- spi_transaction_t tx_trans =
- {
- .flags = SPI_TRANS_USE_TXDATA,
- .cmd = CC1200_WRITE_BIT,
- .addr = addr,
- .length = 8,
- .rxlength = 0,
- .tx_data[0] = data
- };
- if ((addr & 0xFF00) != 0) // send data with extended address in command field
- {
- tx_trans.flags |= (SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR);
- spi_transaction_ext_t tx_trans_ext =
- {
- .base = tx_trans,
- .command_bits = 2,
- .address_bits = 14
- };
- ret = spi_device_polling_transmit(spi, (spi_transaction_t*)&tx_trans_ext);
- }
- else
- {
- ret = spi_device_polling_transmit(spi, &tx_trans);
- }
- ESP_ERROR_CHECK(ret);
- }
- static void IRAM_ATTR cc1200_spi_write_bytes(uint16_t addr, uint8_t* data, uint8_t len)
- {
- esp_err_t ret;
- spi_transaction_t tx_trans =
- {
- .cmd = (CC1200_WRITE_BIT | CC1200_BURST_BIT),
- .addr = addr,
- .length = 8*len,
- .tx_buffer = data
- };
- if ((addr & 0xFF00) != 0) // send data with extended address in command field
- {
- tx_trans.flags |= (SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR);
- spi_transaction_ext_t tx_trans_ext =
- {
- .base = tx_trans,
- .command_bits = 2,
- .address_bits = 14
- };
- ret = spi_device_polling_transmit(spi, (spi_transaction_t*)&tx_trans_ext);
- }
- else
- {
- ret = spi_device_polling_transmit(spi, &tx_trans);
- }
- ESP_ERROR_CHECK(ret);
- }
- static void IRAM_ATTR cc1200_spi_read_byte(uint16_t addr, uint8_t* data)
- {
- esp_err_t ret;
- spi_transaction_t rx_trans =
- {
- .cmd = CC1200_READ_BIT,
- .addr = addr,
- .length = 8,
- .rxlength = 8,
- .rx_buffer = data
- };
- if ((addr & 0xFF00) != 0) // read data with extended address in command field
- {
- rx_trans.flags |= (SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR);
- spi_transaction_ext_t rx_trans_ext =
- {
- .base = rx_trans,
- .command_bits = 2,
- .address_bits = 14
- };
- ret = spi_device_polling_transmit(spi, (spi_transaction_t*)&rx_trans_ext);
- }
- else
- {
- ret = spi_device_polling_transmit(spi, &rx_trans);
- }
- ESP_ERROR_CHECK(ret);
- }
- static void cc1200_spi_read_bytes(uint16_t addr, uint8_t* data, uint8_t len)
- {
- esp_err_t ret;
- spi_transaction_t rx_trans =
- {
- .cmd = (CC1200_READ_BIT | CC1200_BURST_BIT),
- .addr = addr,
- .length = 8*len,
- .rxlength = 8*len,
- .rx_buffer = data
- };
- if ((addr & 0xFF00) != 0) // read data with extended address in command field
- {
- rx_trans.flags |= (SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR);
- spi_transaction_ext_t rx_trans_ext =
- {
- .base = rx_trans,
- .command_bits = 2,
- .address_bits = 14
- };
- ret = spi_device_polling_transmit(spi, (spi_transaction_t*)&rx_trans_ext);
- }
- else
- {
- ret = spi_device_polling_transmit(spi, &rx_trans);
- }
- ESP_ERROR_CHECK(ret);
- }
- rf_status_t IRAM_ATTR cc1200_spi_strobe(uint8_t cmd)
- {
- esp_err_t ret;
- uint8_t temp=0;
- spi_transaction_t rx_trans =
- {
- .flags = (SPI_TRANS_USE_TXDATA | SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR),
- .length = 8,
- .rxlength = 8,
- .rx_buffer = &temp,
- .tx_data[0] = cmd
- };
- spi_transaction_ext_t rx_trans_ext =
- {
- .base = rx_trans,
- .command_bits = 0,
- .address_bits = 0
- };
- ret = spi_device_polling_transmit(spi, (spi_transaction_t*)&rx_trans_ext);
- ESP_ERROR_CHECK(ret);
- return (temp & 0xF0);
- }
- // writes array or register value pairs from smart RF studio to CC1200
- // w/o reseting
- void cc1200_radio_config(const cc1200_reg_settings_t* rf_settings, uint8_t len)
- {
- uint8_t i;
- for (i=0;i<len;i++)
- {
- cc1200_spi_write_byte(rf_settings[i].addr, rf_settings[i].data);
- }
- }
- /* CC1200 Driver Public Functions */
- uint8_t IRAM_ATTR cc1200_radio_read_RSSI(void)
- {
- uint8_t data = 0;
- cc1200_spi_read_byte(CC120X_RSSI1, &data);
- return data;
- }
- uint8_t IRAM_ATTR cc1200_radio_read_CFM(void)
- {
- uint8_t data = 0;
- cc1200_spi_read_byte(CC120X_CFM_RX_DATA_OUT, &data);
- return data;
- }
- void IRAM_ATTR cc1200_radio_write_CFM(int8_t data)
- {
- cc1200_spi_write_byte(CC120X_CFM_TX_DATA_IN, data);
- }
- rf_status_t cc1200_radio_reset(void)
- {
- rf_status_t status;
- uint8_t retry_count = 0;
- cc1200_spi_strobe(CC120X_SRES); // soft reset the chip
- status = cc1200_spi_strobe(CC120X_SNOP); // get chip status
- vTaskDelay(20 / portTICK_PERIOD_MS);
- while((CC120X_RDYn_BIT & (status & 0x80))) // if chip isn't ready, wait 10ms
- {
- vTaskDelay(10 / portTICK_PERIOD_MS);
- if (retry_count > 3)
- {
- ESP_LOGE(CC_TAG, "Reset Failure");
- // TODO: Reset ESP32
- break;
- }
- status = cc1200_spi_strobe(CC120X_SNOP);
- retry_count++;
- }
- return status;
- }
- #define CC1200_LO_DIVIDER 24 // 136.7 - 160 MHz Band
- #define CC1200_XOSC 40000000 // 40MHz
- void cc1200_radio_frequency(uint32_t freq)
- {
- // f_RF = f_VCO / LO Divider
- // f_VCO = FREQ / 2^16 * f_XOSX + FREQOFF / 2^18 * F_XOSC
- double temp_freq;
- // calculate FREQ0, FREQ, FREQ2 registers
- temp_freq = ((double) freq * 65536 * CC1200_LO_DIVIDER) / CC1200_XOSC;
- freq = (uint32_t)temp_freq;
- cc1200_spi_write_byte(CC120X_FREQ0, ((uint8_t *)&freq)[0]);
- cc1200_spi_write_byte(CC120X_FREQ1, ((uint8_t *)&freq)[1]);
- cc1200_spi_write_byte(CC120X_FREQ2, ((uint8_t *)&freq)[2]);
- return ;
- }
- void cc1200_radio_sleep(void)
- {
- // TODO: Write CC1200 sleep function
- return;
- }
- void cc1200_radio_power(uint8_t txPower)
- {
- // TODO: Write function to set CC1200 power
- return;
- }
- void cc1200_radio_idle(void)
- {
- // TODO: Create exception for failure condition
- while (cc1200_spi_strobe(CC120X_SIDLE) != CC120X_STATE_IDLE);
- }
- void cc1200_radio_tx(void)
- {
- // TODO: Create exception for failure condition
- while (cc1200_spi_strobe(CC120X_STX) != CC120X_STATE_TX);
- }
- void cc1200_radio_rx(void)
- {
- // TODO: Create exception for failure condition
- while (cc1200_spi_strobe(CC120X_SRX) != CC120X_STATE_RX);
- }
- void cc1200_radio_init(const cc1200_reg_settings_t* rf_settings, uint8_t len)
- {
- cc1200_gpio_init();
- cc1200_spi_init();
- spi_device_acquire_bus(spi, portMAX_DELAY);
- cc1200_radio_reset(); gpio_set_level(CC1200_RESET, 1);
- //uint8_t data;
- //cc1200_spi_read_byte(CC120X_PARTNUMBER, &data);
- //printf("%x", data);
- uint8_t i;
- for (i=0;i<len;i++)
- {
- cc1200_spi_write_byte(rf_settings[i].addr, rf_settings[i].data);
- }
- while(cc1200_spi_strobe(CC120X_SIDLE) != CC120X_STATE_IDLE);
- }
|