AnsweredAssumed Answered

STM32F030CC, I2C1 busy after I2C_TransferHandling

Question asked by karlis77 on Jan 26, 2016
Latest reply on Jan 26, 2016 by karlis77
Hello, this is my first time on this forum.
I was using STM32F030F4 for communication with battery (SMBus).
I decided to change it for STM32F030CC, but it seems like my I2C1 is not working properly any more.

I used the same pins as before, PA9 and PA10, and the same code, but it seems like I2C1 is stuck in busy state. It happens after this line :
 I2C_TransferHandling(I2C1, 0x16, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);

I will try to enter parts of my code which describes I2C1.
BTW I have tried some of the tips about this problem(reseting I2C1 after enabling it's clock, defining GPIOS after I2C1 configuration, Enabling I2C1 before I2C_Init) , but it doesn't seem to work, maybe there is some problem in my configuration.

#include <stm32f0xx_gpio.h>
#include <stm32f0xx_rcc.h>
#include <stm32f0xx_i2c.h>
#include <stm32f0xx_usart.h>
#include <stm32f0xx.h>
#include <stm32f0xx_adc.h>
#include <stm32f0xx_exti.h>
#include <stm32f0xx_syscfg.h>




#define SMBUS_CLK_PIN GPIO_Pin_9
#define SMBUS_DATA_PIN GPIO_Pin_10


unsigned char volt_A;
unsigned char volt_B;
unsigned char temp_A;
unsigned char temp_B;


void SMBUS_FLAG_BUSY()
{
    while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY))
    ;
        
}


void read_SMBus()  //  this loop is because if the battery gets disconnected while communication is happening  MCU hangs forever waiting a message
{
    while (I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == 0)
    {
        i++;
        if (i == 1000)
        {
            i = 0;
            break;
        }
    }
    i = 0;
}




void PIN_CONFIGURATION(void)
{


    GPIO_InitTypeDef GPIO_InitStructure;
    I2C_InitTypeDef I2C_InitStructure;


    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);


     RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);

    /* AF - alternate function */
    GPIO_PinAFConfig(GPIOA, SMBUS_DATA_PIN, GPIO_AF_4);
    GPIO_PinAFConfig(GPIOA, SMBUS_CLK_PIN, GPIO_AF_4);
    
        //Configure pins: SCL and SDA ------------------
    GPIO_InitStructure.GPIO_Pin = SMBUS_DATA_PIN | SMBUS_CLK_PIN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    GPIO_Init(GPIOA, &GPIO_InitStructure);




    // I2C configuration
    
    I2C_DeInit(I2C1);
    I2C_Cmd(I2C1, DISABLE);
    I2C_InitStructure.I2C_Timing = 0xB0420F13; //B0420F13 vai     moshk sitā 0x00901D23
    I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;// analogaistroksnu filtrs ieslegts
    I2C_InitStructure.I2C_DigitalFilter = 0x00; // digitalais troksnu filtrs izslegts
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; // pec tam buus siten I2c uz SMBus modi janomaina
    I2C_InitStructure.I2C_OwnAddress1 = 0x40;// kaa tiek izveeleeta adrese
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; // sitam jaabut bus ieslegtam ari tad kad SMBus buus laikam
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    


    I2C_Cmd(I2C1, ENABLE);
    I2C_Init(I2C1, &I2C_InitStructure);
    
    
    I2C_TimeoutAConfig(I2C1, 0x63);//
    I2C_TimeoutBConfig(I2C1, 0x1F); // replacable with I2C1->TIMEOUTR |= 0x801F9063; i guess
}


int main(void)
{
    PIN_CONFIGURATION();


    while(1)


        {
        
        SMBUS_FLAG_BUSY();
        
        I2C_TransferHandling(I2C1, 0x16, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
    
        // now I2C1 is busy


        I2C_SendData(I2C1, 0x9);
        I2C_ExtendedClockTimeoutCmd(I2C1, ENABLE);
        SMBUS_FLAG_BUSY();
        I2C_TransferHandling(I2C1, 0x16, 2, I2C_SoftEnd_Mode, I2C_Generate_Start_Read);
        I2C_SlaveByteControlCmd(I2C1, ENABLE);// In order to allow byte ACK control in slave reception mode, Slave Byte Control mode must be enabled by setting the SBC bit in the I2C_CR1 register. This is required to be compliant with SMBus standards.
        read_SMBus();
        volt_A = I2C_ReceiveData(I2C1);
        read_SMBus();
        volt_B = I2C_ReceiveData(I2C1);


    
        SMBUS_FLAG_BUSY();
        I2C_TransferHandling(I2C1, 0x16, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
        I2C_SendData(I2C1, 0x8);
        I2C_ExtendedClockTimeoutCmd(I2C1, ENABLE);
        SMBUS_FLAG_BUSY();
        I2C_TransferHandling(I2C1, 0x16, 2, I2C_SoftEnd_Mode, I2C_Generate_Start_Read);
        I2C_SlaveByteControlCmd(I2C1, ENABLE);// In order to allow byte ACK control in slave reception mode, Slave Byte Control mode must be enabled by setting the SBC bit in the I2C_CR1 register. This is required to be compliant with SMBus standards.
        read_SMBus();
        temp_A = I2C_ReceiveData(I2C1);
        read_SMBus();
        temp_B = I2C_ReceiveData(I2C1);    


        }
}

Outcomes