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 459
/*********************************************************************************************************************** * Copyright (c) 2019 by the authors * * Author: André Borrmann * License: Apache License 2.0 **********************************************************************************************************************/ #![doc(html_root_url = "https://docs.rs/ruspiro-interrupt/0.2.0")] #![no_std] #![feature(asm)] #![feature(linkage)] //! # Raspberry Pi Interrupt handler //! //! This crates provides functions and macros (custom attribute) to conviniently implement interrupt handler for //! Raspberry Pi 3. The possible interrupts a handler can be implemented for are available as enum [irqtypes::Interrupt] //! //! # Usage //! //! ``` //! extern crate ruspiro_interrupt; // <- this kind of usage is VERY IMPORTANT to ensure linking works as expected! //! use ruspiro_interrupt::*; //! //! #[IrqHandler(ArmTimer)] //! fn timer_handler() { //! // TODO: acknowledge the irq //! //! // implement stuff that shall be executed if the interrupt is raised... //! // be careful when this code uses spinlocks as this might lead to dead-locks if the //! // executing code interrupted currently helds a lock the code inside this handler tries to aquire the same one //! println!("timer interrupt raised"); //! } //! //! fn demo() { //! // as we have an interrupt handler defined we need to enable interrupt handling globally as well //! // as the specific interrupt we have a handler implemented for //! IRQ_MANAGER.take_for(|irq_mgr| { //! irq_mgr.enable(); //! irq_mgr.activate(Interrupt.ArmTimer); //! }); //! } //! ``` //! extern crate alloc; extern crate paste; pub use ruspiro_interrupt_macros::*; pub mod irqtypes; pub use irqtypes::*; use ruspiro_singleton::Singleton; mod interface; mod auxhandler; use alloc::vec::*; /// The singleton accessor to the interrupt manager pub static IRQ_MANAGER: Singleton<InterruptManager> = Singleton::new(InterruptManager::new()); /// The interrupt manager representation pub struct InterruptManager { enabled: [u32; 3], // store the current active irq's of the different banks (shadowing the register value) } impl InterruptManager { pub(crate) const fn new () -> Self { InterruptManager { enabled: [0; 3], } } /// One time interrupt manager initialization. This performs the initial configuration and deactivates all IRQs pub fn initialize(&mut self) { interface::initialize(); } /// globally enable interrupts pub fn enable(&self) { interface::enable_i(); interface::enable_f(); } /// globally disable interrupts pub fn disable(&self) { interface::disable_i(); interface::disable_f(); } /// activate a specific interrupt to be raised and handled (id a handler is implemented) /// if there is no handler implemented for this interrupt it may lead to an endless interrupt /// loop as the interrupt never gets acknowledged by the handler. /// The is unfortunately no generic way of acknowledgement implementation possible as the acknowledge /// register and process differs for the individual interrupts. pub fn activate(&mut self, irq: Interrupt) { let irq_num = irq as u32; let irq_bank = irq_num >> 5; interface::activate(irq_bank, irq_num); self.enabled[irq_bank as usize] |= 1 << (irq_num & 0x1F); unsafe{ asm!("dmb") }; } /// deactivate a specific interrupt from beeing raised. This ensures the handler will also not getting called any /// longer pub fn deactivate(&mut self, irq: Interrupt) { let irq_num = irq as u32; let irq_bank = irq_num >> 5; interface::deactivate(irq_bank, irq_num); self.enabled[irq_bank as usize] &= !(1 << (irq_num & 0x1F)); unsafe{ asm!("dmb") }; } } /******************************************************************************************** * Functions that need to be exported, this seem not to work if they are part of of a child * module, so define them here ********************************************************************************************/ /// The IRQ handling entry point. This entrypoint is maintained by the ``rusprio-boot`` crate and points to /// an empty implementation using a linker script "magic". Once interrupts are globally enabled and specific /// interrupts has been activated this entry point will be called wheneever an interrupt occurs. The /// function determines the interrupt source and dispatches the call to the corresponding handler implementations /// done somewhere else #[no_mangle] unsafe fn __interrupt_h(_core: u32) { IRQ_MANAGER.use_weak_for(|mgr| { // check wheter the interrupt in a pending register really has been activiated let pendings: [u32; 3] = interface::get_pending_irqs(); // build a list of interrupt id's based on the bit's set in the 3 irq enable banks let active_irqs: Vec<u8> = (0..).zip(&pendings).fold(Vec::new(), |acc, p| { acc .into_iter() .chain( set_bits_to_vec( (*p.1) & mgr.enabled[p.0 as usize], p.0*32) ) .collect() }); for id in active_irqs { match id { 1 => __irq_handler__SystemTimer1(), 3 => __irq_handler__SystemTimer3(), 8 => __irq_handler__Isp(), 9 => __irq_handler__Usb(), 12 => __irq_handler__CoreSync0(), 13 => __irq_handler__CoreSync1(), 14 => __irq_handler__CoreSync2(), 15 => __irq_handler__CoreSync3(), 29 => auxhandler::aux_handler(),//__irq_handler__Aux(), 30 => __irq_handler__Arm(), 31 => __irq_handler__GpuDma(), 49 => __irq_handler__GpioBank0(), 50 => __irq_handler__GpioBank1(), 51 => __irq_handler__GpioBank2(), 52 => __irq_handler__GpioBank3(), 53 => __irq_handler__I2c(), 54 => __irq_handler__Spi(), 55 => __irq_handler__I2sPcm(), 56 => __irq_handler__Sdio(), 57 => __irq_handler__Pl011(), 64 => __irq_handler__ArmTimer(), 65 => __irq_handler__ArmMailbox(), 66 => __irq_handler__ArmDoorbell0(), 67 => __irq_handler__ArmDoorbell1(), 68 => __irq_handler__ArmGpu0Halted(), 69 => __irq_handler__ArmGpu1Halted(), 70 => __irq_handler__ArmIllegalType1(), 71 => __irq_handler__ArmIllegalType0(), 72 => __irq_handler__ArmPending1(), 73 => __irq_handler__ArmPending2(), _ => __irq_handler_Default() } } }); } fn set_bits_to_vec(value: u32, base: u8) -> Vec<u8> { let mut v: Vec<u8> = Vec::with_capacity(32); let mut check = value; for idx in 0..32 { if check == 0 { break; } if (check & 0x1) != 0 { v.push(idx + base); } check >>= 1; } v } macro_rules! default_handler_impl { ($($name:ident),*) => {$( paste::item!{ #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn [<__irq_handler__ $name>](){ __irq_handler_Default(); } } )*}; } default_handler_impl![ SystemTimer1, SystemTimer3, Isp, Usb, CoreSync0, CoreSync1, CoreSync2, CoreSync3, Aux_Uart1, Aux_Spi1, Aux_Spi2, Arm, GpuDma, GpioBank0, GpioBank1, GpioBank2, GpioBank3, I2c, Spi, I2sPcm, Sdio, Pl011, ArmTimer, ArmMailbox, ArmDoorbell0, ArmDoorbell1, ArmGpu0Halted, ArmGpu1Halted, ArmIllegalType1, ArmIllegalType0, ArmPending1, ArmPending2 ]; #[allow(non_snake_case)] #[no_mangle] fn __irq_handler_Default() { } /* #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__SystemTimer1(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__SystemTimer3(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__Isp(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__Usb(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__CoreSync0(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__CoreSync1(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__CoreSync2(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__CoreSync3(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__Aux(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__Arm(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__GpuDma(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__GpioBank0(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__GpioBank1(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__GpioBank2(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__GpioBank3(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__I2c(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__Spi(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__I2sPcm(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__Sdio(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__Pl011(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__ArmTimer(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__ArmMailbox(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__ArmDoorbell0(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__ArmDoorbell1(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__ArmGpu0Halted(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__ArmGpu1Halted(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__ArmIllegalType1(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__ArmIllegalType0(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__ArmPending1(){ __irq_handler_Default(); } #[allow(non_snake_case)] #[linkage="weak"] #[no_mangle] extern "C" fn __irq_handler__ArmPending2(){ __irq_handler_Default(); } */