BLE FOTA APP

Dependencies:   BLE_API mbed

It doesn't work with the default FOTA bootloader. It use NVIC_SystemReset() to enter a bootloader.

Committer:
yihui
Date:
Fri Oct 10 03:36:28 2014 +0000
Revision:
1:a607cd9655d7
use NVIC_SystemReset() to run bootloader

Who changed what in which revision?

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