diff --git a/platform/drivers/baseband/radio_ttwrplus.cpp b/platform/drivers/baseband/radio_ttwrplus.cpp index bfaa12f1..7ee58bcf 100644 --- a/platform/drivers/baseband/radio_ttwrplus.cpp +++ b/platform/drivers/baseband/radio_ttwrplus.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include "AT1846S.h" #include "radioUtils.h" @@ -118,14 +119,14 @@ void radio_uartPrint(const char *fmt, ...) void radio_uartScan(char *buf) { - k_msgq_get(&uart_msgq, buf, K_FOREVER); + k_msgq_get(&uart_msgq, buf, K_MSEC(100)); } char *radio_getFwVersion() { char *tx_buf = (char *) malloc(sizeof(char) * SA8X8_MSG_SIZE); radio_uartPrint("AT+VERSION\r\n"); - k_msgq_get(&uart_msgq, tx_buf, K_FOREVER); + k_msgq_get(&uart_msgq, tx_buf, K_MSEC(100)); return tx_buf; } @@ -133,10 +134,35 @@ char *radio_getModel() { char *tx_buf = (char *) malloc(sizeof(char) * SA8X8_MSG_SIZE); radio_uartPrint("AT+MODEL\r\n"); - k_msgq_get(&uart_msgq, tx_buf, K_FOREVER); + k_msgq_get(&uart_msgq, tx_buf, K_MSEC(100)); return tx_buf; } +enum Band radio_getBand() +{ + enum Band band = BND_NONE; + char *tx_buf = radio_getModel(); + if (!strncmp(tx_buf, "SA868S-VHF\r", SA8X8_MSG_SIZE)) + band = BND_VHF; + else if (!strncmp(tx_buf, "SA868S-UHF\r", SA8X8_MSG_SIZE)) + band = BND_UHF; + free(tx_buf); + return band; +} + +enum Band radio_waitUntilReady() +{ + bool ready = false; + enum Band band = BND_NONE; + do + { + band = radio_getBand(); + if (band != BND_NONE) + ready = true; + } while(!ready); + return band; +} + void radio_enableTurbo() { int ret = 0; @@ -151,7 +177,7 @@ void radio_enableTurbo() radio_uartPrint("AT+TURBO\r\n"); char *tx_buf = (char *) malloc(sizeof(char) * SA8X8_MSG_SIZE); - ret = k_msgq_get(&uart_msgq, tx_buf, K_FOREVER); + ret = k_msgq_get(&uart_msgq, tx_buf, K_MSEC(100)); if (ret) { printk("Error: in retrieving turbo response!\n"); return; @@ -172,6 +198,9 @@ void radio_init(const rtxStatus_t *rtxState) radioStatus = OFF; int ret; + // Turn on baseband + pmu_setBasebandPower(true); + // Initialize GPIO for SA868S power down if (!gpio_is_ready_dt(&radio_pdn)) { printk("Error: radio device %s is not ready\n", @@ -204,20 +233,21 @@ void radio_init(const rtxStatus_t *rtxState) uart_irq_rx_enable(radio_dev); - ret = gpio_pin_toggle_dt(&radio_pdn); + // Reset the SA868S baseband + ret = gpio_pin_set_dt(&radio_pdn, 1); if (ret != 0) { printk("Failed to reset baseband"); return; } - delayMs(200); + delayMs(10); ret = gpio_pin_set_dt(&radio_pdn, 0); if (ret != 0) { printk("Failed to reset baseband"); return; } - // A small delay is needed to have SA8x8 ready to serve commands - delayMs(100); + // Wait until SA868 is responsive + radio_waitUntilReady(); // Check for minimum supported firmware version. char *fwVersionStr = radio_getFwVersion(); diff --git a/platform/targets/ttwrplus/pmu.cpp b/platform/targets/ttwrplus/pmu.cpp index cc7801ff..930599ff 100644 --- a/platform/targets/ttwrplus/pmu.cpp +++ b/platform/targets/ttwrplus/pmu.cpp @@ -152,7 +152,7 @@ void pmu_init() PMU.enableBLDO1(); //! DC3 Radio & Pixels VDD - PMU.enableDC3(); + PMU.disableDC3(); // power off when not in use PMU.disableDC2(); @@ -235,7 +235,7 @@ void pmu_init() - XPOWERS_CHG_LED_ON, - XPOWERS_CHG_LED_CTRL_CHG, * */ - PMU.setChargingLedMode(XPOWERS_CHG_LED_BLINK_1HZ); + PMU.setChargingLedMode(XPOWERS_CHG_LED_CTRL_CHG); // TODO: Implement IRQ // pinMode(PMU_IRQ, INPUT_PULLUP); @@ -297,3 +297,11 @@ uint16_t pmu_getVbat() { return PMU.isBatteryConnect() ? PMU.getBattVoltage() : 0; } + +void pmu_setBasebandPower(bool enable) +{ + if (enable) + PMU.enableDC3(); + else + PMU.disableDC3(); +} diff --git a/platform/targets/ttwrplus/pmu.h b/platform/targets/ttwrplus/pmu.h index 71755b57..fbe43ae1 100644 --- a/platform/targets/ttwrplus/pmu.h +++ b/platform/targets/ttwrplus/pmu.h @@ -26,6 +26,7 @@ extern "C" { void pmu_init(); uint16_t pmu_getVbat(); +void pmu_setBasebandPower(bool enable); #ifdef __cplusplus }