|
| 1 | +#[cfg(feature = "serde")] |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | + |
| 4 | +/// The possible local timestamp options. |
| 5 | +#[derive(Debug, Eq, PartialEq, Copy, Clone)] |
| 6 | +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 7 | +pub enum LocalTimestampOptions { |
| 8 | + /// Disable local timestamps. |
| 9 | + Disabled, |
| 10 | + /// Enable local timestamps and use no prescaling. |
| 11 | + Enabled, |
| 12 | + /// Enable local timestamps and set the prescaler to divide the |
| 13 | + /// reference clock by 4. |
| 14 | + EnabledDiv4, |
| 15 | + /// Enable local timestamps and set the prescaler to divide the |
| 16 | + /// reference clock by 16. |
| 17 | + EnabledDiv16, |
| 18 | + /// Enable local timestamps and set the prescaler to divide the |
| 19 | + /// reference clock by 64. |
| 20 | + EnabledDiv64, |
| 21 | +} |
| 22 | + |
| 23 | +/// Active exception number |
| 24 | +#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Hash)] |
| 25 | +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 26 | +pub enum VectActive { |
| 27 | + /// Thread mode |
| 28 | + ThreadMode, |
| 29 | + |
| 30 | + /// Processor core exception (internal interrupts) |
| 31 | + Exception(Exception), |
| 32 | + |
| 33 | + /// Device specific exception (external interrupts) |
| 34 | + Interrupt { |
| 35 | + /// Interrupt number. This number is always within half open range `[0, 512)` (9 bit) |
| 36 | + irqn: u16, |
| 37 | + }, |
| 38 | +} |
| 39 | + |
| 40 | +impl VectActive { |
| 41 | + /// Converts a vector number into `VectActive` |
| 42 | + #[inline] |
| 43 | + pub fn from(vect_active: u16) -> Option<Self> { |
| 44 | + Some(match vect_active { |
| 45 | + 0 => VectActive::ThreadMode, |
| 46 | + 2 => VectActive::Exception(Exception::NonMaskableInt), |
| 47 | + 3 => VectActive::Exception(Exception::HardFault), |
| 48 | + 4 => VectActive::Exception(Exception::MemoryManagement), |
| 49 | + 5 => VectActive::Exception(Exception::BusFault), |
| 50 | + 6 => VectActive::Exception(Exception::UsageFault), |
| 51 | + 7 => VectActive::Exception(Exception::SecureFault), |
| 52 | + 11 => VectActive::Exception(Exception::SVCall), |
| 53 | + 12 => VectActive::Exception(Exception::DebugMonitor), |
| 54 | + 14 => VectActive::Exception(Exception::PendSV), |
| 55 | + 15 => VectActive::Exception(Exception::SysTick), |
| 56 | + irqn if (16..512).contains(&irqn) => VectActive::Interrupt { irqn: irqn - 16 }, |
| 57 | + _ => return None, |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/// Processor core exceptions (internal interrupts) |
| 63 | +#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Hash)] |
| 64 | +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 65 | +pub enum Exception { |
| 66 | + /// Non maskable interrupt |
| 67 | + NonMaskableInt, |
| 68 | + |
| 69 | + /// Hard fault interrupt |
| 70 | + HardFault, |
| 71 | + |
| 72 | + /// Memory management interrupt (not present on Cortex-M0 variants) |
| 73 | + MemoryManagement, |
| 74 | + |
| 75 | + /// Bus fault interrupt (not present on Cortex-M0 variants) |
| 76 | + BusFault, |
| 77 | + |
| 78 | + /// Usage fault interrupt (not present on Cortex-M0 variants) |
| 79 | + UsageFault, |
| 80 | + |
| 81 | + /// Secure fault interrupt (only on ARMv8-M) |
| 82 | + SecureFault, |
| 83 | + |
| 84 | + /// SV call interrupt |
| 85 | + SVCall, |
| 86 | + |
| 87 | + /// Debug monitor interrupt (not present on Cortex-M0 variants) |
| 88 | + DebugMonitor, |
| 89 | + |
| 90 | + /// Pend SV interrupt |
| 91 | + PendSV, |
| 92 | + |
| 93 | + /// System Tick interrupt |
| 94 | + SysTick, |
| 95 | +} |
| 96 | + |
| 97 | +impl Exception { |
| 98 | + /// Returns the IRQ number of this `Exception` |
| 99 | + /// |
| 100 | + /// The return value is always within the closed range `[-1, -14]` |
| 101 | + #[inline] |
| 102 | + pub fn irqn(self) -> i8 { |
| 103 | + match self { |
| 104 | + Exception::NonMaskableInt => -14, |
| 105 | + Exception::HardFault => -13, |
| 106 | + Exception::MemoryManagement => -12, |
| 107 | + Exception::BusFault => -11, |
| 108 | + Exception::UsageFault => -10, |
| 109 | + Exception::SecureFault => -9, |
| 110 | + Exception::SVCall => -5, |
| 111 | + Exception::DebugMonitor => -4, |
| 112 | + Exception::PendSV => -2, |
| 113 | + Exception::SysTick => -1, |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments