-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuMT_SAM_SysTick.cpp
279 lines (204 loc) · 7.14 KB
/
uMT_SAM_SysTick.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
////////////////////////////////////////////////////////////////////////////////////
//
// FILE: uMTarduinoSysTick.cpp
// AUTHOR: Antonio Pastore - April 2017
// Program originally written by Antonio Pastore, Torino, ITALY.
// UPDATED: 28 April 2017
//
////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) <2017> Antonio Pastore, Torino, ITALY.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////////
#include <Arduino.h>
#include "uMT.h"
#include "uMTdebug.h"
#if defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_SAMD)
extern void uMT_SystemTicks();
extern unsigned uMTdoTicksWork();
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
// CLASS uMT
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
extern uint32_t GetTickCount();
// Generate a "pendSVHook" exception
#define GeneratePendSVHook_int() SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk
#define EnableInterrupts() asm volatile ("cpsie i")
#define DisableInterrupts() asm volatile ("cpsid i")
extern "C" {
#define USE_TICK_HOOD_SAM 1 // 0 not to use pendSVHook() in sysTickHook(): of course no task switching....
////////////////////////////////////////////////////////////////////////////////////
//
// sysTickHook
//
// This is called by the ARM SysTick interrupt routine.
// It must return 0 so any other step is performed in the original SysTick_Handler
////////////////////////////////////////////////////////////////////////////////////
unsigned int __attribute__((noinline)) sysTickHook()
{
#if USE_TICK_HOOD_SAM==1
// Generate a "pendSVHook" exception
if (uMT::Inited == TRUE)
GeneratePendSVHook_int();
#endif
return (0);
}
////////////////////////////////////////////////////////////////////////////////////
//
// pendSVHook
//
// This is executed when "pendSVHook" exception is generated.
//
////////////////////////////////////////////////////////////////////////////////////
void __attribute__ ((naked)) __attribute__((noinline)) pendSVHook(void)
{
// On entry, HW_EXCP is already saved
#if defined(ARDUINO_ARCH_SAMD)
/* for cortex m0, ldm and stm are restricted to low registers */
asm volatile (
"mov r3, lr;"
"mov r2, r11;"
"mov r1, r10;"
"mov r0, r9;"
"push {r0-r3};"
"mov r3, r8;"
"mov r2, r7;"
"push {r2-r6};"
);
uint32_t CurrentTick = millis();
#else
// Save the remaining registers
asm volatile ("push {r4-r11,lr}");
uint32_t CurrentTick = GetTickCount();
#endif
if (CurrentTick < Kernel.msTickCounter.Low) // RollOver..
Kernel.msTickCounter.High++;
Kernel.msTickCounter.Low = CurrentTick; // Simply copy ticks counter...
if (Kernel.kernelCfg.BlinkingLED)
{
if ((Kernel.msTickCounter % uMT_TICKS_SECONDS) == 0)
{
volatile static Bool_t f_led = TRUE;
if (f_led == TRUE)
{
f_led = FALSE;
digitalWrite(LED_BUILTIN, HIGH);
}
else
{
f_led = TRUE;
digitalWrite(LED_BUILTIN, LOW);
}
}
}
if (uMTdoTicksWork() == 1)
{
///////////////////////////////////////////
// Suspend task and force a reschedule
///////////////////////////////////////////
// Disable INTS
DisableInterrupts();
register StackPtr_t stack_ptr asm ("sp");
if (Kernel.KernelStackMode == FALSE) // to support Restart()
Kernel.Running->SavedSP = stack_ptr; // Save Task's Stack Pointer
Kernel.NoPreempt = TRUE; // Prevent further rescheduling.... until next one
// Switch to private stack and call Reschedule();
Kernel.NewStackReschedule();
// Returning to the caller only when this task is again the RUNNING task
}
else
{
#if defined(ARDUINO_ARCH_SAMD)
/* for cortex m0, ldm and stm are restricted to low registers */
asm volatile (
"pop {r2-r6};"
"mov r8, r3;"
"mov r7, r2;"
"pop {r0-r3};"
"mov r9, r0;"
"mov r10, r1;"
"mov r11, r2;"
"mov lr, r3;"
"bx lr;");
#else
// Restore registers
asm volatile ("pop {r4-r11,lr}");
// Return from EXCEPTION
asm volatile ("bx lr");
#endif
}
}
}; // extern "C" linkage
////////////////////////////////////////////////////////////////////////////////////
//
// uMT::SetupSysTicks
//
////////////////////////////////////////////////////////////////////////////////////
void uMT::SetupSysTicks()
{
// Switch LED off
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
#if defined(ARDUINO_ARCH_SAMD)
// Align SystemTick
Kernel.msTickCounter.Low = millis();
#else
// Align SystemTick
Kernel.msTickCounter.Low = GetTickCount(); // Simply copy ticks counter...
#endif
// Force the loading of the sysTickHook() routine to overwrite Arduino "weak" version
if (Kernel.msTickCounter.Low == 0xFFFFFFFF)
{
sysTickHook();
pendSVHook();
}
#if defined(ARDUINO_ARCH_SAM)
// Set interrupts to be preemptive. Change the grouping to set no sub-priority.
// See SAM3x8E datasheet 12.6.6 page 84 and 12.21.6.1 page 177.
NVIC_SetPriorityGrouping (0b011);
// Configure the system tick frequency to adjust the time quantum allocated to processes.
// Not needed, done already in Arduino
// SysTick_Config (SystemCoreClock / SYSTICK_FREQUENCY_HZ) ;
// Set the base priority register to 0 to allow any exception to be handled.
// See SAM3x8E datasheet 12.4.3.14 page 62.
__set_BASEPRI (0) ;
// Force the PendSV exception to have the lowest priority to avoid killing other interrupts.
// See SAM3x8E datasheet 12.20.10.1 page 168. */
NVIC_SetPriority (PendSV_IRQn, 0xFF) ;
#else
NVIC_SetPriority (PendSV_IRQn, 0xFF) ;
#endif
// Enable SVCall_IRQn
// NVIC_EnableIRQ (SVCall_IRQn) ; // It seems useless
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// isr_Kn_Reboot - ARDUINO_UNO/MEGA
//
//
/////////////////////////////////////////////////////////////////////////////////////////////////
void uMT::isr_Kn_Reboot()
{
NoPreempt = TRUE; // Prevent further rescheduling.... until next one
NVIC_SystemReset(); // Reset processor and internal peripherals
while (1)
{
// do nothing and wait for the eventual...
}
}
#endif
///////////// EOF