1 /* Copyright (C) 2013-2014 Czech Technical University in Prague
3 * - Michal Horn <hornmich@fel.cvut.cz>
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.
11 * The file contains GPIO pins read, write and configure inline functions.
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
23 * - Get/Set pin direction
24 * - Set pin configuration (can be used for initial configuration after MCU reset
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.
35 extern gioPORT_t *port_id_map[MAX_PORT_CNT];
36 extern pin_map_element_t pin_map[MAX_PIN_CNT];
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
44 static inline uint32_t *hal_gpio_pin_get_dsc(const char *pin_name, int len)
47 const char *pin_name_ptr;
48 char pin_name_term[32];
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;
55 else pin_name_ptr = pin_name;
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;
65 * Get port base assigned to port number
66 * @param[in] port_num Port number <0;4>
67 * @return Pointer to port registers
69 static inline gioPORT_t *hal_gpio_get_port_base(uint32_t port_num)
71 return port_id_map[port_num];
75 * Get port number assigned to pin in its descriptor
76 * @param[in] pin descriptor
77 * @return Index of port
79 static inline uint32_t hal_gpio_pin_get_port_num(uint32_t pin_dsc)
82 return (pin_dsc & ~PORT_CONF_MASK) >> PORT_SHIFT;
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
91 static inline gioPORT_t *hal_gpio_pin_get_port_base(uint32_t pin_dsc)
93 return hal_gpio_get_port_base(hal_gpio_pin_get_port_num(pin_dsc));
97 * Get value from GPIO pin
98 * @param[in] pin_dsc pin descriptor
99 * @return value read from specified gpio pin
101 static inline uint32_t hal_gpio_pin_get_value(uint32_t pin_dsc)
103 return ((hal_gpio_pin_get_port_base(pin_dsc)->DIN) >> (pin_dsc & 0x1f)) & 1;
107 * Set value to gpio pin
108 * @param[in] pin_dsc pin descriptor
109 * @param[in] value value to be assigned to the pin
111 static inline void hal_gpio_pin_set_value(uint32_t pin_dsc, uint32_t value)
114 hal_gpio_pin_get_port_base(pin_dsc)->DSET = 1 << (pin_dsc & 0x1f);
116 hal_gpio_pin_get_port_base(pin_dsc)->DCLR = 1 << (pin_dsc & 0x1f);
120 * Set pin direction to input
121 * @param[in] pin_dsc pin descriptor
124 static inline int hal_gpio_pin_direction_input(uint32_t pin_dsc)
126 hal_gpio_pin_get_port_base(pin_dsc)->DIR &= ~(1 << (pin_dsc & 0x1f));
131 * Set pin direction to output
132 * @param[in] pin_dsc pin descriptor
135 static inline int hal_gpio_pin_direction_output(uint32_t pin_dsc, uint32_t value)
137 hal_gpio_pin_set_value(pin_dsc, value);
138 hal_gpio_pin_get_port_base(pin_dsc)->DIR |= (1 << (pin_dsc & 0x1f));
144 * @param[in] pin_dsc pin descriptor
145 * @return 1 - output, 0 - input
147 static inline int hal_gpio_pin_get_direction(uint32_t pin_dsc)
149 return (hal_gpio_pin_get_port_base(pin_dsc)->DIR >> (pin_dsc & 0x1f)) & 1;
153 uint32_t hal_gpio_pin_conf_mode(uint32_t pin_dsc, uint32_t mode);
155 uint32_t hal_gpio_pin_conf_od(uint32_t pin_dsc, uint32_t od);
157 uint32_t hal_gpio_pin_conf_set(uint32_t pin_dsc, uint32_t conf);
160 * Do the initial pin configuration according values in pin descriptor
161 * @param[in] pin_dsc pin descriptor
164 static inline uint32_t hal_gpio_pin_conf(uint32_t pin_dsc)
166 return hal_gpio_pin_conf_set(pin_dsc, pin_dsc);
170 #endif //_HAL_GPIO_H_