/************************************************************************************************/
/**
* @file               common.h
* @author             MCU Ecosystem Development Team
* @brief              COMMON头文件。
*
*
**************************************************************************************************
* @attention
* Copyright (c) CEC Huada Electronic Design Co.,Ltd. All rights reserved.
*
**************************************************************************************************
*/

/* 避免头文件重复引用 */
#ifndef COMMON_H
#define COMMON_H

#ifdef __cplusplus
extern "C" {
#endif

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

/*-------------------------------------------define---------------------------------------------*/
/* I2C_SIM 引脚定义 */
#define I2C_PIN_PORT            GPIOB
#define I2C_PIN_PORT_CLK        RCC_PERIPH_CLK_GPIOB
#define I2C_SCL_PIN             GPIO_PIN_3
#define I2C_SDA_PIN             GPIO_PIN_4

/* I2C_SIM 引脚动作 */
#define SDA_MODE_INPUT()        I2C_PIN_PORT->MODE &= ~(0x03<<(8))
#define SDA_MODE_OUTPUT()       I2C_PIN_PORT->MODE |= (0x01<<(8))
#define SDA_READ()              ((I2C_PIN_PORT->IDR & I2C_SDA_PIN) ? 1 : 0) 
#define SDA_H()                 I2C_PIN_PORT->BSR = I2C_SDA_PIN
#define SDA_L()                 I2C_PIN_PORT->BR  = I2C_SDA_PIN
#define SCL_H()                 I2C_PIN_PORT->BSR = I2C_SCL_PIN
#define SCL_L()                 I2C_PIN_PORT->BR  = I2C_SCL_PIN

/* I2C_SIM_TIM 定义 */
#define I2C_SIM_TIM             TIM1
#define I2C_SIM_TIM_CLK         RCC_PERIPH_CLK_TIM1
#define I2C_SIM_TIM_IRQn        TIM1_BRK_UP_TRG_COM_IRQn
#define I2C_TIM_IRQHandler      TIM1_BRK_UP_TRG_COM_IRQHandler
#define I2C_SIM_TIM_ENABLE()    std_tim_enable(I2C_SIM_TIM)
#define I2C_SIM_TIM_DISABLE()   std_tim_disable(I2C_SIM_TIM)
#define I2C_SIM_TIM_CLR_FLAG()  std_tim_clear_flag(I2C_SIM_TIM, TIM_FLAG_UPDATE)

/* 每字节SCL翻转数 */
#define BYTE_LAST_BIT_SCL       (16U)


/*-------------------------------------------functions------------------------------------------*/
void system_clock_config(void);
void gpio_init(void);
void error_process(void);
void i2c_sim_tim_init(void);

#ifdef __cplusplus
}
#endif

#endif /* COMMON_H */

