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

📄 drvi2c.c

📁 cortex-m0 LCD1602程序
💻 C
📖 第 1 页 / 共 3 页
字号:
/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include "DrvI2C.h"


#define I2C_STA              0x20
#define I2C_STO              0x10
#define I2C_SI               0x08
#define I2C_AA               0x04

static I2C_CALLBACK_T I2CHandler0 = {0};
static I2C_CALLBACK_T I2CHandler1 = {0};


/*---------------------------------------------------------------------------------------------------------*/
/* Function:     <I2C0_IRQHandler>                                                                         */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*               None                                                                                      */
/* Returns:                                                                                                */
/*               None                                                                                      */
/*                                                                                                         */
/* Description:                                                                                            */
/*               ISR to handle I2C0 interrupt event                                                        */
/*---------------------------------------------------------------------------------------------------------*/
void I2C0_IRQHandler(void)
{
    uint32_t status;

    status  = I2C0->I2CSTATUS;

    if (I2C0->I2CTOC.TIF)
    {
        I2C0->I2CTOC.TIF = 1;  /* Clear TIF */
        
        if (I2CHandler0.TimeoutCallBackFn)
        {
            I2CHandler0.TimeoutCallBackFn(status); 
        }
    }
    else
    {
        switch (status)
        {   
            case 0x38:  /* Arbitration loss */
            {
                if (I2CHandler0.ArbitLossCallBackFn)
                    I2CHandler0.ArbitLossCallBackFn(status); 
                break;
            }
            case 0x00:  /* Bus error */
            {
                if (I2CHandler0.BusErrCallBackFn)
                    I2CHandler0.BusErrCallBackFn(status); 
                break;
            }
            default:
            {
                if (I2CHandler0.I2CCallBackFn)
                    I2CHandler0.I2CCallBackFn(status);
            }       
        }   
    }
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:     <I2C1_IRQHandler>                                                                         */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*               None                                                                                      */
/* Returns:                                                                                                */
/*               None                                                                                      */
/*                                                                                                         */
/* Description:                                                                                            */
/*               ISR to handle I2C1 interrupt event                                                        */
/*---------------------------------------------------------------------------------------------------------*/
void I2C1_IRQHandler(void)
{
    uint32_t status;

    status  = I2C1->I2CSTATUS;

    if (I2C1->I2CTOC.TIF)
    {
        I2C1->I2CTOC.TIF = 1;  /* Clear TIF */
        
        if (I2CHandler1.TimeoutCallBackFn)
        {
            I2CHandler1.TimeoutCallBackFn(status); 
        }
    }
    else
    {
        switch (status)
        {   
            case 0x38:  /* Arbitration loss */
            {
                if (I2CHandler1.ArbitLossCallBackFn)
                    I2CHandler1.ArbitLossCallBackFn(status); 
                break;
            }
            case 0x00:  /* Bus error */
            {
                if (I2CHandler1.BusErrCallBackFn)
                    I2CHandler1.BusErrCallBackFn(status); 
                break;
            }
            default:
            {
                if (I2CHandler1.I2CCallBackFn)
                    I2CHandler1.I2CCallBackFn(status);
            }       
        }   
    }  
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvI2C_Ctrl                                                                                   */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               port       - [in]      I2C_PORT0 / I2C_PORT1                                              */
/*               start      - [in]      1:Enable / 0:Disable                                               */
/*               stop       - [in]      1:Enable / 0:Disable                                               */
/*               intFlag    - [in]      Wrtie '1' to clear this flag                                       */
/*               ack        - [in]      1:Enable / 0:Disable                                               */
/* Returns:                                                                                                */
/*               None                                                                                      */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Set I2C control bit                                                                       */       
/*---------------------------------------------------------------------------------------------------------*/
void DrvI2C_Ctrl(E_I2C_PORT port, uint8_t start, uint8_t stop, uint8_t intFlag, uint8_t ack)
{
    
    uint32_t Reg = 0;
        
    if (start)
        Reg |= I2C_STA;
    if (stop)
        Reg |= I2C_STO;
    if (intFlag)
        Reg |= I2C_SI;
    if (ack)
        Reg |= I2C_AA;

    if (port)
        *((__IO uint32_t *)&I2C1->I2CON) = (*((__IO uint32_t *)&I2C1->I2CON) & ~0x3C) | Reg;
    else
        *((__IO uint32_t *)&I2C0->I2CON) = (*((__IO uint32_t *)&I2C0->I2CON) & ~0x3C) | Reg;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvI2C_WriteData                                                                              */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               port       - [in]      I2C_PORT0 / I2C_PORT1                                              */
/*               u8data     - [in]      Byte Data                                                          */
/* Returns:                                                                                                */
/*               None                                                                                      */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Write Data into I2C Data register                                                         */       
/*---------------------------------------------------------------------------------------------------------*/
void DrvI2C_WriteData(E_I2C_PORT port, uint8_t u8data)
{
    if (port)
    {
        I2C1->I2CDAT = u8data;
    }   
    else
    {
        I2C0->I2CDAT = u8data;    
    }
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvI2C_ReadData                                                                               */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               port       - [in]      I2C_PORT0 / I2C_PORT1                                              */
/* Returns:                                                                                                */
/*               Data from I2C Data register                                                               */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Read Data from I2C Data register                                                          */       
/*---------------------------------------------------------------------------------------------------------*/
uint8_t DrvI2C_ReadData(E_I2C_PORT port)
{
    if (port)
    {                          
        return I2C1->I2CDAT;
    }   
    else
    {
        return I2C0->I2CDAT;  
    }
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvI2C_GetIntFlag                                                                             */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               port       - [in]      I2C_PORT0 / I2C_PORT1                                              */
/* Returns:                                                                                                */
/*               0 : Flag is not set                                                                       */
/*               1 : Flag is set                                                                           */
/* Description:                                                                                            */
/*               Get I2C interrupt flag                                                                    */       
/*---------------------------------------------------------------------------------------------------------*/
uint8_t DrvI2C_GetIntFlag(E_I2C_PORT port)
{
    if (port)
    {
        return I2C1->I2CON.SI;
    }   
    else
    {
        return I2C0->I2CON.SI;    
    }
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvI2C_ClearIntFlag                                                                           */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               port       - [in]      I2C_PORT0 / I2C_PORT1                                              */
/* Returns:                                                                                                */
/*               None                                                                                      */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Clear I2C interrupt flag                                                                  */       
/*---------------------------------------------------------------------------------------------------------*/
void DrvI2C_ClearIntFlag(E_I2C_PORT port)
{
    if (port)
    { 
        I2C1->I2CON.SI = 1;
    }   
    else
    {
        I2C0->I2CON.SI = 1;   
    }           
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvI2C_ClearTimeoutFlag                                                                       */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               port       - [in]      I2C_PORT0 / I2C_PORT1                                              */
/* Returns:                                                                                                */
/*               None                                                                                      */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Clear I2C time out flag                                                                   */       
/*---------------------------------------------------------------------------------------------------------*/
void DrvI2C_ClearTimeoutFlag(E_I2C_PORT port)
{
    if (port)
    { 
        I2C1->I2CTOC.TIF = 1;
    }   

⌨️ 快捷键说明

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