/************************************************************************************************/
/**
* @file               common.c
* @author             MCU Ecosystem Development Team
* @brief              通用函数或本外设相关的配置实现函数。
*
*
**************************************************************************************************
* @attention
* Copyright (c) CEC Huada Electronic Design Co.,Ltd. All rights reserved.
*
**************************************************************************************************
*/

/*------------------------------------------includes--------------------------------------------*/
#include "common.h"

/*-------------------------------------------define---------------------------------------------*/
#define SLAVE_ADDRESS1           (0x5C)
#define SLAVE_ADDRESS2           (0x6C)

/*-------------------------------------------functions------------------------------------------*/
/**
* @brief  系统时钟配置
* @retval 无
*/
void system_clock_config(void)
{
    uint32_t ahbdiv_tmp;
    
    /* 设置Flash读访问等待时间 */
    std_flash_set_latency(FLASH_LATENCY_1CLK);

    /* 使能RCH */
    std_rcc_rch_enable();
    while(!std_rcc_get_rch_ready());
    
    /* 备份AHB分频参数 */
    ahbdiv_tmp = std_rcc_get_ahbdiv();    
    /* 配置AHB最大分频：128分频 */
    std_rcc_set_ahbdiv(RCC_HCLK_DIV128);  
    
    /* 切换RCH4到RCH48 */    
    std_rcc_set_rch_source(RCC_RCH_FREQ_48M);
    
    /* 恢复AHB分频参数 */
    std_rcc_set_ahbdiv(ahbdiv_tmp);      

    /* 设置系统时钟源为RCH */
    std_rcc_set_sysclk_source(RCC_SYSCLK_SRC_RCH);
    while(std_rcc_get_sysclk_source() != RCC_SYSCLK_SRC_STATUS_RCH);

    /* 设置AHB分频因子 */
    std_rcc_set_ahbdiv(RCC_HCLK_DIV1);
    /* 设置APB分频因子 */
    std_rcc_set_apbdiv(RCC_PCLK_DIV1);
    
    SystemCoreClockUpdate(); 
}

/**
* @brief  GPIO初始化配置
* @retval 无
*/
void gpio_init(void)
{
    std_gpio_init_t gpio_config = {0};
    
    /* 使能I2C1和LED1的GPIO时钟 */
    std_rcc_gpio_clk_enable(RCC_PERIPH_CLK_GPIOA | RCC_PERIPH_CLK_GPIOB);
    
    /* 初始化LED1 GPIO */
    gpio_config.pin = LED1_PIN;
    gpio_config.mode = GPIO_MODE_OUTPUT;
    gpio_config.pull = GPIO_NOPULL;
    gpio_config.output_type = GPIO_OUTPUT_PUSHPULL;
    std_gpio_init(LED1_GPIO_PORT, &gpio_config);
    
    /* 初始化LED1常灭 */
    LED1_OFF();
    
    /* 
    I2C GPIO初始化
    I2C_SCL: PA12
    I2C_SDA: PA11
    */
    gpio_config.pin = GPIO_PIN_12 | GPIO_PIN_11;
    gpio_config.mode = GPIO_MODE_ALTERNATE;
    gpio_config.pull = GPIO_PULLUP;
    gpio_config.output_type = GPIO_OUTPUT_OPENDRAIN;
    gpio_config.alternate = GPIO_AF6_I2C1;
    std_gpio_init(GPIOA, &gpio_config);
}

/**
* @brief  DMA初始化
* @retval 无
*/
void dma_init(void)
{
    std_dma_init_t dma_i2c_tx_config = {0};
    std_dma_init_t dma_i2c_rx_config = {0};
    
    /* 使能DMA时钟 */
    std_rcc_ahb_clk_enable(RCC_PERIPH_CLK_DMA);

    /* I2C发送DMA通道初始化 */
    dma_i2c_tx_config.dma_channel = DMA_CHANNEL_1;
    dma_i2c_tx_config.dma_req_id = DMA_REQUEST_I2C1_TX;
    dma_i2c_tx_config.transfer_type = DMA_BLOCK_TRANSFER;
    dma_i2c_tx_config.mode = DMA_MODE_NORMAL;
    dma_i2c_tx_config.data_size = DMA_DATA_SIZE_BYTE;
    dma_i2c_tx_config.src_addr_inc = DMA_SRC_INC_ENABLE;
    dma_i2c_tx_config.dst_addr_inc = DMA_DST_INC_DISABLE;
    dma_i2c_tx_config.priority = DMA_PRIORITY_HIGH;
    std_dma_init(&dma_i2c_tx_config);
    
    /* 使能传输完成中断和传输错误中断 */
    std_dma_interrupt_enable(DMA_CHANNEL_1, DMA_INTERRUPT_TF | DMA_INTERRUPT_TE);
    
    /* I2C接收DMA通道初始化 */
    dma_i2c_rx_config.dma_channel = DMA_CHANNEL_0;
    dma_i2c_rx_config.dma_req_id = DMA_REQUEST_I2C1_RX;
    dma_i2c_rx_config.transfer_type = DMA_BLOCK_TRANSFER;
    dma_i2c_rx_config.mode = DMA_MODE_NORMAL;
    dma_i2c_rx_config.data_size = DMA_DATA_SIZE_BYTE;
    dma_i2c_rx_config.src_addr_inc = DMA_SRC_INC_DISABLE;
    dma_i2c_rx_config.dst_addr_inc = DMA_DST_INC_ENABLE;
    dma_i2c_rx_config.priority = DMA_PRIORITY_VERY_HIGH;
    std_dma_init(&dma_i2c_rx_config);
    
    /* 使能传输完成中断和传输错误中断 */
    std_dma_interrupt_enable(DMA_CHANNEL_0, DMA_INTERRUPT_TF | DMA_INTERRUPT_TE);
}

/**
* @brief  I2C1从模式初始化
* @retval 无
*/
void i2c1_slave_init(void)
{
    std_i2c_init_t i2c1_init_config = {0};
    
    /* 使能I2C1外设时钟 */
    std_rcc_apb1_clk_enable(RCC_PERIPH_CLK_I2C1);

    /* 禁止I2C1 */
    std_i2c_disable(I2C1);
    
    /* 初始化I2C1 */
    i2c1_init_config.digital_filter = I2C_DIGITALFILTER_1CLK;
    i2c1_init_config.address1 = SLAVE_ADDRESS1;
    i2c1_init_config.control_mode = I2C_CONTROL_MULTI_BYTES;
    i2c1_init_config.acknowledge_type = I2C_DATA_ACK;
    std_i2c_init(I2C1, &i2c1_init_config);
    
    /* 配置I2C1地址2 */
    std_i2c_device_address2_disable(I2C1);
    std_i2c_device_address2_config(I2C1, SLAVE_ADDRESS2);
    std_i2c_device_address2_enable(I2C1);
    
    /* 使能I2C1 DMA发送请求 */
    std_i2c_dma_tx_req_enable(I2C1);
    
    /* 使能I2C1 DMA接收请求 */
    std_i2c_dma_rx_req_enable(I2C1);
}

/**
* @brief  NVIC初始化
* @retval 无
*/
void nvic_init(void)
{
    NVIC_SetPriority(DMA_Channel0_IRQn, NVIC_PRIO_0);
    NVIC_EnableIRQ(DMA_Channel0_IRQn);
    NVIC_SetPriority(DMA_Channel1_IRQn, NVIC_PRIO_0);
    NVIC_EnableIRQ(DMA_Channel1_IRQn);
}
