Appiko
hal_gpio.h
1 
19 #ifndef CODEBASE_HAL_HAL_GPIO_H_
20 #define CODEBASE_HAL_HAL_GPIO_H_
21 
31 #include "nrf.h"
32 
36 typedef enum {
37  HAL_GPIO_PULL_DISABLED = GPIO_PIN_CNF_PULL_Disabled,
38  HAL_GPIO_PULL_DOWN = GPIO_PIN_CNF_PULL_Pulldown,
39  HAL_GPIO_PULL_UP = GPIO_PIN_CNF_PULL_Pullup
41 
54 static inline void hal_gpio_cfg( uint32_t pin_num,
55  uint32_t dir,
56  uint32_t input,
57  uint32_t pull,
58  uint32_t drive,
59  uint32_t sense);
60 
67 static inline void hal_gpio_cfg_output(uint32_t pin_num, uint32_t init_val);
68 
75 static inline void hal_gpio_cfg_high_output(uint32_t pin_num, uint32_t init_val);
76 
83 static inline void hal_gpio_cfg_input(uint32_t pin_num, hal_gpio_pull_t pull_config);
84 
90 static inline void hal_gpio_pin_write(uint32_t pin_num, uint32_t val);
91 
96 static inline void hal_gpio_pin_set(uint32_t pin_num);
97 
102 static inline void hal_gpio_pin_clear(uint32_t pin_num);
103 
108 static inline void hal_gpio_pin_toggle(uint32_t pin_num);
109 
116 static inline uint32_t hal_gpio_pin_read(uint32_t pin_num);
117 
118 static inline void hal_gpio_cfg(uint32_t pin_num, uint32_t dir, uint32_t input, uint32_t pull,
119  uint32_t drive, uint32_t sense)
120 {
121  NRF_GPIO->PIN_CNF[pin_num] = (dir << GPIO_PIN_CNF_DIR_Pos) | (input << GPIO_PIN_CNF_INPUT_Pos)
122  | (pull << GPIO_PIN_CNF_PULL_Pos) | (drive << GPIO_PIN_CNF_DRIVE_Pos)
123  | (sense << GPIO_PIN_CNF_SENSE_Pos);
124 }
125 
126 static inline void hal_gpio_cfg_output(uint32_t pin_num, uint32_t init_val)
127 {
128  hal_gpio_cfg(pin_num,
129  GPIO_PIN_CNF_DIR_Input,
130  GPIO_PIN_CNF_INPUT_Disconnect,
131  GPIO_PIN_CNF_PULL_Disabled,
132  GPIO_PIN_CNF_DRIVE_S0S1,
133  GPIO_PIN_CNF_SENSE_Disabled);
134  hal_gpio_pin_write(pin_num, init_val);
135  hal_gpio_cfg(pin_num,
136  GPIO_PIN_CNF_DIR_Output,
137  GPIO_PIN_CNF_INPUT_Disconnect,
138  GPIO_PIN_CNF_PULL_Disabled,
139  GPIO_PIN_CNF_DRIVE_S0S1,
140  GPIO_PIN_CNF_SENSE_Disabled);
141 }
142 
143 static inline void hal_gpio_cfg_high_output(uint32_t pin_num, uint32_t init_val)
144 {
145  hal_gpio_cfg(pin_num,
146  GPIO_PIN_CNF_DIR_Input,
147  GPIO_PIN_CNF_INPUT_Disconnect,
148  GPIO_PIN_CNF_PULL_Disabled,
149  GPIO_PIN_CNF_DRIVE_S0S1,
150  GPIO_PIN_CNF_SENSE_Disabled);
151  hal_gpio_pin_write(pin_num, init_val);
152  hal_gpio_cfg(pin_num,
153  GPIO_PIN_CNF_DIR_Output,
154  GPIO_PIN_CNF_INPUT_Disconnect,
155  GPIO_PIN_CNF_PULL_Disabled,
156  GPIO_PIN_CNF_DRIVE_H0H1,
157  GPIO_PIN_CNF_SENSE_Disabled);
158 }
159 
160 static inline void hal_gpio_cfg_input(uint32_t pin_num, hal_gpio_pull_t pull_config)
161 {
162  hal_gpio_cfg(pin_num,
163  GPIO_PIN_CNF_DIR_Input,
164  GPIO_PIN_CNF_INPUT_Connect, pull_config,
165  GPIO_PIN_CNF_DRIVE_S0S1,
166  GPIO_PIN_CNF_SENSE_Disabled);
167 }
168 
169 static inline void hal_gpio_pin_write(uint32_t pin_num, uint32_t val)
170 {
171  if (val)
172  {
173  hal_gpio_pin_set(pin_num);
174  }
175  else
176  {
177  hal_gpio_pin_clear(pin_num);
178  }
179 }
180 
181 static inline void hal_gpio_pin_set(uint32_t pin_num)
182 {
183  NRF_GPIO->OUTSET = (1UL << pin_num);
184 }
185 
186 static inline void hal_gpio_pin_clear(uint32_t pin_num)
187 {
188  NRF_GPIO->OUTCLR = (1UL << pin_num);
189 }
190 
191 static inline void hal_gpio_pin_toggle(uint32_t pin_num)
192 {
193  const uint32_t pin_bit = 1UL << pin_num;
194  const uint32_t pin_state = (NRF_GPIO->OUT & pin_bit);
195 
196  if (pin_state == 0)
197  {
198  // Low to high
199  NRF_GPIO->OUTSET = pin_bit;
200  }
201  else
202  {
203  // High to low
204  NRF_GPIO->OUTCLR = pin_bit;
205  }
206 }
207 
208 static inline uint32_t hal_gpio_pin_read(uint32_t pin_num)
209 {
210  return ((NRF_GPIO->IN >> pin_num) & 1UL);
211 }
212 
213 #endif /* CODEBASE_HAL_HAL_GPIO_H_ */
214 
hal_gpio_pull_t
Definition: hal_gpio.h:36
HAL_GPIO_PULL_UP.
Definition: hal_gpio.h:39
HAL_GPIO_PULL_DISABLED.
Definition: hal_gpio.h:37
HAL_GPIO_PULL_DOWN.
Definition: hal_gpio.h:38