-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtimers.cpp
More file actions
50 lines (46 loc) · 1.6 KB
/
timers.cpp
File metadata and controls
50 lines (46 loc) · 1.6 KB
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
#include "timers.h"
#include "GenesisController.h"
#include "pico/stdlib.h"
//! The controllers array used by these timers
static GenesisController *gTimerControllers = nullptr;
//! The number of controllers stored in gTimerControllers
static uint32_t gTimerControllersCount = 0;
void init_timer(GenesisController *controllers, uint32_t count)
{
gTimerControllers = controllers;
gTimerControllersCount = count;
}
// Note: Pico can't seem to handle reload times of 6 us very well. When attempted, trigger times
// ranged from 9 us to 12 us. Since this chip has two cores, it's not much of an issue to use
// an entire core just for timing :)
void timer_process()
{
absolute_time_t nextTime;
update_us_since_boot(&nextTime, time_us_64());
while (1)
{
// Wait about 20 ms
update_us_since_boot(&nextTime, to_us_since_boot(nextTime) + 20000);
busy_wait_until(nextTime);
for (uint8_t count = 0; count < GenesisController::NUM_COUNTS; ++count)
{
// Wait about 6 us
update_us_since_boot(&nextTime, to_us_since_boot(nextTime) + 6);
busy_wait_until(nextTime);
// Sample all inputs
uint32_t inputs = gpio_get_all();
// Process the current cycle state
GenesisController *controller = gTimerControllers;
for (uint32_t i = gTimerControllersCount; i > 0; --i, ++controller)
{
controller->nextSignal(count, inputs);
}
}
// Commit all signals now
GenesisController *controller = gTimerControllers;
for (uint32_t i = gTimerControllersCount; i > 0; --i, ++controller)
{
controller->commitSignals();
}
}
}