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
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright 2020 Branan Riley <me@branan.info>

//! IO functionality specific to the Teensy 3.5 board

use crate::{
    hw::{
        board::teensy_common::io::{Serial, SerialError},
        mcu::kinetis::{
            mk64fx512::{Pin, Sim, Uart, UartRx, UartTx},
            Mk64Fx512,
        },
    },
    io,
    sync::{Mutex, MutexGuard},
    task::WakerSet,
};
use core::{ptr::write_volatile, sync::atomic::Ordering};

/// The pin used to recieve for serial 1
pub type Serial1Rx = UartRx<Pin<'static, 1, 16>>;

/// The pin used to transmit for serial 1
pub type Serial1Tx = UartTx<Pin<'static, 1, 17>>;

/// The pin used to recieve for serial 2
pub type Serial2Rx = UartRx<Pin<'static, 2, 3>>;

/// The pin used to transmit for serial 2
pub type Serial2Tx = UartTx<Pin<'static, 2, 4>>;

/// The pin used to recieve for serial 3
pub type Serial3Rx = UartRx<Pin<'static, 3, 2>>;

/// The pin used to transmit for serial 3
pub type Serial3Tx = UartTx<Pin<'static, 3, 3>>;

/// The pin used to recieve for serial 4
pub type Serial4Rx = UartRx<Pin<'static, 1, 10>>;

/// The pin used to transmit for serial 4
pub type Serial4Tx = UartTx<Pin<'static, 1, 11>>;

/// The pin used to recieve for serial 5
pub type Serial5Rx = UartRx<Pin<'static, 4, 25>>;

/// The pin used to transmit for serial 5
pub type Serial5Tx = UartTx<Pin<'static, 4, 24>>;

/// The pin used to recieve for serial 6
pub type Serial6Rx = UartRx<Pin<'static, 3, 9>>;

/// The pin used to transmit for serial 6
pub type Serial6Tx = UartTx<Pin<'static, 3, 8>>;

impl io::Serial for Serial<Mk64Fx512, Serial1Tx, Serial1Rx, 0> {
    type Error = SerialError;

    fn enable(&mut self, baud: usize) -> Result<(), <Self as io::Serial>::Error> {
        let divisor = (super::CPU_FREQ.load(Ordering::Relaxed) as usize * 32) / (baud * 16);
        let mut uart = Sim::get()
            .ok_or(SerialError::SimInUse)?
            .enable_peripheral::<Uart<(), (), 0>>()
            .ok_or(SerialError::UartInUse)?;
        uart.set_divisor(divisor);
        uart.enable_tx_fifo(8, 7);
        uart.enable_rx_fifo(8, 1);

        let tx = super::digital::port_b()
            .ok_or(SerialError::PortInUse)?
            .pin::<17>()
            .ok_or(SerialError::PinInUse)?
            .into_uart_tx();
        let rx = super::digital::port_b()
            .ok_or(SerialError::PortInUse)?
            .pin::<16>()
            .ok_or(SerialError::PinInUse)?
            .into_uart_rx();
        let uart = uart.enable_tx(tx).enable_rx(rx);
        self.0 = Some(uart);
        self.1 = Some(&SERIAL_1_WAKERS);
        Ok(())
    }

    fn disable(&mut self) -> Result<(), <Self as io::Serial>::Error> {
        self.0 = None;
        self.1 = None;
        Ok(())
    }
}

impl io::Serial for Serial<Mk64Fx512, Serial2Tx, Serial2Rx, 1> {
    type Error = SerialError;

    fn enable(&mut self, baud: usize) -> Result<(), <Self as io::Serial>::Error> {
        let divisor = (super::CPU_FREQ.load(Ordering::Relaxed) as usize * 32) / (baud * 16);
        let mut uart = Sim::get()
            .ok_or(SerialError::SimInUse)?
            .enable_peripheral::<Uart<(), (), 1>>()
            .ok_or(SerialError::UartInUse)?;
        uart.set_divisor(divisor);
        uart.enable_tx_fifo(8, 7);
        uart.enable_rx_fifo(8, 1);

        let tx = super::digital::port_c()
            .ok_or(SerialError::PortInUse)?
            .pin::<4>()
            .ok_or(SerialError::PinInUse)?
            .into_uart_tx();
        let rx = super::digital::port_c()
            .ok_or(SerialError::PortInUse)?
            .pin::<3>()
            .ok_or(SerialError::PinInUse)?
            .into_uart_rx();
        let uart = uart.enable_tx(tx).enable_rx(rx);
        self.0 = Some(uart);
        self.1 = Some(&SERIAL_2_WAKERS);
        Ok(())
    }

    fn disable(&mut self) -> Result<(), <Self as io::Serial>::Error> {
        self.0 = None;
        self.1 = None;
        Ok(())
    }
}

impl io::Serial for Serial<Mk64Fx512, Serial3Tx, Serial3Rx, 2> {
    type Error = SerialError;

    fn enable(&mut self, baud: usize) -> Result<(), <Self as io::Serial>::Error> {
        let divisor = (super::BUS_FREQ.load(Ordering::Relaxed) as usize * 32) / (baud * 16);
        let mut uart = Sim::get()
            .ok_or(SerialError::SimInUse)?
            .enable_peripheral::<Uart<(), (), 2>>()
            .ok_or(SerialError::UartInUse)?;
        uart.set_divisor(divisor);
        uart.enable_tx_fifo(1, 0);
        uart.enable_rx_fifo(1, 1);

        let tx = super::digital::port_d()
            .ok_or(SerialError::PortInUse)?
            .pin::<3>()
            .ok_or(SerialError::PinInUse)?
            .into_uart_tx();
        let rx = super::digital::port_d()
            .ok_or(SerialError::PortInUse)?
            .pin::<2>()
            .ok_or(SerialError::PinInUse)?
            .into_uart_rx();
        let uart = uart.enable_tx(tx).enable_rx(rx);
        self.0 = Some(uart);
        self.1 = Some(&SERIAL_3_WAKERS);
        Ok(())
    }

    fn disable(&mut self) -> Result<(), <Self as io::Serial>::Error> {
        self.0 = None;
        self.1 = None;
        Ok(())
    }
}

impl io::Serial for Serial<Mk64Fx512, Serial4Tx, Serial4Rx, 3> {
    type Error = SerialError;

    fn enable(&mut self, baud: usize) -> Result<(), <Self as io::Serial>::Error> {
        let divisor = (super::BUS_FREQ.load(Ordering::Relaxed) as usize * 32) / (baud * 16);
        let mut uart = Sim::get()
            .ok_or(SerialError::SimInUse)?
            .enable_peripheral::<Uart<(), (), 3>>()
            .ok_or(SerialError::UartInUse)?;
        uart.set_divisor(divisor);
        uart.enable_tx_fifo(1, 0);
        uart.enable_rx_fifo(1, 1);

        let tx = super::digital::port_b()
            .ok_or(SerialError::PortInUse)?
            .pin::<11>()
            .ok_or(SerialError::PinInUse)?
            .into_uart_tx();
        let rx = super::digital::port_b()
            .ok_or(SerialError::PortInUse)?
            .pin::<10>()
            .ok_or(SerialError::PinInUse)?
            .into_uart_rx();
        let uart = uart.enable_tx(tx).enable_rx(rx);
        self.0 = Some(uart);
        self.1 = Some(&SERIAL_4_WAKERS);
        Ok(())
    }

    fn disable(&mut self) -> Result<(), <Self as io::Serial>::Error> {
        self.0 = None;
        self.1 = None;
        Ok(())
    }
}

impl io::Serial for Serial<Mk64Fx512, Serial5Tx, Serial5Rx, 4> {
    type Error = SerialError;

    fn enable(&mut self, baud: usize) -> Result<(), <Self as io::Serial>::Error> {
        let divisor = (super::BUS_FREQ.load(Ordering::Relaxed) as usize * 32) / (baud * 16);
        let mut uart = Sim::get()
            .ok_or(SerialError::SimInUse)?
            .enable_peripheral::<Uart<(), (), 4>>()
            .ok_or(SerialError::UartInUse)?;
        uart.set_divisor(divisor);
        uart.enable_tx_fifo(1, 0);
        uart.enable_rx_fifo(1, 1);

        let tx = super::digital::port_e()
            .ok_or(SerialError::PortInUse)?
            .pin::<24>()
            .ok_or(SerialError::PinInUse)?
            .into_uart_tx();
        let rx = super::digital::port_e()
            .ok_or(SerialError::PortInUse)?
            .pin::<25>()
            .ok_or(SerialError::PinInUse)?
            .into_uart_rx();
        let uart = uart.enable_tx(tx).enable_rx(rx);
        self.0 = Some(uart);
        self.1 = Some(&SERIAL_5_WAKERS);
        Ok(())
    }

    fn disable(&mut self) -> Result<(), <Self as io::Serial>::Error> {
        self.0 = None;
        self.1 = None;
        Ok(())
    }
}

impl io::Serial for Serial<Mk64Fx512, Serial6Tx, Serial6Rx, 5> {
    type Error = SerialError;

    fn enable(&mut self, baud: usize) -> Result<(), <Self as io::Serial>::Error> {
        let divisor = (super::BUS_FREQ.load(Ordering::Relaxed) as usize * 32) / (baud * 16);
        let mut uart = Sim::get()
            .ok_or(SerialError::SimInUse)?
            .enable_peripheral::<Uart<(), (), 5>>()
            .ok_or(SerialError::UartInUse)?;
        uart.set_divisor(divisor);
        uart.enable_tx_fifo(1, 0);
        uart.enable_rx_fifo(1, 1);

        let tx = super::digital::port_d()
            .ok_or(SerialError::PortInUse)?
            .pin::<8>()
            .ok_or(SerialError::PinInUse)?
            .into_uart_tx();
        let rx = super::digital::port_d()
            .ok_or(SerialError::PortInUse)?
            .pin::<9>()
            .ok_or(SerialError::PinInUse)?
            .into_uart_rx();
        let uart = uart.enable_tx(tx).enable_rx(rx);
        self.0 = Some(uart);
        self.1 = Some(&SERIAL_6_WAKERS);
        Ok(())
    }

    fn disable(&mut self) -> Result<(), <Self as io::Serial>::Error> {
        self.0 = None;
        self.1 = None;
        Ok(())
    }
}

/// The first hardware serial port
pub fn serial_1() -> MutexGuard<'static, Serial<Mk64Fx512, Serial1Tx, Serial1Rx, 0>> {
    static SERIAL: Mutex<Serial<Mk64Fx512, Serial1Tx, Serial1Rx, 0>> = Mutex::new(Serial::new());
    SERIAL.lock()
}

/// The second hardware serial port
pub fn serial_2() -> MutexGuard<'static, Serial<Mk64Fx512, Serial2Tx, Serial2Rx, 1>> {
    static SERIAL: Mutex<Serial<Mk64Fx512, Serial2Tx, Serial2Rx, 1>> = Mutex::new(Serial::new());
    SERIAL.lock()
}

/// The third hardware serial port
pub fn serial_3() -> MutexGuard<'static, Serial<Mk64Fx512, Serial3Tx, Serial3Rx, 2>> {
    static SERIAL: Mutex<Serial<Mk64Fx512, Serial3Tx, Serial3Rx, 2>> = Mutex::new(Serial::new());
    SERIAL.lock()
}

/// The fourth hardware serial port
pub fn serial_4() -> MutexGuard<'static, Serial<Mk64Fx512, Serial4Tx, Serial4Rx, 3>> {
    static SERIAL: Mutex<Serial<Mk64Fx512, Serial4Tx, Serial4Rx, 3>> = Mutex::new(Serial::new());
    SERIAL.lock()
}

/// The fifth hardware serial port
pub fn serial_5() -> MutexGuard<'static, Serial<Mk64Fx512, Serial5Tx, Serial5Rx, 4>> {
    static SERIAL: Mutex<Serial<Mk64Fx512, Serial5Tx, Serial5Rx, 4>> = Mutex::new(Serial::new());
    SERIAL.lock()
}

/// The sixth hardware serial port
pub fn serial_6() -> MutexGuard<'static, Serial<Mk64Fx512, Serial6Tx, Serial6Rx, 5>> {
    static SERIAL: Mutex<Serial<Mk64Fx512, Serial6Tx, Serial6Rx, 5>> = Mutex::new(Serial::new());
    SERIAL.lock()
}

static SERIAL_1_WAKERS: WakerSet = WakerSet::new();
static SERIAL_2_WAKERS: WakerSet = WakerSet::new();
static SERIAL_3_WAKERS: WakerSet = WakerSet::new();
static SERIAL_4_WAKERS: WakerSet = WakerSet::new();
static SERIAL_5_WAKERS: WakerSet = WakerSet::new();
static SERIAL_6_WAKERS: WakerSet = WakerSet::new();

/// The interrupt function for serial 1
pub extern "C" fn serial_1_intr() {
    unsafe {
        const UART_TX_INTR: *mut u8 = bitband_address(0x4006_A003, 7);
        const UART_TC_INTR: *mut u8 = bitband_address(0x4006_A003, 6);
        const UART_RX_INTR: *mut u8 = bitband_address(0x4006_A003, 5);
        write_volatile(UART_TX_INTR, 0);
        write_volatile(UART_TC_INTR, 0);
        write_volatile(UART_RX_INTR, 0);
        SERIAL_1_WAKERS.wake();
    }
}

/// The interrupt function for serial 2
pub extern "C" fn serial_2_intr() {
    unsafe {
        const UART_TX_INTR: *mut u8 = bitband_address(0x4006_B003, 7);
        const UART_TC_INTR: *mut u8 = bitband_address(0x4006_B003, 6);
        const UART_RX_INTR: *mut u8 = bitband_address(0x4006_B003, 5);
        write_volatile(UART_TX_INTR, 0);
        write_volatile(UART_TC_INTR, 0);
        write_volatile(UART_RX_INTR, 0);
        SERIAL_2_WAKERS.wake();
    }
}

/// The interrupt function for serial 3
pub extern "C" fn serial_3_intr() {
    unsafe {
        const UART_TX_INTR: *mut u8 = bitband_address(0x4006_C003, 7);
        const UART_TC_INTR: *mut u8 = bitband_address(0x4006_C003, 6);
        const UART_RX_INTR: *mut u8 = bitband_address(0x4006_C003, 5);
        write_volatile(UART_TX_INTR, 0);
        write_volatile(UART_TC_INTR, 0);
        write_volatile(UART_RX_INTR, 0);
        SERIAL_3_WAKERS.wake();
    }
}

/// The interrupt function for serial 4
pub extern "C" fn serial_4_intr() {
    unsafe {
        const UART_TX_INTR: *mut u8 = bitband_address(0x4006_D003, 7);
        const UART_TC_INTR: *mut u8 = bitband_address(0x4006_D003, 6);
        const UART_RX_INTR: *mut u8 = bitband_address(0x4006_D003, 5);
        write_volatile(UART_TX_INTR, 0);
        write_volatile(UART_TC_INTR, 0);
        write_volatile(UART_RX_INTR, 0);
        SERIAL_1_WAKERS.wake();
    }
}

/// The interrupt function for serial 5
pub extern "C" fn serial_5_intr() {
    unsafe {
        const UART_TX_INTR: *mut u8 = bitband_address(0x400E_A003, 7);
        const UART_TC_INTR: *mut u8 = bitband_address(0x400E_A003, 6);
        const UART_RX_INTR: *mut u8 = bitband_address(0x400E_A003, 5);
        write_volatile(UART_TX_INTR, 0);
        write_volatile(UART_TC_INTR, 0);
        write_volatile(UART_RX_INTR, 0);
        SERIAL_2_WAKERS.wake();
    }
}

/// The interrupt function for serial 6
pub extern "C" fn serial_6_intr() {
    unsafe {
        const UART_TX_INTR: *mut u8 = bitband_address(0x400E_B003, 7);
        const UART_TC_INTR: *mut u8 = bitband_address(0x400E_B003, 6);
        const UART_RX_INTR: *mut u8 = bitband_address(0x400E_B003, 5);
        write_volatile(UART_TX_INTR, 0);
        write_volatile(UART_TC_INTR, 0);
        write_volatile(UART_RX_INTR, 0);
        SERIAL_3_WAKERS.wake();
    }
}

const fn bitband_address<T>(addr: u32, bit: u32) -> *mut T {
    (0x4200_0000 + (addr - 0x4000_0000) * 32 + bit * 4) as _
}