-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibstorm.cc
367 lines (358 loc) · 8.41 KB
/
libstorm.cc
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "interface.h"
#include "libstorm.h"
#include <array>
#include <queue>
#include <cstring>
#include <stdio.h>
namespace storm
{
std::shared_ptr<std::vector<uint8_t>> mkbuf(size_t size)
{
return std::make_shared<std::vector<uint8_t>>(size);
}
std::shared_ptr<std::vector<uint8_t>> mkbuf(std::initializer_list<uint8_t> contents)
{
auto rv = std::make_shared<std::vector<uint8_t>>(contents.size());
int idx = 0;
for (auto v : contents) (*rv)[idx++] = v;
return rv;
}
namespace _priv
{
uint32_t __attribute__((naked)) syscall_ex(...)
{
asm volatile (\
"push {r4-r11}\n\t"\
"svc 8\n\t"\
"pop {r4-r11}\n\t"\
"bx lr":::"memory", "r0");
}
void irq_callback(uint32_t idx);
}
namespace tq
{
void Task::fire()
{
(*target)();
}
std::queue<Task> dyn_tq;
template <> bool add(std::shared_ptr<std::function<void(void)>> target)
{
dyn_tq.push(Task(target));
return true;
}
bool run_one()
{
if (dyn_tq.empty())
{
return false;
}
dyn_tq.front().fire();
dyn_tq.pop();
return true;
}
void __attribute__((noreturn)) scheduler()
{
while(1)
{
while(run_one());
k_wait_callback();
}
}
}
namespace util
{
Resource::Resource()
:active(false)
{}
void Resource::acquire(std::function<void()> cb)
{
if (!active && queue.empty())
{
active = true;
tq::add(cb);
}
else
{
queue.push(cb);
}
}
void Resource::release()
{
if (queue.empty())
{
active = false;
}
else
{
auto cb = queue.front();
queue.pop();
tq::add(cb);
}
}
}
namespace gpio
{
//Pin directions
const Dir OUT={0};
const Dir IN={1};
//Pin values
const uint8_t HIGH = 1;
const uint8_t LOW = 0;
const uint8_t TOGGLE = 2;
//Edges
const Edge RISING = {1};
const Edge FALLING = {2};
const Edge CHANGE = {0};
//Pulls
const Pull UP = {1};
const Pull DOWN = {2};
const Pull KEEP = {3};
const Pull NONE = {0};
static std::shared_ptr<std::function<void()>> irq_ptrs [20] = {nullptr};
uint32_t set_mode(Pin pin, Dir dir)
{
return _priv::syscall_ex(0x101, dir.dir, pin.spec);
}
uint32_t set(Pin pin, uint8_t value)
{
return _priv::syscall_ex(0x102, value, pin.spec);
}
uint8_t get(Pin pin)
{
return _priv::syscall_ex(0x103, pin.spec);
}
void set_pull(Pin pin, Pull pull)
{
_priv::syscall_ex(0x104, pull.dir, pin.spec);
}
void disable_irq(Pin pin)
{
if (irq_ptrs[pin.idx])
{
_priv::syscall_ex(0x108, pin.spec);
irq_ptrs[pin.idx].reset();
}
}
void enable_irq(Pin pin, Edge edge, std::shared_ptr<std::function<void(void)>> callback)
{
disable_irq(pin);
irq_ptrs[pin.idx] = callback;
_priv::syscall_ex(0x106, pin.spec, edge.edge, static_cast<void(*)(uint32_t)>(_priv::irq_callback), pin.idx);
}
}
namespace _priv
{
void irq_callback(uint32_t idx)
{
if (idx <= 20 && gpio::irq_ptrs[idx])
{
tq::add(gpio::irq_ptrs[idx]);
}
}
}
namespace _priv
{
void tmr_callback(Timer *self);
}
void Timer::cancel()
{
//TODO: purge any queued callbacks from the task queue
if(self)
{
_priv::syscall_ex(0x205, id);
self.reset();
}
}
void Timer::fire()
{
auto t = self;
tq::add([=](){
(*callback)(t);
});
if (!is_periodic)
{
self.reset(); //undangle ourselves to be deconstructed
}
}
Timer::Timer(bool periodic, uint32_t ticks, std::shared_ptr<std::function<void(std::shared_ptr<Timer>)>> callback):
is_periodic(periodic), callback(callback)
{
uint32_t rv = _priv::syscall_ex(0x201, ticks, periodic, static_cast<void(*)(Timer*)>(_priv::tmr_callback), this);
if (rv == (uint32_t)-1)
{
while(1);
}
id = rv;
}
namespace _priv
{
/*
I expect this usage of a raw pointer to be safe under the following conditions:
- The timer will not be deleted until cancel() is called
- Cancel stops the kernel from creating more timer callbacks +
also purges any previously issued callbacks from the task queue
*/
void tmr_callback(Timer *self)
{
self->fire();
}
struct udp_recv_params_t
{
uint32_t reserved1;
uint32_t reserved2;
uint8_t* buffer;
uint32_t buflen;
uint8_t src_address [16];
uint32_t port;
uint8_t lqi;
uint8_t rssi;
} __attribute__((__packed__));
}
namespace flash
{
void erase_chip()
{
_priv::syscall_ex(0xA03);
}
util::Resource lock;
}
namespace sys
{
uint32_t now()
{
return _priv::syscall_ex(0x202);
}
uint32_t now(Shift shift)
{
return _priv::syscall_ex(shift.code);
}
void kick_wdt()
{
_priv::syscall_ex(0xb01);
}
void reset()
{
_priv::syscall_ex(0x404);
}
const Shift SHIFT_0 = {0x202};
const Shift SHIFT_16 = {0x203};
const Shift SHIFT_48 = {0x204};
}
template<> std::shared_ptr<UDPSocket> UDPSocket::open(uint16_t port, std::shared_ptr<std::function<void(std::shared_ptr<UDPSocket::Packet>)>> callback)
{
auto rv = std::shared_ptr<UDPSocket>(new UDPSocket(port, callback));
if (!rv->okay)
{
return std::shared_ptr<UDPSocket>();
}
rv->self = rv; //Circle reference, we cannot be deconstructed
return rv;
}
void UDPSocket::close()
{
if(self)
{
//TODO purge callbacks from tq
_priv::syscall_ex(0x303, id);
self.reset();
}
}
void UDPSocket::_handle(_priv::udp_recv_params_t *recv, char *addrstr)
{
auto rv = std::make_shared<Packet>();
rv->payload = std::string(reinterpret_cast<const char*>(recv->buffer), static_cast<size_t>(recv->buflen));
rv->strsrc = std::string(addrstr);
std::memcpy(rv->src, recv->src_address, 16);
rv->port = recv->port;
rv->lqi = recv->lqi;
rv->rssi = recv->rssi;
(*callback)(rv);
}
UDPSocket::UDPSocket(uint16_t port, std::shared_ptr<std::function<void(std::shared_ptr<UDPSocket::Packet>)>> callback)
:okay(false), callback(callback)
{
//create
id = (int32_t) _priv::syscall_ex(0x301);
if (id == -1) {
return;
}
//bind
int rv = _priv::syscall_ex(0x302, id, port);
if (rv == -1) {
return;
}
rv = _priv::syscall_ex(0x305, id, static_cast<void(*)(UDPSocket*, _priv::udp_recv_params_t*, char*)>(_priv::udp_callback), this);
if (rv == -1) {
return;
}
okay = true;
}
bool UDPSocket::sendto(const std::string &addr, uint16_t port, const std::string &payload)
{
return sendto(addr, port, reinterpret_cast<const uint8_t*>(&payload[0]), payload.size());
}
bool UDPSocket::sendto(const std::string &addr, uint16_t port, const uint8_t *payload, size_t length)
{
int rv = _priv::syscall_ex(0x304, id, payload, length, addr.data(), port);
return rv == 0;
}
bool UDPSocket::sendto(const std::string &addr, uint16_t port, buf_t payload, size_t length)
{
return sendto(addr, port, reinterpret_cast<const uint8_t*>(&((*payload)[0])), length);
}
namespace _priv
{
void udp_callback(UDPSocket *sock, udp_recv_params_t *recv, char *addrstr)
{
sock->_handle(recv, addrstr);
}
}
namespace _priv
{
void i2c_wcallback(i2c::I2CWOperation *op, int status)
{
tq::add([op, status]
{
op->invoke(status);
});
}
void i2c_rcallback(i2c::I2CROperation *op, int status)
{
tq::add([op, status]
{
op->invoke(status);
});
}
void flash_wcallback(flash::FlashWOperation *op, int status)
{
storm::Timer::once(40*storm::Timer::MILLISECOND, [=](auto)
{
tq::add([op, status]
{
op->invoke(status);
});
});
}
void flash_rcallback(flash::FlashROperation *op, int status)
{
tq::add([op, status]
{
op->invoke(status);
});
}
}
namespace i2c
{
util::Resource lock;
const char* decode(int code)
{
if (code == OK) return "OK";
if (code == DNAK) return "DNAK";
if (code == ANAK) return "ANAK";
if (code == ERR) return "ERR";
if (code == ARBLST) return "ARBLST";
return "UNK";
}
}
}