Example #1
0
/*!
    \brief      set GPIO mode
    \param[in]  gpio_periph: GPIOx(x = A,B,C,F) 
                only one parameter can be selected which is shown as below:
      \arg        GPIOx(x = A,B,C,F) 
    \param[in]  mode: gpio pin mode
      \arg        GPIO_MODE_INPUT: input mode
      \arg        GPIO_MODE_OUTPUT: output mode
      \arg        GPIO_MODE_AF: alternate function mode
      \arg        GPIO_MODE_ANALOG: analog mode
    \param[in]  pull_up_down: gpio pin with pull-up or pull-down resistor
      \arg        GPIO_PUPD_NONE: floating mode, no pull-up and pull-down resistors
      \arg        GPIO_PUPD_PULLUP: with pull-up resistor
      \arg        GPIO_PUPD_PULLDOWN:with pull-down resistor
    \param[in]  pin: GPIO pin
                one or more parameters can be selected which are shown as below:
      \arg        GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
    \param[out] none
    \retval     none
*/
void gpio_mode_set(uint32_t gpio_periph, uint32_t mode, uint32_t pull_up_down, uint32_t pin)
{
    uint16_t i;
    uint32_t ctl, pupd;

    ctl = GPIO_CTL(gpio_periph);
    pupd = GPIO_PUD(gpio_periph);

    for(i = 0U;i < 16U;i++){
        if((1U << i) & pin){
            /* clear the specified pin mode bits */
            ctl &= ~GPIO_MODE_MASK(i);
            /* set the specified pin mode bits */
            ctl |= GPIO_MODE_SET(i, mode);

            /* clear the specified pin pupd bits */
            pupd &= ~GPIO_PUPD_MASK(i);
            /* set the specified pin pupd bits */
            pupd |= GPIO_PUPD_SET(i, pull_up_down);
        }
    }

    GPIO_CTL(gpio_periph) = ctl;
    GPIO_PUD(gpio_periph) = pupd;
}
Example #2
0
 /*conf_data = 0~3*/
 void GPIO_ModeSetup(kal_uint16 pin, kal_uint16 conf_dada)
 {
    kal_uint16 modeno;
    kal_uint16 remainder;
    
    modeno = pin/8;
    remainder = pin % 8;
    #if defined(MT6226)|| defined(MT6227)
    if(pin>=56)/*special case  */
    {
       DRV_WriteReg(GPIO_MODE8_CLR,(0x0003 << (remainder*2)));
       DRV_WriteReg(GPIO_MODE8_SET,(conf_dada << (remainder*2)));
       return;
    }
    #endif                  
    
    DRV_WriteReg(GPIO_MODE_CLR(modeno),(0x0003 << (remainder*2)));
    DRV_WriteReg(GPIO_MODE_SET(modeno),(conf_dada << (remainder*2)));
 }
Example #3
0
/*!
    \brief      set GPIO output mode
    \param[in]  port
      \arg        GPIOx(x = A,B,C,D,F)
    \param[in]  mode
      \arg        GPIO_MODE_INPUT: input mode
      \arg        GPIO_MODE_OUTPUT: output mode
      \arg        GPIO_MODE_AF: alternate function mode
      \arg        GPIO_MODE_ANALOG: analog mode
    \param[in]  pull_up_down
      \arg        GPIO_PUPD_NONE: without weak pull-up and pull-down resistors
      \arg        GPIO_PUPD_PULLUP: with weak pull-up resistors
      \arg        GPIO_PUPD_PULLDOWN:with weak pull-down resistors
    \param[in]  pin
      \arg        GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
    \param[out] none
    \retval     none
*/
void gpio_mode_set(uint32_t port, uint8_t mode, uint8_t pull_up_down,uint16_t pin)
{
    uint16_t i;
    uint32_t ctl, pupd;

    ctl = GPIO_CTL(port);
    pupd = GPIO_PUD(port);

    for (i = 0; i < 16; i++){
        if (!((1 << i) & pin)){
            continue;
        }
        /* clear the specified pin mode bits */
        ctl &= ~GPIO_MODE_MASK(i);
        ctl |= GPIO_MODE_SET(i, mode);
        /* clear the specified pin pupd bits */
        pupd &= ~GPIO_PUPD_MASK(i);
        pupd |= GPIO_PUPD_SET(i, pull_up_down);
    }

    GPIO_CTL(port) = ctl;
    GPIO_PUD(port) = pupd;
}
Example #4
0
// Set the direction, mode and pull-up for a set of GPIO pins on a given GPIO port
// input:
//   GPIOx - pointer to a GPIO peripheral handle
//   Mode - pin mode (one of GPIO_Mode_xx values)
//   PUPD - pull-up configuration (one of GPIO_PUPD_xx values)
//   Pins - set of pins (ORed combination of GPIO_Pin_X values)
void GPIO_set_mode(GPIO_TypeDef *GPIOx, GPIOMode_TypeDef Mode, GPIOPUPD_TypeDef PUPD, uint16_t Pins) {
	uint16_t pin = 0;
	uint32_t MODER = GPIOx->MODER;
	uint32_t PUPDR = GPIOx->PUPDR;

	// Set the configuration only for the pins mentioned in Pins parameter
	while (pin < 16) {
		if ((1 << pin) & Pins) {
			// Pin mode
			MODER &= ~GPIO_MODE_MSK(pin);
			MODER |=  GPIO_MODE_SET(pin, Mode);

			// Pull-up configuration
			PUPDR &= ~GPIO_PUPD_MSK(pin);
			PUPDR |=  GPIO_PUPD_SET(pin, PUPD);
		}
		pin++;
	}

	// Write new values of the mode and pull-up/pull-down control registers
	GPIOx->MODER = MODER;
	GPIOx->PUPDR = PUPDR;
}