TM STM32F4xx Libraries  v1.0.0
Libraries for STM32F4xx devices from Tilen Majerle
tm_stm32f4_delay.h
1 
30 #ifndef TM_DELAY_H
31 #define TM_DELAY_H 240
32 
155 #include "stm32f4xx.h"
156 #include "stm32f4xx_rcc.h"
157 #include "defines.h"
158 #include "attributes.h"
159 /* If user selectable timer is selected for delay */
160 #if defined(TM_DELAY_TIM)
161 #include "misc.h"
162 #include "stm32f4xx_tim.h"
163 #include "tm_stm32f4_timer_properties.h"
164 #endif
165 #include "stdlib.h"
166 
176 typedef struct {
177  uint32_t ARR;
178  uint32_t AutoReload;
179  uint32_t CNT;
180  uint8_t Enabled;
181  void (*Callback)(void *);
184 
199 #ifndef DELAY_MAX_CUSTOM_TIMERS
200 #define DELAY_MAX_CUSTOM_TIMERS 5
201 #endif
202 
203 /* Memory allocation function */
204 #ifndef LIB_ALLOC_FUNC
205 #define LIB_ALLOC_FUNC malloc
206 #endif
207 
208 /* Memory free function */
209 #ifndef LIB_FREE_FUNC
210 #define LIB_FREE_FUNC free
211 #endif
212 
227 extern __IO uint32_t TM_Time;
228 extern __IO uint32_t TM_Time2;
229 extern __IO uint32_t mult;
230 
247 static __INLINE void Delay(uint32_t micros) {
248 #if defined(TM_DELAY_TIM)
249  volatile uint32_t timer = TM_DELAY_TIM->CNT;
250 
251  do {
252  /* Count timer ticks */
253  while ((TM_DELAY_TIM->CNT - timer) == 0);
254 
255  /* Increase timer */
256  timer = TM_DELAY_TIM->CNT;
257 
258  /* Decrease microseconds */
259  } while (--micros);
260 #else
261  uint32_t amicros;
262 
263  /* Multiply micro seconds */
264  amicros = (micros) * (mult);
265 
266  #ifdef __GNUC__
267  if (SystemCoreClock == 180000000 || SystemCoreClock == 100000000) {
268  amicros -= mult;
269  }
270  #endif
271 
272  /* If clock is 100MHz, then add additional multiplier */
273  /* 100/3 = 33.3 = 33 and delay wouldn't be so accurate */
274  #if defined(STM32F411xE)
275  amicros += mult;
276  #endif
277 
278  /* While loop */
279  while (amicros--);
280 #endif /* TM_DELAY_TIM */
281 }
282 
289 static __INLINE void Delayms(uint32_t millis) {
290  volatile uint32_t timer = TM_Time;
291 
292  /* Called from thread */
293  if (!__get_IPSR()) {
294  /* Wait for timer to count milliseconds */
295  while ((TM_Time - timer) < millis) {
296 #ifdef DELAY_SLEEP
297  /* Go sleep, wait systick interrupt */
298  __WFI();
299 #endif
300  }
301  } else {
302  /* Called from interrupt */
303  while (millis) {
304  if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) {
305  millis--;
306  }
307  }
308  }
309 }
310 
317 void TM_DELAY_Init(void);
318 
324 #define TM_DELAY_Time() (TM_Time)
325 
331 #define TM_DELAY_SetTime(time) (TM_Time = (time))
332 
339 void TM_DELAY_EnableDelayTimer(void);
340 
347 void TM_DELAY_DisableDelayTimer(void);
348 
355 #define TM_DELAY_Time2() (TM_Time2)
356 
363 #define TM_DELAY_SetTime2(time) (TM_Time2 = (time))
364 
375 TM_DELAY_Timer_t* TM_DELAY_TimerCreate(uint32_t ReloadValue, uint8_t AutoReload, uint8_t StartTimer, void (*TM_DELAY_CustomTimerCallback)(void *), void* UserParameters);
376 
383 
390 
397 
404 
412 TM_DELAY_Timer_t* TM_DELAY_TimerAutoReload(TM_DELAY_Timer_t* Timer, uint8_t AutoReload);
413 
421 TM_DELAY_Timer_t* TM_DELAY_TimerAutoReloadValue(TM_DELAY_Timer_t* Timer, uint32_t AutoReloadValue);
422 
430 __weak void TM_DELAY_1msHandler(void);
431 
432 
445 #endif
void TM_DELAY_Init(void)
Initializes timer settings for delay.
TM_DELAY_Timer_t * TM_DELAY_TimerAutoReload(TM_DELAY_Timer_t *Timer, uint8_t AutoReload)
Sets auto reload feature for timer.
TM_DELAY_Timer_t * TM_DELAY_TimerReset(TM_DELAY_Timer_t *Timer)
Resets custom timer counter value.
uint32_t CNT
Definition: tm_stm32f4_delay.h:179
TM_DELAY_Timer_t * TM_DELAY_TimerCreate(uint32_t ReloadValue, uint8_t AutoReload, uint8_t StartTimer, void(*TM_DELAY_CustomTimerCallback)(void *), void *UserParameters)
Creates a new custom timer which has 1ms resolution.
__weak void TM_DELAY_1msHandler(void)
User function, called each 1ms when interrupt from timer happen.
static __INLINE void Delay(uint32_t micros)
Definition: tm_stm32f4_delay.h:247
uint8_t Enabled
Definition: tm_stm32f4_delay.h:180
TM_DELAY_Timer_t * TM_DELAY_TimerStop(TM_DELAY_Timer_t *Timer)
Stops custom timer from counting.
Custom timer structure.
Definition: tm_stm32f4_delay.h:176
uint32_t ARR
Definition: tm_stm32f4_delay.h:177
void TM_DELAY_TimerDelete(TM_DELAY_Timer_t *Timer)
Deletes already allocated timer.
void TM_DELAY_DisableDelayTimer(void)
Disables delay timer.
void TM_DELAY_EnableDelayTimer(void)
Re-enables delay timer It has to be configured before with TM_DELAY_Init()
TM_DELAY_Timer_t * TM_DELAY_TimerStart(TM_DELAY_Timer_t *Timer)
Starts custom timer counting.
TM_DELAY_Timer_t * TM_DELAY_TimerAutoReloadValue(TM_DELAY_Timer_t *Timer, uint32_t AutoReloadValue)
Sets auto reload value for timer.
__IO uint32_t TM_Time
void * UserParameters
Definition: tm_stm32f4_delay.h:182
uint32_t AutoReload
Definition: tm_stm32f4_delay.h:178
static __INLINE void Delayms(uint32_t millis)
Definition: tm_stm32f4_delay.h:289