cancel
Showing results for 
Search instead for 
Did you mean: 

hal_delay() not working

PPopo.1
Senior

So I've created a project and am trying to use the HAL_delay(10); but it gets stuck at this function when im trying to debug.

I have been changing the clock and enabling the TIM2 so I don't know if i've messed something up. Here are the settings that I have:


_legacyfs_online_stmicro_images_0693W00000biAmBQAU.png
_legacyfs_online_stmicro_images_0693W00000biAmGQAU.png
_legacyfs_online_stmicro_images_0693W00000biAmVQAU.png
_legacyfs_online_stmicro_images_0693W00000biAmaQAE.png
_legacyfs_online_stmicro_images_0693W00000biAmfQAE.png 

And that is about it

53 REPLIES 53

I have that

PPopo.1
Senior
int main(void)
{
  /* USER CODE BEGIN 1 */
	uint8_t buf[256];
  /* 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();
  MX_SPI1_Init();
  MX_TIM2_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start(&htim2);
 
  //HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(0x77<<1), 10, 100);
//HAL_Delay(500);
     // read
//  HAL_Delay(1000);
  uint8_t koefS1_0 [2]={0}, koefS1_1 [2]={0},koefS1_2 [2]={0},koefS1_3 [2]={0},koefS1_4 [2]={0};
  uint8_t koefS2_0 [2]={0}, koefS2_1 [2]={0},koefS2_2 [2]={0},koefS2_3 [2]={0},koefS2_4 [2]={0};
  uint8_t koefI2C_0 [2]={0}, koefI2C_1 [2]={0},koefI2C_2 [2]={0},koefI2C_3 [2]={0},koefI2C_4 [2]={0};
 
  uint8_t dataS1[3];
  uint8_t dataS2[3];
  uint8_t dataI2C[3];
 
  dataS1[0] = 255,dataS1[1] = 255, dataS1[2] = 255;
  dataS2[0] = 255,dataS2[1] = 255, dataS2[2] = 255;
  dataI2C[0] = 255,dataI2C[1] = 255, dataI2C[2] = 255;
  HAL_StatusTypeDef ret;
  uint8_t command;
 
  //SPI
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET);
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
  //delay(1);
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET);
  command = 0x1E;
  ret = HAL_I2C_Master_Transmit (&hi2c1, (uint16_t)(0x77<<1), &command, 1, 100);
 
  // Enable write enable latch (allow write operations)
  command = 0x48;
  //delay(1);
  HAL_SPI_Transmit(&hspi1, &command, 1, 100);
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET);
 
  //READ SPI
  command = 0x00;
  //delay(1);
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET);
  ret = HAL_SPI_Transmit(&hspi1,&command, 1, 100);
  delay(1);
  ret = HAL_SPI_Receive(&hspi1, dataS1, 3, 100);
  //delay(1);
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET);
 
command = 0xA2;
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET);
ret = HAL_SPI_Transmit(&hspi1,&command, 1, 100);
//delay(1);
ret = HAL_SPI_Receive(&hspi1, koefS1_4, 2, 100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET);
//delay(10);
command = 0xA4;
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET);
ret = HAL_SPI_Transmit(&hspi1,&command, 1, 100);
delay(1);
ret = HAL_SPI_Receive(&hspi1, koefS1_3, 2, 100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET);
//delay(10);
 
command = 0xA6;
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET);
ret = HAL_SPI_Transmit(&hspi1,&command, 1, 100);
//delay(10);
ret = HAL_SPI_Receive(&hspi1, koefS1_2, 2, 100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET);
//delay(10);
command = 0xA8;
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET);
ret = HAL_SPI_Transmit(&hspi1,&command, 1, 100);
delay(1);
ret = HAL_SPI_Receive(&hspi1, koefS1_1, 2, 100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET);
//delay(10);
__HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_UPDATE);
command = 0xAA;
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET);
ret = HAL_SPI_Transmit(&hspi1,&command, 1, 100);
delay(1);
//delay(1);
ret = HAL_SPI_Receive(&hspi1, koefS1_0, 2, 100);
//delay(1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET);
 
 
uint32_t data_zdruzen = ((uint8_t)dataS1[0] << 16) | ((uint8_t)dataS1[1] << 8) | (uint8_t)dataS1[2];
	  	  uint16_t koef_zdruzen0 = ((int8_t)koefS1_0[0] << 8) | (koefS1_0[1]);
	  	  uint16_t koef_zdruzen1 = ((int8_t)koefS1_1[0] << 8) | (koefS1_1[1]);
	  	  uint16_t koef_zdruzen2 = ((int8_t)koefS1_2[0] << 8) | (koefS1_2[1]);
	  	  uint16_t koef_zdruzen3 = ((int8_t)koefS1_3[0] << 8) | (koefS1_3[1]);
	  	  uint16_t koef_zdruzen4 = ((int8_t)koefS1_4[0] << 8) | (koefS1_4[1]);
 
	  	  uint16_t data_zdruzen_16b = (uint16_t)(data_zdruzen>>8);
		  float temperatura1 = (-2)*koef_zdruzen4*pow(10,-21)*pow((float)data_zdruzen_16b,4)+ 4*koef_zdruzen3*pow(10,-16)*pow((float)data_zdruzen_16b,3)-2*koef_zdruzen2*pow(10,-11)*pow((float)data_zdruzen_16b,2)+1*koef_zdruzen1*pow(10,-6)*(float)data_zdruzen_16b+(-1.5)*koef_zdruzen0*pow(10,-2);
		  //long double temperatura = (-2)*koef_zdruzen4*pow(10,-21)*pow((double)data_zdruzen,4)+ 4*koef_zdruzen3*pow(10,-16)*pow((double)data_zdruzen,3)-2*koef_zdruzen2*pow(10,-11)*pow((double)data_zdruzen,2)+1*koef_zdruzen1*pow(10,-6)*(double)data_zdruzen+(-1.5)*koef_zdruzen0*pow(10,-2);
 
 
///////////////////////////////////////
#define ADDRESS (0x77)
 
 #ifdef _TEST_
   for(uint8_t i=1; i<256; i++)
        {
 	   ret = HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(i<<1), 10, 100);
 	   if (ret != HAL_OK) /* No ACK Received At That Address */
 	   {
 		   //HAL_UART_Transmit(&huart1, Space, sizeof(Space), 10000);
 	   }
 	   else if(ret == HAL_OK)
 	   {
 		   //sprintf(buf, "0x%X", i);
 		   //HAL_UART_Transmit(&huart1, buf, sizeof(buf), 10000);
 	   }
 	   buf[i] = (uint8_t)ret;
        }
#endif
 
  ret = HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(ADDRESS<<1), 10, 100);
//reset
  command = 0x1E;
  ret = HAL_I2C_Master_Transmit (&hi2c1, (uint16_t)(ADDRESS<<1), &command, 1, 100);
  // read
	  command = 0x48;
	  ret = HAL_I2C_Master_Transmit (&hi2c1, (uint16_t)(ADDRESS<<1), &command, 1, 100);
	  	//delay(1);
	 command = 0x00;
	  ret = HAL_I2C_Master_Transmit (&hi2c1, (uint16_t)(ADDRESS<<1), &command, 1, 100);
	  	//delay(1);
	  	  ret = HAL_I2C_Master_Receive (&hi2c1, (uint16_t)(ADDRESS<<1), dataI2C, 3, 100);
 
 
	  command = 0xA2;
	  ret = HAL_I2C_Master_Transmit (&hi2c1, (uint16_t)(ADDRESS<<1), &command, 1, 100);
	  // delay(1);
	  ret = HAL_I2C_Master_Receive (&hi2c1, (uint16_t)(ADDRESS<<1), koefI2C_4, 2, 100);
	  command = 0xA4;
	  ret = HAL_I2C_Master_Transmit (&hi2c1, (uint16_t)(ADDRESS<<1), &command, 1, 100);
	  //delay(1);
	  ret = HAL_I2C_Master_Receive (&hi2c1, (uint16_t)(ADDRESS<<1), koefI2C_3, 2, 100);
	  command = 0xA6;
	  ret = HAL_I2C_Master_Transmit (&hi2c1, (uint16_t)(ADDRESS<<1), &command, 1, 100);
	  //delay(1);
	  ret = HAL_I2C_Master_Receive (&hi2c1, (uint16_t)(ADDRESS<<1), koefI2C_2, 2, 100);
	  command = 0xA8;
	  ret = HAL_I2C_Master_Transmit (&hi2c1, (uint16_t)(ADDRESS<<1), &command, 1, 100);
	  //delay(1);
	  ret = HAL_I2C_Master_Receive (&hi2c1, (uint16_t)(ADDRESS<<1), koefI2C_1, 2, 100);
	  command = 0xAA;
	  ret = HAL_I2C_Master_Transmit (&hi2c1, (uint16_t)(ADDRESS<<1), &command, 1, 100);
	  //delay(1);
	  ret = HAL_I2C_Master_Receive (&hi2c1, (uint16_t)(ADDRESS<<1), koefI2C_0, 2, 100);
 
	  //zdruzevanje podatkov
	  uint32_t data_zdruzenI2C = ((uint8_t)dataI2C[0] << 16) | ((uint8_t)dataI2C[1] << 8) | (uint8_t)dataI2C[2];
	  	  uint16_t koef_zdruzenI2C0 = ((int8_t)koefI2C_0[0] << 8) | (koefI2C_0[1]);
	  	  uint16_t koef_zdruzenI2C1 = ((int8_t)koefI2C_1[0] << 8) | (koefI2C_1[1]);
	  	  uint16_t koef_zdruzenI2C2 = ((int8_t)koefI2C_2[0] << 8) | (koefI2C_2[1]);
	  	  uint16_t koef_zdruzenI2C3 = ((int8_t)koefI2C_3[0] << 8) | (koefI2C_3[1]);
	  	  uint16_t koef_zdruzenI2C4 = ((int8_t)koefI2C_4[0] << 8) | (koefI2C_4[1]);
 
	  	  //temp
	  	  uint16_t data_zdruzenI2C_16b = (uint16_t)(data_zdruzenI2C>>8);
	  	  long double temperaturaI2C = (-2)*koef_zdruzenI2C4*pow(10,-21)*pow((double)data_zdruzenI2C_16b,4)+ 4*koef_zdruzenI2C3*pow(10,-16)*pow((double)data_zdruzenI2C_16b,3)-2*koef_zdruzenI2C2*pow(10,-11)*pow((double)data_zdruzenI2C_16b,2)+1*koef_zdruzenI2C1*pow(10,-6)*(double)data_zdruzenI2C_16b-1.5*koef_zdruzenI2C0*pow(10,-2);
 

This is my main function code. I have also found out that if I comment out SPI communication then I2C works but if I uncomment it and use SPI and I2C then in the I2C part of code it gets stuck after like 3rd transmit function

Karl Yamashita
Lead III

in main, see if you can get past HAL_Init() or does it go to Error_Handler()?

int main(void)
{
  /* USER CODE BEGIN 1 */
	uint8_t buf[256];
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
	if(HAL_Init() != HAL_OK)
	{
		Error_Handler();
	}

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

it doesnt go to error handler

AScha.3
Chief III

why is sys-clk-config light grey ?


_legacyfs_online_stmicro_images_0693W00000biB8qQAE.png

If you feel a post has answered your question, please click "Accept as Solution".
Karl Yamashita
Lead III

how about removing all your peripherals you've added, SPI, I2C, TIM2. See if your RB11 or RA15 toggles

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  HAL_Delay(1000);
	  HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_15);
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

no idea actually, seems normal to me in the editor. I changed theme to black now to see the difference but they all look the same either way

I created a new project and pasted your code in and it's the same. doesnt work :\

this CANNOT be wrong...

what you use: custom board or disco ..?

If you feel a post has answered your question, please click "Accept as Solution".

it's a PCB that I made, using STM32L431CCT6