Skip to content

Commit

Permalink
cpu/cc2538: use bit-banding in the low-level GPIO macros too
Browse files Browse the repository at this point in the history
  • Loading branch information
hexluthor committed Aug 22, 2016
1 parent 174ab47 commit bc6d217
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions cpu/cc2538/include/cc2538_gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ enum {
#define GPIO_BITS_PER_PORT ( 1 << GPIO_PORT_SHIFT ) /**< Number of bits per GPIO port (8) */
#define GPIO_BIT_MASK ( GPIO_BITS_PER_PORT - 1 ) /**< Mask to obtain the bit number from a GPIO number */

/**
* @brief Generate a bit mask in which only the specified bit is high.
*
* @param[in] n Number of the bit to set high in the mask.
*
* @return A bit mask in which bit n is high.
*/
#define GPIO_PIN_MASK(n) ( 1 << (n) )

/**
* @brief Extract the GPIO port number (0-3) from a GPIO number (0-31)
*
Expand Down Expand Up @@ -99,51 +90,51 @@ enum {
*
* @param[in] gpio_num GPIO number (0-31)
*/
#define gpio_hardware_control(gpio_num) ( GPIO_NUM_TO_DEV(gpio_num)->AFSEL |= GPIO_PIN_MASK(GPIO_BIT_NUM(gpio_num)) )
#define gpio_hardware_control(gpio_num) ( BITBAND_VAR32(GPIO_NUM_TO_DEV(gpio_num)->AFSEL, GPIO_BIT_NUM(gpio_num)) = 1 )

/**
* @brief Enable software control for a given GPIO pin number
*
* @param[in] gpio_num GPIO number (0-31)
*/
#define gpio_software_control(gpio_num) ( GPIO_NUM_TO_DEV(gpio_num)->AFSEL &= ~GPIO_PIN_MASK(GPIO_BIT_NUM(gpio_num)) )
#define gpio_software_control(gpio_num) ( BITBAND_VAR32(GPIO_NUM_TO_DEV(gpio_num)->AFSEL, GPIO_BIT_NUM(gpio_num)) = 0 )

/**
* @brief Configure the given GPIO as an output
*
* @param[in] gpio_num GPIO number (0-31)
*/
#define gpio_dir_output(gpio_num) ( GPIO_NUM_TO_DEV(gpio_num)->DIR |= GPIO_PIN_MASK(GPIO_BIT_NUM(gpio_num)) )
#define gpio_dir_output(gpio_num) ( BITBAND_VAR32(GPIO_NUM_TO_DEV(gpio_num)->DIR, GPIO_BIT_NUM(gpio_num)) = 1 )

/**
* @brief Configure the given GPIO as an input
*
* @param[in] gpio_num GPIO number (0-31)
*/
#define gpio_dir_input(gpio_num) ( GPIO_NUM_TO_DEV(gpio_num)->DIR &= ~GPIO_PIN_MASK(GPIO_BIT_NUM(gpio_num)) )
#define gpio_dir_input(gpio_num) ( BITBAND_VAR32(GPIO_NUM_TO_DEV(gpio_num)->DIR, GPIO_BIT_NUM(gpio_num)) = 0 )

#define cc2538_gpio_read(gpio_num) ( (GPIO_NUM_TO_DEV(gpio_num)->DATA >> GPIO_BIT_NUM(gpio_num)) & 1 )
#define cc2538_gpio_read(gpio_num) BITBAND_VAR32(GPIO_NUM_TO_DEV(gpio_num)->DATA, GPIO_BIT_NUM(gpio_num))

/**
* @brief Set a specific GPIO output pin high
*
* @param[in] gpio_num GPIO number (0-31)
*/
#define cc2538_gpio_set(gpio_num) ( GPIO_NUM_TO_DEV(gpio_num)->DATA |= GPIO_PIN_MASK(GPIO_BIT_NUM(gpio_num)) )
#define cc2538_gpio_set(gpio_num) ( BITBAND_VAR32(GPIO_NUM_TO_DEV(gpio_num)->DATA, GPIO_BIT_NUM(gpio_num)) = 1 )

/**
* @brief Set a specific GPIO output pin low
*
* @param[in] gpio_num GPIO number (0-31)
*/
#define cc2538_gpio_clear(gpio_num) ( GPIO_NUM_TO_DEV(gpio_num)->DATA &= ~GPIO_PIN_MASK(GPIO_BIT_NUM(gpio_num)) )
#define cc2538_gpio_clear(gpio_num) ( BITBAND_VAR32(GPIO_NUM_TO_DEV(gpio_num)->DATA, GPIO_BIT_NUM(gpio_num)) = 0 )

/**
* @brief Toggle the output state of a specific GPIO pin
*
* @param[in] gpio_num GPIO number (0-31)
*/
#define cc2538_gpio_toggle(gpio_num) ( GPIO_NUM_TO_DEV(gpio_num)->DATA ^= GPIO_PIN_MASK(GPIO_BIT_NUM(gpio_num)) )
#define cc2538_gpio_toggle(gpio_num) ( BITBAND_VAR32(GPIO_NUM_TO_DEV(gpio_num)->DATA, GPIO_BIT_NUM(gpio_num)) ^= 1 )

/** @name Unique names for each GPIO port/pin combination
* @{
Expand Down

0 comments on commit bc6d217

Please sign in to comment.