]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/hal/gpio.c
5a7ec123b89561b0ae0243761c8810977d03a684
[pes-rpp/rpp-lib.git] / rpp / src / hal / gpio.c
1 /* Copyright (C) 2012-2013 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Michal Horn <hornmich@fel.cvut.cz>
5  *
6  * This document contains proprietary information belonging to Czech
7  * Technical University in Prague. Passing on and copying of this
8  * document, and communication of its contents is not permitted
9  * without prior written authorization.
10  *
11  * File : gpio.c
12  *
13  * Abstract:
14  *          This file contains gpio pin configuration functions.
15  *      Some additional function for setting and getting pin values, getting
16  *      descriptors etc. are defined in header file as inline functions.
17  *
18  *      Each pin is defined by its descriptor defined in hal_gpio_platform_def. The descriptor can be obtained
19  *      by hal_gpio_get_pin_dsc by giving a pin name as an argument.
20  */
21
22 #include "hal/hal.h"
23
24 /**
25  * Set pin as pull down or pull up and pull resistor enabled or disabled.
26  * @param[in] pin_dsc Descriptor of the pin
27  * @param[in] Mode on which pin will be configured to.
28  *              PORT_CONF_MODE_PU - pull up
29  *              PORT_CONF_MODE_PD - pull down
30  *              must be | with
31  *              PORT_CONF_MODE_PEN - pull resistor enable
32  *              PORT_CONF_MODE_PDIS - pull resistor disable
33  * @return always 0
34  */
35 uint32_t hal_gpio_pin_conf_mode(uint32_t pin_dsc, uint32_t mode)
36 {
37         gioPORT_t *gioPort = hal_gpio_pin_get_port_base(pin_dsc);
38
39         if (mode & PORT_CONF_MODE_PTYPE_MASK)
40                 gioPort->PSL |= (1 << (pin_dsc & 0x1f));
41         else
42                 gioPort->PSL &= ~(1 << (pin_dsc & 0x1f));
43         if (mode & PORT_CONF_MODE_PEN_MASK)
44                 gioPort->PULDIS |= (1 << (pin_dsc & 0x1f));
45         else
46                 gioPort->PULDIS &= ~(1 << (pin_dsc & 0x1f));
47         return 0;
48 }
49
50 /**
51  * Configure pin to be open drain or not
52  * @param[in] pin_dsc Descriptor of the pin
53  * @param[in] Mode on which pin will be configured to.
54  *              PORT_CONF_OD_OFF - open-drain disabled
55  *              PORT_CONF_OD_ON - open drain enabled
56  *
57  * @return always 0
58  */
59 uint32_t hal_gpio_pin_conf_od(uint32_t pin_dsc, uint32_t od)
60 {
61         gioPORT_t *gioPort = hal_gpio_pin_get_port_base(pin_dsc);
62
63         if (od & PORT_CONF_OD_ON)
64                 gioPort->PDR |= (1 << (pin_dsc & 0x1f));
65         else
66                 gioPort->PDR &= ~(1 << (pin_dsc & 0x1f));
67         return 0;
68 }
69
70 /**
71  * Configure pin
72  * @param[in] pin_dsc Descriptor of the pin
73  * @param[in] Mode on which pin will be configured to.
74  *              PORT_CONF_OD_OFF - open-drain disabled
75  *              PORT_CONF_OD_ON - open drain enabled
76  *
77  *              PORT_CONF_MODE_PU - pull up
78  *              PORT_CONF_MODE_PD - pull down
79  *
80  *              PORT_CONF_MODE_PEN - pull resistor enable
81  *              PORT_CONF_MODE_PDIS - pull resistor disable
82  *
83  *              PORT_CONF_DIR_IN - direction input
84  *              PORT_CONF_DIR_OUT - direction output
85  *
86  *              PORT_CONF_INIT_LOW - init value 0
87  *              PORT_CONF_INIT_HIGH - init value 1
88  *
89  *              PORT_CONF_FNC_GPIO - port function GPIO
90  *              PORT_CONF_FNC_FNCX - port alternate function X
91  *
92  * @return always 0
93  */
94 uint32_t hal_gpio_pin_conf_set(uint32_t pin_dsc, uint32_t conf)
95 {
96         pin_dsc &= ~PORT_CONF_MASK;
97         hal_gpio_pin_conf_mode(pin_dsc, conf & PORT_CONF_MODE_MASK);
98         hal_gpio_pin_conf_od(pin_dsc, conf & PORT_CONF_OD_MASK);
99         if (conf & PORT_CONF_SET_DIR) {
100                 if ((conf & PORT_CONF_DIR_MASK) == (PORT_CONF_DIR_IN & PORT_CONF_DIR_MASK))
101                         hal_gpio_pin_direction_input(pin_dsc);
102                 else
103                         hal_gpio_pin_direction_output(pin_dsc, conf & PORT_CONF_INIT_HIGH);
104         }
105
106         return 0;
107 }