cancel
Showing results for 
Search instead for 
Did you mean: 

time return always the same value

marccollin7379
Associate II
Posted on May 01, 2015 at 15:48

hi

my rtc is initialized i set date and time. long getUnixTimestamp() { struct tm timeinfo; //Setup a tm structure based on the RTC timeinfo.tm_wday = dateStruct.WeekDay; timeinfo.tm_mon = dateStruct.Month - 1; timeinfo.tm_mday = dateStruct.Date; timeinfo.tm_year = dateStruct.Year + 100; timeinfo.tm_hour = timeStruct.Hours; timeinfo.tm_min = timeStruct.Minutes; timeinfo.tm_sec = timeStruct.Seconds; time_t rawtime = mktime(&timeinfo); printf(''Current date and time are: %s\n'', ctime(&rawtime)); long x = time(&rawtime); printf(''time %lu\n'', x); return x; } i do a call to HAL_RTC_GetTime HAL_RTC_GetDate getUnixTimestamp i get

Current date and time are: Wed Apr 29 22:46:00 2015time 1430347560
5 second later, i do another call to HAL_RTC_GetTime, HAL_RTC_GetDate, getUnixTimestamp and getCurrent date and time return Wed Apr 29 22:46:05 2015 and time 1430347560

why time function value is not modified?

thanks #rtc #stm32f4 #discovery #time
5 REPLIES 5
Posted on May 01, 2015 at 16:36

Maybe the RTC clock (LSE) is not running.

Try running it from LSI, just as an experiment.

JW

Posted on May 01, 2015 at 17:43

Isn't that a hosting issue? ie the tool chain, and it's library, has absolutely no idea how the timing/interrupts work on your board/chip. This is code you'd have to provide for the platform.

http://www.keil.com/support/man/docs/armlib/armlib_chr1358938918041.htm

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on May 01, 2015 at 18:04

Nevermind...

If the HAL functions always return the same values, any further processing of those numbers will result in the same answer.

Check the clock sources, and that the RTC is suitably configured and enabled.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
marccollin7379
Associate II
Posted on May 01, 2015 at 23:58


RCC_ClkInitTypeDef RCC_ClkInitStruct;

RCC_OscInitTypeDef RCC_OscInitStruct;


// Enable Power Control clock

__PWR_CLK_ENABLE();


// Enable access to Backup domain

HAL_PWR_EnableBkUpAccess();


// Reset Backup domain

__HAL_RCC_BACKUPRESET_FORCE();

__HAL_RCC_BACKUPRESET_RELEASE();



// The voltage scaling allows optimizing the power consumption when the

// device is clocked below the maximum system frequency, to update the

// voltage scaling value regarding system frequency refer to product

// datasheet.

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);



// Enable HSE Oscillator and activate PLL with HSE as source

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

RCC_OscInitStruct.HSEState = RCC_HSE_ON;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;


// This assumes the HSE_VALUE is a multiple of 1MHz. If this is not

// your case, you have to recompute these PLL constants.

RCC_OscInitStruct.PLL.PLLM = (HSE_VALUE / 1000000u);

RCC_OscInitStruct.PLL.PLLN = 336;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

RCC_OscInitStruct.PLL.PLLQ = 7;

HAL_RCC_OscConfig(&RCC_OscInitStruct);


// Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2

// clocks dividers

RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK

| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);


// Enable LSE Oscillator

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; /* Mandatory, otherwise the PLL is reconfigured! */

RCC_OscInitStruct.LSEState = RCC_LSE_ON; /* External 768 kHz clock on OSC_IN/OSC_OUT */

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) {

// Connect LSE to RTC

__HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE);

__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);

}



// Enable RTC

__HAL_RCC_RTC_ENABLE();


RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;

RtcHandle.Init.AsynchPrediv = 127;

trace_printf(''LSE_VALUE %d.
'', LSE_VALUE);

RtcHandle.Init.SynchPrediv = (LSE_VALUE / 128) - 1;

RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;

RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;


RtcHandle.Instance = RTC;


if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {

trace_printf(''RTC error: RTC initialization failed.
'');

}

it's not all who return the same value, it's c function. trace_printf(''Current date and time are: %s '', ctime(&rawtime)); trace_printf(''time %lu '', time(&rawtime); Current date and time are: Wed Apr 29 22:46:00 2015 time 4294967295 and the second call Current date and time are: Wed Apr 29 22:46:06 2015

time 4294967295

francescatodiego
Associate II
Posted on May 02, 2015 at 09:12

time return -1 if an error occur

-1 = 0xFFFFFFFF = 4294967295

try first with time (0) ;

time return a time_t type, convert it to long or separe the time call from printf