diff --git a/Core/Inc/stm32f4xx_it.h b/Core/Inc/stm32f4xx_it.h index dfd1d7d..b9be200 100644 --- a/Core/Inc/stm32f4xx_it.h +++ b/Core/Inc/stm32f4xx_it.h @@ -37,6 +37,7 @@ extern "C" { /* Exported constants --------------------------------------------------------*/ /* USER CODE BEGIN EC */ +extern volatile uint32_t SysTickCnt; /* USER CODE END EC */ @@ -57,6 +58,9 @@ void PendSV_Handler(void); void SysTick_Handler(void); /* USER CODE BEGIN EFP */ +uint32_t SysTimeGetAbs(void); +int32_t SysTimeGetRel(uint32_t tref); + /* USER CODE END EFP */ #ifdef __cplusplus diff --git a/Core/Src/main.c b/Core/Src/main.c index fc537e2..8f27755 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -74,6 +74,20 @@ void printf_putc(void *p, char c) { Usart1_PutByte(c); } +/***************************************************************************//** + * @brief Check if specified time interval has elapsed + * @param tref Pointer to time reference variable + * @param tcycle Time interval in milliseconds + *//****************************************************************************/ +static inline uint32_t TickChk(uint32_t *tref, int_fast16_t tcycle) { + int32_t tdif = SysTickCnt - *tref; + if (tdif >= tcycle) { + *tref += tcycle; + return 1; + } + return 0; +} + /* USER CODE END 0 */ /** @@ -120,6 +134,14 @@ int main(void) { /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { + static uint32_t Tick10msRef = 0; + if (TickChk(&Tick10msRef, 10)) { + } + + static uint32_t Tick1secRef = 0; + if (TickChk(&Tick1secRef, 1000)) { + } + int c; c = Usart2_GetByte(); diff --git a/Core/Src/stm32f4xx_it.c b/Core/Src/stm32f4xx_it.c index bc9c66c..67b2c32 100644 --- a/Core/Src/stm32f4xx_it.c +++ b/Core/Src/stm32f4xx_it.c @@ -41,6 +41,7 @@ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ +volatile uint32_t SysTickCnt = 0; /* USER CODE END PV */ @@ -52,6 +53,22 @@ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ +/***************************************************************************//** +* @brief Get absolute system time +*//****************************************************************************/ +uint32_t SysTimeGetAbs (void) { + return SysTickCnt; +} + +/***************************************************************************//** +* @brief Get relative time +*//****************************************************************************/ +int32_t SysTimeGetRel (uint32_t tref) { + int32_t tdif = SysTickCnt - tref; + return tdif; +} + + /* USER CODE END 0 */ /* External variables --------------------------------------------------------*/ @@ -187,7 +204,7 @@ void SysTick_Handler(void) /* USER CODE END SysTick_IRQn 0 */ HAL_IncTick(); /* USER CODE BEGIN SysTick_IRQn 1 */ - + SysTickCnt++; /* USER CODE END SysTick_IRQn 1 */ }