2018-12-09 09:40 PM
I am using STM32L4R5ZI as a I2C slave. From I2C master I am getting first command and On my slave device I am sending the response but the slave code hangs there(and it makes my master device to reboot automatically).
Please provide the necessary inputs.
2018-12-11 09:37 PM
Can you please reply to this?
2018-12-13 01:49 AM
Hi @Shree ,
To have more chance to get an answer, you need to provide more details:
-Amel
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2018-12-13 04:22 AM
Hello Amel,
Thanks for your reply.
I have just modified the I2C_TwoBoards_RestartAdvComIT example from STM32Cube_FW_L4_V1.13.0
to receive and send the data continuously(as a I2C slave). I am using KEIL IDE to compile the code.
In my case master(LPC2138) is continuously sending the commands but on mys slave (STM32L4R5ZI)
side I am receiving only the first command and then transmitting the response . Then my my slave code hangs before receiving next command(if I keep on pressing RESET button then it receives the next command for once).
In below code the control never goes to HAL_I2C_SlaveTxCpltCallback() function.
Can you please guide me that how can I receive the command continuously and then transmit the response to master.
The code is as follow:
int main(void)
{
#if defined(__GNUC__) && defined(MASTER_BOARD)
initialise_monitor_handles(); /*rtt*/
#endif
/* STM32L4xx HAL library initialization:
- Configure the Flash prefetch
- Systick timer is configured by default as source of time base, but user
can eventually implement his proper time base source (a general purpose
timer for example or other time source), keeping in mind that Time base
duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
handled in milliseconds basis.
- Set NVIC Group Priority to 4
- Low Level Initialization
*/
HAL_Init();
/* Configure the system clock to 120 MHz */
SystemClock_Config();
/* Configure LED1 */
BSP_LED_Init(LED1);
BSP_LED_Init(LED2);
BSP_LED_Init(LED3);
/*##-1- Configure the I2C peripheral ######################################*/
I2cHandle.Instance = I2Cx;
I2cHandle.Init.Timing = I2C_TIMING;
I2cHandle.Init.OwnAddress1 = I2C_ADDRESS;
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
I2cHandle.Init.OwnAddress2 = 0xFF;
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if(HAL_I2C_Init(&I2cHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/* Enable the Analog I2C Filter */
//HAL_I2CEx_ConfigAnalogFilter(&I2cHandle,I2C_ANALOGFILTER_ENABLE);
/* Infinite loop */
while (1)
{
/*##-2- Put I2C peripheral in Listen address match code process ##########*/
/* This action will allow I2C periphal to able to treat Master request when
necessary depending of transfer direction requested by Master */
if(HAL_I2C_EnableListen_IT(&I2cHandle) != HAL_OK)
{
/* Transfer error in reception process */
Error_Handler();
}
/*##-3- Wait for a new frame communication with a Master #################*/
/* Before starting a transfer, you need to wait a Master request event.
For simplicity reasons, this example is just waiting till an Address callback event,
but application may perform other tasks while transfer operation is ongoing. */
while(uwTransferInitiated != 1)
{
}
/*##-4- Wait for the end of the frame communication ######################*/
/* Before ending a transfer, you need to wait a Master end event.
For simplicity reasons, this example is just waiting till an Stop condition event,
but application may perform other tasks while transfer operation is ongoing. */
while(uwTransferEnded != 1)
{
}
process_data(aSlaveReceiveBuffer);
/* For User help, keep Leds status until timeout */
HAL_Delay(LED_STATUS_TIMEOUT);
/*##-5- Clear, reset process variables, arrays and Leds status ###########*/
FlushBuffer8(aSlaveReceiveBuffer, COUNTOF(aSlaveReceiveBuffer));
uwTransferInitiated = 0;
uwTransferEnded = 0;
ubSlaveReceiveIndex = 0;
ubSlaveInfoIndex = 0xFF;
BSP_LED_Off(LED1);
BSP_LED_Off(LED2);
#endif /* MASTER_BOARD */
}
}
/**
* @brief System Clock Configuration
* The system Clock is configured as follows :
* System Clock source = PLL (MSI)
* SYSCLK(Hz) = 120000000
* HCLK(Hz) = 120000000
* AHB Prescaler = 1
* APB1 Prescaler = 1
* APB2 Prescaler = 1
* MSI Frequency(Hz) = 4000000
* PLL_M = 1
* PLL_N = 60
* PLL_Q = 2
* PLL_R = 2
* PLL_P = 7
* Flash Latency(WS) = 5
* @param None
* @retval None
*/
void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
/* Enable voltage range 1 boost mode for frequency above 80 Mhz */
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
__HAL_RCC_PWR_CLK_DISABLE();
/* Enable MSI Oscillator and activate PLL with MSI as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 60;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLP = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
/* Initialization Error */
while(1);
}
/* To avoid undershoot due to maximum frequency, select PLL as system clock source */
/* with AHB prescaler divider 2 as first step */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
{
/* Initialization Error */
while(1);
}
/* AHB prescaler divider at 1 as second step */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
/* Initialization Error */
while(1);
}
}
/**
* @brief Tx Transfer completed callback.
* @param I2cHandle: I2C handle
* @note This example shows a simple way to report end of IT Tx transfer, and
* you can add your own implementation.
* @retval None
*/
void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *I2cHandle)
{
/* Turn LED1 off: Transfer in transmission process is correct */
printf("I am from I2C_SlaveTxCpltCallback\n");
pSlaveTransmitBuffer = 0;
FlushBuffer8(response, COUNTOF(response));
BSP_LED_On(LED2);
}
/**
* @brief Rx Transfer completed callback.
* @param I2cHandle: I2C handle
* @note This example shows a simple way to report end of IT Rx transfer, and
* you can add your own implementation.
* @retval None
*/
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *I2cHandle)
{
//uint8_t i = 0;
/* Turn LED1 on: Transfer in reception process is correct */
BSP_LED_On(LED1);
if(HAL_I2C_Slave_Sequential_Receive_IT(I2cHandle, &aSlaveReceiveBuffer[ubSlaveReceiveIndex], 1, I2C_FIRST_FRAME) != HAL_OK)
{
Error_Handler();
}
ubSlaveReceiveIndex++;
}
#ifndef MASTER_BOARD
/**
* @brief Slave Address Match callback.
* @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
* @retval None
*/
void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
{
uint8_t i = 0;
if(AddrMatchCode == I2C_ADDRESS)
{
printf("Received I2C Write request\n");
uwTransferInitiated = 1;
uwTransferDirection = TransferDirection;
/* First of all, check the transfer direction to call the correct Slave Interface */
if(uwTransferDirection == I2C_DIRECTION_TRANSMIT)
{
if(HAL_I2C_Slave_Sequential_Receive_IT(&I2cHandle, &aSlaveReceiveBuffer[ubSlaveReceiveIndex], 1, I2C_FIRST_FRAME) != HAL_OK)
{
Error_Handler();
}
ubSlaveReceiveIndex++;
}
else
{
printf("Received the Read Request\n");
pSlaveTransmitBuffer = response;
ubSlaveNbDataToTransmit = sizeof(response);
if(HAL_I2C_Slave_Sequential_Transmit_IT(&I2cHandle, pSlaveTransmitBuffer, ubSlaveNbDataToTransmit, I2C_LAST_FRAME) != HAL_OK)
{
Error_Handler();
}
printf("Transmitted: ");
for(i=0;i<pSlaveTransmitBuffer[3];i++)
{
printf("0X%x ",pSlaveTransmitBuffer[i]);
}
printf("\n");
}
}
else
{
/* Call Error Handler, Wrong Address Match Code */
Error_Handler();
}
}
2018-12-17 06:04 AM
Can you please reply to this ?
2018-12-17 06:10 AM
> Can you please reply to this ?
This forum is mostly populated by volunteers, so post like this are not at all helpful.
Use a scope to trace the communication on the I2C bus. Do you see an ACK from the slave ?
2018-12-17 06:21 AM
Yes, I tried connecting the Scope.
"Do you see an ACK from the slave ?"
There was no ACK from slave.
2018-12-24 03:00 AM
Hello @Shree ,
Is there any error flag set when slave hangs?
Try to review the configuration of TIMINGR register compared to your system settings and requirement.
-Amel
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.