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