diff --git a/src/lmic/lmic.h b/src/lmic/lmic.h index d2153b3e..2a312fb2 100644 --- a/src/lmic/lmic.h +++ b/src/lmic/lmic.h @@ -486,6 +486,8 @@ struct lmic_radio_data_s { ostime_t txlate_ticks; // number of tx late launches. unsigned txlate_count; + // radio chip raw temperature value. + s1_t temperature; }; /* diff --git a/src/lmic/oslmic.h b/src/lmic/oslmic.h index 0aa49168..d841788e 100644 --- a/src/lmic/oslmic.h +++ b/src/lmic/oslmic.h @@ -115,6 +115,7 @@ int os_init_ex (const void *pPinMap); void os_runloop (void); void os_runloop_once (void); u1_t radio_rssi (void); +s1_t radio_raw_temp(void); void radio_monitor_rssi(ostime_t n, oslmic_radio_rssi_t *pRssi); //================================================================================ diff --git a/src/lmic/radio.c b/src/lmic/radio.c index d6cc9088..952d6219 100644 --- a/src/lmic/radio.c +++ b/src/lmic/radio.c @@ -668,9 +668,9 @@ static void configPower () { if (req_pw >= 20) { policy = LMICHAL_radio_tx_power_policy_20dBm; eff_pw = 20; - } else if (eff_pw >= 14) { + } else if (req_pw >= 14) { policy = LMICHAL_radio_tx_power_policy_paboost; - if (eff_pw > 17) { + if (req_pw > 17) { eff_pw = 17; } else { eff_pw = req_pw; @@ -872,6 +872,12 @@ static void starttx () { opmode(OPMODE_SLEEP); hal_waitUntil(os_getTime() + ms2osticks(1)); } + + // update radio chip raw temperature value + LMIC.radio.temperature = radio_raw_temp(); +#if LMIC_DEBUG_LEVEL > 0 + LMIC_DEBUG_PRINTF("RegTemp=%d\n", LMIC.radio.temperature); +#endif if (LMIC.lbt_ticks > 0) { oslmic_radio_rssi_t rssi; @@ -1176,6 +1182,49 @@ u1_t radio_rssi () { return r; } +// Reads the raw temperature +// \retval New raw temperature reading +s1_t radio_raw_temp(void) { + +#define RF_IMAGECAL_TEMPMONITOR_MASK 0xFE +#define RF_IMAGECAL_TEMPMONITOR_ON 0x00 +#define RF_IMAGECAL_TEMPMONITOR_OFF 0x01 + + s1_t RegTemp = 0; + + // select FSK modem (from sleep mode) + opmodeFSK(); + ASSERT((readReg(RegOpMode) & OPMODE_LORA) == 0); + + // Put device in FSK RxSynth + opmode(OPMODE_FSRX); + + // Enable Temperature reading + writeReg(FSKRegImageCal, (readReg(RegOpMode) & RF_IMAGECAL_TEMPMONITOR_MASK) | + RF_IMAGECAL_TEMPMONITOR_ON); + + // Wait 150us + hal_waitUntil(os_getTime() + us2osticks(150)); + + // Disable Temperature reading + writeReg(FSKRegImageCal, (readReg(RegOpMode) & RF_IMAGECAL_TEMPMONITOR_MASK) | + RF_IMAGECAL_TEMPMONITOR_OFF); + + // Put device in FSK Sleep Mode + opmode(OPMODE_SLEEP); + + // Read temperature + RegTemp = readReg(FSKRegTemp); + + if ((RegTemp & 0x80) == 0x80) { + RegTemp = 255 - RegTemp; + } else { + RegTemp *= -1; + } + + return RegTemp; +} + /// \brief get the current RSSI on the current channel. /// /// monitor rssi for specified number of ostime_t ticks, and return statistics