예제 #1
0
void GpioMcuInit( Gpio_t *obj, PinNames pin, PinModes mode, PinConfigs config, PinTypes type, uint32_t value )
{
	GPIO_InitTypeDef GPIO_InitStructure;

	if ( pin == NC ) 
		return;
	
	obj->portIndex = ( uint32_t ) pin >> 4;
	
	obj->pin = pin;
	obj->pinIndex = ( 0x01 << ( obj->pin & 0x0F ) );
	
	if ( obj->portIndex < 6 )
	{
		obj->port = ( GPIO_TypeDef * )( GPIOA_BASE + ( obj->portIndex << 10 ) );
		RCC_AHBPeriphClockCmd( ( 0x01 << obj->portIndex ), ENABLE );
	}
	else if ( obj->portIndex == 6 )
	{	/* GPIO base address not in alphabetical order after GPIOE (cf stm32l1xx.h, line 926 ) */
		/* Access to GPIOF and GPIOG not implemented */
		obj->port = ( GPIO_TypeDef * )( GPIOH_BASE );
		RCC_AHBPeriphClockCmd( ( 0x01 << ( obj->portIndex - 1 ) ), ENABLE );
	}

	// Sets initial output value
	if ( mode == PIN_OUTPUT )
		GpioMcuWrite( obj, value );

	GPIO_InitStructure.GPIO_Mode = ( GPIOMode_TypeDef )mode;
	GPIO_InitStructure.GPIO_OType = ( GPIOOType_TypeDef )config;
	GPIO_InitStructure.GPIO_PuPd = ( GPIOPuPd_TypeDef )type;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
	GPIO_InitStructure.GPIO_Pin = obj->pinIndex;
	GPIO_Init( obj->port, &GPIO_InitStructure );
}
예제 #2
0
파일: gpio.c 프로젝트: 2thetop/XRange
void GpioWrite( Gpio_t *obj, uint32_t value )
{
    if( ( uint32_t )( obj->pin >> 4 ) <= 6 ) 
    {
        GpioMcuWrite( obj, value );
    }
    else
    {
    }
}
예제 #3
0
void GpioMcuInit( Gpio_t *obj, PinNames pin, PinModes mode, PinConfigs config, PinTypes type, uint32_t value )
{
    GPIO_InitTypeDef GPIO_InitStructure;

    obj->pin = pin;

    if( pin == NC )
    {
        return;
    }

    obj->pinIndex = ( 0x01 << ( obj->pin & 0x0F ) );

    if( ( obj->pin & 0xF0 ) == 0x00 )
    {
        obj->port = GPIOA;
        __HAL_RCC_GPIOA_CLK_ENABLE( );
    }
    else if( ( obj->pin & 0xF0 ) == 0x10 )
    {
        obj->port = GPIOB;
        __HAL_RCC_GPIOB_CLK_ENABLE( );
    }
    else if( ( obj->pin & 0xF0 ) == 0x20 )
    {
        obj->port = GPIOC;
        __HAL_RCC_GPIOC_CLK_ENABLE( );
    }
    else if( ( obj->pin & 0xF0 ) == 0x30 )
    {
        obj->port = GPIOD;
        __HAL_RCC_GPIOD_CLK_ENABLE( );
    }
    else
    {
        assert_param( FAIL );
    }

    GPIO_InitStructure.Pin =  obj->pinIndex ;
    GPIO_InitStructure.Pull = obj->pull = type;
    GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;

    if( mode == PIN_INPUT )
    {
        GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
    }
    else if( mode == PIN_ANALOGIC )
    {
        GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
    }
    else if( mode == PIN_ALTERNATE_FCT )
    {
        if( config == PIN_OPEN_DRAIN )
        {
            GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
        }
        else
        {
            GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
        }
        GPIO_InitStructure.Alternate = value;
    }
    else // mode output
    {
        if( config == PIN_OPEN_DRAIN )
        {
            GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
        }
        else
        {
            GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
        }
    }

    HAL_GPIO_Init( obj->port, &GPIO_InitStructure );

    // Sets initial output value
    if( mode == PIN_OUTPUT )
    {
        GpioMcuWrite( obj, value );
    }
}