
#include "BSP.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

//===============================================================================
/********************************** 功能说明 ***********************************
 *	访问I2C总线
 *******************************************************************************/
#define PinBB(_Port, _Num) (*(__IO int32_t *)(PERIPH_BB_BASE + ((uint32_t) & (_Port)-PERIPH_BASE) * 32u + (_Num)*4u))

/* USER CODE BEGIN 1 */

#define I2C_SCL_PIN  GPIO_Pin_4
#define I2C_SCL_GPIO GPIOB
#define I2C_SDA_PIN  GPIO_Pin_5
#define I2C_SDA_GPIO GPIOB
extern void delay_us(uint32_t us);

void I2C_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    GPIO_InitStructure.GPIO_Pin   = I2C_SCL_PIN | I2C_SDA_PIN;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_OD;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    GPIO_SetBits(I2C_SDA_GPIO, I2C_SDA_PIN);
    GPIO_SetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
}
void I2C_Start(void)
{
    //     SDA:	/￣\__
    //     SCL:	/￣￣\__

    GPIO_SetBits(I2C_SDA_GPIO, I2C_SDA_PIN);
    GPIO_SetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
    delay_us(1);
    GPIO_ResetBits(I2C_SDA_GPIO, I2C_SDA_PIN);
    delay_us(1);
    GPIO_ResetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
    //	delay_us(1);
}

void I2C_Stop(void)
{
    //     SDA:	\__/￣
    //     SCL:	\__/￣
    GPIO_ResetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
    GPIO_ResetBits(I2C_SDA_GPIO, I2C_SDA_PIN);
    delay_us(1);
    GPIO_SetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
    GPIO_SetBits(I2C_SDA_GPIO, I2C_SDA_PIN);
}
// 产生ACK应答
void I2C_ACK()
{
    //     SDA:	\________
    //     SCL:	\__/￣\__
    GPIO_ResetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
    GPIO_ResetBits(I2C_SDA_GPIO, I2C_SDA_PIN);
    delay_us(1);
    GPIO_SetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
    delay_us(1);
    GPIO_ResetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
}

// 不产生ACK应答
void I2C_NACK()
{
    //     SDA:	/￣￣￣￣
    //     SCL:	\__/￣\__
    GPIO_ResetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
    GPIO_SetBits(I2C_SDA_GPIO, I2C_SDA_PIN);
    delay_us(1);
    GPIO_SetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
    delay_us(1);
    GPIO_ResetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
}
// 等待应答信号到来
// 返回值：1，接收应答失败
//         0，接收应答成功
uint8_t I2C_Wait_ACK()
{
    uint8_t timeOut = 0;
    //     SDA:	/￣ X
    //     SCL:	--/￣￣\__
    GPIO_SetBits(I2C_SDA_GPIO, I2C_SDA_PIN);
    delay_us(1);
    GPIO_SetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
    delay_us(1);
    while (GPIO_ReadInputDataBit(I2C_SDA_GPIO, I2C_SDA_PIN)) {
        delay_us(1); //
        timeOut++;
        if (timeOut > 250) {
            I2C_Stop();
            return 1;
        }
    }
    GPIO_ResetBits(I2C_SCL_GPIO, I2C_SCL_PIN);

    return 0;
}
// IIC发送一个字节
void I2C_SendByte(uint8_t byte)
{
    uint8_t i;
    GPIO_ResetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
    delay_us(1);
    for (i = 0; i < 8; i++) {
        if (byte & 0x80)
            GPIO_SetBits(I2C_SDA_GPIO, I2C_SDA_PIN);
        else
            GPIO_ResetBits(I2C_SDA_GPIO, I2C_SDA_PIN);

        delay_us(1);
        GPIO_SetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
        delay_us(1);
        GPIO_ResetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
        byte <<= 1;
    }
}
// 读1个字节
uint8_t I2C_ReadByte(uint8_t ack)
{
    uint8_t i;
    uint8_t byte = 0;
    GPIO_SetBits(I2C_SDA_GPIO, I2C_SDA_PIN);
    delay_us(1);

    for (i = 0; i < 8; i++) {
        GPIO_ResetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
        delay_us(1);
        byte <<= 1;
        GPIO_SetBits(I2C_SCL_GPIO, I2C_SCL_PIN);
        if (GPIO_ReadInputDataBit(I2C_SDA_GPIO, I2C_SDA_PIN))
            byte |= 0x01;
        delay_us(1);
    }

    if (!ack)
        I2C_NACK(); // 发送nACK
    else
        I2C_ACK(); // 发送ACK

    return byte;
}

#define I2C2_SCL_PIN  GPIO_Pin_8
#define I2C2_SCL_GPIO GPIOB
#define I2C2_SDA_PIN  GPIO_Pin_9
#define I2C2_SDA_GPIO GPIOB

void I2C2_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    GPIO_InitStructure.GPIO_Pin   = I2C2_SCL_PIN | I2C2_SDA_PIN;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_OD;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    GPIO_SetBits(I2C2_SDA_GPIO, I2C2_SDA_PIN);
    GPIO_SetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
}
void I2C2_Start(void)
{
    GPIO_SetBits(I2C2_SDA_GPIO, I2C2_SDA_PIN);
    GPIO_SetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
    delay_us(1);
    GPIO_ResetBits(I2C2_SDA_GPIO, I2C2_SDA_PIN);
    delay_us(1);
    GPIO_ResetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
}

void I2C2_Stop(void)
{
    GPIO_ResetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
    GPIO_ResetBits(I2C2_SDA_GPIO, I2C2_SDA_PIN);
    delay_us(1);
    GPIO_SetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
    GPIO_SetBits(I2C2_SDA_GPIO, I2C2_SDA_PIN);
}
// 产生ACK应答
void I2C2_ACK()
{
    GPIO_ResetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
    GPIO_ResetBits(I2C2_SDA_GPIO, I2C2_SDA_PIN);
    delay_us(1);
    GPIO_SetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
    delay_us(1);
    GPIO_ResetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
}

// 不产生ACK应答
void I2C2_NACK()
{
    GPIO_ResetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
    GPIO_SetBits(I2C2_SDA_GPIO, I2C2_SDA_PIN);
    delay_us(1);
    GPIO_SetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
    delay_us(1);
    GPIO_ResetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
}
// 等待应答信号到来
// 返回值：1，接收应答失败
//         0，接收应答成功
uint8_t I2C2_Wait_ACK()
{
    uint8_t timeOut = 0;

    GPIO_SetBits(I2C2_SDA_GPIO, I2C2_SDA_PIN);
    delay_us(1);
    GPIO_SetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
    delay_us(1);
    while (GPIO_ReadInputDataBit(I2C2_SDA_GPIO, I2C2_SDA_PIN)) {
        delay_us(1); //
        timeOut++;
        if (timeOut > 250) {
            I2C2_Stop();
            return 1;
        }
    }
    GPIO_ResetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
    delay_us(1);
    return 0;
}
void I2C2_SendByte(uint8_t byte)
{
    uint8_t i;
    GPIO_ResetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
    delay_us(1);
    for (i = 0; i < 8; i++) {
        if (byte & 0x80)
            GPIO_SetBits(I2C2_SDA_GPIO, I2C2_SDA_PIN);
        else
            GPIO_ResetBits(I2C2_SDA_GPIO, I2C2_SDA_PIN);

        delay_us(1);
        GPIO_SetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
        delay_us(1);
        GPIO_ResetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
        byte <<= 1;
    }
}

uint8_t I2C2_ReadByte(uint8_t ack)
{
    uint8_t i;
    uint8_t byte = 0;
    GPIO_SetBits(I2C2_SDA_GPIO, I2C2_SDA_PIN);

    for (i = 0; i < 8; i++) {
        GPIO_ResetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
        delay_us(1);
        byte <<= 1;
        GPIO_SetBits(I2C2_SCL_GPIO, I2C2_SCL_PIN);
        delay_us(1);
        if (GPIO_ReadInputDataBit(I2C2_SDA_GPIO, I2C2_SDA_PIN))
            byte |= 0x01;
    }

    if (!ack)
        I2C2_NACK(); // 发送nACK
    else
        I2C2_ACK(); // 发送ACK

    return byte;
}

 #define Pin_I2C_STB1_Out PinBB(GPIOA->ODR, 12U)
// #define Pin_I2C_STB2_Out PinBB(GPIOG->ODR, 12U)
#define Pin_I2C_SCL_In  PinBB(GPIOB->IDR, 6U)
#define Pin_I2C_SCL_Out PinBB(GPIOB->ODR, 6U)
#define Pin_I2C_SDA_In  PinBB(GPIOB->IDR, 7U)
#define Pin_I2C_SDA_Out PinBB(GPIOB->ODR, 7U)
/* USER CODE BEGIN 1 */
void WS100T10_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_ResetBits(GPIOA, GPIO_Pin_15);
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

	  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

		GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_OD;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
		Pin_I2C_SCL_Out = 1;
		Pin_I2C_SDA_Out = 1;  

    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_12 ;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_OD;
    GPIO_Init(GPIOA, &GPIO_InitStructure);	

}
void WS100T10_Ctrl(uint8_t OutByte)
{

    uint8_t i, DataH, DataL;
    uint16_t Data;
    // if ((Channel == 1) || (Channel == 2)) {
    //     Pin_I2C_STB1_Out = 0;
    //     Pin_I2C_STB2_Out = 1;
    // } else {
    //     Pin_I2C_STB1_Out = 1;
    //     Pin_I2C_STB2_Out = 0;
    // }
    //    if ((Channel == 2) || (Channel == 4))
    //        DataH |= 0x80u;
    DataH = OutByte;

    DataL = ~DataH;
    Data  = DataL;
    Data |= (DataH << 8);
		Pin_I2C_STB1_Out = 0;
    delay_us(300);

    for (i = 16U; i != 0U; --i) {
        Pin_I2C_SCL_Out = 0;
        delay_us(100);
        if (Data & 0x8000u) {
            Pin_I2C_SDA_Out = 1;
        } else {
            Pin_I2C_SDA_Out = 0;
        }
        delay_us(300);

        Data <<= 1;

        Pin_I2C_SCL_Out = 1;
        delay_us(300);
    }

    Pin_I2C_SDA_Out = 1;
//		Pin_I2C_STB1_Out = 1;
    Pin_I2C_SCL_Out = 1;
    delay_us(300);
}
/* USER CODE END 1 */
