LIS3DH & BLE broadcast example for VTT Node V3 & mbed

Dependencies:   BLE_API TMP_nrf51 mbed nRF51822

Committer:
jejuho
Date:
Mon Jan 25 13:27:15 2016 +0000
Revision:
1:bd7fd35251ab
Parent:
0:de3e4a57ebe0
Initial version.

Who changed what in which revision?

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