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