-
Notifications
You must be signed in to change notification settings - Fork 270
/
main.c
309 lines (246 loc) · 6.98 KB
/
main.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* Copyright (C) 2011-2018 by Jacob Alexander
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// ----- Includes -----
// Compiler Includes
#include <Lib/MainLib.h>
// Project Includes
#include <macro.h>
#include <scan_loop.h>
#include <output_com.h>
#include <cli.h>
#include <latency.h>
#include <led.h>
#include <print.h>
#include <Lib/periodic.h>
#include <Lib/sysview.h>
#include <Lib/storage.h>
// ----- Enumerations -----
typedef enum PeriodicStage {
PeriodicStage_Scan,
PeriodicStage_Macro,
PeriodicStage_Output,
} PeriodicStage;
// ----- Variables -----
// Periodic Stage Tracker
static volatile PeriodicStage stage_tracker;
// ----- Functions -----
// Run periodically at a consistent time rate
// Used to process events that need to be run at regular intervals
// And have negative effect being delayed or stretched too much
//
// Returns 1 if full rotation has completed
// Returns 0 otherwise
int main_periodic()
{
// Scan module periodic routines
switch ( stage_tracker )
{
case PeriodicStage_Scan:
// Returns non-zero if ready to process macros
SEGGER_SYSVIEW_OnTaskStartExec(TASK_SCAN_PERIODIC);
if ( Scan_periodic() )
{
stage_tracker = PeriodicStage_Macro;
}
SEGGER_SYSVIEW_OnTaskTerminate(TASK_SCAN_PERIODIC);
break;
case PeriodicStage_Macro:
// Run Macros over Key Indices and convert to USB Keys
SEGGER_SYSVIEW_OnTaskStartExec(TASK_MACRO_PERIODIC);
Macro_periodic();
stage_tracker = PeriodicStage_Output;
SEGGER_SYSVIEW_OnTaskTerminate(TASK_MACRO_PERIODIC);
break;
case PeriodicStage_Output:
// Send periodic USB results
SEGGER_SYSVIEW_OnTaskStartExec(TASK_OUTPUT_PERIODIC);
Output_periodic();
stage_tracker = PeriodicStage_Scan;
SEGGER_SYSVIEW_OnTaskTerminate(TASK_OUTPUT_PERIODIC);
// Full rotation
return 1;
}
return 0;
}
// ----- MCU-only Functions -----
#if !defined(_host_)
int main()
{
#ifdef SEGGER_SYSVIEW_H
SEGGER_SYSVIEW_Conf();
SEGGER_SYSVIEW_OnTaskCreate(TASK_SCAN_PERIODIC);
SEGGER_SYSVIEW_OnTaskCreate(TASK_MACRO_PERIODIC);
SEGGER_SYSVIEW_OnTaskCreate(TASK_OUTPUT_PERIODIC);
SEGGER_SYSVIEW_OnTaskCreate(TASK_CLI_PROCESS);
SEGGER_SYSVIEW_OnTaskCreate(TASK_SCAN_POLL);
SEGGER_SYSVIEW_OnTaskCreate(TASK_MACRO_POLL);
SEGGER_SYSVIEW_OnTaskCreate(TASK_OUTPUT_POLL);
#endif
// Setup Latency Measurements
Latency_init();
// Enable CLI
CLI_init();
// Setup periodic timer function
Periodic_function( &main_periodic );
#if Storage_Enable_define == 1
Storage_init();
#endif
// Setup Modules
Output_setup();
Macro_setup();
Scan_setup();
#if Storage_Enable_define == 1
storage_load_settings();
#endif
// Start scanning on first periodic loop
stage_tracker = PeriodicStage_Scan;
#if DEBUG_RESETS
// Blink to indicate a reset happened
errorLED(0);
delay_ms(500);
errorLED(1);
delay_ms(500);
errorLED(0);
#endif
// Main Detection Loop
while ( 1 )
{
// Run constantly
// Used to process things such as the cli and output module (i.e. USB).
// Should not be used to run things that require consistent timing.
// While counter-intuitive, things such as LED/Display modules should be run as poll
// as they need to run as quickly as possible, in case there needs to be frame drops
// Process CLI
SEGGER_SYSVIEW_OnTaskStartExec(TASK_CLI_PROCESS);
CLI_process();
SEGGER_SYSVIEW_OnTaskTerminate(TASK_CLI_PROCESS);
//SEGGER_SYSVIEW_OnIdle();
// Scan module poll routines
SEGGER_SYSVIEW_OnTaskStartExec(TASK_SCAN_POLL);
Scan_poll();
SEGGER_SYSVIEW_OnTaskTerminate(TASK_SCAN_POLL);
//SEGGER_SYSVIEW_OnIdle();
// Macro module poll routines
SEGGER_SYSVIEW_OnTaskStartExec(TASK_MACRO_POLL);
Macro_poll();
SEGGER_SYSVIEW_OnTaskTerminate(TASK_MACRO_POLL);
// Output module poll routines
SEGGER_SYSVIEW_OnTaskStartExec(TASK_OUTPUT_POLL);
Output_poll();
SEGGER_SYSVIEW_OnTaskTerminate(TASK_OUTPUT_POLL);
SEGGER_SYSVIEW_OnIdle();
#if defined(_sam_)
// Not locked up... Reset the watchdog timer
WDT->WDT_CR = WDT_CR_KEY_PASSWD | WDT_CR_WDRSTT;
#elif defined(_kinetis_)
// Not locked up... Reset the watchdog timer
if ( WDOG_STCTRLH_WDOGEN && WDOG_TMROUTL > 2 )
{
__disable_irq();
WDOG_REFRESH = WDOG_REFRESH_SEQ1;
WDOG_REFRESH = WDOG_REFRESH_SEQ2;
__enable_irq();
}
#endif
}
}
#endif
// ----- Host-only Functions -----
#if defined(_host_)
int Host_init()
{
// Enable CLI
CLI_init();
// Setup Modules
Output_setup();
Macro_setup();
Scan_setup();
// Start scanning on first periodic loop
stage_tracker = PeriodicStage_Scan;
return 1;
}
int Host_cli_process()
{
// Process CLI
return CLI_process();
}
int Host_periodic()
{
return main_periodic();
}
int Host_poll()
{
// Run constantly
// Used to process things such as the cli and output module (i.e. USB).
// Should not be used to run things that require consistent timing.
// While counter-intuitive, things such as LED/Display modules should be run as poll
// as they need to run as quickly as possible, in case there needs to be frame drops
// Process CLI
CLI_process();
// Scan module poll routines
Scan_poll();
// Macro module poll routines
Macro_poll();
// Output module poll routines
Output_poll();
return 1;
}
int Host_process()
{
// Run periodic loop for a full rotation
while ( !Host_periodic() );
// Then a single poll loop
Host_poll();
return 1;
}
// Register callback for system calls
int Host_register_callback( void* func )
{
Output_Host_Callback = func;
return 1;
}
// Change the value of systick (milliseconds)
volatile uint32_t systick_millis_count;
int Host_set_systick( uint32_t systick_ms )
{
systick_millis_count = systick_ms;
return 1;
}
// Change the nanosecs since last systick
volatile uint32_t ns_since_systick_count;
int Host_set_nanosecs_since_systick( uint32_t systick_ns )
{
ns_since_systick_count = systick_ns;
return 1;
}
// Test function to validate library
int Host_callback_test()
{
// Print twice to validate callback
print("Test 1 ");
print("Test 2 ");
print( NL );
print("Systick Millisecond: ");
printHex32( systick_millis_count );
print( NL );
return 9;
}
#endif