Openwear requires RC oscillator to be used

Fork of nRF51822 by Nordic Semiconductor

Committer:
bogdanm
Date:
Wed Mar 26 14:38:17 2014 +0000
Revision:
0:eff01767de02
Child:
37:c29c330d942c
Initial import of the nRF51822 code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 0:eff01767de02 1 #ifndef NRF_GPIO_H__
bogdanm 0:eff01767de02 2 #define NRF_GPIO_H__
bogdanm 0:eff01767de02 3
bogdanm 0:eff01767de02 4 #include "nordic_global.h"
bogdanm 0:eff01767de02 5 #include "nrf51.h"
bogdanm 0:eff01767de02 6 #include "nrf51_bitfields.h"
bogdanm 0:eff01767de02 7
bogdanm 0:eff01767de02 8 /**
bogdanm 0:eff01767de02 9 * @defgroup nrf_gpio GPIO abstraction
bogdanm 0:eff01767de02 10 * @{
bogdanm 0:eff01767de02 11 * @ingroup nrf_drivers
bogdanm 0:eff01767de02 12 * @brief GPIO pin abstraction and port abstraction for reading and writing byte-wise to GPIO ports.
bogdanm 0:eff01767de02 13 *
bogdanm 0:eff01767de02 14 * Here, the GPIO ports are defined as follows:
bogdanm 0:eff01767de02 15 * - Port 0 -> pin 0-7
bogdanm 0:eff01767de02 16 * - Port 1 -> pin 8-15
bogdanm 0:eff01767de02 17 * - Port 2 -> pin 16-23
bogdanm 0:eff01767de02 18 * - Port 3 -> pin 24-31
bogdanm 0:eff01767de02 19 */
bogdanm 0:eff01767de02 20
bogdanm 0:eff01767de02 21 /**
bogdanm 0:eff01767de02 22 * @enum nrf_gpio_port_dir_t
bogdanm 0:eff01767de02 23 * @brief Enumerator used for setting the direction of a GPIO port.
bogdanm 0:eff01767de02 24 */
bogdanm 0:eff01767de02 25 typedef enum
bogdanm 0:eff01767de02 26 {
bogdanm 0:eff01767de02 27 NRF_GPIO_PORT_DIR_OUTPUT, ///< Output
bogdanm 0:eff01767de02 28 NRF_GPIO_PORT_DIR_INPUT ///< Input
bogdanm 0:eff01767de02 29 } nrf_gpio_port_dir_t;
bogdanm 0:eff01767de02 30
bogdanm 0:eff01767de02 31 /**
bogdanm 0:eff01767de02 32 * @enum nrf_gpio_pin_dir_t
bogdanm 0:eff01767de02 33 * Pin direction definitions.
bogdanm 0:eff01767de02 34 */
bogdanm 0:eff01767de02 35 typedef enum
bogdanm 0:eff01767de02 36 {
bogdanm 0:eff01767de02 37 NRF_GPIO_PIN_DIR_INPUT, ///< Input
bogdanm 0:eff01767de02 38 NRF_GPIO_PIN_DIR_OUTPUT ///< Output
bogdanm 0:eff01767de02 39 } nrf_gpio_pin_dir_t;
bogdanm 0:eff01767de02 40
bogdanm 0:eff01767de02 41 /**
bogdanm 0:eff01767de02 42 * @enum nrf_gpio_port_select_t
bogdanm 0:eff01767de02 43 * @brief Enumerator used for selecting between port 0 - 3.
bogdanm 0:eff01767de02 44 */
bogdanm 0:eff01767de02 45 typedef enum
bogdanm 0:eff01767de02 46 {
bogdanm 0:eff01767de02 47 NRF_GPIO_PORT_SELECT_PORT0 = 0, ///< Port 0 (GPIO pin 0-7)
bogdanm 0:eff01767de02 48 NRF_GPIO_PORT_SELECT_PORT1, ///< Port 1 (GPIO pin 8-15)
bogdanm 0:eff01767de02 49 NRF_GPIO_PORT_SELECT_PORT2, ///< Port 2 (GPIO pin 16-23)
bogdanm 0:eff01767de02 50 NRF_GPIO_PORT_SELECT_PORT3, ///< Port 3 (GPIO pin 24-31)
bogdanm 0:eff01767de02 51 } nrf_gpio_port_select_t;
bogdanm 0:eff01767de02 52
bogdanm 0:eff01767de02 53 /**
bogdanm 0:eff01767de02 54 * @enum nrf_gpio_pin_pull_t
bogdanm 0:eff01767de02 55 * @brief Enumerator used for selecting the pin to be pulled down or up at the time of pin configuration
bogdanm 0:eff01767de02 56 */
bogdanm 0:eff01767de02 57 typedef enum
bogdanm 0:eff01767de02 58 {
bogdanm 0:eff01767de02 59 NRF_GPIO_PIN_NOPULL = GPIO_PIN_CNF_PULL_Disabled, ///< Pin pullup resistor disabled
bogdanm 0:eff01767de02 60 NRF_GPIO_PIN_PULLDOWN = GPIO_PIN_CNF_PULL_Pulldown, ///< Pin pulldown resistor enabled
bogdanm 0:eff01767de02 61 NRF_GPIO_PIN_PULLUP = GPIO_PIN_CNF_PULL_Pullup, ///< Pin pullup resistor enabled
bogdanm 0:eff01767de02 62 } nrf_gpio_pin_pull_t;
bogdanm 0:eff01767de02 63
bogdanm 0:eff01767de02 64 /**
bogdanm 0:eff01767de02 65 * @enum nrf_gpio_pin_sense_t
bogdanm 0:eff01767de02 66 * @brief Enumerator used for selecting the pin to sense high or low level on the pin input.
bogdanm 0:eff01767de02 67 */
bogdanm 0:eff01767de02 68 typedef enum
bogdanm 0:eff01767de02 69 {
bogdanm 0:eff01767de02 70 NRF_GPIO_PIN_NOSENSE = GPIO_PIN_CNF_SENSE_Disabled, ///< Pin sense level disabled.
bogdanm 0:eff01767de02 71 NRF_GPIO_PIN_SENSE_LOW = GPIO_PIN_CNF_SENSE_Low, ///< Pin sense low level.
bogdanm 0:eff01767de02 72 NRF_GPIO_PIN_SENSE_HIGH = GPIO_PIN_CNF_SENSE_High, ///< Pin sense high level.
bogdanm 0:eff01767de02 73 } nrf_gpio_pin_sense_t;
bogdanm 0:eff01767de02 74
bogdanm 0:eff01767de02 75 /**
bogdanm 0:eff01767de02 76 * @brief Function for configuring the GPIO pin range as outputs with normal drive strength.
bogdanm 0:eff01767de02 77 * This function can be used to configure pin range as simple output with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
bogdanm 0:eff01767de02 78 *
bogdanm 0:eff01767de02 79 * @param pin_range_start specifies the start number (inclusive) in the range of pin numbers to be configured (allowed values 0-30)
bogdanm 0:eff01767de02 80 *
bogdanm 0:eff01767de02 81 * @param pin_range_end specifies the end number (inclusive) in the range of pin numbers to be configured (allowed values 0-30)
bogdanm 0:eff01767de02 82 *
bogdanm 0:eff01767de02 83 * @note For configuring only one pin as output use @ref nrf_gpio_cfg_output
bogdanm 0:eff01767de02 84 * Sense capability on the pin is disabled, and input is disconnected from the buffer as the pins are configured as output.
bogdanm 0:eff01767de02 85 */
bogdanm 0:eff01767de02 86 static __INLINE void nrf_gpio_range_cfg_output(uint32_t pin_range_start, uint32_t pin_range_end)
bogdanm 0:eff01767de02 87 {
bogdanm 0:eff01767de02 88 /*lint -e{845} // A zero has been given as right argument to operator '|'" */
bogdanm 0:eff01767de02 89 for (; pin_range_start <= pin_range_end; pin_range_start++)
bogdanm 0:eff01767de02 90 {
bogdanm 0:eff01767de02 91 NRF_GPIO->PIN_CNF[pin_range_start] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
bogdanm 0:eff01767de02 92 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
bogdanm 0:eff01767de02 93 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
bogdanm 0:eff01767de02 94 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
bogdanm 0:eff01767de02 95 | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
bogdanm 0:eff01767de02 96 }
bogdanm 0:eff01767de02 97 }
bogdanm 0:eff01767de02 98
bogdanm 0:eff01767de02 99 /**
bogdanm 0:eff01767de02 100 * @brief Function for configuring the GPIO pin range as inputs with given initial value set, hiding inner details.
bogdanm 0:eff01767de02 101 * This function can be used to configure pin range as simple input.
bogdanm 0:eff01767de02 102 *
bogdanm 0:eff01767de02 103 * @param pin_range_start specifies the start number (inclusive) in the range of pin numbers to be configured (allowed values 0-30)
bogdanm 0:eff01767de02 104 *
bogdanm 0:eff01767de02 105 * @param pin_range_end specifies the end number (inclusive) in the range of pin numbers to be configured (allowed values 0-30)
bogdanm 0:eff01767de02 106 *
bogdanm 0:eff01767de02 107 * @param pull_config State of the pin range pull resistor (no pull, pulled down or pulled high)
bogdanm 0:eff01767de02 108 *
bogdanm 0:eff01767de02 109 * @note For configuring only one pin as input use @ref nrf_gpio_cfg_input
bogdanm 0:eff01767de02 110 * Sense capability on the pin is disabled, and input is connected to buffer so that the GPIO->IN register is readable
bogdanm 0:eff01767de02 111 */
bogdanm 0:eff01767de02 112 static __INLINE void nrf_gpio_range_cfg_input(uint32_t pin_range_start, uint32_t pin_range_end, nrf_gpio_pin_pull_t pull_config)
bogdanm 0:eff01767de02 113 {
bogdanm 0:eff01767de02 114 /*lint -e{845} // A zero has been given as right argument to operator '|'" */
bogdanm 0:eff01767de02 115 for (; pin_range_start <= pin_range_end; pin_range_start++)
bogdanm 0:eff01767de02 116 {
bogdanm 0:eff01767de02 117 NRF_GPIO->PIN_CNF[pin_range_start] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
bogdanm 0:eff01767de02 118 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
bogdanm 0:eff01767de02 119 | (pull_config << GPIO_PIN_CNF_PULL_Pos)
bogdanm 0:eff01767de02 120 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
bogdanm 0:eff01767de02 121 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
bogdanm 0:eff01767de02 122 }
bogdanm 0:eff01767de02 123 }
bogdanm 0:eff01767de02 124
bogdanm 0:eff01767de02 125 /**
bogdanm 0:eff01767de02 126 * @brief Function for configuring the given GPIO pin number as output with given initial value set, hiding inner details.
bogdanm 0:eff01767de02 127 * This function can be used to configure pin range as simple input with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
bogdanm 0:eff01767de02 128 *
bogdanm 0:eff01767de02 129 * @param pin_number specifies the pin number of gpio pin numbers to be configured (allowed values 0-30)
bogdanm 0:eff01767de02 130 *
bogdanm 0:eff01767de02 131 * @note Sense capability on the pin is disabled, and input is disconnected from the buffer as the pins are configured as output.
bogdanm 0:eff01767de02 132 */
bogdanm 0:eff01767de02 133 static __INLINE void nrf_gpio_cfg_output(uint32_t pin_number)
bogdanm 0:eff01767de02 134 {
bogdanm 0:eff01767de02 135 /*lint -e{845} // A zero has been given as right argument to operator '|'" */
bogdanm 0:eff01767de02 136 NRF_GPIO->PIN_CNF[pin_number] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
bogdanm 0:eff01767de02 137 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
bogdanm 0:eff01767de02 138 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
bogdanm 0:eff01767de02 139 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
bogdanm 0:eff01767de02 140 | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
bogdanm 0:eff01767de02 141 }
bogdanm 0:eff01767de02 142
bogdanm 0:eff01767de02 143 /**
bogdanm 0:eff01767de02 144 * @brief Function for configuring the given GPIO pin number as input with given initial value set, hiding inner details.
bogdanm 0:eff01767de02 145 * This function can be used to configure pin range as simple input with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
bogdanm 0:eff01767de02 146 *
bogdanm 0:eff01767de02 147 * @param pin_number specifies the pin number of gpio pin numbers to be configured (allowed values 0-30)
bogdanm 0:eff01767de02 148 *
bogdanm 0:eff01767de02 149 * @param pull_config State of the pin range pull resistor (no pull, pulled down or pulled high)
bogdanm 0:eff01767de02 150 *
bogdanm 0:eff01767de02 151 * @note Sense capability on the pin is disabled, and input is connected to buffer so that the GPIO->IN register is readable
bogdanm 0:eff01767de02 152 */
bogdanm 0:eff01767de02 153 static __INLINE void nrf_gpio_cfg_input(uint32_t pin_number, nrf_gpio_pin_pull_t pull_config)
bogdanm 0:eff01767de02 154 {
bogdanm 0:eff01767de02 155 /*lint -e{845} // A zero has been given as right argument to operator '|'" */
bogdanm 0:eff01767de02 156 NRF_GPIO->PIN_CNF[pin_number] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
bogdanm 0:eff01767de02 157 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
bogdanm 0:eff01767de02 158 | (pull_config << GPIO_PIN_CNF_PULL_Pos)
bogdanm 0:eff01767de02 159 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
bogdanm 0:eff01767de02 160 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
bogdanm 0:eff01767de02 161 }
bogdanm 0:eff01767de02 162
bogdanm 0:eff01767de02 163 /**
bogdanm 0:eff01767de02 164 * @brief Function for configuring the given GPIO pin number as input with given initial value set, hiding inner details.
bogdanm 0:eff01767de02 165 * This function can be used to configure pin range as simple input with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
bogdanm 0:eff01767de02 166 * Sense capability on the pin is configurable, and input is connected to buffer so that the GPIO->IN register is readable.
bogdanm 0:eff01767de02 167 *
bogdanm 0:eff01767de02 168 * @param pin_number specifies the pin number of gpio pin numbers to be configured (allowed values 0-30).
bogdanm 0:eff01767de02 169 *
bogdanm 0:eff01767de02 170 * @param pull_config state of the pin pull resistor (no pull, pulled down or pulled high).
bogdanm 0:eff01767de02 171 *
bogdanm 0:eff01767de02 172 * @param sense_config sense level of the pin (no sense, sense low or sense high).
bogdanm 0:eff01767de02 173 */
bogdanm 0:eff01767de02 174 static __INLINE void nrf_gpio_cfg_sense_input(uint32_t pin_number, nrf_gpio_pin_pull_t pull_config, nrf_gpio_pin_sense_t sense_config)
bogdanm 0:eff01767de02 175 {
bogdanm 0:eff01767de02 176 /*lint -e{845} // A zero has been given as right argument to operator '|'" */
bogdanm 0:eff01767de02 177 NRF_GPIO->PIN_CNF[pin_number] = (sense_config << GPIO_PIN_CNF_SENSE_Pos)
bogdanm 0:eff01767de02 178 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
bogdanm 0:eff01767de02 179 | (pull_config << GPIO_PIN_CNF_PULL_Pos)
bogdanm 0:eff01767de02 180 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
bogdanm 0:eff01767de02 181 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
bogdanm 0:eff01767de02 182 }
bogdanm 0:eff01767de02 183
bogdanm 0:eff01767de02 184 /**
bogdanm 0:eff01767de02 185 * @brief Function for setting the direction for a GPIO pin.
bogdanm 0:eff01767de02 186 *
bogdanm 0:eff01767de02 187 * @param pin_number specifies the pin number [0:31] for which to
bogdanm 0:eff01767de02 188 * set the direction.
bogdanm 0:eff01767de02 189 *
bogdanm 0:eff01767de02 190 * @param direction specifies the direction
bogdanm 0:eff01767de02 191 */
bogdanm 0:eff01767de02 192 static __INLINE void nrf_gpio_pin_dir_set(uint32_t pin_number, nrf_gpio_pin_dir_t direction)
bogdanm 0:eff01767de02 193 {
bogdanm 0:eff01767de02 194 if(direction == NRF_GPIO_PIN_DIR_INPUT)
bogdanm 0:eff01767de02 195 {
bogdanm 0:eff01767de02 196 NRF_GPIO->PIN_CNF[pin_number] =
bogdanm 0:eff01767de02 197 (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
bogdanm 0:eff01767de02 198 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
bogdanm 0:eff01767de02 199 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
bogdanm 0:eff01767de02 200 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
bogdanm 0:eff01767de02 201 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
bogdanm 0:eff01767de02 202 }
bogdanm 0:eff01767de02 203 else
bogdanm 0:eff01767de02 204 {
bogdanm 0:eff01767de02 205 NRF_GPIO->DIRSET = (1UL << pin_number);
bogdanm 0:eff01767de02 206 }
bogdanm 0:eff01767de02 207 }
bogdanm 0:eff01767de02 208
bogdanm 0:eff01767de02 209 /**
bogdanm 0:eff01767de02 210 * @brief Function for setting a GPIO pin.
bogdanm 0:eff01767de02 211 *
bogdanm 0:eff01767de02 212 * Note that the pin must be configured as an output for this
bogdanm 0:eff01767de02 213 * function to have any effect.
bogdanm 0:eff01767de02 214 *
bogdanm 0:eff01767de02 215 * @param pin_number specifies the pin number [0:31] to
bogdanm 0:eff01767de02 216 * set.
bogdanm 0:eff01767de02 217 */
bogdanm 0:eff01767de02 218 static __INLINE void nrf_gpio_pin_set(uint32_t pin_number)
bogdanm 0:eff01767de02 219 {
bogdanm 0:eff01767de02 220 NRF_GPIO->OUTSET = (1UL << pin_number);
bogdanm 0:eff01767de02 221 }
bogdanm 0:eff01767de02 222
bogdanm 0:eff01767de02 223 /**
bogdanm 0:eff01767de02 224 * @brief Function for clearing a GPIO pin.
bogdanm 0:eff01767de02 225 *
bogdanm 0:eff01767de02 226 * Note that the pin must be configured as an output for this
bogdanm 0:eff01767de02 227 * function to have any effect.
bogdanm 0:eff01767de02 228 *
bogdanm 0:eff01767de02 229 * @param pin_number specifies the pin number [0:31] to
bogdanm 0:eff01767de02 230 * clear.
bogdanm 0:eff01767de02 231 */
bogdanm 0:eff01767de02 232 static __INLINE void nrf_gpio_pin_clear(uint32_t pin_number)
bogdanm 0:eff01767de02 233 {
bogdanm 0:eff01767de02 234 NRF_GPIO->OUTCLR = (1UL << pin_number);
bogdanm 0:eff01767de02 235 }
bogdanm 0:eff01767de02 236
bogdanm 0:eff01767de02 237 /**
bogdanm 0:eff01767de02 238 * @brief Function for toggling a GPIO pin.
bogdanm 0:eff01767de02 239 *
bogdanm 0:eff01767de02 240 * Note that the pin must be configured as an output for this
bogdanm 0:eff01767de02 241 * function to have any effect.
bogdanm 0:eff01767de02 242 *
bogdanm 0:eff01767de02 243 * @param pin_number specifies the pin number [0:31] to
bogdanm 0:eff01767de02 244 * toggle.
bogdanm 0:eff01767de02 245 */
bogdanm 0:eff01767de02 246 static __INLINE void nrf_gpio_pin_toggle(uint32_t pin_number)
bogdanm 0:eff01767de02 247 {
bogdanm 0:eff01767de02 248 NRF_GPIO->OUT ^= (1UL << pin_number);
bogdanm 0:eff01767de02 249 }
bogdanm 0:eff01767de02 250
bogdanm 0:eff01767de02 251 /**
bogdanm 0:eff01767de02 252 * @brief Function for writing a value to a GPIO pin.
bogdanm 0:eff01767de02 253 *
bogdanm 0:eff01767de02 254 * Note that the pin must be configured as an output for this
bogdanm 0:eff01767de02 255 * function to have any effect.
bogdanm 0:eff01767de02 256 *
bogdanm 0:eff01767de02 257 * @param pin_number specifies the pin number [0:31] to
bogdanm 0:eff01767de02 258 * write.
bogdanm 0:eff01767de02 259 *
bogdanm 0:eff01767de02 260 * @param value specifies the value to be written to the pin.
bogdanm 0:eff01767de02 261 * @arg 0 clears the pin
bogdanm 0:eff01767de02 262 * @arg >=1 sets the pin.
bogdanm 0:eff01767de02 263 */
bogdanm 0:eff01767de02 264 static __INLINE void nrf_gpio_pin_write(uint32_t pin_number, uint32_t value)
bogdanm 0:eff01767de02 265 {
bogdanm 0:eff01767de02 266 if (value == 0)
bogdanm 0:eff01767de02 267 {
bogdanm 0:eff01767de02 268 nrf_gpio_pin_clear(pin_number);
bogdanm 0:eff01767de02 269 }
bogdanm 0:eff01767de02 270 else
bogdanm 0:eff01767de02 271 {
bogdanm 0:eff01767de02 272 nrf_gpio_pin_set(pin_number);
bogdanm 0:eff01767de02 273 }
bogdanm 0:eff01767de02 274 }
bogdanm 0:eff01767de02 275
bogdanm 0:eff01767de02 276 /**
bogdanm 0:eff01767de02 277 * @brief Function for reading the input level of a GPIO pin.
bogdanm 0:eff01767de02 278 *
bogdanm 0:eff01767de02 279 * Note that the pin must have input connected for the value
bogdanm 0:eff01767de02 280 * returned from this function to be valid.
bogdanm 0:eff01767de02 281 *
bogdanm 0:eff01767de02 282 * @param pin_number specifies the pin number [0:31] to
bogdanm 0:eff01767de02 283 * read.
bogdanm 0:eff01767de02 284 *
bogdanm 0:eff01767de02 285 * @return
bogdanm 0:eff01767de02 286 * @retval 0 if the pin input level is low.
bogdanm 0:eff01767de02 287 * @retval 1 if the pin input level is high.
bogdanm 0:eff01767de02 288 * @retval > 1 should never occur.
bogdanm 0:eff01767de02 289 */
bogdanm 0:eff01767de02 290 static __INLINE uint32_t nrf_gpio_pin_read(uint32_t pin_number)
bogdanm 0:eff01767de02 291 {
bogdanm 0:eff01767de02 292 return ((NRF_GPIO->IN >> pin_number) & 1UL);
bogdanm 0:eff01767de02 293 }
bogdanm 0:eff01767de02 294
bogdanm 0:eff01767de02 295 /**
bogdanm 0:eff01767de02 296 * @brief Generic function for writing a single byte of a 32 bit word at a given
bogdanm 0:eff01767de02 297 * address.
bogdanm 0:eff01767de02 298 *
bogdanm 0:eff01767de02 299 * This function should not be called from outside the nrf_gpio
bogdanm 0:eff01767de02 300 * abstraction layer.
bogdanm 0:eff01767de02 301 *
bogdanm 0:eff01767de02 302 * @param word_address is the address of the word to be written.
bogdanm 0:eff01767de02 303 *
bogdanm 0:eff01767de02 304 * @param byte_no is the the word byte number (0-3) to be written.
bogdanm 0:eff01767de02 305 *
bogdanm 0:eff01767de02 306 * @param value is the value to be written to byte "byte_no" of word
bogdanm 0:eff01767de02 307 * at address "word_address"
bogdanm 0:eff01767de02 308 */
bogdanm 0:eff01767de02 309 static __INLINE void nrf_gpio_word_byte_write(volatile uint32_t * word_address, uint8_t byte_no, uint8_t value)
bogdanm 0:eff01767de02 310 {
bogdanm 0:eff01767de02 311 *((volatile uint8_t*)(word_address) + byte_no) = value;
bogdanm 0:eff01767de02 312 }
bogdanm 0:eff01767de02 313
bogdanm 0:eff01767de02 314 /**
bogdanm 0:eff01767de02 315 * @brief Generic function for reading a single byte of a 32 bit word at a given
bogdanm 0:eff01767de02 316 * address.
bogdanm 0:eff01767de02 317 *
bogdanm 0:eff01767de02 318 * This function should not be called from outside the nrf_gpio
bogdanm 0:eff01767de02 319 * abstraction layer.
bogdanm 0:eff01767de02 320 *
bogdanm 0:eff01767de02 321 * @param word_address is the address of the word to be read.
bogdanm 0:eff01767de02 322 *
bogdanm 0:eff01767de02 323 * @param byte_no is the the byte number (0-3) of the word to be read.
bogdanm 0:eff01767de02 324 *
bogdanm 0:eff01767de02 325 * @return byte "byte_no" of word at address "word_address".
bogdanm 0:eff01767de02 326 */
bogdanm 0:eff01767de02 327 static __INLINE uint8_t nrf_gpio_word_byte_read(const volatile uint32_t* word_address, uint8_t byte_no)
bogdanm 0:eff01767de02 328 {
bogdanm 0:eff01767de02 329 return (*((const volatile uint8_t*)(word_address) + byte_no));
bogdanm 0:eff01767de02 330 }
bogdanm 0:eff01767de02 331
bogdanm 0:eff01767de02 332 /**
bogdanm 0:eff01767de02 333 * @brief Function for setting the direction of a port.
bogdanm 0:eff01767de02 334 *
bogdanm 0:eff01767de02 335 * @param port is the port for which to set the direction.
bogdanm 0:eff01767de02 336 *
bogdanm 0:eff01767de02 337 * @param dir direction to be set for this port.
bogdanm 0:eff01767de02 338 */
bogdanm 0:eff01767de02 339 static __INLINE void nrf_gpio_port_dir_set(nrf_gpio_port_select_t port, nrf_gpio_port_dir_t dir)
bogdanm 0:eff01767de02 340 {
bogdanm 0:eff01767de02 341 if (dir == NRF_GPIO_PORT_DIR_OUTPUT)
bogdanm 0:eff01767de02 342 {
bogdanm 0:eff01767de02 343 nrf_gpio_word_byte_write(&NRF_GPIO->DIRSET, port, 0xFF);
bogdanm 0:eff01767de02 344 }
bogdanm 0:eff01767de02 345 else
bogdanm 0:eff01767de02 346 {
bogdanm 0:eff01767de02 347 nrf_gpio_range_cfg_input(port*8, (port+1)*8-1, NRF_GPIO_PIN_NOPULL);
bogdanm 0:eff01767de02 348 }
bogdanm 0:eff01767de02 349 }
bogdanm 0:eff01767de02 350
bogdanm 0:eff01767de02 351 /**
bogdanm 0:eff01767de02 352 * @brief Function for reading a GPIO port.
bogdanm 0:eff01767de02 353 *
bogdanm 0:eff01767de02 354 * @param port is the port to read.
bogdanm 0:eff01767de02 355 *
bogdanm 0:eff01767de02 356 * @return the input value on this port.
bogdanm 0:eff01767de02 357 */
bogdanm 0:eff01767de02 358 static __INLINE uint8_t nrf_gpio_port_read(nrf_gpio_port_select_t port)
bogdanm 0:eff01767de02 359 {
bogdanm 0:eff01767de02 360 return nrf_gpio_word_byte_read(&NRF_GPIO->IN, port);
bogdanm 0:eff01767de02 361 }
bogdanm 0:eff01767de02 362
bogdanm 0:eff01767de02 363 /**
bogdanm 0:eff01767de02 364 * @brief Function for writing to a GPIO port.
bogdanm 0:eff01767de02 365 *
bogdanm 0:eff01767de02 366 * @param port is the port to write.
bogdanm 0:eff01767de02 367 *
bogdanm 0:eff01767de02 368 * @param value is the value to write to this port.
bogdanm 0:eff01767de02 369 *
bogdanm 0:eff01767de02 370 * @sa nrf_gpio_port_dir_set()
bogdanm 0:eff01767de02 371 */
bogdanm 0:eff01767de02 372 static __INLINE void nrf_gpio_port_write(nrf_gpio_port_select_t port, uint8_t value)
bogdanm 0:eff01767de02 373 {
bogdanm 0:eff01767de02 374 nrf_gpio_word_byte_write(&NRF_GPIO->OUT, port, value);
bogdanm 0:eff01767de02 375 }
bogdanm 0:eff01767de02 376
bogdanm 0:eff01767de02 377 /**
bogdanm 0:eff01767de02 378 * @brief Function for setting individual pins on GPIO port.
bogdanm 0:eff01767de02 379 *
bogdanm 0:eff01767de02 380 * @param port is the port for which to set the pins.
bogdanm 0:eff01767de02 381 *
bogdanm 0:eff01767de02 382 * @param set_mask is a mask specifying which pins to set. A bit
bogdanm 0:eff01767de02 383 * set to 1 indicates that the corresponding port pin shall be
bogdanm 0:eff01767de02 384 * set.
bogdanm 0:eff01767de02 385 *
bogdanm 0:eff01767de02 386 * @sa nrf_gpio_port_dir_set()
bogdanm 0:eff01767de02 387 */
bogdanm 0:eff01767de02 388 static __INLINE void nrf_gpio_port_set(nrf_gpio_port_select_t port, uint8_t set_mask)
bogdanm 0:eff01767de02 389 {
bogdanm 0:eff01767de02 390 nrf_gpio_word_byte_write(&NRF_GPIO->OUTSET, port, set_mask);
bogdanm 0:eff01767de02 391 }
bogdanm 0:eff01767de02 392
bogdanm 0:eff01767de02 393 /**
bogdanm 0:eff01767de02 394 * @brief Function for clearing individual pins on GPIO port.
bogdanm 0:eff01767de02 395 *
bogdanm 0:eff01767de02 396 * @param port is the port for which to clear the pins.
bogdanm 0:eff01767de02 397 *
bogdanm 0:eff01767de02 398 * @param clr_mask is a mask specifying which pins to clear. A bit
bogdanm 0:eff01767de02 399 * set to 1 indicates that the corresponding port pin shall be
bogdanm 0:eff01767de02 400 * cleared.
bogdanm 0:eff01767de02 401 *
bogdanm 0:eff01767de02 402 * @sa nrf_gpio_port_dir_set()
bogdanm 0:eff01767de02 403 */
bogdanm 0:eff01767de02 404 static __INLINE void nrf_gpio_port_clear(nrf_gpio_port_select_t port, uint8_t clr_mask)
bogdanm 0:eff01767de02 405 {
bogdanm 0:eff01767de02 406 nrf_gpio_word_byte_write(&NRF_GPIO->OUTCLR, port, clr_mask);
bogdanm 0:eff01767de02 407 }
bogdanm 0:eff01767de02 408
bogdanm 0:eff01767de02 409 /** @} */
bogdanm 0:eff01767de02 410
bogdanm 0:eff01767de02 411 #endif