1414
1515#include "heater_hardware.h"
1616#include "systemwide.h"
17+ #include "FreeRTOS.h"
18+ #include "task.h"
1719
1820static void init_error (void );
1921static void adc_setup (ADC_HandleTypeDef * adc );
@@ -33,7 +35,7 @@ typedef struct {
3335 DAC_HandleTypeDef dac1 ;
3436 COMP_HandleTypeDef comp4 ;
3537 TIM_OC_InitTypeDef pwm_config ;
36- bool heater_started ;
38+ volatile bool heater_started ;
3739 _Atomic heatpad_cs_state heatpad_cs_status ;
3840 _Atomic uint16_t pwm_pulse_duration ;
3941 uint16_t period_count ;
@@ -311,6 +313,7 @@ HEATPAD_CIRCUIT_ERROR heater_hardware_power_set(heater_hardware* hardware, uint1
311313 if (internal -> heatpad_cs_status == IDLE ) {
312314 internal -> heatpad_cs_status = RUNNING ;
313315 }
316+ taskENTER_CRITICAL ();
314317 if (!internal -> update_lock ) {
315318 internal -> pwm_pulse_duration = setting ;
316319 internal -> pwm_config .Pulse = setting ;
@@ -337,6 +340,7 @@ HEATPAD_CIRCUIT_ERROR heater_hardware_power_set(heater_hardware* hardware, uint1
337340 internal -> update_while_locked = true;
338341 internal -> cached_pulse_setting = setting ;
339342 }
343+ taskEXIT_CRITICAL ();
340344 return HEATPAD_CIRCUIT_NO_ERROR ;
341345}
342346
@@ -430,21 +434,21 @@ uint64_t heater_hardware_get_offset(size_t addr_offset) {
430434 return * (uint64_t * )AddressToRead ;
431435}
432436
433- // The HAL_TIM_OC_DelayElapsedCallback attempts to check for heatpad open and short
434- // circuit conditions via the heatpad current sensing pin once per second. TIM4
435- // channels 2 and 4 are used to trigger this callback at 10% and 90% of the heatpad
436- // pwm period. After one second has elapsed, on the first 90% period callback, the
437- // DAC and comparator settings are updated and the pwm pulse is prevented from
438- // updating. This is done to ensure the peripherals have sufficient time to update
439- // and checking conditions are undisturbed. The following period, if the heatpad pwm
440- // pulse is greater than 20% of the pwm period, the current sensing pin will be
441- // checked to be greater than 0.2V during the 10% callback to confirm there is no
442- // open circuit, and if the heatpad pwm pulse is less than 80% of the pwm period, the
443- // current sensing pin will be checked to be less than 0.2V during the 90% callback
444- // to confirm there is no shorted circuit. Following the last check, the update_lock
445- // is removed and the comparator threshold is returned to 3.2V to detect an overcurrent
446- // condition during the remainder of the one second checking period. Locking the pwm
447- // pulse updating eliminated false positives. If there is an error, the specific error
437+ // The HAL_TIM_OC_DelayElapsedCallback attempts to check for heatpad open and short
438+ // circuit conditions via the heatpad current sensing pin once per second. TIM4
439+ // channels 2 and 4 are used to trigger this callback at 10% and 90% of the heatpad
440+ // pwm period. After one second has elapsed, on the first 90% period callback, the
441+ // DAC and comparator settings are updated and the pwm pulse is prevented from
442+ // updating. This is done to ensure the peripherals have sufficient time to update
443+ // and checking conditions are undisturbed. The following period, if the heatpad pwm
444+ // pulse is greater than 20% of the pwm period, the current sensing pin will be
445+ // checked to be greater than 0.2V during the 10% callback to confirm there is no
446+ // open circuit, and if the heatpad pwm pulse is less than 80% of the pwm period, the
447+ // current sensing pin will be checked to be less than 0.2V during the 90% callback
448+ // to confirm there is no shorted circuit. Following the last check, the update_lock
449+ // is removed and the comparator threshold is returned to 3.2V to detect an overcurrent
450+ // condition during the remainder of the one second checking period. Locking the pwm
451+ // pulse updating eliminated false positives. If there is an error, the specific error
448452// will be reported back by heater_hardware_power_set and an error gcode will be produced.
449453void HAL_TIM_OC_DelayElapsedCallback (TIM_HandleTypeDef * htim )
450454{
@@ -542,7 +546,7 @@ void HAL_TIMEx_Break2Callback(TIM_HandleTypeDef *htim) {
542546void TIM4_IRQHandler (void )
543547{
544548 hw_internal * internal = (hw_internal * )HEATER_HW_HANDLE -> hardware_internal ;
545- HAL_TIM_IRQHandler (& internal -> pad_tim );
549+ HAL_TIM_IRQHandler (& internal -> pad_tim );
546550}
547551
548552static void init_error (void ) {
0 commit comments