#include "iic_slave.h"
#include "stm32f10x_i2c.h"
u8 Buffer_Rx_IIC1[40]; //接收缓存
u8 Rx_Idx_IIC1=0; //接收计数
u8 Flag_RcvOK_IIC1 = 0; //接收完成标志
u8 Tx_Idx_IIC1=0; //发送计数
u8 Response_Message[40] = {11,12,13,16,17,16};//发送缓存
u8 recv_cnt = 0;
void I2C1_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// SCL PB6
// SDA PB7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; //必须设置为开漏输出,实现iic的线与逻辑
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_I2C1);
// GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_I2C1);
}
void I2C1_Configuration(void)
{
I2C_InitTypeDef I2C_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
I2C_DeInit(I2C1);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = I2C1_Slave_Address; //从机地址,一定要设置正确
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress= I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 10000;
I2C_Init(I2C1, &I2C_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;//事件中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = I2C1_ER_IRQn;//错误中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
I2C_ITConfig(I2C1, I2C_IT_BUF | I2C_IT_EVT |I2C_IT_ERR, ENABLE);
I2C_Cmd(I2C1, ENABLE);
}
void I2C1_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
I2C1_GPIO_Configuration();
I2C1_Configuration();
}
#define IIC_P_MAX_SENDLEN 40
#define IIC_P_MAX_RECVLEN 5
typedef union
{
__IO uint32_t reg;
struct
{
uint32_t STARTBF : 1;
uint32_t ADDRF : 1;
uint32_t BSF : 1;
uint32_t ADDR10F : 1;
uint32_t STOPF : 1;
uint32_t Reserved2 : 1;
uint32_t RXDATNE : 1;
uint32_t TXDATE : 1;
uint32_t BUSERR : 1;
uint32_t ARLOST : 1;
uint32_t ACKFAIL : 1;
uint32_t OVERRUN : 1;
uint32_t PECERR : 1;
uint32_t Reserved1 : 1;
uint32_t TIMOUT : 1;
uint32_t SMBALERT : 1;
uint32_t Rev : 16;
}Bit;
}I2C_STS1_TypeDef;
typedef union
{
__IO uint32_t reg;
struct
{
uint32_t MSMODE : 1;
uint32_t BUSY : 1;
uint32_t TRF : 1;
uint32_t Reserved1 : 1;
uint32_t GCALLADDR : 1;
uint32_t SMBDADDR : 1;
uint32_t SMBHADDR : 1;
uint32_t DUALFLAG : 1;
uint32_t PECVAL : 7;
uint32_t REV : 16;
}Bit;
}I2C_STS2_TypeDef;
typedef struct
{
uint8_t Byte[30];
struct
{
uint16_t totalVol;
int16_t current;
uint16_t SOC;
uint16_t sysState;
uint16_t SOH;
uint16_t nominalCap;
uint16_t FCCap;
uint16_t RemainCap;
uint16_t cycleCnt;
uint16_t cellMaxvol;
uint16_t cellMinVol;
uint16_t highTemp;
uint16_t lowTemp;
uint16_t protectState;
uint8_t crc;
uint8_t len;
}msg;
}IIC_P_MSG_Struct;
IIC_P_MSG_Struct IIC_P_msg;
typedef struct
{
uint8_t recvStartFlag;
uint8_t recvEndFlag;
uint8_t sendStartFlag;
uint8_t sendEndFlag;
uint8_t errFlag;
uint8_t recvCnt;
uint8_t sendCnt;
uint8_t sendPMFlag;
uint8_t recvBuf[IIC_P_MAX_SENDLEN];
uint8_t *sendPtr;
}IIC_P_CmdStr_Struct;
IIC_P_CmdStr_Struct IIC_P_CmdStr;
static void IntoShutDown(void);
static uint8_t IIC_P_calc_crc8(uint8_t *ptr , uint8_t len)
{
uint8_t i = 0;
uint8_t crc=0x00;
while(len--)
{
crc ^= *ptr++;
for (i=8; i>0; --i)
{
if (crc & 0x80)
crc = (crc << 1) ^ 0x31;
else
crc = (crc << 1);
}
}
return (crc);
}
#if 0
static void IIC_P_GPIO_Init(void)
{
GPIO_InitType GPIO_InitStruct;
RCC_EnableAPB2PeriphClk(IIC_P_PORT_RCC, ENABLE); //ENABLE CLOCK
GPIO_InitStruct.Pin = IIC_P_SDA|IIC_P_SCL;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(IIC_P_PORT, &GPIO_InitStruct);
//GPIO_ConfigPinRemap(GPIO_RMP_I2C1, ENABLE); //or need Peripheral remap to other pin
}
static void IIC_P_Init(void)
{
I2C_InitType I2C_InitStruct;
NVIC_InitType NVIC_InitStruct;
#if (IIC_P_IN_APBn==IIC_P_APB1)
RCC_EnableAPB2PeriphClk(IIC_P_PERIPH_RCC, ENABLE);
#else
RCC_EnableAPB2PeriphClk(IIC_P_PERIPH_RCC, ENABLE);
#endif
I2C_DeInit(IIC_P_IICx);
I2C_InitStruct.BusMode = I2C_BUSMODE_I2C;
I2C_InitStruct.AddrMode = I2C_ADDR_MODE_7BIT;
I2C_InitStruct.AckEnable = I2C_ACKEN;
I2C_InitStruct.FmDutyCycle = I2C_FMDUTYCYCLE_2;
I2C_InitStruct.ClkSpeed = 100000;
I2C_InitStruct.OwnAddr1 = IIC_P_Slave_Address;
I2C_Init(IIC_P_IICx, &I2C_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = I2C4_EV_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = I2C4_ER_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
/*enable I2C interrupt*/
I2C_ConfigInt(IIC_P_IICx, I2C_INT_BUF | I2C_INT_EVENT |I2C_INT_ERR, ENABLE);
I2C_Enable(IIC_P_IICx, ENABLE);
}
void IIC_Periph_Init(void)
{
IIC_P_GPIO_Init();
IIC_P_Init();
}
#endif
void I2C1_EV_IRQHandler(void)
{
uint8_t crc = 0;
I2C_STS1_TypeDef STS1R;
I2C_STS2_TypeDef STS2R;
STS1R.reg = I2C1->SR1;
STS2R.reg = I2C1->SR2;
/* MSL = 0 I2C is a slave */
if(STS2R.Bit.MSMODE == 0)
//if((SR2Register &0x0001) != 0x0001)
{
/*master send Addr, ADDRF bit set 1: EV1(contain master send or receive)) */
if(STS1R.Bit.ADDRF == 1)
// if((SR1Register & 0x0002) == 0x0002)
{
/*clear this event */
STS1R.reg = 0;
STS2R.reg = 0;
IIC_P_CmdStr.recvCnt = 0;
IIC_P_CmdStr.sendCnt = 0;
IIC_P_CmdStr.recvStartFlag = 1;
}
/* receive data(RXNE = 1: EV2) */
if(STS1R.Bit.RXDATNE == 1)
// if((SR1Register & 0x0040) == 0x0040)
{
IIC_P_CmdStr.recvBuf[IIC_P_CmdStr.recvCnt] = I2C1->DR;
IIC_P_CmdStr.recvCnt++;
if(IIC_P_CmdStr.recvCnt>=IIC_P_MAX_RECVLEN)
{
IIC_P_CmdStr.recvCnt = 0;
}
STS1R.reg = 0;
STS2R.reg = 0;
}
/* send data event(TxE = 1: EV3\EV3-1) */
if(STS1R.Bit.TXDATE == 1)
// if((SR1Register & 0x0080) == 0x0080)
{
I2C1->DR = IIC_P_CmdStr.sendPtr[IIC_P_CmdStr.sendCnt];
IIC_P_CmdStr.sendCnt ++;
if(IIC_P_CmdStr.sendCnt >= IIC_P_MAX_SENDLEN)
{
IIC_P_CmdStr.sendCnt = 0;
}
STS1R.reg = 0;
STS2R.reg = 0;
}
/*detect NACK event(AF =1: EV3-2) */
if(STS1R.Bit.ACKFAIL == 1)
// if(( SR1Register & 0x0400) == 0x0400)
{
I2C1->SR1 &= 0xFDFF;
STS1R.reg = 0;
STS2R.reg = 0;
}
/*receive stop condition(STOPF =1: EV4) */
if(STS1R.Bit.STOPF == 1)
// if(( SR1Register & 0x0010) == 0x0010)
{
I2C1->CR1 |= 0x0001;
STS1R.reg = 0;
STS2R.reg = 0;
if(IIC_P_CmdStr.recvCnt==4)
{
crc = IIC_P_calc_crc8(&IIC_P_CmdStr.recvBuf[0],3);
//if(crc == IIC_P_CmdStr.recvBuf[3])
{
if((IIC_P_CmdStr.recvBuf[0]==0x05) &&
(IIC_P_CmdStr.recvBuf[1]==0xA5)&&
(IIC_P_CmdStr.recvBuf[2]==0x00)) /*this cmd is shutdown*/
{
IntoShutDown();
}
else if((IIC_P_CmdStr.recvBuf[0]==0x04) &&
(IIC_P_CmdStr.recvBuf[1]==0x01) &&
(IIC_P_CmdStr.recvBuf[2]==0xF))/*this cmd is read data*/
{
IIC_P_CmdStr.sendPMFlag = 1;
IIC_P_FillMsg();
IIC_P_CmdStr.sendPtr = &IIC_P_msg.Byte[0];
}
else {}
}
//else {}
}
IIC_P_CmdStr.recvEndFlag = 1;
}
}
/* MSL = 1 I2C is a master */
if(STS2R.Bit.MSMODE ==