before merging master
[inav.git] / lib / main / AT32F43x / Drivers / AT32F43x_StdPeriph_Driver / src / at32f435_437_gpio.c
blobcb62c5eb73d982ef158f3ce262a271fb55190d95
1 /**
2 **************************************************************************
3 * @file at32f435_437_gpio.c
4 * @version v2.1.0
5 * @date 2022-08-16
6 * @brief contains all the functions for the gpio firmware library
7 **************************************************************************
8 * Copyright notice & Disclaimer
10 * The software Board Support Package (BSP) that is made available to
11 * download from Artery official website is the copyrighted work of Artery.
12 * Artery authorizes customers to use, copy, and distribute the BSP
13 * software and its related documentation for the purpose of design and
14 * development in conjunction with Artery microcontrollers. Use of the
15 * software is governed by this copyright notice and the following disclaimer.
17 * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
18 * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
19 * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
20 * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
21 * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 **************************************************************************
27 #include "at32f435_437_conf.h"
29 /** @addtogroup AT32F435_437_periph_driver
30 * @{
33 /** @defgroup GPIO
34 * @brief GPIO driver modules
35 * @{
38 #ifdef GPIO_MODULE_ENABLED
40 /** @defgroup GPIO_private_functions
41 * @{
44 /**
45 * @brief reset the gpio register
46 * @param gpio_x: to select the gpio peripheral.
47 * this parameter can be one of the following values:
48 * GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH.
49 * @retval none
51 void gpio_reset(gpio_type *gpio_x)
53 if(gpio_x == GPIOA)
55 crm_periph_reset(CRM_GPIOA_PERIPH_RESET, TRUE);
56 crm_periph_reset(CRM_GPIOA_PERIPH_RESET, FALSE);
58 else if(gpio_x == GPIOB)
60 crm_periph_reset(CRM_GPIOB_PERIPH_RESET, TRUE);
61 crm_periph_reset(CRM_GPIOB_PERIPH_RESET, FALSE);
63 else if(gpio_x == GPIOC)
65 crm_periph_reset(CRM_GPIOC_PERIPH_RESET, TRUE);
66 crm_periph_reset(CRM_GPIOC_PERIPH_RESET, FALSE);
68 else if(gpio_x == GPIOD)
70 crm_periph_reset(CRM_GPIOD_PERIPH_RESET, TRUE);
71 crm_periph_reset(CRM_GPIOD_PERIPH_RESET, FALSE);
73 else if(gpio_x == GPIOE)
75 crm_periph_reset(CRM_GPIOE_PERIPH_RESET, TRUE);
76 crm_periph_reset(CRM_GPIOE_PERIPH_RESET, FALSE);
78 else if(gpio_x == GPIOF)
80 crm_periph_reset(CRM_GPIOF_PERIPH_RESET, TRUE);
81 crm_periph_reset(CRM_GPIOF_PERIPH_RESET, FALSE);
83 else if(gpio_x == GPIOG)
85 crm_periph_reset(CRM_GPIOG_PERIPH_RESET, TRUE);
86 crm_periph_reset(CRM_GPIOG_PERIPH_RESET, FALSE);
88 else if(gpio_x == GPIOH)
90 crm_periph_reset(CRM_GPIOH_PERIPH_RESET, TRUE);
91 crm_periph_reset(CRM_GPIOH_PERIPH_RESET, FALSE);
95 /**
96 * @brief initialize the gpio peripheral.
97 * @param gpio_x: to select the gpio peripheral.
98 * this parameter can be one of the following values:
99 * GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH.
100 * @param gpio_init_struct: pointer to gpio init structure.
101 * @retval none
103 void gpio_init(gpio_type *gpio_x, gpio_init_type *gpio_init_struct)
105 uint16_t pinx_value, pin_index = 0;
107 pinx_value = (uint16_t)gpio_init_struct->gpio_pins;
109 while(pinx_value > 0)
111 if(pinx_value & 0x01)
113 gpio_x->cfgr &= (uint32_t)~(0x03 << (pin_index * 2));
114 gpio_x->cfgr |= (uint32_t)(gpio_init_struct->gpio_mode << (pin_index * 2));
116 gpio_x->omode &= (uint32_t)~(0x01 << (pin_index));
117 gpio_x->omode |= (uint32_t)(gpio_init_struct->gpio_out_type << (pin_index));
119 gpio_x->odrvr &= (uint32_t)~(0x03 << (pin_index * 2));
120 gpio_x->odrvr |= (uint32_t)(gpio_init_struct->gpio_drive_strength << (pin_index * 2));
122 gpio_x->pull &= (uint32_t)~(0x03 << (pin_index * 2));
123 gpio_x->pull |= (uint32_t)(gpio_init_struct->gpio_pull << (pin_index * 2));
125 pinx_value >>= 1;
126 pin_index++;
131 * @brief fill each gpio_init_type member with its default value.
132 * @param gpio_init_struct : pointer to a gpio_init_type structure which will be initialized.
133 * @retval none
135 void gpio_default_para_init(gpio_init_type *gpio_init_struct)
137 /* reset gpio init structure parameters values */
138 gpio_init_struct->gpio_pins = GPIO_PINS_ALL;
139 gpio_init_struct->gpio_mode = GPIO_MODE_INPUT;
140 gpio_init_struct->gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
141 gpio_init_struct->gpio_pull = GPIO_PULL_NONE;
142 gpio_init_struct->gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
146 * @brief read the specified input port pin.
147 * @param gpio_x: to select the gpio peripheral.
148 * this parameter can be one of the following values:
149 * GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH.
150 * @param pins: gpio pin number
151 * this parameter can be one of the following values:
152 * - GPIO_PINS_0
153 * - GPIO_PINS_1
154 * - GPIO_PINS_2
155 * - GPIO_PINS_3
156 * - GPIO_PINS_4
157 * - GPIO_PINS_5
158 * - GPIO_PINS_6
159 * - GPIO_PINS_7
160 * - GPIO_PINS_8
161 * - GPIO_PINS_9
162 * - GPIO_PINS_10
163 * - GPIO_PINS_11
164 * - GPIO_PINS_12
165 * - GPIO_PINS_13
166 * - GPIO_PINS_14
167 * - GPIO_PINS_15
168 * @retval flag_status (SET or RESET)
170 flag_status gpio_input_data_bit_read(gpio_type *gpio_x, uint16_t pins)
172 flag_status status = RESET;
174 if(pins != (pins & gpio_x->idt))
176 status = RESET;
178 else
180 status = SET;
183 return status;
187 * @brief read the specified gpio input data port.
188 * @param gpio_x: to select the gpio peripheral.
189 * this parameter can be one of the following values:
190 * GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH.
191 * @retval gpio input data port value.
193 uint16_t gpio_input_data_read(gpio_type *gpio_x)
195 return ((uint16_t)(gpio_x->idt));
199 * @brief read the specified output port pin.
200 * @param gpio_x: to select the gpio peripheral.
201 * this parameter can be one of the following values:
202 * GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH.
203 * @param pins: gpio pin number
204 * this parameter can be one of the following values:
205 * - GPIO_PINS_0
206 * - GPIO_PINS_1
207 * - GPIO_PINS_2
208 * - GPIO_PINS_3
209 * - GPIO_PINS_4
210 * - GPIO_PINS_5
211 * - GPIO_PINS_6
212 * - GPIO_PINS_7
213 * - GPIO_PINS_8
214 * - GPIO_PINS_9
215 * - GPIO_PINS_10
216 * - GPIO_PINS_11
217 * - GPIO_PINS_12
218 * - GPIO_PINS_13
219 * - GPIO_PINS_14
220 * - GPIO_PINS_15
221 * @retval flag_status (SET or RESET)
223 flag_status gpio_output_data_bit_read(gpio_type *gpio_x, uint16_t pins)
225 flag_status status = RESET;
227 if((gpio_x->odt & pins) != RESET)
229 status = SET;
231 else
233 status = RESET;
236 return status;
240 * @brief read the specified gpio ouput data port.
241 * @param gpio_x: to select the gpio peripheral.
242 * this parameter can be one of the following values:
243 * GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH.
244 * @retval gpio input data port value.
246 uint16_t gpio_output_data_read(gpio_type *gpio_x)
248 return ((uint16_t)(gpio_x->odt));
252 * @brief set the selected data port bits.
253 * @param gpio_x: to select the gpio peripheral.
254 * this parameter can be one of the following values:
255 * GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH.
256 * @param pins: gpio pin number
257 * parameter can be any combination of gpio_pin_x, gpio_pin_x as following values:
258 * - GPIO_PINS_0
259 * - GPIO_PINS_1
260 * - GPIO_PINS_2
261 * - GPIO_PINS_3
262 * - GPIO_PINS_4
263 * - GPIO_PINS_5
264 * - GPIO_PINS_6
265 * - GPIO_PINS_7
266 * - GPIO_PINS_8
267 * - GPIO_PINS_9
268 * - GPIO_PINS_10
269 * - GPIO_PINS_11
270 * - GPIO_PINS_12
271 * - GPIO_PINS_13
272 * - GPIO_PINS_14
273 * - GPIO_PINS_15
274 * - GPIO_PINS_ALL
275 * @retval none
277 void gpio_bits_set(gpio_type *gpio_x, uint16_t pins)
279 gpio_x->scr = pins;
283 * @brief clear the selected data port bits.
284 * @param gpio_x: to select the gpio peripheral.
285 * this parameter can be one of the following values:
286 * GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH.
287 * @param pins: gpio pin number
288 * parameter can be any combination of gpio_pin_x, gpio_pin_x as following values:
289 * - GPIO_PINS_0
290 * - GPIO_PINS_1
291 * - GPIO_PINS_2
292 * - GPIO_PINS_3
293 * - GPIO_PINS_4
294 * - GPIO_PINS_5
295 * - GPIO_PINS_6
296 * - GPIO_PINS_7
297 * - GPIO_PINS_8
298 * - GPIO_PINS_9
299 * - GPIO_PINS_10
300 * - GPIO_PINS_11
301 * - GPIO_PINS_12
302 * - GPIO_PINS_13
303 * - GPIO_PINS_14
304 * - GPIO_PINS_15
305 * - GPIO_PINS_ALL
306 * @retval none
308 void gpio_bits_reset(gpio_type *gpio_x, uint16_t pins)
310 gpio_x->clr = pins;
314 * @brief set or clear the selected data port bit.
315 * @param gpio_x: to select the gpio peripheral.
316 * this parameter can be one of the following values:
317 * GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH.
318 * @param pins: gpio pin number
319 * parameter can be any combination of gpio_pin_x, gpio_pin_x as following values:
320 * - GPIO_PINS_0
321 * - GPIO_PINS_1
322 * - GPIO_PINS_2
323 * - GPIO_PINS_3
324 * - GPIO_PINS_4
325 * - GPIO_PINS_5
326 * - GPIO_PINS_6
327 * - GPIO_PINS_7
328 * - GPIO_PINS_8
329 * - GPIO_PINS_9
330 * - GPIO_PINS_10
331 * - GPIO_PINS_11
332 * - GPIO_PINS_12
333 * - GPIO_PINS_13
334 * - GPIO_PINS_14
335 * - GPIO_PINS_15
336 * - GPIO_PINS_ALL
337 * @param bit_state: specifies the value to be written to the selected bit (TRUE or FALSE).
338 * @retval none
340 void gpio_bits_write(gpio_type *gpio_x, uint16_t pins, confirm_state bit_state)
342 if(bit_state != FALSE)
344 gpio_x->scr = pins;
346 else
348 gpio_x->clr = pins;
353 * @brief write data to the specified gpio data port.
354 * @param gpio_x: to select the gpio peripheral.
355 * this parameter can be one of the following values:
356 * GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH.
357 * @param port_value: specifies the value to be written to the port output data register.
358 * @retval none
360 void gpio_port_write(gpio_type *gpio_x, uint16_t port_value)
362 gpio_x->odt = port_value;
366 * @brief write protect gpio pins configuration registers.
367 * @param gpio_x: to select the gpio peripheral.
368 * this parameter can be one of the following values:
369 * GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH.
370 * @param pins: gpio pin number
371 * this parameter can be any combination of the following:
372 * - GPIO_PINS_0
373 * - GPIO_PINS_1
374 * - GPIO_PINS_2
375 * - GPIO_PINS_3
376 * - GPIO_PINS_4
377 * - GPIO_PINS_5
378 * - GPIO_PINS_6
379 * - GPIO_PINS_7
380 * - GPIO_PINS_8
381 * - GPIO_PINS_9
382 * - GPIO_PINS_10
383 * - GPIO_PINS_11
384 * - GPIO_PINS_12
385 * - GPIO_PINS_13
386 * - GPIO_PINS_14
387 * - GPIO_PINS_15
388 * - GPIO_PINS_ALL
389 * @retval none
391 void gpio_pin_wp_config(gpio_type *gpio_x, uint16_t pins)
393 uint32_t temp = 0x00010000;
395 temp |= pins;
396 /* set wpen bit */
397 gpio_x->wpr = temp;
398 /* reset wpen bit */
399 gpio_x->wpr = pins;
400 /* set wpen bit */
401 gpio_x->wpr = temp;
402 /* read wpen bit*/
403 temp = gpio_x->wpr;
404 /* read wpen bit*/
405 temp = gpio_x->wpr;
409 * @brief enable or disable gpio pins huge driven.
410 * @param gpio_x: to select the gpio peripheral.
411 * this parameter can be one of the following values:
412 * GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH.
413 * @param pins: gpio pin number
414 * parameter can be any combination of gpio_pin_x, gpio_pin_x as following values:
415 * - GPIO_PINS_0
416 * - GPIO_PINS_1
417 * - GPIO_PINS_2
418 * - GPIO_PINS_3
419 * - GPIO_PINS_4
420 * - GPIO_PINS_5
421 * - GPIO_PINS_6
422 * - GPIO_PINS_7
423 * - GPIO_PINS_8
424 * - GPIO_PINS_9
425 * - GPIO_PINS_10
426 * - GPIO_PINS_11
427 * - GPIO_PINS_12
428 * - GPIO_PINS_13
429 * - GPIO_PINS_14
430 * - GPIO_PINS_15
431 * - GPIO_PINS_ALL
432 * @param new_state: new state of the slew rate.
433 * this parameter can be: true or false.
434 * @retval none
436 void gpio_pins_huge_driven_config(gpio_type *gpio_x, uint16_t pins, confirm_state new_state)
438 if(new_state != FALSE)
440 gpio_x->hdrv |= pins;
442 else
444 gpio_x->hdrv &= ~pins;
449 * @brief configure the pin's muxing function.
450 * @param gpio_x: to select the gpio peripheral.
451 * this parameter can be one of the following values:
452 * GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH.
453 * @param gpio_pin_source: specifies the pin for the muxing function.
454 * this parameter can be one of the following values:
455 * - GPIO_PINS_SOURCE0
456 * - GPIO_PINS_SOURCE1
457 * - GPIO_PINS_SOURCE2
458 * - GPIO_PINS_SOURCE3
459 * - GPIO_PINS_SOURCE4
460 * - GPIO_PINS_SOURCE5
461 * - GPIO_PINS_SOURCE6
462 * - GPIO_PINS_SOURCE7
463 * - GPIO_PINS_SOURCE8
464 * - GPIO_PINS_SOURCE9
465 * - GPIO_PINS_SOURCE10
466 * - GPIO_PINS_SOURCE11
467 * - GPIO_PINS_SOURCE12
468 * - GPIO_PINS_SOURCE13
469 * - GPIO_PINS_SOURCE14
470 * - GPIO_PINS_SOURCE15
471 * @param gpio_mux: select the pin to used as muxing function.
472 * this parameter can be one of the following values:
473 * - GPIO_MUX_0
474 * - GPIO_MUX_1
475 * - GPIO_MUX_2
476 * - GPIO_MUX_3
477 * - GPIO_MUX_4
478 * - GPIO_MUX_5
479 * - GPIO_MUX_6
480 * - GPIO_MUX_7
481 * - GPIO_MUX_8
482 * - GPIO_MUX_9
483 * - GPIO_MUX_10
484 * - GPIO_MUX_11
485 * - GPIO_MUX_12
486 * - GPIO_MUX_13
487 * - GPIO_MUX_14
488 * - GPIO_MUX_15
489 * @retval none
491 void gpio_pin_mux_config(gpio_type *gpio_x, gpio_pins_source_type gpio_pin_source, gpio_mux_sel_type gpio_mux)
493 uint32_t temp = 0x00;
494 uint32_t temp_2 = 0x00;
496 temp = ((uint32_t)(gpio_mux) << ((uint32_t)((uint32_t)gpio_pin_source & (uint32_t)0x07) * 4));
497 if(gpio_pin_source >> 0x03)
499 gpio_x->muxh &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)gpio_pin_source & (uint32_t)0x07) * 4));
500 temp_2 = gpio_x->muxh | temp;
501 gpio_x->muxh = temp_2;
503 else
505 gpio_x->muxl &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)gpio_pin_source & (uint32_t)0x07) * 4));
506 temp_2 = gpio_x->muxl | temp;
507 gpio_x->muxl = temp_2;
512 * @}
515 #endif
518 * @}
522 * @}