source: test2.git/main.c

Last change on this file was b6aa0ba, checked in by andib <andi.b@…>, 2 years ago

test with more files

  • Property mode set to 100644
File size: 9.2 KB
RevLine 
[b6aa0ba]1/*******************************************************************************
2MODULE : main.c
3 Simple scheduler. Wakes up about every 1ms by RTCC (0.976ms). When RTCC is
4 running by internal LFRCO some jitter and inaccuracy is to be expected. From
5 main loop tasks can be called every about 5ms, 10ms, 25ms.... Additionally
6 iTask1s is set every 1s (RTC accuracy) for time keeping.
7
8 After all tasks are completed EFM32 is switched to EM2 to minimize energy
9 consumption. LED lits whenever core is out of EM2.
10
11 When some pheriperal needs HFCLK switching to EM2 can be prohbited by
12 signaling via f.i. TraceIsRunning(). In this case core is switched to EM1
13 instead. Core is woken up by RTCC interrupt or any other interrupt.
14
15201xxxxx AB Initial
1620150407 AB Adapted for USV_BatteryMonitor Slave, remove LEDs and Testpins.
1720191212 AB JADE EFM32JG1B test, TRACE now needs HFCLK. RTCC instead RTC.
18*******************************************************************************/
19
20#define LOGGING_TOKEN MAI_LOGGING
21#include "TraceLeUart.h"
22
23#include <stdio.h>
24#include "em_chip.h"
25#include "em_cmu.h"
26#include "em_emu.h"
27#include "em_adc.h"
28#include "em_device.h"
29#include "em_gpio.h"
30#include "em_ldma.h"
31#include "em_prs.h"
32#include "em_rtcc.h"
33
34#include "command.h"
35#include "types.h"
36#include "adc.h"
37#include "leds.h"
38
39/* Defines for RTCC */
40#define RTCC_WAKEUP_MS 1
41#define RTCC_WAKEUP_COUNT (((32768 * RTCC_WAKEUP_MS) / 1000) - 1)
42
43#define TESTPIN_INIT(); { GPIO_PinModeSet(gpioPortD, 15, gpioModePushPull, 0);}
44#define TESTPIN( x ) { if ( x ) GPIO->P[gpioPortD].DOUT |= 1 << 15; else GPIO->P[gpioPortD].DOUT &= ~(1 << 15); }
45
46// --- PUBLIC Variables -------------------------------------------------------
47extern int _STACK_START;
48extern int _STACK_END;
49
50// --- Variables --------------------------------------------------------------
51uint32_t rtccFlag;
52
53static int iMsTicks = 0; // for counting real seconds
54static int iTaskTicks = 0; // for counting various tasks
55
56static int iTask5ms = 0;
57static int iTask10ms = 0;
58static int iTask25ms = 0;
59static int iTask50ms = 0;
60static int iTask100ms = 0;
61static int iTask200ms = 0;
62static int iTask500ms = 0;
63static int iTask1s = 0;
64
65static int iTaskClock1s = 0; // real seconds task
66
67/*static int iSec = 0;
68static int iMin = 0;
69static int iHours = 0;
70static int iDays = 0;
71static uint32_t Seconds = 0; // counts seconds from boot, wraps around in >130 years
72*/
73// --- Macros -----------------------------------------------------------------
74
75// --- Functionprototypes -----------------------------------------------------
76void RtcInit(void);
77
78/**************************************************************************//**
79 * @brief Main function
80 *****************************************************************************/
81int main(void)
82 {
83 int Seconds = 0;
84
85 /* Chip errata */
86 CHIP_Init();
87
88 TESTPIN_INIT();
89 TESTPIN(ON);
90
91 LedInit();
92
93/* // LFRCO Clock Output on Pin PD9
94 // only series 2 CMU_ClkOutPinConfig(0, cmuSelect_LFRCO, 1, gpioPortD, 9);
95 CMU->CTRL = (CMU->CTRL | CMU_CTRL_CLKOUTSEL0_LFRCO);
96 CMU->ROUTEPEN = (CMU->ROUTEPEN | 1 ); // CLKOUT0PEN
97 CMU->ROUTELOC0 = (CMU->ROUTELOC0 | 4 ); // LOC4
98
99 CMU_ClockEnable(cmuClock_GPIO, true); // Enable GPIO clock
100 GPIO_PinModeSet(gpioPortD, 9, gpioModePushPull, 1);
101*/
102
103 RtcInit();
104
105 TraceInit();
106 TraceInit2();
107
108 CommandInit();
109
110 AdcInit();
111
112 while (1)
113 {
114 TESTPIN(OFF);
115
116 // switch to EM2 except some module needs HFCLK
117 __disable_irq(); // __asm("CPSID i");
118 if ( TraceIsRunning() /* | AdcIsRunning() */ )
119 { // if some module needs HFCLK switch to EM1 instead EM2
120 EMU_EnterEM1();
121 __enable_irq(); // __asm("CPSIE i");
122 }
123 else
124 {// go to EM2
125 LedOff(2);
126 EMU_EnterEM2(true);
127 __enable_irq(); // __asm("CPSIE i");
128 LedOn(2); // superflous here cause should be done in every IRQ handler
129 }
130
131 if ( iTask5ms )
132 {
133 iTask5ms = FALSE;
134 // tasks to be done every 5 milliseconds
135
136 }
137
138 if ( iTask10ms )
139 {
140 iTask10ms = FALSE;
141 // tasks to be done every 10 milliseconds
142
143 LedOff(0); // switch of LED (was switched on in Task1s)
144 }
145
146 if ( iTask25ms )
147 {
148 iTask25ms = FALSE;
149 // tasks to be done every 25 milliseconds
150
151 }
152
153 if ( iTask50ms )
154 {
155 iTask50ms = FALSE;
156 // tasks to be done every 50 milliseconds
157
158 AdcTicker50ms();
159 }
160
161 if ( iTask100ms )
162 {
163 iTask100ms = FALSE;
164 // tasks to be done every 100 milliseconds
165
166 AdcTicker100ms();
167 }
168
169 if ( iTask200ms )
170 {
171 iTask200ms = FALSE;
172 // tasks to be done every 200 milliseconds
173
174 AdcTicker200ms();
175 CommandProcessing();
176 }
177
178
179 if ( iTask500ms )
180 {
181 iTask500ms = FALSE;
182 // tasks to be done every 500 milliseconds
183
184 }
185
186 if ( iTask1s )
187 {
188 iTask1s = FALSE;
189 // tasks to be be done every second (about)
190
191 LedOn(0); // switch on LED once a second (alive)
192 AdcTicker1s();
193 }
194
195 if ( iTaskClock1s )
196 {
197 iTaskClock1s = FALSE;
198 // tasks to be done every real second
199
200// MaiUptimeInc();
201 Seconds++; // overflow in 136 years
202
203 TraceTicker1s();
204 }
205 }
206 }
207
208/**************************************************************************//**
209 * @brief Setup RTC
210 *****************************************************************************/
211void RtcInit(void)
212 {
213 RTCC_Init_TypeDef rtcc = RTCC_INIT_DEFAULT;
214 // Configure the compare settings
215 RTCC_CCChConf_TypeDef compare = RTCC_CH_INIT_COMPARE_DEFAULT;
216
217 /* Enabling clock to the interface of the low energy modules */
218 CMU_ClockEnable(cmuClock_CORELE, true);
219
220 /* Routing the LFRCO clock to the RTC */
221 CMU_ClockSelectSet(cmuClock_LFE, cmuSelect_LFRCO);
222 CMU_ClockEnable(cmuClock_RTCC, true);
223
224 // Configure the RTCC settings
225 rtcc.enable = false;
226 rtcc.presc = rtccCntPresc_1;
227 rtcc.cntMode = _RTCC_CTRL_CNTMODE_NORMAL;
228 rtcc.cntWrapOnCCV1 = true;
229
230 /* Initialize the RTCC */
231 RTCC_Init(&rtcc);
232
233 // Initialise RTCC compare with a date, the date when interrupt will occur
234 RTCC_ChannelInit(1, &compare);
235 RTCC_ChannelCompareValueSet(1, (((32768 * RTCC_WAKEUP_MS) / 1000) - 1) );
236
237 // Set channel 1 to cause an interrupt
238 RTCC_IntEnable(RTCC_IEN_CC1);
239 NVIC_SetPriority(RTCC_IRQn, 7);
240 NVIC_ClearPendingIRQ(RTCC_IRQn);
241 NVIC_EnableIRQ(RTCC_IRQn);
242
243 // Start counter after all initialisations are complete
244 RTCC_Enable(true);
245
246
247 /* Setting up RTC for about 1ms */
248 // - 1 is needed for 32 count cycles => 0.9765625ms
249 // let it run for 33 counts (without - 1) gives 1.007080078ms
250 // ToDo: RTC_CompareSet(0, (RTC_FREQ / 1000) - 1);
251
252 // wait for sync
253 // while ( RTC->SYNCBUSY ) ;
254 }
255
256/******************************************************************************
257void RTC_IRQHandler(void)
258 Called every ~976us. Set flags for various periodically tasks.
259 Times are multiples of 976us so iTask50ms is set every ~48.828ms and iTask1s
260 every 0.97656s.
261
262 In addition iTaskClock1s is set every real seconds.
263
264 ToDo: Check if still true with JADE RTCC
265******************************************************************************/
266//void RTC_IRQHandler(void)
267void RTCC_IRQHandler(void)
268 {
269 LedOn(2);
270 // Read the interrupt source
271 rtccFlag = RTCC_IntGet();
272
273 // Clear interrupt flag
274 RTCC_IntClear(rtccFlag);
275
276 iTaskTicks++;
277
278 // prevent iTaskTicks overflow as this will produce glitches
279 if ( iTaskTicks > 2147482999 ) // wraps around every 2199028s = 610.84h = 25 days
280 { // then there's a little error for iTaskClock1s
281 iTaskTicks = 1000;
282 }
283
284 if ( (iTaskTicks % 5) == 0 ) iTask5ms = TRUE;
285 if ( (iTaskTicks % 10) == 0 ) iTask10ms = TRUE;
286 if ( (iTaskTicks % 25) == 0 ) iTask25ms = TRUE;
287 if ( (iTaskTicks % 50) == 0 ) iTask50ms = TRUE;
288 if ( (iTaskTicks % 100) == 0 ) iTask100ms = TRUE;
289 if ( (iTaskTicks % 200) == 0 ) iTask200ms = TRUE;
290 if ( (iTaskTicks % 500) == 0 ) iTask500ms = TRUE;
291 if ( (iTaskTicks % 1000) == 0 ) iTask1s = TRUE;
292
293 // for counting real seconds
294 iMsTicks++;
295 if ( (iMsTicks % 1024) == 0 ) iTaskClock1s = TRUE;
296 }
297
298/******************************************************************************
299int StackCheck (void)
300 Checks max usage of stack, searches from STACK_END upwards till other than
301 initial value is found
302
303 INITIALIZE_STACK must be defined for thumb_crt0.s in order to enable stack
304 checking. ..\..\System\thumb_crt0.s is a modified version of Rowleys
305
306Returns unused bytes on stack left or -1 in case of error
307******************************************************************************/
308int StackCheck (void)
309 {
310 int i;
311 BYTE *pB;
312
313 // _STACK_START and _STACK_END is reversed in startup code
314 pB = (BYTE *) _STACK_START;
315
316 TRACE_L3("MAI: stack from 0x%0X to 0x%0X (%d bytes)", _STACK_END, _STACK_START, _STACK_END - _STACK_START);
317 if (_STACK_START == _STACK_END)
318 {
319 TRACE_L2("MAI: stack not initialized, unable to perform stack check");
320 return -1;
321 }
322
323 for (i = 0; i < _STACK_END - _STACK_START; i++, pB++)
324 {
325 if (*pB != 0xCC)
326 {
327 TRACE_L3("MAI: stack - %d bytes free", i );
328 return i;
329 }
330 }
331
332 return -1;
333 }
334
335
336
337
Note: See TracBrowser for help on using the repository browser.