first commit

Dependencies:   mbed

Dependents:   ibeacon

Fork of BLE_WallbotBLE_Challenge by JKSoft

Committer:
jksoft
Date:
Wed Nov 12 02:40:34 2014 +0000
Revision:
0:76dfa9657d9d
????????

Who changed what in which revision?

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