cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_I2C_Master_Transmit_IT error in the Hal lib?

Electronic1
Associate II

I am using the HAL library generated with the STM32CUbeIDE for controlling a I2C sensor.

With the demoboard nucleo stm32l031k6

I am using the HAL interrupt master transmit function

but the byte’s send aren't the ones that I attach.

While debugging I see that at some point the data

added to the buffer hi2c1 -> pBuffPtr * is overwritten by another one different from the one I

programmed and the overwritten values are the one shown in the output.

0x20001FBC

HAL_I2C_Master_Transmit_IT(&hi2c1, (uint16_t)I2C_ADR, (uint8_t*)aTxBuffer, 2)

I suspect some Interrupt effect modifying the values but

I can’t find it. Anyone could confirm?

2 REPLIES 2
wiedemfl
Associate

I am having the same problem. Did you find any solution?

That post was over 4 years ago. I'm sure he figured it out by now. 

 

In the OP's code, he has the aTxBuffer array as local. So as soon as it leaves the function the variable is deallocated and destroyed. The array needs to be declared global or static.

 

etError SF05_WriteCommand(etCommands cmd){ //2 bytes always
//==============================================================================
  etError error = NO_ERROR; // error code

/*##-2- Start the transmission process #####################################*/
/* While the I2C in reception process, user can transmit data through "aTxBuffer" buffer */
/* Timeout is set to 10S */

  uint8_t aTxBuffer[2];

  	aTxBuffer[0] = 0xFF; //prova	(cmd & 0xFF00)>>8 ;
  	aTxBuffer[1] = 0xFF; //prova    cmd & 0x00FF ;

  	if(HAL_I2C_Master_Transmit_IT(&hi2c1, (uint16_t)I2C_ADR, (uint8_t*)aTxBuffer, 2)!= HAL_OK)
  	{ // Error_Handler(); /* Error_Handler() function is called when error occurs. */
  		error=ACK_ERROR;
  	}

  	if(error == NO_ERROR)
  	{
  		currentCommand = cmd;
  	}

  return error;
}