/**
 * @file        g32f031_i2c_cfg.c
 *
 * @brief       This file provides all configuration support for I2C
 *
 * @version     V1.0.0
 *
 * @date        2026-01-15
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes ***************************************************************/
#include "g32f031_device_cfg.h"

/* Private includes *******************************************************/

/* Private macro **********************************************************/

/* Private typedef ********************************************************/

/* Private variables ******************************************************/

/* Private function prototypes ********************************************/

/* External variables *****************************************************/

/* External functions *****************************************************/

/**
 * @brief   I2C configuration
 *
 * @param   None
 *
 * @retval  None
 */
void I2C_Config(void)
{
    DDL_I2C_InitTypeDef I2C_InitStruct = {0};
    DDL_GPIO_InitTypeDef GPIO_InitStruct = {0};

    DDL_RCC_Unlock();
    DDL_AHB_GRP1_EnableClock(DDL_AHB_GRP1_PERIPH_GPIOA);
    DDL_APB_GRP1_EnableClock(DDL_APB_GRP1_PERIPH_I2C);
    DDL_RCC_Lock();

    DDL_GPIO_LockKey(GPIOA, DDL_GPIO_LOCK_DISABLE);
    /* Configure the I2C SCL pin */
    GPIO_InitStruct.Pin = DDL_GPIO_PIN_7;
    GPIO_InitStruct.Mode = DDL_GPIO_MODE_ALTERNATE;
    GPIO_InitStruct.Drive = DDL_GPIO_DRIVE_HIGH;
    GPIO_InitStruct.OutputType = DDL_GPIO_OUTPUT_OPENDRAIN;
    GPIO_InitStruct.InputEnable = DDL_GPIO_INPUT_DISABLE;
    GPIO_InitStruct.Pull = DDL_GPIO_PULL_UP;
    GPIO_InitStruct.Alternate = DDL_GPIO_AF_2;
    DDL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* Configure the I2C SDA pin */
    GPIO_InitStruct.Pin = DDL_GPIO_PIN_8;
    GPIO_InitStruct.Mode = DDL_GPIO_MODE_ALTERNATE;
    GPIO_InitStruct.Drive = DDL_GPIO_DRIVE_HIGH;
    GPIO_InitStruct.OutputType = DDL_GPIO_OUTPUT_OPENDRAIN;
    GPIO_InitStruct.InputEnable = DDL_GPIO_INPUT_ENABLE;
    GPIO_InitStruct.Pull = DDL_GPIO_PULL_UP;
    GPIO_InitStruct.Alternate = DDL_GPIO_AF_2;
    DDL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    DDL_GPIO_LockKey(GPIOA, DDL_GPIO_LOCK_ENABLE);

    I2C_InitStruct.PeripheralMode = DDL_I2C_MODE_I2C;
    I2C_InitStruct.ClockSpeed = 100000;
    I2C_InitStruct.DutyCycle = DDL_I2C_DUTYCYCLE_2;
    I2C_InitStruct.AnalogFilter = DDL_I2C_ANALOGFILTER_DISABLE;
    I2C_InitStruct.DigitalFilter = 0;
    I2C_InitStruct.OwnAddress1 = 0xA0;
    I2C_InitStruct.TypeAcknowledge = DDL_I2C_ACK;
    I2C_InitStruct.OwnAddrSize = DDL_I2C_OWNADDRESS1_7BIT;
    DDL_I2C_Init(I2C, &I2C_InitStruct);

    DDL_I2C_Enable(I2C);
}
