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