Debian Codegraph @

ports/renesas-ra/ra/ra_gpio.h

raw
1 /*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2021,2022 Renesas Electronics Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #ifndef RA_RA_GPIO_H_
26 #define RA_RA_GPIO_H_
27
28 // clang-format off
29
30 #include <stdint.h>
31
32 typedef struct ra_af_pin {
33 uint8_t af;
34 uint8_t ch;
35 uint32_t pin;
36 } ra_af_pin_t;
37
38 enum CPU_PIN {
39 P000 = 0x00, P001, P002, P003, P004, P005, P006, P007, P008, P009, P010, P011, P012, P013, P014, P015,
40 P100 = 0x10, P101, P102, P103, P104, P105, P106, P107, P108, P109, P110, P111, P112, P113, P114, P115,
41 P200 = 0x20, P201, P202, P203, P204, P205, P206, P207, P208, P209, P210, P211, P212, P213, P214, P215,
42 P300 = 0x30, P301, P302, P303, P304, P305, P306, P307, P308, P309, P310, P311, P312, P313, P314, P315,
43 P400 = 0x40, P401, P402, P403, P404, P405, P406, P407, P408, P409, P410, P411, P412, P413, P414, P415,
44 P500 = 0x50, P501, P502, P503, P504, P505, P506, P507, P508, P509, P510, P511, P512, P513, P514, P515,
45 P600 = 0x60, P601, P602, P603, P604, P605, P606, P607, P608, P609, P610, P611, P612, P613, P614, P615,
46 P700 = 0x70, P701, P702, P703, P704, P705, P706, P707, P708, P709, P710, P711, P712, P713, P714, P715,
47 P800 = 0x80, P801, P802, P803, P804, P805, P806, P807, P808, P809, P810, P811, P812, P813, P814, P815,
48 P900 = 0x90, P901, P902, P903, P904, P905, P906, P907, P908, P909, P910, P911, P912, P913, P914, P915,
49 PA00 = 0xA0, PA01, PA02, PA03, PA04, PA05, PA06, PA07, PA08, PA09, PA10, PA11, PA12, PA13, PA14, PA15,
50 PB00 = 0xB0, PB01, PB02, PB03, PB04, PB05, PB06, PB07, PB08, PB09, PB10, PB11, PB12, PB13, PB14, PB15,
51 PIN_END = 0xff,
52 };
53
54 enum AF_INDEX {
55 AF_GPIO = 0,
56 AF_AGT = 1,
57 AF_GPT1 = 2,
58 AF_GPT2 = 3,
59 AF_SCI1 = 4,
60 AF_SCI2 = 5,
61 AF_SPI = 6,
62 AF_I2C = 7,
63 AF_KINT = 8,
64 AF_RTC = 9,
65 AF_CAC = 10,
66 AF_CTSU = 12,
67 AF_SLCDC = 13,
68 AF_CAN = 16,
69 AF_SSIE = 18,
70 AF_USBFS = 19,
71 AF_END = 0xff,
72 };
73
74 #define GPIO_MODE_INPUT 0
75 #define GPIO_MODE_OUTPUT_PP 1
76 #define GPIO_MODE_OUTPUT_OD 2
77 #define GPIO_MODE_AF_PP 3
78 #define GPIO_MODE_AF_OD 4
79 #define GPIO_MODE_ANALOG 5
80
81 #define GPIO_IRQ_FALLING 0x1
82 #define GPIO_IRQ_RISING 0x2
83 #define GPIO_IRQ_LOWLEVEL 0x4
84 #define GPIO_IRQ_HIGHLEVEL 0x8
85
86 #define GPIO_NOPULL 0
87 #define GPIO_PULLUP 1
88 #define GPIO_PULLDOWN 2
89
90 #define GPIO_LOW_POWER 0
91 #define GPIO_MID_POWER 1
92 #define GPIO_MID_FAST_POWER 2
93 #define GPIO_HIGH_POWER 3
94
95 #define IS_GPIO_MODE(MODE) (((MODE) == GPIO_MODE_INPUT) || \
96 ((MODE) == GPIO_MODE_OUTPUT_PP) || \
97 ((MODE) == GPIO_MODE_OUTPUT_OD) || \
98 ((MODE) == GPIO_MODE_AF_PP) || \
99 ((MODE) == GPIO_MODE_AF_OD) || \
100 ((MODE) == GPIO_MODE_ANALOG))
101
102 #define IS_GPIO_DRIVE(DRIVE) (((DRIVE) == GPIO_LOW_POWER) || \
103 ((DRIVE) == GPIO_MID_POWER) || \
104 ((DRIVE) == GPIO_MID_FAST_POWER) || \
105 ((DRIVE) == GPIO_HIGH_POWER))
106
107 #define IS_GPIO_PULL(PULL) (((PULL) == GPIO_NOPULL) || \
108 ((PULL) == GPIO_PULLUP) || \
109 ((PULL) == GPIO_PULLDOWN))
110
111 #define IS_GPIO_AF(AF) ((AF) <= (uint8_t)0x1F)
112
113 #define GPIO_PORT(pin) ((pin) >> 4)
114 #define GPIO_MASK(pin) (1 << ((pin) & 0xf))
115 #define GPIO_BIT(pin) ((pin) & 0xf)
116
117 #define PODR_MASK (uint32_t)0x00000001
118 #define PIDR_MASK (uint32_t)0x00000002
119 #define PDR_MASK (uint32_t)0x00000004
120 #define PCR_MASK (uint32_t)0x00000010
121 #define NCODR_MASK (uint32_t)0x00000040
122 #define DSCR_MASK (uint32_t)0x00000400
123 #define DSCR1_MASK (uint32_t)0x00000800
124 #define EOR_MASK (uint32_t)0x00001000
125 #define EOF_MASK (uint32_t)0x00002000
126 #define ISEL_MASK (uint32_t)0x00004000
127 #define ASEL_MASK (uint32_t)0x00008000
128 #define PMR_MASK (uint32_t)0x00010000
129 #define PSEL_MASK (uint32_t)0x1f000000
130
131 #define _PWPR (*(volatile uint8_t *)(R_PFS_BASE + 0x503))
132
133 #define _PXXPFS(port, bit) (*(volatile uint32_t *)(R_PFS_BASE + (0x40 * ((uint32_t)port)) + (0x4 * ((uint32_t)bit))))
134 #define _PCNTR1(port) (*(volatile uint32_t *)(R_PORT0_BASE + (0x20 * (port))))
135 #define _PODR(port) (*(volatile uint16_t *)(R_PORT0_BASE + (0x20 * (port))))
136 #define _PDR(port) (*(volatile uint16_t *)(R_PORT0_BASE + 0x2 + (0x20 * (port))))
137 #define _PCNTR2(port) (*(volatile uint32_t *)(R_PORT0_BASE + 0x4 + (0x20 * (port))))
138 #define _EIDR(port) (*(volatile uint16_t *)(R_PORT0_BASE + 0x4 + (0x20 * (port))))
139 #define _PIDR(port) (*(volatile uint16_t *)(R_PORT0_BASE + 0x6 + (0x20 * (port))))
140 #define _PCNTR3(port) (*(volatile uint32_t *)(R_PORT0_BASE + 0x8 + (0x20 * (port))))
141 #define _PORR(port) (*(volatile uint16_t *)(R_PORT0_BASE + 0x8 + (0x20 * (port))))
142 #define _POSR(port) (*(volatile uint16_t *)(R_PORT0_BASE + 0xa + (0x20 * (port))))
143 #define _PCNTR4(port) (*(volatile uint32_t *)(R_PORT0_BASE + 0xc + (0x20 * (port))))
144 #define _EORR(port) (*(volatile uint16_t *)(R_PORT0_BASE + 0xc + (0x20 * (port))))
145 #define _EOSR(port) (*(volatile uint16_t *)(R_PORT0_BASE + 0xe + (0x20 * (port))))
146
147 #define _PPXXPFS(port, bit) ((volatile uint32_t *)(R_PFS_BASE + (0x40 * (port)) + (0x4 * (bit))))
148 #define _PPCNTR1(port) ((volatile uint32_t *)(R_PORT0_BASE + (0x20 * (port))))
149 #define _PPODR(port) ((volatile uint16_t *)(R_PORT0_BASE + (0x20 * (port))))
150 #define _PPDR(port) ((volatile uint16_t *)(R_PORT0_BASE + 0x2 + (0x20 * (port))))
151 #define _PPCNTR2(port) ((volatile uint32_t *)(R_PORT0_BASE + 0x4 + (0x20 * (port))))
152 #define _PEIDR(port) ((volatile uint16_t *)(R_PORT0_BASE + 0x4 + (0x20 * (port))))
153 #define _PPIDR(port) ((volatile uint16_t *)(R_PORT0_BASE + 0x6 + (0x20 * (port))))
154 #define _PPCNTR3(port) ((volatile uint32_t *)(R_PORT0_BASE + 0x8 + (0x20 * (port))))
155 #define _PPORR(port) ((volatile uint16_t *)(R_PORT0_BASE + 0x8 + (0x20 * (port))))
156 #define _PPOSR(port) ((volatile uint16_t *)(R_PORT0_BASE + 0xa + (0x20 * (port))))
157 #define _PPCNTR4(port) ((volatile uint32_t *)(R_PORT0_BASE + 0xc + (0x20 * (port))))
158 #define _PEORR(port) ((volatile uint16_t *)(R_PORT0_BASE + 0xc + (0x20 * (port))))
159 #define _PEOSR(port) ((volatile uint16_t *)(R_PORT0_BASE + 0xe + (0x20 * (port))))
160
161 void ra_gpio_config(uint32_t pin, uint32_t mode, uint32_t pull, uint32_t drive, uint32_t alt);
162 void ra_gpio_mode_output(uint32_t pin);
163 void ra_gpio_mode_input(uint32_t pin);
164 void ra_gpio_set(uint32_t pin, uint32_t value);
165 uint32_t ra_gpio_get(uint32_t pin);
166 void ra_gpio_toggle(uint32_t pin);
167 void ra_gpio_write(uint32_t pin, uint32_t value);
168 uint32_t ra_gpio_read(uint32_t pin);
169 uint32_t ra_gpio_get_mode(uint32_t pin);
170 uint32_t ra_gpio_get_pull(uint32_t pin);
171 uint32_t ra_gpio_get_af(uint32_t pin);
172 uint32_t ra_gpio_get_drive(uint32_t pin);
173
174 inline static void pwpr_unprotect(void) {
175 _PWPR &= (uint8_t) ~0x80;
176 _PWPR |= (uint8_t)0x40;
177 }
178
179 inline static void pwpr_protect(void) {
180 _PWPR &= (uint8_t) ~0x40;
181 _PWPR |= (uint8_t)0x80;
182 }
183
184 #endif /* RA_RA_GPIO_H_ */