2 **************************************************************************
3 * @file at32f435_437_gpio.c
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
34 * @brief GPIO driver modules
38 #ifdef GPIO_MODULE_ENABLED
40 /** @defgroup GPIO_private_functions
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.
51 void gpio_reset(gpio_type
*gpio_x
)
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
);
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.
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));
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.
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:
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
))
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:
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
)
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:
277 void gpio_bits_set(gpio_type
*gpio_x
, uint16_t 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:
308 void gpio_bits_reset(gpio_type
*gpio_x
, uint16_t 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:
337 * @param bit_state: specifies the value to be written to the selected bit (TRUE or FALSE).
340 void gpio_bits_write(gpio_type
*gpio_x
, uint16_t pins
, confirm_state bit_state
)
342 if(bit_state
!= FALSE
)
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.
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:
391 void gpio_pin_wp_config(gpio_type
*gpio_x
, uint16_t pins
)
393 uint32_t temp
= 0x00010000;
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:
432 * @param new_state: new state of the slew rate.
433 * this parameter can be: true or false.
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
;
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:
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
;
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
;