diff --git a/ccapp/src/main.c b/ccapp/src/main.c index 5a3b1ea..a5ef309 100644 --- a/ccapp/src/main.c +++ b/ccapp/src/main.c @@ -13,13 +13,28 @@ LOG_MODULE_REGISTER(main, CONFIG_APP_LOG_LEVEL); #error "whoops, node label radio0 not found" #endif +int select_mode(const struct device *cc2500, uint8_t mode, uint8_t volume) +{ + uint8_t data_len = 9; + uint8_t buffer[] = {0x01, 0x00, 0xa5, 0x28, 0x28, 0x00, 0x00, mode, volume}; + int i = 0; + static uint8_t accel = 0x00; + buffer[4] = accel; + buffer[3] = accel; + accel++; + for (i=0; i<20; i++) { + propy_radio_write(cc2500, buffer, data_len-2); + k_sleep(K_MSEC(50)); + propy_radio_write(cc2500, buffer, data_len); + k_sleep(K_MSEC(50)); + } + return(0); +} int main(void) { static const struct device *cc2500 = DEVICE_DT_GET(DT_NODELABEL(radio0)); - uint8_t data_len = 7; - uint8_t buffer[] = {0x01, 0x00, 0xa5, 0x28, 0x28, 0x00, 0x00}; - int i = 0; + int i; if (!device_is_ready(cc2500)) { LOG_ERR("Sensor not ready"); @@ -27,14 +42,23 @@ int main(void) } LOG_INF("Device ready"); for (i=0; i<20; i++) { - propy_radio_write(cc2500, buffer, data_len); - k_sleep(K_MSEC(20)); + LOG_INF("Mode 5, 1"); + select_mode(cc2500, 5, 1); + LOG_INF("Mode 5, 0"); + select_mode(cc2500, 5, 0); + } + for (i=0; i<20; i++) { + LOG_INF("Mode 5 level 1"); + select_mode(cc2500, 4, 1); + LOG_INF("Mode 5 level 0"); + select_mode(cc2500, 4, 0); + } + for (i=0; i<20; i++) { + LOG_INF("Mode 1"); + select_mode(cc2500, 1, 6); } - /* - while (!propy_radio_write(cc2500, buffer, data_len)) - { - k_sleep(K_MSEC(10)); - }*/ + LOG_INF("Vol 0"); + select_mode(cc2500, 0, 0); return 0; } diff --git a/drivers/cc2500/cc2500.c b/drivers/cc2500/cc2500.c index 3251a19..30f6dce 100644 --- a/drivers/cc2500/cc2500.c +++ b/drivers/cc2500/cc2500.c @@ -190,6 +190,44 @@ uint8_t cc2500_write_register_len(const struct device *dev, uint8_t reg, const u return rx_data; } +uint8_t cc2500_read_register_len(const struct device *dev, uint8_t reg, uint8_t* data, uint8_t len) +{ + const struct cc2500_config *config = dev->config; + uint8_t tx_data[SPI_MAX_MSG_LEN + 1] = {0}; + uint8_t rx_data[SPI_MAX_MSG_LEN + 1] = {0}; + int ret; + const struct spi_buf tx_buf[1] = { + { + .buf = tx_data, + .len = (len + 1) + } + }; + const struct spi_buf rx_buf[1] = { + { + .buf = &rx_data, + .len = (len + 1) + } + }; + struct spi_buf_set tx = { + .buffers = tx_buf, + .count = 1 + }; + const struct spi_buf_set rx = { + .buffers = rx_buf, + .count = 1 + }; + + tx_data[0] = ( READ_BURST | ( RW_MASK & reg ) ); + + ret = spi_transceive_dt(&config->spi, &tx, &rx); + if (ret) { + LOG_ERR("Error transceive %d", ret); + return 0; + } + memcpy(data, &rx_data[1], len); + return rx_data[0]; +} + uint8_t cc2500_cmd_register(const struct device *dev, uint8_t cmd) { const struct cc2500_config *config = dev->config; @@ -288,11 +326,17 @@ static uint8_t cc2500_set_pkt_len(const struct device *dev, uint8_t len) static void cc2500_write_register_burst(const struct device *dev, uint8_t reg, const uint8_t* data, uint8_t len) { - /* Packet length +1 because we count the reg byte */ - cc2500_set_pkt_len(dev, len+1); cc2500_write_register_len(dev, reg, data, len); } +static uint8_t cc2500_get_rssi(const struct device *dev) +{ + /* Quirk: read RSSI has to be burst read, not single byte*/ + uint8_t reg_value; + cc2500_read_register_len(dev, RSSI, ®_value, 1); + return(reg_value); +} + static bool cc2500_test_spi(const struct device *dev) { uint8_t ret = 0; @@ -309,7 +353,14 @@ static bool cc2500_test_spi(const struct device *dev) static int cc2500_read(const struct device *dev, uint8_t *buffer, uint8_t data_len) { int ret = 0; - + uint8_t status = 0; + cc2500_idle(dev); + cc2500_set_rx(dev); + /* Packet length +1 because we count the reg byte */ + cc2500_set_pkt_len(dev, data_len); + status = cc2500_read_register_len(dev, RXFIFO, buffer, data_len); + LOG_DBG("Status: 0x%x", status); + cc2500_idle(dev); return ret; } @@ -319,20 +370,24 @@ static int cc2500_write(const struct device *dev, uint8_t *buffer, uint8_t data_ int status = 0; cc2500_idle(dev); + status = cc2500_read_status(dev); cc2500_write_register_burst(dev, TXFIFO, buffer, data_len); + /* Packet length +1 because we count the reg byte */ + cc2500_set_pkt_len(dev, data_len); + cc2500_set_tx(dev); status = cc2500_read_status(dev); - LOG_DBG("Status: 0x%x", status); - k_msleep(1); + //LOG_DBG("Status: 0x%x", status); + k_usleep(800); status = cc2500_read_status(dev); - LOG_DBG("Status: 0x%x", status); + //LOG_DBG("Status: 0x%x", status); cc2500_flush_tx(dev); status = cc2500_idle(dev); - LOG_DBG("Status idle: 0x%x", status); + //LOG_DBG("Status idle: 0x%x", status); cc2500_set_rx(dev); status = cc2500_idle(dev); - LOG_DBG("Status idle: 0x%x", status); + //LOG_DBG("Status idle: 0x%x", status); return ret; } @@ -341,6 +396,58 @@ static const struct propy_radio_api cc2500_api = { .write = cc2500_write, }; +/* Init subfunction */ +static int cc2500_set_channel_process(const struct device *dev, uint8_t chann) +{ + const struct cc2500_config *config = dev->config; + uint8_t reg_value; + uint8_t status; + int i; + int ret = 0; + cc2500_idle(dev); + cc2500_idle(dev); + status = cc2500_read_status(dev); + cc2500_set_channel_num(dev, chann); + cc2500_flush_rx(dev); + cc2500_flush_tx(dev); + cc2500_idle(dev); + cc2500_set_rx(dev); + + return(ret); +} + +static int cc2500_rssi_process(const struct device *dev) +{ + const struct cc2500_config *config = dev->config; + uint8_t reg_value; + int i; + int ret = 0; + long total = 0; + const int num_mes = 45; + uint8_t current_chan = 0x01; + + for (current_chan=1; current_chan<0x09; current_chan++) { + total = 0; + cc2500_set_channel_process(dev, current_chan); + k_msleep(5); + /* Single read */ + reg_value = cc2500_read_register(dev, FSCAL1); + LOG_DBG("FS cal1: %d", reg_value); + for (i=0; iconfig; struct cc2500_data *data = dev->data; uint8_t reg_value; + uint8_t status; int i; int ret; @@ -364,7 +472,11 @@ static int cc2500_init(const struct device *dev) LOG_ERR("Could not configure CS GPIO (%d)", ret); return ret; } - cc2500_test_spi(dev); + + if (!cc2500_test_spi(dev)) { + LOG_ERR("SPI read write test failed"); + return(-EIO); + } cc2500_reset(dev); cc2500_set_config_registers(dev); @@ -373,26 +485,14 @@ static int cc2500_init(const struct device *dev) reg_value = cc2500_read_register(dev, FREQ0); LOG_DBG("Freq 0: %d", reg_value); - cc2500_write_register(dev, IOCFG2, 0x5C); - cc2500_write_register(dev, IOCFG0, 0x5B); - cc2500_flush_rx(dev); - cc2500_set_channel_num(dev, 0x01); cc2500_flush_tx(dev); cc2500_idle(dev); - cc2500_set_rx(dev); - - /* Single read */ - reg_value = cc2500_read_register(dev, FSCAL1); - LOG_DBG("FS Cal1: %d", reg_value); - - for (i=0; i<30; i++) { - reg_value = cc2500_read_register(dev, RSSI); - LOG_DBG("RSSI: %d", reg_value); - k_msleep(1); - } + cc2500_write_register(dev, IOCFG2, 0x5C); + cc2500_write_register(dev, IOCFG0, 0x5B); + cc2500_rssi_process(dev); return 0; }