From 56cae1abdbb1e6cd891683cabc493dfea91a9c43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Mon, 13 Mar 2023 00:38:06 +0100 Subject: [PATCH] events and exti --- BluepillTrouba.ioc | 29 +++++++++++++------ Core/Inc/main.h | 3 ++ Core/Inc/stm32f1xx_it.h | 1 + Core/Src/app_gui.c | 12 ++++++-- Core/Src/app_gui.h | 15 ++++++++++ Core/Src/app_heater.c | 13 +++++++++ Core/Src/app_heater.h | 6 ++++ Core/Src/app_knob.c | 63 ++++++++++++++++++++++++++++++++++++++++- Core/Src/app_knob.h | 5 ++++ Core/Src/app_temp.c | 5 ++-- Core/Src/freertos.c | 27 ++++++++++++++++-- Core/Src/gpio.c | 24 +++++++++++++--- Core/Src/stm32f1xx_it.c | 32 ++++++++++++++++++++- Core/Src/tim.c | 20 +++++++++---- Core/Src/usart.c | 3 +- Makefile | 2 +- 16 files changed, 230 insertions(+), 30 deletions(-) diff --git a/BluepillTrouba.ioc b/BluepillTrouba.ioc index b739ee3..665bb6d 100644 --- a/BluepillTrouba.ioc +++ b/BluepillTrouba.ioc @@ -31,9 +31,9 @@ Dma.RequestsNb=1 FREERTOS.FootprintOK=true FREERTOS.IPParameters=Tasks01,configUSE_MALLOC_FAILED_HOOK,configCHECK_FOR_STACK_OVERFLOW,configUSE_NEWLIB_REENTRANT,FootprintOK,Mutexes01,Queues01,Timers01,configUSE_TRACE_FACILITY,configUSE_STATS_FORMATTING_FUNCTIONS,configENABLE_BACKWARD_COMPATIBILITY FREERTOS.Mutexes01=heaterMutex,Static,heaterMutexControlBlock -FREERTOS.Queues01=guiEventQue,16,8,1,Static,guiEventQueBuffer,guiEventQueControlBlock +FREERTOS.Queues01=guiEventQue,32,1,1,Static,guiEventQueBuffer,guiEventQueControlBlock FREERTOS.Tasks01=mainTsk,24,256,app_task_main,As weak,NULL,Static,defaultTaskBuffer,defaultTaskControlBlock;heaterTsk,24,128,app_task_heater,As external,NULL,Static,heaterTskBuffer,heaterTskControlBlock;guiTsk,40,256,app_task_gui,As external,NULL,Static,guiTskBuffer,guiTskControlBlock -FREERTOS.Timers01=beepTimer,app_beep_end,osTimerOnce,As external,NULL,Static,beepTimerControlBlock +FREERTOS.Timers01=beepTimer,app_beep_end,osTimerOnce,As external,NULL,Static,beepTimerControlBlock;buttonPushTimer,app_push_debounce,osTimerOnce,As external,1,Static,buttonPushTimerControlBlock;buttonReleaseTimer,app_push_debounce,osTimerOnce,As external,0,Static,buttonReleaseTimerControlBlock FREERTOS.configCHECK_FOR_STACK_OVERFLOW=2 FREERTOS.configENABLE_BACKWARD_COMPATIBILITY=0 FREERTOS.configUSE_MALLOC_FAILED_HOOK=1 @@ -41,7 +41,7 @@ FREERTOS.configUSE_NEWLIB_REENTRANT=1 FREERTOS.configUSE_STATS_FORMATTING_FUNCTIONS=1 FREERTOS.configUSE_TRACE_FACILITY=1 File.Version=6 -GPIO.groupedBy=Group By Peripherals +GPIO.groupedBy=Show All IWDG.IPParameters=Prescaler,Reload IWDG.Prescaler=IWDG_PRESCALER_256 IWDG.Reload=624 @@ -99,6 +99,7 @@ MxDb.Version=DB.6.0.50 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:true NVIC.DMA1_Channel1_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true\:true NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:true +NVIC.EXTI9_5_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true NVIC.ForceEnableDMAVector=true NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:true NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:true @@ -117,6 +118,8 @@ PA1.GPIOParameters=GPIO_Label PA1.GPIO_Label=ADC_SENSR PA1.Locked=true PA1.Signal=ADCx_IN1 +PA10.GPIOParameters=GPIO_PuPd +PA10.GPIO_PuPd=GPIO_PULLUP PA10.Mode=Asynchronous PA10.Signal=USART1_RX PA13.Locked=true @@ -129,6 +132,8 @@ PA15.GPIOParameters=GPIO_Speed,GPIO_Label PA15.GPIO_Label=BUZZER PA15.GPIO_Speed=GPIO_SPEED_FREQ_HIGH PA15.Signal=S_TIM2_CH1_ETR +PA6.GPIOParameters=GPIO_Label +PA6.GPIO_Label=PWM_HEATER PA6.Locked=true PA6.Signal=S_TIM3_CH1 PA9.Mode=Asynchronous @@ -159,10 +164,12 @@ PB7.GPIOParameters=GPIO_PuPd,GPIO_Label PB7.GPIO_Label=KNOB_B PB7.GPIO_PuPd=GPIO_PULLUP PB7.Signal=S_TIM4_CH2 -PB8.GPIOParameters=GPIO_Label +PB8.GPIOParameters=GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultEXTI PB8.GPIO_Label=KNOB_PUSH +PB8.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_RISING_FALLING +PB8.GPIO_PuPd=GPIO_PULLUP PB8.Locked=true -PB8.Signal=GPIO_Input +PB8.Signal=GPXTI8 PC13-TAMPER-RTC.GPIOParameters=GPIO_Label PC13-TAMPER-RTC.GPIO_Label=LED PC13-TAMPER-RTC.Locked=true @@ -223,6 +230,8 @@ SH.ADCx_IN0.0=ADC1_IN0,IN0 SH.ADCx_IN0.ConfNb=1 SH.ADCx_IN1.0=ADC1_IN1,IN1 SH.ADCx_IN1.ConfNb=1 +SH.GPXTI8.0=GPIO_EXTI8 +SH.GPXTI8.ConfNb=1 SH.S_TIM2_CH1_ETR.0=TIM2_CH1,PWM Generation1 CH1 SH.S_TIM2_CH1_ETR.ConfNb=1 SH.S_TIM3_CH1.0=TIM3_CH1,PWM Generation1 CH1 @@ -238,9 +247,10 @@ SPI2.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,BaudRatePrescaler SPI2.Mode=SPI_MODE_MASTER SPI2.VirtualType=VM_MASTER TIM1.Channel-PWM\ Generation1\ No\ Output=TIM_CHANNEL_1 -TIM1.IPParameters=Prescaler,Period,TIM_MasterOutputTrigger,Channel-PWM Generation1 No Output -TIM1.Period=100 +TIM1.IPParameters=Prescaler,Period,TIM_MasterOutputTrigger,Channel-PWM Generation1 No Output,Pulse-PWM Generation1 No Output +TIM1.Period=10 TIM1.Prescaler=64000 +TIM1.Pulse-PWM\ Generation1\ No\ Output=1 TIM1.TIM_MasterOutputTrigger=TIM_TRGO_OC1REF TIM2.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 TIM2.IPParameters=Channel-PWM Generation1 CH1,Prescaler @@ -248,10 +258,11 @@ TIM2.Prescaler=2 TIM3.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 TIM3.IPParameters=Channel-PWM Generation1 CH1,Prescaler,Pulse-PWM Generation1 CH1,Period TIM3.Period=64000 -TIM3.Prescaler=2000 +TIM3.Prescaler=1000 TIM3.Pulse-PWM\ Generation1\ CH1=0 +TIM4.IC1Filter=15 TIM4.IC2Filter=15 -TIM4.IPParameters=IC2Filter,Prescaler +TIM4.IPParameters=IC2Filter,Prescaler,IC1Filter TIM4.Prescaler=0 USART1.IPParameters=VirtualMode,Mode USART1.Mode=MODE_TX_RX diff --git a/Core/Inc/main.h b/Core/Inc/main.h index d4be7db..07639ad 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -81,6 +81,8 @@ void Error_Handler(void); #define LED_GPIO_Port GPIOC #define ADC_SENSR_Pin LL_GPIO_PIN_1 #define ADC_SENSR_GPIO_Port GPIOA +#define PWM_HEATER_Pin LL_GPIO_PIN_6 +#define PWM_HEATER_GPIO_Port GPIOA #define OLED_CS_Pin LL_GPIO_PIN_0 #define OLED_CS_GPIO_Port GPIOB #define OLED_DC_Pin LL_GPIO_PIN_1 @@ -95,6 +97,7 @@ void Error_Handler(void); #define KNOB_B_GPIO_Port GPIOB #define KNOB_PUSH_Pin LL_GPIO_PIN_8 #define KNOB_PUSH_GPIO_Port GPIOB +#define KNOB_PUSH_EXTI_IRQn EXTI9_5_IRQn #ifndef NVIC_PRIORITYGROUP_0 #define NVIC_PRIORITYGROUP_0 ((uint32_t)0x00000007) /*!< 0 bit for pre-emption priority, 4 bits for subpriority */ diff --git a/Core/Inc/stm32f1xx_it.h b/Core/Inc/stm32f1xx_it.h index ec7462b..fdcc2a7 100644 --- a/Core/Inc/stm32f1xx_it.h +++ b/Core/Inc/stm32f1xx_it.h @@ -54,6 +54,7 @@ void UsageFault_Handler(void); void DebugMon_Handler(void); void SysTick_Handler(void); void DMA1_Channel1_IRQHandler(void); +void EXTI9_5_IRQHandler(void); void TIM4_IRQHandler(void); /* USER CODE BEGIN EFP */ diff --git a/Core/Src/app_gui.c b/Core/Src/app_gui.c index 1bb3dcb..75f171d 100644 --- a/Core/Src/app_gui.c +++ b/Core/Src/app_gui.c @@ -2,13 +2,21 @@ * TODO file description */ +#include #include "app_gui.h" -#include "cmsis_os2.h" #include "FreeRTOS.h" #include "task.h" +#include "queue.h" void app_task_gui(void *argument) { + PRINTF("app_task_gui\r\n"); while (1) { - vTaskDelay(pdMS_TO_TICKS(1000)); + uint8_t message = 0; + bool suc = osMessageQueueGet(guiEventQueHandle, &message, NULL, pdMS_TO_TICKS(1000)); + + if (suc == osOK && (message != GUI_NOTIFY_TEMP_CHANGE)) { + // input events + PRINTF("Event %d\r\n", message); + } } } diff --git a/Core/Src/app_gui.h b/Core/Src/app_gui.h index 722aa54..287a88c 100644 --- a/Core/Src/app_gui.h +++ b/Core/Src/app_gui.h @@ -5,6 +5,21 @@ #ifndef BLUEPILLTROUBA_APP_GUI_H #define BLUEPILLTROUBA_APP_GUI_H +#include "cmsis_os2.h" + +extern osThreadId_t guiTskHandle; +extern osMessageQueueId_t guiEventQueHandle; + void app_task_gui(void *argument); +// sent through the notify queue +enum GuiNotify { + GUI_EVENT_NONE = 0, + GUI_NOTIFY_KNOB_PLUS, + GUI_NOTIFY_KNOB_MINUS, + GUI_NOTIFY_KNOB_PRESS, + GUI_NOTIFY_KNOB_RELEASE, + GUI_NOTIFY_TEMP_CHANGE, +}; + #endif //BLUEPILLTROUBA_APP_GUI_H diff --git a/Core/Src/app_heater.c b/Core/Src/app_heater.c index 3dcebfb..e464c67 100644 --- a/Core/Src/app_heater.c +++ b/Core/Src/app_heater.c @@ -7,6 +7,8 @@ #include "app_heater.h" #include "cmsis_os2.h" #include "tim.h" +#include "queue.h" +#include "app_gui.h" extern osMutexId_t heaterMutexHandle; @@ -79,6 +81,14 @@ void app_heater_set_target(float target) { heaterExitCritical(); } +void app_heater_emergency_shutdown() { + PID_SetCtlMode(&state.pid, PID_MANUAL); + + // TODO check if this really turns the channel off! + LL_TIM_OC_SetCompareCH1(TIM_HEATER, 0); + LL_TIM_CC_DisableChannel(TIM_HEATER, LL_TIM_CHANNEL_CH1); +} + void app_task_heater(void *argument) { heater_pwm_init(); @@ -96,6 +106,9 @@ void app_task_heater(void *argument) state.oven_temp = app_temp_read_oven(); state.soc_temp = app_temp_read_soc(); + enum GuiNotify ev = GUI_NOTIFY_TEMP_CHANGE; + xQueueSend(guiEventQueHandle, &ev, pdMS_TO_TICKS(100)); + heaterEnterCritical(); PID_Compute(&state.pid, state.oven_temp); if (state.pid.ctlMode == PID_AUTOMATIC) { diff --git a/Core/Src/app_heater.h b/Core/Src/app_heater.h index 9b80fc6..deae371 100644 --- a/Core/Src/app_heater.h +++ b/Core/Src/app_heater.h @@ -6,6 +6,9 @@ #define BLUEPILLTROUBA_APP_HEATER_H #include +#include "cmsis_os2.h" + +extern osThreadId_t heaterTskHandle; void app_task_heater(void *argument); @@ -21,4 +24,7 @@ void app_heater_enable(bool enable); /// Mutex is locked internally. void app_heater_set_target(float target); +/// Shutdown the heater; This function does not use mutex and just disables the PWM via register access. +void app_heater_emergency_shutdown(); + #endif //BLUEPILLTROUBA_APP_HEATER_H diff --git a/Core/Src/app_knob.c b/Core/Src/app_knob.c index 769d700..8834631 100644 --- a/Core/Src/app_knob.c +++ b/Core/Src/app_knob.c @@ -6,6 +6,13 @@ #include "main.h" #include "app_knob.h" #include "tim.h" +#include "app_gui.h" +#include "FreeRTOS.h" +#include "queue.h" +#include "timers.h" + +extern osTimerId_t buttonPushTimerHandle; +extern osTimerId_t buttonReleaseTimerHandle; static struct { uint16_t wheel; @@ -18,14 +25,68 @@ void app_knob_init() LL_TIM_CC_EnableChannel(TIM_KNOB, LL_TIM_CHANNEL_CH1 | LL_TIM_CHANNEL_CH2); LL_TIM_EnableCounter(TIM_KNOB); -// HAL_TIM_Encoder_Start(&htim4, TIM_CHANNEL_ALL); + // for the change interrupt + LL_TIM_EnableIT_CC1(TIM_KNOB); + LL_TIM_EnableIT_CC2(TIM_KNOB); } uint16_t app_knob_get_raw() { return LL_TIM_GetCounter(TIM_KNOB); } +static char buf[100]; + +void app_knob_turn_isr() +{ + // TODO + uint16_t old_wheel = s_knob.wheel; + s_knob.wheel = LL_TIM_GetCounter(TIM_KNOB); + + int16_t wheel_change = (int16_t)(s_knob.wheel - old_wheel); + + BaseType_t yield = pdFALSE; + + while (wheel_change > 0) { + wheel_change--; + enum GuiNotify ev = GUI_NOTIFY_KNOB_PLUS; + xQueueSendFromISR(guiEventQueHandle, &ev, &yield); + } + + while (wheel_change < 0) { + wheel_change++; + enum GuiNotify ev = GUI_NOTIFY_KNOB_MINUS; + xQueueSendFromISR(guiEventQueHandle, &ev, &yield); + } + + portYIELD_FROM_ISR(yield); +} + + +// TODO use EXTI for push bool app_knob_pushed() { return 0 == LL_GPIO_IsInputPinSet(KNOB_PUSH_GPIO_Port, KNOB_PUSH_Pin); } + +void app_knob_push_isr(bool push) +{ + PUTCHAR(push ? '#' : '.'); + + BaseType_t yield = pdFALSE; + if (push) { + xTimerStopFromISR(buttonReleaseTimerHandle, &yield); + xTimerStopFromISR(buttonPushTimerHandle, &yield); + xTimerChangePeriodFromISR(buttonPushTimerHandle, pdMS_TO_TICKS(10), &yield); + } else { + xTimerStopFromISR(buttonPushTimerHandle, &yield); + xTimerStopFromISR(buttonReleaseTimerHandle, &yield); + xTimerChangePeriodFromISR(buttonReleaseTimerHandle, pdMS_TO_TICKS(10), &yield); + } + portYIELD_FROM_ISR(yield); +} + +void app_push_debounce(void *argument) { + bool push = (bool) argument; + enum GuiNotify ev = push ? GUI_NOTIFY_KNOB_PRESS : GUI_NOTIFY_KNOB_RELEASE; + xQueueSend(guiEventQueHandle, &ev, pdMS_TO_TICKS(100)); +} diff --git a/Core/Src/app_knob.h b/Core/Src/app_knob.h index e7c0eb6..36c7b1a 100644 --- a/Core/Src/app_knob.h +++ b/Core/Src/app_knob.h @@ -5,9 +5,14 @@ #ifndef BLUEPILLTROUBA_APP_KNOB_H #define BLUEPILLTROUBA_APP_KNOB_H +#include + void app_knob_init(); uint16_t app_knob_get_raw(); bool app_knob_pushed(); +void app_knob_turn_isr(); +void app_knob_push_isr(bool push); + #endif //BLUEPILLTROUBA_APP_KNOB_H diff --git a/Core/Src/app_temp.c b/Core/Src/app_temp.c index ca3f981..0f88f74 100644 --- a/Core/Src/app_temp.c +++ b/Core/Src/app_temp.c @@ -158,11 +158,10 @@ void app_analog_init() LL_ADC_REG_StartConversionExtTrig(ADC_TEMP, LL_ADC_REG_TRIG_EXT_RISING); // LL_ADC_REG_StartConversionSWStart(ADC_TEMP); + // this timer runs with 1 kHz clock LL_TIM_SetTriggerOutput(TIM1, LL_TIM_TRGO_CC1IF); LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH1); - LL_TIM_OC_SetCompareCH1(TIM1, 10); - LL_TIM_SetAutoReload(TIM1, 20); - LL_TIM_EnableAllOutputs(TIM1); + LL_TIM_EnableAllOutputs(TIM1); // TIM1 needs outputs specially enabled - it's "advanced timer" LL_TIM_EnableCounter(TIM1); } diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index dc79c68..28176cf 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -90,7 +90,7 @@ const osThreadAttr_t guiTsk_attributes = { }; /* Definitions for guiEventQue */ osMessageQueueId_t guiEventQueHandle; -uint8_t guiEventQueBuffer[ 16 * 8 ]; +uint8_t guiEventQueBuffer[ 32 * 1 ]; osStaticMessageQDef_t guiEventQueControlBlock; const osMessageQueueAttr_t guiEventQue_attributes = { .name = "guiEventQue", @@ -107,6 +107,22 @@ const osTimerAttr_t beepTimer_attributes = { .cb_mem = &beepTimerControlBlock, .cb_size = sizeof(beepTimerControlBlock), }; +/* Definitions for buttonPushTimer */ +osTimerId_t buttonPushTimerHandle; +osStaticTimerDef_t buttonPushTimerControlBlock; +const osTimerAttr_t buttonPushTimer_attributes = { + .name = "buttonPushTimer", + .cb_mem = &buttonPushTimerControlBlock, + .cb_size = sizeof(buttonPushTimerControlBlock), +}; +/* Definitions for buttonReleaseTimer */ +osTimerId_t buttonReleaseTimerHandle; +osStaticTimerDef_t buttonReleaseTimerControlBlock; +const osTimerAttr_t buttonReleaseTimer_attributes = { + .name = "buttonReleaseTimer", + .cb_mem = &buttonReleaseTimerControlBlock, + .cb_size = sizeof(buttonReleaseTimerControlBlock), +}; /* Definitions for heaterMutex */ osMutexId_t heaterMutexHandle; osStaticMutexDef_t heaterMutexControlBlock; @@ -125,6 +141,7 @@ void app_task_main(void *argument); extern void app_task_heater(void *argument); extern void app_task_gui(void *argument); extern void app_beep_end(void *argument); +extern void app_push_debounce(void *argument); void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ @@ -184,13 +201,19 @@ void MX_FREERTOS_Init(void) { /* creation of beepTimer */ beepTimerHandle = osTimerNew(app_beep_end, osTimerOnce, NULL, &beepTimer_attributes); + /* creation of buttonPushTimer */ + buttonPushTimerHandle = osTimerNew(app_push_debounce, osTimerOnce, (void*) 1, &buttonPushTimer_attributes); + + /* creation of buttonReleaseTimer */ + buttonReleaseTimerHandle = osTimerNew(app_push_debounce, osTimerOnce, (void*) 0, &buttonReleaseTimer_attributes); + /* USER CODE BEGIN RTOS_TIMERS */ /* start timers, add new ones, ... */ /* USER CODE END RTOS_TIMERS */ /* Create the queue(s) */ /* creation of guiEventQue */ - guiEventQueHandle = osMessageQueueNew (16, 8, &guiEventQue_attributes); + guiEventQueHandle = osMessageQueueNew (32, 1, &guiEventQue_attributes); /* USER CODE BEGIN RTOS_QUEUES */ /* add queues, ... */ diff --git a/Core/Src/gpio.c b/Core/Src/gpio.c index 3a0914a..12e31a0 100644 --- a/Core/Src/gpio.c +++ b/Core/Src/gpio.c @@ -44,6 +44,7 @@ void MX_GPIO_Init(void) { + LL_EXTI_InitTypeDef EXTI_InitStruct = {0}; LL_GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ @@ -98,12 +99,27 @@ void MX_GPIO_Init(void) LL_GPIO_Init(GPIOB, &GPIO_InitStruct); /**/ - GPIO_InitStruct.Pin = KNOB_PUSH_Pin; - GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING; - LL_GPIO_Init(KNOB_PUSH_GPIO_Port, &GPIO_InitStruct); + LL_GPIO_AF_EnableRemap_PD01(); /**/ - LL_GPIO_AF_EnableRemap_PD01(); + LL_GPIO_AF_SetEXTISource(LL_GPIO_AF_EXTI_PORTB, LL_GPIO_AF_EXTI_LINE8); + + /**/ + EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_8; + EXTI_InitStruct.LineCommand = ENABLE; + EXTI_InitStruct.Mode = LL_EXTI_MODE_IT; + EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_RISING_FALLING; + LL_EXTI_Init(&EXTI_InitStruct); + + /**/ + LL_GPIO_SetPinPull(KNOB_PUSH_GPIO_Port, KNOB_PUSH_Pin, LL_GPIO_PULL_UP); + + /**/ + LL_GPIO_SetPinMode(KNOB_PUSH_GPIO_Port, KNOB_PUSH_Pin, LL_GPIO_MODE_INPUT); + + /* EXTI interrupt init*/ + NVIC_SetPriority(EXTI9_5_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),5, 0)); + NVIC_EnableIRQ(EXTI9_5_IRQn); } diff --git a/Core/Src/stm32f1xx_it.c b/Core/Src/stm32f1xx_it.c index a0d9cc7..7e8428b 100644 --- a/Core/Src/stm32f1xx_it.c +++ b/Core/Src/stm32f1xx_it.c @@ -24,8 +24,8 @@ #include "task.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ -#include #include "app_temp.h" +#include "app_knob.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -206,6 +206,29 @@ void DMA1_Channel1_IRQHandler(void) /* USER CODE END DMA1_Channel1_IRQn 1 */ } +/** + * @brief This function handles EXTI line[9:5] interrupts. + */ +void EXTI9_5_IRQHandler(void) +{ + /* USER CODE BEGIN EXTI9_5_IRQn 0 */ + bool state = LL_GPIO_IsInputPinSet(KNOB_PUSH_GPIO_Port, KNOB_PUSH_Pin); + + /* USER CODE END EXTI9_5_IRQn 0 */ + if (LL_EXTI_IsActiveFlag_0_31(LL_EXTI_LINE_8) != RESET) + { + LL_EXTI_ClearFlag_0_31(LL_EXTI_LINE_8); + /* USER CODE BEGIN LL_EXTI_LINE_8 */ + + app_knob_push_isr(0 == state); + + /* USER CODE END LL_EXTI_LINE_8 */ + } + /* USER CODE BEGIN EXTI9_5_IRQn 1 */ + + /* USER CODE END EXTI9_5_IRQn 1 */ +} + /** * @brief This function handles TIM4 global interrupt. */ @@ -213,6 +236,13 @@ void TIM4_IRQHandler(void) { /* USER CODE BEGIN TIM4_IRQn 0 */ + if (LL_TIM_IsActiveFlag_CC1(TIM4) || LL_TIM_IsActiveFlag_CC2(TIM4)) { + LL_TIM_ClearFlag_CC1(TIM4); + LL_TIM_ClearFlag_CC2(TIM4); + app_knob_turn_isr(); + } + + /* USER CODE END TIM4_IRQn 0 */ /* USER CODE BEGIN TIM4_IRQn 1 */ diff --git a/Core/Src/tim.c b/Core/Src/tim.c index 3021269..5f1c87e 100644 --- a/Core/Src/tim.c +++ b/Core/Src/tim.c @@ -30,6 +30,8 @@ void MX_TIM1_Init(void) /* USER CODE BEGIN TIM1_Init 0 */ + // --- this timer is used to time ADC sampling. --- + /* USER CODE END TIM1_Init 0 */ LL_TIM_InitTypeDef TIM_InitStruct = {0}; @@ -44,7 +46,7 @@ void MX_TIM1_Init(void) /* USER CODE END TIM1_Init 1 */ TIM_InitStruct.Prescaler = 64000; TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; - TIM_InitStruct.Autoreload = 100; + TIM_InitStruct.Autoreload = 10; TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1; TIM_InitStruct.RepetitionCounter = 0; LL_TIM_Init(TIM1, &TIM_InitStruct); @@ -54,7 +56,7 @@ void MX_TIM1_Init(void) TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1; TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE; TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE; - TIM_OC_InitStruct.CompareValue = 0; + TIM_OC_InitStruct.CompareValue = 1; TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH; TIM_OC_InitStruct.OCNPolarity = LL_TIM_OCPOLARITY_HIGH; TIM_OC_InitStruct.OCIdleState = LL_TIM_OCIDLESTATE_LOW; @@ -82,6 +84,8 @@ void MX_TIM2_Init(void) /* USER CODE BEGIN TIM2_Init 0 */ + // --- this timer generates the buzzer PWM. The PWM parameters are adjusted on the fly --- + /* USER CODE END TIM2_Init 0 */ LL_TIM_InitTypeDef TIM_InitStruct = {0}; @@ -133,6 +137,8 @@ void MX_TIM3_Init(void) /* USER CODE BEGIN TIM3_Init 0 */ + // --- this timer generates the heater PWM. The duty is set as 640 * percent --- + /* USER CODE END TIM3_Init 0 */ LL_TIM_InitTypeDef TIM_InitStruct = {0}; @@ -145,7 +151,7 @@ void MX_TIM3_Init(void) /* USER CODE BEGIN TIM3_Init 1 */ /* USER CODE END TIM3_Init 1 */ - TIM_InitStruct.Prescaler = 2000; + TIM_InitStruct.Prescaler = 1000; TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; TIM_InitStruct.Autoreload = 64000; TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1; @@ -169,11 +175,11 @@ void MX_TIM3_Init(void) /**TIM3 GPIO Configuration PA6 ------> TIM3_CH1 */ - GPIO_InitStruct.Pin = LL_GPIO_PIN_6; + GPIO_InitStruct.Pin = PWM_HEATER_Pin; GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL; - LL_GPIO_Init(GPIOA, &GPIO_InitStruct); + LL_GPIO_Init(PWM_HEATER_GPIO_Port, &GPIO_InitStruct); } /* TIM4 init function */ @@ -182,6 +188,8 @@ void MX_TIM4_Init(void) /* USER CODE BEGIN TIM4_Init 0 */ + // --- This timer captures the rotary encoder. The count must be divided by 4, but it can't be done by the hardware, doesn't work well. --- + /* USER CODE END TIM4_Init 0 */ LL_TIM_InitTypeDef TIM_InitStruct = {0}; @@ -211,7 +219,7 @@ void MX_TIM4_Init(void) LL_TIM_SetEncoderMode(TIM4, LL_TIM_ENCODERMODE_X2_TI1); LL_TIM_IC_SetActiveInput(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_DIRECTTI); LL_TIM_IC_SetPrescaler(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1); - LL_TIM_IC_SetFilter(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV1); + LL_TIM_IC_SetFilter(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV32_N8); LL_TIM_IC_SetPolarity(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_RISING); LL_TIM_IC_SetActiveInput(TIM4, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_DIRECTTI); LL_TIM_IC_SetPrescaler(TIM4, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV1); diff --git a/Core/Src/usart.c b/Core/Src/usart.c index a631429..67f45d5 100644 --- a/Core/Src/usart.c +++ b/Core/Src/usart.c @@ -51,7 +51,8 @@ void MX_USART1_UART_Init(void) LL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = LL_GPIO_PIN_10; - GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING; + GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = LL_GPIO_PULL_UP; LL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN USART1_Init 1 */ diff --git a/Makefile b/Makefile index 653a776..f94a3f5 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [3.16.0] date: [Sun Mar 12 21:45:54 CET 2023] +# File automatically-generated by tool: [projectgenerator] version: [3.16.0] date: [Mon Mar 13 00:15:21 CET 2023] ########################################################################################################################## # ------------------------------------------------