modified locally to use with HRM1017

Dependents:   BLE_HTM_HRM1017

Fork of nRF51822 by Nordic Semiconductor

Committer:
Rohit Grover
Date:
Mon Jul 07 13:43:31 2014 +0100
Revision:
37:c29c330d942c
Parent:
0:eff01767de02
changes required to upgrade to V7 of the soft-device

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