/**
  ******************************************************************************
  * @file     I2C_CFG.c
  * @author   SINOMCU-AE
  * @brief    I2C1 config
  *    
  *          This file provides functions to usart1 initialization:
  *              PA9    --> I2C1_SCL
  *              PA10   <-> I2C1_SDA
  *              Standard mode,100K
  *         
  ******************************************************************************
  * @attention 
  *                           DISCLAIMER
  * The present firmware which is for guidance only aims at providing customers
  * with coding information regarding their products in order for them to save
  * time. As a result, SinoMCU shall not be held liable for any direct,indirect
  * or consequential damages with respect to any claims arising from the content 
  * of such firmware and/or the use made by customers of the coding information 
  * contained herein in connection with their products.
  *
  * <h2><center>&copy; Copyright (c) 2021 sinomcu All rights reserved.</center></h2>
  *
  ******************************************************************************
  */

#include "I2C_CFG.h"

/**
  * @brief I2C1 Initialization Function
  * @param None
  * @retval None
  */
void I2C1_Pin_Init(void)
{
  MS32_GPIO_InitTypeDef GPIO_InitStruct = {0};
  
  // I2C1 GPIO Configuration
  // PA9    --> I2C1_SCL
  // PA10   <-> I2C1_SDA
  MS32_AHB1_GRP1_EnableClock(MS32_AHB1_GRP1_PERIPH_GPIOA);

  MS32_GPIO_StructInit(&GPIO_InitStruct);    
  GPIO_InitStruct.Pin = MS32_GPIO_PIN_9;
  GPIO_InitStruct.Mode = MS32_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = MS32_GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.OutputType = MS32_GPIO_OUTPUT_OPENDRAIN;
  GPIO_InitStruct.Pull = MS32_GPIO_PULL_UP;
  GPIO_InitStruct.Alternate = MS32_GPIO_AF_4;
  MS32_GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = MS32_GPIO_PIN_10;
  GPIO_InitStruct.Mode = MS32_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = MS32_GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.OutputType = MS32_GPIO_OUTPUT_OPENDRAIN;
  GPIO_InitStruct.Pull = MS32_GPIO_PULL_UP;
  GPIO_InitStruct.Alternate = MS32_GPIO_AF_4;
  MS32_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

/**
  * @brief I2C1 Initialization Function
  * @param None
  * @retval None
  */
void I2C1_I2C_Init(void)
{
  MS32_I2C_InitTypeDef I2C_InitStruct = {0};

  I2C1_Pin_Init();

  MS32_APB1_GRP1_EnableClock(MS32_APB1_GRP1_PERIPH_I2C1);

  // I2C Initialization:master,7bit,Standard mode 100K
  MS32_I2C_StructInit(&I2C_InitStruct); 
  MS32_I2C_DisableOwnAddress2(I2C1);
  MS32_I2C_DisableGeneralCall(I2C1);
  MS32_I2C_EnableClockStretching(I2C1);
  I2C_InitStruct.PeripheralMode = MS32_I2C_MODE_I2C;
  I2C_InitStruct.Timing = 0x2000090E;
  I2C_InitStruct.AnalogFilter = MS32_I2C_ANALOGFILTER_ENABLE;
  I2C_InitStruct.DigitalFilter = 0;
  I2C_InitStruct.OwnAddress1 = 0;
  I2C_InitStruct.TypeAcknowledge = MS32_I2C_ACK;
  I2C_InitStruct.OwnAddrSize = MS32_I2C_OWNADDRESS1_7BIT;
  MS32_I2C_Init(I2C1, &I2C_InitStruct);
  MS32_I2C_EnableAutoEndMode(I2C1);
  MS32_I2C_SetOwnAddress2(I2C1, 0, MS32_I2C_OWNADDRESS2_NOMASK);
}

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