SPI and I 2 C 1
Serial Peripheral Interface (SPI) SPI allow half/full duplex, synchronous, serial communication with external devices The interface can be configured as master for MCU board Communication clock (SCLK) is also provided for synchronous communication 2
SPI features Full duplex synchronous transfer on 3 lines Simplex synchronous transfer on 2 lines Master or slave operation Multi-master mode support 3
SPI block diagram 4
Basic SPI configuration Usually, SPI is connected using 4 pins MISO (Master In/Slave Out): transmit data in slave mode, and received data in master mode MOSI (Master Out/Slave In): transmit data in master mode, and received data in slave mode SCLK: Clock NSS (slave select): to select a slave device 5
Single Master/Single Slave config 6
Configuring SPI in Master mode Select BR[2:0] to define baud rate Select CPOL and CPHA to define data transfer and serial clock relationship Set DFF bit to define 8 or 16 bit data transfer Configure LSBFirst bit in SPI_CR1 to define frame format Connect NSS if required MSTR and SPE must be set 7
Configuring SPI in Slave mode Select CPOL and CPHA to define data transfer and serial clock relationship Set DFF bit to define 8 or 16 bit data transfer LSBFirst bit in SPI_CR1 must be the same as Master Connect NSS if required Clear MSTR and set SPE bit 8
Timing diagram 9
Control register TXE bit is set on the transfer data from Tx buffer to the shift register RXNE bit is set on the data transfer from shift register to Rxbuffer 10
I2C Inter-Integrated bus (I2C) Interface between microcontroller and I2C bus Provide multi-master support 11
I2C feature Master master configuration I2C master feature Clock generation Start and Stop generation I2C slave feature programmable I2C address detection dual address capabilities Stop bit detection 2 interrupt vectors 12
I2C function There are 2 pins, data pin (SDA) and clock (SCL) It can be connected with standard speed (up to 100KHz) or fast mode (up to 400KHz) 13
I2C function description Mode selection: Slave transmit Slave receiver Master transmit Master receiver By default, it operates in Slave mode. It automatic switch once it receive a START condition from master to slave 14
Communication flow In master mode, I2C interface initiates the data transfer and the clock signal. A serial data transfer always begins with a start condition and ends with a stop condition In slave mode, the interface is capable of recognizing its own address (7-10 bits) Data are transmitted as 8 bit bytes, MSB first. A 9 th clock pulse, receiver must send an acknowledge 15
I2C data transfer 16
I2C block diagram 17
Mode selection By default, the device operates in slave mode. It will automatically switch from slave to master, after the device generate START condition from master to slave. 18
I2c master mode Program the peripheral input clock in I2C_CR2 register to generate correct timing Configure the clock control register Configure the rise time register Program I2C_CR1 register to enable the peripheral Set the start bit in I2C_CR1 to generate a Start condition 19
Transfer sequence 20
I2C reading data 1. Send start sequence 2. Send address of the device (with read bit) 3. Wait for response 4. Send address register of the device 5. Wait for response 6. Send start sequence again 7. Send address of the device (with read bit) 8. Wait for response 9. Wait for data (high byte first) 21 10.Wait for data (lower byte)
I2C writing data 1. Send start sequence 2. Send address of the device (with write bit) 3. Wait for response 4. Send address register of the device 5. Wait for response 6. Write high byte 7. Write lower byte 22
Example: reading data from compass 1. Send start sequence 2. Send address 3. Send start sequence again 4. Send address again 5. Read data 6. Send stop sequence 23
Example int main(){ } RCC_setup(); GPIO_setup(); I2C_setup(); while(1){ } if(i2c_lm75_status()==success){ } temp = LM75_readTemp(); void GPIO_setup(){ GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB RCC_APB2Periph_AFIO,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); } 24
void I2C_setup(){ I2C_InitTypeDef I2C_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE); I2C_DeInit(I2C1); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x00; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgeAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 200000; I2C_Init(I2C1, &I2C_InitStructure); I2C_Cmd(I2C1, ENALBE); // enable I2C1 } 25
ErrorStatus I2C_LM75_Status(){ unsigned int I2C_TimeOut = 100000; I2C_ClearFlag(I2C1, I2C_FLAG_AF); I2C_AcknowledgeConfig(I2C1, ENABLE); I2C_GenrateSTART(I2C1, ENABLE); while(!i2c_checkevent(i2c1, I2C_EVENT_MASTER_MODE_SELECT); // wait for start I2C_Send7BitAddress(I2C1, LM75_ADDR, I2C_Direction_Transmitter); //send slave addr while(!i2c_checkevent(i2c1,i2c_event_master_transmitter_mode_selecte D)) && I2C_TimeOut){ // get status ready } } I2C_TimeOut--; if(i2c_getflagstatus(i2c1,i2c_flag_af!= 0x0){ return ERROR; }else return SUCCESS; unsigned int LM75_readTemp(){ unsigned int RegValue = 0; I2C_AcknowledgeConfig(I2C1, ENABLE); I2C_GenerateStart(I2C1, ENABLE); // wait for START to complete while(i2c_checkevent(i2c1, I2CEVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1, LM75_ADDR, I2C_Direction_Transmitter); 26
// wait for data transfer complete while(i2c_checkevent(i2c1, I2CEVENT_MASTER_RECEIVER_MODE_SELECTED)); // send data pointer I2C_SendData(I2C1,LM75_TEMP_REG); // wait for data transfer complete while(i2c_checkevent(i2c1, I2CEVENT_MASTER_BYTE_TRANSMITTED)); I2C_GenerateStart(I2C1, ENABLE); // wait for START to complete while(i2c_checkevent(i2c1, I2CEVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1, LM75_ADDR, I2C_Direction_Receiver); while(i2c_checkevent(i2c1, I2CEVENT_MASTER_RECEIVER_MODE_SELECTED)); while(i2c_checkevent(i2c1, I2CEVENT_MASTER_BYTE_RECEIVED)); // store data RegValue = I2C_ReceiveData(I2c1) << 8; // disable acknowledgement I2CAcknowledgeConfig(I2C1, DISABLE); // send I2C1 stop condition I2c_GenerateStop(I2C1, ENABLE); // test on EV7 and clear while(i2c_checkevent(i2c1, I2CEVENT_MASTER_BYTE_RECEIVED)); Regvalue!= I2C_ReceiveData(I2C1); return RegValue >> 7; 27 }
Simulink SPI Master Setup 28
Configuration 29
SPI Read/Write 30
Configuration Data Read/Write = the number of port 31
Clock polarity and clock phase Clock polarity low and clock phase 1 st edge (CPOL_Low/CPHA_1Edge) 32
Clock polarity and clock phase Clock polarity low and clock phase 2nd edge (CPOL_Low/CPHA_2Edge) 33
Clock polarity and clock phase Clock polarity high and clock phase 1 st edge (CPOL_High/CPHA_1Edge) 34
Clock polarity and clock phase Clock polarity high and clock phase 2nd edge (CPOL_High/CPHA_2Edge) 35
SPI Example 36
SPI Example 37
SPI Subsystem 38
Hardware 39
Test with Matlab Host 40
Output 41
I2C 42
Configuration 43
I2C Read/Write 44
I2C Configuration 45
I2C Sensor L3G4200D 46
Hardware 47
Matlab Host 48
Output 49
Humidity and Temperature Sensor 50
Hardware 51
Output 52
Questions? 53