1
- /* *
2
- ******************************************************************************
3
- * @file gpio_hal.c
4
- * @authors Matthew McGowan
5
- * @version V1.0.0
6
- * @date 27-Sept-2014
7
- * @brief
8
- ******************************************************************************
9
- Copyright (c) 2013-2015 Particle Industries, Inc. All rights reserved.
10
-
11
- This library is free software; you can redistribute it and/or
12
- modify it under the terms of the GNU Lesser General Public
13
- License as published by the Free Software Foundation, either
14
- version 3 of the License, or (at your option) any later version.
15
-
16
- This library is distributed in the hope that it will be useful,
17
- but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
- Lesser General Public License for more details.
20
-
21
- You should have received a copy of the GNU Lesser General Public
22
- License along with this library; if not, see <http://www.gnu.org/licenses/>.
23
- ******************************************************************************
1
+ /*
2
+ * Copyright (c) 2018 Particle Industries, Inc. All rights reserved.
3
+ *
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation, either
7
+ * version 3 of the License, or (at your option) any later version.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24
16
*/
25
17
26
- /* Includes ----------------------------------------------------------------- */
18
+ # include " nrf_gpio.h "
27
19
#include " gpio_hal.h"
20
+ #include " pinmap_impl.h"
21
+ #include " hw_ticks.h"
22
+ #include < stddef.h>
28
23
29
- /* Private typedef ----------------------------------------------------------*/
30
-
31
- /* Private define -----------------------------------------------------------*/
32
-
33
- /* Private macro ------------------------------------------------------------*/
34
-
35
- /* Private variables --------------------------------------------------------*/
36
-
37
- /* Extern variables ---------------------------------------------------------*/
38
-
39
- /* Private function prototypes ----------------------------------------------*/
24
+ inline bool is_valid_pin (pin_t pin) __attribute__((always_inline));
25
+ inline bool is_valid_pin (pin_t pin)
26
+ {
27
+ return pin < TOTAL_PINS;
28
+ }
40
29
41
30
PinMode HAL_Get_Pin_Mode (pin_t pin)
42
31
{
43
- return PIN_MODE_NONE;
32
+ return (! is_valid_pin (pin)) ? PIN_MODE_NONE : HAL_Pin_Map ()[pin]. pin_mode ;
44
33
}
45
34
46
35
PinFunction HAL_Validate_Pin_Function (pin_t pin, PinFunction pinFunction)
47
36
{
37
+ NRF5x_Pin_Info* PIN_MAP = HAL_Pin_Map ();
38
+
39
+ if (!is_valid_pin (pin))
40
+ return PF_NONE;
41
+ if (pinFunction==PF_ADC && PIN_MAP[pin].adc_channel != ADC_CHANNEL_NONE)
42
+ return PF_ADC;
43
+ // Compatible with STM32 for wiring layer
44
+ if (pinFunction==PF_TIMER && PIN_MAP[pin].pwm_instance != PWM_INSTANCE_NONE)
45
+ return PF_TIMER;
48
46
return PF_DIO;
49
47
}
50
48
@@ -54,34 +52,168 @@ PinFunction HAL_Validate_Pin_Function(pin_t pin, PinFunction pinFunction)
54
52
*/
55
53
void HAL_Pin_Mode (pin_t pin, PinMode setMode)
56
54
{
55
+ if (!is_valid_pin (pin))
56
+ {
57
+ return ;
58
+ }
59
+
60
+ NRF5x_Pin_Info* PIN_MAP = HAL_Pin_Map ();
61
+ uint32_t gpio_pin_map = NRF_GPIO_PIN_MAP (PIN_MAP[pin].gpio_port , PIN_MAP[pin].gpio_pin );
62
+
63
+ switch (setMode)
64
+ {
65
+ case OUTPUT:
66
+ nrf_gpio_cfg_output (gpio_pin_map);
67
+ PIN_MAP[pin].pin_mode = OUTPUT;
68
+ PIN_MAP[pin].pin_func = PF_DIO;
69
+ break ;
70
+
71
+ case INPUT:
72
+ nrf_gpio_cfg_input (gpio_pin_map, NRF_GPIO_PIN_NOPULL);
73
+ PIN_MAP[pin].pin_mode = INPUT;
74
+ PIN_MAP[pin].pin_func = PF_DIO;
75
+ break ;
76
+
77
+ case INPUT_PULLUP:
78
+ nrf_gpio_cfg_input (gpio_pin_map, NRF_GPIO_PIN_PULLUP);
79
+ PIN_MAP[pin].pin_mode = INPUT_PULLUP;
80
+ PIN_MAP[pin].pin_func = PF_DIO;
81
+ break ;
82
+
83
+ case INPUT_PULLDOWN:
84
+ nrf_gpio_cfg_input (gpio_pin_map, NRF_GPIO_PIN_PULLDOWN);
85
+ PIN_MAP[pin].pin_mode = INPUT_PULLDOWN;
86
+ PIN_MAP[pin].pin_func = PF_DIO;
87
+ break ;
88
+
89
+ case PIN_MODE_NONE:
90
+ nrf_gpio_cfg_default (gpio_pin_map);
91
+ PIN_MAP[pin].pin_func = PF_NONE;
92
+ default :
93
+ break ;
94
+ }
95
+
96
+
57
97
}
58
98
59
99
/*
60
100
* @brief Saves a pin mode to be recalled later.
61
101
*/
62
- void HAL_GPIO_Save_Pin_Mode (PinMode mode )
102
+ void HAL_GPIO_Save_Pin_Mode (uint16_t pin )
63
103
{
104
+ // deprecated
64
105
}
65
106
66
107
/*
67
108
* @brief Recalls a saved pin mode.
68
109
*/
69
- PinMode HAL_GPIO_Recall_Pin_Mode ()
110
+ PinMode HAL_GPIO_Recall_Pin_Mode (uint16_t pin )
70
111
{
71
- return PIN_MODE_NONE;
112
+ // deprecated
113
+ return PIN_MODE_NONE;
72
114
}
73
115
74
116
/*
75
117
* @brief Sets a GPIO pin to HIGH or LOW.
76
118
*/
77
119
void HAL_GPIO_Write (uint16_t pin, uint8_t value)
78
120
{
121
+ if (!is_valid_pin (pin))
122
+ {
123
+ return ;
124
+ }
125
+
126
+ NRF5x_Pin_Info* PIN_MAP = HAL_Pin_Map ();
127
+ uint32_t gpio_pin_map = NRF_GPIO_PIN_MAP (PIN_MAP[pin].gpio_port , PIN_MAP[pin].gpio_pin );
128
+
129
+ if (value == 0 )
130
+ {
131
+ nrf_gpio_pin_clear (gpio_pin_map);
132
+ }
133
+ else
134
+ {
135
+ nrf_gpio_pin_set (gpio_pin_map);
136
+ }
79
137
}
80
138
81
139
/*
82
140
* @brief Reads the value of a GPIO pin. Should return either 1 (HIGH) or 0 (LOW).
83
141
*/
84
142
int32_t HAL_GPIO_Read (uint16_t pin)
85
143
{
86
- return 0 ;
144
+ if (!is_valid_pin (pin))
145
+ {
146
+ return 0 ;
147
+ }
148
+
149
+ NRF5x_Pin_Info* PIN_MAP = HAL_Pin_Map ();
150
+ uint32_t gpio_pin_map = NRF_GPIO_PIN_MAP (PIN_MAP[pin].gpio_port , PIN_MAP[pin].gpio_pin );
151
+
152
+ if ((PIN_MAP[pin].pin_mode == INPUT) ||
153
+ (PIN_MAP[pin].pin_mode == INPUT_PULLUP) ||
154
+ (PIN_MAP[pin].pin_mode == INPUT_PULLDOWN))
155
+ {
156
+ return nrf_gpio_pin_read (gpio_pin_map);
157
+ }
158
+ else
159
+ {
160
+ return 0 ;
161
+ }
162
+ }
163
+
164
+ /*
165
+ * @brief blocking call to measure a high or low pulse
166
+ * @returns uint32_t pulse width in microseconds up to 3 seconds,
167
+ * returns 0 on 3 second timeout error, or invalid pin.
168
+ */
169
+ uint32_t HAL_Pulse_In (pin_t pin, uint16_t value)
170
+ {
171
+ if (!is_valid_pin (pin))
172
+ {
173
+ return 0 ;
174
+ }
175
+
176
+ volatile uint32_t timeout_start = GetSystem1MsTick ();
177
+
178
+ HAL_Pin_Mode (pin, INPUT);
179
+
180
+ /* If already on the value we want to measure, wait for the next one.
181
+ * Time out after 3 seconds so we don't block the background tasks
182
+ */
183
+ while (HAL_GPIO_Read (pin) == value)
184
+ {
185
+ if (GetSystem1MsTick () - timeout_start > 3000 )
186
+ {
187
+ return 0 ;
188
+ }
189
+ }
190
+
191
+ /* Wait until the start of the pulse.
192
+ * Time out after 3 seconds so we don't block the background tasks
193
+ */
194
+ while (HAL_GPIO_Read (pin) != value)
195
+ {
196
+ if (GetSystem1MsTick () - timeout_start > 3000 )
197
+ {
198
+ return 0 ;
199
+ }
200
+ }
201
+
202
+ /* Wait until this value changes, this will be our elapsed pulse width.
203
+ * Time out after 3 seconds so we don't block the background tasks
204
+ */
205
+ volatile uint32_t pulse_start = GetSystem1MsTick ();
206
+ while (HAL_GPIO_Read (pin) == value)
207
+ {
208
+ if (GetSystem1MsTick () - timeout_start > 3000 )
209
+ {
210
+ return 0 ;
211
+ }
212
+ }
213
+
214
+ return GetSystem1MsTick () - pulse_start;
87
215
}
216
+
217
+
218
+
219
+
0 commit comments