From 86818e89918978878121047d71ea68df89694ddb Mon Sep 17 00:00:00 2001 From: Mateusz Lubecki Date: Sun, 29 Oct 2023 06:36:28 +0100 Subject: [PATCH] aprsis: sending gsm status to aprs-is server, powersave: different handling of cutoff event using both average and current measurements, powersave: sampling battery voltage every 10 seconds instead of 1 minute --- .../ParaMETEO-STM32L476RG.launch | 2 +- include/LedConfig.h | 16 +++ include/aprsis.h | 1 + include/io.h | 10 ++ include/pwr_save.h | 7 +- include/rte_main.h | 2 + src/LedConfig.c | 17 +++ src/aprsis.c | 40 +++++- src/main.c | 117 ++++++++++-------- src/pwr_save.c | 114 +++++------------ src/rte_main.c | 3 + system/src/aprs/telemetry.c | 2 +- 12 files changed, 190 insertions(+), 141 deletions(-) diff --git a/doc/eclipse_debugger_launch/ParaMETEO-STM32L476RG.launch b/doc/eclipse_debugger_launch/ParaMETEO-STM32L476RG.launch index cbe5fb9..a62a50b 100644 --- a/doc/eclipse_debugger_launch/ParaMETEO-STM32L476RG.launch +++ b/doc/eclipse_debugger_launch/ParaMETEO-STM32L476RG.launch @@ -58,6 +58,6 @@ - + diff --git a/include/LedConfig.h b/include/LedConfig.h index 3c91fac..7b861ef 100644 --- a/include/LedConfig.h +++ b/include/LedConfig.h @@ -35,6 +35,7 @@ extern uint8_t led_blinking_led1; void led_config(void); void led_init(void); +void led_deinit(void); void led_service_blink(void); #ifdef STM32F10X_MD_VL @@ -89,6 +90,14 @@ inline void led_blink_led2_botoom(void) { #endif #ifdef STM32L471xx + +/** + * Switches on or off upper LED in ParaTNC or bottom led + * in ParaMETEO. In ParaMETEO leds are distinguished when + * holding the PCB on longer edge towards bottom, and + * weather sensors connectors upwards. + * @param _in + */ inline void led_control_led1_upper(bool _in) { if (_in == true) { GPIOC->BSRR |= GPIO_BSRR_BS8; @@ -98,6 +107,13 @@ inline void led_control_led1_upper(bool _in) { } } +/** + * Switches on or off bottom LED in ParaTNC or upper led + * in ParaMETEO. In ParaMETEO leds are distinguished when + * holding the PCB on longer edge towards bottom, and + * weather sensors connectors upwards. + * @param _in + */ inline void led_control_led2_bottom(bool _in) { if (_in == true) { GPIOC->BSRR |= GPIO_BSRR_BS9; diff --git a/include/aprsis.h b/include/aprsis.h index 5ffd23f..396e2c6 100644 --- a/include/aprsis.h +++ b/include/aprsis.h @@ -98,6 +98,7 @@ telemetry_description_t aprsis_send_description_telemetry(uint8_t async, void aprsis_igate_to_aprsis(AX25Msg *msg, const char * callsign_with_ssid); void aprsis_send_server_conn_status(const char * callsign_with_ssid); void aprsis_send_loginstring(const char * callsign_with_ssid); +void aprsis_send_gpsstatus(const char * callsign_with_ssid); char * aprsis_get_tx_buffer(void); uint8_t aprsis_get_aprsis_logged(void); diff --git a/include/io.h b/include/io.h index 8d0d2e8..2b1b8d8 100644 --- a/include/io.h +++ b/include/io.h @@ -155,6 +155,16 @@ inline void io___cntrl_gprs_dtr_high() { GPIOB->BSRR |= GPIO_BSRR_BS8; } +inline uint8_t io_get_cntrl_vbat_g(void) { + uint8_t out = 0; + + if ((GPIOA->ODR & GPIO_ODR_ODR_6) != 0) { + out = 1; + } + + return out; +} + inline uint8_t io_get_cntrl_vbat_c(void) { uint8_t out = 0; diff --git a/include/pwr_save.h b/include/pwr_save.h index 7b926af..1709ad1 100644 --- a/include/pwr_save.h +++ b/include/pwr_save.h @@ -121,7 +121,12 @@ int pwr_save_switch_mode_to_m4a(void); void pwr_save_switch_mode_to_i5(void); void pwr_save_switch_mode_to_l6(uint16_t sleep_time); void pwr_save_switch_mode_to_l7(uint16_t sleep_time); -config_data_powersave_mode_t pwr_save_pooling_handler(const config_data_mode_t * config, const config_data_basic_t * timers, int16_t minutes_to_wx, uint16_t vbatt); // this should be called from 10 seconds pooler +config_data_powersave_mode_t pwr_save_pooling_handler( + const config_data_mode_t * config, + const config_data_basic_t * timers, + int16_t minutes_to_wx, + uint16_t vbatt_average, + uint16_t vbatt_current); // this should be called from 10 seconds pooler int pwr_save_is_currently_cutoff(void); #endif diff --git a/include/rte_main.h b/include/rte_main.h index 14b7c76..fb212d2 100644 --- a/include/rte_main.h +++ b/include/rte_main.h @@ -22,6 +22,8 @@ extern uint8_t rte_main_trigger_gsm_telemetry_values; extern uint8_t rte_main_trigger_gsm_telemetry_descriptions; +extern uint8_t rte_main_trigger_gsm_status_gsm; + //!< Trigger some reinitialization after waking up from deep sleep extern uint8_t rte_main_woken_up; diff --git a/src/LedConfig.c b/src/LedConfig.c index fbbf3ca..964a960 100644 --- a/src/LedConfig.c +++ b/src/LedConfig.c @@ -68,6 +68,23 @@ void led_init(void) { } +void led_deinit(void) { +#ifdef PARAMETEO + LL_GPIO_InitTypeDef GPIO_InitTypeDef; + + GPIO_InitTypeDef.Pin = LL_GPIO_PIN_9; + GPIO_InitTypeDef.Speed = LL_GPIO_SPEED_FREQ_MEDIUM; + GPIO_InitTypeDef.Mode = LL_GPIO_MODE_ANALOG; + GPIO_InitTypeDef.OutputType = LL_GPIO_OUTPUT_OPENDRAIN; + GPIO_InitTypeDef.Pull = LL_GPIO_PULL_NO; + + LL_GPIO_Init(GPIOC, &GPIO_InitTypeDef); + + GPIO_InitTypeDef.Pin = LL_GPIO_PIN_8; + LL_GPIO_Init(GPIOC, &GPIO_InitTypeDef); +#endif +} + void led_service_blink(void) { if (led_blinking_led1 > 0) { diff --git a/src/aprsis.c b/src/aprsis.c index c35ecaf..1e2018f 100644 --- a/src/aprsis.c +++ b/src/aprsis.c @@ -39,10 +39,12 @@ char aprsis_packet_tx_buffer[APRSIS_TX_BUFFER_LN]; /** * Lenght of buffer */ -#define APRSIS_TELEMETRY_BUFFER_LN 34 +#define APRSIS_TELEMETRY_BUFFER_LN 96 /** - * Buffer used to sent telemetry frames to APRS-IS + * Buffer used to sent telemetry frames to APRS-IS. It is also + * used to construct frame with GPS connection status, which is sent + * only once after APRS-IS connection is established */ char aprsis_packet_telemetry_buffer[APRSIS_TELEMETRY_BUFFER_LN]; @@ -300,6 +302,9 @@ aprsis_return_t aprsis_connect_and_login(const char * address, uint8_t address_l if (retval == 0) { aprsis_logged = 1; + // trigger GSM status APRS-IS packet, when connection is ready + rte_main_trigger_gsm_status_gsm = 1; + // set current timestamp as last aprsis_last_keepalive_ts = master_time; @@ -937,12 +942,41 @@ void aprsis_send_loginstring(const char * callsign_with_ssid) { aprsis_tx_counter++; - aprsis_packet_tx_message_size = snprintf(aprsis_packet_tx_buffer, APRSIS_TX_BUFFER_LN - 1, "%s>AKLPRZ,qAR,%s:>[aprsis][]%s\r\n", callsign_with_ssid, callsign_with_ssid, aprsis_login_string_reveived); + aprsis_packet_tx_message_size = snprintf( + aprsis_packet_tx_buffer, + APRSIS_TX_BUFFER_LN - 1, + "%s>AKLPRZ,qAR,%s:>[aprsis][]%s\r\n", + callsign_with_ssid, + callsign_with_ssid, + aprsis_login_string_reveived); gsm_sim800_tcpip_async_write((uint8_t *)aprsis_packet_tx_buffer, aprsis_packet_tx_message_size, aprsis_serial_port, aprsis_gsm_modem_state); } +void aprsis_send_gpsstatus(const char * callsign_with_ssid) { + if (aprsis_logged == 0) { + return; + } + + memset (aprsis_packet_tx_buffer, 0x00, APRSIS_TX_BUFFER_LN); + + aprsis_tx_counter++; + + // reuse a buffer for telemetry for this one occasion + gsm_sim800_create_status(aprsis_packet_telemetry_buffer, APRSIS_TELEMETRY_BUFFER_LN); + + aprsis_packet_tx_message_size = snprintf( + aprsis_packet_tx_buffer, + APRSIS_TX_BUFFER_LN - 1, + "%s>AKLPRZ,qAR,%s:%s\r\n", + callsign_with_ssid, + callsign_with_ssid, + aprsis_packet_telemetry_buffer); + + gsm_sim800_tcpip_async_write((uint8_t *)aprsis_packet_tx_buffer, aprsis_packet_tx_message_size, aprsis_serial_port, aprsis_gsm_modem_state); +} + char * aprsis_get_tx_buffer(void) { return aprsis_packet_tx_buffer; } diff --git a/src/main.c b/src/main.c index b243d5c..ee64dbd 100644 --- a/src/main.c +++ b/src/main.c @@ -148,14 +148,6 @@ // ----- main() --------------------------------------------------------------- -// Sample pragmas to cope with warnings. Please note the related line at -// the end of this function, used to pop the compiler diagnostics status. -//#pragma GCC diagnostic push -//#pragma GCC diagnostic ignored "-Wunused-parameter" -//#pragma GCC diagnostic ignored "-Wmissing-declarations" -//#pragma GCC diagnostic ignored "-Wreturn-type" -//#pragma GCC diagnostic ignored "-Wempty-body" - // used configuration structures const config_data_mode_t * main_config_data_mode = 0; const config_data_basic_t * main_config_data_basic = 0; @@ -411,7 +403,7 @@ int main(int argc, char* argv[]){ // restore config to default if requested if (main_reset_config_to_default == 1) { - main_crc_result = 0; + main_crc_result = 0; backup_reg_reset_counters(); @@ -800,6 +792,7 @@ int main(int argc, char* argv[]){ main_wx_srl_ctx_ptr->te_port = GPIOA; main_wx_srl_ctx_ptr->early_tx_assert = configuration_get_early_tx_assert(); // TODO: was 1 + // initialize UART used to communicate with GPRS modem srl_init(main_gsm_srl_ctx_ptr, USART3, srl_usart3_rx_buffer, RX_BUFFER_3_LN, srl_usart3_tx_buffer, TX_BUFFER_3_LN, 115200, 1); #endif @@ -1067,14 +1060,17 @@ int main(int argc, char* argv[]){ // reload watchdog counter main_reload_internal_wdg(); + // reload external watchdog in case it is installed io_ext_watchdog_service(); #ifdef PARAMETEO + // initialize NVM logger if it is enabled if (main_config_data_mode->nvm_logger != 0) { nvm_measurement_init(); } + // initialize everything related to GSM module if (main_config_data_mode->gsm == 1) { it_handlers_inhibit_radiomodem_dcd_led = 1; @@ -1084,6 +1080,9 @@ int main(int argc, char* argv[]){ http_client_init(&main_gsm_state, main_gsm_srl_ctx_ptr, 0); + // as for now it is not possible to have APRS-IS communciation and REST api at once, + // due to some data races and another timing problems while disconnecting APRS-IS to make + // room for HTTP request - hence that if below if (main_config_data_gsm->api_enable == 1 && main_config_data_gsm->aprsis_enable == 0) { api_init((const char *)main_config_data_gsm->api_base_url, (const char *)(main_config_data_gsm->api_station_name)); } @@ -1110,9 +1109,14 @@ int main(int argc, char* argv[]){ if (main_config_data_basic-> beacon_at_bootup == 1) { #if defined(PARAMETEO) - beacon_send_own(rte_main_battery_voltage, system_is_rtc_ok()); - main_wait_for_tx_complete(); - delay_fixed(1500); + beacon_send_own(rte_main_battery_voltage, system_is_rtc_ok()); + main_wait_for_tx_complete(); + + // this delay is put in case if beacon is configured to use + // any path like WIDE1-1 or WIDE2-1 or another. The delay + // will wait for some time to have this beacon digipeated + // by the APRS radio network + delay_fixed(1500); #else beacon_send_own(0, 0); @@ -1130,17 +1134,18 @@ int main(int argc, char* argv[]){ // Infinite loop while (1) { - backup_reg_set_monitor(-1); + backup_reg_set_monitor(-1); - // incrementing current cpu ticks - main_current_cpu_idle_ticks++; + // incrementing current cpu ticks + main_current_cpu_idle_ticks++; - if (rte_main_reboot_req == 1) { - NVIC_SystemReset(); - } - else { - ; - } + // system reset may be requested from various places in the application + if (rte_main_reboot_req == 1) { + NVIC_SystemReset(); + } + else { + ; + } backup_reg_set_monitor(0); @@ -1150,6 +1155,7 @@ int main(int argc, char* argv[]){ // restart ADCs io_vbat_meas_enable(); + // get current battery voltage and calculate current average rte_main_battery_voltage = io_vbat_meas_get(); rte_main_average_battery_voltage = io_vbat_meas_average(rte_main_battery_voltage); @@ -1164,16 +1170,19 @@ int main(int argc, char* argv[]){ main_ax25.dcd = false; //DA_Init(); - ADCStartConfig(); - DACStartConfig(); - AFSK_Init(&main_afsk); - ax25_init(&main_ax25, &main_afsk, 0, message_callback, 0); - //TimerConfig(); + ADCStartConfig(); + DACStartConfig(); + AFSK_Init(&main_afsk); + ax25_init(&main_ax25, &main_afsk, 0, message_callback, 0); + //TimerConfig(); - rte_main_woken_up = 0; + rte_main_woken_up = 0; main_check_adc = 1; + // reinitialize UART used to communicate with GPRS modem + srl_init(main_gsm_srl_ctx_ptr, USART3, srl_usart3_rx_buffer, RX_BUFFER_3_LN, srl_usart3_tx_buffer, TX_BUFFER_3_LN, 115200, 1); + backup_reg_set_monitor(1); } #endif @@ -1216,7 +1225,7 @@ int main(int argc, char* argv[]){ #ifdef PARAMETEO // if GSM communication is enabled - if (main_config_data_mode->gsm == 1) { + if (main_config_data_mode->gsm == 1 && io_get_cntrl_vbat_g() == 1) { // if data has been received if (main_gsm_srl_ctx_ptr->srl_rx_state == SRL_RX_DONE || main_gsm_srl_ctx_ptr->srl_rx_state == SRL_RX_ERROR) { @@ -1231,6 +1240,12 @@ int main(int argc, char* argv[]){ gsm_sim800_tx_done_event_handler(main_gsm_srl_ctx_ptr, &main_gsm_state); } + if (rte_main_trigger_gsm_status_gsm == 1 && gsm_sim800_tcpip_tx_busy() == 0) { + rte_main_trigger_gsm_status_gsm = 0; + + aprsis_send_gpsstatus((const char *)&main_callsign_with_ssid); + } + // if GSM status message is triggered and GSM module is not busy transmitting something else if (rte_main_trigger_gsm_status_packet == 1 && gsm_sim800_tcpip_tx_busy() == 0) { rte_main_trigger_gsm_status_packet = 0; @@ -1396,18 +1411,6 @@ int main(int argc, char* argv[]){ // get all meteo measuremenets each 65 seconds. some values may not be // downloaded from sensors if _METEO and/or _DALLAS_AS_TELEM aren't defined if (main_wx_sensors_pool_timer < 10) { -#ifdef PARAMETEO - // get current battery voltage. for non parameteo this will return 0 - rte_main_battery_voltage = io_vbat_meas_get(); - - // get average battery voltage - rte_main_average_battery_voltage = io_vbat_meas_average(rte_main_battery_voltage); - - // meas average will return 0 if internal buffer isn't filled completely - if (rte_main_average_battery_voltage == 0) { - rte_main_average_battery_voltage = rte_main_battery_voltage; - } -#endif if ((main_config_data_mode->wx & WX_ENABLED) == 1) { // notice: UMB-master and Modbus-RTU uses the same UART @@ -1505,7 +1508,14 @@ int main(int argc, char* argv[]){ button_debounce(); #ifdef PARAMETEO + // this if cannot be guarded by checking if VBAT_G is enabled + // because VBAT_G itself is controlled by initialization + // pooler if (main_config_data_mode->gsm == 1) { + gsm_sim800_initialization_pool(main_gsm_srl_ctx_ptr, &main_gsm_state); + } + + if (main_config_data_mode->gsm == 1 && io_get_cntrl_vbat_g() == 1) { // check if GSM modem must be power-cycled / restarted like after // waking up from deep sleep or chaning power saving mode @@ -1538,9 +1548,6 @@ int main(int argc, char* argv[]){ //retval = http_client_async_post("http://pogoda.cc:8080/meteo_backend/parameteo/skrzyczne/status", strlen("http://pogoda.cc:8080/meteo_backend/parameteo/skrzyczne/status"), post_content, strlen(post_content), 0, dupa); } - - gsm_sim800_initialization_pool(main_gsm_srl_ctx_ptr, &main_gsm_state); - gsm_sim800_poolers_one_second(main_gsm_srl_ctx_ptr, &main_gsm_state, main_config_data_gsm); aprsis_check_alive(); @@ -1604,16 +1611,28 @@ int main(int argc, char* argv[]){ rte_main_trigger_wx_packet = 0; } - #ifdef PARAMETEO +#ifdef PARAMETEO + if (main_check_adc == 1) { AD_Restart(); main_check_adc = 0; } + // get current battery voltage. for non parameteo this will return 0 + rte_main_battery_voltage = io_vbat_meas_get(); + + // get average battery voltage + rte_main_average_battery_voltage = io_vbat_meas_average(rte_main_battery_voltage); + + // meas average will return 0 if internal buffer isn't filled completely + if (rte_main_average_battery_voltage == 0) { + rte_main_average_battery_voltage = rte_main_battery_voltage; + } + // inhibit any power save switching when modem transmits data if (!main_afsk.sending && rte_main_woken_up == 0) { - pwr_save_pooling_handler(main_config_data_mode, main_config_data_basic, packet_tx_get_minutes_to_next_wx(), rte_main_average_battery_voltage); + pwr_save_pooling_handler(main_config_data_mode, main_config_data_basic, packet_tx_get_minutes_to_next_wx(), rte_main_average_battery_voltage, rte_main_battery_voltage); } if ((main_config_data_mode->wx_dust_sensor & WX_DUST_SDS011_PWM) > 0) { @@ -1627,17 +1646,17 @@ int main(int argc, char* argv[]){ if (pwm_second_channel != 0) { rte_wx_pm10 = pwm_second_channel; } - #endif +#endif backup_reg_set_monitor(9); - #ifdef PARAMETEO - if (main_config_data_mode->gsm == 1) { +#ifdef PARAMETEO + if (main_config_data_mode->gsm == 1 && io_get_cntrl_vbat_g() == 1) { gsm_sim800_poolers_ten_seconds(main_gsm_srl_ctx_ptr, &main_gsm_state); packet_tx_tcp_handler(); } - #endif +#endif if (main_config_data_mode->wx_umb == 1) { umb_channel_pool(&rte_wx_umb, &rte_wx_umb_context, main_config_data_umb); diff --git a/src/pwr_save.c b/src/pwr_save.c index b0e38e1..65652d2 100644 --- a/src/pwr_save.c +++ b/src/pwr_save.c @@ -23,6 +23,7 @@ #include "afsk_pr.h" #include "gsm/sim800c.h" #include "aprsis.h" +#include "it_handlers.h" #include "rte_main.h" @@ -92,7 +93,6 @@ static void pwr_save_clear_powersave_idication_bits() { pwr_save_unclock_rtc_backup_regs(); // clear all previous powersave indication bits - //REGISTER &= (0xFFFFFFFF ^ ALL_STATES_BITMASK); backup_reg_reset_all_powersave_states(); // lock access to backup @@ -133,7 +133,6 @@ static void pwr_save_enter_stop2(void) { RTC->BKP0R |= IN_STOP2_MODE; // save a timestamp when micro has been switched to STOP2 mode - //REGISTER_LAST_SLEEP = RTC->TR; backup_reg_set_last_sleep_timestamp(); pwr_save_lock_rtc_backup_regs(); @@ -199,7 +198,6 @@ static void pwr_save_exit_after_last_stop2_cycle(void) { backup_reg_set_last_wakeup_timestamp(); // increase wakeup counter - //counter = (uint32_t)((REGISTER_COUNTERS & 0xFFFF0000) >> 16); counter = backup_reg_get_wakeup_counter(); counter++; @@ -212,7 +210,6 @@ static void pwr_save_exit_after_last_stop2_cycle(void) { counter = 0; } - //REGISTER_COUNTERS = (REGISTER_COUNTERS & 0x0000FFFF) | (counter << 16); backup_reg_set_wakeup_counter(counter); pwr_save_lock_rtc_backup_regs(); @@ -261,12 +258,20 @@ static void pwr_save_exit_after_last_stop2_cycle(void) { break; } + // reinitialize LEDs when controller goes out from sleep + it_handlers_inhibit_radiomodem_dcd_led = 0; + led_init(); + backup_reg_set_monitor(29); } /** - * This function is called in two places within a pooler, just after the micro - * wakes up from STOP2 deep slepp and returns from RTC interrupt handler + * This function is called in two places within a pooler. + * 1st: just after the micro wakes up from STOP2 deep sleep caused by low battery + * voltage and returns from an interrupt + * from RTC interrupt handler. + * 2nd: just after the micro wakes up from STOP2 caues by aggressive powersave + * configuration. */ static void pwr_save_after_stop2_rtc_wakeup_it(void) { // check if we are just after waking up from STOP2 mode @@ -315,11 +320,9 @@ int pwr_save_switch_mode_to_c0(void) { pwr_save_unclock_rtc_backup_regs(); // clear all previous powersave indication bits - //REGISTER &= 0xFFFFFFFF ^ ALL_STATES_BITMASK; backup_reg_reset_all_powersave_states(); // set for C0 mode - //REGISTER |= IN_C0_STATE; backup_reg_set_powersave_state(IN_C0_STATE); // lock access to backup @@ -332,9 +335,6 @@ int pwr_save_switch_mode_to_c0(void) { // in HW-RevB this will disable external VHF radio!! int pwr_save_switch_mode_to_c1(void) { -// if ((REGISTER & ALL_STATES_BITMASK) == IN_C1_STATE) { -// return 0; -// } if (backup_reg_is_in_powersave_state(IN_C0_STATE) != 0) { return 0; } @@ -363,11 +363,9 @@ int pwr_save_switch_mode_to_c1(void) { pwr_save_unclock_rtc_backup_regs(); // clear all previous powersave indication bits - //REGISTER &= (0xFFFFFFFF ^ ALL_STATES_BITMASK); backup_reg_reset_all_powersave_states(); // set for C0 mode - //REGISTER |= IN_C1_STATE; backup_reg_set_powersave_state(IN_C1_STATE); // lock access to backup @@ -381,9 +379,6 @@ int pwr_save_switch_mode_to_c1(void) { // line which controls +4V_G void pwr_save_switch_mode_to_c2(void) { -// if ((REGISTER & ALL_STATES_BITMASK) == IN_C2_STATE) { -// return; -// } if (backup_reg_is_in_powersave_state(IN_C0_STATE) != 0) { return; } @@ -412,11 +407,9 @@ void pwr_save_switch_mode_to_c2(void) { pwr_save_unclock_rtc_backup_regs(); // clear all previous powersave indication bits - //REGISTER &= (0xFFFFFFFF ^ ALL_STATES_BITMASK); backup_reg_reset_all_powersave_states(); // set for C2 mode - //REGISTER |= IN_C2_STATE; backup_reg_set_powersave_state(IN_C2_STATE); // lock access to backup @@ -426,9 +419,6 @@ void pwr_save_switch_mode_to_c2(void) { void pwr_save_switch_mode_to_c3(void) { -// if ((REGISTER & ALL_STATES_BITMASK) == IN_C3_STATE) { -// return; -// } if (backup_reg_is_in_powersave_state(IN_C3_STATE) != 0) { return; } @@ -454,11 +444,9 @@ void pwr_save_switch_mode_to_c3(void) { pwr_save_unclock_rtc_backup_regs(); // clear all previous powersave indication bits - //REGISTER &= (0xFFFFFFFF ^ ALL_STATES_BITMASK); backup_reg_reset_all_powersave_states(); // set for C3 mode - //REGISTER |= IN_C3_STATE; backup_reg_set_powersave_state(IN_C3_STATE); // lock access to backup @@ -469,9 +457,6 @@ void pwr_save_switch_mode_to_c3(void) { // in HW-RevB this will keep internal VHF radio module working! int pwr_save_switch_mode_to_m4(void) { -// if ((REGISTER & ALL_STATES_BITMASK) == IN_M4_STATE) { -// return 0; -// } if (backup_reg_is_in_powersave_state(IN_M4_STATE) != 0) { return 0; } @@ -500,11 +485,9 @@ int pwr_save_switch_mode_to_m4(void) { pwr_save_unclock_rtc_backup_regs(); // clear all previous powersave indication bits - //REGISTER &= (0xFFFFFFFF ^ ALL_STATES_BITMASK); backup_reg_reset_all_powersave_states(); // set for C3 mode - //REGISTER |= IN_M4_STATE; backup_reg_set_powersave_state(IN_M4_STATE); // lock access to backup @@ -514,9 +497,7 @@ int pwr_save_switch_mode_to_m4(void) { } int pwr_save_switch_mode_to_m4a(void) { -// if ((REGISTER & ALL_STATES_BITMASK) == IN_M4_STATE) { -// return 0; -// } + if (backup_reg_is_in_powersave_state(IN_M4_STATE) != 0) { return 0; } @@ -542,11 +523,9 @@ int pwr_save_switch_mode_to_m4a(void) { pwr_save_unclock_rtc_backup_regs(); // clear all previous powersave indication bits - //REGISTER &= (0xFFFFFFFF ^ ALL_STATES_BITMASK); backup_reg_reset_all_powersave_states(); // set for C3 mode - //REGISTER |= IN_M4_STATE; backup_reg_set_powersave_state(IN_M4_STATE); // lock access to backup @@ -557,9 +536,6 @@ int pwr_save_switch_mode_to_m4a(void) { void pwr_save_switch_mode_to_i5(void) { -// if ((REGISTER & ALL_STATES_BITMASK) == IN_I5_STATE) { -// return; -// } if (backup_reg_is_in_powersave_state(IN_I5_STATE) != 0) { return; } @@ -588,11 +564,9 @@ void pwr_save_switch_mode_to_i5(void) { pwr_save_unclock_rtc_backup_regs(); // clear all previous powersave indication bits - //REGISTER &= (0xFFFFFFFF ^ ALL_STATES_BITMASK); backup_reg_reset_all_powersave_states(); // set for C3 mode - //REGISTER |= IN_I5_STATE; backup_reg_set_powersave_state(IN_I5_STATE); // lock access to backup @@ -616,9 +590,6 @@ void pwr_save_switch_mode_to_l6(uint16_t sleep_time) { return; } -// if ((REGISTER & ALL_STATES_BITMASK) == IN_L6_STATE) { -// return; -// } if (backup_reg_is_in_powersave_state(IN_L6_STATE) != 0) { return; } @@ -656,14 +627,11 @@ void pwr_save_switch_mode_to_l6(uint16_t sleep_time) { pwr_save_unclock_rtc_backup_regs(); // clear all previous powersave indication bits - //REGISTER &= (0xFFFFFFFF ^ ALL_STATES_BITMASK); backup_reg_reset_all_powersave_states(); // set for C3 mode - //REGISTER |= IN_L6_STATE; backup_reg_set_powersave_state(IN_L6_STATE); - //REGISTER_LAST_SLTIM = sleep_time; backup_reg_set_last_sleep_timestamp(); // increment the STOP2 sleep counters @@ -673,7 +641,6 @@ void pwr_save_switch_mode_to_l6(uint16_t sleep_time) { rte_main_going_sleep_count = counter; - //REGISTER_COUNTERS = (REGISTER_COUNTERS & 0xFFFF0000) | counter; backup_reg_set_sleep_counter(counter); // lock access to backup @@ -685,8 +652,10 @@ void pwr_save_switch_mode_to_l6(uint16_t sleep_time) { pwr_save_sleep_time_in_seconds = sleep_time; // turn off leds to save power + it_handlers_inhibit_radiomodem_dcd_led = 1; led_control_led1_upper(false); led_control_led2_bottom(false); + led_deinit(); pwr_save_enter_stop2(); @@ -711,9 +680,6 @@ void pwr_save_switch_mode_to_l7(uint16_t sleep_time) { return; } -// if ((REGISTER & ALL_STATES_BITMASK) == IN_L7_STATE) { -// return; -// } if (backup_reg_is_in_powersave_state(IN_L7_STATE) != 0) { return; } @@ -750,28 +716,12 @@ void pwr_save_switch_mode_to_l7(uint16_t sleep_time) { // inhibit GSM modem gsm_sim800_inhibit(1); - // unlock access to backup registers - //pwr_save_unclock_rtc_backup_regs(); - // clear all previous powersave indication bits - //REGISTER &= (0xFFFFFFFF ^ ALL_STATES_BITMASK); backup_reg_reset_all_powersave_states(); // set for C3 mode - //REGISTER |= IN_L7_STATE; backup_reg_set_powersave_state(IN_L7_STATE); -// REGISTER_LAST_SLTIM = sleep_time; -// -// // increment the STOP2 sleep counters -// counter = (uint16_t)(REGISTER_COUNTERS & 0xFFFF); -// -// counter++; -// -// rte_main_going_sleep_count = counter; -// -// REGISTER_COUNTERS = (REGISTER_COUNTERS & 0xFFFF0000) | counter; - //REGISTER_LAST_SLTIM = sleep_time; backup_reg_set_last_sleep_timestamp(); @@ -782,12 +732,8 @@ void pwr_save_switch_mode_to_l7(uint16_t sleep_time) { rte_main_going_sleep_count = counter; - //REGISTER_COUNTERS = (REGISTER_COUNTERS & 0xFFFF0000) | counter; backup_reg_set_sleep_counter(counter); - // lock access to backup - //pwr_save_lock_rtc_backup_regs(); - // configure how long micro should sleep system_clock_configure_auto_wakeup_l4(PWR_SAVE_STOP2_CYCLE_LENGHT_SEC); @@ -795,8 +741,10 @@ void pwr_save_switch_mode_to_l7(uint16_t sleep_time) { pwr_save_sleep_time_in_seconds = sleep_time; // turn off leds to save power + it_handlers_inhibit_radiomodem_dcd_led = 1; led_control_led1_upper(false); led_control_led2_bottom(false); + led_deinit(); pwr_save_enter_stop2(); @@ -854,7 +802,6 @@ void pwr_save_init(config_data_powersave_mode_t mode) { //pwr_save_unclock_rtc_backup_regs(); // reset a status register - //REGISTER = 0; backup_reg_reset_all_powersave_states(); backup_reg_reset_inhibit_periodic_pwr_switch(); @@ -864,16 +811,18 @@ void pwr_save_init(config_data_powersave_mode_t mode) { break; case PWSAVE_NORMAL: case PWSAVE_AGGRESV: - //REGISTER |= INHIBIT_PWR_SWITCH_PERIODIC_H; backup_reg_inhibit_periodic_pwr_switch(); break; } - //pwr_save_lock_rtc_backup_regs(); } -config_data_powersave_mode_t pwr_save_pooling_handler(const config_data_mode_t * config, const config_data_basic_t * timers, int16_t minutes_to_wx, uint16_t vbatt) { +config_data_powersave_mode_t pwr_save_pooling_handler( const config_data_mode_t * config, + const config_data_basic_t * timers, + int16_t minutes_to_wx, + uint16_t vbatt_average, + uint16_t vbatt_current) { // this function should be called from 10 seconds pooler int reinit_sensors = 0; @@ -889,31 +838,30 @@ config_data_powersave_mode_t pwr_save_pooling_handler(const config_data_mode_t * pwr_save_previously_cutoff = pwr_save_currently_cutoff; // check if battery voltage measurement is done and senseful - if (vbatt < MINIMUM_SENSEFUL_VBATT_VOLTAGE) { + if (vbatt_average < MINIMUM_SENSEFUL_VBATT_VOLTAGE) { // inhibit both cutoff and aggresive powersave if vbatt measurement is either not // done at all or scaling factor are really screwed - vbatt = 0xFFFFu; + vbatt_average = 0xFFFFu; } #ifdef INHIBIT_CUTOFF - vbatt = 0xFFFFu; // TODO:: THis shall not be uncommented on production!!! + vbatt_average = 0xFFFFu; // TODO:: THis shall not be uncommented on production!!! #endif - if (vbatt > PWR_SAVE_STARTUP_RESTORE_VOLTAGE_DEF) { + if (vbatt_average > PWR_SAVE_STARTUP_RESTORE_VOLTAGE_DEF) { pwr_save_currently_cutoff = 0; backup_reg_set_monitor(23); } else { - if (vbatt <= PWR_SAVE_CUTOFF_VOLTAGE_DEF) { + if (vbatt_current <= PWR_SAVE_CUTOFF_VOLTAGE_DEF && vbatt_average <= PWR_SAVE_AGGRESIVE_POWERSAVE_VOLTAGE) { backup_reg_set_monitor(22); // if the battery voltage is below cutoff level and the ParaMETEO controller is currently not cut off pwr_save_currently_cutoff |= CURRENTLY_CUTOFF; } - // check if battery voltage is below low voltage level - if (vbatt <= PWR_SAVE_AGGRESIVE_POWERSAVE_VOLTAGE) { + else if (vbatt_average <= PWR_SAVE_AGGRESIVE_POWERSAVE_VOLTAGE) { backup_reg_set_monitor(21); // if battery voltage is low swtich to aggressive powersave mode @@ -927,7 +875,7 @@ config_data_powersave_mode_t pwr_save_pooling_handler(const config_data_mode_t * // check if cutoff status has changed if (pwr_save_currently_cutoff != pwr_save_previously_cutoff) { - status_send_powersave_cutoff(vbatt, pwr_save_previously_cutoff, pwr_save_currently_cutoff); + status_send_powersave_cutoff(vbatt_average, pwr_save_previously_cutoff, pwr_save_currently_cutoff); } @@ -1221,12 +1169,6 @@ config_data_powersave_mode_t pwr_save_pooling_handler(const config_data_mode_t * // reinitialize everything realted to anemometer analog_anemometer_init(main_config_data_mode->wx_anemometer_pulses_constant, 38, 100, 1); - -// // reset anemometer direction handler -// analog_anemometer_direction_reset(); -// -// // reset anemometer windspeed handler -// analog_anemometer_timer_irq(); } return psave_mode; diff --git a/src/rte_main.c b/src/rte_main.c index 4992ca2..b956675 100644 --- a/src/rte_main.c +++ b/src/rte_main.c @@ -30,6 +30,9 @@ uint8_t rte_main_trigger_gsm_telemetry_values = 0; //!< Trigger set of packets with telemetry description uint8_t rte_main_trigger_gsm_telemetry_descriptions = 0; +//!< +uint8_t rte_main_trigger_gsm_status_gsm = 0; + //!< Trigger some reinitialization after waking up from deep sleep uint8_t rte_main_woken_up = 0; diff --git a/system/src/aprs/telemetry.c b/system/src/aprs/telemetry.c index fe475d1..c566c6a 100644 --- a/system/src/aprs/telemetry.c +++ b/system/src/aprs/telemetry.c @@ -521,7 +521,7 @@ void telemetry_send_values( uint8_t rx_pkts, // reset the buffer where the frame will be contructed and stored for transmission memset(main_own_aprs_msg, 0x00, sizeof(main_own_aprs_msg)); -#ifdef STM32L471xx +#ifdef PARAMETEO if (config_mode->digi_viscous == 0) { // generate the telemetry frame from values main_own_aprs_msg_len = sprintf(main_own_aprs_msg, "T#%03d,%03d,%03d,%03d,%03d,%03d,%c%c%c%c%c%c%c%c", telemetry_counter++, rx_pkts, tx_pkts, digi_pkts, scaled_vbatt_voltage, telemetry_scaled_temperature, telemetry_qf, telemetry_degr, telemetry_nav, telemetry_pressure_qf_navaliable, telemetry_humidity_qf_navaliable, telemetry_anemometer_degradated, telemetry_anemometer_navble, telemetry_vbatt_low);