]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/include/hal/gpio.h
5bc603f6aa084ef26d207e7ade6f95cd0d94be17
[pes-rpp/rpp-lib.git] / rpp / include / hal / gpio.h
1 /* Copyright (C) 2013-2014 Czech Technical University in Prague
2  * Authors:
3  *     - Michal Horn <hornmich@fel.cvut.cz>
4  *
5  * This document contains proprietary information belonging to Czech
6  * Technical University in Prague. Passing on and copying of this
7  * document, and communication of its contents is not permitted
8  * without prior written authorization.
9  *
10  * Abstract:
11  *     The file contains GPIO pins read, write and configure inline functions.
12  */
13
14 /**
15  * @file
16  *
17  *  This file contains gpio pin inline functions.
18  *  - Get pin descriptor from name function
19  *  - Get port base (pointer to registers) from port number
20  *  - Get port number from pin descriptor
21  *  - Get port base (pointer to registers) from pin descriptor
22  *  - Get/Set pin value
23  *  - Get/Set pin direction
24  *  - Set pin configuration (can be used for initial configuration after MCU reset
25  *
26  *  Each pin is defined by its descriptor defined in hal_gpio_platform_def. The descriptor can be obtained
27  *  by hal_gpio_get_pin_dsc by giving a pin name as an argument.
28  */
29
30 #ifndef _HAL_GPIO_H
31 #define _HAL_GPIO_H_
32
33 #include "hal/hal.h"
34
35 extern gioPORT_t *port_id_map[MAX_PORT_CNT];
36 extern pin_map_element_t pin_map[MAX_PIN_CNT];
37
38 /**
39  *  Get pin descriptor assigned to pin name.
40  *  @param[in]  pin_name    Pointer to string - the name of the pin.
41  *  @param[in]  len         Length of the name, if terminated by '/0', then len=-1
42  *  @return Pin descriptor or NULL if not found
43  */
44 static inline uint32_t *hal_gpio_pin_get_dsc(const char *pin_name, int len)
45 {
46         uint32_t i;
47         const char *pin_name_ptr;
48         char pin_name_term[32];
49
50         if (len != -1) {    // pin name not terminated by '\0'
51                 strncpy(pin_name_term, pin_name, len);
52                 pin_name_term[len] = '\0';
53                 pin_name_ptr = pin_name_term;
54         }
55         else pin_name_ptr = pin_name;
56
57         for (i = 0; i < MAX_PIN_CNT; i++) {
58                 if (strcmp(pin_name_ptr, pin_map[i].pin_name) == 0)
59                         return &pin_map[i].pin_desc;
60         }
61         return NULL;
62 }
63
64 /**
65  * Get port base assigned to port number
66  * @param[in]   port_num    Port number <0;4>
67  * @return Pointer to port registers
68  */
69 static inline gioPORT_t *hal_gpio_get_port_base(uint32_t port_num)
70 {
71         return port_id_map[port_num];
72 }
73
74 /**
75  * Get port number assigned to pin in its descriptor
76  * @param[in]   pin descriptor
77  * @return  Index of port
78  */
79 static inline uint32_t hal_gpio_pin_get_port_num(uint32_t pin_dsc)
80 {
81
82         return (pin_dsc & ~PORT_CONF_MASK) >> PORT_SHIFT;
83 }
84
85 /**
86  * Get port base from pin descriptor
87  * Combines two upper defined functions
88  * @param[in]   pin_dcs Pin descriptor
89  * @return      Pointer to port registers
90  */
91 static inline gioPORT_t *hal_gpio_pin_get_port_base(uint32_t pin_dsc)
92 {
93         return hal_gpio_get_port_base(hal_gpio_pin_get_port_num(pin_dsc));
94 }
95
96 /**
97  * Get value from GPIO pin
98  * @param[in]   pin_dsc pin descriptor
99  * @return  value read from specified gpio pin
100  */
101 static inline uint32_t hal_gpio_pin_get_value(uint32_t pin_dsc)
102 {
103         return ((hal_gpio_pin_get_port_base(pin_dsc)->DIN) >> (pin_dsc & 0x1f)) & 1;
104 }
105
106 /**
107  * Set value to gpio pin
108  * @param[in]   pin_dsc pin descriptor
109  * @param[in]   value   value to be assigned to the pin
110  */
111 static inline void hal_gpio_pin_set_value(uint32_t pin_dsc, uint32_t value)
112 {
113         if (value)
114                 hal_gpio_pin_get_port_base(pin_dsc)->DSET = 1 << (pin_dsc & 0x1f);
115         else
116                 hal_gpio_pin_get_port_base(pin_dsc)->DCLR = 1 << (pin_dsc & 0x1f);
117 }
118
119 /**
120  * Set pin direction to input
121  * @param[in] pin_dsc pin descriptor
122  * @return always 0
123  */
124 static inline int hal_gpio_pin_direction_input(uint32_t pin_dsc)
125 {
126         hal_gpio_pin_get_port_base(pin_dsc)->DIR &= ~(1 << (pin_dsc & 0x1f));
127         return 0;
128 }
129
130 /**
131  * Set pin direction to output
132  * @param[in] pin_dsc pin descriptor
133  * @return always 0
134  */
135 static inline int hal_gpio_pin_direction_output(uint32_t pin_dsc, uint32_t value)
136 {
137         hal_gpio_pin_set_value(pin_dsc, value);
138         hal_gpio_pin_get_port_base(pin_dsc)->DIR |= (1 << (pin_dsc & 0x1f));
139         return 0;
140 }
141
142 /**
143  * Get pin direction
144  * @param[in] pin_dsc pin descriptor
145  * @return 1 - output, 0 - input
146  */
147 static inline int hal_gpio_pin_get_direction(uint32_t pin_dsc)
148 {
149         return (hal_gpio_pin_get_port_base(pin_dsc)->DIR >> (pin_dsc & 0x1f)) & 1;
150 }
151
152
153 uint32_t hal_gpio_pin_conf_mode(uint32_t pin_dsc, uint32_t mode);
154
155 uint32_t hal_gpio_pin_conf_od(uint32_t pin_dsc, uint32_t od);
156
157 uint32_t hal_gpio_pin_conf_set(uint32_t pin_dsc, uint32_t conf);
158
159 /**
160  * Do the initial pin configuration according values in pin descriptor
161  * @param[in]   pin_dsc pin descriptor
162  * @return always 0;
163  */
164 static inline uint32_t hal_gpio_pin_conf(uint32_t pin_dsc)
165 {
166         return hal_gpio_pin_conf_set(pin_dsc, pin_dsc);
167 }
168
169
170 #endif //_HAL_GPIO_H_