#include "i2c_test.h"
#include "USART2.H"
#include "crc8.h"
#include "utils.h"
#define TIME_OUT
#define SMBus_Max_Delay_Cycles 0x7ff//1024//256
uint8_t write_Addr_Data[4] = {0x00, 0x2E, 0x02, 0x00};
uint8_t EraseSA[4]={0x00,0x2E,0x00,0x00};
uint32_t wrt_Addr_pec = 0x85;
uint32_t erase_Addr_pec = 0x6f;
/*******************************************************************************
* Function Name : crc8
* Description :
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
// Return CRC-8 of the data, using x^8 + x^2 + x + 1 polynomial.
// A table-based algorithm would be faster, but for only a few bytes
// it isn't worth the code size.
// Ref: https://chromium.googlesource.com/chromiumos/platform/vboot_reference/+/master/firmware/lib/crc8.c
/* uint8_t crc8(const void *vptr, int len) */
/* { */
/* const uint8_t *data = vptr; */
/* unsigned crc = 0; */
/* int i, j; */
/* for (j = len; j; j--, data++) */
/* { */
/* crc ^= (*data << 8); */
/* for (i = 8; i; i--) */
/* { */
/* if (crc & 0x8000) crc ^= (0x1070 << 3); */
/* crc <<= 1; */
/* } */
/* } */
/* return (uint8_t)(crc >> 8); */
/* } */
/*******************************************************************************
* Function Name : I2C_Configuration
* Description :
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void I2C_Configuration(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_I2C2); //这两个的配置必须有,没有则无法输出
//GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_I2C2);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
// RCC_APB2Periph_AFIO
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
/* Configure I2C2 pins: PB10->SCL and PB11->SDA */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_DeInit(I2C2);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x66;//0x30;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; // disable ack
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 200000;//100000 // 2018/1/4 lower speed >10K e.g 20K
I2C_Cmd(I2C2, ENABLE);
I2C_Init(I2C2, &I2C_InitStructure);
I2C_AcknowledgeConfig(I2C2, ENABLE);
}
/*******************************************************************************
* Function Name : I2C_delay
* Description :
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void I2C_delay(uint16_t cnt)
{
while (cnt--)
;
}
/*******************************************************************************
* Function Name : I2C_AcknowledgePolling
* Description :
* Input : I2C_TypeDef * , uint8_t
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void I2C_AcknowledgePolling(I2C_TypeDef *I2Cx, uint8_t I2C_Addr)
{
vu16 SR1_Tmp;
do
{
I2C_GenerateSTART(I2Cx, ENABLE);
SR1_Tmp = I2C_ReadRegister(I2Cx, I2C_Register_SR1);
/* #ifdef AT24C01A */
I2C_Send7bitAddress(I2Cx, I2C_Addr, I2C_Direction_Transmitter);
/* #else */
/* I2C_Send7bitAddress(I2Cx, 0, I2C_Direction_Transmitter); */
/* #endif */
} while (!(I2C_ReadRegister(I2Cx, I2C_Register_SR1) & 0x0002));
I2C_ClearFlag(I2Cx, I2C_FLAG_AF);
I2C_GenerateSTOP(I2Cx, ENABLE);
}
/*******************************************************************************
* Function Name : I2C_Read
* Description :
* Input : addr MLX cmd addr
* Output :
* Return :
* Attention : None
*******************************************************************************/
uint8_t I2C_Read(I2C_TypeDef *I2Cx, uint8_t I2C_Addr, uint8_t addr, uint8_t *buf, uint16_t num)
{
uint32_t counter =SMBus_Max_Delay_Cycles;
if (num == 0) return 1;
/* while (I2C_GetFlagStatus(SMBus_NAME, I2C_FLAG_BUSY)&&counter) --counter; */
/* if (counter == 0) */
/* { */
/* return 1; */
/* } */
while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
{
I2C_Reset(I2Cx);
//USART_SendData(USART2,0x52) ;
}
I2C_AcknowledgeConfig(I2Cx, ENABLE);
I2C_GenerateSTART(I2Cx, ENABLE);
#if 0
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
;
#endif
#ifdef TIME_OUT
counter = SMBus_Max_Delay_Cycles;
while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))&&counter) --counter;
if (counter == 0)
{
I2C_Reset(I2Cx);
return 1;
}
#else
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
;
#endif
//#ifdef AT24C01A
I2C_Send7bitAddress(I2Cx, I2C_Addr, I2C_Direction_Transmitter);
#if 0
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
; // EV5
#endif
#ifdef TIME_OUT
counter = SMBus_Max_Delay_Cycles;
while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))&&counter) --counter;
if (counter == 0)
{
I2C_Reset(I2Cx);
return 1;
}
#else
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
; // EV5
#endif
I2C_SendData(I2Cx, addr); // send cmd
#if 0
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
#endif
#ifdef TIME_OUT
counter = SMBus_Max_Delay_Cycles;
while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))&&counter) --counter;
if (counter == 0)
{
I2C_Reset(I2Cx);
return 1;
}
#else
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
#endif
I2C_GenerateSTART(I2Cx, ENABLE); // repeated start condition
#if 0
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
;
#endif
#ifdef TIME_OUT
counter = SMBus_Max_Delay_Cycles;
while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))&&counter) --counter;
if (counter == 0)
{
I2C_Reset(I2Cx);
return 1;
}
#else
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
;
#endif
I2C_Send7bitAddress(I2Cx, I2C_Addr, I2C_Direction_Receiver);
#if 0
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
; // EV7
#endif
#ifdef TIME_OUT
counter = SMBus_Max_Delay_Cycles;
while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))&&counter) --counter;
if (counter == 0)
{
I2C_Reset(I2Cx);
return 1;
}
#else
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
; // EV7
#endif
//#else
// I2C_Send7bitAddress(I2Cx, addr<<1, I2C_Direction_Receiver);
// whil
评论0