changed low freq. clock source to IRC

Dependents:   BLE_ANCS_SDAPI_IRC

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nrf_gpio.h Source File

nrf_gpio.h

00001 #ifndef NRF_GPIO_H__
00002 #define NRF_GPIO_H__
00003 
00004 #include "nordic_global.h"
00005 #include "nrf51.h"
00006 #include "nrf51_bitfields.h"
00007 
00008 /**
00009  * @defgroup nrf_gpio GPIO abstraction
00010  * @{
00011  * @ingroup nrf_drivers
00012  * @brief GPIO pin abstraction and port abstraction for reading and writing byte-wise to GPIO ports.
00013  *
00014  * Here, the GPIO ports are defined as follows:
00015  * - Port 0 -> pin 0-7
00016  * - Port 1 -> pin 8-15
00017  * - Port 2 -> pin 16-23
00018  * - Port 3 -> pin 24-31
00019  */
00020 
00021 /**
00022  * @enum nrf_gpio_port_dir_t
00023  * @brief Enumerator used for setting the direction of a GPIO port.
00024  */
00025 typedef enum
00026 {
00027     NRF_GPIO_PORT_DIR_OUTPUT,       ///<  Output
00028     NRF_GPIO_PORT_DIR_INPUT         ///<  Input
00029 } nrf_gpio_port_dir_t;
00030 
00031 /**
00032  * @enum nrf_gpio_pin_dir_t
00033  * Pin direction definitions.
00034  */
00035 typedef enum
00036 {
00037     NRF_GPIO_PIN_DIR_INPUT,   ///< Input
00038     NRF_GPIO_PIN_DIR_OUTPUT   ///< Output
00039 } nrf_gpio_pin_dir_t;
00040 
00041 /**
00042  * @enum nrf_gpio_port_select_t
00043  * @brief Enumerator used for selecting between port 0 - 3.
00044  */
00045 typedef enum
00046 {
00047     NRF_GPIO_PORT_SELECT_PORT0 = 0,           ///<  Port 0 (GPIO pin 0-7)
00048     NRF_GPIO_PORT_SELECT_PORT1,               ///<  Port 1 (GPIO pin 8-15)
00049     NRF_GPIO_PORT_SELECT_PORT2,               ///<  Port 2 (GPIO pin 16-23)
00050     NRF_GPIO_PORT_SELECT_PORT3,               ///<  Port 3 (GPIO pin 24-31)
00051 } nrf_gpio_port_select_t;
00052 
00053 /**
00054  * @enum nrf_gpio_pin_pull_t
00055  * @brief Enumerator used for selecting the pin to be pulled down or up at the time of pin configuration
00056  */
00057 typedef enum
00058 {
00059     NRF_GPIO_PIN_NOPULL   = GPIO_PIN_CNF_PULL_Disabled,                 ///<  Pin pullup resistor disabled
00060     NRF_GPIO_PIN_PULLDOWN = GPIO_PIN_CNF_PULL_Pulldown,                 ///<  Pin pulldown resistor enabled
00061     NRF_GPIO_PIN_PULLUP   = GPIO_PIN_CNF_PULL_Pullup,                   ///<  Pin pullup resistor enabled
00062 } nrf_gpio_pin_pull_t;
00063 
00064 /**
00065  * @enum nrf_gpio_pin_sense_t
00066  * @brief Enumerator used for selecting the pin to sense high or low level on the pin input.
00067  */
00068 typedef enum
00069 {
00070     NRF_GPIO_PIN_NOSENSE    = GPIO_PIN_CNF_SENSE_Disabled,              ///<  Pin sense level disabled.
00071     NRF_GPIO_PIN_SENSE_LOW  = GPIO_PIN_CNF_SENSE_Low,                   ///<  Pin sense low level.
00072     NRF_GPIO_PIN_SENSE_HIGH = GPIO_PIN_CNF_SENSE_High,                  ///<  Pin sense high level.
00073 } nrf_gpio_pin_sense_t;
00074 
00075 /**
00076  * @brief Function for configuring the GPIO pin range as outputs with normal drive strength.
00077  *        This function can be used to configure pin range as simple output with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
00078  *
00079  * @param pin_range_start specifies the start number (inclusive) in the range of pin numbers to be configured (allowed values 0-30)
00080  *
00081  * @param pin_range_end specifies the end number (inclusive) in the range of pin numbers to be configured (allowed values 0-30)
00082  *
00083  * @note For configuring only one pin as output use @ref nrf_gpio_cfg_output
00084  *       Sense capability on the pin is disabled, and input is disconnected from the buffer as the pins are configured as output.
00085  */
00086 static __INLINE void nrf_gpio_range_cfg_output(uint32_t pin_range_start, uint32_t pin_range_end)
00087 {
00088     /*lint -e{845} // A zero has been given as right argument to operator '|'" */
00089     for (; pin_range_start <= pin_range_end; pin_range_start++)
00090     {
00091         NRF_GPIO->PIN_CNF[pin_range_start] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
00092                                         | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
00093                                         | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
00094                                         | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
00095                                         | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
00096     }
00097 }
00098 
00099 /**
00100  * @brief Function for configuring the GPIO pin range as inputs with given initial value set, hiding inner details.
00101  *        This function can be used to configure pin range as simple input.
00102  *
00103  * @param pin_range_start specifies the start number (inclusive) in the range of pin numbers to be configured (allowed values 0-30)
00104  *
00105  * @param pin_range_end specifies the end number (inclusive) in the range of pin numbers to be configured (allowed values 0-30)
00106  *
00107  * @param pull_config State of the pin range pull resistor (no pull, pulled down or pulled high)
00108  *
00109  * @note  For configuring only one pin as input use @ref nrf_gpio_cfg_input
00110  *        Sense capability on the pin is disabled, and input is connected to buffer so that the GPIO->IN register is readable
00111  */
00112 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)
00113 {
00114     /*lint -e{845} // A zero has been given as right argument to operator '|'" */
00115     for (; pin_range_start <= pin_range_end; pin_range_start++)
00116     {
00117         NRF_GPIO->PIN_CNF[pin_range_start] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
00118                                         | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
00119                                         | (pull_config << GPIO_PIN_CNF_PULL_Pos)
00120                                         | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
00121                                         | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
00122     }
00123 }
00124 
00125 /**
00126  * @brief Function for configuring the given GPIO pin number as output with given initial value set, hiding inner details.
00127  *        This function can be used to configure pin range as simple input with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
00128  *
00129  * @param pin_number specifies the pin number of gpio pin numbers to be configured (allowed values 0-30)
00130  *
00131  * @note  Sense capability on the pin is disabled, and input is disconnected from the buffer as the pins are configured as output.
00132  */
00133 static __INLINE void nrf_gpio_cfg_output(uint32_t pin_number)
00134 {
00135     /*lint -e{845} // A zero has been given as right argument to operator '|'" */
00136     NRF_GPIO->PIN_CNF[pin_number] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
00137                                             | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
00138                                             | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
00139                                             | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
00140                                             | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
00141 }
00142 
00143 /**
00144  * @brief Function for configuring the given GPIO pin number as input with given initial value set, hiding inner details.
00145  *        This function can be used to configure pin range as simple input with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
00146  *
00147  * @param pin_number specifies the pin number of gpio pin numbers to be configured (allowed values 0-30)
00148  *
00149  * @param pull_config State of the pin range pull resistor (no pull, pulled down or pulled high)
00150  *
00151  * @note  Sense capability on the pin is disabled, and input is connected to buffer so that the GPIO->IN register is readable
00152  */
00153 static __INLINE void nrf_gpio_cfg_input(uint32_t pin_number, nrf_gpio_pin_pull_t pull_config)
00154 {
00155     /*lint -e{845} // A zero has been given as right argument to operator '|'" */
00156     NRF_GPIO->PIN_CNF[pin_number] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
00157                                         | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
00158                                         | (pull_config << GPIO_PIN_CNF_PULL_Pos)
00159                                         | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
00160                                         | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
00161 }
00162 
00163 /**
00164  * @brief Function for configuring the given GPIO pin number as input with given initial value set, hiding inner details.
00165  *        This function can be used to configure pin range as simple input with gate driving GPIO_PIN_CNF_DRIVE_S0S1 (normal cases).
00166  *        Sense capability on the pin is configurable, and input is connected to buffer so that the GPIO->IN register is readable.
00167  *
00168  * @param pin_number specifies the pin number of gpio pin numbers to be configured (allowed values 0-30).
00169  *
00170  * @param pull_config state of the pin pull resistor (no pull, pulled down or pulled high).
00171  *
00172  * @param sense_config sense level of the pin (no sense, sense low or sense high).
00173  */
00174 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)
00175 {
00176     /*lint -e{845} // A zero has been given as right argument to operator '|'" */
00177     NRF_GPIO->PIN_CNF[pin_number] = (sense_config << GPIO_PIN_CNF_SENSE_Pos)
00178                                         | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
00179                                         | (pull_config << GPIO_PIN_CNF_PULL_Pos)
00180                                         | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
00181                                         | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
00182 }
00183 
00184 /**
00185  * @brief Function for setting the direction for a GPIO pin.
00186  *
00187  * @param pin_number specifies the pin number [0:31] for which to
00188  * set the direction.
00189  *
00190  * @param direction specifies the direction
00191  */
00192 static __INLINE void nrf_gpio_pin_dir_set(uint32_t pin_number, nrf_gpio_pin_dir_t direction)
00193 {
00194     if(direction == NRF_GPIO_PIN_DIR_INPUT)
00195     {
00196         NRF_GPIO->PIN_CNF[pin_number] =
00197           (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
00198         | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
00199         | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
00200         | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
00201         | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
00202     }
00203     else
00204     {
00205         NRF_GPIO->DIRSET = (1UL << pin_number);
00206     }
00207 }
00208 
00209 /**
00210  * @brief Function for setting a GPIO pin.
00211  *
00212  * Note that the pin must be configured as an output for this
00213  * function to have any effect.
00214  *
00215  * @param pin_number specifies the pin number [0:31] to
00216  * set.
00217  */
00218 static __INLINE void nrf_gpio_pin_set(uint32_t pin_number)
00219 {
00220     NRF_GPIO->OUTSET = (1UL << pin_number);
00221 }
00222 
00223 /**
00224  * @brief Function for clearing a GPIO pin.
00225  *
00226  * Note that the pin must be configured as an output for this
00227  * function to have any effect.
00228  *
00229  * @param pin_number specifies the pin number [0:31] to
00230  * clear.
00231  */
00232 static __INLINE void nrf_gpio_pin_clear(uint32_t pin_number)
00233 {
00234     NRF_GPIO->OUTCLR = (1UL << pin_number);
00235 }
00236 
00237 /**
00238  * @brief Function for toggling a GPIO pin.
00239  *
00240  * Note that the pin must be configured as an output for this
00241  * function to have any effect.
00242  *
00243  * @param pin_number specifies the pin number [0:31] to
00244  * toggle.
00245  */
00246 static __INLINE void nrf_gpio_pin_toggle(uint32_t pin_number)
00247 {
00248     NRF_GPIO->OUT ^= (1UL << pin_number);
00249 }
00250 
00251 /**
00252  * @brief Function for writing a value to a GPIO pin.
00253  *
00254  * Note that the pin must be configured as an output for this
00255  * function to have any effect.
00256  *
00257  * @param pin_number specifies the pin number [0:31] to
00258  * write.
00259  *
00260  * @param value specifies the value to be written to the pin.
00261  * @arg 0 clears the pin
00262  * @arg >=1 sets the pin.
00263  */
00264 static __INLINE void nrf_gpio_pin_write(uint32_t pin_number, uint32_t value)
00265 {
00266     if (value == 0)
00267     {
00268         nrf_gpio_pin_clear(pin_number);
00269     }
00270     else
00271     {
00272         nrf_gpio_pin_set(pin_number);
00273     }
00274 }
00275 
00276 /**
00277  * @brief Function for reading the input level of a GPIO pin.
00278  *
00279  * Note that the pin must have input connected for the value
00280  * returned from this function to be valid.
00281  *
00282  * @param pin_number specifies the pin number [0:31] to
00283  * read.
00284  *
00285  * @return
00286  * @retval 0 if the pin input level is low.
00287  * @retval 1 if the pin input level is high.
00288  * @retval > 1 should never occur.
00289  */
00290 static __INLINE uint32_t nrf_gpio_pin_read(uint32_t pin_number)
00291 {
00292     return  ((NRF_GPIO->IN >> pin_number) & 1UL);
00293 }
00294 
00295 /**
00296  * @brief Generic function for writing a single byte of a 32 bit word at a given
00297  * address.
00298  *
00299  * This function should not be called from outside the nrf_gpio
00300  * abstraction layer.
00301  *
00302  * @param word_address is the address of the word to be written.
00303  *
00304  * @param byte_no is the the word byte number (0-3) to be written.
00305  *
00306  * @param value is the value to be written to byte "byte_no" of word
00307  * at address "word_address"
00308  */
00309 static __INLINE void nrf_gpio_word_byte_write(volatile uint32_t * word_address, uint8_t byte_no, uint8_t value)
00310 {
00311     *((volatile uint8_t*)(word_address) + byte_no) = value;
00312 }
00313 
00314 /**
00315  * @brief Generic function for reading a single byte of a 32 bit word at a given
00316  * address.
00317  *
00318  * This function should not be called from outside the nrf_gpio
00319  * abstraction layer.
00320  *
00321  * @param word_address is the address of the word to be read.
00322  *
00323  * @param byte_no is the the byte number (0-3) of the word to be read.
00324  *
00325  * @return byte "byte_no" of word at address "word_address".
00326  */
00327 static __INLINE uint8_t nrf_gpio_word_byte_read(const volatile uint32_t* word_address, uint8_t byte_no)
00328 {
00329     return (*((const volatile uint8_t*)(word_address) + byte_no));
00330 }
00331 
00332 /**
00333  * @brief Function for setting the direction of a port.
00334  *
00335  * @param port is the port for which to set the direction.
00336  *
00337  * @param dir direction to be set for this port.
00338  */
00339 static __INLINE void nrf_gpio_port_dir_set(nrf_gpio_port_select_t port, nrf_gpio_port_dir_t dir)
00340 {
00341     if (dir == NRF_GPIO_PORT_DIR_OUTPUT)
00342     {
00343         nrf_gpio_word_byte_write(&NRF_GPIO->DIRSET, port, 0xFF);
00344     }
00345     else
00346     {
00347         nrf_gpio_range_cfg_input(port*8, (port+1)*8-1, NRF_GPIO_PIN_NOPULL);
00348     }
00349 }
00350 
00351 /**
00352  * @brief Function for reading a GPIO port.
00353  *
00354  * @param port is the port to read.
00355  *
00356  * @return the input value on this port.
00357  */
00358 static __INLINE uint8_t nrf_gpio_port_read(nrf_gpio_port_select_t port)
00359 {
00360     return nrf_gpio_word_byte_read(&NRF_GPIO->IN, port);
00361 }
00362 
00363 /**
00364  * @brief Function for writing to a GPIO port.
00365  *
00366  * @param port is the port to write.
00367  *
00368  * @param value is the value to write to this port.
00369  *
00370  * @sa nrf_gpio_port_dir_set()
00371  */
00372 static __INLINE void nrf_gpio_port_write(nrf_gpio_port_select_t port, uint8_t value)
00373 {
00374     nrf_gpio_word_byte_write(&NRF_GPIO->OUT, port, value);
00375 }
00376 
00377 /**
00378  * @brief Function for setting individual pins on GPIO port.
00379  *
00380  * @param port is the port for which to set the pins.
00381  *
00382  * @param set_mask is a mask specifying which pins to set. A bit
00383  * set to 1 indicates that the corresponding port pin shall be
00384  * set.
00385  *
00386  * @sa nrf_gpio_port_dir_set()
00387  */
00388 static __INLINE void nrf_gpio_port_set(nrf_gpio_port_select_t port, uint8_t set_mask)
00389 {
00390     nrf_gpio_word_byte_write(&NRF_GPIO->OUTSET, port, set_mask);
00391 }
00392 
00393 /**
00394  * @brief Function for clearing individual pins on GPIO port.
00395  *
00396  * @param port is the port for which to clear the pins.
00397  *
00398  * @param clr_mask is a mask specifying which pins to clear. A bit
00399  * set to 1 indicates that the corresponding port pin shall be
00400  * cleared.
00401  *
00402  * @sa nrf_gpio_port_dir_set()
00403  */
00404 static __INLINE void nrf_gpio_port_clear(nrf_gpio_port_select_t port, uint8_t clr_mask)
00405 {
00406     nrf_gpio_word_byte_write(&NRF_GPIO->OUTCLR, port, clr_mask);
00407 }
00408 
00409 /** @} */
00410 
00411 #endif