To get started with Seeed Tiny BLE, include detecting motion, button and battery level.

Dependencies:   BLE_API eMPL_MPU6050 mbed nRF51822

Committer:
yihui
Date:
Wed Apr 22 07:47:17 2015 +0000
Revision:
1:fc2f9d636751
update libraries; ; delete nRF51822/nordic-sdk/components/gpiote/app_gpiote.c to solve GPIOTE_IRQHandler multiply defined issue. temperarily change nRF51822 library to folder

Who changed what in which revision?

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