Contiki 3.x
rtimer-arch.c
Go to the documentation of this file.
1 /**
2  * \addtogroup mbxxx-platform
3  *
4  * @{
5  */
6 
7 /*
8  * Copyright (c) 2010, STMicroelectronics.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * 3. The name of the author may not be used to endorse or promote
21  * products derived from this software without specific prior
22  * written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
25  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
28  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
30  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 /**
39 * \file
40 * Real-timer specific implementation for STM32W.
41 * \author
42 * Salvatore Pitrulli <salvopitru@users.sourceforge.net>
43 */
44 
45 #include "sys/energest.h"
46 #include "sys/rtimer.h"
47 
48 #define DEBUG 0
49 #if DEBUG
50 #include <stdio.h>
51 #define PRINTF(...) printf(__VA_ARGS__)
52 #else
53 #define PRINTF(...)
54 #endif
55 
56 static uint16_t saved_TIM1CFG;
57 static uint32_t time_msb = 0; /* Most significant bits of the current time. */
58 
59 /* time of the next rtimer event. Initially is set to the max
60  value. */
61 static rtimer_clock_t next_rtimer_time = 0;
62 
63 /*---------------------------------------------------------------------------*/
64 void
65 halTimer1Isr(void)
66 {
67  if(INT_TIM1FLAG & INT_TIMUIF) {
68  rtimer_clock_t now, clock_to_wait;
69  /* Overflow event. */
70  /* PRINTF("O %4x.\r\n", TIM1_CNT); */
71  /* printf("OV "); */
72  time_msb++;
73  now = ((rtimer_clock_t) time_msb << 16) | TIM1_CNT;
74  clock_to_wait = next_rtimer_time - now;
75 
76  if(clock_to_wait <= 0x10000 && clock_to_wait > 0) {
77  /* We must now set the Timer Compare Register. */
78  TIM1_CCR1 = (uint16_t) clock_to_wait;
79  INT_TIM1FLAG = INT_TIMCC1IF;
80  INT_TIM1CFG |= INT_TIMCC1IF; /* Compare 1 interrupt enable. */
81  }
82  INT_TIM1FLAG = INT_TIMUIF;
83  } else {
84  if(INT_TIM1FLAG & INT_TIMCC1IF) {
85  /* Compare event. */
86  INT_TIM1CFG &= ~INT_TIMCC1IF; /* Disable the next compare interrupt */
87  PRINTF("\nCompare event %4x\r\n", TIM1_CNT);
88  PRINTF("INT_TIM1FLAG %2x\r\n", INT_TIM1FLAG);
89  ENERGEST_ON(ENERGEST_TYPE_IRQ);
91  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
92  INT_TIM1FLAG = INT_TIMCC1IF;
93  }
94  }
95 }
96 /*---------------------------------------------------------------------------*/
97 void
99 {
100  TIM1_CR1 = 0;
101  TIM1_PSC = RTIMER_ARCH_PRESCALER;
102 
103  /* Counting from 0 to the maximum value. */
104  TIM1_ARR = 0xffff;
105 
106  /* Bits of TIMx_CCMR1 as default. */
107  /* Update Generation. */
108  TIM1_EGR = TIM_UG;
109  INT_TIM1FLAG = 0xffff;
110 
111  /* Update interrupt enable (interrupt on overflow).*/
112  INT_TIM1CFG = INT_TIMUIF;
113 
114  /* Counter enable. */
115  TIM1_CR1 = TIM_CEN;
116 
117  /* Enable top level interrupt. */
118  INT_CFGSET = INT_TIM1;
119 
120 }
121 /*---------------------------------------------------------------------------*/
122 void
123 rtimer_arch_disable_irq(void)
124 {
125  ATOMIC(saved_TIM1CFG = INT_TIM1CFG; INT_TIM1CFG = 0;)
126 }
127 /*---------------------------------------------------------------------------*/
128 void
129 rtimer_arch_enable_irq(void)
130 {
131  INT_TIM1CFG = saved_TIM1CFG;
132 }
133 /*---------------------------------------------------------------------------*/
134 rtimer_clock_t
136 {
137  rtimer_clock_t t;
138 
139  ATOMIC(t = ((rtimer_clock_t) time_msb << 16) | TIM1_CNT;)
140  return t;
141 }
142 
143 /*---------------------------------------------------------------------------*/
144 void
145 rtimer_arch_schedule(rtimer_clock_t t)
146 {
147  rtimer_clock_t now, clock_to_wait;
148  PRINTF("rtimer_arch_schedule time %4x\r\n", /*((uint32_t*)&t)+1, */
149  (uint32_t)t);
150  next_rtimer_time = t;
151  now = rtimer_arch_now();
152  clock_to_wait = t - now;
153 
154  PRINTF("now %2x\r\n", TIM1_CNT);
155  PRINTF("clock_to_wait %4x\r\n", clock_to_wait);
156 
157  if(clock_to_wait <= 0x10000) {
158  /* We must now set the Timer Compare Register. */
159  TIM1_CCR1 = (uint16_t)now + (uint16_t)clock_to_wait;
160  INT_TIM1FLAG = INT_TIMCC1IF;
161  INT_TIM1CFG |= INT_TIMCC1IF; /* Compare 1 interrupt enable. */
162  PRINTF("2-INT_TIM1FLAG %2x\r\n", INT_TIM1FLAG);
163  }
164  /* else compare register will be set at overflow interrupt closer to
165  the rtimer event. */
166 }
167 /*---------------------------------------------------------------------------*/
168 /** @} */
void rtimer_arch_init(void)
We don&#39;t need to explicitly initialise anything but this routine is required by the API...
Definition: rtimer-arch.c:78
#define rtimer_arch_now()
Definition: rtimer-arch.h:40
Header file for the energy estimation mechanism
Header file for the real-time timer module.
#define ATOMIC(blah)
A block of code may be made atomic by wrapping it with this macro.
Definition: gnu.h:459
void rtimer_arch_schedule(rtimer_clock_t t)
Schedules an rtimer task to be triggered at time t.
Definition: rtimer-arch.c:115
void rtimer_run_next(void)
Execute the next real-time task and schedule the next task, if any.
Definition: rtimer.c:92