⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drvi2c.c

📁 cortex-m0 LCD1602程序
💻 C
📖 第 1 页 / 共 3 页
字号:
    else
    {
        I2C0->I2CTOC.TIF = 1;  
    }           
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvI2C_GetStatus                                                                              */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               port       - [in]      I2C_PORT0 / I2C_PORT1                                              */
/* Returns:                                                                                                */
/*               status                                                                                    */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Get I2C status. There are 26 status codes. Please refer to data sheet in detail.          */       
/*---------------------------------------------------------------------------------------------------------*/
uint32_t DrvI2C_GetStatus(E_I2C_PORT port)
{
    if (port)
    {
        return I2C1->I2CSTATUS;
    }
    else
    {
        return I2C0->I2CSTATUS;
    }   
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvI2C_Open                                                                                   */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               port          - [in]       I2C_PORT0 / I2C_PORT1                                          */
/*               u32BusClock   - [in]       I2C bus clock frequency (Hz)                                   */
/* Returns:                                                                                                */
/*               0 : Success                                                                               */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Enable I2C function and set clock divider. I2C clock = I2C source clock / (4x(divider+1)) */       
/*               The maximum of I2C clock is 1MHz.                                                         */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvI2C_Open(E_I2C_PORT port, uint32_t u32BusClock)
{
    uint32_t divider;     
 
    divider = (uint32_t) (((SystemCoreClock*10)/(u32BusClock * 4) + 5) / 10 - 1);   /* Compute proper divider for I2C clock */
    
    if (port)
    {
        SYSCLK->APBCLK.I2C1_EN = 1;
        SYS->IPRSTC2.I2C1_RST = 1;
        SYS->IPRSTC2.I2C1_RST = 0;
        I2C1->I2CON.ENS1 = 1;
        I2C1->I2CLK = divider;
    }
    else
    {
        SYSCLK->APBCLK.I2C0_EN = 1;
        SYS->IPRSTC2.I2C0_RST = 1;
        SYS->IPRSTC2.I2C0_RST = 0;  
        I2C0->I2CON.ENS1 = 1;
        I2C0->I2CLK = divider;
    }

    return 0;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvI2C_Close                                                                                  */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               port       - [in]      I2C_PORT0 / I2C_PORT1                                              */
/* Returns:                                                                                                */
/*               0 : Success                                                                               */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Disable I2C function and clock source                                                     */       
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvI2C_Close(E_I2C_PORT port)
{
    if (port)
    {
        I2C1->I2CON.ENS1 = 0;
        SYS->IPRSTC2.I2C1_RST = 1;
        SYS->IPRSTC2.I2C1_RST = 0;
        SYSCLK->APBCLK.I2C1_EN = 0;
    }
    else
    {
        I2C0->I2CON.ENS1 = 0;
        SYS->IPRSTC2.I2C0_RST = 1;
        SYS->IPRSTC2.I2C0_RST = 0;
        SYSCLK->APBCLK.I2C0_EN = 0;
    }
    
    return 0;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvI2C_SetClockFreq                                                                           */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               port         - [in]        I2C_PORT0 / I2C_PORT1                                          */
/*               u32BusClock  - [in]        I2C Bus Clock Frequency (Hz)                                   */
/* Returns:                                                                                                */
/*               0 : Success                                                                               */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Set clock divider. I2C clock = I2C source clock / (4x(divider+1))                         */       
/*               The maximum of I2C clock is 1MHz.                                                         */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvI2C_SetClockFreq(E_I2C_PORT port, uint32_t u32BusClock)
{
    uint32_t divider;     
 
    divider = (uint32_t) (((SystemCoreClock*10)/(u32BusClock * 4) + 5) / 10 - 1);
    if (port)
    {
        I2C1->I2CLK = divider;
    }
    else
    {
        I2C0->I2CLK = divider;
    }
    return 0;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvI2C_GetClockFreq                                                                           */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               port          - [in]       I2C_PORT0 / I2C_PORT1                                          */
/* Returns:                                                                                                */
/*               I2C Bus Clock Frequency                                                                   */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Get I2C Bus Clock                                                                         */       
/*---------------------------------------------------------------------------------------------------------*/
uint32_t DrvI2C_GetClockFreq(E_I2C_PORT port)
{
    uint32_t divider;     
 
    divider = ( (port)? I2C1->I2CLK:I2C0->I2CLK );
    return ( SystemCoreClock / ((divider+1)<<2) );
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvI2C_EnableInt                                                                              */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               port          - [in]       I2C_PORT0 / I2C_PORT1                                          */
/* Returns:                                                                                                */
/*               0 : Success                                                                               */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Enable I2C interrupt and NVIC corresponding to I2C                                        */       
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvI2C_EnableInt(E_I2C_PORT port)
{
    if (port)
    {
        I2C1->I2CON.EI = 1;
        NVIC_SetPriority(I2C1_IRQn, (1<<__NVIC_PRIO_BITS) - 2);
        NVIC_EnableIRQ(I2C1_IRQn);
    }
    else
    {
        I2C0->I2CON.EI = 1;
        NVIC_SetPriority(I2C0_IRQn, (1<<__NVIC_PRIO_BITS) - 2);
        NVIC_EnableIRQ(I2C0_IRQn);
    }

    return 0;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvI2C_DisableInt                                                                             */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               port          - [in]       I2C_PORT0 / I2C_PORT1                                          */
/* Returns:                                                                                                */
/*               0 : Success                                                                               */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Disable I2C interrupt and NVIC corresponding to I2C                                       */       
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvI2C_DisableInt(E_I2C_PORT port)
{
    if (port)
    {
        I2C1->I2CON.EI = 0;
        NVIC_DisableIRQ(I2C1_IRQn);
    }
    else
    {
        I2C0->I2CON.EI = 0;
        NVIC_DisableIRQ(I2C0_IRQn);
    }

    return 0;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvI2C_SetTimeoutCounter                                                                          */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               port          - [in]       I2C_PORT0 / I2C_PORT1                                          */
/*               i32enable     - [in]       1:Enable / 0:Disable                                           */
/*               u8div4        - [in]       1:Enable / 0:Disable                                           */
/* Returns:                                                                                                */
/*               0 : Success                                                                               */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Enable/Disable I2C 14-bit timeout counter and set div4 bit of timeout counter             */       
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvI2C_SetTimeoutCounter(E_I2C_PORT port, int32_t i32enable, uint8_t u8div4)
{
    if (port)
    {
        I2C1->I2CTOC.DIV4 = u8div4;
        I2C1->I2CTOC.ENTI = i32enable;
    }
    else
    {
        I2C0->I2CTOC.DIV4 = u8div4;
        I2C0->I2CTOC.ENTI = i32enable;
    }
    return 0;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvI2C_SetAddress                                                                             */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               port          - [in]       I2C_PORT0 / I2C_PORT1                                          */
/*               slaveNo       - [in]       There are 4 slave addrss. The slaveNo is 0 ~ 3.                */
/*               slave_addr    - [in]       7-bit physical slave address                                   */
/*               GC_Flag       - [in]       1:Enable / 0:Disable                                           */
/* Returns:                                                                                                */
/*               0 : Success                                                                               */
/*              <0 : Failed                                                                                */
/* Description:                                                                                            */
/*               Set 4 7-bit slave addresses and enable General Call function                              */       
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvI2C_SetAddress(E_I2C_PORT port, uint8_t slaveNo, uint8_t slave_addr, uint8_t GC_Flag)
{
    if (port)
    {
        switch (slaveNo)
        {
            case 0:
                I2C1->I2CADDR0.I2CADDR = slave_addr;
                I2C1->I2CADDR0.GC      = GC_Flag;
                break;
            case 1:
                I2C1->I2CADDR1.I2CADDR = slave_addr;
                I2C1->I2CADDR1.GC      = GC_Flag;
                break;
            case 2:
                I2C1->I2CADDR2.I2CADDR = slave_addr;
                I2C1->I2CADDR2.GC      = GC_Flag;
                break;
            case 3:
                I2C1->I2CADDR3.I2CADDR = slave_addr;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -