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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#![no_std]
#![no_main]
#![feature(asm, const_fn, lang_items, compiler_builtins_lib)]
extern crate capsules;
extern crate compiler_builtins;
#[allow(unused_imports)]
#[macro_use(debug, debug_gpio, static_init)]
extern crate kernel;
extern crate sam4l;
use capsules::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
use capsules::virtual_i2c::{I2CDevice, MuxI2C};
use capsules::virtual_spi::{MuxSpiMaster, VirtualSpiMasterDevice};
use kernel::Platform;
use kernel::hil;
use kernel::hil::Controller;
use kernel::hil::spi::SpiMaster;
#[macro_use]
pub mod io;
#[allow(dead_code)]
mod test_take_map_cell;
static mut SPI_READ_BUF: [u8; 64] = [0; 64];
static mut SPI_WRITE_BUF: [u8; 64] = [0; 64];
const NUM_PROCS: usize = 4;
const FAULT_RESPONSE: kernel::process::FaultResponse = kernel::process::FaultResponse::Panic;
#[link_section = ".app_memory"]
static mut APP_MEMORY: [u8; 49152] = [0; 49152];
static mut PROCESSES: [Option<kernel::Process<'static>>; NUM_PROCS] = [None, None, None, None];
struct Hail {
console: &'static capsules::console::Console<'static, sam4l::usart::USART>,
gpio: &'static capsules::gpio::GPIO<'static, sam4l::gpio::GPIOPin>,
alarm: &'static capsules::alarm::AlarmDriver<
'static,
VirtualMuxAlarm<'static, sam4l::ast::Ast<'static>>,
>,
ambient_light: &'static capsules::ambient_light::AmbientLight<'static>,
temp: &'static capsules::temperature::TemperatureSensor<'static>,
ninedof: &'static capsules::ninedof::NineDof<'static>,
humidity: &'static capsules::humidity::HumiditySensor<'static>,
spi: &'static capsules::spi::Spi<'static, VirtualSpiMasterDevice<'static, sam4l::spi::Spi>>,
nrf51822: &'static capsules::nrf51822_serialization::Nrf51822Serialization<
'static,
sam4l::usart::USART,
>,
adc: &'static capsules::adc::Adc<'static, sam4l::adc::Adc>,
led: &'static capsules::led::LED<'static, sam4l::gpio::GPIOPin>,
button: &'static capsules::button::Button<'static, sam4l::gpio::GPIOPin>,
rng: &'static capsules::rng::SimpleRng<'static, sam4l::trng::Trng<'static>>,
ipc: kernel::ipc::IPC,
crc: &'static capsules::crc::Crc<'static, sam4l::crccu::Crccu<'static>>,
dac: &'static capsules::dac::Dac<'static>,
}
impl Platform for Hail {
fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
where
F: FnOnce(Option<&kernel::Driver>) -> R,
{
match driver_num {
capsules::console::DRIVER_NUM => f(Some(self.console)),
capsules::gpio::DRIVER_NUM => f(Some(self.gpio)),
capsules::alarm::DRIVER_NUM => f(Some(self.alarm)),
capsules::spi::DRIVER_NUM => f(Some(self.spi)),
capsules::nrf51822_serialization::DRIVER_NUM => f(Some(self.nrf51822)),
capsules::ambient_light::DRIVER_NUM => f(Some(self.ambient_light)),
capsules::adc::DRIVER_NUM => f(Some(self.adc)),
capsules::led::DRIVER_NUM => f(Some(self.led)),
capsules::button::DRIVER_NUM => f(Some(self.button)),
capsules::humidity::DRIVER_NUM => f(Some(self.humidity)),
capsules::temperature::DRIVER_NUM => f(Some(self.temp)),
capsules::ninedof::DRIVER_NUM => f(Some(self.ninedof)),
capsules::rng::DRIVER_NUM => f(Some(self.rng)),
capsules::crc::DRIVER_NUM => f(Some(self.crc)),
capsules::dac::DRIVER_NUM => f(Some(self.dac)),
kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
_ => f(None),
}
}
}
unsafe fn set_pin_primary_functions() {
use sam4l::gpio::{PA, PB};
use sam4l::gpio::PeripheralFunction::{A, B};
PA[04].configure(Some(A));
PA[05].configure(Some(A));
PA[06].configure(Some(A));
PA[07].configure(None);
PA[08].configure(Some(A));
PA[09].configure(None);
PA[10].configure(None);
PA[11].configure(Some(A));
PA[12].configure(Some(A));
PA[13].configure(None);
PA[14].configure(None);
PA[15].configure(None);
PA[16].configure(None);
PA[17].configure(None);
PA[18].configure(None);
PA[19].configure(None);
PA[20].configure(None);
PA[21].configure(Some(A));
PA[22].configure(Some(A));
PA[23].configure(Some(A));
PA[24].configure(Some(A));
PA[25].configure(Some(B));
PA[26].configure(Some(B));
PB[00].configure(Some(A));
PB[01].configure(Some(A));
PB[02].configure(Some(A));
PB[03].configure(Some(A));
PB[04].configure(Some(A));
PB[05].configure(Some(A));
PB[06].configure(Some(A));
PB[07].configure(Some(A));
PB[08].configure(None);
PB[09].configure(Some(A));
PB[10].configure(Some(A));
PB[11].configure(None);
PB[12].configure(None);
PB[13].configure(None);
PB[14].configure(None);
PB[15].configure(None);
}
#[no_mangle]
pub unsafe fn reset_handler() {
sam4l::init();
sam4l::pm::PM.setup_system_clock(sam4l::pm::SystemClockSource::PllExternalOscillatorAt48MHz {
frequency: sam4l::pm::OscillatorFrequency::Frequency16MHz,
startup_mode: sam4l::pm::OscillatorStartup::FastStart,
});
sam4l::bpm::set_ck32source(sam4l::bpm::CK32Source::RC32K);
set_pin_primary_functions();
kernel::debug::assign_gpios(
Some(&sam4l::gpio::PA[13]),
Some(&sam4l::gpio::PA[15]),
Some(&sam4l::gpio::PA[14]),
);
let mut chip = sam4l::chip::Sam4l::new();
let console = static_init!(
capsules::console::Console<sam4l::usart::USART>,
capsules::console::Console::new(
&sam4l::usart::USART0,
115200,
&mut capsules::console::WRITE_BUF,
kernel::Grant::create()
)
);
hil::uart::UART::set_client(&sam4l::usart::USART0, console);
let nrf_serialization = static_init!(
capsules::nrf51822_serialization::Nrf51822Serialization<sam4l::usart::USART>,
capsules::nrf51822_serialization::Nrf51822Serialization::new(
&sam4l::usart::USART3,
&mut capsules::nrf51822_serialization::WRITE_BUF,
&mut capsules::nrf51822_serialization::READ_BUF
)
);
hil::uart::UART::set_client(&sam4l::usart::USART3, nrf_serialization);
let ast = &sam4l::ast::AST;
let mux_alarm = static_init!(
MuxAlarm<'static, sam4l::ast::Ast>,
MuxAlarm::new(&sam4l::ast::AST)
);
ast.configure(mux_alarm);
let sensors_i2c = static_init!(MuxI2C<'static>, MuxI2C::new(&sam4l::i2c::I2C1));
sam4l::i2c::I2C1.set_master_client(sensors_i2c);
let si7021_i2c = static_init!(
capsules::virtual_i2c::I2CDevice,
capsules::virtual_i2c::I2CDevice::new(sensors_i2c, 0x40)
);
let si7021_virtual_alarm = static_init!(
VirtualMuxAlarm<'static, sam4l::ast::Ast>,
VirtualMuxAlarm::new(mux_alarm)
);
let si7021 = static_init!(
capsules::si7021::SI7021<'static, VirtualMuxAlarm<'static, sam4l::ast::Ast>>,
capsules::si7021::SI7021::new(
si7021_i2c,
si7021_virtual_alarm,
&mut capsules::si7021::BUFFER
)
);
si7021_i2c.set_client(si7021);
si7021_virtual_alarm.set_client(si7021);
let temp = static_init!(
capsules::temperature::TemperatureSensor<'static>,
capsules::temperature::TemperatureSensor::new(si7021, kernel::Grant::create()),
96 / 8
);
kernel::hil::sensors::TemperatureDriver::set_client(si7021, temp);
let humidity = static_init!(
capsules::humidity::HumiditySensor<'static>,
capsules::humidity::HumiditySensor::new(si7021, kernel::Grant::create()),
96 / 8
);
kernel::hil::sensors::HumidityDriver::set_client(si7021, humidity);
let isl29035_i2c = static_init!(I2CDevice, I2CDevice::new(sensors_i2c, 0x44));
let isl29035_virtual_alarm = static_init!(
VirtualMuxAlarm<'static, sam4l::ast::Ast>,
VirtualMuxAlarm::new(mux_alarm)
);
let isl29035 = static_init!(
capsules::isl29035::Isl29035<'static, VirtualMuxAlarm<'static, sam4l::ast::Ast>>,
capsules::isl29035::Isl29035::new(
isl29035_i2c,
isl29035_virtual_alarm,
&mut capsules::isl29035::BUF
)
);
isl29035_i2c.set_client(isl29035);
isl29035_virtual_alarm.set_client(isl29035);
let ambient_light = static_init!(
capsules::ambient_light::AmbientLight<'static>,
capsules::ambient_light::AmbientLight::new(isl29035, kernel::Grant::create())
);
hil::sensors::AmbientLight::set_client(isl29035, ambient_light);
let virtual_alarm1 = static_init!(
VirtualMuxAlarm<'static, sam4l::ast::Ast>,
VirtualMuxAlarm::new(mux_alarm)
);
let alarm = static_init!(
capsules::alarm::AlarmDriver<'static, VirtualMuxAlarm<'static, sam4l::ast::Ast>>,
capsules::alarm::AlarmDriver::new(virtual_alarm1, kernel::Grant::create())
);
virtual_alarm1.set_client(alarm);
let fxos8700_i2c = static_init!(I2CDevice, I2CDevice::new(sensors_i2c, 0x1e));
let fxos8700 = static_init!(
capsules::fxos8700cq::Fxos8700cq<'static>,
capsules::fxos8700cq::Fxos8700cq::new(
fxos8700_i2c,
&sam4l::gpio::PA[9],
&mut capsules::fxos8700cq::BUF
)
);
fxos8700_i2c.set_client(fxos8700);
sam4l::gpio::PA[9].set_client(fxos8700);
let ninedof = static_init!(
capsules::ninedof::NineDof<'static>,
capsules::ninedof::NineDof::new(fxos8700, kernel::Grant::create())
);
hil::sensors::NineDof::set_client(fxos8700, ninedof);
let mux_spi = static_init!(
MuxSpiMaster<'static, sam4l::spi::Spi>,
MuxSpiMaster::new(&sam4l::spi::SPI)
);
sam4l::spi::SPI.set_client(mux_spi);
sam4l::spi::SPI.init();
sam4l::spi::SPI.enable();
let syscall_spi_device = static_init!(
VirtualSpiMasterDevice<'static, sam4l::spi::Spi>,
VirtualSpiMasterDevice::new(mux_spi, 0)
);
let spi_syscalls = static_init!(
capsules::spi::Spi<'static, VirtualSpiMasterDevice<'static, sam4l::spi::Spi>>,
capsules::spi::Spi::new(syscall_spi_device)
);
spi_syscalls.config_buffers(&mut SPI_READ_BUF, &mut SPI_WRITE_BUF);
syscall_spi_device.set_client(spi_syscalls);
let led_pins = static_init!(
[(&'static sam4l::gpio::GPIOPin, capsules::led::ActivationMode); 3],
[
(
&sam4l::gpio::PA[13],
capsules::led::ActivationMode::ActiveLow
),
(
&sam4l::gpio::PA[15],
capsules::led::ActivationMode::ActiveLow
),
(
&sam4l::gpio::PA[14],
capsules::led::ActivationMode::ActiveLow
)
]
);
let led = static_init!(
capsules::led::LED<'static, sam4l::gpio::GPIOPin>,
capsules::led::LED::new(led_pins)
);
let button_pins = static_init!(
[(&'static sam4l::gpio::GPIOPin, capsules::button::GpioMode); 1],
[
(
&sam4l::gpio::PA[16],
capsules::button::GpioMode::LowWhenPressed
)
]
);
let button = static_init!(
capsules::button::Button<'static, sam4l::gpio::GPIOPin>,
capsules::button::Button::new(button_pins, kernel::Grant::create())
);
for &(btn, _) in button_pins.iter() {
btn.set_client(button);
}
let adc_channels = static_init!(
[&'static sam4l::adc::AdcChannel; 6],
[
&sam4l::adc::CHANNEL_AD0,
&sam4l::adc::CHANNEL_AD1,
&sam4l::adc::CHANNEL_AD3,
&sam4l::adc::CHANNEL_AD4,
&sam4l::adc::CHANNEL_AD5,
&sam4l::adc::CHANNEL_AD6
]
);
let adc = static_init!(
capsules::adc::Adc<'static, sam4l::adc::Adc>,
capsules::adc::Adc::new(
&mut sam4l::adc::ADC0,
adc_channels,
&mut capsules::adc::ADC_BUFFER1,
&mut capsules::adc::ADC_BUFFER2,
&mut capsules::adc::ADC_BUFFER3
)
);
sam4l::adc::ADC0.set_client(adc);
let rng = static_init!(
capsules::rng::SimpleRng<'static, sam4l::trng::Trng>,
capsules::rng::SimpleRng::new(&sam4l::trng::TRNG, kernel::Grant::create())
);
sam4l::trng::TRNG.set_client(rng);
let gpio_pins = static_init!(
[&'static sam4l::gpio::GPIOPin; 4],
[
&sam4l::gpio::PB[14],
&sam4l::gpio::PB[15],
&sam4l::gpio::PB[11],
&sam4l::gpio::PB[12]
]
);
let gpio = static_init!(
capsules::gpio::GPIO<'static, sam4l::gpio::GPIOPin>,
capsules::gpio::GPIO::new(gpio_pins)
);
for pin in gpio_pins.iter() {
pin.set_client(gpio);
}
let crc = static_init!(
capsules::crc::Crc<'static, sam4l::crccu::Crccu<'static>>,
capsules::crc::Crc::new(&mut sam4l::crccu::CRCCU, kernel::Grant::create())
);
sam4l::crccu::CRCCU.set_client(crc);
let dac = static_init!(
capsules::dac::Dac<'static>,
capsules::dac::Dac::new(&mut sam4l::dac::DAC)
);
let hail = Hail {
console: console,
gpio: gpio,
alarm: alarm,
ambient_light: ambient_light,
temp: temp,
humidity: humidity,
ninedof: ninedof,
spi: spi_syscalls,
nrf51822: nrf_serialization,
adc: adc,
led: led,
button: button,
rng: rng,
ipc: kernel::ipc::IPC::new(),
crc: crc,
dac: dac,
};
sam4l::gpio::PA[17].enable();
sam4l::gpio::PA[17].enable_output();
sam4l::gpio::PA[17].clear();
for _ in 0..10 {
kernel::support::nop();
}
sam4l::gpio::PA[17].set();
hail.console.initialize();
let kc = static_init!(capsules::console::App, capsules::console::App::default());
kernel::debug::assign_console_driver(Some(hail.console), kc);
hail.nrf51822.initialize();
extern "C" {
static _sapps: u8;
}
kernel::process::load_processes(
&_sapps as *const u8,
&mut APP_MEMORY,
&mut PROCESSES,
FAULT_RESPONSE,
);
kernel::main(&hail, &mut chip, &mut PROCESSES, &hail.ipc);
}