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
//! Implementation of `interrupture` for `stm32f7x6`

#![no_std]
#![warn(missing_docs)]
#![deny(clippy::all)]

use interrupture::handle_isr;
use core::convert::TryFrom;
use cortex_m_rt::exception;

pub use stm32f7::stm32f7x6::Interrupt as InterruptRequest;
use stm32f7::stm32f7x6::{NVIC, NVIC_STIR};
use interrupture::Nr;

/// A convenience wrapper around `interrupture::scope` for `stm32f7x6`
pub fn scope<'a, F, C, R>(
    nvic: &'a mut NVIC,
    nvic_stir: &'a mut NVIC_STIR,
    default_handler: F,
    code: C,
) -> R
where
    F: FnMut(u8) + Send,
    C: FnOnce(&mut interrupture::InterruptTable<'a, Ic<'a>>) -> R,
{
    let ic = Ic { nvic, nvic_stir };
    interrupture::scope(ic, default_handler, code)
}

#[doc(hidden)]
/// This type only exists for the `InterruptController` trait bound on closure
/// of `scope`, do not use directly, you will never interact with it directly anyway.
pub struct Ic<'a> {
    nvic: &'a mut NVIC,
    nvic_stir: &'a mut NVIC_STIR,
}

// HACK: Nr should be more convenient to use (e.g. have some forwarding impls)
struct NrWrap<'a, T: Nr>(&'a T);
unsafe impl<'a, T: Nr> Nr for NrWrap<'a, T> {
    fn nr(&self) -> u8 {
        self.0.nr()
    }
}

impl<'a> interrupture::InterruptController for Ic<'a> {
    type Request = InterruptRequest;
    type Priority = Priority;
    fn trigger(&mut self, irq: &Self::Request) {
        self.nvic_stir
            .stir
            .write(|w| unsafe { w.intid().bits(irq.nr().into()) });
    }
    fn is_pending(irq: &Self::Request) -> bool {
        NVIC::is_pending(NrWrap(irq))
    }
    fn pend(irq: &Self::Request) {
        NVIC::pend(NrWrap(irq));
    }
    fn unpend(irq: &Self::Request) {
        NVIC::unpend(NrWrap(irq));
    }
    fn get_priority(irq: &Self::Request) -> Self::Priority {
        let res = NVIC::get_priority(NrWrap(irq));

        // STM32F7 only uses 4 bits for Priority. priority << 4, because the upper 4 bits are used
        // for priority.
        match Priority::from_u8(res >> 4) {
            Ok(priority) => priority,
            Err(PriorityDoesNotExistError(prio_number)) => {
                unreachable!("Priority {} does not exist", prio_number)
            }
        }
    }
    fn set_priority(&mut self, irq: &Self::Request, priority: Self::Priority) {
        // The STM32F7 only supports 16 priority levels
        // Assert that priority < 16
        // STM32F7 only uses 4 bits for Priority. priority << 4, because the upper 4 bits are used
        // for priority.
        let priority = (priority as u8) << 4;

        unsafe { self.nvic.set_priority(NrWrap(irq), priority) };
    }
    fn disable(&mut self, irq: &Self::Request) {
        self.nvic.disable(NrWrap(irq));
    }
    fn enable(&mut self, irq: &Self::Request) {
        self.nvic.enable(NrWrap(irq));
    }
}

/// The default interrupt handler that is called for all uncaught IRQs.
#[exception]
fn DefaultHandler(irqn: i16) {
    if let Ok(irqn) = u8::try_from(irqn) {
        handle_isr(irqn)
    } else {
        panic!("Unhandled exception (IRQn = {})", irqn);
    }
}

/// Possible interrupt priorities of the stm32f7.
///
/// Lower number means higher priority:
/// `P1` has a higher priority than e.g. `P2`, `P5`, ...
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum Priority {
    /// Priority 0
    P0 = 0,
    /// Priority 1
    P1,
    /// Priority 2
    P2,
    /// Priority 3
    P3,
    /// Priority 4
    P4,
    /// Priority 5
    P5,
    /// Priority 6
    P6,
    /// Priority 7
    P7,
    /// Priority 8
    P8,
    /// Priority 9
    P9,
    /// Priority 10
    P10,
    /// Priority 11
    P11,
    /// Priority 12
    P12,
    /// Priority 13
    P13,
    /// Priority 14
    P14,
    /// Priority 15
    P15,
}
struct PriorityDoesNotExistError(u8);

impl Priority {
    /// Converts a u8 to a Priority.
    ///
    /// Returns an `Err` when no variant with the given `priority` exists.
    // use FromPrimitive?
    fn from_u8(priority: u8) -> Result<Priority, PriorityDoesNotExistError> {
        use self::Priority::*;
        match priority {
            0 => Ok(P0),
            1 => Ok(P1),
            2 => Ok(P2),
            3 => Ok(P3),
            4 => Ok(P4),
            5 => Ok(P5),
            6 => Ok(P6),
            7 => Ok(P7),
            8 => Ok(P8),
            9 => Ok(P9),
            10 => Ok(P10),
            11 => Ok(P11),
            12 => Ok(P12),
            13 => Ok(P13),
            14 => Ok(P14),
            15 => Ok(P15),
            _ => Err(PriorityDoesNotExistError(priority)),
        }
    }
}