From b32090075e079d78e7558ef3768eff3b5d76df6e Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 10 Sep 2023 10:24:43 +0200 Subject: [PATCH] timer emulation --- main.c | 9 +++++++++ stm32f1xx_hal.c | 17 +++++++++++++++++ stm32f1xx_hal.h | 8 ++++++-- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index a3e3bdb..aa9d9de 100644 --- a/main.c +++ b/main.c @@ -10,6 +10,7 @@ #include "PSE_unit.h" #include "home_screen.h" #include "PSE_unit.h" +#include "pse_stepper_planer.h" #define BUFF_SIZE (320 * 10) #define LCD_WIDTH 320 @@ -176,3 +177,11 @@ void * tick_thread (void *args){ lv_tick_inc(5); /*Tell LVGL that 5 milliseconds were elapsed*/ } } + +void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim){ + for(int i = 0; i < PSE_UNITS_NUM; i++){ + if(htim == pse_units[i].stepper_conf->tim){ + pse_stepper_planer_tick(&pse_units[i]); + } + } +} diff --git a/stm32f1xx_hal.c b/stm32f1xx_hal.c index 829be15..6eef2c5 100644 --- a/stm32f1xx_hal.c +++ b/stm32f1xx_hal.c @@ -1,5 +1,7 @@ #include "stm32f1xx_hal.h" +#include #include +#include void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin){ printf("toggling pin\n"); @@ -12,11 +14,26 @@ void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState Pin printf("writing pin\n"); } +static void* tim_handler(void* arg){ + TIM_HandleTypeDef* htim = arg; + while(htim->started){ + uint64_t delay_ps = 15625 * htim->presc * htim->period; + printf("delay : %lu\n", delay_ps); + usleep(delay_ps / 1000000); + HAL_TIM_PeriodElapsedCallback(htim); + } + return NULL; +} + HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim){ printf("starting timer\n"); + htim->started = 1; + pthread_t tim_thread; + pthread_create(&tim_thread, NULL, tim_handler, htim); return HAL_STATUS_OK; } HAL_StatusTypeDef HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim){ printf("stopping timer\n"); + htim->started = 0; return HAL_STATUS_OK; } diff --git a/stm32f1xx_hal.h b/stm32f1xx_hal.h index 5ebe6a0..65db0c7 100644 --- a/stm32f1xx_hal.h +++ b/stm32f1xx_hal.h @@ -7,8 +7,10 @@ typedef struct { uint8_t index; } GPIO_TypeDef; -typedef struct -{ +typedef struct{ + uint16_t presc; + uint16_t period; + uint8_t started; } TIM_HandleTypeDef; typedef int HAL_StatusTypeDef; @@ -71,3 +73,5 @@ void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState Pin HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim); HAL_StatusTypeDef HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim); + +void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim);