Skip to main content
PPopo.1
Senior
April 16, 2023
Question

hal_delay() not working

  • April 16, 2023
  • 54 replies
  • 21813 views

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

This topic has been closed for replies.

54 replies

PPopo.1
PPopo.1Author
Senior
April 16, 2023

so this is the inside debug of delay:


_legacyfs_online_stmicro_images_0693W00000biAu0QAE.pngit gets stuck at this while

Tesla DeLorean
Guru
April 16, 2023

Make sure SysTick_Handler() is calling HAL_IncTick(), that it's not weak, and that it is binding into the image.

Double check SCB->VTOR pointer to your Vector Table address, and that the interrupts aren't disabled.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
PPopo.1
PPopo.1Author
Senior
April 16, 2023

ok so this is systick:
_legacyfs_online_stmicro_images_0693W00000biAvNQAU.pngand this is hal_gettick


_legacyfs_online_stmicro_images_0693W00000biAvhQAE.png

AScha.3
Super User
April 16, 2023

seems: sysclk not running ...because tickstart should not stay at 0.

should be so: (> 0)


_legacyfs_online_stmicro_images_0693W00000biAuUQAU.png

If you feel a post has answered your question, please click on " Best Answer ".
PPopo.1
PPopo.1Author
Senior
April 16, 2023

any idea how do i fix that ?

PPopo.1
PPopo.1Author
Senior
April 16, 2023

I tried creating a new projectg without any setup changes and tried running HAL_Delay(300); and it also got stuck

AScha.3
Super User
April 16, 2023

and in ***_it.c file you should have :


_legacyfs_online_stmicro_images_0693W00000biAvm.png

If you feel a post has answered your question, please click on " Best Answer ".
PPopo.1
PPopo.1Author
Senior
April 16, 2023

I have that

PPopo.1
PPopo.1Author
Senior
April 16, 2023

So basically I am trying to read 3 sensors, two with SPI and one with I2C.

PPopo.1
PPopo.1Author
Senior
April 16, 2023
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
Principal
April 16, 2023

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();
	}

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
PPopo.1
PPopo.1Author
Senior
April 16, 2023

it doesnt go to error handler

AScha.3
Super User
April 16, 2023

why is sys-clk-config light grey ?


_legacyfs_online_stmicro_images_0693W00000biB8qQAE.png

If you feel a post has answered your question, please click on " Best Answer ".
PPopo.1
PPopo.1Author
Senior
April 16, 2023

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

Karl Yamashita
Principal
April 16, 2023

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 */

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
PPopo.1
PPopo.1Author
Senior
April 16, 2023

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

AScha.3
Super User
April 17, 2023

this CANNOT be wrong...

what you use: custom board or disco ..?

If you feel a post has answered your question, please click on " Best Answer ".
PPopo.1
PPopo.1Author
Senior
April 17, 2023

@AScha.3​  It seems to be working fine, so I have no idea what could be the issue

AScha.3
Super User
April 17, 2023

>but Im not sure how much can gpio handle 

read in DS , p. 135 . depends on core voltage/speed setting , for high 11 ->


_legacyfs_online_stmicro_images_0693W00000biEBxQAM.png>I have no idea what could be the issue

me too.

try new project ? and leave everything, as it is, no modified settings.

If you feel a post has answered your question, please click on " Best Answer ".
PPopo.1
PPopo.1Author
Senior
April 17, 2023

I tried that, also created whole new workspace not just the project. Will try with another STM32 when I get home