2020-06-30 02:45 AM
Hi,
I have a issue with I2C Slave on an STM32F030K6.
Project is generated with :
The STM32 is configured as an I2C Slave ans answers to an other STM32F0 HAL_I2C_Mem_Read() request.
I2C is configured in IT mode, and use :
When HAL_I2C_Slave_Seq_Transmit_IT() is called with data equal to 0x00 the MCU reset (Else it works well).
In this case, it works well
void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
{
if(AddrMatchCode == 4)
{
switch(TransferDirection) {
case I2C_DIRECTION_TRANSMIT :
if(HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, &aRxBuffer[0], RXBUFFERSIZE, I2C_FIRST_FRAME) != HAL_OK)
{
Error_Handler();
}
break;
case I2C_DIRECTION_RECEIVE :
{
//Fill response with appropriate value
//------------------------------------
aTxBuffer[0] = 0xFF;
aTxBuffer[1] = 0x0;
aTxBuffer[2] = 0x0;
//Set to transmit
if(HAL_I2C_Slave_Seq_Transmit_IT(&hi2c1, &aTxBuffer[0], TXBUFFERSIZE, I2C_FIRST_FRAME) != HAL_OK)
{
Error_Handler();
}
}
break;
default :
break;
}
}
}
In this case, MCU reset
void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
{
if(AddrMatchCode == 4)
{
switch(TransferDirection) {
case I2C_DIRECTION_TRANSMIT :
if(HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, &aRxBuffer[0], RXBUFFERSIZE, I2C_FIRST_FRAME) != HAL_OK)
{
Error_Handler();
}
break;
case I2C_DIRECTION_RECEIVE :
{
//Fill response with appropriate value
//------------------------------------
aTxBuffer[0] = 0x0;
aTxBuffer[1] = 0x0;
aTxBuffer[2] = 0x0;
//Set to transmit
if(HAL_I2C_Slave_Seq_Transmit_IT(&hi2c1, &aTxBuffer[0], TXBUFFERSIZE, I2C_FIRST_FRAME) != HAL_OK)
{
Error_Handler();
}
}
break;
default :
break;
}
}
}
Note : I didn't see related issue in Errata.
Code attached (main.c & I2C.c)
Any idea will be appreciate ;)
2020-06-30 06:28 AM
Perhaps aTxBuffer is not defined properly and is writing somewhere it shouldn't.
2020-06-30 07:14 AM
Thanks for your answer.
Yes first I suspected that, but aTxBuffer is declared as a global : uint8_t aTxBuffer[TXBUFFERSIZE];
I have tried to use TXBUFFERSIZE bigger than necessary, but it doesn't solve the bug.
2020-07-08 01:46 PM
Any idea ?
2020-07-08 07:03 PM
It's unlikely the chip is resetting as a direct cause of a byte in a buffer being 0. There just isn't a mechanism for that. The problem is probably somewhere in the code you're not showing up. Perhaps there is some logic in your code based on that value.
Instrument your startup to show the reason for the reset in the RCC_CSR register.
2020-07-14 10:10 AM
Hi,
Thanks for you reply, I kindly appreciate.
Yes I also suspect something wrong somewhere in my code causing the reset. That is why I have generated a new project with CubeMX and simply add I2C routine. ( Nothing in the while() main loop , no other interrupt...)
in stm32f0xx_it.c, simply add : I2C irq handler (HAL)
/******************************************************************************/
/* STM32F0xx Peripheral Interrupt Handlers */
/* Add here the Interrupt Handlers for the used peripherals. */
/* For the available peripheral interrupt handler names, */
/* please refer to the startup file (startup_stm32f0xx.s). */
/******************************************************************************/
/**
* @brief This function handles I2C1 global interrupt.
*/
void I2C1_IRQHandler(void)
{
/* USER CODE BEGIN I2C1_IRQn 0 */
/* USER CODE END I2C1_IRQn 0 */
if (hi2c1.Instance->ISR & (I2C_FLAG_BERR | I2C_FLAG_ARLO | I2C_FLAG_OVR)) {
HAL_I2C_ER_IRQHandler(&hi2c1);
} else {
HAL_I2C_EV_IRQHandler(&hi2c1);
}
/* USER CODE BEGIN I2C1_IRQn 1 */
/* USER CODE END I2C1_IRQn 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
main.c :
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "gpio.h"
#include <string.h>
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define RXBUFFERSIZE 3
#define TXBUFFERSIZE 3
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint16_t Rxcounter = 0;
uint16_t Txcounter = 0;
uint8_t aTxBuffer[TXBUFFERSIZE];
uint8_t aRxBuffer[RXBUFFERSIZE];
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/********************************************************************
* Function: HAL_I2C_AddrCallback
*
* PreCondition: None
*
* param : hi2c Pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for the specified I2C.
* param : TransferDirection: Master request Transfer Direction (Write/Read), value of @ref I2C_XferOptions_definition
* param : AddrMatchCode: Address Match Code
*
* Side Effects: None
*
* Overview: Slave Address Match callback.
* Note:
*******************************************************************/
void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
{
if(AddrMatchCode == 4)
{
switch(TransferDirection) {
case I2C_DIRECTION_TRANSMIT :
if(HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, &aRxBuffer[0], RXBUFFERSIZE, I2C_FIRST_FRAME) != HAL_OK)
{
Error_Handler();
}
break;
case I2C_DIRECTION_RECEIVE :
{
//Fill response with appropriate value
//------------------------------------
aTxBuffer[0] = 0x0;
aTxBuffer[1] = 0x0;
aTxBuffer[2] = 0x0;
//Set to transmit
if(HAL_I2C_Slave_Seq_Transmit_IT(&hi2c1, &aTxBuffer[0], TXBUFFERSIZE, I2C_FIRST_FRAME) != HAL_OK)
{
Error_Handler();
}
}
break;
default :
break;
}
}
}
/********************************************************************************
* Description : HAL_I2C_SlaveRxCpltCallback
*
*
* Param?tres :
*
*
* Retour :
*
********************************************************************************/
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
memset( aRxBuffer, 0, RXBUFFERSIZE);
Rxcounter++;
}
/********************************************************************************
* Description : HAL_I2C_SlaveTxCpltCallback
*
*
* Param?tres :
*
*
* Retour :
*
********************************************************************************/
void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
{
aRxBuffer[0] = 0; //Remove previous sub-address
Txcounter++;
}
/********************************************************************
* Function: HAL_I2C_ListenCpltCallback
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: end of transfert
* Note:
*******************************************************************/
void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
{
HAL_I2C_EnableListen_IT(hi2c); // Restart
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
HAL_I2C_EnableListen_IT(&hi2c1);
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
LL_FLASH_SetLatency(LL_FLASH_LATENCY_1);
if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_1)
{
Error_Handler();
}
LL_RCC_HSI_Enable();
/* Wait till HSI is ready */
while(LL_RCC_HSI_IsReady() != 1)
{
}
LL_RCC_HSI_SetCalibTrimming(16);
LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI_DIV_2, LL_RCC_PLL_MUL_12);
LL_RCC_PLL_Enable();
/* Wait till PLL is ready */
while(LL_RCC_PLL_IsReady() != 1)
{
}
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
/* Wait till System clock is ready */
while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
{
}
LL_SetSystemCoreClock(48000000);
/* Update the time base */
if (HAL_InitTick (TICK_INT_PRIORITY) != HAL_OK)
{
Error_Handler();
};
LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_HSI);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Yes, I will save RCC_CSR register after reboot
2020-07-14 10:39 AM
When firmware works (With aTxBuffer[0] = 0xFF) : RCC_CSR = 0x1C80 0000
When firmware reset (With aTxBuffer[0] = 0x00) : RCC_CSR = 0x0C80 0000
2020-07-14 03:29 PM
I can't see any reason why your code would reset based on a value in aTxBuffer.
If I really wanted to look at things, I would probe the SCL/SDA lines along with NRST to see when the reset actually occurs. Maybe this would provide more info?
You should instrument Error_Handler so that it doesn't just silently return. You have no mechanism to actually detect HAL errors. But unlikely this is the problem.