Formula Student Electronics & Software
The code for the embedded software
Loading...
Searching...
No Matches
cycle64.cpp
Go to the documentation of this file.
1
2
3#include "Arduino.h"
4
5namespace cycles64
6{
7 uint64_t get();
8
9 namespace // private -----------------------------
10 {
11 uint32_t oldLow = ARM_DWT_CYCCNT;
12 uint32_t curHigh = 0;
13
14 void SNVS_isr(void)
15 {
16 SNVS_HPSR |= 0b11; // reset interrupt flag
17 get(); // call to check for overflow
18 asm("dsb"); // prevent double calls of the isr
19 }
20
21 } // end private namespace <<---------------------
22
23 uint64_t get()
24 {
25 noInterrupts();
26 uint32_t curLow = ARM_DWT_CYCCNT;
27 if (curLow < oldLow) // roll over
28 {
29 curHigh++;
30 }
31 oldLow = curLow;
32 uint64_t curVal = ((uint64_t)curHigh << 32) | curLow;
33 interrupts();
34
35 return curVal;
36
37 }
38
39 void begin()
40 {
41 // disable periodic snvs interrupt
42 SNVS_HPCR &= ~SNVS_HPCR_PI_EN;
43 while ((SNVS_HPCR & SNVS_HPCR_PI_EN)) {}
44
45 // interrupt frequency 1Hz
46 SNVS_HPCR = SNVS_HPCR_PI_FREQ(0b1111);
47
48 // enable periodic snvs interrupt
49 SNVS_HPCR |= SNVS_HPCR_PI_EN;
50 while (!(SNVS_HPCR & SNVS_HPCR_PI_EN)) {}
51
52 attachInterruptVector(IRQ_SNVS_IRQ, SNVS_isr);
53 NVIC_SET_PRIORITY(IRQ_SNVS_IRQ, 255); // lowest
54 NVIC_ENABLE_IRQ(IRQ_SNVS_IRQ);
55 }
56}
uint64_t get()
Definition cycle64.cpp:23
void begin()
Definition cycle64.cpp:39