Search code examples
carmprintfiarstm32

STM32 printf and RTC


* UPDATE *

Here is what I found. Whenever I had that function in there it wouldn't actually make the code lock up. It would actually make the read RTC I²C function very slow to execute, but the code would still run properly, but I had to wait a really long time to get past every time I read the RTC.

So there is an alarm interrupt for the RTC and this was triggering other I²C interactions inside the ISR, so it looks like it was trying to do two I²C communications at the same time, therefore slowing down the process. I removed the functions in the ISR and it's working now. I will keep investigating.


I am having this problem when programming an STM32F103 microcontroller using IAR 5.40. I have this function that if I try to printf a local variable it causes the code to freeze at another point way before it even gets to that function in question.

What could possibly be causing this?

This is the function:

u8 GSM_Telit_ReadSms(u8 bSmsIndex)
{
  char bTmpSms[3] = {0};

  itoa(bSmsIndex, bTmpSms, 10); // Converts the smsindex into a string

  printf("index = %s\n", bTmpSms); // This printf caused the code to get stuck in the RTC // byte read function!

  GSM_Telit_RequestModem("AT+CMGR=""1", 10, "CMGR", 5, 0);
  return 1;
}

I tried this as well and this does not cause the lock I experienced:

u8 GSM_Telit_ReadSms(u8 bSmsIndex)
{
  char bTmpSms[3] = {0};

  itoa(bSmsIndex, bTmpSms, 10);
  printf("index = 2\n");


  GSM_Telit_RequestModem("AT+CMGR=""1", 10, "CMGR", 5, 0);
  return 1;
}

There is no optimization enabled whatsoever and the code gets stuck when trying to read a byte out of my I²C RTC, but as soon as I remove this printf("index = %s\n", bTmpSms); or use this one instead printf("index = 2\n"); then everything is happy. Any ideas?

The bSmsIndex will never be more than 30 actually and even then the lock up happens wayyyy before this function gets called.


Solution

  • It seems that if I don't initialize the variable bTmpSms to something the problem occurs.

    I also realized that it is not the printf that is the problem. It is the itoa function. It got me to check that even though I didn't think that was the problem, when I commented the itoa function then the whole code worked.

    So I ended up doing this:

    u8 GSM_Telit_ReadSms(u8 bSmsIndex)
    {
      char bTmpSms[4] = "aaa";    // I still need to find out why this is !!!
    
      itoa(bSmsIndex, bTmpSms, 10); // Converts the smsindex into a string
    
      printf("index = %s\n", bTmpSms); // This printf caused the code to get stuck in the RTC // byte read function!
    
      GSM_Telit_RequestModem("AT+CMGR=""1", 10, "CMGR", 5, 0);
      return 1;
    }
    

    This is the itoa function I got:

    char itoa(int value, char* result, int base)
    {
      // Check that the base if valid
      if (base < 2 || base > 36) {
          *result = '\0';
          return 0;
      }
    
      char* ptr = result, *ptr1 = result, tmp_char;
      int tmp_value;
    
      do
      {
        tmp_value = value;
        value /= base;
        *ptr++ = "zyxwvutsr
    

    qponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; } while (value);

      // Apply negative sign
      if (tmp_value < 0)
          *ptr++ = '-';
      *ptr-- = '\0';
      while(ptr1 < ptr)
      {
        tmp_char = *ptr;
        *ptr--= *ptr1;
        *ptr1++ = tmp_char;
      }
      return 1;
    }