aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf/src/ipc.rs
blob: 410783ef4703be28a49bdd420862153dad7c6bf7 (plain)
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
//! InterProcessor Communication (IPC)

#![macro_use]

use core::future::poll_fn;
use core::sync::atomic::{compiler_fence, Ordering};
use core::task::Poll;

use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;

use crate::peripherals::IPC;
use crate::{interrupt, pac};

/// IPC Event
#[derive(Debug, Clone, Copy)]
pub enum IpcEvent {
    /// IPC Event 0
    Event0 = 0,
    /// IPC Event 1
    Event1 = 1,
    /// IPC Event 2
    Event2 = 2,
    /// IPC Event 3
    Event3 = 3,
    /// IPC Event 4
    Event4 = 4,
    /// IPC Event 5
    Event5 = 5,
    /// IPC Event 6
    Event6 = 6,
    /// IPC Event 7
    Event7 = 7,
    /// IPC Event 8
    Event8 = 8,
    /// IPC Event 9
    Event9 = 9,
    /// IPC Event 10
    Event10 = 10,
    /// IPC Event 11
    Event11 = 11,
    /// IPC Event 12
    Event12 = 12,
    /// IPC Event 13
    Event13 = 13,
    /// IPC Event 14
    Event14 = 14,
    /// IPC Event 15
    Event15 = 15,
}

const EVENTS: [IpcEvent; 16] = [
    IpcEvent::Event0,
    IpcEvent::Event1,
    IpcEvent::Event2,
    IpcEvent::Event3,
    IpcEvent::Event4,
    IpcEvent::Event5,
    IpcEvent::Event6,
    IpcEvent::Event7,
    IpcEvent::Event8,
    IpcEvent::Event9,
    IpcEvent::Event10,
    IpcEvent::Event11,
    IpcEvent::Event12,
    IpcEvent::Event13,
    IpcEvent::Event14,
    IpcEvent::Event15,
];

/// IPC Channel
#[derive(Debug, Clone, Copy)]
pub enum IpcChannel {
    /// IPC Channel 0
    Channel0,
    /// IPC Channel 1
    Channel1,
    /// IPC Channel 2
    Channel2,
    /// IPC Channel 3
    Channel3,
    /// IPC Channel 4
    Channel4,
    /// IPC Channel 5
    Channel5,
    /// IPC Channel 6
    Channel6,
    /// IPC Channel 7
    Channel7,
    /// IPC Channel 8
    Channel8,
    /// IPC Channel 9
    Channel9,
    /// IPC Channel 10
    Channel10,
    /// IPC Channel 11
    Channel11,
    /// IPC Channel 12
    Channel12,
    /// IPC Channel 13
    Channel13,
    /// IPC Channel 14
    Channel14,
    /// IPC Channel 15
    Channel15,
}

/// Interrupt Handler
pub struct InterruptHandler {}

impl interrupt::typelevel::Handler<interrupt::typelevel::IPC> for InterruptHandler {
    unsafe fn on_interrupt() {
        let regs = IPC::regs();

        // Check if an event was generated, and if it was, trigger the corresponding waker
        for event in EVENTS {
            if regs.events_receive(event as usize).read() & 0x01 == 0x01 {
                // Event is set. Reset and wake waker
                regs.events_receive(event as usize).write_value(0);
                IPC::state().waker_for(event);
            }

            // Ensure the state is actually cleared
            //  Ref: nRF5340 PS v1.5 7.1.9.1 p.153
            compiler_fence(Ordering::SeqCst);
            while regs.events_receive(event as usize).read() & 0x01 != 0x00 {}
        }
    }
}

/// IPC driver
pub struct Ipc<'d, T: Instance> {
    _peri: PeripheralRef<'d, T>,
}

impl<'d, T: Instance> From<PeripheralRef<'d, T>> for Ipc<'d, T> {
    fn from(value: PeripheralRef<'d, T>) -> Self {
        Self { _peri: value }
    }
}

impl<'d, T: Instance> Ipc<'d, T> {
    /// Create IPC driver
    pub fn new(ipc: impl Peripheral<P = T> + 'd) -> Self {
        into_ref!(ipc);

        Self { _peri: ipc }
    }

    /// Duplicates the peripheral singleton
    ///
    /// # Safety
    ///
    /// Ensure manually that only one peripheral is in use at one time
    pub unsafe fn clone_unchecked(&self) -> Self {
        Self {
            _peri: self._peri.clone_unchecked(),
        }
    }

    /// Configures the sending of events
    ///
    /// Events can be configured to broadcast on one or multiple IPC channels.
    pub fn configure_send_event<I: IntoIterator<Item = IpcChannel>>(&self, ev: IpcEvent, channels: I) {
        let regs = T::regs();

        regs.send_cnf(ev as usize).write(|w| {
            for channel in channels {
                match channel {
                    IpcChannel::Channel0 => w.set_chen0(true),
                    IpcChannel::Channel1 => w.set_chen1(true),
                    IpcChannel::Channel2 => w.set_chen2(true),
                    IpcChannel::Channel3 => w.set_chen3(true),
                    IpcChannel::Channel4 => w.set_chen4(true),
                    IpcChannel::Channel5 => w.set_chen5(true),
                    IpcChannel::Channel6 => w.set_chen6(true),
                    IpcChannel::Channel7 => w.set_chen7(true),
                    IpcChannel::Channel8 => w.set_chen8(true),
                    IpcChannel::Channel9 => w.set_chen9(true),
                    IpcChannel::Channel10 => w.set_chen10(true),
                    IpcChannel::Channel11 => w.set_chen11(true),
                    IpcChannel::Channel12 => w.set_chen12(true),
                    IpcChannel::Channel13 => w.set_chen13(true),
                    IpcChannel::Channel14 => w.set_chen14(true),
                    IpcChannel::Channel15 => w.set_chen15(true),
                }
            }
        })
    }

    /// Configures the receiving of events
    ///
    /// Events can be configured to be received by one or multiple IPC channels.
    pub fn configure_receive_event<I: IntoIterator<Item = IpcChannel>>(&self, ev: IpcEvent, channels: I) {
        let regs = T::regs();

        regs.receive_cnf(ev as usize).write(|w| {
            for channel in channels {
                match channel {
                    IpcChannel::Channel0 => w.set_chen0(true),
                    IpcChannel::Channel1 => w.set_chen1(true),
                    IpcChannel::Channel2 => w.set_chen2(true),
                    IpcChannel::Channel3 => w.set_chen3(true),
                    IpcChannel::Channel4 => w.set_chen4(true),
                    IpcChannel::Channel5 => w.set_chen5(true),
                    IpcChannel::Channel6 => w.set_chen6(true),
                    IpcChannel::Channel7 => w.set_chen7(true),
                    IpcChannel::Channel8 => w.set_chen8(true),
                    IpcChannel::Channel9 => w.set_chen9(true),
                    IpcChannel::Channel10 => w.set_chen10(true),
                    IpcChannel::Channel11 => w.set_chen11(true),
                    IpcChannel::Channel12 => w.set_chen12(true),
                    IpcChannel::Channel13 => w.set_chen13(true),
                    IpcChannel::Channel14 => w.set_chen14(true),
                    IpcChannel::Channel15 => w.set_chen15(true),
                }
            }
        });
    }

    /// Triggers an event
    pub fn trigger_event(&self, ev: IpcEvent) {
        let regs = T::regs();

        regs.tasks_send(ev as usize).write_value(0x01);
    }

    /// Wait for event to be triggered
    pub async fn wait_for_event(&self, ev: IpcEvent) {
        let regs = T::regs();

        // Enable interrupt
        match ev {
            IpcEvent::Event0 => {
                regs.inten().modify(|m| m.set_receive0(true));
            }
            IpcEvent::Event1 => {
                regs.inten().modify(|m| m.set_receive1(true));
            }
            IpcEvent::Event2 => {
                regs.inten().modify(|m| m.set_receive2(true));
            }
            IpcEvent::Event3 => {
                regs.inten().modify(|m| m.set_receive3(true));
            }
            IpcEvent::Event4 => {
                regs.inten().modify(|m| m.set_receive4(true));
            }
            IpcEvent::Event5 => {
                regs.inten().modify(|m| m.set_receive5(true));
            }
            IpcEvent::Event6 => {
                regs.inten().modify(|m| m.set_receive6(true));
            }
            IpcEvent::Event7 => {
                regs.inten().modify(|m| m.set_receive7(true));
            }
            IpcEvent::Event8 => {
                regs.inten().modify(|m| m.set_receive8(true));
            }
            IpcEvent::Event9 => {
                regs.inten().modify(|m| m.set_receive9(true));
            }
            IpcEvent::Event10 => {
                regs.inten().modify(|m| m.set_receive10(true));
            }
            IpcEvent::Event11 => {
                regs.inten().modify(|m| m.set_receive11(true));
            }
            IpcEvent::Event12 => {
                regs.inten().modify(|m| m.set_receive12(true));
            }
            IpcEvent::Event13 => {
                regs.inten().modify(|m| m.set_receive13(true));
            }
            IpcEvent::Event14 => {
                regs.inten().modify(|m| m.set_receive14(true));
            }
            IpcEvent::Event15 => {
                regs.inten().modify(|m| m.set_receive15(true));
            }
        };

        poll_fn(|cx| {
            IPC::state().waker_for(ev).register(cx.waker());

            if regs.events_receive(ev as usize).read() & 0x01 == 0x01 {
                regs.events_receive(ev as usize).write_value(0x00);

                Poll::Ready(())
            } else {
                Poll::Pending
            }
        })
        .await;
    }
}

pub(crate) struct State {
    wakers: [AtomicWaker; 16],
}

impl State {
    pub(crate) const fn new() -> Self {
        const WAKER: AtomicWaker = AtomicWaker::new();

        Self { wakers: [WAKER; 16] }
    }

    const fn waker_for(&self, ev: IpcEvent) -> &AtomicWaker {
        match ev {
            IpcEvent::Event0 => &self.wakers[0],
            IpcEvent::Event1 => &self.wakers[1],
            IpcEvent::Event2 => &self.wakers[2],
            IpcEvent::Event3 => &self.wakers[3],
            IpcEvent::Event4 => &self.wakers[4],
            IpcEvent::Event5 => &self.wakers[5],
            IpcEvent::Event6 => &self.wakers[6],
            IpcEvent::Event7 => &self.wakers[7],
            IpcEvent::Event8 => &self.wakers[8],
            IpcEvent::Event9 => &self.wakers[9],
            IpcEvent::Event10 => &self.wakers[10],
            IpcEvent::Event11 => &self.wakers[11],
            IpcEvent::Event12 => &self.wakers[12],
            IpcEvent::Event13 => &self.wakers[13],
            IpcEvent::Event14 => &self.wakers[14],
            IpcEvent::Event15 => &self.wakers[15],
        }
    }
}

pub(crate) trait SealedInstance {
    fn regs() -> pac::ipc::Ipc;
    fn state() -> &'static State;
}

/// IPC peripheral instance.
#[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static + Send {
    /// Interrupt for this peripheral.
    type Interrupt: interrupt::typelevel::Interrupt;
}

macro_rules! impl_ipc {
    ($type:ident, $pac_type:ident, $irq:ident) => {
        impl crate::ipc::SealedInstance for peripherals::$type {
            fn regs() -> pac::ipc::Ipc {
                pac::$pac_type
            }

            fn state() -> &'static crate::ipc::State {
                static STATE: crate::ipc::State = crate::ipc::State::new();
                &STATE
            }
        }
        impl crate::ipc::Instance for peripherals::$type {
            type Interrupt = crate::interrupt::typelevel::$irq;
        }
    };
}