AnsweredAssumed Answered

Code works differently in debug mode than in real time

Question asked by schperplata on Nov 26, 2014
Latest reply on Nov 29, 2014 by schperplata
Hello. 
I've got problem regarding STM32L100 (on discovery board). I've switched from Arduino where I enjoy the simplicity of printing data to via UART and delay/millis functions. So, I decided to write my own, and they suited me until I discovered, that they don't work as they should. 
The strangest thing is, that in debug mode, line-by-line, code works flawless. When code works in normal mode eg. 32MHz, something wierd is happening and I can't figure out what. 

I've added my own files for printing on USARTx and my own millis functions. Here is the main code, since result printed on my computer is what bother me. 
main.c:
01.#include "main.h"
02. 
03.//STM32L100RCT6-Discovery - clock setumfunction - SYSCLK = 32MHz, HSI.
04.void clockCorrection()
05.{
06.    RCC_DeInit();
07.    RCC_HSICmd(ENABLE);
08.     
09.    while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) != SET)    // Wait until HSI is ready
10.    {
11.    }
12.  
13.    FLASH->ACR |= FLASH_ACR_ACC64;      // Enable 64-bit access
14.    FLASH->ACR |= FLASH_ACR_PRFTEN;     // Enable Prefetch Buffer
15.    FLASH->ACR |= FLASH_ACR_LATENCY;    // Flash 1 wait state
16.     
17.    RCC->APB1ENR |= RCC_APB1ENR_PWREN;  // Power enable
18.    PWR->CR = PWR_CR_VOS_0;             // Select the Voltage Range 1 (1.8 V)
19.    while((PWR->CSR & PWR_CSR_VOSF) != RESET) // Wait until the Voltage Regulator is ready
20.    {
21.    }
22.     
23.    RCC_PLLConfig(RCC_PLLSource_HSI, RCC_PLLMul_4, RCC_PLLDiv_2);   // HSI = 16MHz; 16MHz * 4 / 2 = 32MHz
24.    RCC_PLLCmd(ENABLE);
25.    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) != SET)    // Wait untill PLL is ready
26.    {
27.    }
28.    RCC_PCLK1Config(RCC_HCLK_Div1);         // Configures the Low Speed APB(APB1) clock (PCLK1).
29.    RCC_PCLK2Config(RCC_HCLK_Div1);         // Configures the High Speed APB(APB2) clock (PCLK2).
30. 
31.    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);  // Configures the System Clock source to PLL
32.    while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)  // Wait till PLL is used as system clock source
33.    {
34.    }
35.}
36. 
37.int main(void)
38.{  
39.        uint32_t tajm;
40.    uint32_t tajm2;
41.     
42.    clockCorrection();
43.    timer2_millis_init();  
44.    init_usart(USART1, GPIOA, 57600);
45.    printStringLn(USART1, "UART initialized!");
46.     
47.    while(1)
48.        
49.        printString(USART1, "tajm:");   //print to UART1
50.        delay(500);  //delay 500ms
51.        printNumberLn(USART1, micros(), DEC);  //print microseconds from the begining of tim2 init
52.        }
53.}

Values printed out on terminal are wrong - microsecond part of number isn't changing at all:
01.tajm:UART initialized!
02.tajm:505003
03.tajm:1007003
04.tajm:1509003
05.tajm:2011003
06.tajm:2513003
07.tajm:3015003
08.tajm:3517003
09.tajm:4019003
10.tajm:4521003
11.tajm:5023003
12.tajm:5525003
13.tajm:6027003
14.tajm:6529003
15.tajm:7031003
16.tajm:7533003
17.tajm:8035003
18.tajm:8537003

If I add simple character after delay(500);
printString(USART1, " ");
01.tajm:UART initialized!
02.tajm: 505020
03.tajm: 1007021
04.tajm: 1509022
05.tajm: 2011022
06.tajm: 2513023
07.tajm: 3015024
08.tajm: 3517007
09.tajm: 4019008
10.tajm: 4521008
11.tajm: 5023009
12.tajm: 5525010
13.tajm: 6027010
14.tajm: 6529011
15.tajm: 7031012
16.tajm: 7533012
17.tajm: 8035013
18.tajm: 8537014
19.tajm: 9039014
20.tajm: 9541015
21.tajm: 10043016
22.tajm: 10545016
23.tajm: 11047017
24.tajm: 11549018
25.tajm: 12051017
26.tajm: 12553019
27.tajm: 13055019
28.tajm: 13557019
29.tajm: 14059020
30.tajm: 14561021
31.tajm: 15063021
32.tajm: 15565022
33.tajm: 16067023
34.tajm: 16569023
35.tajm: 17071024
36.tajm: 17573008
37.tajm: 18075008
38.tajm: 18577009
39.tajm: 19079009

or: printString(USART1, "  ");
01.tajm:UART initialized!
02.tajm:  505189
03.tajm:  1007190
04.tajm:  1509190
05.tajm:  2011191
06.tajm:  2513192
07.tajm:  3015192
08.tajm:  3517193
09.tajm:  4019177
10.tajm:  4521177
11.tajm:  5023178
12.tajm:  5525179
13.tajm:  6027179
14.tajm:  6529180
15.tajm:  7031181
16.tajm:  7533181
17.tajm:  8035182
18.tajm:  8537182
As you can see, values of microsecond part of printed numbers are incrementing, than at some point "restarted" and again incrementing. I can't figure why is that - and what is causing this "delay dependant" value. Seems like second and milisecond part of printed numbers are OK and are somewhat in "time" with real clock. 
If I run identical code in debug mode, printed values are OK. So it seem something - my uart or my millis "library" is messing with printed data when stm32 runs on high speed - 32MHz. I don't know, just guessing. 

I hope I added all files, let me know if there is anything missing. Currently I've spent too much time for this "simple" problem, so I hope someone has a solution.

Attachments

Outcomes