aboutsummaryrefslogtreecommitdiff
path: root/crates/rp-pico-2040/src/lib.rs
blob: 8f877aa0e02dc90a34c9e6aa3b32c2c09f3c02bc (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
//! Standard library support crate for an Raspberry Pi Pico with an RP2040.
//!
//! The blinky support example comes from the rp-hal-boards example, available
//! <https://github.com/rp-rs/rp-hal-boards/blob/5135e3dafe3e69b112e6d2d72cfb1856a7679b82/boards/rp-pico/examples/pico_blinky.rs#L1>
//! and licensed MIT or APACHE-2.0.

#![no_std]
#![feature(rustc_private)]
extern crate fusible;

use core::pin::Pin;

use rust_threadx_cortex_m as _;
use rust_threadx_stdio_semihosting as _;
use rust_threadx_sync_single_core as _;

use embedded_hal::digital::OutputPin;
use rp_pico::hal;
use rp_pico::hal::pac;

#[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::Core);
        SYST.set_reload(125_000_000 / 100 - 1);
        SYST.clear_current();
        SYST.enable_interrupt();
        SYST.enable_counter();
    }
}

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() {
    let mut pac = pac::Peripherals::take().unwrap();

    let mut watchdog = hal::Watchdog::new(pac.WATCHDOG);

    hal::clocks::init_clocks_and_plls(
        rp_pico::XOSC_CRYSTAL_FREQ,
        pac.XOSC,
        pac.CLOCKS,
        pac.PLL_SYS,
        pac.PLL_USB,
        &mut pac.RESETS,
        &mut watchdog,
    )
    .ok()
    .unwrap();

    let sio = hal::Sio::new(pac.SIO);

    let pins = rp_pico::Pins::new(
        pac.IO_BANK0,
        pac.PADS_BANK0,
        sio.gpio_bank0,
        &mut pac.RESETS,
    );

    let mut led_pin = pins.led.into_push_pull_output();

    Thread::create(
        Pin::static_ref(&THREAD),
        STACK.take().unwrap(),
        &Default::default(),
        move || loop {
            led_pin.set_high().unwrap();
            fusible::thread::sleep(50);
            led_pin.set_low().unwrap();
            fusible::thread::sleep(50);
        },
    )
    .unwrap();
}