diff --git a/firmware/auxout.cpp b/firmware/auxout.cpp index 527f145..8446e98 100644 --- a/firmware/auxout.cpp +++ b/firmware/auxout.cpp @@ -29,18 +29,12 @@ static PWMConfig auxPwmConfig = { .frequency = STM32_SYSCLK, .period = 1 << 12, .callback = nullptr, - .channels = { - [0] = {0, nullptr}, - [1] = {0, nullptr}, - [2] = {0, nullptr}, - [3] = {0, nullptr} - }, + .channels = {[0] = {0, nullptr}, [1] = {0, nullptr}, [2] = {0, nullptr}, [3] = {0, nullptr}}, .cr2 = 0, #if STM32_PWM_USE_ADVANCED .bdtr = 0, #endif - .dier = 0 -}; + .dier = 0}; static void auxDacFillPwmConfig(void) { @@ -86,7 +80,8 @@ void SetAuxDac(int channel, float voltage) auxDac.SetDuty(auxOutPwmCh[channel], duty); // Ripple cancelation channel - if (auxOutPwmChN[channel >= 0]) { + if (auxOutPwmChN[channel >= 0]) + { auxDac.SetDuty(auxOutPwmChN[channel], duty); } } @@ -95,11 +90,7 @@ void SetAuxDac(int channel, float voltage) #ifdef AUXOUT_DAC_DEVICE -static DACConfig auxDacConfig = { - .init = 2047U, - .datamode = DAC_DHRM_12BIT_RIGHT, - .cr = 0 -}; +static DACConfig auxDacConfig = {.init = 2047U, .datamode = DAC_DHRM_12BIT_RIGHT, .cr = 0}; static Dac auxDac(AUXOUT_DAC_DEVICE); @@ -125,22 +116,15 @@ static float AuxGetInputSignal(AuxOutputMode sel) { switch (sel) { - case AuxOutputMode::Afr0: - return 14.7f * GetLambda(0); - case AuxOutputMode::Afr1: - return 14.7f * GetLambda(1); - case AuxOutputMode::Lambda0: - return GetLambda(0); - case AuxOutputMode::Lambda1: - return GetLambda(1); + case AuxOutputMode::Afr0: return 14.7f * GetLambda(0); + case AuxOutputMode::Afr1: return 14.7f * GetLambda(1); + case AuxOutputMode::Lambda0: return GetLambda(0); + case AuxOutputMode::Lambda1: return GetLambda(1); #if HAL_USE_SPI - case AuxOutputMode::Egt0: - return getEgtDrivers()[0].temperature; - case AuxOutputMode::Egt1: - return getEgtDrivers()[1].temperature; + case AuxOutputMode::Egt0: return getEgtDrivers()[0].temperature; + case AuxOutputMode::Egt1: return getEgtDrivers()[1].temperature; #endif - default: - return 0; + default: return 0; } return 0; @@ -154,7 +138,7 @@ void AuxOutThread(void*) chRegSetThreadName("Aux out"); - while(1) + while (1) { for (int ch = 0; ch < AFR_CHANNELS; ch++) { @@ -188,8 +172,6 @@ void InitAuxDac() #else /* (AUXOUT_DAC_PWM_DEVICE || AUXOUT_DAC_DEVICE) */ -void InitAuxDac() -{ -} +void InitAuxDac() {} #endif diff --git a/firmware/dac.cpp b/firmware/dac.cpp index 5c71d1a..7c86392 100644 --- a/firmware/dac.cpp +++ b/firmware/dac.cpp @@ -17,7 +17,8 @@ void Dac::Start(DACConfig& config) dacStart(m_driver, &config); } -void Dac::SetVoltage(int channel, float voltage) { +void Dac::SetVoltage(int channel, float voltage) +{ voltage = clampF(0, voltage, VCC_VOLTS); m_voltageFloat[channel] = voltage; diff --git a/firmware/gpio.h b/firmware/gpio.h index 3b825f7..a05fbd4 100644 --- a/firmware/gpio.h +++ b/firmware/gpio.h @@ -5,17 +5,12 @@ class Gpio { public: - Gpio(ioline_t line) : m_line(line) {}; + Gpio(ioline_t line) + : m_line(line) {}; - void Set() - { - palSetLine(m_line); - } + void Set() { palSetLine(m_line); } - void Clear() - { - palClearLine(m_line); - } + void Clear() { palClearLine(m_line); } private: const ioline_t m_line; diff --git a/firmware/indication.cpp b/firmware/indication.cpp index 4a167db..af6b638 100644 --- a/firmware/indication.cpp +++ b/firmware/indication.cpp @@ -12,20 +12,21 @@ using namespace wbo; #ifdef ADVANCED_INDICATION -#define LED_BLINK_FAST (50) -#define LED_BLINK_MEDIUM (300) -#define LED_BLINK_SLOW (700) -#define LED_OFF_TIME (2000) +#define LED_BLINK_FAST (50) +#define LED_BLINK_MEDIUM (300) +#define LED_BLINK_SLOW (700) +#define LED_OFF_TIME (2000) -struct indicationThreadData { - uint32_t idx; - ioline_t line; +struct indicationThreadData +{ + uint32_t idx; + ioline_t line; }; indicationThreadData indData[] = { - {0, PAL_LINE(LED_GREEN_PORT, LED_GREEN_PIN)}, + {0, PAL_LINE(LED_GREEN_PORT, LED_GREEN_PIN)}, #ifdef LED_R_GREEN_PORT - {1, PAL_LINE(LED_R_GREEN_PORT, LED_R_GREEN_PIN)}, + {1, PAL_LINE(LED_R_GREEN_PORT, LED_R_GREEN_PIN)}, #endif }; @@ -34,12 +35,12 @@ static THD_WORKING_AREA(waIndicationThread, 128); static THD_WORKING_AREA(waIndicationThread2, 128); #endif -static void IndicationThread(void *ptr) +static void IndicationThread(void* ptr) { chRegSetThreadName("Indication"); - indicationThreadData *data = (indicationThreadData *)ptr; + indicationThreadData* data = (indicationThreadData*)ptr; - while(true) + while (true) { auto status = GetCurrentStatus(data->idx); @@ -82,36 +83,40 @@ void InitIndication() /* Can be calles from two TS channels. */ void onDataArrived(bool status) { - /* to avoid blinking when two TS channels available and only one is communicating - * another one will call this function with status = false every timeout */ - /* TODO: total crap, rework */ - static int filter = 0; + /* to avoid blinking when two TS channels available and only one is communicating + * another one will call this function with status = false every timeout */ + /* TODO: total crap, rework */ + static int filter = 0; - if (status) { - if (filter < 3) { - filter = 3; - } - } else { - if (filter > 0) { - filter--; - } - } + if (status) + { + if (filter < 3) + { + filter = 3; + } + } + else + { + if (filter > 0) + { + filter--; + } + } - if (filter) { - palSetPad(LED_BLUE_PORT, LED_BLUE_PIN); - } else { - palClearPad(LED_BLUE_PORT, LED_BLUE_PIN); - } + if (filter) + { + palSetPad(LED_BLUE_PORT, LED_BLUE_PIN); + } + else + { + palClearPad(LED_BLUE_PORT, LED_BLUE_PIN); + } } #else -void InitIndication() -{ -} +void InitIndication() {} -void onDataArrived(bool) -{ -} +void onDataArrived(bool) {} #endif \ No newline at end of file diff --git a/firmware/pump_dac.cpp b/firmware/pump_dac.cpp index c64d92c..6131644 100644 --- a/firmware/pump_dac.cpp +++ b/firmware/pump_dac.cpp @@ -12,20 +12,17 @@ // 48MHz / 1024 = 46.8khz PWM // 64MHz / 1024 = 62.5khz PWM // 160MHz / 1024 = 156.25khz PWM -static const PWMConfig pumpDacConfig = { - STM32_SYSCLK, - 1024, - nullptr, - { - {PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, nullptr}, - {PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, nullptr}, - {PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, nullptr}, - {PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, nullptr} - }, - 0, - 0, +static const PWMConfig pumpDacConfig = {STM32_SYSCLK, + 1024, + nullptr, + {{PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, nullptr}, + {PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, nullptr}, + {PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, nullptr}, + {PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, nullptr}}, + 0, + 0, #if STM32_PWM_USE_ADVANCED - 0 + 0 #endif }; @@ -56,16 +53,11 @@ static void SetPumpVoltage(int ch, float volts) #ifdef PUMP_DAC_DAC_DEVICE_0 -static DACConfig pumpDacConfig = { - .init = 2047U, /* half of VCC */ - .datamode = DAC_DHRM_12BIT_RIGHT, - .cr = 0 -}; +static DACConfig pumpDacConfig = {.init = 2047U, /* half of VCC */ + .datamode = DAC_DHRM_12BIT_RIGHT, + .cr = 0}; -static Dac pumpDacs[] { - Dac(PUMP_DAC_DAC_DEVICE_0), - Dac(PUMP_DAC_DAC_DEVICE_1) -}; +static Dac pumpDacs[]{Dac(PUMP_DAC_DAC_DEVICE_0), Dac(PUMP_DAC_DAC_DEVICE_1)}; static void SetPumpVoltage(int ch, float volts) { @@ -74,7 +66,8 @@ static void SetPumpVoltage(int ch, float volts) #endif -struct pump_dac_state { +struct pump_dac_state +{ int32_t curIpump; }; diff --git a/firmware/pwm.cpp b/firmware/pwm.cpp index 6d3e95a..2515331 100644 --- a/firmware/pwm.cpp +++ b/firmware/pwm.cpp @@ -17,7 +17,8 @@ void Pwm::Start(const PWMConfig& config) pwmStart(m_driver, &config); } -void Pwm::SetDuty(int channel, float duty) { +void Pwm::SetDuty(int channel, float duty) +{ auto dutyFloat = clampF(0, duty, 1); m_dutyFloat[channel] = dutyFloat; pwmcnt_t highTime = m_counterPeriod * dutyFloat; diff --git a/firmware/pwm.h b/firmware/pwm.h index d162f45..0b0c2b6 100644 --- a/firmware/pwm.h +++ b/firmware/pwm.h @@ -18,7 +18,7 @@ public: private: PWMDriver* const m_driver; - //const uint8_t m_channel; + // const uint8_t m_channel; /* const */ uint32_t m_counterFrequency; /* const */ uint16_t m_counterPeriod; float m_dutyFloat[4]; diff --git a/firmware/status.cpp b/firmware/status.cpp index 4c64cc6..bb429bd 100644 --- a/firmware/status.cpp +++ b/firmware/status.cpp @@ -19,7 +19,8 @@ bool HasFault() { bool fault = false; - for (int ch = 0; ch < AFR_CHANNELS; ch++) { + for (int ch = 0; ch < AFR_CHANNELS; ch++) + { fault |= (GetCurrentStatus(ch) >= Status::SensorDidntHeat); } diff --git a/firmware/uart.cpp b/firmware/uart.cpp index e12fc72..eaa5681 100644 --- a/firmware/uart.cpp +++ b/firmware/uart.cpp @@ -21,11 +21,7 @@ #ifdef DEBUG_SERIAL_PORT SerialConfig cfg = { - .speed = DEBUG_SERIAL_BAUDRATE, - .cr1 = 0, - .cr2 = USART_CR2_STOP1_BITS | PORT_EXTRA_SERIAL_CR2, - .cr3 = 0 -}; + .speed = DEBUG_SERIAL_BAUDRATE, .cr1 = 0, .cr2 = USART_CR2_STOP1_BITS | PORT_EXTRA_SERIAL_CR2, .cr3 = 0}; static char printBuffer[200]; @@ -36,11 +32,11 @@ static void UartThread(void*) sdStart(&SD1, &cfg); - while(true) + while (true) { int ch; - #ifdef BOARD_HAS_VOLTAGE_SENSE +#ifdef BOARD_HAS_VOLTAGE_SENSE { float vbatt = GetSupplyVoltage(); @@ -49,12 +45,18 @@ static void UartThread(void*) int tempC = GetMcuTemperature(); - size_t writeCount = chsnprintf(printBuffer, sizeof(printBuffer), "Board: VBatt %d.%01d Temp %d deg C\r\n", vbattIntPart, vbattTenths, tempC); - chnWrite(&SD1, (const uint8_t *)printBuffer, writeCount); + size_t writeCount = chsnprintf(printBuffer, + sizeof(printBuffer), + "Board: VBatt %d.%01d Temp %d deg C\r\n", + vbattIntPart, + vbattTenths, + tempC); + chnWrite(&SD1, (const uint8_t*)printBuffer, writeCount); } - #endif +#endif - for (ch = 0; ch < AFR_CHANNELS; ch++) { + for (ch = 0; ch < AFR_CHANNELS; ch++) + { float lambda = GetLambda(ch); int lambdaIntPart = lambda; int lambdaThousandths = (lambda - lambdaIntPart) * 1000; @@ -62,29 +64,35 @@ static void UartThread(void*) int heaterDuty = GetHeaterDuty(ch) * 100; int pumpDuty = GetPumpOutputDuty(ch) * 100; - size_t writeCount = chsnprintf(printBuffer, sizeof(printBuffer), - "[AFR%d]: %d.%03d DC: %4d mV AC: %4d mV ESR: %5d T: %4d C Ipump: %6d uA PumpDac: %3d Vheater: %5d heater: %s (%d)\tfault: %s\r\n", - ch, - lambdaIntPart, lambdaThousandths, - (int)(GetSampler(ch).GetNernstDc() * 1000.0), - (int)(GetSampler(ch).GetNernstAc() * 1000.0), - (int)GetSampler(ch).GetSensorInternalResistance(), - (int)GetSampler(ch).GetSensorTemperature(), - (int)(GetSampler(ch).GetPumpNominalCurrent() * 1000), - pumpDuty, - heaterVoltageMv, - describeHeaterState(GetHeaterState(ch)), heaterDuty, - describeStatus(GetCurrentStatus(ch))); - chnWrite(&SD1, (const uint8_t *)printBuffer, writeCount); + size_t writeCount = chsnprintf(printBuffer, + sizeof(printBuffer), + "[AFR%d]: %d.%03d DC: %4d mV AC: %4d mV ESR: %5d T: %4d C Ipump: %6d uA " + "PumpDac: %3d Vheater: %5d heater: %s (%d)\tfault: %s\r\n", + ch, + lambdaIntPart, + lambdaThousandths, + (int)(GetSampler(ch).GetNernstDc() * 1000.0), + (int)(GetSampler(ch).GetNernstAc() * 1000.0), + (int)GetSampler(ch).GetSensorInternalResistance(), + (int)GetSampler(ch).GetSensorTemperature(), + (int)(GetSampler(ch).GetPumpNominalCurrent() * 1000), + pumpDuty, + heaterVoltageMv, + describeHeaterState(GetHeaterState(ch)), + heaterDuty, + describeStatus(GetCurrentStatus(ch))); + chnWrite(&SD1, (const uint8_t*)printBuffer, writeCount); } #if (EGT_CHANNELS > 0) - for (ch = 0; ch < EGT_CHANNELS; ch++) { - size_t writeCount = chsnprintf(printBuffer, sizeof(printBuffer), - "EGT[%d]: %d C (int %d C)\r\n", - (int)getEgtDrivers()[ch].temperature, - (int)getEgtDrivers()[ch].coldJunctionTemperature); - chnWrite(&SD1, (const uint8_t *)printBuffer, writeCount); + for (ch = 0; ch < EGT_CHANNELS; ch++) + { + size_t writeCount = chsnprintf(printBuffer, + sizeof(printBuffer), + "EGT[%d]: %d C (int %d C)\r\n", + (int)getEgtDrivers()[ch].temperature, + (int)getEgtDrivers()[ch].coldJunctionTemperature); + chnWrite(&SD1, (const uint8_t*)printBuffer, writeCount); } #endif /* EGT_CHANNELS > 0 */ @@ -104,10 +112,15 @@ static UartTsChannel primaryChannel(TS_PRIMARY_UART_PORT); static SerialTsChannel primaryChannel(TS_PRIMARY_SERIAL_PORT); #endif -struct PrimaryChannelThread : public TunerstudioThread { - PrimaryChannelThread() : TunerstudioThread("Primary TS Channel") { } +struct PrimaryChannelThread : public TunerstudioThread +{ + PrimaryChannelThread() + : TunerstudioThread("Primary TS Channel") + { + } - TsChannelBase* setupChannel() { + TsChannelBase* setupChannel() + { primaryChannel.start(TS_PRIMARY_BAUDRATE); return &primaryChannel; @@ -119,10 +132,15 @@ static PrimaryChannelThread primaryChannelThread; #ifdef TS_SECONDARY_SERIAL_PORT static SerialTsChannel secondaryChannel(TS_SECONDARY_SERIAL_PORT); -struct SecondaryChannelThread : public TunerstudioThread { - SecondaryChannelThread() : TunerstudioThread("Secondary TS Channel") { } +struct SecondaryChannelThread : public TunerstudioThread +{ + SecondaryChannelThread() + : TunerstudioThread("Secondary TS Channel") + { + } - TsChannelBase* setupChannel() { + TsChannelBase* setupChannel() + { secondaryChannel.start(TS_SECONDARY_BAUDRATE); return &secondaryChannel;