|
| 1 | +//! Board file for Imix bootloader. |
| 2 | +
|
| 3 | +#![no_std] |
| 4 | +#![no_main] |
| 5 | +#![feature(asm, const_fn, lang_items, panic_implementation)] |
| 6 | + |
| 7 | +extern crate bootloader; |
| 8 | +extern crate cortexm4; |
| 9 | +#[macro_use(create_capability, static_init)] |
| 10 | +extern crate kernel; |
| 11 | +extern crate capsules; |
| 12 | +extern crate sam4l; |
| 13 | + |
| 14 | +use core::panic::PanicInfo; |
| 15 | + |
| 16 | +use kernel::capabilities; |
| 17 | +use kernel::hil; |
| 18 | +use kernel::hil::Controller; |
| 19 | +use kernel::Platform; |
| 20 | + |
| 21 | +include!(concat!(env!("OUT_DIR"), "/attributes.rs")); |
| 22 | + |
| 23 | +// No processes are supported. |
| 24 | +static mut PROCESSES: [Option<&'static kernel::procs::ProcessType>; 0] = []; |
| 25 | + |
| 26 | +/// Dummy buffer that causes the linker to reserve enough space for the stack. |
| 27 | +#[no_mangle] |
| 28 | +#[link_section = ".stack_buffer"] |
| 29 | +pub static mut STACK_MEMORY: [u8; 0x2000] = [0; 0x2000]; |
| 30 | + |
| 31 | +struct ImixBootloader { |
| 32 | + bootloader: &'static bootloader::bootloader::Bootloader< |
| 33 | + 'static, |
| 34 | + sam4l::usart::USART, |
| 35 | + sam4l::flashcalw::FLASHCALW, |
| 36 | + sam4l::gpio::GPIOPin, |
| 37 | + >, |
| 38 | +} |
| 39 | + |
| 40 | +impl Platform for ImixBootloader { |
| 41 | + fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R |
| 42 | + where |
| 43 | + F: FnOnce(Option<&kernel::Driver>) -> R, |
| 44 | + { |
| 45 | + // Bootloader does not support apps. |
| 46 | + match driver_num { |
| 47 | + _ => f(None), |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +unsafe fn set_pin_primary_functions() { |
| 53 | + use sam4l::gpio::PeripheralFunction::{A, B, C, E}; |
| 54 | + use sam4l::gpio::{PA, PB, PC}; |
| 55 | + |
| 56 | + // Right column: Imix pin name |
| 57 | + // Left column: SAM4L peripheral function |
| 58 | + PA[04].configure(Some(A)); // AD0 -- ADCIFE AD0 |
| 59 | + PA[05].configure(Some(A)); // AD1 -- ADCIFE AD1 |
| 60 | + PA[06].configure(Some(C)); // EXTINT1 -- EIC EXTINT1 |
| 61 | + PA[07].configure(Some(A)); // AD1 -- ADCIFE AD2 |
| 62 | + PA[08].configure(None); //... RF233 IRQ -- GPIO pin |
| 63 | + PA[09].configure(None); //... RF233 RST -- GPIO pin |
| 64 | + PA[10].configure(None); //... RF233 SLP -- GPIO pin |
| 65 | + PA[13].configure(None); //... TRNG EN -- GPIO pin |
| 66 | + PA[14].configure(None); //... TRNG_OUT -- GPIO pin |
| 67 | + PA[17].configure(None); //... NRF INT -- GPIO pin |
| 68 | + PA[18].configure(Some(A)); // NRF CLK -- USART2_CLK |
| 69 | + PA[20].configure(None); //... D8 -- GPIO pin |
| 70 | + PA[21].configure(Some(E)); // TWI2 SDA -- TWIM2_SDA |
| 71 | + PA[22].configure(Some(E)); // TWI2 SCL -- TWIM2 TWCK |
| 72 | + PA[25].configure(Some(A)); // USB_N -- USB DM |
| 73 | + PA[26].configure(Some(A)); // USB_P -- USB DP |
| 74 | + PB[00].configure(Some(A)); // TWI1_SDA -- TWIMS1 TWD |
| 75 | + PB[01].configure(Some(A)); // TWI1_SCL -- TWIMS1 TWCK |
| 76 | + PB[02].configure(Some(A)); // AD3 -- ADCIFE AD3 |
| 77 | + PB[03].configure(Some(A)); // AD4 -- ADCIFE AD4 |
| 78 | + PB[04].configure(Some(A)); // AD5 -- ADCIFE AD5 |
| 79 | + PB[05].configure(Some(A)); // VHIGHSAMPLE -- ADCIFE AD6 |
| 80 | + PB[06].configure(None); //... RTS3 -- USART3 RTS !! FTDI DTR BOOTLOADER SELECT |
| 81 | + PB[07].configure(None); //... NRF RESET -- GPIO |
| 82 | + PB[09].configure(Some(A)); // RX3 -- USART3 RX |
| 83 | + PB[10].configure(Some(A)); // TX3 -- USART3 TX |
| 84 | + PB[11].configure(Some(A)); // CTS0 -- USART0 CTS |
| 85 | + PB[12].configure(Some(A)); // RTS0 -- USART0 RTS |
| 86 | + PB[13].configure(Some(A)); // CLK0 -- USART0 CLK |
| 87 | + PB[14].configure(Some(A)); // RX0 -- USART0 RX |
| 88 | + PB[15].configure(Some(A)); // TX0 -- USART0 TX |
| 89 | + PC[00].configure(Some(A)); // CS2 -- SPI NPCS2 |
| 90 | + PC[01].configure(Some(A)); // CS3 (RF233) -- SPI NPCS3 |
| 91 | + PC[02].configure(Some(A)); // CS1 -- SPI NPCS1 |
| 92 | + PC[03].configure(Some(A)); // CS0 -- SPI NPCS0 |
| 93 | + PC[04].configure(Some(A)); // MISO -- SPI MISO |
| 94 | + PC[05].configure(Some(A)); // MOSI -- SPI MOSI |
| 95 | + PC[06].configure(Some(A)); // SCK -- SPI CLK |
| 96 | + PC[07].configure(Some(B)); // RTS2 (BLE) -- USART2_RTS |
| 97 | + PC[08].configure(Some(E)); // CTS2 (BLE) -- USART2_CTS |
| 98 | + //PC[09].configure(None); //... NRF GPIO -- GPIO |
| 99 | + //PC[10].configure(None); //... USER LED -- GPIO |
| 100 | + PC[09].configure(Some(E)); // ACAN1 -- ACIFC comparator |
| 101 | + PC[10].configure(Some(E)); // ACAP1 -- ACIFC comparator |
| 102 | + PC[11].configure(Some(B)); // RX2 (BLE) -- USART2_RX |
| 103 | + PC[12].configure(Some(B)); // TX2 (BLE) -- USART2_TX |
| 104 | + //PC[13].configure(None); //... ACC_INT1 -- GPIO |
| 105 | + //PC[14].configure(None); //... ACC_INT2 -- GPIO |
| 106 | + PC[13].configure(Some(E)); //... ACBN1 -- ACIFC comparator |
| 107 | + PC[14].configure(Some(E)); //... ACBP1 -- ACIFC comparator |
| 108 | + PC[16].configure(None); //... SENSE_PWR -- GPIO pin |
| 109 | + PC[17].configure(None); //... NRF_PWR -- GPIO pin |
| 110 | + PC[18].configure(None); //... RF233_PWR -- GPIO pin |
| 111 | + PC[19].configure(None); //... TRNG_PWR -- GPIO Pin |
| 112 | + PC[22].configure(None); //... KERNEL LED -- GPIO Pin |
| 113 | + PC[24].configure(None); //... USER_BTN -- GPIO Pin |
| 114 | + PC[25].configure(Some(B)); // LI_INT -- EIC EXTINT2 |
| 115 | + PC[26].configure(None); //... D7 -- GPIO Pin |
| 116 | + PC[27].configure(None); //... D6 -- GPIO Pin |
| 117 | + PC[28].configure(None); //... D5 -- GPIO Pin |
| 118 | + PC[29].configure(None); //... D4 -- GPIO Pin |
| 119 | + PC[30].configure(None); //... D3 -- GPIO Pin |
| 120 | + PC[31].configure(None); //... D2 -- GPIO Pin |
| 121 | +} |
| 122 | + |
| 123 | +#[no_mangle] |
| 124 | +pub unsafe fn reset_handler() { |
| 125 | + sam4l::init(); |
| 126 | + |
| 127 | + sam4l::pm::PM.setup_system_clock(sam4l::pm::SystemClockSource::PllExternalOscillatorAt48MHz { |
| 128 | + frequency: sam4l::pm::OscillatorFrequency::Frequency16MHz, |
| 129 | + startup_mode: sam4l::pm::OscillatorStartup::FastStart, |
| 130 | + }); |
| 131 | + |
| 132 | + // Source 32Khz and 1Khz clocks from RC23K (SAM4L Datasheet 11.6.8) |
| 133 | + sam4l::bpm::set_ck32source(sam4l::bpm::CK32Source::RC32K); |
| 134 | + |
| 135 | + set_pin_primary_functions(); |
| 136 | + |
| 137 | + // Create main kernel object. This contains the main loop function. |
| 138 | + let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&PROCESSES)); |
| 139 | + |
| 140 | + // Initialize USART3 for Uart |
| 141 | + sam4l::usart::USART3.set_mode(sam4l::usart::UsartMode::Uart); |
| 142 | + |
| 143 | + pub static mut PAGEBUFFER: sam4l::flashcalw::Sam4lPage = sam4l::flashcalw::Sam4lPage::new(); |
| 144 | + |
| 145 | + sam4l::flashcalw::FLASH_CONTROLLER.configure(); |
| 146 | + let bootloader = static_init!( |
| 147 | + bootloader::bootloader::Bootloader< |
| 148 | + 'static, |
| 149 | + sam4l::usart::USART, |
| 150 | + sam4l::flashcalw::FLASHCALW, |
| 151 | + sam4l::gpio::GPIOPin, |
| 152 | + >, |
| 153 | + bootloader::bootloader::Bootloader::new( |
| 154 | + &sam4l::usart::USART3, |
| 155 | + &mut sam4l::flashcalw::FLASH_CONTROLLER, |
| 156 | + &sam4l::gpio::PB[06], |
| 157 | + &mut PAGEBUFFER, |
| 158 | + &mut bootloader::bootloader::BUF |
| 159 | + ) |
| 160 | + ); |
| 161 | + hil::uart::UART::set_client(&sam4l::usart::USART3, bootloader); |
| 162 | + hil::flash::HasClient::set_client(&sam4l::flashcalw::FLASH_CONTROLLER, bootloader); |
| 163 | + |
| 164 | + let imix = ImixBootloader { |
| 165 | + bootloader: bootloader, |
| 166 | + }; |
| 167 | + |
| 168 | + let chip = static_init!(sam4l::chip::Sam4l, sam4l::chip::Sam4l::new()); |
| 169 | + |
| 170 | + imix.bootloader.initialize(); |
| 171 | + |
| 172 | + let main_loop_capability = create_capability!(capabilities::MainLoopCapability); |
| 173 | + board_kernel.kernel_loop(&imix, chip, None, &main_loop_capability); |
| 174 | +} |
| 175 | + |
| 176 | +/// Panic handler. |
| 177 | +#[cfg(not(test))] |
| 178 | +#[no_mangle] |
| 179 | +#[panic_implementation] |
| 180 | +pub unsafe extern "C" fn panic_fmt(_pi: &PanicInfo) -> ! { |
| 181 | + loop {} |
| 182 | +} |
0 commit comments