Debian Codegraph @

ports/renesas-ra/ra/ra_gpio.c

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 #include <stdint.h>
26 #include "hal_data.h"
27 #include "ra_config.h"
28 #include "ra_gpio.h"
29
30 void ra_gpio_config(uint32_t pin, uint32_t mode, uint32_t pull, uint32_t drive, uint32_t alt) {
31 uint32_t port = GPIO_PORT(pin);
32 uint32_t bit = GPIO_BIT(pin);
33 pwpr_unprotect();
34 _PXXPFS(port, bit) &= ~(PMR_MASK | ASEL_MASK | NCODR_MASK | PCR_MASK | PDR_MASK | DSCR1_MASK | DSCR_MASK);
35 switch (mode) {
36 case GPIO_MODE_INPUT:
37 if (pull == GPIO_PULLUP) {
38 _PXXPFS(port, bit) |= PCR_MASK; // set pullup
39 }
40 break;
41 case GPIO_MODE_OUTPUT_PP:
42 _PXXPFS(port, bit) |= PDR_MASK; // output
43 break;
44 case GPIO_MODE_OUTPUT_OD:
45 _PXXPFS(port, bit) |= (PDR_MASK | NCODR_MASK);
46 break;
47 case GPIO_MODE_AF_PP:
48 _PXXPFS(port, bit) |= (PMR_MASK | PDR_MASK);
49 break;
50 case GPIO_MODE_AF_OD:
51 _PXXPFS(port, bit) |= (PMR_MASK | PDR_MASK | NCODR_MASK);
52 break;
53 case GPIO_MODE_ANALOG:
54 _PXXPFS(port, bit) |= ASEL_MASK;
55 break;
56 }
57 switch (drive) {
58 case GPIO_HIGH_POWER:
59 _PXXPFS(port, bit) |= (DSCR1_MASK | DSCR_MASK);
60 break;
61 case GPIO_MID_FAST_POWER:
62 _PXXPFS(port, bit) |= DSCR1_MASK;
63 break;
64 case GPIO_MID_POWER:
65 _PXXPFS(port, bit) |= DSCR_MASK;
66 break;
67 case GPIO_LOW_POWER:
68 default:
69 /* Bits are already cleared */
70 break;
71 }
72 _PXXPFS(port, bit) &= ~(uint32_t)(0x1f000000);
73 if (alt != 0) {
74 _PXXPFS(port, bit) |= (alt << 24); // Must set PSEL when PMR is 0
75 _PXXPFS(port, bit) |= PMR_MASK;
76 }
77 pwpr_protect();
78 }
79
80 void ra_gpio_mode_output(uint32_t pin) {
81 uint32_t port = GPIO_PORT(pin);
82 uint32_t bit = GPIO_BIT(pin);
83 pwpr_unprotect();
84 _PXXPFS(port, bit) &= ~(PMR_MASK | ASEL_MASK | PCR_MASK); // GPIO
85 _PXXPFS(port, bit) |= PDR_MASK; // output
86 pwpr_protect();
87 }
88
89 void ra_gpio_mode_input(uint32_t pin) {
90 uint32_t port = GPIO_PORT(pin);
91 uint32_t bit = GPIO_BIT(pin);
92 pwpr_unprotect();
93 _PXXPFS(port, bit) &= ~(PMR_MASK | ASEL_MASK); // GPIO
94 _PXXPFS(port, bit) &= ~PDR_MASK; // input
95 pwpr_protect();
96 }
97
98 void ra_gpio_toggle(uint32_t pin) {
99 uint32_t port = GPIO_PORT(pin);
100 uint32_t bit = GPIO_BIT(pin);
101 pwpr_unprotect();
102 _PXXPFS(port, bit) ^= 1;
103 pwpr_protect();
104 }
105
106 void ra_gpio_write(uint32_t pin, uint32_t value) {
107 uint32_t port = GPIO_PORT(pin);
108 uint32_t bit = GPIO_BIT(pin);
109 pwpr_unprotect();
110 if (value != 0) {
111 _PXXPFS(port, bit) |= 1;
112 } else {
113 _PXXPFS(port, bit) &= ~(uint32_t)1;
114 }
115 pwpr_protect();
116 }
117
118 uint32_t ra_gpio_read(uint32_t pin) {
119 uint32_t port = GPIO_PORT(pin);
120 uint32_t bit = GPIO_BIT(pin);
121 return ((_PXXPFS(port, bit) & PIDR_MASK) != 0) ? 1 : 0;
122 }
123
124 uint32_t ra_gpio_get_mode(uint32_t pin) {
125 uint8_t mode = 0;
126 uint32_t pfs = _PXXPFS(GPIO_PORT(pin), GPIO_BIT(pin));
127 if ((pfs & ASEL_MASK) != 0) {
128 mode = GPIO_MODE_ANALOG;
129 } else if ((pfs & PMR_MASK) != 0) {
130 if ((pfs & NCODR_MASK) != 0) {
131 mode = GPIO_MODE_AF_OD;
132 } else {
133 mode = GPIO_MODE_AF_PP;
134 }
135 } else if ((pfs & PDR_MASK) != 0) {
136 if ((pfs & NCODR_MASK) != 0) {
137 mode = GPIO_MODE_OUTPUT_OD;
138 } else {
139 mode = GPIO_MODE_OUTPUT_PP;
140 }
141 } else {
142 mode = GPIO_MODE_INPUT;
143 }
144 return mode;
145 }
146
147 uint32_t ra_gpio_get_pull(uint32_t pin) {
148 uint8_t pull = 0;
149 uint32_t port = GPIO_PORT(pin);
150 uint32_t bit = GPIO_BIT(pin);
151 if ((_PXXPFS(port, bit) & PCR_MASK) != 0) {
152 pull = GPIO_PULLUP;
153 } else {
154 pull = GPIO_NOPULL;
155 }
156 return pull;
157 }
158
159 uint32_t ra_gpio_get_af(uint32_t pin) {
160 uint32_t port = GPIO_PORT(pin);
161 uint32_t bit = GPIO_BIT(pin);
162 return (_PXXPFS(port, bit) & PMR_MASK) != 0;
163 }
164
165 uint32_t ra_gpio_get_drive(uint32_t pin) {
166 uint8_t drive = 0;
167 uint32_t port = GPIO_PORT(pin);
168 uint32_t bit = GPIO_BIT(pin);
169 switch (_PXXPFS(port, bit) & (DSCR1_MASK | DSCR_MASK)) {
170 case (DSCR1_MASK | DSCR_MASK):
171 drive = GPIO_HIGH_POWER;
172 break;
173 case DSCR1_MASK:
174 drive = GPIO_MID_FAST_POWER;
175 break;
176 case DSCR_MASK:
177 drive = GPIO_MID_POWER;
178 break;
179 case 0:
180 default:
181 drive = GPIO_LOW_POWER;
182 break;
183 }
184 return drive;
185 }