diff --git a/include/config_data.h b/include/config_data.h index cbf54ba..b956050 100644 --- a/include/config_data.h +++ b/include/config_data.h @@ -21,6 +21,8 @@ typedef struct config_data_mode_t { #define WX_INTERNAL_AS_BACKUP (1 << 1) #define WX_INTERNAL_SPARKFUN_WIND (1 << 2) +#define WX_MODBUS_DEBUG (1 << 1) + uint8_t digi; uint8_t wx; @@ -31,7 +33,7 @@ typedef struct config_data_mode_t { uint8_t wx_davis; - uint8_t wx_ms5611_or_bme; + uint8_t wx_ms5611_or_bme; // set to one to choose bme, zero to ms5611 uint8_t victron; diff --git a/src/it_handlers.c b/src/it_handlers.c index abe55de..8e68f07 100644 --- a/src/it_handlers.c +++ b/src/it_handlers.c @@ -156,15 +156,9 @@ void TIM4_IRQHandler( void ) { DAC->DHR8R1 = AFSK_DAC_ISR(&main_afsk); DAC->SWTRIGR |= 1; -#ifndef _METEO - led_control_led2_bottom(main_afsk.sending); -#endif -// if (main_afsk.sending) { -// GPIO_SetBits(GPIOC, GPIO_Pin_9); -// } -// else { -// GPIO_ResetBits(GPIOC, GPIO_Pin_9); -// } + if ((main_config_data_mode->wx & WX_ENABLED) == 0) { + led_control_led2_bottom(main_afsk.sending); + } } diff --git a/src/main.c b/src/main.c index 0038892..1a168a1 100644 --- a/src/main.c +++ b/src/main.c @@ -38,7 +38,6 @@ #include "rte_main.h" #include "rte_rtu.h" -#ifdef _METEO #include #include "drivers/dallas.h" #include "drivers/i2c.h" @@ -50,23 +49,14 @@ #include "../system/include/davis_vantage/davis.h" #include "../system/include/davis_vantage/davis_parsers.h" -#ifdef _SENSOR_MS5611 #include "drivers/ms5611.h" -#endif - -#ifdef _SENSOR_BME280 #include -#endif #include "umb_master/umb_master.h" #include "umb_master/umb_channel_pool.h" #include "umb_master/umb_0x26_status.h" -#endif // _METEO - -#ifdef _DALLAS_AS_TELEM #include "drivers/dallas.h" -#endif #include "KissCommunication.h" @@ -437,58 +427,58 @@ int main(int argc, char* argv[]){ } -#if (defined(PARATNC_HWREV_B) || defined(PARATNC_HWREV_C)) && defined(_DAVIS_SERIAL) - // reinitialize the KISS serial port temporary to davis baudrate - main_target_kiss_baudrate = DAVIS_DEFAULT_BAUDRATE; + if (main_config_data_mode->wx_davis == 1) { + // reinitialize the KISS serial port temporary to davis baudrate + main_target_kiss_baudrate = DAVIS_DEFAULT_BAUDRATE; - // reset RX state to allow reinitialization with changed baudrate - main_kiss_srl_ctx_ptr->srl_rx_state = SRL_RX_NOT_CONFIG; - - // reinitializing serial hardware to wake up Davis wx station - srl_init(main_kiss_srl_ctx_ptr, USART1, srl_usart1_rx_buffer, RX_BUFFER_1_LN, srl_usart1_tx_buffer, TX_BUFFER_1_LN, main_target_kiss_baudrate, 1); - - srl_switch_timeout(main_kiss_srl_ctx_ptr, SRL_TIMEOUT_ENABLE, 3000); - - davis_init(main_kiss_srl_ctx_ptr); - - // try to wake up the davis base - rte_wx_davis_station_avaliable = (davis_wake_up(DAVIS_BLOCKING_IO) == 0 ? 1 : 0); - - // if davis weather stations is connected to SERIAL port - if (rte_wx_davis_station_avaliable == 1) { - // turn LCD backlight on.. - davis_control_backlight(1); - - // wait for a while - delay_fixed(1000); - - // and then off to let the user know that communication is working - davis_control_backlight(0); - - // disable the KISS modem as the UART will be used for DAVIS wx station - main_kiss_enabled = 0; - - // enable the davis serial protocol client to allow pooling callbacks to be called in main loop. - // This only controls the callback it doesn't mean that the station itself is responding to - // communication. It stays set to one event if Davis station - main_davis_serial_enabled = 1; - - // trigger the rxcheck to get all counter values - davis_trigger_rxcheck_packet(); - - } - else { - // if not revert back to KISS configuration - main_target_kiss_baudrate = 9600u; + // reset RX state to allow reinitialization with changed baudrate main_kiss_srl_ctx_ptr->srl_rx_state = SRL_RX_NOT_CONFIG; - // initializing UART drvier + // reinitializing serial hardware to wake up Davis wx station srl_init(main_kiss_srl_ctx_ptr, USART1, srl_usart1_rx_buffer, RX_BUFFER_1_LN, srl_usart1_tx_buffer, TX_BUFFER_1_LN, main_target_kiss_baudrate, 1); - srl_init(main_wx_srl_ctx_ptr, USART2, srl_usart2_rx_buffer, RX_BUFFER_2_LN, srl_usart2_tx_buffer, TX_BUFFER_2_LN, main_target_wx_baudrate, 1); + srl_switch_timeout(main_kiss_srl_ctx_ptr, SRL_TIMEOUT_ENABLE, 3000); + + davis_init(main_kiss_srl_ctx_ptr); + + // try to wake up the davis base + rte_wx_davis_station_avaliable = (davis_wake_up(DAVIS_BLOCKING_IO) == 0 ? 1 : 0); + + // if davis weather stations is connected to SERIAL port + if (rte_wx_davis_station_avaliable == 1) { + // turn LCD backlight on.. + davis_control_backlight(1); + + // wait for a while + delay_fixed(1000); + + // and then off to let the user know that communication is working + davis_control_backlight(0); + + // disable the KISS modem as the UART will be used for DAVIS wx station + main_kiss_enabled = 0; + + // enable the davis serial protocol client to allow pooling callbacks to be called in main loop. + // This only controls the callback it doesn't mean that the station itself is responding to + // communication. It stays set to one event if Davis station + main_davis_serial_enabled = 1; + + // trigger the rxcheck to get all counter values + davis_trigger_rxcheck_packet(); + + } + else { + // if not revert back to KISS configuration + main_target_kiss_baudrate = 9600u; + main_kiss_srl_ctx_ptr->srl_rx_state = SRL_RX_NOT_CONFIG; + + // initializing UART drvier + srl_init(main_kiss_srl_ctx_ptr, USART1, srl_usart1_rx_buffer, RX_BUFFER_1_LN, srl_usart1_tx_buffer, TX_BUFFER_1_LN, main_target_kiss_baudrate, 1); + srl_init(main_wx_srl_ctx_ptr, USART2, srl_usart2_rx_buffer, RX_BUFFER_2_LN, srl_usart2_tx_buffer, TX_BUFFER_2_LN, main_target_wx_baudrate, 1); + + } } -#endif - if (main_config_data_mode->wx_modbus == 1) { + else if (main_config_data_mode->wx_modbus == 1) { rtu_serial_init(&rte_rtu_pool_queue, 1, main_wx_srl_ctx_ptr, main_config_data_rtu); @@ -584,28 +574,27 @@ int main(int argc, char* argv[]){ } } -#ifdef _DALLAS_AS_TELEM - #ifndef _DALLAS_SPLIT_PIN - dallas_init(GPIOC, GPIO_Pin_6, GPIO_PinSource6, &rte_wx_dallas_average); - #else - dallas_init(GPIOC, GPIO_Pin_11, GPIO_PinSource11, &rte_wx_dallas_average); - #endif -#endif +//#ifdef _DALLAS_AS_TELEM +// #ifndef _DALLAS_SPLIT_PIN +// dallas_init(GPIOC, GPIO_Pin_6, GPIO_PinSource6, &rte_wx_dallas_average); +// #else +// dallas_init(GPIOC, GPIO_Pin_11, GPIO_PinSource11, &rte_wx_dallas_average); +// #endif +//#endif // configuring interrupt priorities it_handlers_set_priorities(); -#if (defined _METEO && defined _SENSOR_MS5611) - ms5611_reset(&rte_wx_ms5611_qf); - ms5611_read_calibration(SensorCalData, &rte_wx_ms5611_qf); - ms5611_trigger_measure(0, 0); -#endif - -#if (defined _METEO && defined _SENSOR_BME280) - bme280_reset(&rte_wx_bme280_qf); - bme280_setup(); - bme280_read_calibration(bme280_calibration_data); -#endif + if (main_config_data_mode->wx_ms5611_or_bme == 0) { + ms5611_reset(&rte_wx_ms5611_qf); + ms5611_read_calibration(SensorCalData, &rte_wx_ms5611_qf); + ms5611_trigger_measure(0, 0); + } + else if (main_config_data_mode->wx_ms5611_or_bme == 1) { + bme280_reset(&rte_wx_bme280_qf); + bme280_setup(); + bme280_read_calibration(bme280_calibration_data); + } if (main_kiss_enabled == 1) { // preparing initial beacon which will be sent to host PC using KISS protocol via UART @@ -661,10 +650,10 @@ int main(int argc, char* argv[]){ AFSK_Init(&main_afsk); ax25_init(&main_ax25, &main_afsk, 0, message_callback); - #ifdef _METEO - // getting all meteo measuremenets to be sure that WX frames want be sent with zeros - wx_get_all_measurements(main_config_data_wx_sources, main_config_data_mode, main_config_data_umb, main_config_data_rtu); -#endif + if ((main_config_data_mode->wx & WX_ENABLED) == 1) { + // getting all meteo measuremenets to be sure that WX frames want be sent with zeros + wx_get_all_measurements(main_config_data_wx_sources, main_config_data_mode, main_config_data_umb, main_config_data_rtu); + } // start serial port i/o transaction depending on station configuration if (main_config_data_mode->victron == 1) { @@ -723,27 +712,30 @@ int main(int argc, char* argv[]){ ; } + // read the state of a button input if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) { + // if modem is not busy on transmitting something and the button is not + // inhibited if (main_afsk.sending == false && button_inhibit == 0) { + // wait for radio channel to be released while(main_ax25.dcd == true); -#ifndef _METEO - //telemetry_send_values(rx10m, tx10m, digi10m, kiss10m, rte_wx_temperature_dallas_valid, rte_wx_dallas_qf, rte_wx_ms5611_qf, rte_wx_dht.qf); - beacon_send_own(); -#else + if ((main_config_data_mode->wx & WX_ENABLED) == 0) { - //SendWXFrame(rte_wx_average_windspeed, rte_wx_max_windspeed, rte_wx_average_winddirection, rte_wx_temperature_dallas_valid, rte_wx_pressure_valid, rte_wx_humidity); - - srl_wait_for_tx_completion(main_kiss_srl_ctx_ptr); - - SendWXFrameToBuffer(rte_wx_average_windspeed, rte_wx_max_windspeed, rte_wx_average_winddirection, rte_wx_temperature_average_external_valid, rte_wx_pressure_valid, rte_wx_humidity, main_kiss_srl_ctx.srl_tx_buf_pointer, TX_BUFFER_1_LN, &ln); - - if (main_kiss_enabled == 1) { - srl_start_tx(main_kiss_srl_ctx_ptr, ln); + beacon_send_own(); + } + else { + + srl_wait_for_tx_completion(main_kiss_srl_ctx_ptr); + + SendWXFrameToBuffer(rte_wx_average_windspeed, rte_wx_max_windspeed, rte_wx_average_winddirection, rte_wx_temperature_average_external_valid, rte_wx_pressure_valid, rte_wx_humidity, main_kiss_srl_ctx.srl_tx_buf_pointer, TX_BUFFER_1_LN, &ln); + + if (main_kiss_enabled == 1) { + srl_start_tx(main_kiss_srl_ctx_ptr, ln); + } } -#endif // #ifndef _METEO } button_inhibit = 1; @@ -873,6 +865,7 @@ int main(int argc, char* argv[]){ if (main_config_data_mode->wx_umb == 1) { + // umb_0x26_status_request(&rte_wx_umb, &rte_wx_umb_context, main_config_data_umb); } diff --git a/src/packet_tx_handler.c b/src/packet_tx_handler.c index 9219186..04bd725 100644 --- a/src/packet_tx_handler.c +++ b/src/packet_tx_handler.c @@ -23,13 +23,11 @@ uint8_t packet_tx_beacon_counter = 0; uint8_t packet_tx_error_status_interval = 2; uint8_t packet_tx_error_status_counter = 0; -#ifdef _METEO uint8_t packet_tx_meteo_interval = _WX_INTERVAL; uint8_t packet_tx_meteo_counter = 2; uint8_t packet_tx_meteo_kiss_interval = 2; uint8_t packet_tx_meteo_kiss_counter = 0; -#endif uint8_t packet_tx_telemetry_interval = 10; uint8_t packet_tx_telemetry_counter = 0; @@ -80,19 +78,17 @@ void packet_tx_handler(const config_data_basic_t * const config_basic, const con packet_tx_error_status_counter++; packet_tx_telemetry_counter++; packet_tx_telemetry_descr_counter++; -#ifdef _METEO - packet_tx_meteo_counter++; - packet_tx_meteo_kiss_counter++; -#endif + if ((main_config_data_mode->wx & WX_ENABLED) == WX_ENABLED) { + packet_tx_meteo_counter++; + packet_tx_meteo_kiss_counter++; + } if (packet_tx_error_status_counter >= packet_tx_error_status_interval) { - //#if defined(_UMB_MASTER) if (config_mode->wx_umb) { umb_construct_status_str(&rte_wx_umb_context, main_own_aprs_msg, sizeof(main_own_aprs_msg), &ln, master_time); packet_tx_multi_per_call_handler(); } - //#endif packet_tx_error_status_counter = 0; } @@ -108,75 +104,68 @@ void packet_tx_handler(const config_data_basic_t * const config_basic, const con } -#ifdef _METEO - if (packet_tx_meteo_counter >= packet_tx_meteo_interval) { + if ((main_config_data_mode->wx & WX_ENABLED) == WX_ENABLED) { + if (packet_tx_meteo_counter >= packet_tx_meteo_interval) { - packet_tx_multi_per_call_handler(); + packet_tx_multi_per_call_handler(); - #if defined _DALLAS_AS_TELEM - // _DALLAS_AS_TELEM wil be set during compilation wx packets will be filled by temperature from MS5611 sensor - //SendWXFrame(&VNAME, rte_wx_temperature_valid, rte_wx_pressure_valid); - SendWXFrame(rte_wx_average_windspeed, rte_wx_max_windspeed, rte_wx_average_winddirection, rte_wx_temperature_ms, rte_wx_pressure_valid, rte_wx_humidity_valid); - #else - SendWXFrame(rte_wx_average_windspeed, rte_wx_max_windspeed, rte_wx_average_winddirection, rte_wx_temperature_average_external_valid, rte_wx_pressure_valid, rte_wx_humidity_valid); - #endif + SendWXFrame(rte_wx_average_windspeed, rte_wx_max_windspeed, rte_wx_average_winddirection, rte_wx_temperature_average_external_valid, rte_wx_pressure_valid, rte_wx_humidity_valid); - #ifdef EXTERNAL_WATCHDOG - GPIOA->ODR ^= GPIO_Pin_12; - #endif + #ifdef EXTERNAL_WATCHDOG + GPIOA->ODR ^= GPIO_Pin_12; + #endif - packet_tx_meteo_counter = 0; - } -#endif + packet_tx_meteo_counter = 0; + } -#ifdef _METEO -#ifdef _MODBUS_RTU - // send the status packet with raw values of all requested modbus-RTU registers - if (packet_tx_meteo_counter == (packet_tx_meteo_interval - 1) && + if ((main_config_data_mode->wx_modbus & WX_MODBUS_DEBUG) == WX_MODBUS_DEBUG) { + // send the status packet with raw values of all requested modbus-RTU registers + if (packet_tx_meteo_counter == (packet_tx_meteo_interval - 1) && + packet_tx_telemetry_descr_counter >= packet_tx_modbus_raw_values) + { + + packet_tx_multi_per_call_handler(); + + telemetry_send_status_raw_values_modbus(); + } + + // trigger the status packet with modbus-rtu state like error counters, timestamps etc. + if (packet_tx_meteo_counter == (packet_tx_meteo_interval - 1) && + packet_tx_telemetry_descr_counter > packet_tx_modbus_status && + packet_tx_telemetry_descr_counter <= packet_tx_modbus_status * 2) + { + + packet_tx_multi_per_call_handler(); + + rte_main_trigger_modbus_status = 1; + } + } + + // check if Victron VE.Direct serial protocol client is enabled and it is + // a time to send the status message + if (config_mode->victron == 1 && + packet_tx_meteo_counter == (packet_tx_meteo_interval - 1) && packet_tx_telemetry_descr_counter >= packet_tx_modbus_raw_values) - { + { + packet_tx_multi_per_call_handler(); - packet_tx_multi_per_call_handler(); + telemetry_send_status_pv(&rte_pv_average, &rte_pv_last_error, rte_pv_struct.system_state, master_time, rte_pv_messages_count, rte_pv_corrupted_messages_count); - telemetry_send_status_raw_values_modbus(); + } + + // send wx frame to KISS host once every two minutes + if (packet_tx_meteo_kiss_counter >= packet_tx_meteo_kiss_interval && main_kiss_enabled == 1) { + + srl_wait_for_tx_completion(main_kiss_srl_ctx_ptr); + + SendWXFrameToBuffer(rte_wx_average_windspeed, rte_wx_max_windspeed, rte_wx_average_winddirection, rte_wx_temperature_average_external_valid, rte_wx_pressure_valid, rte_wx_humidity_valid, main_kiss_srl_ctx_ptr->srl_tx_buf_pointer, main_kiss_srl_ctx_ptr->srl_tx_buf_ln, &ln); + + srl_start_tx(main_kiss_srl_ctx_ptr, ln); + + packet_tx_meteo_kiss_counter = 0; + } } - // trigger the status packet with modbus-rtu state like error counters, timestamps etc. - if (packet_tx_meteo_counter == (packet_tx_meteo_interval - 1) && - packet_tx_telemetry_descr_counter > packet_tx_modbus_status && - packet_tx_telemetry_descr_counter <= packet_tx_modbus_status * 2) - { - - packet_tx_multi_per_call_handler(); - - rte_main_trigger_modbus_status = 1; - } -#endif - - // check if Victron VE.Direct serial protocol client is enabled and it is - // a time to send status message - if (config_mode->victron == 1 && - packet_tx_meteo_counter == (packet_tx_meteo_interval - 1) && - packet_tx_telemetry_descr_counter >= packet_tx_modbus_raw_values) - { - packet_tx_multi_per_call_handler(); - - telemetry_send_status_pv(&rte_pv_average, &rte_pv_last_error, rte_pv_struct.system_state, master_time, rte_pv_messages_count, rte_pv_corrupted_messages_count); - - } - - if (packet_tx_meteo_kiss_counter >= packet_tx_meteo_kiss_interval && main_kiss_enabled == 1) { - - srl_wait_for_tx_completion(main_kiss_srl_ctx_ptr); - - SendWXFrameToBuffer(rte_wx_average_windspeed, rte_wx_max_windspeed, rte_wx_average_winddirection, rte_wx_temperature_average_external_valid, rte_wx_pressure_valid, rte_wx_humidity_valid, main_kiss_srl_ctx_ptr->srl_tx_buf_pointer, main_kiss_srl_ctx_ptr->srl_tx_buf_ln, &ln); - - srl_start_tx(main_kiss_srl_ctx_ptr, ln); - - packet_tx_meteo_kiss_counter = 0; - } -#endif // #ifdef _METEO - if (packet_tx_telemetry_counter >= packet_tx_telemetry_interval) { packet_tx_multi_per_call_handler(); @@ -209,61 +198,50 @@ void packet_tx_handler(const config_data_basic_t * const config_basic, const con rte_wx_current_dallas_qf = DALLAS_QF_UNKNOWN; } - #if defined(_SENSOR_MS5611) // some metaprogramming to save RAM - // pressure sensors quality factors - if (rte_wx_ms5611_qf == MS5611_QF_UNKNOWN) { - #if defined(_SENSOR_BME280) - // use BME280 is used instead - switch (rte_wx_bme280_qf) { - case BME280_QF_FULL: - case BME280_QF_HUMIDITY_DEGRADED: pressure_qf = PRESSURE_QF_FULL; break; - case BME280_QF_UKNOWN: - case BME280_QF_NOT_AVAILABLE: pressure_qf = PRESSURE_QF_NOT_AVALIABLE; break; - case BME280_QF_PRESSURE_DEGRADED: - case BME280_QF_GEN_DEGRADED: pressure_qf = PRESSURE_QF_DEGRADATED; break; - } - #endif - } - else { - // if not use MS5611 + // get quality factors for internal pressure and humidity sensors + if (config_mode->wx_ms5611_or_bme == 0) { + // pressure sensors quality factors + switch (rte_wx_ms5611_qf) { case MS5611_QF_FULL: pressure_qf = PRESSURE_QF_FULL; break; case MS5611_QF_NOT_AVALIABLE: pressure_qf = PRESSURE_QF_NOT_AVALIABLE; break; case MS5611_QF_DEGRADATED: pressure_qf = PRESSURE_QF_DEGRADATED; break; case MS5611_QF_UNKNOWN: pressure_qf = PRESSURE_QF_UNKNOWN; break; } - } - #elif defined(_SENSOR_BME280) - // humidity sensors quality factors - if (rte_wx_bme280_qf == BME280_QF_UKNOWN) { - ; + } + else if (config_mode->wx_ms5611_or_bme == 1) { + //#elif defined(_SENSOR_BME280) + // humidity sensors quality factors + if (rte_wx_bme280_qf == BME280_QF_UKNOWN) { + ; + } + else { + // use BME280 + switch (rte_wx_bme280_qf) { + case BME280_QF_FULL: + case BME280_QF_PRESSURE_DEGRADED: humidity_qf = HUMIDITY_QF_FULL; break; + case BME280_QF_UKNOWN: + case BME280_QF_NOT_AVAILABLE: humidity_qf = HUMIDITY_QF_NOT_AVALIABLE; break; + case BME280_QF_HUMIDITY_DEGRADED: + case BME280_QF_GEN_DEGRADED: humidity_qf = HUMIDITY_QF_DEGRADATED; break; + } + + switch (rte_wx_bme280_qf) { + case BME280_QF_FULL: + case BME280_QF_HUMIDITY_DEGRADED: pressure_qf = PRESSURE_QF_FULL; break; + case BME280_QF_UKNOWN: + case BME280_QF_NOT_AVAILABLE: pressure_qf = PRESSURE_QF_NOT_AVALIABLE; break; + case BME280_QF_PRESSURE_DEGRADED: + case BME280_QF_GEN_DEGRADED: pressure_qf = PRESSURE_QF_DEGRADATED; break; + } + + } } else { - // use BME280 - switch (rte_wx_bme280_qf) { - case BME280_QF_FULL: - case BME280_QF_PRESSURE_DEGRADED: humidity_qf = HUMIDITY_QF_FULL; break; - case BME280_QF_UKNOWN: - case BME280_QF_NOT_AVAILABLE: humidity_qf = HUMIDITY_QF_NOT_AVALIABLE; break; - case BME280_QF_HUMIDITY_DEGRADED: - case BME280_QF_GEN_DEGRADED: humidity_qf = HUMIDITY_QF_DEGRADATED; break; - } - - switch (rte_wx_bme280_qf) { - case BME280_QF_FULL: - case BME280_QF_HUMIDITY_DEGRADED: pressure_qf = PRESSURE_QF_FULL; break; - case BME280_QF_UKNOWN: - case BME280_QF_NOT_AVAILABLE: pressure_qf = PRESSURE_QF_NOT_AVALIABLE; break; - case BME280_QF_PRESSURE_DEGRADED: - case BME280_QF_GEN_DEGRADED: pressure_qf = PRESSURE_QF_DEGRADATED; break; - } - - } - #else pressure_qf = PRESSURE_QF_NOT_AVALIABLE; humidity_qf = HUMIDITY_QF_NOT_AVALIABLE; - #endif + } // wind quality factor if (rte_wx_wind_qf == AN_WIND_QF_UNKNOWN) {