CHERIoT RTOS
A compartmentalised RTOS for CHERIoT hardware
Toggle main menu visibility
Loading...
Searching...
No Matches
sdk
include
platform
sunburst
v0.2
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
*/
13
enum 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
*/
31
struct
SonataGPIO
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
};
SonataGPIO
A Simple Driver for the Sonata's GPIO.
Definition
platform-gpio.hh:32
SonataGPIO::LEDMask
static constexpr uint32_t LEDMask
The mask covering the GPIO pins used for user LEDs.
Definition
platform-gpio.hh:57
SonataGPIO::led_off
void led_off(uint32_t index) volatile
Switches on the LED at the given user LED index.
Definition
platform-gpio.hh:78
SonataGPIO::read_switch
bool read_switch(uint32_t index) volatile
Returns the value of the switch at the given user switch index.
Definition
platform-gpio.hh:120
SonataGPIO::LEDCount
static constexpr uint32_t LEDCount
The number of user LEDs.
Definition
platform-gpio.hh:53
SonataGPIO::LastLED
static constexpr uint32_t LastLED
The bit index of the last GPIO pin connected to a user LED.
Definition
platform-gpio.hh:49
SonataGPIO::SwitchMask
static constexpr uint32_t SwitchMask
The mask covering the GPIO pins used for user switches.
Definition
platform-gpio.hh:106
SonataGPIO::SwitchCount
static constexpr uint32_t SwitchCount
The number of user switches.
Definition
platform-gpio.hh:102
SonataGPIO::led_toggle
void led_toggle(uint32_t index) volatile
Toggles the LED at the given user LED index.
Definition
platform-gpio.hh:86
SonataGPIO::led_bit
static constexpr uint32_t led_bit(uint32_t index)
The output bit mask for a given user LED index.
Definition
platform-gpio.hh:62
SonataGPIO::read_joystick
SonataJoystick read_joystick() volatile
Returns the state of the joystick.
Definition
platform-gpio.hh:128
SonataGPIO::FirstLED
static constexpr uint32_t FirstLED
The bit index of the first GPIO pin connected to a user LED.
Definition
platform-gpio.hh:45
SonataGPIO::FirstSwitch
static constexpr uint32_t FirstSwitch
The bit index of the first GPIO pin connected to a user switch.
Definition
platform-gpio.hh:94
SonataGPIO::led_on
void led_on(uint32_t index) volatile
Switches off the LED at the given user LED index.
Definition
platform-gpio.hh:70
SonataGPIO::LastSwitch
static constexpr uint32_t LastSwitch
The bit index of the last GPIO pin connected to a user switch.
Definition
platform-gpio.hh:98
SonataGPIO::switch_bit
static constexpr uint32_t switch_bit(uint32_t index)
The input bit mask for a given user switch index.
Definition
platform-gpio.hh:112
Generated by
1.18.0