/**
 * @brief  GPIO初始化配置
 * @code
 *    //初始化配置PORTB端口的10引脚为推挽输出引脚
 *    GPIO_InitTypeDef GPIO_InitStruct1;      //申请一个结构变量
 *    GPIO_InitStruct1.instance = HW_GPIOB;   //选择PORTB端口
 *    GPIO_InitStruct1.mode = kGPIO_Mode_OPP; //推挽输出
 *    GPIO_InitStruct1.pinx = 10;             //选择10引脚
 *    //调用初始化GPIO函数 
 *    GPIO_Init(&GPIO_InitStruct1);
 * @endcode
 * @param  GPIO_InitStruct: GPIO初始化结构体,包含了引脚状态参数  
           GPIO_InitStruct.instance   :端口号 HW_GPIOA ~ HW_GPIOE
 * @retval None
 */
void GPIO_Init(GPIO_InitTypeDef * GPIO_InitStruct)
{
    /* param check */
    assert_param(IS_GPIO_ALL_INSTANCE(GPIO_InitStruct->instance));
    assert_param(IS_PORT_ALL_INSTANCE(GPIO_InitStruct->instance));
    assert_param(IS_GPIO_ALL_PIN(GPIO_InitStruct->pinx));
    /* config state */
    switch(GPIO_InitStruct->mode)
    {
        case kGPIO_Mode_IFT:
            PORT_PinPullConfig(GPIO_InitStruct->instance, GPIO_InitStruct->pinx, kPullDisabled);
            GPIO_PinConfig(GPIO_InitStruct->instance, GPIO_InitStruct->pinx, kInput);
            break;
        case kGPIO_Mode_IPD:
            PORT_PinPullConfig(GPIO_InitStruct->instance, GPIO_InitStruct->pinx, kPullDown);
            GPIO_PinConfig(GPIO_InitStruct->instance, GPIO_InitStruct->pinx, kInput);
            break;
        case kGPIO_Mode_IPU:
            PORT_PinPullConfig(GPIO_InitStruct->instance, GPIO_InitStruct->pinx, kPullUp);
            GPIO_PinConfig(GPIO_InitStruct->instance, GPIO_InitStruct->pinx, kInput);
            break;
        case kGPIO_Mode_OPP:
            PORT_PinPullConfig(GPIO_InitStruct->instance, GPIO_InitStruct->pinx, kPullDisabled);
            GPIO_PinConfig(GPIO_InitStruct->instance, GPIO_InitStruct->pinx, kOutput);
            break;
        default:
            break;					
    }
    /* config pinMux */
    PORT_PinMuxConfig(GPIO_InitStruct->instance, GPIO_InitStruct->pinx, kPinAlt1);
}
Пример #2
0
/**
 * @brief  I2C快速初始化函数
 * \code 
 *   //PE00\PE01初始化为i2c功能,波特率为
 *   uint32_t instance;
 *   instance = I2C_QuickInit(I2C1_SCL_PE01_SDA_PE00,1000);
 * \endcode
 * @param[in] MAP  I2C引脚配置缩略图,详见i2c.h
 * \param[in] baudrate 波特率(函数中暂未配置,取为默认)
 * @retval i2c模块号
 */
uint8_t I2C_QuickInit(uint32_t MAP, uint32_t baudrate)
{
    uint8_t i;
    map_t * pq = (map_t*)&(MAP);
    
    /* open drain and pull up */
    for(i = 0; i < pq->pin_cnt; i++)
    {
        GPIO_QuickInit(pq->io, pq->pin_start + i, kGPIO_Mode_OPP);
        GPIO_QuickInit(pq->io, pq->pin_start + i, kGPIO_Mode_OPP);
        GPIO_WriteBit(pq->io, pq->pin_start + i, 1);
        PORT_PinPullConfig(pq->io, pq->pin_start + i, kPullUp);
    }

    /* i2c_gpio struct setup */
    i2c.instace = pq->io;
    
    switch(MAP)
    {
        case I2C1_SCL_PE01_SDA_PE00:
            i2c.scl_pin = 1;i2c.sda_pin = 0;
            break;
        case I2C0_SCL_PE19_SDA_PE18:
            i2c.scl_pin = 19;i2c.sda_pin = 18;
            break;
        case I2C0_SCL_PF22_SDA_PF23:
            i2c.scl_pin = 22;i2c.sda_pin = 23;
            break;
        case I2C0_SCL_PB00_SDA_PB01:
            i2c.scl_pin = 0;i2c.sda_pin = 1;
            break;
        case I2C0_SCL_PB02_SDA_PB03:
            i2c.scl_pin = 2;i2c.sda_pin = 3;
            break;
        case I2C1_SCL_PC10_SDA_PC11:
            i2c.scl_pin = 10;i2c.sda_pin = 11;
            break;
        case I2C0_SCL_PD08_SDA_PD09:
            i2c.scl_pin = 8;i2c.sda_pin = 9;
            break;
        case I2C0_SCL_PE24_SDA_PE25:
            i2c.scl_pin = 24;i2c.sda_pin = 25;
            break;
        case I2C1_SCL_PC01_SDA_PC02:
            i2c.scl_pin = 1;i2c.sda_pin = 2;
            break;
        case I2Cx_SCL_PC14_SDA_PC15:
            i2c.scl_pin = 14;i2c.sda_pin = 15;
            break;
        default:
            break;
    }
    return pq->ip;
}
Пример #3
0
 /**
 * @brief  快速初始化IIC模块
 * @code
 *      // 初始化I2C模块: 使用I2C1模块的SCL:PC10 SDA:PC11,通信速度:47000Hz
 *      I2C_QuickInit(I2C1_SCL_PC10_SDA_PC11, 47000);
 * @endcode
 * @param  MAP: I2C快速初始化选择项,详见i2c.h文件
 *         @arg I2C1_SCL_PE01_SDA_PE00
 *         @arg         ...
 *         @arg I2C1_SCL_PC10_SDA_PC11
 * @param  baudrate :通信速度 建议为: 48000Hz 76000Hz 96000Hz 376000Hz
 * @retval i2c模块号
 */
uint8_t I2C_QuickInit(uint32_t MAP, uint32_t baudrate)
{
    uint8_t i;
    I2C_InitTypeDef I2C_InitStruct1;
    QuickInit_Type * pq = (QuickInit_Type*)&(MAP);
    I2C_InitStruct1.baudrate = baudrate;
    I2C_InitStruct1.instance = pq->ip_instance;
    /* init pinmux and  open drain and pull up */
    for(i = 0; i < pq->io_offset; i++)
    {
        PORT_PinMuxConfig(pq->io_instance, pq->io_base + i, (PORT_PinMux_Type)pq->mux);
        PORT_PinPullConfig(pq->io_instance, pq->io_base + i, kPullUp); 
        PORT_PinOpenDrainConfig(pq->io_instance, pq->io_base + i, ENABLE);
    }
    /* init moudle */
    I2C_Init(&I2C_InitStruct1);
    return pq->ip_instance;
}