aboutsummaryrefslogtreecommitdiff
path: root/crates/imxrt1170evk/src/lib.rs
blob: b626843420ce591052ada55f4471a8b7dfdd0e7d (plain)
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
#![no_std]
#![feature(rustc_private)]
extern crate fusible;

use core::pin::Pin;

use imxrt_ral as ral;
use imxrt_rt as _;
use imxrt1170evk_fcb as _;
use rust_threadx_cortex_m as _;
use rust_threadx_stdio_semihosting as _;

#[doc(hidden)]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn _tx_initialize_low_level() {
    use cortex_m::{
        Peripherals,
        peripheral::{scb::SystemHandler, syst::SystClkSource},
    };

    cortex_m::interrupt::disable();

    unsafe {
        let Peripherals {
            mut SYST, mut SCB, ..
        } = Peripherals::steal();

        SCB.set_priority(SystemHandler::SysTick, 0x40);
        SCB.set_priority(SystemHandler::PendSV, 0xff);
        SCB.set_priority(SystemHandler::SVCall, 0xff);

        SYST.set_clock_source(SystClkSource::External);
        SYST.set_reload(100_000 / 100 - 1);
        SYST.clear_current();
        SYST.enable_interrupt();
        SYST.enable_counter();
    }

    unsafe {
        use ral::ccm;

        let ccm = ccm::CCM::instance();
        let clock_root = &ccm.CLOCK_ROOT[8];
        ral::modify_reg!(ccm::clockroot, clock_root, CLOCK_ROOT_CONTROL, MUX: 1, DIV: 240 - 1);
    }

    unsafe {
        use ral::iomuxc;
        let iomuxc = iomuxc::IOMUXC::instance();

        ral::write_reg!(iomuxc, iomuxc, SW_MUX_CTL_PAD_GPIO_AD_04, MUX_MODE: 5, SION: 0);

        ral::write_reg!(iomuxc, iomuxc, SW_PAD_CTL_PAD_GPIO_AD_04, PUE: 0);
    }

    unsafe {
        use ral::gpio;
        let gpio3 = gpio::GPIO3::instance();

        const GPIO3_OUTPUTS: u32 = led::MASK;

        ral::write_reg!(gpio, gpio3, GDIR, GPIO3_OUTPUTS);
    }
}

/// Controls the LED.
pub mod led {
    use crate::ral::gpio;

    pub(crate) const MASK: u32 = 1 << 3;

    /// Turn on the LED.
    #[inline]
    pub fn set() {
        unsafe { crate::ral::write_reg!(gpio, gpio::GPIO3, DR_SET, MASK) };
    }

    /// Turn off the LED.
    #[inline]
    pub fn clear() {
        unsafe { crate::ral::write_reg!(gpio, gpio::GPIO3, DR_CLEAR, MASK) };
    }

    /// Toggle the LED.
    ///
    /// This is achieved in hardware, without reading the current state
    /// of the LED. It's usually more efficient.
    #[inline]
    pub fn toggle() {
        unsafe { crate::ral::write_reg!(gpio, gpio::GPIO3, DR_TOGGLE, MASK) };
    }

    /// Drive the LED on or off.
    #[inline]
    pub fn drive(value: bool) {
        if value { set() } else { clear() }
    }
}

use fusible::thread::{StaticStack, Thread, ThreadContext};
static STACK: StaticStack<512> = StaticStack::new();
static THREAD: ThreadContext = Thread::context();

#[unsafe(no_mangle)]
#[doc(hidden)]
pub extern "C" fn __rust_threadx_app_define() {
    Thread::create(
        Pin::static_ref(&THREAD),
        STACK.take().unwrap(),
        &Default::default(),
        || loop {
            fusible::thread::sleep(25);
            led::toggle();
        },
    )
    .unwrap();
}