avr-oxide 0.0.3

An extremely simply Rusty operating system for AVR microcontrollers
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
/* ports.rs
 *
 * Developed by Tim Walls <tim.walls@snowgoons.com>
 * Copyright (c) All Rights Reserved, Tim Walls
 */
//! Generic I/O ports provided by the AVR microcontroller.

// Imports ===================================================================


// Declarations ==============================================================
/**
 * Input/output mode for a pin
 */
#[derive(Clone,Copy,PartialEq,Eq)]
pub enum PinMode {
  /// Pin is configured as output
  Output,
  /// Pin is configured as an input with internal pullup enabled
  InputPullup,
  /// Pin is configured as an input with internal pullup disabled
  InputFloating
}

/**
 * Interrupt generation mode for a pin
 */
#[derive(Clone,Copy,PartialEq,Eq)]
pub enum InterruptMode {
  /// Do not generate interrupts
  Disabled,
  /// Interrupt on both rising and falling edges
  BothEdges,
  /// Interrupt on rising edge
  RisingEdge,
  /// Interrupt on falling edge
  FallingEdge,
  /// Interrupt while input is low
  LowLevel
}

/**
 * Callback called by a pin when it generates an interrupt.  The callback
 * is given the new state read from the pin (true = high).
 * The callback should return a boolean:
 * * true:  Continue generating interrupts for this pin
 * * false: Disable generating interrupts for this pin
 *
 * Note: The callback runs *within the interrupt context* - be careful about
 * using mutual exclusion where necessary, and *DO NOT DO HEAVY PROCESSING
 * IN THE CALLBACK*.
 */
pub type PinInterruptHandler = fn(bool) -> ();

pub trait Pin {
  /// Set the input/output mode for this pin
  fn set_mode(&mut self, mode: PinMode);

  /// Toggle this pin's output level
  fn toggle(&mut self);

  /// Set the pin's output to high
  fn set_high(&mut self);

  /// Set the pin's output to low
  fn set_low(&mut self);

  /// Set the pin's output according to the boolean (true == high, false == low)
  fn set(&mut self, high: bool);

  /// Get the pin's input; true == high, false == low.
  fn get(&self) -> bool;

  /// Set when this pin will generate interrupts
  fn set_interrupt_mode(&mut self, mode: InterruptMode);

  /// Listen for interrupts on this pin and call the given handler callback
  /// when one occurs.
  fn listen(&mut self, handler: Option<PinInterruptHandler>);

}

pub trait Port<P>
where
  P: Pin
{
  fn pin_instance(&self, pin: u8) -> &P;
}

// Code ======================================================================
pub mod base {
  use crate::hal::generic::port::{Port,InterruptMode};

  pub trait AtmelPortControl {
    /// Set this pin as an output.
    fn enable_output(&mut self, p: u8);
    /// Disable output on this pin
    fn disable_output(&mut self, p: u8);
    /// Enable internal pullup on this pin
    fn enable_pullup(&mut self, p: u8);
    /// Disable internal pullup on this pin
    fn disable_pullup(&mut self, p: u8);
    /// Set a pin's output state high
    fn set_high(&mut self, p: u8);
    /// Set a pin's output state low
    fn set_low(&mut self, p: u8);
    /// Toggle a pin's input state
    fn toggle(&mut self, p: u8);
    /// Get a pin's state
    fn get(&self, p: u8) -> bool;
    /// Set the mode for interrupt generation for a pin
    fn set_interrupt_mode(&mut self, p: u8, mode: InterruptMode);
    /// True iff the given pin has generated an interrupt
    fn interrupted(&self, p: u8) -> bool;
    /// Clear the interrupt flag for all pins on this port
    fn clear_interrupts(&mut self);
  }

  impl InterruptMode {
    #[inline(always)]
    fn to_pinctrl_isc(&self) -> u8 {
      match self {
        InterruptMode::Disabled => 0x00,
        InterruptMode::BothEdges => 0x01,
        InterruptMode::RisingEdge => 0x02,
        InterruptMode::FallingEdge => 0x03,
        InterruptMode::LowLevel => 0x05
      }
    }

    #[inline(always)]
    fn pinctrl_mask() -> u8 {
      0b00000111
    }
  }

  /**
   * The AVR port control register block.  The structure provided by the
   * auto-generated avr-device crate is just horrible, so we use our own
   * rendition.  This allows accessing pin control by index, and also avoids
   * having different types for every port.
   */
  #[repr(C)]
  pub struct AvrPortRegisterBlock {
    pub(crate) dir: u8,
    pub(crate) dir_set: u8,
    pub(crate) dir_clr: u8,
    pub(crate) dir_tgl: u8,
    pub(crate) out: u8,
    pub(crate) out_set: u8,
    pub(crate) out_clr: u8,
    pub(crate) out_tgl: u8,
    pub(crate) inp: u8,
    pub(crate) intflags: u8,
    pub(crate) portctrl: u8,
    pub(crate) reserved: [u8; 5],
    pub(crate) pinctrl: [u8; 8]
  }



  impl AtmelPortControl for AvrPortRegisterBlock {
    #[inline(always)]
    fn enable_output(&mut self, p: u8) {
      unsafe {
        core::ptr::write_volatile(&mut self.dir_set as *mut u8, 0x01 << p);
      }
    }
    #[inline(always)]
    fn disable_output(&mut self, p: u8) {
      unsafe {
        core::ptr::write_volatile(&mut self.dir_clr as *mut u8, 0x01 << p);
      }
    }

    fn enable_pullup(&mut self, p: u8) {
      unsafe {
        let pinctrl_reg = &mut self.pinctrl[p as usize] as *mut u8;

        let pinctrl = core::ptr::read_volatile(pinctrl_reg);

        core::ptr::write_volatile(pinctrl_reg, pinctrl | 0b00001000);
      }
    }

    fn disable_pullup(&mut self, p: u8) {
      unsafe {
        let pinctrl_reg = &mut self.pinctrl[p as usize] as *mut u8;

        let pinctrl = core::ptr::read_volatile(pinctrl_reg);

        core::ptr::write_volatile(pinctrl_reg, pinctrl & 0b11110111);
      }
    }

    #[inline(always)]
    fn set_high(&mut self, p: u8) {
      unsafe {
        core::ptr::write_volatile(&mut self.out_set as *mut u8, 0x01 << p);
      }
    }
    #[inline(always)]
    fn set_low(&mut self, p: u8) {
      unsafe {
        core::ptr::write_volatile(&mut self.out_clr as *mut u8, 0x01 << p);
      }
    }
    #[inline(always)]
    fn toggle(&mut self, p: u8) {
      unsafe {
        core::ptr::write_volatile(&mut self.out_tgl as *mut u8, 0x01 << p);
      }
    }

    #[inline(always)]
    fn get(&self, p: u8) -> bool {
      unsafe {
        core::ptr::read_volatile(&self.inp as *const u8) & (0x01 << p) != 0
      }
    }

    fn set_interrupt_mode(&mut self, p: u8, mode: InterruptMode) {
      unsafe {
        let pinctrl_reg = &mut self.pinctrl[p as usize] as *mut u8;

        let pinctrl = core::ptr::read_volatile(pinctrl_reg);

        core::ptr::write_volatile(pinctrl_reg,
                                  (pinctrl & !InterruptMode::pinctrl_mask()) | mode.to_pinctrl_isc());
      }
    }

    #[inline(always)]
    fn interrupted(&self, p: u8) -> bool {
      unsafe {
        core::ptr::read_volatile(&self.intflags as *const u8) & (0x01 << p) != 0
      }
    }

    #[inline(always)]
    fn clear_interrupts(&mut self) {
      unsafe {
        core::ptr::write_volatile(&mut self.intflags as *mut u8, 0xff);
      }
    }


  }

  #[macro_export]
  macro_rules! atmel_port_tpl {
    ($ref:expr, $isr:ident) => {
      extern crate avr_device_snowgoons as avr_device;

      use crate::hal::generic::port::{PinMode,Port,Pin,InterruptMode,PinInterruptHandler};
      use crate::hal::generic::port::base::{AtmelPortControl,AvrPortRegisterBlock};

      pub type PortImpl = AvrPortRegisterBlock;
      pub type PinImpl = AtmelPin;

      /**
       * A no-op function that is the default handler for the button
       * interrupt callbacks.
       */
      #[inline(never)]
      fn nop_handler(_state: bool) -> () {
        unsafe { llvm_asm!("nop" :::: "volatile" ) }
      }

      /**
       * A single pin instance.  This is just a wrapper for a reference to
       * the pin number and a place to stash the callback function that will
       * be called when we have an interrupt.
       */
      pub struct AtmelPin {
        n: u8,
        handler: PinInterruptHandler
      }

      /**
       * Implementation of the Pin API for our Atmel PINs.
       */
      impl Pin for AtmelPin {
        #[inline(always)]
        fn set_mode(&mut self, mode: PinMode) {
          match mode {
            PinMode::Output => {
              instance().enable_output(self.n);
            },
            PinMode::InputPullup => {
              instance().enable_pullup(self.n);
              instance().disable_output(self.n);
            },
            PinMode::InputFloating => {
              instance().disable_pullup(self.n);
              instance().disable_output(self.n);
            }
          }
        }
        #[inline(always)]
        fn toggle(&mut self) {
          instance().toggle(self.n);
        }
        #[inline(always)]
        fn set_high(&mut self) {
          instance().set_high(self.n);
        }
        #[inline(always)]
        fn set_low(&mut self) {
          instance().set_low(self.n);
        }
        #[inline(always)]
        fn set(&mut self, high: bool) {
          match high {
            true  => instance().set_high(self.n),
            false => instance().set_low(self.n)
          }
        }
        #[inline(always)]
        fn get(&self) -> bool {
          instance().get(self.n)
        }
        fn set_interrupt_mode(&mut self, mode: InterruptMode) {
          instance().set_interrupt_mode(self.n, mode)
        }
        fn listen(&mut self, handler: Option<PinInterruptHandler>){
          crate::hal::concurrency::imp::atomic(|_|{
            unsafe {
              PINS[self.n as usize].handler = match handler {
                Some(handler) => handler,
                None => nop_handler
              }
            }
          });
        }
      }

      static mut INITIALISED: bool = false;

      /**
       * Get an instance of this port's register block.  Also does any
       * static initialisation required of the device the first time it is
       * called.
       */
      #[inline(always)]
      pub fn instance() -> &'static mut AvrPortRegisterBlock  {
        unsafe {
          // Not sure if this is a compiler bug or what, but our static
          // muts don't seem to be correctly initialised, so we need to
          // do this here instead
          //
          // ( @todo investigate - Probably I need to amend my boot code to call
          //         something to do the initialisation for me )
          if(!INITIALISED){
            for pin in 0..=7 {
              PINS[pin].n = pin as u8;
              PINS[pin].handler = nop_handler;
            }
            INITIALISED = true;
          }

          core::mem::transmute($ref)
        }
      }

      static mut PINS : [AtmelPin; 8] = [
        AtmelPin { n: 0, handler: nop_handler },
        AtmelPin { n: 1, handler: nop_handler },
        AtmelPin { n: 2, handler: nop_handler },
        AtmelPin { n: 3, handler: nop_handler },
        AtmelPin { n: 4, handler: nop_handler },
        AtmelPin { n: 5, handler: nop_handler },
        AtmelPin { n: 6, handler: nop_handler },
        AtmelPin { n: 7, handler: nop_handler },
      ];


      /**
       * Return a single pin instance wrapper.
       */
      pub fn pin_instance(pin: u8) -> &'static mut AtmelPin {
        debug_assert!(pin < 8);

        unsafe {
          PINS[0].handler = nop_handler;

          &mut PINS[pin as usize]
        }
      }

      /**
       * Interrupt service routine.  Checks which pins caused the interrupt
       * and then calls the relevant callback routine.
       */
      #[no_mangle]
      pub unsafe extern "avr-interrupt" fn $isr() {
        let intflags:u8 = core::ptr::read_volatile(&instance().intflags as *const u8);
        let state:u8    = core::ptr::read_volatile(&instance().inp as *const u8);
        let mut mask    = 0x01;

        for pin in 0..=7 {
          if (intflags & mask) > 0 {
            (PINS[pin].handler)((state & mask) > 0);
          }
          mask <<= 1;
        }
        instance().clear_interrupts();
      }
    }
  }
}