cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 I2C not receiving further bytes

HGhaf.1
Associate

Hello

I am beginner to MCU, I have set STM32F407 Discovery as a master on I2C communication with nucleo STM32F446RE as a slave. PC host connected to the STM32F407 via UART. The code for the master is

char RecBuff [20];
while (1)
  {
	  HAL_UART_Receive(&huart2, command, 2, HAL_MAX_DELAY);
 
	  	  if(command[0] == 1)
	  	  {
	  	  HAL_UART_Receive(&huart2, (uint8_t*)RecBuff , command[1], HAL_MAX_DELAY);	  	 
	  	  }	  	 
	  	uint8_t LL = strlen(RecBuff);	  	
	  	HAL_I2C_Master_Transmit(&hi2c3, Motor1,&LL, 1, HAL_MAX_DELAY);
	  	HAL_I2C_Master_Transmit(&hi2c3, Motor1, (uint8_t*)RecBuff,LL, HAL_MAX_DELAY);
	  	HAL_I2C_Master_Transmit(&hi2c3, Motor1, &d1,  1, HAL_MAX_DELAY);
}

RecBuff holding a value of motor rotation comes from the host for example 5000. d1 is the direction

the code for the Nucleo is

char rotval_s[10];
uint32_t rotval_d;
while (1)
  {
  uint8_t MSG[50] = {'\0'};	  	  
HAL_I2C_Slave_Receive_IT(&hi2c3, &LL, 1);
HAL_I2C_Slave_Receive_IT(&hi2c3, (uint8_t*)rotval_s, LL);
HAL_I2C_Slave_Receive_IT(&hi2c3, &direction, 1);
rotval_d = atoi(rotval_s);	  	  	
sprintf(MSG2, "%lu\n\r", echoval.motval);
	
  if(direction == 0)
 {
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, 1);
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, 0);
  }
  if(direction == 1)
 {
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, 0);
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, 1);
  }
  if(direction == 2)
  {
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, 0);
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, 0);
  }
 
  if(HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_1))
  {
   val = ((TIM2->CNT)>>2);
  if(val >= rotval_d)
   {
  direction = 2;	  	  	  	  	  			  
  }
 }else
 {
   val = ((TIM2->CNT)>>2);
  if(val >= rotval_d)
 {
  direction = 2;	  	  	  	  	  		  
  }
 }
  sprintf(MSG, "%lu\n\r", val);
  HAL_UART_Transmit(&huart2, MSG, sizeof(MSG), HAL_MAX_DELAY);	  	  	  	  	  	  
  }

The non blocking code in Nucleo is used to enable MCU to send the current value of the motor encoder to COM port

the problem is when master send the first byte, which is the length of the next word, Nucleo acknowledge receive but then it does not acknowledge of the next coming byte as shown in Picture from logic analyzer

0693W000005BNhPQAW.jpgThank you for any suggestions

Hazim

1 REPLY 1
Piranha
Chief II

Non-blocking means what it says - the functions (*_IT ones) don't block/wait but return immediately.

Also I suggest parsing text commands at master and using binary commands on I2C.