I2C 通讯协议:(Inter-Integrated Circuit)是由Phiilps 公司开发的,由于它引脚少,硬件实现简单,可扩展性强,不需要USART、CAN 等通讯协议的外部收发设备,现在被广泛地使用在系统内多个集成电路(IC)间的通讯,其常用的连接方式如下:
物理层:
(1) 它是一个支持设备的总线。“总线”指多个设备共用的信号线。在一个I2C 通讯总线中,可连接多个I2C 通讯设备,支持多个通讯主机及多个通讯从机。
(2) 一个I2C 总线只使用两条总线线路,一条双向串行数据线(SDA) ,一条串行时钟线(SCL)。数据线即用来表示数据,时钟线用于数据收发同步。
(3) 每个连接到总线的设备都有一个独立的地址,主机可以利用这个地址进行不同设备之间的访问。
(4) 总线通过上拉电阻接到电源。当I2C 设备空闲时,会输出高阻态,而当所有设备都空闲,都输出高阻态时,由上拉电阻把总线拉成高电平。
(5) 多个主机同时使用总线时,为了防止数据冲突,会利用仲裁方式决定由哪个设备占用总线。
(6) 具有三种传输模式:标准模式传输速率为100kbit/s ,快速模式为400kbit/s ,高速模式下可达3.4Mbit/s,但目前大多I C 设备尚不支持高速模式。
(7) 连接到相同总线的IC 数量受到总线的最大电容400pF 限制
协议层:
- 起始信号:由主机的IIC接口产生的传输起始信号,这时连接到总线上的所有从机都会收到这个信号;
- 地址广播:起始信号产生后,所有从机都会开始等待主机接下来广播的从机地址信号,以选中从机设备,没有被选中的将会忽略之后的数据信号;
- 传输方向:0表示主机向从机写数据,1表示主机向从机读数据;
- 应答信号:从机接收到匹配的地址之后,从机或主机会返回一个应答(ACK)或非应答(NACK)信号,只有接收到信号,主机才能继续发送或接受数据;
- 写数据/读数据:主机每发送完一个数据包之后。都要重新等待从机的应答信号,并重复这个步骤;/从机每发送玩一个数据包之后,都会重新等待主机的应答信号,并重复这个步骤;
- 停止信号:数据传输完成后(得到NACK信号后),主机向从机发送一个停止传输信号,表示不再传输。/当主机希望停止接受数据时,就会返回一个NACK信号给从机,从机就会自动停止数据传输;
除了基本的读写,I2C通讯更常用的是复合格式,即第三幅图的情况,该传输过程有两次起始信号(S)。一般在第一次传输中,主机通过SLAVE_ADDRESS 寻找到从设备后,发送一段“数据”,这段数据通常用于表示从设备内部的寄存器或存储器地址(注意区分它与 SLAVE_ADDRESS 的区别);在第二次的传输中,对该地址的内容进行读或写。也就是说,第一次通讯是告诉从机读写地址,第二次则是读写的实际内容。
bsp_iic_ee.h文件如下:
#ifndef __I2C_EE_H #define __I2C_EE_H #include "stm32f10x.h" #define I2Cx_EEPROM I2C1 #define I2C_EEPROM_CLK RCC_APB1Periph_I2C1 #define I2C_EEPROM_GPIO_CLK RCC_APB2Periph_GPIOB #define I2C_EPPROM_SCL_PORT GPIOB//时钟线 #define I2C_EEPROM_SCL_PIN GPIO_Pin_6 #define I2C_EEPROM_SDA_PORT GPIOB//数据线 #define I2C_EEPROM_SDA_PIN GPIO_Pin_7 #define I2C_Speed 400000//通讯速度kbits/s #define I2Cx_OWN_ADDRESS7 0X0A //主机地址可自行配置,但在总线上这个地址要唯一 #define I2C_PageSize 8//每页8个字节 #define I2CT_FLAG_TIMEOUT ((uint32_t)0x1000)//等待超时时间 #define I2CT_LONG_TIMEOUT ((uint32_t)(10 * I2CT_FLAG_TIMEOUT)) #define EEPROM_DEBUG_ON 0 #define EEPROM_INFO(fmt,arg...) printf("<<-EEPROM-INFO->> "fmt"\n",##arg)//##args表示参数个数可变 #define EEPROM_ERROR(fmt,arg...) printf("<<-EEPROM-ERROR->> "fmt"\n",##arg) #define EEPROM_DEBUG(fmt,arg...) do{\ if(EEPROM_DEBUG_ON)\ printf("<<-EEPROM-DEBUG->> [%d]"fmt"\n",__LINE__, ##arg);\ }while(0) /* * AT24C02 2kb = 2048bit = 2048/8 B = 256 B * 32 pages of 8 bytes each * * Device Address * 1 0 1 0 A2 A1 A0 R/W * 1 0 1 0 0 0 0 0 = 0XA0//写地址 * 1 0 1 0 0 0 0 1 = 0XA1 //读地址 */ /* EEPROM Addresses defines */ #define EEPROM_Block0_ADDRESS 0xA0 /* E2 = 0 */ //#define EEPROM_Block1_ADDRESS 0xA2 /* E2 = 0 */ //#define EEPROM_Block2_ADDRESS 0xA4 /* E2 = 0 */ //#define EEPROM_Block3_ADDRESS 0xA6 /* E2 = 0 */ void I2C_EE_Init(void); void I2C_EE_BufferWrite(u8* pBuffer, u8 WriteAddr, u16 NumByteToWrite); uint32_t I2C_EE_ByteWrite(u8* pBuffer, u8 WriteAddr); uint32_t I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite); uint32_t I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead); void I2C_EE_WaitEepromStandbyState(void); #endif
bsp_iic_ee.c文件如下:
#include "bsp_iic_ee.h" #include "bsp_usart.h" uint16_t EEPROM_ADDRESS;//从设备地址 static __IO uint32_t I2CTimeout = I2CT_LONG_TIMEOUT; //定义超时变量 static uint32_t I2C_TIMEOUT_UserCallback(uint8_t errorCode);//声明超时用户回调函数 static void I2C_GPIO_Config(void)//引脚配置 { GPIO_InitTypeDef GPIO_InitStructure; //开启I2C,GPIO时钟 RCC_APB1PeriphClockCmd(I2C_EEPROM_CLK, ENABLE ); RCC_APB2PeriphClockCmd(I2C_EEPROM_GPIO_CLK, ENABLE ); //配置SCL GPIO_InitStructure.GPIO_Pin = I2C_EEPROM_SCL_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; //这里必须复用开漏输出 GPIO_Init(I2C_EPPROM_SCL_PORT , &GPIO_InitStructure); //配置SDA GPIO_InitStructure.GPIO_Pin = I2C_EEPROM_SDA_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; //也必须复用开漏输出 GPIO_Init(I2C_EEPROM_SDA_PORT , &GPIO_InitStructure); } static void I2C_Mode_Config(void)//模式配置 { I2C_InitTypeDef I2C_InitStructure; I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;//此处未分主从,直接选这个 I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;//占空比 I2C_InitStructure.I2C_OwnAddress1 =I2Cx_OWN_ADDRESS7; //主机地址 I2C_InitStructure.I2C_Ack = I2C_Ack_Enable ;//使能响应 I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;//一般使用7位地址 I2C_InitStructure.I2C_ClockSpeed = I2C_Speed;//速度 I2C_Init(I2Cx_EEPROM , &I2C_InitStructure);//初始化配置 I2C_Cmd(I2Cx_EEPROM , ENABLE); //使能I2C1 } void I2C_EE_Init(void) { I2C_GPIO_Config(); I2C_Mode_Config(); #ifdef EEPROM_Block0_ADDRESS EEPROM_ADDRESS = EEPROM_Block0_ADDRESS;//块0被选中 #endif #ifdef EEPROM_Block1_ADDRESS EEPROM_ADDRESS = EEPROM_Block1_ADDRESS; #endif #ifdef EEPROM_Block2_ADDRESS EEPROM_ADDRESS = EEPROM_Block2_ADDRESS; #endif #ifdef EEPROM_Block3_ADDRESS EEPROM_ADDRESS = EEPROM_Block3_ADDRESS; #endif }
//将缓冲区的数据写入到I2C_EE中 void I2C_EE_BufferWrite(u8* pBuffer, u8 WriteAddr, u16 NumByteToWrite) { u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0; Addr = WriteAddr % I2C_PageSize; count = I2C_PageSize - Addr; NumOfPage = NumByteToWrite / I2C_PageSize; NumOfSingle = NumByteToWrite % I2C_PageSize; /* If WriteAddr is I2C_PageSize aligned */ if(Addr == 0) { /* If NumByteToWrite < I2C_PageSize */ if(NumOfPage == 0) { I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle); I2C_EE_WaitEepromStandbyState(); } /* If NumByteToWrite > I2C_PageSize */ else { while(NumOfPage--) { I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize); I2C_EE_WaitEepromStandbyState(); WriteAddr += I2C_PageSize; pBuffer += I2C_PageSize; } if(NumOfSingle!=0) { I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle); I2C_EE_WaitEepromStandbyState(); } } } /* If WriteAddr is not I2C_PageSize aligned */ else { /* If NumByteToWrite < I2C_PageSize */ if(NumOfPage== 0) { I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle); I2C_EE_WaitEepromStandbyState(); } /* If NumByteToWrite > I2C_PageSize */ else { NumByteToWrite -= count; NumOfPage = NumByteToWrite / I2C_PageSize; NumOfSingle = NumByteToWrite % I2C_PageSize; if(count != 0) { I2C_EE_PageWrite(pBuffer, WriteAddr, count); I2C_EE_WaitEepromStandbyState(); WriteAddr += count; pBuffer += count; } while(NumOfPage--) { I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize); I2C_EE_WaitEepromStandbyState(); WriteAddr += I2C_PageSize; pBuffer += I2C_PageSize; } if(NumOfSingle != 0) { I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle); I2C_EE_WaitEepromStandbyState(); } } } } //写一个字节给EE,读读英文解释吧,这就是主机发送的过程 uint32_t I2C_EE_ByteWrite(u8* pBuffer, u8 WriteAddr) { /* Send STRAT condition */ I2C_GenerateSTART(I2Cx_EEPROM , ENABLE); I2CTimeout = I2CT_FLAG_TIMEOUT; /* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2Cx_EEPROM , I2C_EVENT_MASTER_MODE_SELECT)) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0); } I2CTimeout = I2CT_FLAG_TIMEOUT; /* Send EEPROM address for write */ I2C_Send7bitAddress(I2Cx_EEPROM , EEPROM_ADDRESS, I2C_Direction_Transmitter); /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2Cx_EEPROM , I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(1); } /* Send the EEPROM's internal address to write to */ I2C_SendData(I2Cx_EEPROM , WriteAddr); I2CTimeout = I2CT_FLAG_TIMEOUT; /* Test on EV8 and clear it */ while(!I2C_CheckEvent(I2Cx_EEPROM , I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(2); } /* Send the byte to be written */ I2C_SendData(I2Cx_EEPROM , *pBuffer); I2CTimeout = I2CT_FLAG_TIMEOUT; /* Test on EV8 and clear it */ while(!I2C_CheckEvent(I2Cx_EEPROM , I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(3); } /* Send STOP condition */ I2C_GenerateSTOP(I2Cx_EEPROM , ENABLE); return 1; } //页写入,但NumByteToWrite不能超过8 uint32_t I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite) { I2CTimeout = I2CT_LONG_TIMEOUT; while(I2C_GetFlagStatus(I2Cx_EEPROM , I2C_FLAG_BUSY)) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(4); } /* Send START condition */ I2C_GenerateSTART(I2Cx_EEPROM , ENABLE); I2CTimeout = I2CT_FLAG_TIMEOUT; /* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2Cx_EEPROM , I2C_EVENT_MASTER_MODE_SELECT)) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(5); } /* Send EEPROM address for write */ I2C_Send7bitAddress(I2Cx_EEPROM , EEPROM_ADDRESS, I2C_Direction_Transmitter); I2CTimeout = I2CT_FLAG_TIMEOUT; /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2Cx_EEPROM , I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(6); } /* Send the EEPROM's internal address to write to */ I2C_SendData(I2Cx_EEPROM , WriteAddr); I2CTimeout = I2CT_FLAG_TIMEOUT; /* Test on EV8 and clear it */ while(! I2C_CheckEvent(I2Cx_EEPROM , I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(7); } /* While there is data to be written */ while(NumByteToWrite--) { /* Send the current byte */ I2C_SendData(I2Cx_EEPROM , *pBuffer); /* Point to the next byte to be written */ pBuffer++; I2CTimeout = I2CT_FLAG_TIMEOUT; /* Test on EV8 and clear it */ while (!I2C_CheckEvent(I2Cx_EEPROM , I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(8); } } /* Send STOP condition */ I2C_GenerateSTOP(I2Cx_EEPROM , ENABLE); return 1; } //读缓存区里的数据,主机接受的过程 uint32_t I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead) { I2CTimeout = I2CT_LONG_TIMEOUT; //*((u8 *)0x4001080c) |=0x80; while(I2C_GetFlagStatus(I2Cx_EEPROM , I2C_FLAG_BUSY)) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(9); } /* Send START condition */ I2C_GenerateSTART(I2Cx_EEPROM , ENABLE); //*((u8 *)0x4001080c) &=~0x80; I2CTimeout = I2CT_FLAG_TIMEOUT; /* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2Cx_EEPROM , I2C_EVENT_MASTER_MODE_SELECT)) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(10); } /* Send EEPROM address for write */ I2C_Send7bitAddress(I2Cx_EEPROM , EEPROM_ADDRESS, I2C_Direction_Transmitter); I2CTimeout = I2CT_FLAG_TIMEOUT; /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2Cx_EEPROM , I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(11); } /* Clear EV6 by setting again the PE bit */ I2C_Cmd(I2Cx_EEPROM , ENABLE); /* Send the EEPROM's internal address to write to */ I2C_SendData(I2Cx_EEPROM , ReadAddr); I2CTimeout = I2CT_FLAG_TIMEOUT; /* Test on EV8 and clear it */ while(!I2C_CheckEvent(I2Cx_EEPROM , I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(12); } /* Send STRAT condition a second time */ I2C_GenerateSTART(I2Cx_EEPROM , ENABLE); I2CTimeout = I2CT_FLAG_TIMEOUT; /* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2Cx_EEPROM , I2C_EVENT_MASTER_MODE_SELECT)) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(13); } /* Send EEPROM address for read */ I2C_Send7bitAddress(I2Cx_EEPROM , EEPROM_ADDRESS, I2C_Direction_Receiver); I2CTimeout = I2CT_FLAG_TIMEOUT; /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2Cx_EEPROM , I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(14); } /* While there is data to be read */ while(NumByteToRead) { if(NumByteToRead == 1) { /* Disable Acknowledgement */ I2C_AcknowledgeConfig(I2Cx_EEPROM , DISABLE); /* Send STOP Condition */ I2C_GenerateSTOP(I2Cx_EEPROM , ENABLE); } /* Test on EV7 and clear it */ I2CTimeout = I2CT_LONG_TIMEOUT; while(I2C_CheckEvent(I2Cx_EEPROM , I2C_EVENT_MASTER_BYTE_RECEIVED)==0) { if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(3); } { /* Read a byte from the EEPROM */ *pBuffer = I2C_ReceiveData(I2Cx_EEPROM ); /* Point to the next location where the byte read will be saved */ pBuffer++; /* Decrement the read bytes counter */ NumByteToRead--; } } /* Enable Acknowledgement to be ready for another reception */ I2C_AcknowledgeConfig(I2Cx_EEPROM , ENABLE); return 1; } //此函数内容是,在发送从设备地址之后,检测EE的响应,若EE发送ACK则准备好了,可以开始下一次通讯 void I2C_EE_WaitEepromStandbyState(void) { vu16 SR1_Tmp = 0; do { /* Send START condition */ I2C_GenerateSTART(I2Cx_EEPROM , ENABLE); /* Read I2C1 SR1 register */ SR1_Tmp = I2C_ReadRegister(I2Cx_EEPROM , I2C_Register_SR1); /* Send EEPROM address for write */ I2C_Send7bitAddress(I2Cx_EEPROM , EEPROM_ADDRESS, I2C_Direction_Transmitter); }while(!(I2C_ReadRegister(I2Cx_EEPROM , I2C_Register_SR1) & 0x0002)); /* Clear AF flag */ I2C_ClearFlag(I2Cx_EEPROM , I2C_FLAG_AF); /* STOP condition */ I2C_GenerateSTOP(I2Cx_EEPROM , ENABLE); } //能定位是哪里超时了 static uint32_t I2C_TIMEOUT_UserCallback(uint8_t errorCode) { /* Block communication and all processes */ EEPROM_ERROR("I2C等待超时!errorCode = %d",errorCode); return 0; }
main.c文件如下:
#include "stm32f10x.h" #include "bsp_led.h" #include "bsp_usart.h" #include "bsp_iic_ee.h" #include <string.h> #define EEP_Firstpage 0x00 uint8_t I2c_Buf_Write[256]; uint8_t I2c_Buf_Read[256]; uint8_t I2C_Test(void); int main(void) { LED_GPIO_Config(); blue(ON); USART_Config(); printf("\r\n 这是一个IIC外设读写实验 \r\n"); I2C_EE_Init(); printf("\r\n 这是一个IIC外设读写实验 \r\n"); if(I2C_Test() ==1) { green(ON); } else { red(ON); } while (1) { } } uint8_t I2C_Test(void) { uint16_t i; printf("写入的数据\n\r"); for ( i=0; i<=255; i++ ) { I2c_Buf_Write[i] = i; printf("0x%02X ", I2c_Buf_Write[i]); if(i%16 == 15) printf("\n\r"); } //将数据写入到EEPROM I2C_EE_BufferWrite( I2c_Buf_Write, EEP_Firstpage, 256); EEPROM_INFO("\n\r写成功\n\r"); EEPROM_INFO("\n\r读出的数据\n\r"); I2C_EE_BufferRead(I2c_Buf_Read, EEP_Firstpage, 256); for (i=0; i<256; i++) { if(I2c_Buf_Read[i] != I2c_Buf_Write[i]) { EEPROM_ERROR("0x%02X ", I2c_Buf_Read[i]); EEPROM_ERROR("错误,读入数据与写入数据不一致\n\r"); return 0; } printf("0x%02X ", I2c_Buf_Read[i]); if(i%16 == 15) printf("\n\r"); } EEPROM_INFO("I2C(AT24C02)读写测试成功\n\r"); return 1; }