|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +use cortex_m_rt::{entry, pre_init}; |
| 5 | +use panic_halt as _; |
| 6 | + |
| 7 | +#[link_section = ".boot_loader"] |
| 8 | +#[no_mangle] |
| 9 | +pub static BOOT_LOADER: [u8; 512] = *include_bytes!("boot2_and_reset.bin"); |
| 10 | + |
| 11 | +#[pre_init] |
| 12 | +unsafe fn pre_init() { |
| 13 | + let sio = &*rp2040_pac::SIO::ptr(); |
| 14 | + |
| 15 | + // If we are core 1, then stop dead and let core 0 do all the work. |
| 16 | + if sio.cpuid.read() != 0u32 { |
| 17 | + loop { |
| 18 | + cortex_m::asm::nop(); |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +unsafe fn setup_chip(p: &mut rp2040_pac::Peripherals) { |
| 24 | + // Now reset all the peripherals, except QSPI and XIP (we're using those |
| 25 | + // to execute from external flash!) |
| 26 | + |
| 27 | + p.RESETS.reset.write(|w| { |
| 28 | + w.adc().set_bit(); |
| 29 | + w.busctrl().set_bit(); |
| 30 | + w.dma().set_bit(); |
| 31 | + w.i2c0().set_bit(); |
| 32 | + w.i2c1().set_bit(); |
| 33 | + w.io_bank0().set_bit(); |
| 34 | + w.io_qspi().clear_bit(); |
| 35 | + w.jtag().set_bit(); |
| 36 | + w.pads_bank0().set_bit(); |
| 37 | + w.pads_qspi().clear_bit(); |
| 38 | + w.pio0().set_bit(); |
| 39 | + w.pio1().set_bit(); |
| 40 | + w.pll_sys().clear_bit(); |
| 41 | + w.pll_usb().clear_bit(); |
| 42 | + w.pwm().set_bit(); |
| 43 | + w.rtc().set_bit(); |
| 44 | + w.spi0().set_bit(); |
| 45 | + w.spi1().set_bit(); |
| 46 | + w.syscfg().set_bit(); |
| 47 | + w.sysinfo().set_bit(); |
| 48 | + w.tbman().set_bit(); |
| 49 | + w.timer().set_bit(); |
| 50 | + w.uart0().set_bit(); |
| 51 | + w.uart1().set_bit(); |
| 52 | + w.usbctrl().set_bit(); |
| 53 | + w |
| 54 | + }); |
| 55 | + |
| 56 | + // unreset_block_wait(RESETS_RESET_BITS /* 01ff_ffff */ & ~( |
| 57 | + // RESETS_RESET_ADC_BITS | |
| 58 | + // RESETS_RESET_RTC_BITS | |
| 59 | + // RESETS_RESET_SPI0_BITS | |
| 60 | + // RESETS_RESET_SPI1_BITS | |
| 61 | + // RESETS_RESET_UART0_BITS | |
| 62 | + // RESETS_RESET_UART1_BITS | |
| 63 | + // RESETS_RESET_USBCTRL_BITS |
| 64 | + // )); |
| 65 | + |
| 66 | + const RESETS_RESET_BITS: u32 = 0x01ffffff; |
| 67 | + const RESETS_RESET_USBCTRL_BITS: u32 = 0x01000000; |
| 68 | + const RESETS_RESET_UART1_BITS: u32 = 0x00800000; |
| 69 | + const RESETS_RESET_UART0_BITS: u32 = 0x00400000; |
| 70 | + const RESETS_RESET_SPI1_BITS: u32 = 0x00020000; |
| 71 | + const RESETS_RESET_SPI0_BITS: u32 = 0x00010000; |
| 72 | + const RESETS_RESET_RTC_BITS: u32 = 0x00008000; |
| 73 | + const RESETS_RESET_ADC_BITS: u32 = 0x00000001; |
| 74 | + |
| 75 | + const PERIPHERALS_TO_UNRESET: u32 = RESETS_RESET_BITS |
| 76 | + & !(RESETS_RESET_ADC_BITS |
| 77 | + | RESETS_RESET_RTC_BITS |
| 78 | + | RESETS_RESET_SPI0_BITS |
| 79 | + | RESETS_RESET_SPI1_BITS |
| 80 | + | RESETS_RESET_UART0_BITS |
| 81 | + | RESETS_RESET_UART1_BITS |
| 82 | + | RESETS_RESET_USBCTRL_BITS); |
| 83 | + |
| 84 | + p.RESETS.reset.modify(|_r, w| { |
| 85 | + // w.adc().clear_bit(); |
| 86 | + w.busctrl().clear_bit(); |
| 87 | + w.dma().clear_bit(); |
| 88 | + w.i2c0().clear_bit(); |
| 89 | + w.i2c1().clear_bit(); |
| 90 | + w.io_bank0().clear_bit(); |
| 91 | + w.io_qspi().clear_bit(); |
| 92 | + w.jtag().clear_bit(); |
| 93 | + w.pads_bank0().clear_bit(); |
| 94 | + w.pads_qspi().clear_bit(); |
| 95 | + w.pio0().clear_bit(); |
| 96 | + w.pio1().clear_bit(); |
| 97 | + w.pll_sys().clear_bit(); |
| 98 | + w.pll_usb().clear_bit(); |
| 99 | + w.pwm().clear_bit(); |
| 100 | + // w.rtc().clear_bit(); |
| 101 | + // w.spi0().clear_bit(); |
| 102 | + // w.spi1().clear_bit(); |
| 103 | + w.syscfg().clear_bit(); |
| 104 | + w.sysinfo().clear_bit(); |
| 105 | + w.tbman().clear_bit(); |
| 106 | + w.timer().clear_bit(); |
| 107 | + // w.uart0().clear_bit(); |
| 108 | + // w.uart1().clear_bit(); |
| 109 | + // w.usbctrl().clear_bit(); |
| 110 | + w |
| 111 | + }); |
| 112 | + |
| 113 | + while (!p.RESETS.reset_done.read().bits() & PERIPHERALS_TO_UNRESET) != 0 { |
| 114 | + cortex_m::asm::nop(); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#[entry] |
| 119 | +fn main() -> ! { |
| 120 | + let mut p = rp2040_pac::Peripherals::take().unwrap(); |
| 121 | + |
| 122 | + unsafe { |
| 123 | + setup_chip(&mut p); |
| 124 | + } |
| 125 | + |
| 126 | + // Set GPIO25 to be an input (output enable is cleared) |
| 127 | + p.SIO.gpio_oe_clr.write(|w| unsafe { |
| 128 | + w.bits(1 << 25); |
| 129 | + w |
| 130 | + }); |
| 131 | + |
| 132 | + // Set GPIO25 to be an output low (output is cleared) |
| 133 | + p.SIO.gpio_out_clr.write(|w| unsafe { |
| 134 | + w.bits(1 << 25); |
| 135 | + w |
| 136 | + }); |
| 137 | + |
| 138 | + // Configure pin 25 for GPIO |
| 139 | + p.PADS_BANK0.gpio25.write(|w| { |
| 140 | + // Output Disable off |
| 141 | + w.od().clear_bit(); |
| 142 | + // Input Enable on |
| 143 | + w.ie().set_bit(); |
| 144 | + w |
| 145 | + }); |
| 146 | + p.IO_BANK0.gpio25_ctrl.write(|w| { |
| 147 | + // Map pin 25 to SIO |
| 148 | + w.funcsel().sio_25(); |
| 149 | + w |
| 150 | + }); |
| 151 | + |
| 152 | + // Set GPIO25 to be an output (output enable is set) |
| 153 | + p.SIO.gpio_oe_set.write(|w| unsafe { |
| 154 | + w.bits(1 << 25); |
| 155 | + w |
| 156 | + }); |
| 157 | + |
| 158 | + loop { |
| 159 | + for _i in 0..1000000 { |
| 160 | + cortex_m::asm::nop(); |
| 161 | + } |
| 162 | + |
| 163 | + // Set GPIO25 to be low |
| 164 | + p.SIO.gpio_out_clr.write(|w| unsafe { |
| 165 | + w.bits(1 << 25); |
| 166 | + w |
| 167 | + }); |
| 168 | + |
| 169 | + for _i in 0..1000000 { |
| 170 | + cortex_m::asm::nop(); |
| 171 | + } |
| 172 | + |
| 173 | + // Set GPIO25 to be high |
| 174 | + p.SIO.gpio_out_set.write(|w| unsafe { |
| 175 | + w.bits(1 << 25); |
| 176 | + w |
| 177 | + }); |
| 178 | + } |
| 179 | +} |
0 commit comments