Repository navigation
Expand file tree
/
Copy pathPendSV.cpp
More file actions
162 lines (131 loc) · 3.99 KB
/
Copy pathPendSV.cpp
File metadata and controls
162 lines (131 loc) · 3.99 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
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
#include "PendSV.h"
#include <Arduino.h>
#include <limits.h>
#if defined(USE_TINYUSB)
extern "C" void TinyUSB_Device_Task(void);
#endif
namespace {
PendSV pendSv;
#if defined(USE_TINYUSB)
void tinyUsbDeviceTaskService(uint8_t serviceId, void *context) {
(void)serviceId;
(void)context;
// One invocation drains TinyUSB's queued controller events under OPT_OS_NONE.
TinyUSB_Device_Task();
}
#endif
}
PendSV &PendSV::instance() {
return pendSv;
}
#if defined(USE_TINYUSB)
extern "C" void tud_event_hook_cb(uint8_t rhport, uint32_t eventId, bool inIsr) {
(void)rhport;
(void)eventId;
(void)inIsr;
// TinyUSB owns the event queue. PendSV is only its deferred wake signal, so
// multiple controller events before dispatch require only one service call.
PendSV::instance().setPendingOnce(PendSVChannels::Usb);
}
#endif
bool PendSV::initializeCoreServices() {
NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
#if defined(USE_TINYUSB)
return instance().registerService(PendSVChannels::Usb, tinyUsbDeviceTaskService, nullptr);
#else
return true;
#endif
}
uint32_t PendSV::enterCritical() {
const uint32_t primask = __get_PRIMASK();
__disable_irq();
return primask;
}
void PendSV::exitCritical(uint32_t primask) {
__set_PRIMASK(primask);
}
bool PendSV::registerService(uint8_t serviceId, ServiceFn fn, void *context) {
if (serviceId >= kMaxServices || fn == nullptr)
return false;
const uint32_t primask = enterCritical();
pendingCount_[serviceId] = 0;
pendingMask_ &= ~(1u << serviceId);
services_[serviceId].fn = fn;
services_[serviceId].context = context;
exitCritical(primask);
return true;
}
void PendSV::clearService(uint8_t serviceId) {
if (serviceId >= kMaxServices)
return;
const uint32_t primask = enterCritical();
services_[serviceId].fn = nullptr;
services_[serviceId].context = nullptr;
pendingCount_[serviceId] = 0;
pendingMask_ &= ~(1u << serviceId);
exitCritical(primask);
}
void PendSV::setPending(uint8_t serviceId) {
if (serviceId >= kMaxServices)
return;
const uint32_t primask = enterCritical();
uint16_t &pendingCount = pendingCount_[serviceId];
if (pendingCount < UINT16_MAX)
++pendingCount;
pendingMask_ |= (1u << serviceId);
exitCritical(primask);
__DMB();
SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
}
void PendSV::setPendingOnce(uint8_t serviceId) {
if (serviceId >= kMaxServices)
return;
const uint32_t primask = enterCritical();
pendingCount_[serviceId] = 1;
pendingMask_ |= (1u << serviceId);
exitCritical(primask);
__DMB();
SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
}
void PendSV::dispatchPending() {
// Bound one PendSV entry so high-rate producers do not monopolize return to
// thread mode. Remaining work re-pends PendSV below.
uint8_t dispatched = 0;
while (dispatched < kDispatchBudget) {
uint32_t primask = enterCritical();
const uint32_t pending = pendingMask_;
if (pending == 0) {
exitCritical(primask);
return;
}
const uint8_t serviceId = static_cast<uint8_t>(__builtin_ctz(pending));
uint16_t &pendingCount = pendingCount_[serviceId];
if (pendingCount == 0) {
// Defensive scrub in case mask and count drift out of sync.
pendingMask_ &= ~(1u << serviceId);
exitCritical(primask);
continue;
}
--pendingCount;
if (pendingCount == 0)
pendingMask_ &= ~(1u << serviceId);
ServiceEntry entry = services_[serviceId];
exitCritical(primask);
// Pending work without a registered service is intentionally dropped.
// Producers are expected to register before calling setPending(), and
// clearService() cancels queued work for that service.
if (entry.fn != nullptr)
entry.fn(serviceId, entry.context);
++dispatched;
}
const uint32_t primask = enterCritical();
const bool hasRemaining = (pendingMask_ != 0);
exitCritical(primask);
if (hasRemaining) {
__DMB();
SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
}
}
extern "C" void PendSV_Handler(void) {
PendSV::instance().dispatchPending();
}