vbatt measurement

pull/7/head
Mateusz Lubecki 2022-03-26 19:43:41 +01:00
rodzic 327e6c3861
commit 67b8c3fad5
5 zmienionych plików z 181 dodań i 72 usunięć

Wyświetl plik

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="ilg.gnumcueclipse.debug.gdbjtag.openocd.launchConfigurationType">
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.PERIPHERALS" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;peripherals&gt;&#10; &lt;peripheral name=&quot;GPIOC&quot;/&gt;&#10; &lt;peripheral name=&quot;GPIOB&quot;/&gt;&#10;&lt;/peripherals&gt;&#10;"/>
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.PERIPHERALS" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;peripherals&gt;&#10; &lt;peripheral name=&quot;GPIOB&quot;/&gt;&#10; &lt;peripheral name=&quot;GPIOC&quot;/&gt;&#10; &lt;peripheral name=&quot;ADC2&quot;/&gt;&#10;&lt;/peripherals&gt;&#10;"/>
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doContinue" value="true"/>
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doDebugInRam" value="false"/>
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doFirstReset" value="true"/>

Wyświetl plik

@ -19,9 +19,14 @@ void io_oc_init(void);
void io_oc_output_low(void);
void io_oc_output_hiz(void);
void io_pwr_init(void);
void io_ext_watchdog_config(void);
void io_ext_watchdog_service(void);
void io_vbat_meas_init(int8_t a_coeff, int8_t b_coeff);
uint16_t io_vbat_meas_get(void);
inline void io_5v_isol_sw_enable(void) {
// ParaMETEO - UC_CNTRL_VS

165
src/io.c
Wyświetl plik

@ -23,6 +23,8 @@
#if defined(PARAMETEO)
LL_GPIO_InitTypeDef GPIO_InitTypeDef;
int8_t io_vbat_a_coeff, io_vbat_b_coeff;
#endif
void io_oc_init(void) {
@ -49,6 +51,81 @@ void io_oc_output_hiz(void) {
#endif
}
void io_pwr_init(void) {
#if defined(STM32F10X_MD_VL)
// RELAY_CNTRL
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
#if (defined PARATNC_HWREV_A || defined PARATNC_HWREV_B)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
#elif (defined PARATNC_HWREV_C)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
#else
#error ("Hardware Revision not chosen.")
#endif
GPIO_Init(GPIOB, &GPIO_InitStructure);
#if (defined PARATNC_HWREV_C)
// +12V PWR_CNTRL
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOA, &GPIO_InitStructure);
#endif
wx_pwr_state = WX_PWR_OFF;
GPIO_ResetBits(GPIOB, GPIO_Pin_8);
#if (defined PARATNC_HWREV_C)
// +12V_SW PWR_CNTRL
GPIO_ResetBits(GPIOA, GPIO_Pin_6);
#endif
#endif
#if defined(STM32L471xx)
// PC13 - UC_CNTRL_VS
GPIO_InitTypeDef.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitTypeDef.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitTypeDef.Pin = LL_GPIO_PIN_13;
GPIO_InitTypeDef.Pull = LL_GPIO_PULL_NO;
GPIO_InitTypeDef.Speed = LL_GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitTypeDef.Alternate = LL_GPIO_AF_7;
LL_GPIO_Init(GPIOC, &GPIO_InitTypeDef);
// PA6 - UC_CNTRL_VG
GPIO_InitTypeDef.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitTypeDef.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitTypeDef.Pin = LL_GPIO_PIN_6;
GPIO_InitTypeDef.Pull = LL_GPIO_PULL_NO;
GPIO_InitTypeDef.Speed = LL_GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitTypeDef.Alternate = LL_GPIO_AF_7;
LL_GPIO_Init(GPIOA, &GPIO_InitTypeDef);
// PA1 - UC_CNTRL_VC
GPIO_InitTypeDef.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitTypeDef.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitTypeDef.Pin = LL_GPIO_PIN_1;
GPIO_InitTypeDef.Pull = LL_GPIO_PULL_NO;
GPIO_InitTypeDef.Speed = LL_GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitTypeDef.Alternate = LL_GPIO_AF_7;
LL_GPIO_Init(GPIOA, &GPIO_InitTypeDef);
// PB1 - UC_CNTRL_VC
GPIO_InitTypeDef.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitTypeDef.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitTypeDef.Pin = LL_GPIO_PIN_1;
GPIO_InitTypeDef.Pull = LL_GPIO_PULL_NO;
GPIO_InitTypeDef.Speed = LL_GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitTypeDef.Alternate = LL_GPIO_AF_7;
LL_GPIO_Init(GPIOB, &GPIO_InitTypeDef);
#endif
}
void io_ext_watchdog_config(void) {
#ifdef STM32F10X_MD_VL
// initialize Watchdog output
@ -74,3 +151,91 @@ void io_ext_watchdog_service(void) {
#endif
}
void io_vbat_meas_init(int8_t a_coeff, int8_t b_coeff) {
io_vbat_a_coeff = a_coeff;
io_vbat_b_coeff = b_coeff;
GPIO_InitTypeDef.Mode = LL_GPIO_MODE_ANALOG;
GPIO_InitTypeDef.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitTypeDef.Pin = LL_GPIO_PIN_5;
GPIO_InitTypeDef.Pull = LL_GPIO_PULL_NO;
GPIO_InitTypeDef.Speed = LL_GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitTypeDef.Alternate = LL_GPIO_AF_7;
LL_GPIO_Init(GPIOC, &GPIO_InitTypeDef);
LL_GPIO_EnablePinAnalogControl(GPIOC, LL_GPIO_PIN_5);
volatile int stupid_delay = 0;
// reset the clock for ADC
// RCC->AHB2ENR &= (0xFFFFFFFF ^ RCC_AHB2ENR_ADCEN);
// RCC->AHB2ENR |= RCC_AHB2ENR_ADCEN;
// the adc should be disabled now, but just to be sure that this is a case
ADC2->CR &= (0xFFFFFFFF ^ ADC_CR_ADEN);
// exit from deep-power-down mode
ADC2->CR &= (0xFFFFFFFF ^ ADC_CR_DEEPPWD);
// start ADC voltage regulator
ADC2->CR |= ADC_CR_ADVREGEN;
// wait for voltage regulator to start
for (; stupid_delay < 0x1FFFF; stupid_delay++);
// start the calibration
ADC2->CR |= ADC_CR_ADCAL;
// wait for calibration to finish
while((ADC2->CR & ADC_CR_ADCAL) == ADC_CR_ADCAL);
// set the first (and only channel in a conversion sequence) channel 14
ADC2->SQR1 |= (14 << 6);
// set the sampling rate to 12.5 ADC clock cycles
ADC2->SMPR1 |= 0x2;
ADC2->CFGR &= (0xFFFFFFFF ^ ADC_CFGR_CONT);
// set discontinous conversion
ADC2->CFGR |= ADC_CFGR_DISCEN;
// ignore overrun and overwrite data register content with new conversion result
ADC2->CFGR |= ADC_CFGR_OVRMOD;
}
uint16_t io_vbat_meas_get(void) {
uint16_t out = 0;
float temp = 0.0f;
// start ADC
ADC2->CR |= ADC_CR_ADEN;
// wait for startup
while((ADC2->ISR & ADC_ISR_ADRDY) == 0);
// start conversion
ADC2->CR |= ADC_CR_ADSTART;
// wait for conversion to finish
while((ADC2->ISR & ADC_ISR_EOC) != ADC_ISR_EOC) {
;
}
out = ADC2->DR;
// disable ADC
ADC2->CR &= (0xFFFFFFFF ^ ADC_CR_ADEN);
temp = (float)out * 0.00081f;
out = (uint16_t) (temp * (float)io_vbat_a_coeff + (float)io_vbat_b_coeff);
return out;
}

Wyświetl plik

@ -227,6 +227,8 @@ unsigned short rx10m = 0, tx10m = 0, digi10m = 0, digidrop10m = 0, kiss10m = 0;
LL_GPIO_InitTypeDef GPIO_InitTypeDef;
gsm_sim800_state_t main_gsm_state;
uint16_t main_battery_voltage;
#endif
static void message_callback(struct AX25Msg *msg) {
@ -467,11 +469,17 @@ int main(int argc, char* argv[]){
#if defined(STM32L471xx)
// initialize all powersaving functions
pwr_save_init(main_config_data_mode->powersave);
// initialize B+ measurement
io_vbat_meas_init(100, 10);
#endif
// initalizing separated Open Collector output
io_oc_init();
// initializing GPIO used for swithing on and off voltages on pcb
io_pwr_init();
// initialize sensor power control and switch off supply voltage
wx_pwr_switch_init();
@ -860,6 +868,8 @@ int main(int argc, char* argv[]){
led_control_led2_bottom(false);
#if defined(STM32L471xx)
main_battery_voltage = io_vbat_meas_get();
pwr_save_switch_mode_to_c0();
// sleep a little bit and wait for everything to power up completely

Wyświetl plik

@ -86,78 +86,7 @@ void wx_pwr_switch_case_off_paratnc() {
}
void wx_pwr_switch_init(void) {
#if defined(STM32F10X_MD_VL)
// RELAY_CNTRL
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
#if (defined PARATNC_HWREV_A || defined PARATNC_HWREV_B)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
#elif (defined PARATNC_HWREV_C)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
#else
#error ("Hardware Revision not chosen.")
#endif
GPIO_Init(GPIOB, &GPIO_InitStructure);
#if (defined PARATNC_HWREV_C)
// +12V PWR_CNTRL
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOA, &GPIO_InitStructure);
#endif
wx_pwr_state = WX_PWR_OFF;
GPIO_ResetBits(GPIOB, GPIO_Pin_8);
#if (defined PARATNC_HWREV_C)
// +12V_SW PWR_CNTRL
GPIO_ResetBits(GPIOA, GPIO_Pin_6);
#endif
#endif
#if defined(STM32L471xx)
LL_GPIO_InitTypeDef GPIO_InitTypeDef;
// PC13 - UC_CNTRL_VS
GPIO_InitTypeDef.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitTypeDef.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitTypeDef.Pin = LL_GPIO_PIN_13;
GPIO_InitTypeDef.Pull = LL_GPIO_PULL_NO;
GPIO_InitTypeDef.Speed = LL_GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitTypeDef.Alternate = LL_GPIO_AF_7;
LL_GPIO_Init(GPIOC, &GPIO_InitTypeDef);
// PA6 - UC_CNTRL_VG
GPIO_InitTypeDef.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitTypeDef.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitTypeDef.Pin = LL_GPIO_PIN_6;
GPIO_InitTypeDef.Pull = LL_GPIO_PULL_NO;
GPIO_InitTypeDef.Speed = LL_GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitTypeDef.Alternate = LL_GPIO_AF_7;
LL_GPIO_Init(GPIOA, &GPIO_InitTypeDef);
// PA1 - UC_CNTRL_VC
GPIO_InitTypeDef.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitTypeDef.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitTypeDef.Pin = LL_GPIO_PIN_1;
GPIO_InitTypeDef.Pull = LL_GPIO_PULL_NO;
GPIO_InitTypeDef.Speed = LL_GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitTypeDef.Alternate = LL_GPIO_AF_7;
LL_GPIO_Init(GPIOA, &GPIO_InitTypeDef);
// PB1 - UC_CNTRL_VC
GPIO_InitTypeDef.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitTypeDef.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitTypeDef.Pin = LL_GPIO_PIN_1;
GPIO_InitTypeDef.Pull = LL_GPIO_PULL_NO;
GPIO_InitTypeDef.Speed = LL_GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitTypeDef.Alternate = LL_GPIO_AF_7;
LL_GPIO_Init(GPIOB, &GPIO_InitTypeDef);
#endif
}
void wx_pwr_switch_periodic_handle(void) {