Contiki 3.x
rtimer-arch.c
1 /*
2  * Copyright (c) 2012, STMicroelectronics.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *
30  */
31 /*---------------------------------------------------------------------------*/
32 #include "contiki.h"
33 #include "platform-conf.h"
34 
35 #include "sys/rtimer.h"
36 #include "sys/process.h"
37 #include "dev/watchdog.h"
38 
39 #include "stm32l1xx.h"
40 #include "stm32l1xx_hal_gpio.h"
41 #include "stm32l1xx_hal_rcc.h"
42 #include "stm32l1xx_hal_tim.h"
43 #include "stm32l1xx_hal_cortex.h"
44 #include "st-lib.h"
45 /*---------------------------------------------------------------------------*/
46 volatile uint32_t rtimer_clock = 0uL;
47 /*---------------------------------------------------------------------------*/
48 st_lib_tim_handle_typedef htim2;
49 /*---------------------------------------------------------------------------*/
50 void
51 st_lib_tim2_irq_handler(void)
52 {
53  /* clear interrupt pending flag */
54  st_lib_hal_tim_clear_it(&htim2, TIM_IT_UPDATE);
55 
56  rtimer_clock++;
57 }
58 /*---------------------------------------------------------------------------*/
59 void
60 rtimer_arch_init(void)
61 {
62  st_lib_tim_clock_config_typedef s_clock_source_config;
63  st_lib_tim_oc_init_typedef s_config_oc;
64 
65  st_lib_tim2_clk_enable();
66  htim2.Instance = TIM2;
67  htim2.Init.Prescaler = PRESCALER;
68  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
69  htim2.Init.Period = 1;
70 
71  st_lib_hal_tim_base_init(&htim2);
72  st_lib_hal_tim_base_start_it(&htim2);
73 
74  s_clock_source_config.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
75  st_lib_hal_tim_config_clock_source(&htim2, &s_clock_source_config);
76 
77  st_lib_hal_tim_oc_init(&htim2);
78 
79  s_config_oc.OCMode = TIM_OCMODE_TIMING;
80  s_config_oc.Pulse = 0;
81  s_config_oc.OCPolarity = TIM_OCPOLARITY_HIGH;
82  st_lib_hal_tim_oc_config_channel(&htim2, &s_config_oc, TIM_CHANNEL_1);
83 
84  st_lib_hal_tim_clear_flag(&htim2, TIM_FLAG_UPDATE);
85 
86  /* Enable TIM2 Update interrupt */
87  st_lib_hal_tim_enable_it(&htim2, TIM_IT_UPDATE);
88 
89  st_lib_hal_tim_enable(&htim2);
90 
91  st_lib_hal_nvic_set_priority((st_lib_irq_n_type)TIM2_IRQn, 0, 0);
92  st_lib_hal_nvic_enable_irq((st_lib_irq_n_type)(TIM2_IRQn));
93 }
94 /*---------------------------------------------------------------------------*/
95 rtimer_clock_t
96 rtimer_arch_now(void)
97 {
98  return rtimer_clock;
99 }
100 /*---------------------------------------------------------------------------*/
101 void
102 rtimer_arch_schedule(rtimer_clock_t t)
103 {
104 }
105 /*---------------------------------------------------------------------------*/
Header file for the real-time timer module.
Header file for the STM32Cube HAL APIs.
rtimer_clock_t rtimer_arch_now()
Returns the current real-time clock time.
Definition: rtimer-arch.c:116
void rtimer_arch_init(void)
We don't need to explicitly initialise anything but this routine is required by the API...
Definition: rtimer-arch.c:60
Header file for the Contiki process interface.
void rtimer_arch_schedule(rtimer_clock_t t)
Schedules an rtimer task to be triggered at time t.
Definition: rtimer-arch.c:72