/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2024. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
/******************************************************************************
 * @version: V3.1.0  AC7840x  Demo
 ******************************************************************************/

/*!
 * @file i2c_master_dma_sample.c
 *
 * @brief i2c_master_dma_sample源文件
 */
/* ===========================================  Includes  =========================================== */
#include "ac7840x.h"
#include "gpio_drv.h"
#include "gpio_hw.h"
#include "i2c_drv.h"
#include "i2c_hw.h"
#include "dma_drv.h"
#include "dma_hw.h"
#include "i2c_master_dma_sample.h"
#include <string.h>

/* ============================================  Define  ============================================ */
#define I2C0_INSTANCE        0               /* i2c 序列号 */

#define I2C_RX_SIZE          256             /* i2c 接收数据大小 */
#define I2C_TX_SIZE          256             /* i2c 发送数据大小 */
  
#define I2C_SLAVE_ADDRESS    0x50            /* i2c从机地址 */

#define I2C0_VIRTUAL_CHANNEL 0               /* i2c0 通道号 */

/* ===========================================  Typedef  ============================================ */


/* ==========================================  Variables  =========================================== */
#if    defined ( __ICCARM__ )        /* IAR Compiler */
uint8_t g_rxBuff[I2C_RX_SIZE] @0x20000000 = {0}; 
uint8_t g_txBuff[I2C_TX_SIZE] @0x20000000 = {0};
#elif  defined ( __CC_ARM )        /* Arm Compiler */
uint8_t g_rxBuff[I2C_RX_SIZE] __attribute__((at(0x20000000))) = {0}; 
uint8_t g_txBuff[I2C_TX_SIZE] __attribute__((at(0x20000200))) = {0};
#endif

i2c_master_state_t   g_i2c_master_state;      /* i2c0 主机状态结构体 */

dma_chn_state_t dma_i2c0_txchn_state = {
    .virtChn  = I2C0_VIRTUAL_CHANNEL,         /* dma 通道号 */
    .status   = DMA_CHN_NORMAL,               /* dma 通道正常状态 */
    .callback = NULL,                         /* 无回调函数 */
};
dma_chn_state_t * const g_dma_chn_state[1] = {&dma_i2c0_txchn_state};  /* dma 通道状态 */

dma_state_t  g_dma_state = {                  /* i2c0 dma 状态 */ 
    .virtChnState[0] = &dma_i2c0_txchn_state,
};             

dma_channel_config_t dma_i2c0_txchn_config = {
    .channelPriority = DMA_CHN_PRIORITY_HIGH,  /* dma 通道优先级 */
    .virtChnConfig   = I2C0_VIRTUAL_CHANNEL,   /* dma 通道号 */
    .source          = DMA_REQ_I2C0_TX,        /* i2c0 发送的dma通道请求  */
    .callback        = NULL,                   /* 无回调函数 */
    .enableTrigger   = false,                  /* dma 周期触发禁能 */
};
const dma_channel_config_t * const g_dma_chn_config_Arr[1] = {&dma_i2c0_txchn_config};   /* dma 通道配置结构体 */

/* ====================================  Functions declaration  ===================================== */


/* ======================================  Functions define  ======================================== */

/*!
* @brief I2C0 初始化
*
* @param[in] none
* @return none
*/
void I2C0_Init(void)
{
    /* I2C 配置结构体 */
    i2c_master_user_config_t i2c_master_config;
    
    /* 清I2C 配置结构体 */
    memset(&i2c_master_config, 0, sizeof(i2c_master_user_config_t));
    memset(&g_i2c_master_state, 0, sizeof(i2c_master_state_t));
    
    /* PA2->SDA,?PPA3->SCL */
    GPIO_DRV_SetMuxModeSel(PORTA, (gpio_channel_type_t)2U, PORT_MUX_ALT3);
    GPIO_DRV_SetMuxModeSel(PORTA, (gpio_channel_type_t)3U, PORT_MUX_ALT3);
    
    i2c_master_config.is10bitAddr    = false;                /* 7bit地址模式 */
    i2c_master_config.slaveAddress   = I2C_SLAVE_ADDRESS;    /* i2c 从机地址 */
    i2c_master_config.dmaChannel     = I2C0_VIRTUAL_CHANNEL; /* dma 通道 */
    i2c_master_config.transferType   = I2C_USING_DMA;        /* 使用i2c DMA模式 */
    i2c_master_config.syncEn         = false;                /* i2c 同步禁能 */
    i2c_master_config.arbitration    = true;                 /* i2c 仲裁禁能 */
    i2c_master_config.baudRate       = 100000;               /* i2c 波特率 */
    i2c_master_config.callback       = NULL;                 /* 无回调函数 */
   
    I2C_DRV_MasterInit(I2C0_INSTANCE,&i2c_master_config,&g_i2c_master_state);  /* I2C0 主机初始化 */
    
    I2C0_DMA_Init();                                                           /* I2C0 DMA初始化 */
}

/*!
* @brief I2C0 DMA初始化
*
* @param[in] none
* @return none
*/
void I2C0_DMA_Init(void)
{
    /* dma driver init */
    DMA_DRV_Init(&g_dma_state,g_dma_chn_state,g_dma_chn_config_Arr,1);  
}

/*!
* @brief I2C0 DMA发送函数
*
* @param[in] none
* @return none
*/
void I2C0_DMATx_Test(void)
{
    uint16_t i = 0;
    
    for(i = 0; i < I2C_TX_SIZE; i++)
    {
        g_txBuff[i] = i;
    }
     
    I2C_DRV_MasterSendData(I2C0_INSTANCE,g_txBuff,I2C_TX_SIZE,ENABLE);  /* I2C 主机发送函数 */
}

