ARMEBS4  revision-26.06.2015
time.c
Go to the documentation of this file.
1 /**
2  * \file heivs/time.c
3  * \author marc dot pignat at hevs dot ch
4  *
5  * Uses timer 7.
6  */
7 
8 #include "heivs/time.h"
9 #include "heivs/config.h"
10 #include "heivs/delay.h"
11 #include "stm32/stm32f4xx_tim.h"
12 #include "stm32/stm32f4xx_rcc.h"
13 #include "stm32/stm32f4xx_misc.h"
14 
16 {
17  timeout_t time = time_get();
18  time.value += ms;
19 
20  /* The timeout should be at least ms */
21  time.value += 1;
22  return time;
23 }
24 
25 volatile uint32_t gTimeCounter = 0;
26 
27 static void tim7_irq_handler(void)
28 {
29  if (TIM7->SR & TIM_SR_UIF)
30  {
31  gTimeCounter++;
32  TIM_ClearITPendingBit(TIM7, TIM_IT_Update);
33  }
34 }
35 
36 void time_suspend()
37 {
38  TIM_ITConfig(TIM7, TIM_IT_Update, DISABLE);
39  TIM_Cmd(TIM7, DISABLE);
40 }
41 
42 void time_resume()
43 {
44  TIM_ITConfig(TIM7, TIM_IT_Update, ENABLE);
45  TIM_Cmd(TIM7, ENABLE);
46 }
47 
48 void time_init(void)
49 {
50  NVIC_InitTypeDef NVIC_InitStructure;
51  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
52 
53  gTimeCounter = 0;
54 
55  NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn;
56  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 8;
57  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
58  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
59  NVIC_InitStructure.NVIC_Handler = tim7_irq_handler;
60  NVIC_Init(&NVIC_InitStructure);
61 
62  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);
63  RCC_APB1PeriphClockLPModeCmd(RCC_APB1Periph_TIM7, ENABLE);
64  /* From clock to 1 MHz */
65  TIM_TimeBaseStructure.TIM_Prescaler = (SystemClock.hclk/1000000)/2 - 1;
66  /* from 1 MHz to 1 KHz*/
67  TIM_TimeBaseStructure.TIM_Period = 1000 - 1;
68  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
69  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
70  TIM_TimeBaseInit(TIM7, &TIM_TimeBaseStructure);
71  time_resume();
72 
73 #if !USE_BOOT_TIME_CRITICAL
74  delay_wait_ms(500);
75 #endif
76 }
77 
78 uint32_t time_diff_ms(timeout_t now, timeout_t before)
79 {
80  return now.value - before.value;
81 }
This file contains all the functions prototypes for the RCC firmware library.
uint8_t NVIC_IRQChannel
void delay_wait_ms(uint32_t ms)
Wait for at least that time.
Definition: delay.c:12
simple time abstraction
This file contains all the functions prototypes for the TIM firmware library.
libheivs configuration file
#define TIM_SR_UIF
Definition: stm32f4xx.h:6130
uint8_t NVIC_IRQChannelSubPriority
NVIC Init Structure definition.
static timeout_t time_get(void)
Get current time in obscure format from boot.
Definition: time.h:46
TIM Time Base Init structure definition.
Definition: stm32f4xx_tim.h:49
FunctionalState NVIC_IRQChannelCmd
simple delays
uint8_t NVIC_IRQChannelPreemptionPriority
Timeout structure.
Definition: time.h:34
timeout_t time_set_timeout_ms(uint32_t ms)
Set an obscure time at least ms milliseconds in the future.
Definition: time.c:15
struct system_clock_t SystemClock