170 lines
3.6 KiB
C
170 lines
3.6 KiB
C
#include "sccb_gpio.h"
|
|
|
|
|
|
#define SCCB_ID 0x42
|
|
|
|
void SCCB_SDA_IN()
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
GPIO_InitStructure.GPIO_Pin = I2C_EE_SDA ;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
|
GPIO_Init(I2C_EE_GPIO, &GPIO_InitStructure);
|
|
}
|
|
void SCCB_SDA_OUT() {
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
GPIO_InitStructure.GPIO_Pin = I2C_EE_SDA ;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
|
GPIO_Init(I2C_EE_GPIO, &GPIO_InitStructure);
|
|
}
|
|
|
|
//???SCCB??
|
|
//CHECK OK
|
|
void SCCB_Init(void){
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
/* Configure I2C_EE pins: SCL and SDA */
|
|
GPIO_InitStructure.GPIO_Pin = I2C_EE_SCL ;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
|
GPIO_Init(I2C_EE_GPIO, &GPIO_InitStructure);
|
|
GPIO_InitStructure.GPIO_Pin = I2C_EE_SDA ;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
|
GPIO_Init(I2C_EE_GPIO, &GPIO_InitStructure);
|
|
SCCB_SDA_OUT();
|
|
GPIO_SetBits(GPIOB,I2C_EE_SDA);
|
|
GPIO_SetBits(GPIOB,I2C_EE_SCL);
|
|
}
|
|
void delay_us(unsigned int x){
|
|
for(unsigned int i = 0;i < x;i++){
|
|
for(unsigned int z = 0;z < 2;z++){
|
|
z = z;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
//SCCB????
|
|
//??????,???????,?????
|
|
//??????,SDA?SCL?????
|
|
void SCCB_Start(void){
|
|
GPIO_SetBits(GPIOB,I2C_EE_SDA);
|
|
GPIO_SetBits(GPIOB,I2C_EE_SCL);
|
|
delay_us(50);
|
|
GPIO_ResetBits(GPIOB,I2C_EE_SDA);
|
|
delay_us(50);
|
|
GPIO_ResetBits(GPIOB,I2C_EE_SCL);
|
|
}
|
|
|
|
//SCCB????
|
|
//??????,???????,?????
|
|
//??????,SDA?SCL?????
|
|
void SCCB_Stop(void){
|
|
GPIO_ResetBits(GPIOB,I2C_EE_SDA);
|
|
delay_us(50);
|
|
GPIO_SetBits(GPIOB,I2C_EE_SCL);
|
|
delay_us(50);
|
|
GPIO_SetBits(GPIOB,I2C_EE_SDA);
|
|
delay_us(50);
|
|
}
|
|
//??NA??
|
|
void SCCB_No_Ack(void)
|
|
{
|
|
delay_us(50);
|
|
GPIO_SetBits(GPIOB,I2C_EE_SDA);
|
|
GPIO_SetBits(GPIOB,I2C_EE_SCL);
|
|
delay_us(50);
|
|
GPIO_ResetBits(GPIOB,I2C_EE_SCL);
|
|
delay_us(50);
|
|
GPIO_SetBits(GPIOB,I2C_EE_SDA);
|
|
delay_us(50);
|
|
}
|
|
//SCCB,??????
|
|
//??0????
|
|
u8 SCCB_WR_Byte(u8 dat)
|
|
{
|
|
u8 j,res;
|
|
for(j=0;j<8;j++) //??8?????
|
|
{
|
|
if(dat&0x80)
|
|
GPIO_SetBits(GPIOB,I2C_EE_SDA);
|
|
else
|
|
GPIO_ResetBits(GPIOB,I2C_EE_SDA);
|
|
dat<<=1;
|
|
delay_us(50);
|
|
GPIO_SetBits(GPIOB,I2C_EE_SCL);
|
|
delay_us(50);
|
|
GPIO_ResetBits(GPIOB,I2C_EE_SCL);
|
|
}
|
|
SCCB_SDA_IN();
|
|
delay_us(50);
|
|
GPIO_SetBits(GPIOB,I2C_EE_SCL);
|
|
delay_us(50);
|
|
|
|
if(GPIO_ReadInputDataBit(GPIOB,I2C_EE_SDA))
|
|
res=1; //SDA=1????
|
|
else
|
|
res=0; //SDA=0????
|
|
GPIO_ResetBits(GPIOB,I2C_EE_SCL);
|
|
SCCB_SDA_OUT();
|
|
return res;
|
|
}
|
|
//SCCB ??????
|
|
//?SCL????,????
|
|
//???:?????
|
|
u8 SCCB_RD_Byte(void)
|
|
{
|
|
u8 temp=0,j;
|
|
SCCB_SDA_IN();
|
|
for(j=8;j>0;j--) //??8?????
|
|
{
|
|
delay_us(50);
|
|
GPIO_SetBits(GPIOB,I2C_EE_SCL);
|
|
temp=temp<<1;
|
|
if(GPIO_ReadInputDataBit(GPIOB,I2C_EE_SDA))
|
|
temp++;
|
|
delay_us(50);
|
|
GPIO_ResetBits(GPIOB,I2C_EE_SCL);
|
|
}
|
|
SCCB_SDA_OUT();
|
|
return temp;
|
|
}
|
|
//????
|
|
//???:0??
|
|
u8 SCCB_WR_Reg(u8 reg,u8 data)
|
|
{
|
|
u8 res=0;
|
|
SCCB_Start(); //????
|
|
if(SCCB_WR_Byte(SCCB_ID))res=1; //???ID
|
|
delay_us(100);
|
|
if(SCCB_WR_Byte(reg))res=1; //??????
|
|
delay_us(100);
|
|
if(SCCB_WR_Byte(data))res=1; //???
|
|
SCCB_Stop(); //????
|
|
return res;
|
|
}
|
|
//????
|
|
u8 SCCB_RD_Reg(u8 reg)
|
|
{
|
|
u8 val=0;
|
|
SCCB_Start(); //????
|
|
SCCB_WR_Byte(SCCB_ID); //???ID
|
|
delay_us(100);
|
|
SCCB_WR_Byte(reg); //??????
|
|
delay_us(100);
|
|
SCCB_Stop();
|
|
delay_us(100);
|
|
//???????????
|
|
SCCB_Start();
|
|
SCCB_WR_Byte(SCCB_ID|0X01); //?????
|
|
delay_us(100);
|
|
val=SCCB_RD_Byte(); //???
|
|
SCCB_No_Ack(); //??NA
|
|
SCCB_Stop(); //????
|
|
return val;
|
|
}
|