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
//! Reference Manual for the Imxrt-1052 development board
//!
//! - <https://www.nxp.com/webapp/Download?colCode=IMXRT1050RM>

#![no_std]
#![no_main]
#![deny(missing_docs)]

use capsules::virtual_alarm::VirtualMuxAlarm;
use components::gpio::GpioComponent;
use kernel::capabilities;
use kernel::common::dynamic_deferred_call::{DynamicDeferredCall, DynamicDeferredCallClientState};
use kernel::component::Component;
use kernel::debug;
use kernel::hil::gpio::Configure;
use kernel::hil::led::LedLow;
use kernel::Platform;
use kernel::{create_capability, static_init};

// use components::fxos8700::Fxos8700Component;
// use components::ninedof::NineDofComponent;
use imxrt1050::iomuxc::DriveStrength;
use imxrt1050::iomuxc::MuxMode;
use imxrt1050::iomuxc::OpenDrainEn;
use imxrt1050::iomuxc::PadId;
use imxrt1050::iomuxc::PullKeepEn;
use imxrt1050::iomuxc::PullUpDown;
use imxrt1050::iomuxc::Sion;
use imxrt1050::iomuxc::Speed;
use imxrt10xx as imxrt1050;

// Unit Tests for drivers.
// #[allow(dead_code)]
// mod virtual_uart_rx_test;

/// Support routines for debugging I/O.
pub mod io;

/// Defines a vector which contains the boot section
pub mod boot_header;

// Number of concurrent processes this platform supports.
const NUM_PROCS: usize = 1;

// Actual memory for holding the active process structures.
static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROCS] = [None];

type Chip = imxrt1050::chip::Imxrt10xx<imxrt1050::chip::Imxrt10xxDefaultPeripherals>;
static mut CHIP: Option<&'static Chip> = None;

// How should the kernel respond when a process faults.
const FAULT_RESPONSE: kernel::procs::FaultResponse = kernel::procs::FaultResponse::Panic;

// Manually setting the boot header section that contains the FCB header
#[used]
#[link_section = ".boot_hdr"]
static BOOT_HDR: [u8; 8192] = boot_header::BOOT_HDR;

/// Dummy buffer that causes the linker to reserve enough space for the stack.
#[no_mangle]
#[link_section = ".stack_buffer"]
pub static mut STACK_MEMORY: [u8; 0x1000] = [0; 0x1000];

// const NUM_LEDS: usize = 1;

/// A structure representing this platform that holds references to all
/// capsules for this platform.
struct Imxrt1050EVKB {
    alarm: &'static capsules::alarm::AlarmDriver<
        'static,
        VirtualMuxAlarm<'static, imxrt1050::gpt::Gpt1<'static>>,
    >,
    button: &'static capsules::button::Button<'static, imxrt1050::gpio::Pin<'static>>,
    console: &'static capsules::console::Console<'static>,
    gpio: &'static capsules::gpio::GPIO<'static, imxrt1050::gpio::Pin<'static>>,
    ipc: kernel::ipc::IPC<NUM_PROCS>,
    led: &'static capsules::led::LedDriver<'static, LedLow<'static, imxrt1050::gpio::Pin<'static>>>,
    ninedof: &'static capsules::ninedof::NineDof<'static>,
}

/// Mapping of integer syscalls to objects that implement syscalls.
impl Platform for Imxrt1050EVKB {
    fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
    where
        F: FnOnce(Option<&dyn kernel::Driver>) -> R,
    {
        match driver_num {
            capsules::alarm::DRIVER_NUM => f(Some(self.alarm)),
            capsules::button::DRIVER_NUM => f(Some(self.button)),
            capsules::console::DRIVER_NUM => f(Some(self.console)),
            capsules::gpio::DRIVER_NUM => f(Some(self.gpio)),
            kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
            capsules::led::DRIVER_NUM => f(Some(self.led)),
            capsules::ninedof::DRIVER_NUM => f(Some(self.ninedof)),
            _ => f(None),
        }
    }
}

/// Helper function called during bring-up that configures DMA.
/// DMA for imxrt1050-evkb is not implemented yet.
// unsafe fn setup_dma() {
// }

/// Helper function called during bring-up that configures multiplexed I/O.
unsafe fn set_pin_primary_functions(
    peripherals: &'static imxrt1050::chip::Imxrt10xxDefaultPeripherals,
) {
    use imxrt1050::gpio::PinId;

    peripherals.ccm.enable_iomuxc_clock();
    peripherals.ccm.enable_iomuxc_snvs_clock();

    peripherals.ports.gpio1.enable_clock();

    // User_LED is connected to GPIO_AD_B0_09.
    // Values set accordingly to the evkbimxrt1050_iled_blinky SDK example

    // First we configure the pin in GPIO mode and disable the Software Input
    // on Field, so that the Input Path is determined by functionality.
    peripherals.iomuxc.enable_sw_mux_ctl_pad_gpio(
        PadId::AdB0,
        MuxMode::ALT5, // ALT5 for AdB0_09: GPIO1_IO09 of instance: gpio1
        Sion::Disabled,
        9,
    );

    // Configure the pin resistance value, pull up or pull down and other
    // physical aspects.
    peripherals.iomuxc.configure_sw_pad_ctl_pad_gpio(
        PadId::AdB0,
        9,
        PullUpDown::Pus0_100kOhmPullDown,   // 100K Ohm Pull Down
        PullKeepEn::Pke1PullKeeperEnabled,  // Pull-down resistor or keep the previous value
        OpenDrainEn::Ode0OpenDrainDisabled, // Output is CMOS, either 0 logic or 1 logic
        Speed::Medium2,                     // Operating frequency: 100MHz - 150MHz
        DriveStrength::DSE6, // Dual/Single voltage: 43/43 Ohm @ 1.8V, 40/26 Ohm @ 3.3V
    );

    // Configuring the GPIO_AD_B0_09 as output
    let pin = peripherals.ports.pin(PinId::AdB0_09);
    pin.make_output();
    kernel::debug::assign_gpios(Some(pin), None, None);

    // User_Button is connected to IOMUXC_SNVS_WAKEUP.
    peripherals.ports.gpio5.enable_clock();

    // We configure the pin in GPIO mode and disable the Software Input
    // on Field, so that the Input Path is determined by functionality.
    peripherals.iomuxc_snvs.enable_sw_mux_ctl_pad_gpio(
        MuxMode::ALT5, // ALT5 for AdB0_09: GPIO5_IO00 of instance: gpio5
        Sion::Disabled,
        0,
    );

    // Configuring the IOMUXC_SNVS_WAKEUP pin as input
    peripherals.ports.pin(PinId::Wakeup).make_input();
}

/// Helper function for miscellaneous peripheral functions
unsafe fn setup_peripherals(peripherals: &imxrt1050::chip::Imxrt10xxDefaultPeripherals) {
    // LPUART1 IRQn is 20
    cortexm7::nvic::Nvic::new(imxrt1050::nvic::LPUART1).enable();

    // TIM2 IRQn is 28
    peripherals.gpt1.enable_clock();
    peripherals.gpt1.start(
        peripherals.ccm.perclk_sel(),
        peripherals.ccm.perclk_divider(),
    );
    cortexm7::nvic::Nvic::new(imxrt1050::nvic::GPT1).enable();
}

/// Reset Handler.
///
/// This symbol is loaded into vector table by the IMXRT1050 chip crate.
/// When the chip first powers on or later does a hard reset, after the core
/// initializes all the hardware, the address of this function is loaded and
/// execution begins here.
#[no_mangle]
pub unsafe fn reset_handler() {
    imxrt1050::init();
    let ccm = static_init!(imxrt1050::ccm::Ccm, imxrt1050::ccm::Ccm::new());
    let peripherals = static_init!(
        imxrt1050::chip::Imxrt10xxDefaultPeripherals,
        imxrt1050::chip::Imxrt10xxDefaultPeripherals::new(ccm)
    );
    peripherals.ccm.set_low_power_mode();
    peripherals.lpuart1.disable_clock();
    peripherals.lpuart2.disable_clock();
    peripherals
        .ccm
        .set_uart_clock_sel(imxrt1050::ccm::UartClockSelection::PLL3);
    peripherals.ccm.set_uart_clock_podf(1);
    peripherals.lpuart1.set_baud();

    set_pin_primary_functions(peripherals);

    setup_peripherals(peripherals);

    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&PROCESSES));

    let dynamic_deferred_call_clients =
        static_init!([DynamicDeferredCallClientState; 2], Default::default());
    let dynamic_deferred_caller = static_init!(
        DynamicDeferredCall,
        DynamicDeferredCall::new(dynamic_deferred_call_clients)
    );
    DynamicDeferredCall::set_global_instance(dynamic_deferred_caller);

    let chip = static_init!(Chip, Chip::new(peripherals));
    CHIP = Some(chip);

    // LPUART1

    // Enable tx and rx from iomuxc
    // TX is on pad GPIO_AD_B0_12
    // RX is on pad GPIO_AD_B0_13
    // Values set accordingly to the evkbimxrt1050_hello_world SDK example

    // First we configure the pin in LPUART mode and disable the Software Input
    // on Field, so that the Input Path is determined by functionality.
    peripherals.iomuxc.enable_sw_mux_ctl_pad_gpio(
        PadId::AdB0,
        MuxMode::ALT2, // ALT2: LPUART1_TXD of instance: lpuart1
        Sion::Disabled,
        13,
    );
    peripherals.iomuxc.enable_sw_mux_ctl_pad_gpio(
        PadId::AdB0,
        MuxMode::ALT2, // ALT2: LPUART1_RXD of instance: lpuart1
        Sion::Disabled,
        14,
    );

    // Configure the pin resistance value, pull up or pull down and other
    // physical aspects.
    peripherals.iomuxc.configure_sw_pad_ctl_pad_gpio(
        PadId::AdB0,
        13,
        PullUpDown::Pus0_100kOhmPullDown,   // 100K Ohm Pull Down
        PullKeepEn::Pke1PullKeeperEnabled,  // Pull-down resistor or keep the previous value
        OpenDrainEn::Ode0OpenDrainDisabled, // Output is CMOS, either 0 logic or 1 logic
        Speed::Medium2,                     // Operating frequency: 100MHz - 150MHz
        DriveStrength::DSE6, // Dual/Single voltage: 43/43 Ohm @ 1.8V, 40/26 Ohm @ 3.3V
    );
    peripherals.iomuxc.configure_sw_pad_ctl_pad_gpio(
        PadId::AdB0,
        14,
        PullUpDown::Pus0_100kOhmPullDown,   // 100K Ohm Pull Down
        PullKeepEn::Pke1PullKeeperEnabled,  // Pull-down resistor or keep the previous value
        OpenDrainEn::Ode0OpenDrainDisabled, // Output is CMOS, either 0 logic or 1 logic
        Speed::Medium2,                     // Operating frequency: 100MHz - 150MHz
        DriveStrength::DSE6, // Dual/Single voltage: 43/43 Ohm @ 1.8V, 40/26 Ohm @ 3.3V
    );

    // Enable clock
    peripherals.lpuart1.enable_clock();

    let lpuart_mux = components::console::UartMuxComponent::new(
        &peripherals.lpuart1,
        115200,
        dynamic_deferred_caller,
    )
    .finalize(());
    io::WRITER.set_initialized();

    // Create capabilities that the board needs to call certain protected kernel
    // functions.
    let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
    let process_management_capability =
        create_capability!(capabilities::ProcessManagementCapability);

    // Setup the console.
    let console = components::console::ConsoleComponent::new(board_kernel, lpuart_mux).finalize(());
    // Create the debugger object that handles calls to `debug!()`.
    components::debug_writer::DebugWriterComponent::new(lpuart_mux).finalize(());

    // LEDs

    // Clock to Port A is enabled in `set_pin_primary_functions()
    let led = components::led::LedsComponent::new(components::led_component_helper!(
        LedLow<'static, imxrt1050::gpio::Pin<'static>>,
        LedLow::new(peripherals.ports.pin(imxrt1050::gpio::PinId::AdB0_09)),
    ))
    .finalize(components::led_component_buf!(
        LedLow<'static, imxrt1050::gpio::Pin<'static>>
    ));

    // BUTTONs
    let button = components::button::ButtonComponent::new(
        board_kernel,
        components::button_component_helper!(
            imxrt1050::gpio::Pin,
            (
                peripherals.ports.pin(imxrt1050::gpio::PinId::Wakeup),
                kernel::hil::gpio::ActivationMode::ActiveHigh,
                kernel::hil::gpio::FloatingState::PullDown
            )
        ),
    )
    .finalize(components::button_component_buf!(imxrt1050::gpio::Pin));

    // ALARM
    let gpt1 = &peripherals.gpt1;
    let mux_alarm = components::alarm::AlarmMuxComponent::new(gpt1).finalize(
        components::alarm_mux_component_helper!(imxrt1050::gpt::Gpt1),
    );

    let alarm = components::alarm::AlarmDriverComponent::new(board_kernel, mux_alarm)
        .finalize(components::alarm_component_helper!(imxrt1050::gpt::Gpt1));

    // GPIO
    // For now we expose only two pins
    let gpio = GpioComponent::new(
        board_kernel,
        components::gpio_component_helper!(
            imxrt1050::gpio::Pin<'static>,
            // The User Led
            0 => peripherals.ports.pin(imxrt1050::gpio::PinId::AdB0_09)
        ),
    )
    .finalize(components::gpio_component_buf!(
        imxrt1050::gpio::Pin<'static>
    ));

    // LPI2C
    // AD_B1_00 is LPI2C1_SCL
    // AD_B1_01 is LPI2C1_SDA
    // Values set accordingly to the evkbimxrt1050_bubble_peripheral SDK example

    // First we configure the pin in LPUART mode and enable the Software Input
    // on Field, so that we force input path of the pad.
    peripherals.iomuxc.enable_sw_mux_ctl_pad_gpio(
        PadId::AdB1,
        MuxMode::ALT3, // ALT3:  LPI2C1_SCL of instance: lpi2c1
        Sion::Enabled,
        0,
    );
    // Selecting AD_B1_00 for LPI2C1_SCL in the Daisy Chain.
    peripherals.iomuxc.enable_lpi2c_scl_select_input();

    peripherals.iomuxc.enable_sw_mux_ctl_pad_gpio(
        PadId::AdB1,
        MuxMode::ALT3, // ALT3:  LPI2C1_SDA of instance: lpi2c1
        Sion::Enabled,
        1,
    );
    // Selecting AD_B1_01 for LPI2C1_SDA in the Daisy Chain.
    peripherals.iomuxc.enable_lpi2c_sda_select_input();

    // Configure the pin resistance value, pull up or pull down and other
    // physical aspects.
    peripherals.iomuxc.configure_sw_pad_ctl_pad_gpio(
        PadId::AdB1,
        0,
        PullUpDown::Pus3_22kOhmPullUp,     // 22K Ohm Pull Up
        PullKeepEn::Pke1PullKeeperEnabled, // Pull-down resistor or keep the previous value
        OpenDrainEn::Ode1OpenDrainEnabled, // Open Drain Enabled (Output is Open Drain)
        Speed::Medium2,                    // Operating frequency: 100MHz - 150MHz
        DriveStrength::DSE6, // Dual/Single voltage: 43/43 Ohm @ 1.8V, 40/26 Ohm @ 3.3V
    );

    peripherals.iomuxc.configure_sw_pad_ctl_pad_gpio(
        PadId::AdB1,
        1,
        PullUpDown::Pus3_22kOhmPullUp,     // 22K Ohm Pull Up
        PullKeepEn::Pke1PullKeeperEnabled, // Pull-down resistor or keep the previous value
        OpenDrainEn::Ode1OpenDrainEnabled, // Open Drain Enabled (Output is Open Drain)
        Speed::Medium2,                    // Operating frequency: 100MHz - 150MHz
        DriveStrength::DSE6, // Dual/Single voltage: 43/43 Ohm @ 1.8V, 40/26 Ohm @ 3.3V
    );

    // Enabling the lpi2c1 clock and setting the speed.
    peripherals.lpi2c1.enable_clock();
    peripherals
        .lpi2c1
        .set_speed(imxrt1050::lpi2c::Lpi2cSpeed::Speed100k, 8);

    use imxrt1050::gpio::PinId;
    let mux_i2c =
        components::i2c::I2CMuxComponent::new(&peripherals.lpi2c1, None, dynamic_deferred_caller)
            .finalize(components::i2c_mux_component_helper!());

    // Fxos8700 sensor
    let fxos8700 = components::fxos8700::Fxos8700Component::new(
        mux_i2c,
        peripherals.ports.pin(PinId::AdB1_00),
    )
    .finalize(());

    // Ninedof
    let ninedof = components::ninedof::NineDofComponent::new(board_kernel)
        .finalize(components::ninedof_component_helper!(fxos8700));

    let imxrt1050 = Imxrt1050EVKB {
        console: console,
        ipc: kernel::ipc::IPC::new(board_kernel, &memory_allocation_capability),
        led: led,
        button: button,
        ninedof: ninedof,
        alarm: alarm,
        gpio: gpio,
    };

    // Optional kernel tests
    //
    // See comment in `boards/imix/src/main.rs`
    // virtual_uart_rx_test::run_virtual_uart_receive(mux_uart);

    debug!("Tock OS initialization complete. Entering main loop");

    extern "C" {
        /// Beginning of the ROM region containing app images.
        ///
        /// This symbol is defined in the linker script.
        static _sapps: u8;
        /// End of the ROM region containing app images.
        ///
        /// This symbol is defined in the linker script.
        static _eapps: u8;
        /// Beginning of the RAM region for app memory.
        static mut _sappmem: u8;
        /// End of the RAM region for app memory.
        static _eappmem: u8;
    }

    kernel::procs::load_processes(
        board_kernel,
        chip,
        core::slice::from_raw_parts(
            &_sapps as *const u8,
            &_eapps as *const u8 as usize - &_sapps as *const u8 as usize,
        ),
        core::slice::from_raw_parts_mut(
            &mut _sappmem as *mut u8,
            &_eappmem as *const u8 as usize - &_sappmem as *const u8 as usize,
        ),
        &mut PROCESSES,
        FAULT_RESPONSE,
        &process_management_capability,
    )
    .unwrap_or_else(|err| {
        debug!("Error loading processes!");
        debug!("{:?}", err);
    });

    let scheduler = components::sched::round_robin::RoundRobinComponent::new(&PROCESSES)
        .finalize(components::rr_component_helper!(NUM_PROCS));
    board_kernel.kernel_loop(
        &imxrt1050,
        chip,
        Some(&imxrt1050.ipc),
        scheduler,
        &main_loop_capability,
    );
}