/*********************************************************
*Copyright (C), 2017, Shanghai Eastsoft Microelectronics Co., Ltd.
 * @文件名:  main.h
 * @作  者:  AE Team
 * @版  本:  V1.01
 * @日  期:  2022/07/25
 * @描  述:  IIC主机演示主程序头文件
 * @note
 *          Change Logs:
 *          Date            Author          Notes
 *          25 July 2022    AE Team         change license to Apache-2.0
 *
 * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **********************************************************************************
 */

#include "irqhandler.h"


/***************************************************************
***************************************************************/
uint8_t rxcnt;
uint8_t txcnt;


/***************************************************************
***************************************************************/
void NMI_IRQHandler(void)
{

}

void HardFault_IRQHandler(void)
{
    while (1)
    {
    }
}

void SVC_IRQHandler(void)
{

}

void PendSV_IRQHandler(void)
{

}

void SysTick_IRQHandler(void)
{

}


/***************************************************************
 函数名: void I2C0_IRQHandler(void)
 描  述：IIC中断函数
         用户需要高速传输时，尽量使用操作寄存器的方法
 输入值：无
 输出值：无
 返回值：无
***************************************************************/
void I2C0_IRQHandler(void)
{
    /* 收到起始位+地址匹配+发送应答位 */
    if ((IIC_GetITStatus(I2C0, IIC_IT_SR) == SET) && (IIC_GetFlagStatus(I2C0, IIC_IF_SR) == SET))
    {
        IIC_ClearITPendingBit(I2C0, IIC_CLR_SR);

        if (IIC_GetRWMode(I2C0) == IIC_Mode_Read)
        {
            txcnt = 0x0U;

            IIC_ClearITPendingBit(I2C0, IIC_CLR_NA);
            IIC_ClearITPendingBit(I2C0, IIC_CLR_TIDLE);
            IIC_ITConfig(I2C0, IIC_IT_RB, Disable);
            IIC_ITConfig(I2C0, IIC_IT_TIDLE, Enable);
        }
        else if (IIC_GetRWMode(I2C0) == IIC_Mode_Write)
        {
            rxcnt = 0x0U;

            IIC_ClearITPendingBit(I2C0, IIC_CLR_NA);
            IIC_ITConfig(I2C0, IIC_IT_RB, Enable);
            IIC_ITConfig(I2C0, IIC_IT_TIDLE, Disable);
        }
    }

    /* 接收中断 */
    if ((IIC_GetITStatus(I2C0, IIC_IT_RB) == SET) && (IIC_GetFlagStatus(I2C0, IIC_IF_RB) == SET))
    {
        g_buf[rxcnt++] = IIC_RecByte(I2C0);
    }

    /* 发送中断 */
    if ((IIC_GetITStatus(I2C0, IIC_IT_TIDLE) == SET) && (IIC_GetFlagStatus(I2C0, IIC_IF_TIDLE) == SET))
    {
        IIC_ClearITPendingBit(I2C0, IIC_CLR_TIDLE);

        if (IIC_GetFlagStatus(I2C0, IIC_IF_NA) != SET)
        {
            IIC_SendByte(I2C0, g_buf[txcnt++]);
        }
    }

    /* 收到起始位+地址匹配+发送应答位 */
    if ((IIC_GetITStatus(I2C0, IIC_IT_SP) == SET) && (IIC_GetFlagStatus(I2C0, IIC_IF_SP) == SET)) /* 收到结束位 */
    {
        rxcnt = 0x0U;
        txcnt = 0x0U;

        IIC_ClearITPendingBit(I2C0, IIC_CLR_SP);
        IIC_ClearITPendingBit(I2C0, IIC_CLR_NA);

        IIC_ITConfig(I2C0, IIC_IT_RB, Disable);
        IIC_ITConfig(I2C0, IIC_IT_TIDLE, Disable);
    }
}


/*************************END OF FILE**********************/
