cancel
Showing results for 
Search instead for 
Did you mean: 

[i2C] [STM32F303RE] [EC25] Communication issues on i2c between two controllers

vedant
Associate

Hello,

We have been trying to read 12 bytes of data on EC25 from stm32 controller.

On the first run, data is transmitted by stm32 in slave trasmit mode and EC25 is reading the data correctly. We are able to capture the read log on EC25[Master read mode]. 

On the second iteration, when EC25 tries to read the data from stm32, it fails. We could see that the stm32 has received an address match interrupt from the master, but it fails to transmit any data. 

Both the devices have I2C configured on 400KHz. 

I2C Master Slave configuration between STM32F303RE[Slave] and Quectel EC25[Master]

Unable to figure out the root cause since it works in the first iteration but fails the second time around. Attaching the code for both the systems here. Please advice. 

EC25 Code:

#include "ql_oe.h"
#define I2C_DEV "/dev/i2c-2" //i2c-2 on EC20xx
#define I2C_SLAVE_ADDR1 0x10 
 
#define WHO_AM_I 0x00
#define rxmaxlen 11
 
int main(int argc, char *argv[])
{
    int iRet;
    printf("< I2C code for slave reception of %d bytes>", rxmaxlen);
    while (1)
    {
        int fd_i2c = Ql_I2C_Init(I2C_DEV);
        printf("< Ql_I2C_Init=%d >\n", fd_i2c);
 
        if (fd_i2c < 0)
        {
            printf("< Fail to open i2c >\n");
            return -1;
        }
 
        unsigned char rdBuff[15] = {0};
        int i = 0;
        //Read "Hello World" from stm32
        iRet = Ql_I2C_Read(fd_i2c, I2C_SLAVE_ADDR1, WHO_AM_I, rdBuff, rxmaxlen);
        for (i = 0; i <= rxmaxlen; i++)
        {
            printf("< read i2c iRet=%d, value=0x%x \n>", iRet, rdBuff[i]);
        }
        printf("\n< End of data >\n");
        sleep(1);
        Ql_I2C_Deinit(fd_i2c);
     }
     return 0;
}

stm32_main.c

#include"main.h"
 
void Init_I2C(void);
 
int main(void) {
	Init_I2C();
	while (1)
	{
	}
} 
 
void Init_I2C(void)
{	
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
	
	//I2C1 Configuration
	I2C_Cmd(I2C1, DISABLE);
 
	RCC_I2CCLKConfig((RCC_I2C1CLK_SYSCLK));					//I2C1 clock enable
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
 
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_1;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
 
	GPIO_Init(GPIOB, &GPIO_InitStruct);				//I2C1 pins configuration
 
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_4);//Alternate function I2C1
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_4);
 
	//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
 
	I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
	//I2C_InitStruct.I2C_Timing = I2C_TIMING;
	I2C_InitStruct.I2C_OwnAddress1 = I2C_ADDRESS;
	I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStruct.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
	I2C_InitStruct.I2C_DigitalFilter = 0x00;
 
	I2C_Init(I2C1, &I2C_InitStruct);
 
	I2C_GeneralCallCmd(I2C1, DISABLE);
	I2C_StretchClockCmd(I2C1, ENABLE);
	//I2C_StretchClockCmd(I2C1, DISABLE);
 
	I2C_SlaveByteControlCmd(I2C1, ENABLE);
	I2C_AcknowledgeConfig(I2C1, ENABLE);
	I2C_ExtendedClockTimeoutCmd(I2C1,ENABLE);
 
	I2C_Cmd(I2C1, ENABLE);
 
	NVIC_InitStruct.NVIC_IRQChannel = I2C1_EV_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
 
	NVIC_Init(&NVIC_InitStruct);
 
	I2C_ITConfig(I2C1, I2C_IT_ADDR, ENABLE);
}

stm32_it.c

#include"main.h"
#include "stm32f30x_it.h"
 
uint32_t DIR_Bit = 0;
uint8_t txcount = 12;
uint8_t rxcount = 1;
uint8_t RXBuff[1];
uint8_t TXBuff[]="Hello World";
 
uint8_t intr_flag=0;
uint8_t i = 0;
 
void I2C1_EV_IRQHandler(void)
{
	if (I2C_GetITStatus(I2C1, I2C_IT_ADDR) != RESET)
	{
		I2C_ClearITPendingBit(I2C1, I2C_IT_ADDR);
		//I2C_ClearFlag(I2C1, I2C_FLAG_ADDR);
 
		while (!DIR_Bit)
		{
			DIR_Bit = I2C_ReadRegister(I2C1, I2C_Register_ISR);
			DIR_Bit = (DIR_Bit >> 16) & 1;
		}
 
		/*Slave Receiver Mode*/
		if (!DIR_Bit) {
			//Slave reception mode
		}
		/*Slave Transmitter Mode*/
		else
		{
			txcount = 12;
			rxcount = 1;
 
			I2C_SlaveByteControlCmd(I2C1,DISABLE);
			I2C_AcknowledgeConfig(I2C1, ENABLE);
 
			uint8_t j = 0;
 
			while (rxcount > 0)
			{
				while (I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) != SET);
				RXBuff[j++] = I2C_ReceiveData(I2C1);
				rxcount--;
			}
 
			I2C_ClearFlag(I2C1, I2C_FLAG_ADDR);
 
			while (txcount > 0)
			{
				while(I2C_GetFlagStatus(I2C1,I2C_FLAG_TXIS)!=SET);
				I2C_SendData(I2C1, TXBuff[i++]);
				txcount--;
			}
 
			while (I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF) != SET);
			I2C_ClearFlag(I2C1, I2C_FLAG_STOPF);
 
			while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET);
			I2C_ClearFlag(I2C1, I2C_FLAG_BUSY);
		}		
	}	
}

stm32_main.h

#ifndef __MAIN_H
#define __MAIN_H
 
#include "stm32f30x.h"
#include "stm32f30x_conf.h"
 
I2C_InitTypeDef I2C_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
 
#define I2C_ADDRESS	(0x10 << 1)	//slave own address
#define I2C_OAR2_ADDR	0x01
#define I2C_TIMING   0x10C30000
 
#endif /* __MAIN_H */

Regards.

0 REPLIES 0