0

I'm programming an STM32 microcontroller. In the code I'm just calling sprintf the way I did before and it worked then, but now it just freezes the entire program. What's the problem here?

char str[100]; 
sprintf(str, "TEMP_ADC: %d\n", 50);

I've included stdlib.h, stdio.h, math.h and string.h. The 100 char limit is more than enough.

I tried lighting LEDS above and below the sprintf() call and it's clear the program gets stuck on the sprintf(). I tried switching to snprintf() but it resulted in the same behaviour.

EDIT:

I noticed that sprintf() works when I use it before starting the DMA. If I use it after, the program freezes. Could it have something to do with casting an uint16_t array to uint32_t?

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

  uint16_t values[4];

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

/* Configure the peripherals common clocks */
  PeriphCommonClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_ADC1_Init();
  MX_TIM2_Init();
  MX_TIM16_Init();
  MX_MEMORYMAP_Init();
  MX_LPUART1_UART_Init();
  MX_RF_Init();
  /* USER CODE BEGIN 2 */

  char str[100];
  sprintf(str, "TEMP_ADC: %d\n", 50);
  int size = (int)((ceil(log10(50))+1+11)*sizeof(char));
  HAL_UART_Transmit(&hlpuart1, str, size, 100);
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, SET);

  HAL_ADC_Start_DMA(&hadc1, (uint32_t *)values, 4);
  uint16_t temp = 0;


  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
          HAL_Delay(1000);

        /* USER CODE END WHILE */

        /* USER CODE BEGIN 3 */
      }
      /* USER CODE END 3 */
    }
28
  • 1
    You probably messed up something in another part of your code (maybe writing in some memory that doesn't belong to you). The code you show is 100% correct. Commented Mar 8, 2024 at 16:34
  • 2
    What you posted looks fine. Try doing a diff with the version that worked. Try commenting out as much of the code as possible. Post a minimal example that demonstrates the problem. Commented Mar 8, 2024 at 16:36
  • 1
    You can mess up things even without using the heap, for example with off by one errors writing into an array, buffer overflows, poor string management, etc. Who knows, we cannot see your code... As suggested by another comment, simplify your code until it works. Then you'll see what triggers the problem. Commented Mar 8, 2024 at 16:52
  • 1
    The code fragment presented in the question is not erroneous. Nevertheless, it is a bit overblown. sprintf is pretty heavyweight for a string that easily could be formatted manually. The exact fragment shown could be more simply achieved with char str[100] = "TEMP_ADC: 50\n";. Or if the real circumstances don't permit using an initializer, then strcpy() would still be an improvement over sprintf() for this particular case. Commented Mar 8, 2024 at 17:21
  • 2
    There's simply no way to answer the question from the little information you've provided. The problem is almost certainly somewhere else in the program. When you cause undefined behavior, the symptoms can be in completely unrelated parts of the code. Commented Mar 8, 2024 at 17:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.