I2C传输协议- Master-Transmitter/Receiver Protocol

本文详细介绍了I2C总线协议中的Master-Transmitter和Master-Receiver两种模式下的7bit与10bit地址传输协议。阐述了传输过程中START、STOP、RESTART信号的作用,以及数据传输中ACK/NACK信号的产生方式。

前言

在I2C虚拟项目中,只对DUT的master模式进行了验证,由于涉及不同模式/不同地址格式的读写传输,很容易混淆,故将master模式下的不同地址下的传输协议进行梳理。


1.基本概念

在描述传输协议之前,我们需要先知道transmitter和receiver,master和slave两组概念的区别。
Transmitter:指向总线发送数据的设备,它可以是向总线发起数据传输(称之为 master-transmitter),也可以是对master发送数据到总线后的响应(slave-transmitter);
Receiver:指从总线接收数据的设备,它可以是根据自身请求从总线上接收数据(master-receiver)或者是响应其他master的请求去接收数据(slave-receiver);
Master:指发起传输(START命令)、产生时钟信号和终止传输(STOP命令)的组件;
Slave:指被master寻址的设备。
比如DW_APB_I2C(DUT)想通过I2C总线发送一组数据并通过寻址找到设备A,A给出了ACK信号表明可以接收,那么这里DUT就是master transmitter,A就是slave receiver;比如DUT想要通过I2C总线读取一组数据,并传输了对应地址,B设备给出了ACK信号,这里DUT就是master-receiver,B设备就是slave-transmitter。

2.Master-Transmitter Protocol(写操作)

8bit字节格式传输,数据传输的字节数量没有限制。在master发送完地址位和读写位

### MC32F7361 I2C Communication Code Example For the MC32F7361 microcontroller, implementing an I2C communication interface involves configuring and initializing the hardware peripherals properly to ensure reliable data transmission between master and slave devices. Below is a detailed example of how this can be achieved. #### Initialization Function The initialization function sets up the necessary parameters for I2C operation including clock speed, address mode, etc. ```c void I2C_Init(void) { // Enable GPIO and I2C peripheral clocks RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2Cx, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOx, ENABLE); // Configure SCL and SDA pins as alternate function open-drain outputs GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Pin = GPIO_PIN_SDA | GPIO_PIN_SCL; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOx, &GPIO_InitStruct); // Initialize I2C structure with desired settings I2C_InitTypeDef I2C_InitStruct; I2C_InitStruct.I2C_ClockSpeed = 100000; // Standard Mode 100kHz I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStruct.I2C_OwnAddress1 = OWN_ADDRESS; I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_Init(I2Cx, &I2C_InitStruct); // Enable I2C Peripheral I2C_Cmd(I2Cx, ENABLE); } ``` #### Write Data Function This function sends data over the I2C bus from the master device to the slave device specified by its address. ```c uint8_t I2C_WriteData(uint8_t* pData, uint16_t length, uint8_t slaveAddr) { if (!length || !pData) return ERROR; // Generate START condition I2C_GenerateSTART(I2Cx, ENABLE); while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)); // Send Slave Address for write I2C_Send7bitAddress(I2Cx, slaveAddr, I2C_Direction_Transmitter); while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); // Transmit bytes one at a time until all are sent for(int i=0;i<length;i++) { I2C_SendData(I2Cx, *(pData+i)); while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); } // Generate STOP Condition after sending last byte I2C_GenerateSTOP(I2Cx, ENABLE); return SUCCESS; } ``` #### Read Data Function To read data back from a connected sensor or another module via I2C protocol. ```c uint8_t I2C_ReadData(uint8_t* pBuffer, uint16_t length, uint8_t slaveAddr) { if (!length || !pBuffer) return ERROR; // Start condition generation followed by addressing the target node I2C_GenerateSTART(I2Cx, ENABLE); while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2Cx, slaveAddr, I2C_Direction_Transmitter); while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); // Switch direction to receive mode before issuing repeated start I2C_RepeatedStart(I2Cx); I2C_Send7bitAddress(I2Cx, slaveAddr, I2C_Direction_Receiver); while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); // Receive multiple bytes into buffer depending on requested count for(int i=0 ;i<(int)(length-1);i++) { *pBuffer++ = I2C_ReceiveData(I2Cx); while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)); } // Last byte should not acknowledge receipt so that sender knows end has been reached I2C_AcknowledgeConfig(I2Cx, DISABLE); *pBuffer = I2C_ReceiveData(I2Cx); while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)); // Re-enable acknowledgment and generate stop signal upon completion I2C_AcknowledgeConfig(I2Cx, ENABLE); I2C_GenerateSTOP(I2Cx, ENABLE); return SUCCESS; } ``` These functions provide basic building blocks for establishing robust two-way communications using the I2C standard on the MC32F7361 platform[^2].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ann_xia66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值