/
content-catlora-lab5.ino
121 lines (94 loc) · 3.17 KB
/
content-catlora-lab5.ino
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
#include "CatLoRaS76S.h"
#include "LedModule.h"
#define RED_LED_PIN 13
#define GREEN_LED_PIN 12
#define LORA_ABP_CLASS "C"
#define LORA_DEV_EUI "????????????????"
#define LORA_DEV_ADDR "????????"
#define LORA_NWKS_KEY "????????????????????????????????"
#define LORA_APPS_KEY "????????????????????????????????"
LedModule redLed(RED_LED_PIN);
LedModule greenLed(GREEN_LED_PIN);
CatLoRaS76S lora;
int sampleRate = 1024;
void setup() {
Serial.begin(115200);
delay(2000);
redLed.begin();
greenLed.begin();
tcStartTimer(sampleRate);
Serial.println("-> Lora Setting...");
lora.begin(115200);
Serial.println("-> Lora ABP Join...");
lora.joinABP(String(LORA_ABP_CLASS),
String(LORA_DEV_EUI),
String(LORA_DEV_ADDR),
String(LORA_NWKS_KEY),
String(LORA_APPS_KEY));
tcStopTimer();
greenLed.on();
Serial.println("->Ready Go.");
}
void loop() {
}
void TC5_Handler (void) {
//----this code will run every second----
redLed.toggle();
//--------------------------------------
TC5->COUNT16.INTFLAG.bit.MC0 = 1; //Writing a 1 to INTFLAG.bit.MC0 clears the interrupt so that it will run again
}
void tcSetTimerFrequency(int sampleRate)
{
//set TC5 timer counter based off of the system clock and the user defined sample rate or waveform
TC5->COUNT16.CC[0].reg = (uint16_t) (SystemCoreClock / sampleRate - 1);
while (tcIsSyncing());
}
//Function that is used to check if TC5 is done syncing
//returns true when it is done syncing
bool tcIsSyncing()
{
return TC5->COUNT16.STATUS.reg & TC_STATUS_SYNCBUSY;
}
//This function enables TC5 and waits for it to be ready
void tcStartTimer(int sampleRate)
{
// Enable GCLK for TCC2 and TC5 (timer counter input clock)
GCLK->CLKCTRL.reg = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID(GCM_TC4_TC5)) ;
while (GCLK->STATUS.bit.SYNCBUSY);
tcReset(); //reset TC5
// Set Timer counter Mode to 16 bits
TC5->COUNT16.CTRLA.reg |= TC_CTRLA_MODE_COUNT16;
while (TC5->COUNT16.STATUS.bit.SYNCBUSY == 1); // wait for sync
// Set TC5 mode as match frequency
TC5->COUNT16.CTRLA.reg |= TC_CTRLA_WAVEGEN_MFRQ;
while (TC5->COUNT16.STATUS.bit.SYNCBUSY == 1); // wait for sync
//set prescaler and enable TC5
//you can use different prescaler divisons here like TC_CTRLA_PRESCALER_DIV1 to get different ranges of frequencies
TC5->COUNT16.CTRLA.reg |= TC_CTRLA_PRESCALER_DIV1024 | TC_CTRLA_ENABLE;
while (TC5->COUNT16.STATUS.bit.SYNCBUSY == 1); // wait for sync
tcSetTimerFrequency(sampleRate);
// Configure interrupt request
NVIC_DisableIRQ(TC5_IRQn);
NVIC_ClearPendingIRQ(TC5_IRQn);
NVIC_SetPriority(TC5_IRQn, 0);
NVIC_EnableIRQ(TC5_IRQn);
// Enable the TC5 interrupt request
TC5->COUNT16.INTENSET.bit.MC0 = 1;
while (tcIsSyncing()); //wait until TC5 is done syncing
TC5->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE; //set the CTRLA register
while (tcIsSyncing()); //wait until snyc'd
}
//Reset TC5
void tcReset()
{
TC5->COUNT16.CTRLA.reg = TC_CTRLA_SWRST;
while (tcIsSyncing());
while (TC5->COUNT16.CTRLA.bit.SWRST);
}
//disable TC5
void tcStopTimer()
{
TC5->COUNT16.CTRLA.reg &= ~TC_CTRLA_ENABLE;
while (tcIsSyncing());
tcReset();
}