I am writing code to generate a timestamp to use it as a reference for to perform a certain function. can anyone help me how to do it. i am new to embedded programming.
-
2Welcome to StackOverflow. Please [take the tour](https://stackoverflow.com/tour) an read [What topics can I ask about](https://stackoverflow.com/help/on-topic) and [What topics to avoid](https://stackoverflow.com/help/dont-ask) and [How to ask a good question](https://stackoverflow.com/help/how-to-ask) and [the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](https://stackoverflow.com/help/mcve). – common sense Jan 25 '18 at 10:10
2 Answers
First of all, you must init RTC. Hope you know, how to do this STM32CubeMX.
Then it is simple code:
#include <time.h>
/* Global Vars */
RTC_TimeTypeDef currentTime;
RTC_DateTypeDef currentDate;
time_t timestamp;
struct tm currTime;
/* Code to get timestamp
*
* You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
* in the higher-order calendar shadow registers to ensure consistency between the time and date values.
* Reading RTC current time locks the values in calendar shadow registers until Current date is read
* to ensure consistency between the time and date values.
*/
HAL_RTC_GetTime(&hrtc, ¤tTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, ¤tDate, RTC_FORMAT_BIN);
currTime.tm_year = currentDate.Year + 100; // In fact: 2000 + 18 - 1900
currTime.tm_mday = currentDate.Date;
currTime.tm_mon = currentDate.Month - 1;
currTime.tm_hour = currentTime.Hours;
currTime.tm_min = currentTime.Minutes;
currTime.tm_sec = currentTime.Seconds;
timestamp = mktime(&currTime);
P.S. I do not check if Date and Time data are correct. If you wanna to - make some checks for correct data yourself.

- 1,020
- 12
- 27
This is a very broad question. With the STM32, you have different ways to implement a timestamp. I assume, you mean the elapsed milliseconds since startup. If you want the actual time on the clock, you need an RTC (real time clock).
Generally, you need to write an function called the interrupt routine (or interrupt handler), which increments a variable every 1 millisecond (or any other suitable time interval). This function will be called every time the interrupt triggers, usually due to a timer overflow.
Now you can either configure the STM32 registers directly (which I do not recommend) or use a library (such as StdPeriph or HAL). Especially the latter is very simple to use.
I.e. with the HAL, a simple call of HAL_GetTick() gives you a timestamp in ms. See this for more information.

- 533
- 5
- 22
-
1
-
3@PeterJ_01 It gets the job done very quickly and leads beginners to succes at an early stage. Beginners cannot read the datasheet and set the registers correctly if they don't know what they have to be looking for. In my opinion, a library is the first step to become used to the concept of microcontrollers while having quick results. If you succeed using a library, then you can go deeper, setting the bits manually (i.e. in RCC_APBEN, GPIOx->MODE, ODR and IDR). I know that there are other completely valid approaches, but this my humble opinion. – MemAllox Jan 25 '18 at 15:23
-
2It is only your personal opinion, which is in my opinion 100% wrong. – 0___________ Jan 25 '18 at 16:25
-
3I respect that. I'd like to suggest that you write an alternative answer then, proposing your approach instead of (or at least additionally to) downvoting answers which are obviously not wrong. – MemAllox Jan 25 '18 at 17:49
-
It is the comment only as there is no recommended method of writing programme. It is up to programmer. – 0___________ Jan 25 '18 at 18:56
-
`HAL_Init()` does not give timestamp. `HAL_GetTick()` provides a tick value in millisecond from start. And the question is about **Timestamp**, not **Tick**. – Bulkin Jan 26 '18 at 12:24