APM:Libraries
gpio_hal.h
Go to the documentation of this file.
1 #ifndef _GPIO_H
2 #define _GPIO_H
3 
4 #include "hal_types.h"
5 #include "exti.h"
6 
7 
15 /*
16  we should define modes to be compatible with HAL_GPIO_ defines from HAL.h
17 #define HAL_GPIO_INPUT 0
18 #define HAL_GPIO_OUTPUT 1
19 #define HAL_GPIO_ALT 2
20 
21 */
22 
23 typedef enum gpio_pin_mode {
27 // more complex modes
31  /* GPIO_INPUT_PU treated as a special case, for ODR twiddling */
38 
39 #define Bit_RESET 0
40 #define Bit_SET 1
41 
42 typedef enum
43 {
44  GPIO_speed_2MHz = 0x00,
48 } GPIOSpeed_t;
49 
50 
51 
53 typedef struct gpio_dev {
54  GPIO_TypeDef *GPIOx;
55  uint32_t clk;
57 } gpio_dev;
58 
59 #ifdef __cplusplus
60  extern "C" {
61 #endif
62 
63 extern const gpio_dev gpioa;
64 extern const gpio_dev* const _GPIOA;
65 extern const gpio_dev gpiob;
66 extern const gpio_dev* const _GPIOB;
67 extern const gpio_dev gpioc;
68 extern const gpio_dev* const _GPIOC;
69 extern const gpio_dev gpiod;
70 extern const gpio_dev* const _GPIOD;
71 extern const gpio_dev gpioe;
72 extern const gpio_dev* const _GPIOE;
73 extern const gpio_dev gpiof;
74 extern const gpio_dev* const _GPIOF;
75 extern const gpio_dev gpiog;
76 extern const gpio_dev* const _GPIOG;
77 
81 extern void gpio_init(const gpio_dev* const dev);
82 
86 extern void gpio_init_all(void);
87 
91 extern void gpio_set_mode(const gpio_dev* const dev, uint8_t pin, gpio_pin_mode mode);
92 
93 
102 static inline void gpio_set_af_mode(const gpio_dev* const dev, uint8_t pin, uint8_t mode)
103 {
104  /* Check the parameters */
105  assert_param(IS_GPIO_ALL_PERIPH(dev->GPIOx));
106  assert_param(IS_GPIO_PIN_SOURCE(pin));
107  assert_param(IS_GPIO_AF(mode));
108 
109 // GPIO_PinAFConfig(dev->GPIOx, pin, mode);
110  uint32_t temp = dev->GPIOx->AFR[pin >> 0x03] & ~((uint32_t)0xF << ((uint32_t)((uint32_t)pin & (uint32_t)0x07) * 4));
111  dev->GPIOx->AFR[pin >> 0x03] = temp | ((uint32_t)(mode) << ((uint32_t)((uint32_t)pin & (uint32_t)0x07) * 4));
112 }
113 
114 
115 static INLINE void gpio_write_bit(const gpio_dev* const dev, uint8_t pin, uint8_t val)
116 {
117  /* Check the parameters */
118  assert_param(IS_GPIO_ALL_PERIPH(dev->GPIOx));
119  assert_param(IS_GPIO_PIN_SOURCE(pin));
120 
121  uint16_t bv = BIT(pin);
122 
123  if (val) {
124  dev->GPIOx->BSRRL = bv;
125  } else {
126  dev->GPIOx->BSRRH = bv;
127  }
128 }
129 
130 static INLINE uint8_t gpio_read_bit(const gpio_dev* const dev, uint8_t pin)
131 {
132  /* Check the parameters */
133  assert_param(IS_GPIO_ALL_PERIPH(dev->GPIOx));
134  assert_param(IS_GPIO_PIN_SOURCE(pin));
135 
136  if ((dev->GPIOx->IDR & BIT(pin)) != Bit_RESET){
137  return (uint8_t)Bit_SET;
138  }
139 
140  return (uint8_t)Bit_RESET;
141 
142 
143 }
144 
145 static inline void gpio_toggle_bit(const gpio_dev* const dev, uint8_t pin)
146 {
147  /* Check the parameters */
148  assert_param(IS_GPIO_ALL_PERIPH(dev->GPIOx));
149  assert_param(IS_GPIO_PIN_SOURCE(pin));
150  dev->GPIOx->ODR ^= BIT(pin);
151 }
152 
153 static inline afio_exti_port gpio_exti_port(const gpio_dev* const dev)
154 {
155  /* Check the parameters */
156  assert_param(IS_GPIO_ALL_PERIPH(dev->GPIOx));
157  return dev->exti_port;
158 }
159 
160 
161 static inline void gpio_set_speed(const gpio_dev* const dev, uint8_t pin, GPIOSpeed_t gpio_speed){
162 /* Speed mode configuration */
163  dev->GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pin * 2));
164  dev->GPIOx->OSPEEDR |= ((uint32_t)(gpio_speed) << (pin * 2));
165 }
166 
167 
168 static inline void afio_exti_select(afio_exti_port gpio_port, afio_exti_num pin)
169 {
170  uint32_t tmp = ((uint32_t)0x0F) << (0x04 * (pin & (uint8_t)0x03));
171  SYSCFG->EXTICR[pin >> 0x02] &= ~tmp;
172  SYSCFG->EXTICR[pin >> 0x02] |= (((uint32_t)gpio_port) << (0x04 * (pin & (uint8_t)0x03)));
173 
174 }
175 
176 
177 #ifdef __cplusplus
178  }
179 #endif
180 
181 #endif
182 
afio_exti_port exti_port
Definition: gpio_hal.h:56
const gpio_dev gpiod
Definition: gpio_hal.c:41
void gpio_set_mode(const gpio_dev *const dev, uint8_t pin, gpio_pin_mode mode)
Definition: gpio_hal.c:125
#define assert_param(expr)
const gpio_dev *const _GPIOE
Definition: gpio_hal.c:55
struct gpio_dev gpio_dev
const gpio_dev *const _GPIOB
Definition: gpio_hal.c:31
const gpio_dev gpioc
Definition: gpio_hal.c:33
const gpio_dev *const _GPIOD
Definition: gpio_hal.c:47
static afio_exti_port gpio_exti_port(const gpio_dev *const dev)
Definition: gpio_hal.h:153
const gpio_dev *const _GPIOA
Definition: gpio_hal.c:23
GPIO_TypeDef * GPIOx
Definition: gpio_hal.h:54
const gpio_dev *const _GPIOG
Definition: gpio_hal.c:71
const gpio_dev gpiog
Definition: gpio_hal.c:65
const gpio_dev *const _GPIOC
Definition: gpio_hal.c:39
#define Bit_SET
Definition: gpio_hal.h:40
void gpio_init_all(void)
Definition: gpio_hal.c:90
void gpio_init(const gpio_dev *const dev)
static void gpio_toggle_bit(const gpio_dev *const dev, uint8_t pin)
Definition: gpio_hal.h:145
static AP_HAL::OwnPtr< AP_HAL::Device > dev
Definition: ICM20789.cpp:16
const gpio_dev *const _GPIOF
Definition: gpio_hal.c:63
#define BIT(shift)
Definition: util.h:40
#define INLINE
Definition: hal_types.h:79
const gpio_dev gpioa
Definition: gpio_hal.c:17
afio_exti_num
Definition: exti.h:15
const gpio_dev gpiob
Definition: gpio_hal.c:25
const gpio_dev gpioe
Definition: gpio_hal.c:49
gpio_pin_mode
GPIO Pin modes.
Definition: gpio_hal.h:23
uint32_t clk
Definition: gpio_hal.h:55
static INLINE uint8_t gpio_read_bit(const gpio_dev *const dev, uint8_t pin)
Definition: gpio_hal.h:130
static void gpio_set_speed(const gpio_dev *const dev, uint8_t pin, GPIOSpeed_t gpio_speed)
Definition: gpio_hal.h:161
static void gpio_set_af_mode(const gpio_dev *const dev, uint8_t pin, uint8_t mode)
Definition: gpio_hal.h:102
static int8_t pin
Definition: AnalogIn.cpp:15
const gpio_dev gpiof
Definition: gpio_hal.c:57
#define Bit_RESET
Definition: gpio_hal.h:39
static void afio_exti_select(afio_exti_port gpio_port, afio_exti_num pin)
Definition: gpio_hal.h:168
GPIOSpeed_t
Definition: gpio_hal.h:42
static INLINE void gpio_write_bit(const gpio_dev *const dev, uint8_t pin, uint8_t val)
Definition: gpio_hal.h:115
afio_exti_port
External interrupt line port selector.
Definition: exti.h:40