-
Notifications
You must be signed in to change notification settings - Fork 270
/
gpio.h
159 lines (132 loc) · 3.99 KB
/
gpio.h
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
/* Copyright (C) 2014-2020 by Jacob Alexander
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once
// ----- Includes -----
// Compiler Includes
#include <stdbool.h>
#include <stdint.h>
// Project Includes
#include <Lib/mcu_compat.h>
#if defined(_sam_)
#include <sam/drivers/pio/pio.h>
#endif
// ----- Defines -----
// ----- Macros -----
#define gpio( port, pin ) { GPIO_Port_##port, GPIO_Pin_##pin }
#define periph_io( port, pin, periph ) { GPIO_Port_##port, GPIO_Pin_##pin, GPIO_Peripheral_##periph }
// ----- Enums -----
// GPIO ports
typedef enum GPIO_Port {
GPIO_Port_A = 0,
GPIO_Port_B = 1,
GPIO_Port_C = 2,
GPIO_Port_D = 3,
GPIO_Port_E = 4,
GPIO_Port_F = 5,
} GPIO_Port;
// Each port has a possible 32 pins
typedef enum GPIO_Pin_Num {
GPIO_Pin_0 = 0,
GPIO_Pin_1 = 1,
GPIO_Pin_2 = 2,
GPIO_Pin_3 = 3,
GPIO_Pin_4 = 4,
GPIO_Pin_5 = 5,
GPIO_Pin_6 = 6,
GPIO_Pin_7 = 7,
GPIO_Pin_8 = 8,
GPIO_Pin_9 = 9,
GPIO_Pin_10 = 10,
GPIO_Pin_11 = 11,
GPIO_Pin_12 = 12,
GPIO_Pin_13 = 13,
GPIO_Pin_14 = 14,
GPIO_Pin_15 = 15,
GPIO_Pin_16 = 16,
GPIO_Pin_17 = 17,
GPIO_Pin_18 = 18,
GPIO_Pin_19 = 19,
GPIO_Pin_20 = 20,
GPIO_Pin_21 = 21,
GPIO_Pin_22 = 22,
GPIO_Pin_23 = 23,
GPIO_Pin_24 = 24,
GPIO_Pin_25 = 25,
GPIO_Pin_26 = 26,
GPIO_Pin_27 = 27,
GPIO_Pin_28 = 28,
GPIO_Pin_29 = 29,
GPIO_Pin_30 = 30,
GPIO_Pin_31 = 31,
} GPIO_Pin_Num;
// Type of pin
typedef enum GPIO_Type {
GPIO_Type_DriveHigh,
GPIO_Type_DriveLow,
GPIO_Type_DriveToggle,
GPIO_Type_DriveSetup,
GPIO_Type_Read,
GPIO_Type_ReadSetup,
} GPIO_Type;
// Sense/Strobe configuration
typedef enum GPIO_Config {
GPIO_Config_None = 0, // No configuration (ok for drive high/low)
GPIO_Config_Pullup = 1, // Internal pull-up
GPIO_Config_Pulldown = 2, // Internal pull-down
GPIO_Config_Pulloff = 3, // Disable both pull resistors (used with inputs), equivalent to N/C
GPIO_Config_Opendrain = 4, // External pull resistor
} GPIO_Config;
// Peripheral selection
typedef enum GPIO_Peripheral {
#if defined(_sam_)
GPIO_Peripheral_A = PIO_PERIPH_A,
GPIO_Peripheral_B = PIO_PERIPH_B,
GPIO_Peripheral_C = PIO_PERIPH_C,
GPIO_Peripheral_D = PIO_PERIPH_D,
GPIO_Peripheral_Input = PIO_INPUT,
GPIO_Peripheral_Output0 = PIO_OUTPUT_0,
GPIO_Peripheral_Output1 = PIO_OUTPUT_1,
#else
GPIO_Peripheral_A,
GPIO_Peripheral_B,
GPIO_Peripheral_C,
#endif
} GPIO_Peripheral;
// ----- Structs -----
// Struct container for defining Rows (Sense) and Columns (Strobes)
typedef struct GPIO_Pin {
GPIO_Port port;
GPIO_Pin_Num pin;
} GPIO_Pin;
// Struct container for configuring a peripheral
typedef struct GPIO_ConfigPin {
GPIO_Port port;
GPIO_Pin_Num pin;
GPIO_Peripheral peripheral;
} GPIO_ConfigPin;
// ----- Functions -----
uint8_t GPIO_Ctrl(GPIO_Pin gpio, GPIO_Type type, GPIO_Config config);
void PIO_Setup(GPIO_ConfigPin config);
void GPIO_IrqSetup(GPIO_Pin gpio, uint8_t enable);
void Reset_AssertExternal(uint8_t length, bool wait);
void Reset_CleanupExternal();
void Reset_DisableExternal();
void Reset_FullReset();