From b50e7c25f660aa8457e1cbd5758dd209193139b6 Mon Sep 17 00:00:00 2001 From: Ea-r-th <39779954+Ea-r-th@users.noreply.github.com> Date: Fri, 7 Nov 2025 00:20:43 -0800 Subject: [PATCH] Finish testing stage --- .../Timer/Reg/SHAL_TIM_REG_L432KC.h | 2 +- .../STM32L4XX/Peripheral/Timer/SHAL_TIM.cpp | 4 + .../Peripheral/Timer/SHAL_TIM_CALLBACK.cpp | 4 +- SHAL/Src/main.cpp | 111 +++++++++++++----- 4 files changed, 88 insertions(+), 33 deletions(-) diff --git a/SHAL/Include/Peripheral/Timer/Reg/SHAL_TIM_REG_L432KC.h b/SHAL/Include/Peripheral/Timer/Reg/SHAL_TIM_REG_L432KC.h index a1eb3b2..c17dce7 100644 --- a/SHAL/Include/Peripheral/Timer/Reg/SHAL_TIM_REG_L432KC.h +++ b/SHAL/Include/Peripheral/Timer/Reg/SHAL_TIM_REG_L432KC.h @@ -178,7 +178,7 @@ getTimerCaptureCompareModeRegistersOutput(Timer_Key key) { static inline SHAL_TIM_Break_Dead_Time_Register getTimerBreakDeadTimeRegister(Timer_Key key){ - SHAL_TIM_Break_Dead_Time_Register res = {nullptr, 1UL << 15}; + SHAL_TIM_Break_Dead_Time_Register res = {nullptr, TIM_BDTR_MOE}; volatile TIM_TypeDef* tim = TIM_TABLE[static_cast(key)]; diff --git a/SHAL/Src/STM32L4XX/Peripheral/Timer/SHAL_TIM.cpp b/SHAL/Src/STM32L4XX/Peripheral/Timer/SHAL_TIM.cpp index f2ab060..c3b192d 100644 --- a/SHAL/Src/STM32L4XX/Peripheral/Timer/SHAL_TIM.cpp +++ b/SHAL/Src/STM32L4XX/Peripheral/Timer/SHAL_TIM.cpp @@ -56,6 +56,9 @@ void Timer::init(uint32_t prescaler, uint32_t autoReload) { setPrescaler(prescaler); setARR(autoReload); + + *getTimerStatusRegister(m_key).reg = 0; + *getTimerDMAInterruptEnableRegister(m_key).reg = 0; } void Timer::setPWMMode(SHAL_Timer_Channel channel, SHAL_TIM_Output_Compare_Mode outputCompareMode, SHAL_Timer_Channel_Main_Output_Mode mainOutputMode, @@ -89,6 +92,7 @@ void Timer::setPWMMode(SHAL_Timer_Channel channel, SHAL_TIM_Output_Compare_Mode SHAL_set_bits(ccer.reg, 4, fullChannelModeMask, offset); SHAL_apply_bitmask(bdtr.reg, bdtr.main_output_enable_mask); + } void Timer::setPWMDutyCycle(uint32_t dutyCycle) { diff --git a/SHAL/Src/STM32L4XX/Peripheral/Timer/SHAL_TIM_CALLBACK.cpp b/SHAL/Src/STM32L4XX/Peripheral/Timer/SHAL_TIM_CALLBACK.cpp index 6a212b8..b6cd25d 100644 --- a/SHAL/Src/STM32L4XX/Peripheral/Timer/SHAL_TIM_CALLBACK.cpp +++ b/SHAL/Src/STM32L4XX/Peripheral/Timer/SHAL_TIM_CALLBACK.cpp @@ -8,8 +8,8 @@ DEFINE_TIMER_IRQ(Timer_Key::S_TIM1, TIM1_IRQHandler) DEFINE_TIMER_IRQ(Timer_Key::S_TIM2, TIM2_IRQHandler) DEFINE_TIMER_IRQ(Timer_Key::S_TIM6, TIM6_IRQHandler) DEFINE_TIMER_IRQ(Timer_Key::S_TIM7, TIM7_IRQHandler) -DEFINE_TIMER_IRQ(Timer_Key::S_TIM15, TIM15_IRQHandler) -DEFINE_TIMER_IRQ(Timer_Key::S_TIM16, TIM16_IRQHandler) +DEFINE_TIMER_IRQ(Timer_Key::S_TIM15, TIM1_BRK_TIM15_IRQHandler) +DEFINE_TIMER_IRQ(Timer_Key::S_TIM16, TIM1_UP_TIM16_IRQHandler) void registerTimerCallback(Timer_Key key, TimerCallback callback){ timer_callbacks[static_cast(key)] = callback; diff --git a/SHAL/Src/main.cpp b/SHAL/Src/main.cpp index 42acc1a..0a9e1a2 100644 --- a/SHAL/Src/main.cpp +++ b/SHAL/Src/main.cpp @@ -19,13 +19,14 @@ uint16_t vals[NUM_CHANNELS] = {0,0,0,0,0,0,0,0}; uint8_t currentSensor = 0; bool isAlarmBeeping = false; -bool prevIsCalibrateButtonHigh = false; -uint16_t sensorThresholds[NUM_CHANNELS] = {}; +uint16_t sensorThresholds[NUM_CHANNELS]; int buzzer_beepCount = 0; bool isBeepingForCalibration = false; +bool prevIsCalibrateButtonHigh = false; + int cyclesPerPrint = 2; int currentCycle = 0; @@ -44,10 +45,12 @@ void getSensorData(){ currentSensor = (currentSensor + 1) % NUM_CHANNELS; currentCycle = (currentCycle + 1) % cyclesPerPrint; + } void startBeeping(){ - SHAL_TIM1.start(); + SHAL_TIM6.init(4000000,400); //PWM switcher - standard error beeping rate + SHAL_TIM6.start(); } @@ -58,6 +61,50 @@ void stopBeeping(){ isBeepingForCalibration = false; } +void checkSensorThresholds(){ + + //bool sensorsRequirementsTemp = areSensorRequirementsMetCurrent; TODO uncomment all of this + + /* + for(int i = 0; i < NUM_CHANNELS; i++){ + if(vals[i] < sensorThresholds[i]){ + areSensorRequirementsMetCurrent = false; //All sensors must be valid + + if(sensorsRequirementsTemp){ //Requirements were met before and aren't now, so start beeping timer + SHAL_TIM15.start(); + PIN(B5).setHigh(); + } + + break; + } + } + + if(areSensorRequirementsMetCurrent){ + SHAL_TIM15.stop(); + stopBeeping(); + } + */ + + //Copied from loop TODO remove this once real functionality is implemented + if(!areSensorRequirementsMetCurrent){ + + if(areSensorRequirementsMetPrevious) { + PIN(B5).setHigh(); + SHAL_TIM15.start(); + } + } + //-------------------------------------------------------------------------------- + + else{ + PIN(B5).setLow(); + + if(!areSensorRequirementsMetPrevious) { //Transition from not met -> met + SHAL_TIM15.stop(); + stopBeeping(); + } + } +} + void calibrateThresholds(){ for(int i = 0; i < 6; i++){ @@ -66,9 +113,13 @@ void calibrateThresholds(){ } } - void PWMToggle(){ + //Flash light + PIN(B5).toggle(); + + SHAL_TIM15.stop(); //Stop timer for allowed time off sensors + if(isBeepingForCalibration && buzzer_beepCount > 2){ isBeepingForCalibration = false; buzzer_beepCount = 0; @@ -88,7 +139,8 @@ void PWMToggle(){ } void buttonHoldCallback(){ - PIN(B3).toggle(); + + PIN(B5).toggle(); SHAL_TIM7.stop(); //Stop this timer @@ -110,18 +162,21 @@ int main() { SHAL_init(); - //SHAL_UART2.init(UART_Pair_Key::Tx2A2_Rx2A3); - //SHAL_UART2.begin(115200); + SHAL_UART2.init(UART_Pair_Key::Tx2A2_Rx2A3); + SHAL_UART2.begin(115200); PIN(A0).setPinMode(PinMode::ANALOG_MODE); PIN(A1).setPinMode(PinMode::ANALOG_MODE); - PIN(A2).setPinMode(PinMode::ANALOG_MODE); - PIN(A3).setPinMode(PinMode::ANALOG_MODE); + //PIN(A2).setPinMode(PinMode::ANALOG_MODE); + //PIN(A3).setPinMode(PinMode::ANALOG_MODE); PIN(A4).setPinMode(PinMode::ANALOG_MODE); PIN(A5).setPinMode(PinMode::ANALOG_MODE); PIN(A6).setPinMode(PinMode::ANALOG_MODE); PIN(A7).setPinMode(PinMode::ANALOG_MODE); + PIN(B5).setPinMode(PinMode::OUTPUT_MODE); + + PIN(B6).setPinMode(PinMode::INPUT_MODE); SHAL_TIM2.init(4000000,400); @@ -131,12 +186,11 @@ int main() { PIN(B0).setAlternateFunction(GPIO_Alternate_Function_Mapping::B0_TIM1CH2N); - SHAL_TIM1.init(0,2400); - SHAL_TIM1.setPWMMode(SHAL_Timer_Channel::CH2,SHAL_TIM_Output_Compare_Mode::PWMMode1,SHAL_Timer_Channel_Main_Output_Mode::Disabled,SHAL_Timer_Channel_Complimentary_Output_Mode::Polarity_Reversed); + SHAL_TIM1.init(0,2400); //PWM signal + SHAL_TIM1.setPWMMode(SHAL_Timer_Channel::CH2,SHAL_TIM_Output_Compare_Mode::PWMMode1,SHAL_Timer_Channel_Main_Output_Mode::Polarity_Normal,SHAL_Timer_Channel_Complimentary_Output_Mode::Polarity_Reversed); SHAL_TIM1.setPWMDutyCycle(900); - //PIN(B0).setPinMode(PinMode::OUTPUT_MODE); - SHAL_TIM6.init(4000000,400); + SHAL_TIM6.init(4000000,400); //PWM switcher SHAL_TIM6.setCallbackFunc(PWMToggle); SHAL_TIM6.enableInterrupt(); @@ -144,31 +198,25 @@ int main() { SHAL_TIM7.setCallbackFunc(buttonHoldCallback); SHAL_TIM7.enableInterrupt(); - PIN(B6).setPinMode(PinMode::INPUT_MODE); - PIN(B3).setPinMode(PinMode::OUTPUT_MODE); //Test - PIN(B3).setLow(); + SHAL_TIM15.init(4000000,5000); //1 second + SHAL_TIM15.setCallbackFunc(startBeeping); + SHAL_TIM15.enableInterrupt(); - while (true) { - //Retarded polling based button methods cause EXTI decided to not work on L432KC for some stupid reason at the last second - if(!(PIN(B6).digitalRead() == 1)){ - areSensorRequirementsMetCurrent = true; - if(!areSensorRequirementsMetPrevious){ - startBeeping(); - } + while (true) { //TODO set to use button for simulating off sensor, uncomment for real functionality + + if(PIN(B6).digitalRead() != 1){ + areSensorRequirementsMetCurrent = false; + /* if(!prevIsCalibrateButtonHigh){ SHAL_TIM7.start(); } prevIsCalibrateButtonHigh = true; - */ - + */ } else{ - areSensorRequirementsMetCurrent = false; - if(areSensorRequirementsMetPrevious){ - stopBeeping(); - } + areSensorRequirementsMetCurrent = true; /* if(prevIsCalibrateButtonHigh){ @@ -176,9 +224,12 @@ int main() { SHAL_TIM7.stop(); } prevIsCalibrateButtonHigh = false; - */ + */ } + checkSensorThresholds(); + areSensorRequirementsMetPrevious = areSensorRequirementsMetCurrent; + } } \ No newline at end of file