diff --git a/Core/Inc/shorttimer.h b/Core/Inc/shorttimer.h new file mode 100644 index 0000000..9700bf3 --- /dev/null +++ b/Core/Inc/shorttimer.h @@ -0,0 +1,28 @@ +#ifndef INC_SHORTTIMER_H +#define INC_SHORTTIMER_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Timer with an arbitrary period. + * + * shorttimer_callback() must be called in HAL_TIM_PeriodElapsedCallback. + */ + +#include "stm32h7xx_hal.h" + +#include + +void shorttimer_init(TIM_HandleTypeDef *handle); + +uint32_t shorttimer_gettick(void); +void shorttimer_sleep(uint32_t microseconds); +void shorttimer_callback(void); + +#ifdef __cplusplus +} +#endif + +#endif // INC_SHORTTIMER_H diff --git a/Core/Inc/stm32h7xx_it.h b/Core/Inc/stm32h7xx_it.h index c1e0dbd..94da4e5 100644 --- a/Core/Inc/stm32h7xx_it.h +++ b/Core/Inc/stm32h7xx_it.h @@ -59,6 +59,7 @@ void FDCAN1_IT1_IRQHandler(void); void EXTI9_5_IRQHandler(void); void TIM6_DAC_IRQHandler(void); void LTDC_IRQHandler(void); +void TIM17_IRQHandler(void); /* USER CODE BEGIN EFP */ /* USER CODE END EFP */ diff --git a/Core/Src/hx8357d.c b/Core/Src/hx8357d.c index d980346..ae1d00c 100644 --- a/Core/Src/hx8357d.c +++ b/Core/Src/hx8357d.c @@ -1,6 +1,7 @@ #include "hx8357d.h" #include "main.h" +#include "shorttimer.h" void HX8357D_WriteData(uint8_t *data, size_t data_len) { for (size_t byte = 0; byte < data_len; byte++) { @@ -9,9 +10,9 @@ void HX8357D_WriteData(uint8_t *data, size_t data_len) { ((data[byte] >> (7 - bit)) & 1) ? GPIO_PIN_SET : GPIO_PIN_RESET; HAL_GPIO_WritePin(DISPSPI_SCL_GPIO_Port, DISPSPI_SCL_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(DISPSPI_SDA_GPIO_Port, DISPSPI_SDA_Pin, state); - HAL_Delay(1); + shorttimer_sleep(1); HAL_GPIO_WritePin(DISPSPI_SCL_GPIO_Port, DISPSPI_SCL_Pin, GPIO_PIN_SET); - HAL_Delay(1); + shorttimer_sleep(1); } } } @@ -19,11 +20,11 @@ void HX8357D_WriteData(uint8_t *data, size_t data_len) { void HX8357D_WriteReg(uint8_t addr, uint8_t *data, size_t data_len) { HAL_GPIO_WritePin(DISPSPI_CSX_GPIO_Port, DISPSPI_CSX_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(DISPSPI_DCX_GPIO_Port, DISPSPI_DCX_Pin, GPIO_PIN_RESET); - HAL_Delay(1); + shorttimer_sleep(1); HX8357D_WriteData(&addr, 1); HAL_GPIO_WritePin(DISPSPI_DCX_GPIO_Port, DISPSPI_DCX_Pin, GPIO_PIN_SET); - HAL_Delay(1); + shorttimer_sleep(1); if (data_len > 0) { HX8357D_WriteData(data, data_len); diff --git a/Core/Src/main.c b/Core/Src/main.c index 272486a..2726d01 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -26,6 +26,8 @@ #include "ft_logo_orange_rgb565.h" #include "ft_logo_rainbow_rgb565.h" #include "hx8357d.h" +#include "shorttimer.h" + #include /* USER CODE END Includes */ @@ -63,8 +65,10 @@ SPI_HandleTypeDef hspi3; TIM_HandleTypeDef htim1; TIM_HandleTypeDef htim2; TIM_HandleTypeDef htim4; +TIM_HandleTypeDef htim17; /* USER CODE BEGIN PV */ +TIM_HandleTypeDef *htim_us = &htim17; volatile int ltdc_cb_triggered; /* USER CODE END PV */ @@ -81,6 +85,7 @@ static void MX_TIM1_Init(void); static void MX_TIM2_Init(void); static void MX_TIM4_Init(void); static void MX_CRC_Init(void); +static void MX_TIM17_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ @@ -128,14 +133,17 @@ int main(void) { MX_TIM2_Init(); MX_TIM4_Init(); MX_CRC_Init(); + MX_TIM17_Init(); /* Call PreOsInit function */ MX_TouchGFX_PreOSInit(); /* USER CODE BEGIN 2 */ if (HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4) != HAL_OK) { Error_Handler(); } + shorttimer_init(htim_us); HX8357D_Init(); + HX8357D_Mode_RGB666(); /* USER CODE END 2 */ @@ -678,6 +686,35 @@ static void MX_TIM4_Init(void) { HAL_TIM_MspPostInit(&htim4); } +/** + * @brief TIM17 Initialization Function + * @param None + * @retval None + */ +static void MX_TIM17_Init(void) { + + /* USER CODE BEGIN TIM17_Init 0 */ + + /* USER CODE END TIM17_Init 0 */ + + /* USER CODE BEGIN TIM17_Init 1 */ + + /* USER CODE END TIM17_Init 1 */ + htim17.Instance = TIM17; + htim17.Init.Prescaler = 16 - 1; + htim17.Init.CounterMode = TIM_COUNTERMODE_UP; + htim17.Init.Period = 1000 - 1; + htim17.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim17.Init.RepetitionCounter = 0; + htim17.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_Base_Init(&htim17) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN TIM17_Init 2 */ + + /* USER CODE END TIM17_Init 2 */ +} + /** * @brief GPIO Initialization Function * @param None @@ -772,7 +809,10 @@ static void MX_GPIO_Init(void) { } /* USER CODE BEGIN 4 */ - +// Ugly hack to ensure HAL_TIM_PeriodElapsedCallback() is called +#ifdef __cplusplus +extern "C" { +#endif /* USER CODE END 4 */ /** @@ -791,7 +831,9 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { HAL_IncTick(); } /* USER CODE BEGIN Callback 1 */ - + if (htim->Instance == htim_us->Instance) { + shorttimer_callback(); + } /* USER CODE END Callback 1 */ } @@ -805,6 +847,13 @@ void Error_Handler(void) { __disable_irq(); while (1) { } + // Ugly hack to ensure HAL_TIM_PeriodElapsedCallback() is called +} +#ifdef __cplusplus +} +#endif +void _damnyoucubemx() { + ; /* USER CODE END Error_Handler_Debug */ } diff --git a/Core/Src/shorttimer.c b/Core/Src/shorttimer.c new file mode 100644 index 0000000..a914f35 --- /dev/null +++ b/Core/Src/shorttimer.c @@ -0,0 +1,29 @@ +#include "shorttimer.h" + +#include "stm32h7xx_hal.h" +#include "stm32h7xx_hal_tim.h" +#include + +static TIM_HandleTypeDef *htim; +static uint32_t ticks; + +void shorttimer_init(TIM_HandleTypeDef *handle) { + htim = handle; + ticks = 0; + HAL_TIM_Base_Start_IT(htim); +} + +uint32_t shorttimer_gettick(void) { return ticks; } + +void shorttimer_sleep(uint32_t microseconds) { + volatile uint32_t start = shorttimer_gettick(); + // Add another tick to guarantee a minimum wait + if (microseconds < UINT32_MAX) { + microseconds++; + } + + while (shorttimer_gettick() - start < microseconds) { + } +} + +void shorttimer_callback() { ticks++; } diff --git a/Core/Src/stm32h7xx_hal_msp.c b/Core/Src/stm32h7xx_hal_msp.c index 7590378..abb3409 100644 --- a/Core/Src/stm32h7xx_hal_msp.c +++ b/Core/Src/stm32h7xx_hal_msp.c @@ -714,6 +714,20 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) /* USER CODE END TIM2_MspInit 1 */ } + else if(htim_base->Instance==TIM17) + { + /* USER CODE BEGIN TIM17_MspInit 0 */ + + /* USER CODE END TIM17_MspInit 0 */ + /* Peripheral clock enable */ + __HAL_RCC_TIM17_CLK_ENABLE(); + /* TIM17 interrupt Init */ + HAL_NVIC_SetPriority(TIM17_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(TIM17_IRQn); + /* USER CODE BEGIN TIM17_MspInit 1 */ + + /* USER CODE END TIM17_MspInit 1 */ + } } @@ -839,6 +853,20 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base) /* USER CODE END TIM2_MspDeInit 1 */ } + else if(htim_base->Instance==TIM17) + { + /* USER CODE BEGIN TIM17_MspDeInit 0 */ + + /* USER CODE END TIM17_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_TIM17_CLK_DISABLE(); + + /* TIM17 interrupt DeInit */ + HAL_NVIC_DisableIRQ(TIM17_IRQn); + /* USER CODE BEGIN TIM17_MspDeInit 1 */ + + /* USER CODE END TIM17_MspDeInit 1 */ + } } diff --git a/Core/Src/stm32h7xx_it.c b/Core/Src/stm32h7xx_it.c index b290ffc..83d2ec7 100644 --- a/Core/Src/stm32h7xx_it.c +++ b/Core/Src/stm32h7xx_it.c @@ -57,6 +57,7 @@ /* External variables --------------------------------------------------------*/ extern FDCAN_HandleTypeDef hfdcan1; extern LTDC_HandleTypeDef hltdc; +extern TIM_HandleTypeDef htim17; extern TIM_HandleTypeDef htim6; /* USER CODE BEGIN EV */ @@ -260,6 +261,20 @@ void LTDC_IRQHandler(void) /* USER CODE END LTDC_IRQn 1 */ } +/** + * @brief This function handles TIM17 global interrupt. + */ +void TIM17_IRQHandler(void) +{ + /* USER CODE BEGIN TIM17_IRQn 0 */ + + /* USER CODE END TIM17_IRQn 0 */ + HAL_TIM_IRQHandler(&htim17); + /* USER CODE BEGIN TIM17_IRQn 1 */ + + /* USER CODE END TIM17_IRQn 1 */ +} + /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ diff --git a/Makefile b/Makefile index 827e5f7..3a495e7 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [3.19.2] date: [Thu Mar 16 22:32:27 CET 2023] +# File automatically-generated by tool: [projectgenerator] version: [3.19.2] date: [Fri Mar 17 17:47:57 CET 2023] ########################################################################################################################## # ------------------------------------------------ diff --git a/STM32Make.make b/STM32Make.make index 760c9bf..1290f51 100644 --- a/STM32Make.make +++ b/STM32Make.make @@ -41,6 +41,7 @@ Core/Lib/FT_CAN_AL/FT_CAN_AL.c \ Core/Src/app.c \ Core/Src/app_threadx.c \ Core/Src/hx8357d.c \ +Core/Src/shorttimer.c \ Core/Src/stm32h7xx_hal_msp.c \ Core/Src/stm32h7xx_hal_timebase_tim.c \ Core/Src/stm32h7xx_it.c \ diff --git a/steering-wheel.ioc b/steering-wheel.ioc index e0b19c5..26945c7 100644 --- a/steering-wheel.ioc +++ b/steering-wheel.ioc @@ -48,6 +48,7 @@ Mcu.IP11=SYS Mcu.IP12=TIM1 Mcu.IP13=TIM2 Mcu.IP14=TIM4 +Mcu.IP15=TIM17 Mcu.IP2=DEBUG Mcu.IP3=DMA2D Mcu.IP4=FDCAN1 @@ -56,7 +57,7 @@ Mcu.IP6=LTDC Mcu.IP7=NVIC Mcu.IP8=OCTOSPI1 Mcu.IP9=RCC -Mcu.IPNb=15 +Mcu.IPNb=16 Mcu.Name=STM32H7A3Z(G-I)Tx Mcu.Package=LQFP144 Mcu.Pin0=PE2 @@ -129,11 +130,12 @@ Mcu.Pin69=VP_SYS_VS_tim6 Mcu.Pin7=PF2 Mcu.Pin70=VP_TIM1_VS_ClockSourceINT Mcu.Pin71=VP_TIM2_VS_ClockSourceINT -Mcu.Pin72=VP_STMicroelectronics.X-CUBE-TOUCHGFX_VS_GraphicsJjApplication_4.21.2 -Mcu.Pin73=VP_STMicroelectronics.X-CUBE-AZRTOS-H7_VS_RTOSJjThreadX_6.1.12_3.0.0 +Mcu.Pin72=VP_TIM17_VS_ClockSourceINT +Mcu.Pin73=VP_STMicroelectronics.X-CUBE-TOUCHGFX_VS_GraphicsJjApplication_4.21.2 +Mcu.Pin74=VP_STMicroelectronics.X-CUBE-AZRTOS-H7_VS_RTOSJjThreadX_6.1.12_3.0.0 Mcu.Pin8=PF3 Mcu.Pin9=PF4 -Mcu.PinsNb=74 +Mcu.PinsNb=75 Mcu.ThirdParty0=STMicroelectronics.X-CUBE-AZRTOS-H7.3.0.0 Mcu.ThirdParty1=STMicroelectronics.X-CUBE-TOUCHGFX.4.21.2 Mcu.ThirdPartyNb=2 @@ -161,6 +163,7 @@ NVIC.SavedPendsvIrqHandlerGenerated=true NVIC.SavedSvcallIrqHandlerGenerated=true NVIC.SavedSystickIrqHandlerGenerated=true NVIC.SysTick_IRQn=true\:0\:0\:false\:false\:false\:false\:false\:true\:false +NVIC.TIM17_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true\:true NVIC.TIM6_DAC_IRQn=true\:15\:0\:false\:false\:true\:false\:false\:true\:true NVIC.TimeBase=TIM6_DAC_IRQn NVIC.TimeBaseIP=TIM6 @@ -420,7 +423,7 @@ ProjectManager.StackSize=0x400 ProjectManager.TargetToolchain=Makefile ProjectManager.ToolChainLocation= ProjectManager.UnderRoot=false -ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_DMA2D_Init-DMA2D-false-HAL-true,4-MX_FDCAN1_Init-FDCAN1-false-HAL-true,5-MX_LTDC_Init-LTDC-false-HAL-true,6-MX_JPEG_Init-JPEG-false-HAL-true,7-MX_OCTOSPI1_Init-OCTOSPI1-false-HAL-true,8-MX_SPI3_Init-SPI3-false-HAL-true,9-MX_TIM1_Init-TIM1-false-HAL-true,10-MX_TIM2_Init-TIM2-false-HAL-true,11-MX_TIM4_Init-TIM4-false-HAL-true,12-MX_CRC_Init-CRC-false-HAL-true,14-MX_TouchGFX_Init-STMicroelectronics.X-CUBE-TOUCHGFX.4.21.2-false-HAL-false,15-MX_TouchGFX_Process-STMicroelectronics.X-CUBE-TOUCHGFX.4.21.2-false-HAL-false,0-MX_CORTEX_M7_Init-CORTEX_M7-false-HAL-true +ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_DMA2D_Init-DMA2D-false-HAL-true,4-MX_FDCAN1_Init-FDCAN1-false-HAL-true,5-MX_LTDC_Init-LTDC-false-HAL-true,6-MX_JPEG_Init-JPEG-false-HAL-true,7-MX_OCTOSPI1_Init-OCTOSPI1-false-HAL-true,8-MX_SPI3_Init-SPI3-false-HAL-true,9-MX_TIM1_Init-TIM1-false-HAL-true,10-MX_TIM2_Init-TIM2-false-HAL-true,11-MX_TIM4_Init-TIM4-false-HAL-true,12-MX_CRC_Init-CRC-false-HAL-true,13-MX_TouchGFX_Init-STMicroelectronics.X-CUBE-TOUCHGFX.4.21.2-false-HAL-false,14-MX_TouchGFX_Process-STMicroelectronics.X-CUBE-TOUCHGFX.4.21.2-false-HAL-false,0-MX_CORTEX_M7_Init-CORTEX_M7-false-HAL-true RCC.ADCFreq_Value=24000000 RCC.AHB12Freq_Value=160000000 RCC.AHB4Freq_Value=160000000 @@ -558,6 +561,9 @@ TIM1.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 TIM1.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2 TIM1.Channel-PWM\ Generation3\ CH3=TIM_CHANNEL_3 TIM1.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2,Channel-PWM Generation3 CH3 +TIM17.IPParameters=Prescaler,Period +TIM17.Period=1000-1 +TIM17.Prescaler=16-1 TIM2.Channel-PWM\ Generation4\ CH4=TIM_CHANNEL_4 TIM2.IPParameters=Channel-PWM Generation4 CH4,Prescaler,Period,Pulse-PWM Generation4 CH4,OCPolarity_4 TIM2.OCPolarity_4=TIM_OCPOLARITY_HIGH @@ -581,6 +587,8 @@ VP_STMicroelectronics.X-CUBE-TOUCHGFX_VS_GraphicsJjApplication_4.21.2.Mode=Graph VP_STMicroelectronics.X-CUBE-TOUCHGFX_VS_GraphicsJjApplication_4.21.2.Signal=STMicroelectronics.X-CUBE-TOUCHGFX_VS_GraphicsJjApplication_4.21.2 VP_SYS_VS_tim6.Mode=TIM6 VP_SYS_VS_tim6.Signal=SYS_VS_tim6 +VP_TIM17_VS_ClockSourceINT.Mode=Enable_Timer +VP_TIM17_VS_ClockSourceINT.Signal=TIM17_VS_ClockSourceINT VP_TIM1_VS_ClockSourceINT.Mode=Internal VP_TIM1_VS_ClockSourceINT.Signal=TIM1_VS_ClockSourceINT VP_TIM2_VS_ClockSourceINT.Mode=Internal