CHERIoT RTOS
A compartmentalised RTOS for CHERIoT hardware
Loading...
Searching...
No Matches
platform-gpio.hh
1#pragma once
2#include <cdefs.h>
3#include <stdint.h>
4
5/**
6 * Represents the state of the Sonata's joystick.
7 *
8 * Note, that up to three of the bits may be asserted at any given time.
9 * There may be up to two cardinal directions asserted when the joystick is
10 * pushed in a diagonal and the joystick may be pressed while being pushed in a
11 * given direction.
12 */
13enum class SonataJoystick : uint8_t
14{
15 Left = 1 << 0,
16 Up = 1 << 1,
17 Pressed = 1 << 2,
18 Down = 1 << 3,
19 Right = 1 << 4,
20};
21
22/**
23 * A Simple Driver for the Sonata's GPIO.
24 *
25 * Documentation source can be found at:
26 * https://github.com/lowRISC/sonata-system/blob/1a59633d2515d4fe186a07d53e49ff95c18d9bbf/doc/ip/gpio.md
27 *
28 * Rendered documentation is served from:
29 * https://lowrisc.org/sonata-system/doc/ip/gpio.html
30 */
32{
33 uint32_t output;
34 uint32_t input;
35 uint32_t debouncedInput;
36 uint32_t debouncedThreshold;
37 uint32_t raspberryPiHeader;
38 uint32_t raspberryPiMask;
39 uint32_t arduinoShieldHeader;
40 uint32_t arduinoShieldMask;
41
42 /**
43 * The bit index of the first GPIO pin connected to a user LED.
44 */
45 static constexpr uint32_t FirstLED = 4;
46 /**
47 * The bit index of the last GPIO pin connected to a user LED.
48 */
49 static constexpr uint32_t LastLED = 11;
50 /**
51 * The number of user LEDs.
52 */
53 static constexpr uint32_t LEDCount = LastLED - FirstLED + 1;
54 /**
55 * The mask covering the GPIO pins used for user LEDs.
56 */
57 static constexpr uint32_t LEDMask = ((1 << LEDCount) - 1) << FirstLED;
58
59 /**
60 * The output bit mask for a given user LED index
61 */
62 constexpr static uint32_t led_bit(uint32_t index)
63 {
64 return (1 << (index + FirstLED)) & LEDMask;
65 }
66
67 /**
68 * Switches off the LED at the given user LED index
69 */
70 void led_on(uint32_t index) volatile
71 {
72 output = output | led_bit(index);
73 }
74
75 /**
76 * Switches on the LED at the given user LED index
77 */
78 void led_off(uint32_t index) volatile
79 {
80 output = output & ~led_bit(index);
81 }
82
83 /**
84 * Toggles the LED at the given user LED index
85 */
86 void led_toggle(uint32_t index) volatile
87 {
88 output = output ^ led_bit(index);
89 }
90
91 /**
92 * The bit index of the first GPIO pin connected to a user switch.
93 */
94 static constexpr uint32_t FirstSwitch = 5;
95 /**
96 * The bit index of the last GPIO pin connected to a user switch.
97 */
98 static constexpr uint32_t LastSwitch = 13;
99 /**
100 * The number of user switches.
101 */
102 static constexpr uint32_t SwitchCount = LastSwitch - FirstSwitch + 1;
103 /**
104 * The mask covering the GPIO pins used for user switches.
105 */
106 static constexpr uint32_t SwitchMask = ((1 << SwitchCount) - 1)
107 << FirstSwitch;
108
109 /**
110 * The input bit mask for a given user switch index
111 */
112 constexpr static uint32_t switch_bit(uint32_t index)
113 {
114 return (1 << (index + FirstSwitch)) & SwitchMask;
115 }
116
117 /**
118 * Returns the value of the switch at the given user switch index.
119 */
120 bool read_switch(uint32_t index) volatile
121 {
122 return (input & switch_bit(index)) > 0;
123 }
124
125 /**
126 * Returns the state of the joystick.
127 */
128 SonataJoystick read_joystick() volatile
129 {
130 return static_cast<SonataJoystick>(input & 0x1f);
131 }
132};
A Simple Driver for the Sonata's GPIO.
static constexpr uint32_t LEDMask
The mask covering the GPIO pins used for user LEDs.
void led_off(uint32_t index) volatile
Switches on the LED at the given user LED index.
bool read_switch(uint32_t index) volatile
Returns the value of the switch at the given user switch index.
static constexpr uint32_t LEDCount
The number of user LEDs.
static constexpr uint32_t LastLED
The bit index of the last GPIO pin connected to a user LED.
static constexpr uint32_t SwitchMask
The mask covering the GPIO pins used for user switches.
static constexpr uint32_t SwitchCount
The number of user switches.
void led_toggle(uint32_t index) volatile
Toggles the LED at the given user LED index.
static constexpr uint32_t led_bit(uint32_t index)
The output bit mask for a given user LED index.
SonataJoystick read_joystick() volatile
Returns the state of the joystick.
static constexpr uint32_t FirstLED
The bit index of the first GPIO pin connected to a user LED.
static constexpr uint32_t FirstSwitch
The bit index of the first GPIO pin connected to a user switch.
void led_on(uint32_t index) volatile
Switches off the LED at the given user LED index.
static constexpr uint32_t LastSwitch
The bit index of the last GPIO pin connected to a user switch.
static constexpr uint32_t switch_bit(uint32_t index)
The input bit mask for a given user switch index.