AnsweredAssumed Answered

CPU halts on slave I2C bus shut down

Question asked by Mohajer on Aug 9, 2015
Latest reply on Aug 9, 2015 by Mohajer

Hi,

I am using master I2C on STM32F407 using standard peripheral library.

I have one slave module connected to the I2C bus.

In some situations slave module Shuts down and CPU halts at highlighted line:

 

ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)
{
  uint32_t lastevent = 0;
  uint32_t flag1 = 0, flag2 = 0;
  ErrorStatus status = ERROR;
 
  /* Check the parameters */
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  assert_param(IS_I2C_EVENT(I2C_EVENT)); sticks here!
 
// ====================================================================
 
void I2C_Config(void){
    
    GPIO_InitTypeDef GPIO_InitStruct;
    I2C_InitTypeDef I2C_InitStruct;
    
    // enable APB1 peripheral clock for I2C2
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
    // enable clock for SCL and SDA pins
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    
    /* setup SCL and SDA pins
    * 1. SCL on PB10 and SDA on PB11
    */
 
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; // we are going to use PB10 and PB11
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // set pins to alternate function
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // set GPIO speed
    GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; // set output to open drain --> the line has to be only pulled low, not driven high
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // enable pull up resistors
    GPIO_Init(GPIOB, &GPIO_InitStruct); // init GPIOB
    
            // Connect I2C2 pins to AF
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2); // SCL
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2); // SDA
    
    I2C_DeInit(I2C2);
    
    // configure I2C2
    I2C_InitStruct.I2C_ClockSpeed = 400000; // 100kHz
    I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // I2C mode
    I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard
    I2C_InitStruct.I2C_OwnAddress1 = 0x00; // own address, not relevant in master mode
    I2C_InitStruct.I2C_Ack = I2C_Ack_Disable; // disable acknowledge when reading (can be changed later on)
    I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses
    
    I2C_Init(I2C2, &I2C_InitStruct); // init I2C2
    // enable I2C2
    I2C_Cmd(I2C2, ENABLE);
    
}
 

// --------------------------------------------------------------------------------

 

void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction){
    
    
    // wait until I2C1 is not busy anymore
    while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
    
    // Send I2C1 START condition
    I2C_GenerateSTART(I2Cx, ENABLE);
    
    // wait for I2C1 EV5 --> Slave has acknowledged start condition
    while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
    
    // Send slave Address for write
    I2C_Send7bitAddress(I2Cx, address, direction);
    
    /* wait for I2C1 EV6, check if
    * either Slave has acknowledged Master transmitter or
    * Master receiver mode, depending on the transmission
    * direction
    */
    if(direction == I2C_Direction_Transmitter){
    while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    }
    
    else if(direction == I2C_Direction_Receiver){
    while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
    }
 
}
 
 
 
 
/* This function transmits one byte to the slave device
* Parameters:
* I2Cx --> the I2C peripheral e.g. I2C1
* data --> the data byte to be transmitted
*/
void I2C_write(I2C_TypeDef* I2Cx, uint8_t data){
    
    I2C_SendData(I2Cx, data);
    
    // wait for I2C1 EV8_2 --> byte has been transmitted
    while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    
}
 
 
/* This function reads one byte from the slave device
* and acknowledges the byte (requests another byte)
*/
uint8_t I2C_read_ack(I2C_TypeDef* I2Cx){
    
    uint8_t data = 0;
    // enable acknowledge of recieved data
    I2C_AcknowledgeConfig(I2Cx, ENABLE);
    // wait until one byte has been received
    while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
    // read data from I2C data register and return data byte
    data = I2C_ReceiveData(I2Cx);
    return data;
    
}
/* This function reads one byte from the slave device
* and doesn't acknowledge the recieved data
*/
uint8_t I2C_read_nack(I2C_TypeDef* I2Cx){
    
    uint8_t data = 0;
    
    // disabe acknowledge of received data
    // nack also generates stop condition after last byte received
    // see reference manual for more info
    
    I2C_AcknowledgeConfig(I2Cx, DISABLE);
    I2C_GenerateSTOP(I2Cx, ENABLE);
    // wait until one byte has been received
    while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
    // read data from I2C data register and return data byte
    data = I2C_ReceiveData(I2Cx);
    return data;
    
}
/* This funtion issues a stop condition and therefore
* releases the bus
*/
void I2C_stop(I2C_TypeDef* I2Cx){
    
    // Send I2C1 STOP Condition
    I2C_GenerateSTOP(I2Cx, ENABLE);
    
}

 

Outcomes