teensycore 0.1.0

A kernel for the teensy4.0 microcontroller
Documentation
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! This module represents the serial communication protocol
//! based on UART physical hardware.
//!
//! Typical operations in the kernel do not require direct uart access.
//! Instead, the `serio` interface has been devised which abstracts
//! much of the nuance away.
//!
//! On the Teensy4.0, Uart6 is what most would think of as the "Primary" uart.
//! It is located on pins 0 and 1.
//!
//! It is worth noting that the debug module of this kernel leverages
//! SerioDevice::Uart4 to output any debug data.
//!
//! Simple usage
//!
//! ```no_run
//! use teensycore::serio::*;
//!
//! serial_init(SerioDevice::Uart6);
//! serial_write(SerioDevice::Uart6, b"Hello, world!\r\n");
//!
//! while serial_available(SerioDevice::Uart6) > 0 {
//!     let sb = serial_read(SerioDevice::Uart6);
//!     // Do something with the Str
//! }
//! ```

#![allow(unused)]

use crate::debug::*;
use crate::phys::addrs;
use crate::phys::irq::*;
use crate::phys::pins::*;
use crate::phys::uart::*;
use crate::system::buffer::*;
use crate::system::str::*;
use crate::system::vector::*;

struct HardwareConfig {
    device: Device,
    tx_pin: usize,
    rx_pin: usize,
    irq: Irq,
    sel_inp_reg: Option<u32>,
    sel_inp_val: Option<u32>,
}

static mut TEMP_BUF: [u8; 128] = [0; 128];
const UART_WATERMARK_SIZE: u32 = 0x2;
const UART_BUFFER_DEPTH: usize = 512; // Note: this is repeated for every uart device. Don't make it too big.
static mut UART1: Uart = Uart::new(HardwareConfig {
    device: Device::Uart1,
    tx_pin: 24,
    rx_pin: 25,
    irq: Irq::Uart1,
    sel_inp_reg: None,
    sel_inp_val: None,
});

static mut UART2: Uart = Uart::new(HardwareConfig {
    device: Device::Uart2,
    tx_pin: 14,
    rx_pin: 15,
    irq: Irq::Uart2,
    sel_inp_reg: Some(addrs::IOMUXC_LPUART2_RX_SELECT_INPUT),
    sel_inp_val: Some(0x1),
});

static mut UART3: Uart = Uart::new(HardwareConfig {
    device: Device::Uart3,
    tx_pin: 17,
    rx_pin: 16,
    irq: Irq::Uart3,
    sel_inp_reg: Some(addrs::IOMUXC_LPUART3_RX_SELECT_INPUT),
    sel_inp_val: Some(0x0),
});

static mut UART4: Uart = Uart::new(HardwareConfig {
    device: Device::Uart4,
    tx_pin: 8,
    rx_pin: 7,
    irq: Irq::Uart4,
    sel_inp_reg: Some(addrs::IOMUXC_LPUART4_RX_SELECT_INPUT),
    sel_inp_val: Some(0x2),
});

static mut UART5: Uart = Uart::new(HardwareConfig {
    device: Device::Uart5,
    tx_pin: 1,
    rx_pin: 0,
    irq: Irq::Uart5,
    sel_inp_reg: Some(addrs::IOMUXC_LPUART5_RX_SELECT_INPUT),
    sel_inp_val: Some(0x0),
}); // NOTE: THIS DEVICE DOESN'T HAVE VALID PINS

static mut UART6: Uart = Uart::new(HardwareConfig {
    device: Device::Uart6,
    tx_pin: 1,
    rx_pin: 0,
    irq: Irq::Uart6,
    sel_inp_reg: Some(addrs::IOMUXC_LPUART6_RX_SELECT_INPUT),
    sel_inp_val: Some(0x1),
});

static mut UART7: Uart = Uart::new(HardwareConfig {
    device: Device::Uart7,
    tx_pin: 29,
    rx_pin: 28,
    irq: Irq::Uart7,
    sel_inp_reg: Some(addrs::IOMUXC_LPUART7_RX_SELECT_INPUT),
    sel_inp_val: Some(0x1),
});

static mut UART8: Uart = Uart::new(HardwareConfig {
    device: Device::Uart8,
    tx_pin: 20,
    rx_pin: 21,
    irq: Irq::Uart8,
    sel_inp_reg: Some(addrs::IOMUXC_LPUART8_RX_SELECT_INPUT),
    sel_inp_val: Some(0x0),
});

#[derive(Clone, Copy)]
pub enum SerioDevice {
    Uart1 = 0x0,
    Uart2 = 0x1,
    Uart3 = 0x2,
    Uart4 = 0x3,
    Uart5 = 0x4,
    Uart6 = 0x5,
    Uart7 = 0x6,
    Uart8 = 0x7,
    Default = 0x8,
}

/**
    This encapsulates an entire Uart device
    being instantiated, including all necessary memory
    and mappings.
*/
struct Uart {
    device: Device,
    tx_pin: usize,
    rx_pin: usize,
    initialized: bool,
    irq: Irq,
    tx_buffer: Buffer<UART_BUFFER_DEPTH, u8>,
    rx_buffer: Str,
    sel_inp_reg: Option<u32>,
    sel_inp_val: Option<u32>,
    buffer_head: usize,
    tx_count: u32,
    paused: bool,
}

impl Uart {
    pub const fn new(config: HardwareConfig) -> Uart {
        return Uart {
            device: config.device,
            tx_buffer: Buffer {
                data: [0; UART_BUFFER_DEPTH],
                tail: 0,
            },
            rx_buffer: Str::new(),
            buffer_head: 0,
            initialized: false,
            tx_pin: config.tx_pin,
            rx_pin: config.rx_pin,
            sel_inp_reg: config.sel_inp_reg,
            sel_inp_val: config.sel_inp_val,
            irq: config.irq,
            tx_count: 0,
            paused: false,
        };
    }

    fn initialize(&mut self) {
        if self.initialized {
            return;
        }

        // Initialize the pins
        pin_mux_config(self.tx_pin, Alt::Alt2);
        pin_pad_config(
            self.tx_pin,
            PadConfig {
                hysterisis: true,
                resistance: PullUpDown::PullDown100k,
                pull_keep: PullKeep::Keeper,
                pull_keep_en: false,
                open_drain: false,
                speed: PinSpeed::Low50MHz,
                drive_strength: DriveStrength::MaxDiv3,
                fast_slew_rate: true,
            },
        );

        pin_mux_config(self.rx_pin, Alt::Alt2);
        pin_pad_config(
            self.rx_pin,
            PadConfig {
                hysterisis: true,
                resistance: PullUpDown::PullUp22k,
                pull_keep: PullKeep::Pull,
                pull_keep_en: true,
                open_drain: false,
                speed: PinSpeed::Low50MHz,
                drive_strength: DriveStrength::MaxDiv3,
                fast_slew_rate: false,
            },
        );

        // Configure the base settings
        uart_disable(self.device);
        uart_sw_reset(self.device, true);
        uart_sw_reset(self.device, false);
        uart_configure(
            self.device,
            UartConfig {
                r9t8: false,
                invert_transmission_polarity: false,
                overrun_irq_en: true,
                noise_error_irq_en: false,
                framing_error_irq_en: false,
                parity_error_irq_en: false,
                tx_irq_en: false, // This gets set later
                rx_irq_en: true,
                tx_complete_irq_en: true,
                idle_line_irq_en: true,
                tx_en: false,
                rx_en: false,
                match1_irq_en: false,
                match2_irq_en: false,
                idle_config: IdleConfiguration::Idle64Char,
                doze_en: false,
                bit_mode: BitMode::EightBits,
                parity_en: false,
                parity_type: ParityType::Even,
            },
        );

        uart_configure_fifo(
            self.device,
            FifoConfig {
                tx_fifo_underflow_flag: false,
                rx_fifo_underflow_flag: false,
                tx_flush: false,
                rx_flush: false,
                tx_fifo_overflow_irq_en: false,
                rx_fifo_underflow_irq_en: true,
                tx_fifo_en: true,
                rx_fifo_en: true,
            },
        );

        uart_set_pin_config(self.device, InputTrigger::Disabled);
        uart_disable_fifo(self.device);

        uart_watermark(self.device, UART_WATERMARK_SIZE);
        uart_enable(self.device);

        pin_mode(self.tx_pin, Mode::Output);
        pin_mode(self.rx_pin, Mode::Input);

        // If this uart requires additional input muxing, do it.
        if self.sel_inp_reg.is_some() {
            crate::phys::assign(self.sel_inp_reg.unwrap(), self.sel_inp_val.unwrap());
        }

        pin_out(self.tx_pin, Power::Low);

        irq_attach(self.irq, serio_handle_irq);
        irq_enable(self.irq);
        irq_priority(self.irq, 128);
        uart_baud_rate(self.device, 115200);

        self.initialized = true;
    }

    pub fn available(&self) -> usize {
        return self.rx_buffer.len();
    }

    pub fn pause(&mut self) {
        self.paused = true;
    }

    pub fn resume(&mut self) {
        self.paused = false;
    }

    pub fn write(&mut self, bytes: &[u8]) {
        for byte_idx in 0..bytes.len() {
            self.tx_buffer.enqueue(bytes[byte_idx]);
        }

        uart_set_reg(self.device, &CTRL_TCIE);
        pin_out(self.tx_pin, Power::High);
    }

    pub fn write_vec(&mut self, bytes: &Vector<u8>) {
        for item in bytes.into_iter() {
            self.tx_buffer.push(item);
        }

        pin_out(self.tx_pin, Power::High);
        uart_set_reg(self.device, &CTRL_TCIE);
    }

    pub fn get_rx_buffer(&mut self) -> &mut Str {
        return &mut self.rx_buffer;
    }

    fn handle_receive_irq(&mut self) {
        let irq_statuses = uart_get_irq_statuses(self.device);

        // TODO: Implement some logic for these edge cases
        // but it's really not needed for just simply
        // receiving messages.
        let rx_overrun = irq_statuses & (0x1 << 19) > 0;
        // let rx_active = irq_statuses & (0x1 << 24) > 0;
        // let rx_buffer_full = irq_statuses & (0x1 << 21) > 0;
        // let rx_idle = irq_statuses & (0x1 << 20) > 0;

        // Read until it is empty
        let mut count = 0;
        while uart_has_data(self.device) {
            let msg: u8 = uart_read_fifo(self.device);
            self.rx_buffer.append(&[msg]);
            unsafe { TEMP_BUF[count] = msg };
            count += 1;
        }

        if rx_overrun {
            crate::debug::blink_accumulate();
        }
    }

    fn transmit(&mut self) {
        match self.tx_buffer.dequeue() {
            None => {}
            Some(byte) => {
                // Get the next byte to write and beam it
                uart_write_fifo(self.device, byte);
            }
        }
    }

    fn handle_send_irq(&mut self) {
        // Transmission complete
        let irq_statuses = uart_get_irq_statuses(self.device);
        let tx_complete = irq_statuses & (0x1 << 22) > 0;
        let pending_data = self.tx_buffer.size() > 0;

        // Check if there is space in the buffer
        if pending_data && tx_complete {
            self.transmit();
        } else if !pending_data {
            uart_clear_reg(self.device, &CTRL_TCIE);
        }
    }

    pub fn handle_irq(&mut self) {
        // Don't process a uart device that hasn't
        // been used
        if !self.initialized {
            return;
        }

        self.handle_receive_irq();
        self.handle_send_irq();
        uart_clear_irq(self.device);
    }
}

fn get_uart_interface(device: SerioDevice) -> &'static mut Uart {
    unsafe {
        return match device {
            SerioDevice::Uart1 => &mut UART1,
            SerioDevice::Uart2 => &mut UART2,
            SerioDevice::Uart3 => &mut UART3,
            SerioDevice::Uart4 => &mut UART4,
            SerioDevice::Uart5 => &mut UART5,
            SerioDevice::Uart6 => &mut UART6,
            SerioDevice::Uart7 => &mut UART7,
            SerioDevice::Uart8 => &mut UART8,

            // Specify defaut here
            SerioDevice::Default => &mut UART6,
        };
    }
}

/// Initializes the serial device. This configures
/// and muxes the relevant pins, sets baud rate,
/// enables peripheral device, and generally
/// wakes up the uart.
pub fn serial_init(device: SerioDevice) {
    let uart = get_uart_interface(device);
    uart.initialize();
}

/// Retuns the current buffer of data the serial interface
/// has accumulated.
///
/// You can interact with this buffer, modify it, etc. It is
/// recommended to call `.clear()` on the buffer as soon as you are
/// done with the data, otherwise the buffer will eventually
/// overflow.
pub fn serial_read<'a>(device: SerioDevice) -> &'a mut Str {
    let uart = get_uart_interface(device);
    return uart.get_rx_buffer();
}

/// Returns the amount of data in the currenet read buffer.
pub fn serial_available(device: SerioDevice) -> usize {
    let uart = get_uart_interface(device);
    return uart.available();
}

/// Enqueue data to be written over serial.
///
/// This data will be written at the next available interrupt
/// cycle.
pub fn serial_write(device: SerioDevice, bytes: &[u8]) {
    let uart = get_uart_interface(device);
    uart.write(bytes);
}

pub fn serial_write_vec(device: SerioDevice, bytes: &Vector<u8>) {
    let uart = get_uart_interface(device);
    for byte in bytes.into_iter() {
        uart.write(&[byte]);
    }
}

pub fn serial_write_str(device: SerioDevice, bytes: &mut Str) {
    let uart = get_uart_interface(device);
    for byte in bytes.into_iter() {
        uart.write(&[byte]);
    }

    // Fixes memory leak. When calling this function you'll
    // usually be operating with an intermediary string and
    // won't be able to drop() it yourself.
    bytes.drop();
}

pub fn serial_baud(device: SerioDevice, rate: u32) {
    let uart = get_uart_interface(device);
    uart_baud_rate(uart.device, rate);
}

pub fn serio_handle_irq() {
    irq_disable(Irq::Uart1);
    irq_disable(Irq::Uart2);
    irq_disable(Irq::Uart3);
    irq_disable(Irq::Uart4);
    irq_disable(Irq::Uart5);
    irq_disable(Irq::Uart6);
    irq_disable(Irq::Uart7);
    irq_disable(Irq::Uart8);

    get_uart_interface(SerioDevice::Uart1).handle_irq();
    get_uart_interface(SerioDevice::Uart2).handle_irq();
    get_uart_interface(SerioDevice::Uart3).handle_irq();
    get_uart_interface(SerioDevice::Uart4).handle_irq();
    get_uart_interface(SerioDevice::Uart5).handle_irq();
    get_uart_interface(SerioDevice::Uart6).handle_irq();
    get_uart_interface(SerioDevice::Uart7).handle_irq();
    get_uart_interface(SerioDevice::Uart8).handle_irq();

    irq_enable(Irq::Uart1);
    irq_enable(Irq::Uart2);
    irq_enable(Irq::Uart3);
    irq_enable(Irq::Uart4);
    irq_enable(Irq::Uart5);
    irq_enable(Irq::Uart6);
    irq_enable(Irq::Uart7);
    irq_enable(Irq::Uart8);
}