2019-01-26 17:02:19 +00:00
|
|
|
/*
|
|
|
|
* wx_handler.c
|
|
|
|
*
|
|
|
|
* Created on: 26.01.2019
|
|
|
|
* Author: mateusz
|
|
|
|
*/
|
|
|
|
|
2019-12-16 21:40:42 +00:00
|
|
|
#include "wx_handler.h"
|
|
|
|
|
2019-01-26 22:18:25 +00:00
|
|
|
#include <rte_wx.h>
|
2020-01-04 13:15:33 +00:00
|
|
|
#include <math.h>
|
2020-06-11 18:36:36 +00:00
|
|
|
#include <stm32f10x.h>
|
2019-01-26 17:02:19 +00:00
|
|
|
#include "drivers/_dht22.h"
|
|
|
|
#include "drivers/ms5611.h"
|
2020-01-04 13:15:33 +00:00
|
|
|
#include "drivers/analog_anemometer.h"
|
2020-03-02 21:05:43 +00:00
|
|
|
#include "drivers/tx20.h"
|
2019-01-26 22:18:25 +00:00
|
|
|
|
|
|
|
#include "station_config.h"
|
2019-01-26 17:02:19 +00:00
|
|
|
|
2019-12-16 21:40:42 +00:00
|
|
|
#include "delay.h"
|
2019-04-15 20:46:36 +00:00
|
|
|
#include "telemetry.h"
|
2019-12-16 21:40:42 +00:00
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#define WX_WATCHDOG_PERIOD (SYSTICK_TICKS_PER_SECONDS * SYSTICK_TICKS_PERIOD * 90)
|
|
|
|
#define WX_WATCHDOG_RESET_DURATION (SYSTICK_TICKS_PER_SECONDS * SYSTICK_TICKS_PERIOD * 3)
|
|
|
|
|
2019-12-22 11:34:03 +00:00
|
|
|
#define WX_MAX_TEMPERATURE_SLEW_RATE 4.0f
|
|
|
|
|
2019-12-16 21:40:42 +00:00
|
|
|
uint32_t wx_last_good_temperature_time = 0;
|
|
|
|
uint32_t wx_last_good_wind_time = 0;
|
|
|
|
wx_pwr_state_t wx_pwr_state;
|
2019-12-22 11:34:03 +00:00
|
|
|
uint8_t wx_inhibit_slew_rate_check = 1;
|
2020-02-29 21:12:08 +00:00
|
|
|
uint32_t wx_wind_pool_call_counter = 0;
|
2019-04-15 20:46:36 +00:00
|
|
|
|
2020-02-01 22:20:17 +00:00
|
|
|
static const float direction_constant = M_PI/180.0f;
|
|
|
|
|
2019-01-26 17:02:19 +00:00
|
|
|
void wx_get_all_measurements(void) {
|
|
|
|
|
2020-03-06 19:24:20 +00:00
|
|
|
int8_t j = 0;
|
|
|
|
int32_t i = 0;
|
2019-01-26 17:02:19 +00:00
|
|
|
int32_t return_value = 0;
|
2020-03-06 19:24:20 +00:00
|
|
|
float pressure_average_sum = 0.0f;
|
2019-01-26 17:02:19 +00:00
|
|
|
|
2020-06-21 21:11:16 +00:00
|
|
|
#if (defined _METEO && defined _SENSOR_MS5611)
|
2019-09-08 21:42:59 +00:00
|
|
|
// quering MS5611 sensor for temperature
|
2019-10-27 19:50:01 +00:00
|
|
|
return_value = ms5611_get_temperature(&rte_wx_temperature_ms, &rte_wx_ms5611_qf);
|
2019-09-08 21:42:59 +00:00
|
|
|
|
|
|
|
if (return_value == MS5611_OK) {
|
2019-10-27 19:50:01 +00:00
|
|
|
rte_wx_temperature_ms_valid = rte_wx_temperature_ms;
|
2019-09-08 21:42:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-06-29 07:44:33 +00:00
|
|
|
#if (defined _METEO && defined _SENSOR_BME280)
|
2020-06-21 21:11:16 +00:00
|
|
|
// reading raw values
|
2020-06-24 16:45:06 +00:00
|
|
|
return_value = bme280_read_raw_data(bme280_data_buffer);
|
2020-06-21 21:11:16 +00:00
|
|
|
|
2020-06-23 22:17:59 +00:00
|
|
|
if (return_value == BME280_OK) {
|
2020-06-21 21:11:16 +00:00
|
|
|
|
2020-06-24 16:45:06 +00:00
|
|
|
// setting back the Quality Factor to FULL to trace any problems with sensor readouts
|
|
|
|
rte_wx_bme280_qf = BME280_QF_FULL;
|
|
|
|
|
2020-06-21 21:11:16 +00:00
|
|
|
// converting raw values to temperature
|
2020-06-24 16:45:06 +00:00
|
|
|
bme280_get_temperature(&rte_wx_temperature_ms, bme280_get_adc_t(), &rte_wx_bme280_qf);
|
2020-06-21 21:11:16 +00:00
|
|
|
|
|
|
|
// converting raw values to pressure
|
2020-06-24 16:45:06 +00:00
|
|
|
bme280_get_pressure(&rte_wx_pressure, bme280_get_adc_p(), &rte_wx_bme280_qf);
|
2020-06-23 22:17:59 +00:00
|
|
|
|
|
|
|
// converting raw values to humidity
|
2020-06-24 16:45:06 +00:00
|
|
|
bme280_get_humidity(&rte_wx_humidity, bme280_get_adc_h(), &rte_wx_bme280_qf);
|
|
|
|
|
|
|
|
if (rte_wx_bme280_qf == BME280_QF_FULL) {
|
|
|
|
|
|
|
|
rte_wx_pressure_valid = rte_wx_pressure;
|
|
|
|
rte_wx_temperature_ms_valid = rte_wx_temperature_ms;
|
|
|
|
rte_wx_humidity_valid = rte_wx_humidity;
|
2020-06-21 21:11:16 +00:00
|
|
|
|
|
|
|
// add the current pressure into buffer
|
|
|
|
rte_wx_pressure_history[rte_wx_pressure_it++] = rte_wx_pressure;
|
|
|
|
|
|
|
|
// reseting the average length iterator
|
|
|
|
j = 0;
|
|
|
|
|
|
|
|
// check if and end of the buffer was reached
|
|
|
|
if (rte_wx_pressure_it >= PRESSURE_AVERAGE_LN) {
|
|
|
|
rte_wx_pressure_it = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculating the average of pressure measuremenets
|
|
|
|
for (i = 0; i < PRESSURE_AVERAGE_LN; i++) {
|
|
|
|
|
|
|
|
// skip empty slots in the history to provide proper value even for first wx packet
|
|
|
|
if (rte_wx_pressure_history[i] < 10.0f) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add to the average
|
|
|
|
pressure_average_sum += rte_wx_pressure_history[i];
|
|
|
|
|
|
|
|
// increase the average lenght iterator
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
rte_wx_pressure_valid = pressure_average_sum / (float)j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-26 22:18:25 +00:00
|
|
|
#if defined _METEO || defined _DALLAS_AS_TELEM
|
|
|
|
|
2019-01-26 17:02:19 +00:00
|
|
|
// quering dallas DS12B20 thermometer for current temperature
|
2019-04-15 20:46:36 +00:00
|
|
|
rte_wx_temperature_dallas = dallas_query(&rte_wx_current_dallas_qf);
|
2019-01-26 17:02:19 +00:00
|
|
|
|
|
|
|
// checking if communication was successfull
|
2019-04-15 20:46:36 +00:00
|
|
|
if (rte_wx_temperature_dallas != -128.0f) {
|
|
|
|
|
2019-12-22 11:34:03 +00:00
|
|
|
// calculate the slew rate
|
|
|
|
rte_wx_temperature_dalls_slew_rate = rte_wx_temperature_dallas - rte_wx_temperature_dallas_valid;
|
|
|
|
|
|
|
|
// chcecking the positive (ascending) slew rate of the temperature measuremenets
|
|
|
|
if (rte_wx_temperature_dalls_slew_rate > WX_MAX_TEMPERATURE_SLEW_RATE && wx_inhibit_slew_rate_check == 0) {
|
|
|
|
|
|
|
|
// if temeperature measuremenet has changed more than maximum allowed slew rate set degradadet QF
|
|
|
|
rte_wx_error_dallas_qf = DALLAS_QF_DEGRADATED;
|
|
|
|
|
|
|
|
// and increase the temperature only by 1.0f to decrease slew rate
|
|
|
|
rte_wx_temperature_dallas += 1.0f;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// chcecking the negaive (descending) slew rate of the temperature measuremenets
|
|
|
|
if (rte_wx_temperature_dalls_slew_rate < -WX_MAX_TEMPERATURE_SLEW_RATE && wx_inhibit_slew_rate_check == 0) {
|
|
|
|
|
|
|
|
// if temeperature measuremenet has changed more than maximum allowed slew rate set degradadet QF
|
|
|
|
rte_wx_error_dallas_qf = DALLAS_QF_DEGRADATED;
|
|
|
|
|
|
|
|
// and decrease the temperature only by 1.0f to decrease slew rate
|
|
|
|
rte_wx_temperature_dallas -= 1.0f;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-27 19:50:01 +00:00
|
|
|
// store current value
|
2019-01-26 17:02:19 +00:00
|
|
|
rte_wx_temperature_dallas_valid = rte_wx_temperature_dallas;
|
2019-04-15 20:46:36 +00:00
|
|
|
|
2019-10-27 19:50:01 +00:00
|
|
|
// include current temperature into the average
|
|
|
|
dallas_average(rte_wx_temperature_dallas, &rte_wx_dallas_average);
|
|
|
|
|
|
|
|
// update the current temperature with current average
|
|
|
|
rte_wx_temperature_average_dallas_valid = dallas_get_average(&rte_wx_dallas_average);
|
|
|
|
|
|
|
|
// update current minimal temperature
|
|
|
|
rte_wx_temperature_min_dallas_valid = dallas_get_min(&rte_wx_dallas_average);
|
|
|
|
|
|
|
|
// and update maximum also
|
|
|
|
rte_wx_temperature_max_dallas_valid = dallas_get_max(&rte_wx_dallas_average);
|
2019-12-16 21:40:42 +00:00
|
|
|
|
|
|
|
// updating last good measurement time
|
|
|
|
wx_last_good_temperature_time = master_time;
|
2019-04-15 20:46:36 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-04-28 07:36:24 +00:00
|
|
|
// if there were a communication error set the error to unavaliable
|
2019-04-15 20:46:36 +00:00
|
|
|
rte_wx_error_dallas_qf = DALLAS_QF_NOT_AVALIABLE;
|
2019-04-28 07:36:24 +00:00
|
|
|
|
2019-04-15 20:46:36 +00:00
|
|
|
}
|
2019-12-22 11:34:03 +00:00
|
|
|
|
|
|
|
// enabling slew rate checking after first power up
|
|
|
|
wx_inhibit_slew_rate_check = 0;
|
2019-01-26 22:18:25 +00:00
|
|
|
#endif
|
2019-01-26 17:02:19 +00:00
|
|
|
|
2020-06-21 21:11:16 +00:00
|
|
|
#if (defined _METEO) && (defined _SENSOR_MS5611)
|
2019-01-26 17:02:19 +00:00
|
|
|
// quering MS5611 sensor for pressure
|
|
|
|
return_value = ms5611_get_pressure(&rte_wx_pressure, &rte_wx_ms5611_qf);
|
|
|
|
|
|
|
|
if (return_value == MS5611_OK) {
|
2020-03-06 19:24:20 +00:00
|
|
|
// add the current pressure into buffer
|
|
|
|
rte_wx_pressure_history[rte_wx_pressure_it++] = rte_wx_pressure;
|
|
|
|
|
|
|
|
// reseting the average length iterator
|
|
|
|
j = 0;
|
|
|
|
|
|
|
|
// check if and end of the buffer was reached
|
|
|
|
if (rte_wx_pressure_it >= PRESSURE_AVERAGE_LN) {
|
|
|
|
rte_wx_pressure_it = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculating the average of pressure measuremenets
|
|
|
|
for (i = 0; i < PRESSURE_AVERAGE_LN; i++) {
|
|
|
|
|
|
|
|
// skip empty slots in the history to provide proper value even for first wx packet
|
|
|
|
if (rte_wx_pressure_history[i] < 10.0f) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add to the average
|
|
|
|
pressure_average_sum += rte_wx_pressure_history[i];
|
|
|
|
|
|
|
|
// increase the average lenght iterator
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
rte_wx_pressure_valid = pressure_average_sum / (float)j;
|
2019-01-26 17:02:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 21:11:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _METEO
|
2019-01-26 17:02:19 +00:00
|
|
|
// if humidity sensor is idle trigger the communiction & measuremenets
|
|
|
|
if (dht22State == DHT22_STATE_DONE || dht22State == DHT22_STATE_TIMEOUT)
|
|
|
|
dht22State = DHT22_STATE_IDLE;
|
|
|
|
|
2019-01-26 22:18:25 +00:00
|
|
|
#endif
|
2020-03-23 20:41:01 +00:00
|
|
|
|
|
|
|
#if defined(_UMB_MASTER)
|
|
|
|
if (rte_wx_umb_qf == UMB_QF_FULL) {
|
|
|
|
rte_wx_temperature_average_dallas_valid = umb_get_temperature();
|
|
|
|
rte_wx_pressure_valid = umb_get_qfe();
|
|
|
|
}
|
|
|
|
#endif
|
2019-01-26 17:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void wx_pool_dht22(void) {
|
|
|
|
|
|
|
|
dht22_timeout_keeper();
|
|
|
|
|
|
|
|
switch (dht22State) {
|
|
|
|
case DHT22_STATE_IDLE:
|
|
|
|
dht22_comm(&rte_wx_dht);
|
|
|
|
break;
|
|
|
|
case DHT22_STATE_DATA_RDY:
|
|
|
|
dht22_decode(&rte_wx_dht);
|
|
|
|
break;
|
|
|
|
case DHT22_STATE_DATA_DECD:
|
|
|
|
rte_wx_dht_valid = rte_wx_dht; // powrot do stanu DHT22_STATE_IDLE jest w TIM3_IRQHandler
|
2019-01-27 11:34:43 +00:00
|
|
|
//rte_wx_dht_valid.qf = DHT22_QF_FULL;
|
2020-06-24 16:45:06 +00:00
|
|
|
rte_wx_humidity = rte_wx_dht.humidity;
|
2019-01-26 17:02:19 +00:00
|
|
|
dht22State = DHT22_STATE_DONE;
|
|
|
|
#ifdef _DBG_TRACE
|
|
|
|
trace_printf("DHT22: temperature=%d,humi=%d\r\n", dht_valid.scaledTemperature, dht_valid.humidity);
|
|
|
|
#endif
|
|
|
|
break;
|
2019-01-27 11:34:43 +00:00
|
|
|
case DHT22_STATE_TIMEOUT:
|
|
|
|
rte_wx_dht_valid.qf = DHT22_QF_UNAVALIABLE;
|
|
|
|
break;
|
2019-01-26 17:02:19 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-12-16 21:40:42 +00:00
|
|
|
|
2020-03-23 20:41:01 +00:00
|
|
|
void wx_pool_anemometer(void) {
|
2020-01-04 13:15:33 +00:00
|
|
|
|
|
|
|
// locals
|
|
|
|
uint32_t average_windspeed = 0;
|
2020-02-01 22:20:17 +00:00
|
|
|
int32_t wind_direction_x_avg = 0;
|
|
|
|
int32_t wind_direction_y_avg = 0;
|
|
|
|
int16_t wind_direction_x = 0;
|
|
|
|
int16_t wind_direction_y = 0;
|
|
|
|
volatile float dir_temp = 0;
|
|
|
|
volatile float arctan_value = 0.0f;
|
2020-01-04 13:15:33 +00:00
|
|
|
short i = 0;
|
2020-02-29 21:12:08 +00:00
|
|
|
uint8_t average_ln;
|
|
|
|
|
|
|
|
wx_wind_pool_call_counter++;
|
2020-01-04 13:15:33 +00:00
|
|
|
|
2020-03-07 18:13:24 +00:00
|
|
|
uint16_t scaled_windspeed = 0;
|
|
|
|
|
2020-02-23 06:50:22 +00:00
|
|
|
#ifdef _ANEMOMETER_ANALOGUE
|
2020-01-04 13:15:33 +00:00
|
|
|
// this windspeed is scaled * 10. Example: 0.2 meters per second is stored as 2
|
2020-03-07 18:13:24 +00:00
|
|
|
scaled_windspeed = analog_anemometer_get_ms_from_pulse(rte_wx_windspeed_pulses);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _ANEMOMETER_TX20
|
|
|
|
scaled_windspeed = tx20_get_scaled_windspeed();
|
2020-03-03 21:12:15 +00:00
|
|
|
rte_wx_winddirection_last = tx20_get_wind_direction();
|
2020-02-23 06:50:22 +00:00
|
|
|
#endif
|
2020-01-04 13:15:33 +00:00
|
|
|
|
2020-03-23 20:41:01 +00:00
|
|
|
#if defined(_UMB_MASTER)
|
|
|
|
rte_wx_average_winddirection = umb_get_winddirection();
|
|
|
|
rte_wx_average_windspeed = umb_get_windspeed();
|
|
|
|
rte_wx_max_windspeed = umb_get_windgusts();
|
|
|
|
#else
|
|
|
|
|
2020-02-29 21:12:08 +00:00
|
|
|
// check how many times before the pool function was called
|
|
|
|
if (wx_wind_pool_call_counter < WIND_AVERAGE_LEN) {
|
|
|
|
// if it was called less time than a length of buffers, the average length
|
|
|
|
// needs to be shortened to handle the underrun properly
|
|
|
|
average_ln = (uint8_t)wx_wind_pool_call_counter;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
average_ln = WIND_AVERAGE_LEN;
|
|
|
|
}
|
|
|
|
|
2020-01-04 13:15:33 +00:00
|
|
|
// putting the wind speed into circular buffer
|
|
|
|
rte_wx_windspeed[rte_wx_windspeed_it] = scaled_windspeed;
|
|
|
|
|
|
|
|
// increasing the iterator to the windspeed buffer
|
|
|
|
rte_wx_windspeed_it++;
|
|
|
|
|
|
|
|
// checking if iterator reached an end of the buffer
|
2020-02-29 21:12:08 +00:00
|
|
|
if (rte_wx_windspeed_it >= WIND_AVERAGE_LEN) {
|
2020-01-04 13:15:33 +00:00
|
|
|
rte_wx_windspeed_it = 0;
|
2020-02-29 21:12:08 +00:00
|
|
|
}
|
2020-01-04 13:15:33 +00:00
|
|
|
|
|
|
|
// calculating the average windspeed
|
2020-02-29 21:12:08 +00:00
|
|
|
for (i = 0; i < average_ln; i++)
|
2020-01-04 13:15:33 +00:00
|
|
|
average_windspeed += rte_wx_windspeed[i];
|
|
|
|
|
2020-02-29 21:12:08 +00:00
|
|
|
average_windspeed /= average_ln;
|
2020-01-04 13:15:33 +00:00
|
|
|
|
|
|
|
// store the value in rte
|
|
|
|
rte_wx_average_windspeed = average_windspeed;
|
|
|
|
|
|
|
|
// reuse the local variable to find maximum value
|
|
|
|
average_windspeed = 0;
|
|
|
|
|
|
|
|
// looking for gusts
|
2020-02-29 21:12:08 +00:00
|
|
|
for (i = 0; i < average_ln; i++) {
|
2020-01-04 13:15:33 +00:00
|
|
|
if (average_windspeed < rte_wx_windspeed[i])
|
|
|
|
average_windspeed = rte_wx_windspeed[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// storing wind gusts value in rte
|
|
|
|
rte_wx_max_windspeed = average_windspeed;
|
|
|
|
|
2020-02-01 22:20:17 +00:00
|
|
|
// adding last wind direction to the buffers
|
|
|
|
if (rte_wx_winddirection_it >= WIND_AVERAGE_LEN)
|
|
|
|
rte_wx_winddirection_it = 0;
|
|
|
|
|
|
|
|
rte_wx_winddirection[rte_wx_winddirection_it++] = rte_wx_winddirection_last;
|
|
|
|
|
2020-01-04 13:15:33 +00:00
|
|
|
// calculating average wind direction
|
2020-02-29 21:12:08 +00:00
|
|
|
for (i = 0; i < average_ln; i++) {
|
2020-02-01 22:20:17 +00:00
|
|
|
|
|
|
|
dir_temp = (float)rte_wx_winddirection[i];
|
|
|
|
|
2020-01-04 13:15:33 +00:00
|
|
|
// split the wind direction into x and y component
|
2020-02-01 22:20:17 +00:00
|
|
|
wind_direction_x = (int16_t)(100.0f * cosf(dir_temp * direction_constant));
|
|
|
|
wind_direction_y = (int16_t)(100.0f * sinf(dir_temp * direction_constant));
|
2020-01-04 13:15:33 +00:00
|
|
|
|
|
|
|
// adding components to calculate average
|
|
|
|
wind_direction_x_avg += wind_direction_x;
|
|
|
|
wind_direction_y_avg += wind_direction_y;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// dividing to get average of x and y componen
|
2020-02-29 21:12:08 +00:00
|
|
|
wind_direction_x_avg /= average_ln;
|
|
|
|
wind_direction_y_avg /= average_ln;
|
2020-01-04 13:15:33 +00:00
|
|
|
|
|
|
|
// converting x & y component of wind direction back to an angle
|
2020-02-01 22:20:17 +00:00
|
|
|
arctan_value = atan2f(wind_direction_y_avg , wind_direction_x_avg);
|
|
|
|
|
|
|
|
rte_wx_average_winddirection = (int16_t)(arctan_value * (180.0f/M_PI));
|
2020-01-04 13:15:33 +00:00
|
|
|
|
|
|
|
if (rte_wx_average_winddirection < 0)
|
|
|
|
rte_wx_average_winddirection += 360;
|
2020-03-23 20:41:01 +00:00
|
|
|
|
|
|
|
#endif
|
2020-01-04 13:15:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 21:40:42 +00:00
|
|
|
void wx_pwr_init(void) {
|
2020-06-11 18:36:36 +00:00
|
|
|
// RELAY_CNTRL
|
2019-12-16 21:40:42 +00:00
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
2020-06-11 18:36:36 +00:00
|
|
|
#if (defined PARATNC_HWREV_A || defined PARATNC_HWREV_B)
|
2019-12-16 21:40:42 +00:00
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
|
2020-06-11 18:36:36 +00:00
|
|
|
#elif (defined PARATNC_HWREV_C)
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
|
|
|
#else
|
|
|
|
#error ("Hardware Revision not chosen.")
|
|
|
|
#endif
|
2019-12-16 21:40:42 +00:00
|
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
|
2020-06-11 18:36:36 +00:00
|
|
|
#if (defined PARATNC_HWREV_C)
|
|
|
|
// +12V PWR_CNTRL
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
|
|
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
#endif
|
|
|
|
|
2019-12-16 21:40:42 +00:00
|
|
|
wx_pwr_state = WX_PWR_OFF;
|
|
|
|
|
|
|
|
GPIO_ResetBits(GPIOB, GPIO_Pin_8);
|
|
|
|
|
2020-06-11 18:36:36 +00:00
|
|
|
#if (defined PARATNC_HWREV_C)
|
|
|
|
// +12V_SW PWR_CNTRL
|
|
|
|
GPIO_ResetBits(GPIOA, GPIO_Pin_6);
|
|
|
|
#endif
|
|
|
|
|
2019-12-16 21:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void wx_pwr_periodic_handle(void) {
|
|
|
|
// check when last measuremenets was sent by wind or temperature sensor
|
|
|
|
if ( (master_time - wx_last_good_temperature_time >= WX_WATCHDOG_PERIOD ||
|
|
|
|
master_time - wx_last_good_wind_time >= WX_WATCHDOG_PERIOD) &&
|
|
|
|
wx_pwr_state == WX_PWR_ON) {
|
|
|
|
|
|
|
|
// if timeout watchod expired there is a time to reset the supply voltage
|
|
|
|
wx_pwr_state = WX_PWR_UNDER_RESET;
|
|
|
|
|
2020-06-11 18:36:36 +00:00
|
|
|
// pulling the output down to switch the relay and disable +5V_ISOL (VDD_SW)
|
2019-12-16 21:40:42 +00:00
|
|
|
GPIO_ResetBits(GPIOB, GPIO_Pin_8);
|
|
|
|
|
|
|
|
// setting the last_good timers to current value to prevent reset loop
|
|
|
|
wx_last_good_temperature_time = master_time;
|
|
|
|
wx_last_good_wind_time = master_time;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// service actual supply state
|
|
|
|
switch (wx_pwr_state) {
|
|
|
|
case WX_PWR_OFF:
|
|
|
|
|
|
|
|
// one second delay
|
|
|
|
delay_fixed(2000);
|
|
|
|
|
2020-06-11 18:36:36 +00:00
|
|
|
#if (defined PARATNC_HWREV_C)
|
|
|
|
// Turn on the +12V_SW voltage
|
|
|
|
GPIO_SetBits(GPIOA, GPIO_Pin_6);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
delay_fixed(100);
|
|
|
|
|
|
|
|
// Turn on the +5V_ISOL (VDD_SW) voltage
|
2019-12-16 21:40:42 +00:00
|
|
|
GPIO_SetBits(GPIOB, GPIO_Pin_8);
|
|
|
|
|
|
|
|
// power is off after power-up and needs to be powered on
|
|
|
|
wx_pwr_state = WX_PWR_ON;
|
|
|
|
break;
|
|
|
|
case WX_PWR_ON:
|
|
|
|
break;
|
|
|
|
case WX_PWR_UNDER_RESET:
|
|
|
|
|
2020-06-11 18:36:36 +00:00
|
|
|
// Turn on the +5V_ISOL (VDD_SW) voltage
|
2019-12-16 21:40:42 +00:00
|
|
|
GPIO_SetBits(GPIOB, GPIO_Pin_8);
|
|
|
|
|
|
|
|
wx_pwr_state = WX_PWR_ON;
|
|
|
|
|
2020-06-11 18:36:36 +00:00
|
|
|
break;
|
|
|
|
case WX_PWR_DISABLED:
|
2019-12-16 21:40:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-06-11 18:36:36 +00:00
|
|
|
|
|
|
|
void wx_pwr_disable_12v_sw(void) {
|
|
|
|
#if (defined PARATNC_HWREV_C)
|
|
|
|
wx_pwr_state = WX_PWR_DISABLED;
|
|
|
|
|
|
|
|
GPIO_ResetBits(GPIOA, GPIO_Pin_6);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void wx_pwr_disable_5v_isol(void) {
|
|
|
|
wx_pwr_state = WX_PWR_DISABLED;
|
|
|
|
|
|
|
|
GPIO_ResetBits(GPIOB, GPIO_Pin_8);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void wx_pwr_enable_12v_sw(void) {
|
|
|
|
#if (defined PARATNC_HWREV_C)
|
|
|
|
wx_pwr_state = WX_PWR_OFF;
|
|
|
|
|
|
|
|
// setting last good measurements timers to inhibit relay clicking
|
|
|
|
// just after the power is applied
|
|
|
|
wx_last_good_temperature_time = master_time;
|
|
|
|
wx_last_good_wind_time = master_time;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void wx_pwr_enable_5v_isol(void) {
|
|
|
|
#if (defined PARATNC_HWREV_C)
|
|
|
|
wx_pwr_state = WX_PWR_OFF;
|
|
|
|
|
|
|
|
// setting last good measurements timers to inhibit relay clicking
|
|
|
|
// just after the power is applied
|
|
|
|
wx_last_good_temperature_time = master_time;
|
|
|
|
wx_last_good_wind_time = master_time;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|