forked from CU-ECEN-5823/course-project-PuneetBansal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
letimer.c
180 lines (148 loc) · 3.85 KB
/
letimer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* @filename : letimer.c
* @description : This file contains functions to configure LETIMER
* @author : Puneet Bansal
*
*/
#include "letimer.h"
#include "main.h"
#include "native_gecko.h"
int i=1;
uint32_t overflow_count=0;
/*
* @description
* Populating LETIMER_Init structure with default values and initialising LETIMER.
* Set the compare value in COMP0 register according to the required delay using CompareSet().
* Enable underflow interrupt, NVIC and LETIMER.
*/
void letimer_init()
{
LETIMER_Init_TypeDef init = LETIMER_INIT_DEFAULT;
init.enable=false; //start counting when init completed
init.comp0Top=true; //load comp0 value in CNT on underflow
init.debugRun=false; //not running counter in debug mode
init.repMode=letimerRepeatFree; //count until stopped
init.out0Pol=0;
init.out1Pol=0;
init.ufoa0=letimerUFOANone;
init.ufoa1=letimerUFOANone;
prescale_set();
LETIMER_Init(LETIMER0,&init);
LETIMER_CompareSet(LETIMER0,0,ticks);
//LedOn_Ticks= freq*LED_ONTIME;
//LETIMER_CompareSet(LETIMER0,1,LedOn_Ticks);
LETIMER_IntEnable(LETIMER0, LETIMER_IEN_UF ); /*Enable Underflow interrupts*/
NVIC_EnableIRQ(LETIMER0_IRQn);
LETIMER_Enable(LETIMER0, true);
}
/*
* @description
* Set the prescaler value depending on the selected energy mode and the desired
* LED period.
*/
void prescale_set()
{
int j=0;
if(sleepEM>=0 && sleepEM<3)
{
freq= LFX0_FREQ; /*Select freq=32768 for EM(0-2)*/
}
else if (sleepEM==3)
{
freq= ULFRCO_FREQ; /*Select freq=1000Hz for EM(3)*/
}
ticks=LED_PERIOD*freq;
if(ticks> MAX_TICKS)
{
while(ticks> MAX_TICKS)
{
ticks=ticks/2;
freq=freq/2;
j++;
}
CMU_ClockDivSet(cmuClock_LETIMER0,pow(2,j));
}
}
/*
* @description
* Function to generate delay in micro seconds.
*
* @param us_wait
* delay in microsecond required
*
*/
void timerWaitUs(uint32_t us_wait)
{
uint32_t current_ticks,max_tick,count_upto;
uint32_t us_ticks;
us_ticks=freq * us_wait;
us_ticks=us_ticks/1000000; /*Calculate the ticks required in us*/
current_ticks=LETIMER_CounterGet(LETIMER0); /*Get the present value of timer count*/
if(current_ticks>us_ticks)
{
count_upto=current_ticks-us_ticks;
//gpioLed0SetOn();
while(LETIMER_CounterGet(LETIMER0)!=count_upto);
//gpioLed0SetOff();
}
else
{
max_tick=LETIMER_CompareGet(LETIMER0,0);
//gpioLed0SetOn();
while(LETIMER_CounterGet(LETIMER0)!=(max_tick-(us_ticks-current_ticks)));
//gpioLed0SetOff();
}
}
/*
* @description
* triggers a comp1 interrupt on achieving the desired amount of ms delay.
*
*/
void timerSetEventinms(uint32_t ms_until_wakeup)
{
uint32_t current_ticks=0;
uint32_t max_tick,count_upto;
uint32_t ms_ticks;
ms_ticks=freq * ms_until_wakeup;
ms_ticks=ms_ticks/1000; /*Calculate the ticks required in s*/
current_ticks=LETIMER_CounterGet(LETIMER0); /*Get the present value of timer count*/
if(current_ticks>=ms_ticks)
{
count_upto=current_ticks-ms_ticks;
}
else
{
max_tick=LETIMER_CompareGet(LETIMER0,0);
count_upto=(max_tick-(ms_ticks-current_ticks));
}
LETIMER_CompareSet(LETIMER0,1,count_upto);
LETIMER_IntEnable(LETIMER0, LETIMER_IEN_COMP1);
}
/*
* @description
* Interrupt handler for LETIMER0. Hitting the underflow interrupt means that 3seconds delay has passed
* and a comp0underflow event is set.
* Hitting the Comp1 interrupt means that the desired ms delay has been achieved and comp1 interrupt event
* has been set
*
*/
void LETIMER0_IRQHandler(void)
{
CORE_ATOMIC_IRQ_DISABLE();
uint32_t flags =LETIMER_IntGetEnabled(LETIMER0);
LETIMER_IntClear(LETIMER0,flags);
if(flags & LETIMER_IF_UF)
{
overflow_count++;
displayUpdate();
}
if(flags & LETIMER_IF_COMP1)
{
LETIMER_CompareSet(LETIMER0,1,0xFFFF);
LETIMER_IntDisable(LETIMER0,LETIMER_IFC_COMP1);
//event=Comp1Interrupt; //to add
prevState=powerOn_humid;//to add
presentState=powerOn_humid;//to add
}
CORE_ATOMIC_IRQ_ENABLE();
}