esp32/
gpio.rs

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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! GPIO driver.

use core::ops::{Index, IndexMut};
use kernel::hil::gpio;
use kernel::utilities::cells::OptionalCell;
use kernel::utilities::registers::interfaces::{ReadWriteable, Readable, Writeable};
use kernel::utilities::registers::{
    register_bitfields, register_structs, Field, ReadWrite, WriteOnly,
};
use kernel::utilities::StaticRef;

pub const GPIO_BASE: StaticRef<GpioRegisters> =
    unsafe { StaticRef::new(0x6000_4000 as *const GpioRegisters) };

pub const IOMUX_BASE: StaticRef<IoMuxRegisters> =
    unsafe { StaticRef::new(0x6000_9000 as *const IoMuxRegisters) };

register_structs! {
    pub GpioRegisters {
        (0x0 => bt_select: ReadWrite<u32>),
        (0x4 => gpio_out: ReadWrite<u32, pins::Register>),
        (0x8 => gpio_out_w1ts: WriteOnly<u32, pins::Register>),
        (0xC => gpio_out_w1tc: WriteOnly<u32, pins::Register>),
        (0x10 => _reserved0),
        (0x1C => sdio_select: ReadWrite<u32>),
        (0x20 => enable: ReadWrite<u32, pins::Register>),
        (0x24 => enable_w1ts: ReadWrite<u32, pins::Register>),
        (0x28 => enable_w1tc: ReadWrite<u32, pins::Register>),
        (0x2C => _reserved1),
        (0x38 => strap: ReadWrite<u32>),
        (0x3C => gpio_in: ReadWrite<u32, pins::Register>),
        (0x40 => _reserved2),
        (0x44 => status: ReadWrite<u32, pins::Register>),
        (0x48 => status_w1ts: ReadWrite<u32, pins::Register>),
        (0x4C => status_w1tc: ReadWrite<u32, pins::Register>),
        (0x50 => _reserved3),
        (0x5C => pcpu_int: ReadWrite<u32>),
        (0x60 => pcpu_nmi_int: ReadWrite<u32>),
        (0x64 => cpusdio_int: ReadWrite<u32>),
        (0x68 => _reserved4),
        (0x74 => pin: [ReadWrite<u32, PIN::Register>; 26]),
        (0xDC => _reserved5),
        (0x14C => status_next: ReadWrite<u32>),
        (0x150 => _reserved6),
        (0x154 => func_in_sel_cfg: [ReadWrite<u32>; 128]),
        (0x354 => _reserved7),
        (0x554 => func_out_sel_cfg: [ReadWrite<u32>; 26]),
        (0x5BC => _reserved8),
        (0x62C => clock_gate: ReadWrite<u32>),
        (0x630 => _reserved9),
        (0x6FC => date: ReadWrite<u32>),
        (0x700 => @END),
    },

    pub IoMuxRegisters {
        (0x00 => pin_ctrl: ReadWrite<u32>),
        (0x04 => gpio: [ReadWrite<u32, IO_MUX_GPIO::Register>; 22]),
        (0x5C => _reserved0),
        (0xFC => date: ReadWrite<u32>),
        (0x100 => @END),
    }
}

register_bitfields![u32,
    pub pins [
        pin0 0,
        pin1 1,
        pin2 2,
        pin3 3,
        pin4 4,
        pin5 5,
        pin6 6,
        pin7 7,
        pin8 8,
        pin9 9,
        pin10 10,
        pin11 11,
        pin12 12,
        pin13 13,
        pin14 14,
        pin15 15,
        pin16 16,
        pin17 17,
        pin18 18,
        pin19 19,
        pin20 20,
        pin21 21,
        pin22 22,
        pin23 23,
        pin24 24,
        pin25 25
    ],
    MASK_HALF [
        DATA OFFSET(0) NUMBITS(16) [],
        MASK OFFSET(16) NUMBITS(16) [],
    ],
    PIN [
        INT_ENA OFFSET(13) NUMBITS(5) [
            Disabled = 0,
            Enable = 1,
            NMI = 2,
        ],
        CONFIG OFFSET(11) NUMBITS(2) [],
        WAKEUP_ENABLE OFFSET(10) NUMBITS(1) [],
        INT_TYPE OFFSET(7) NUMBITS(3) [
            DISABLE = 0,
            POSEDGE = 1,
            NEGEDGE = 2,
            ANYEDGE = 3,
            LOW_LEVEL = 4,
            HIGH_LEVEL = 5,
        ],
        SYNC1_BYPASS OFFSET(3) NUMBITS(2) [],
        PAD_DRIVER OFFSET(2) NUMBITS(1) [],
        SYNC2_BYPASS OFFSET(0) NUMBITS(2) [],
    ],
];

register_bitfields![u32,
    IO_MUX_GPIO [
        FILTER_EN OFFSET(15) NUMBITS(1) [],
        MCU_SEL OFFSET(12) NUMBITS(3) [
            FUN_0 = 0,
            FUN_1 = 1,
            FUN_2 = 2,
            FUN_3 = 3
        ],
        FUN_IE OFFSET(9) NUMBITS(1) [],
        FUN_WPU OFFSET(8) NUMBITS(1) [],
        FUN_WPD OFFSET(7) NUMBITS(1) [],
        MCU_IE OFFSET(4) NUMBITS(1) [],
        MCU_WPU OFFSET(3) NUMBITS(1) [],
        MCU_WPD OFFSET(2) NUMBITS(1) [],
        SLP_SEL OFFSET(1) NUMBITS(1) [],
        MCU_OE OFFSET(0) NUMBITS(1) [],
    ],
];

pub struct GpioPin<'a> {
    registers: StaticRef<GpioRegisters>,
    iomux_registers: StaticRef<IoMuxRegisters>,
    pin: Field<u32, pins::Register>,
    client: OptionalCell<&'a dyn gpio::Client>,
}

impl<'a> GpioPin<'a> {
    pub const fn new(
        gpio_base: StaticRef<GpioRegisters>,
        iomux_base: StaticRef<IoMuxRegisters>,
        pin: Field<u32, pins::Register>,
    ) -> GpioPin<'a> {
        GpioPin {
            registers: gpio_base,
            iomux_registers: iomux_base,
            pin,
            client: OptionalCell::empty(),
        }
    }

    fn handle_interrupt(&self) {
        // Clear the interrupt
        self.registers.status_w1tc.set(1 << self.pin.shift);

        // Trigger the upcall
        self.client.map(|client| {
            client.fired();
        });
    }
}

impl gpio::Configure for GpioPin<'_> {
    fn configuration(&self) -> gpio::Configuration {
        if self.registers.enable.is_set(self.pin) {
            gpio::Configuration::Input
        } else {
            gpio::Configuration::InputOutput
        }
    }

    fn set_floating_state(&self, mode: gpio::FloatingState) {
        match mode {
            gpio::FloatingState::PullUp => {
                self.iomux_registers.gpio[self.pin.shift]
                    .modify(IO_MUX_GPIO::FUN_WPU::SET + IO_MUX_GPIO::MCU_WPU::SET);
                self.iomux_registers.gpio[self.pin.shift]
                    .modify(IO_MUX_GPIO::FUN_WPD::CLEAR + IO_MUX_GPIO::MCU_WPD::CLEAR);
            }
            gpio::FloatingState::PullDown => {
                self.iomux_registers.gpio[self.pin.shift]
                    .modify(IO_MUX_GPIO::FUN_WPU::CLEAR + IO_MUX_GPIO::MCU_WPU::CLEAR);
                self.iomux_registers.gpio[self.pin.shift]
                    .modify(IO_MUX_GPIO::FUN_WPD::SET + IO_MUX_GPIO::MCU_WPD::SET);
            }
            gpio::FloatingState::PullNone => {
                self.iomux_registers.gpio[self.pin.shift]
                    .modify(IO_MUX_GPIO::FUN_WPU::CLEAR + IO_MUX_GPIO::MCU_WPU::CLEAR);
                self.iomux_registers.gpio[self.pin.shift]
                    .modify(IO_MUX_GPIO::FUN_WPD::CLEAR + IO_MUX_GPIO::MCU_WPD::CLEAR);
            }
        }
    }

    fn floating_state(&self) -> gpio::FloatingState {
        if self.iomux_registers.gpio[self.pin.shift].is_set(IO_MUX_GPIO::FUN_WPU) {
            gpio::FloatingState::PullUp
        } else if self.iomux_registers.gpio[self.pin.shift].is_set(IO_MUX_GPIO::FUN_WPD) {
            gpio::FloatingState::PullDown
        } else {
            gpio::FloatingState::PullNone
        }
    }

    fn deactivate_to_low_power(&self) {
        self.disable_input();
        self.disable_output();
    }

    fn make_output(&self) -> gpio::Configuration {
        // Setting peripheral index 128 causes GPIO_OUT_REG and GPIO_ENABLE_REG to enable for the given pin
        self.registers.func_out_sel_cfg[self.pin.shift].set(0x80);

        // IO Mux function 1 on all pins is GPIO, no alternate peripherals (see table 5-2 in ESP32 datasheet)
        self.iomux_registers.gpio[self.pin.shift].modify(IO_MUX_GPIO::MCU_SEL::FUN_1);

        self.registers
            .enable_w1ts
            .set(self.pin.mask << self.pin.shift);
        gpio::Configuration::Output
    }

    fn disable_output(&self) -> gpio::Configuration {
        self.registers
            .enable_w1tc
            .set(self.pin.mask << self.pin.shift);
        gpio::Configuration::Input
    }

    fn make_input(&self) -> gpio::Configuration {
        self.configuration()
    }

    fn disable_input(&self) -> gpio::Configuration {
        /* We can't do this from the GPIO controller.
         * It does look like the IO Mux is capable of this
         * though.
         */
        gpio::Configuration::Input
    }
}

impl gpio::Input for GpioPin<'_> {
    fn read(&self) -> bool {
        self.registers.gpio_in.is_set(self.pin)
    }
}

impl gpio::Output for GpioPin<'_> {
    fn toggle(&self) -> bool {
        let old_state = self.registers.gpio_out.is_set(self.pin);
        if old_state {
            self.clear();
        } else {
            self.set();
        }
        self.registers.gpio_out.is_set(self.pin)
    }

    fn set(&self) {
        self.registers
            .gpio_out_w1ts
            .set(self.pin.mask << self.pin.shift);
    }

    fn clear(&self) {
        self.registers
            .gpio_out_w1tc
            .set(self.pin.mask << self.pin.shift);
    }
}

impl<'a> gpio::Interrupt<'a> for GpioPin<'a> {
    fn set_client(&self, client: &'a dyn gpio::Client) {
        self.client.set(client);
    }

    fn enable_interrupts(&self, mode: gpio::InterruptEdge) {
        self.registers.pin[self.pin.shift].modify(PIN::INT_ENA::Enable);

        match mode {
            gpio::InterruptEdge::RisingEdge => {
                self.registers.pin[self.pin.shift].modify(PIN::INT_TYPE::POSEDGE);
            }
            gpio::InterruptEdge::FallingEdge => {
                self.registers.pin[self.pin.shift].modify(PIN::INT_TYPE::NEGEDGE);
            }
            gpio::InterruptEdge::EitherEdge => {
                self.registers.pin[self.pin.shift].modify(PIN::INT_TYPE::ANYEDGE);
            }
        }

        self.iomux_registers.gpio[self.pin.shift]
            .write(IO_MUX_GPIO::FUN_IE::SET + IO_MUX_GPIO::MCU_IE::SET);

        self.registers.pin[self.pin.shift].modify(PIN::SYNC2_BYPASS::SET);
        self.registers.pin[self.pin.shift].modify(PIN::SYNC1_BYPASS::SET);

        self.registers
            .status_next
            .set(1 << self.pin.shift | self.registers.status_next.get());

        self.registers.pin[self.pin.shift].modify(PIN::WAKEUP_ENABLE::SET);
    }

    fn disable_interrupts(&self) {
        self.registers.pin[self.pin.shift].modify(PIN::INT_ENA::Disabled);
    }

    fn is_pending(&self) -> bool {
        self.registers.status.is_set(self.pin)
    }
}

pub struct Port<'a> {
    pins: [GpioPin<'a>; 17],
}

impl Port<'_> {
    pub const fn new() -> Self {
        Self {
            pins: [
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin0),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin1),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin2),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin3),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin4),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin5),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin6),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin7),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin8),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin9),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin10),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin11),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin12),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin13),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin14),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin15),
                GpioPin::new(GPIO_BASE, IOMUX_BASE, pins::pin16),
            ],
        }
    }

    pub fn handle_interrupt(&self) {
        // Determine the GPIO pin that triggered.
        let pin = self.pins[0].registers.status.get().trailing_zeros() as usize;

        self.pins[pin].handle_interrupt();
    }
}

impl<'a> Index<usize> for Port<'a> {
    type Output = GpioPin<'a>;

    fn index(&self, index: usize) -> &GpioPin<'a> {
        &self.pins[index]
    }
}

impl<'a> IndexMut<usize> for Port<'a> {
    fn index_mut(&mut self, index: usize) -> &mut GpioPin<'a> {
        &mut self.pins[index]
    }
}