{"id":4263,"date":"2020-04-01T20:00:34","date_gmt":"2020-04-01T18:00:34","guid":{"rendered":"https:\/\/msalamon.pl\/?p=4263"},"modified":"2025-12-27T17:30:08","modified_gmt":"2025-12-27T16:30:08","slug":"additional-rtc-features-in-stm32f4-part-1","status":"publish","type":"post","link":"https:\/\/msalamon.pl\/en\/additional-rtc-features-in-stm32f4-part-1\/","title":{"rendered":"Additional RTC features in STM32F4 part 1"},"content":{"rendered":"\n<p>The RTC clock you\u2019ll find in STM32 microcontrollers is used not only for simple timekeeping. Besides this basic function, it also has several other more or less useful ones. I\u2019ve already shown you the backup registers, which can be used to store sensitive data or remember the date in a less advanced clock. This time I\u2019d like to show you a few other features that are available to us.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>While writing, it turned out I produced quite a lot of text, so I split this post into two parts \ud83d\ude42<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Additional RTC features in STM32F4<\/h1>\n\n\n\n<p>You may remember from the previous post that in the F4 series there are really quite a lot of registers in the real-time clock module itself. This is caused, among other things, by the fact that we have a few interesting functions added. I\u2019ll show you the first two related to \u201cbreak-ins\u201d:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Tamper<\/li>\n\n\n\n<li>Time Stamp<\/li>\n\n\n\n<li>Tamper + Time Stamp<\/li>\n<\/ul>\n\n\n\n<p>Let me base this on the board I chose in the previous article. It\u2019s the so-called <a href=\"https:\/\/sklep.msalamon.pl\/produkt\/stm32f401ccu6-dev-board\/?utm_source=blog&amp;utm_medium=article&amp;utm_campaign=rtc_features&amp;utm_content=Text\">BlackPill with STM32F401<\/a> onboard. You can buy <a href=\"https:\/\/sklep.msalamon.pl\/produkt\/stm32f401ccu6-dev-board\/?utm_source=blog&amp;utm_medium=post&amp;utm_campaign=rtcf4\">such a board<\/a> (or a <a href=\"https:\/\/sklep.msalamon.pl\/produkt\/stm32f411ceu6-dev-board\/?utm_source=blog&amp;utm_medium=post&amp;utm_campaign=rtcf4\">slightly more powerful one<\/a>) from me.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/sklep.msalamon.pl\/produkt\/stm32f401ccu6-dev-board\/?utm_source=blog&amp;utm_medium=banner&amp;utm_campaign=rtc_features&amp;utm_content=blackpill\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"400\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2019\/12\/BlackPill-baner.jpg\" alt=\"stm32f411\" class=\"wp-image-1326\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2019\/12\/BlackPill-baner.jpg 1200w, https:\/\/msalamon.pl\/wp-content\/uploads\/2019\/12\/BlackPill-baner-300x100.jpg 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2019\/12\/BlackPill-baner-1024x341.jpg 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2019\/12\/BlackPill-baner-768x256.jpg 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2019\/12\/BlackPill-baner-24x8.jpg 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2019\/12\/BlackPill-baner-36x12.jpg 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2019\/12\/BlackPill-baner-160x53.jpg 160w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The software I\u2019ll use is:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>STM32CubeIDE v1.2.0<\/li>\n\n\n\n<li>STM32CubeMX v5.5.0 built into the IDE<\/li>\n\n\n\n<li>HAL F4 v1.24.2<\/li>\n<\/ul>\n\n\n\n<p>I\u2019ll also base this on the <a href=\"https:\/\/github.com\/lamik\/RTC_F401\" target=\"_blank\" rel=\"noopener\">project I created for STM32F4<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Time Stamp in STM32F4<\/h2>\n\n\n\n<p>Translating this into Polish, it would be \u201cStempel Czasu\u201d (like from Harry Potter, darn it). This function works such that during a \u201cbreak-in\u201d, i.e., when an edge is detected on a dedicated pin, the date and time are latched. This is done \u201csilently\u201d, so the intruder won\u2019t realize what happened.<\/p>\n\n\n\n<p>We can immediately send such registered tampering remotely to headquarters or store it in logs in non-volatile memory.<\/p>\n\n\n\n<p>First, you need to enable this function in Cube by checking it.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"829\" height=\"305\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp_cube.png\" alt=\"\" class=\"wp-image-1466\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp_cube.png 829w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp_cube-300x110.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp_cube-768x283.png 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp_cube-24x9.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp_cube-36x13.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp_cube-160x59.png 160w\" sizes=\"auto, (max-width: 829px) 100vw, 829px\" \/><\/figure>\n<\/div>\n\n\n<p> A configured GPIO pin will appear on PC13. It will serve as the stamp source.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"395\" height=\"297\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube2.png\" alt=\"\" class=\"wp-image-1461\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube2.png 395w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube2-300x226.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube2-24x18.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube2-36x27.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube2-106x80.png 106w\" sizes=\"auto, (max-width: 395px) 100vw, 395px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>An additional section will appear in the RTC configuration.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"817\" height=\"83\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp_cube2.png\" alt=\"\" class=\"wp-image-1467\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp_cube2.png 817w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp_cube2-300x30.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp_cube2-768x78.png 768w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp_cube2-24x2.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp_cube2-36x4.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp_cube2-160x16.png 160w\" sizes=\"auto, (max-width: 817px) 100vw, 817px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Select which edge you\u2019re interested in here. I chose the falling edge. On pin PC13 I additionally added a 10 k\u03a9 pull-up resistor to supply. You can\u2019t set an internal Pull Up for a special pin.<\/p>\n\n\n\n<p>The Event Time Stamp generates an interrupt. Of course, in HAL we\u2019ll find its handler that we can implement. In it, I read the date and time of the occurrence and set my own flag, which I\u2019ll use in the main loop.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)\n{\n  HAL_RTCEx_GetTimeStamp(hrtc, &amp;amp;RtcTimeStamp, &amp;amp;RtcDateStamp, RTC_FORMAT_BIN);\n  MillisecondsStamp = ((RtcTime.SecondFraction-RtcTimeStamp.SubSeconds)\/((float)RtcTime.SecondFraction+1) * 100);\n\tTimeStampFlag = 1;\n}<\/pre>\n\n\n\n<p>I use this flag in the main loop to print the stamp date and time. The rest of the time, every second I send data from the RTC to the serial port.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">  \/* USER CODE BEGIN WHILE *\/\n  while (1)\n  {\n\t  HAL_RTC_GetTime(&amp;amp;hrtc, &amp;amp;RtcTime, RTC_FORMAT_BIN);\n\t  Milliseconds = ((RtcTime.SecondFraction-RtcTime.SubSeconds)\/((float)RtcTime.SecondFraction+1) * 100);\n\t  HAL_RTC_GetDate(&amp;amp;hrtc, &amp;amp;RtcDate, RTC_FORMAT_BIN);\n\n\t  if(RtcTime.Seconds != CompareSeconds)\n\t  {\n\t\t  MessageLen = sprintf((char*)Message, \"Date: %02d.%02d.20%02d Time: %02d:%02d:%02d:%02d\\n\\r\", RtcDate.Date, RtcDate.Month, RtcDate.Year, RtcTime.Hours, RtcTime.Minutes, RtcTime.Seconds, Milliseconds);\n\t\t  HAL_UART_Transmit(&amp;amp;huart2, Message, MessageLen, 100);\n\t\t  CompareSeconds = RtcTime.Seconds;\n\t  }\n\n\t  if(TimeStampFlag == 1)\n\t  {\n\t\t  MessageLen = sprintf((char*)Message, \"TimeStamp! Date: %02d.%02d.20%02d Time: %02d:%02d:%02d:%02d\\n\\r\", RtcDateStamp.Date, RtcDateStamp.Month,\n\t\t\t\t  RtcDateStamp.Year, RtcTimeStamp.Hours, RtcTimeStamp.Minutes, RtcTimeStamp.Seconds, MillisecondsStamp);\n\t\t  HAL_UART_Transmit(&amp;amp;huart2, Message, MessageLen, 100);\n\n\t\t  TimeStampFlag = 0;\n\t  }\n\n\t  if(GPIO_PIN_RESET == HAL_GPIO_ReadPin(TEST_GPIO_Port, TEST_Pin))\n\t  {\n\t\t while(GPIO_PIN_RESET == HAL_GPIO_ReadPin(TEST_GPIO_Port, TEST_Pin))\n\t\t {}\n\t\t SetRTC();\n\t  }\n    \/* USER CODE END WHILE *\/\n\n    \/* USER CODE BEGIN 3 *\/\n  }<\/pre>\n\n\n\n<p>And here is the effect in action.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"686\" height=\"441\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp.png\" alt=\"\" class=\"wp-image-1465\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp.png 686w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp-300x193.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp-24x15.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp-36x23.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_timestamp-124x80.png 124w\" sizes=\"auto, (max-width: 686px) 100vw, 686px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>After the falling edge appears, an interrupt occurs and it\u2019s printed to serial. Simple \ud83d\ude42 <a href=\"https:\/\/github.com\/lamik\/RTC_F401\/tree\/timestamp\" target=\"_blank\" rel=\"noopener\">You can find the full example code here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tamper in STM32F4<\/h2>\n\n\n\n<p>I already mentioned this function in the context of the F1 articles because it\u2019s present there as well. Its operation consists in clearing the backup registers after detecting an edge on a dedicated pin. It\u2019s a kind of anti-burglar. When someone tries to break into a device and opens the enclosure, we can automatically erase sensitive data from battery-backed registers.<\/p>\n\n\n\n<p>To set Tamper, it\u2019s enough to check the appropriate box in Cube.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"759\" height=\"299\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube.png\" alt=\"\" class=\"wp-image-1460\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube.png 759w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube-300x118.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube-24x9.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube-36x14.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube-160x63.png 160w\" sizes=\"auto, (max-width: 759px) 100vw, 759px\" \/><\/figure>\n<\/div>\n\n\n<p>Here too, a configured pin will appear on the microcontroller diagram.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"395\" height=\"297\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube2.png\" alt=\"\" class=\"wp-image-1461\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube2.png 395w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube2-300x226.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube2-24x18.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube2-36x27.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube2-106x80.png 106w\" sizes=\"auto, (max-width: 395px) 100vw, 395px\" \/><\/figure>\n<\/div>\n\n\n<p>And of course, new options appeared in the RTC configuration<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"753\" height=\"149\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube3.png\" alt=\"\" class=\"wp-image-1462\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube3.png 753w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube3-300x59.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube3-24x5.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube3-36x7.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_cube3-160x32.png 160w\" sizes=\"auto, (max-width: 753px) 100vw, 753px\" \/><\/figure>\n<\/div>\n\n\n<p>These are mainly pin behavior options.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Filter \u2013 you can decide on which edge the registers will be cleared.<\/li>\n\n\n\n<li>Sampling Frequency \u2013 how often the pin should be checked. The visible setting means checking the pin once per second.<\/li>\n\n\n\n<li>Precharge Duration \u2013 precharging the pin to a high state. A kind of pull-up without a resistor, if I understand it correctly. <strong>Note \u2013 to PC13 in <a href=\"https:\/\/sklep.msalamon.pl\/produkt\/stm32f401ccu6-dev-board\/?utm_source=blog&amp;utm_medium=article&amp;utm_campaign=rtc_features&amp;utm_content=Text\">BlackPill<\/a> an LED is connected, which will immediately discharge this charge!<\/strong><\/li>\n\n\n\n<li>Tamper PullUp \u2013 whether to use this precharge.<\/li>\n\n\n\n<li>Time Stamp On Tamper Detection \u2013 a combination with Time Stamp<\/li>\n\n\n\n<li>Tamper 1 Trigger \u2013 which edge the Tamper module should react to.<\/li>\n<\/ul>\n\n\n\n<p>The event that triggers Tamper not only erases the contents of the backup registers, but it can also trigger an interrupt. In the example I use it to set Tamper again.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">  while (1)\n  {\n\t  HAL_RTC_GetTime(&amp;amp;hrtc, &amp;amp;RtcTime, RTC_FORMAT_BIN);\n\t  Milliseconds = ((RtcTime.SecondFraction-RtcTime.SubSeconds)\/((float)RtcTime.SecondFraction+1) * 100);\n\t  HAL_RTC_GetDate(&amp;amp;hrtc, &amp;amp;RtcDate, RTC_FORMAT_BIN);\n\n\t  if(RtcTime.Seconds != CompareSeconds)\n\t  {\n\t\t  MessageLen = sprintf((char*)Message, \"Date: %02d.%02d.20%02d Time: %02d:%02d:%02d:%02d\\n\\r\", RtcDate.Date, RtcDate.Month, RtcDate.Year, RtcTime.Hours, RtcTime.Minutes, RtcTime.Seconds, Milliseconds);\n\t\t  HAL_UART_Transmit(&amp;amp;huart2, Message, MessageLen, 100);\n\t\t  CompareSeconds = RtcTime.Seconds;\n\t  }\n\n\t  if(TamperFlag == 1)\n\t  {\n\n\t\t  HAL_RTCEx_GetTimeStamp(&amp;amp;hrtc, &amp;amp;RtcTimeTimeStamp, &amp;amp;RtcDateTimeStamp, RTC_FORMAT_BIN);\n\t\t  Milliseconds = ((RtcTime.SecondFraction-RtcTimeTimeStamp.SubSeconds)\/((float)RtcTime.SecondFraction+1) * 100);\n\n\t\t  MessageLen = sprintf((char*)Message, \"Tamper detected at Time Stamp %02d.%02d.20%02d Time: %02d:%02d:%02d:%02d!\\n\\r\",RtcDateTimeStamp.Date, RtcDateTimeStamp.Month, RtcDateTimeStamp.Year,\n\t\t\t\t  RtcTimeTimeStamp.Hours, RtcTimeTimeStamp.Minutes, RtcTimeTimeStamp.Seconds, Milliseconds);\n\t\t  HAL_UART_Transmit(&amp;amp;huart2, Message, MessageLen, 100);\n\n\t\t  MessageLen = sprintf((char*)Message, \"Backup Registers cleared:\\n\\r\");\n\t\t  HAL_UART_Transmit(&amp;amp;huart2, Message, MessageLen, 100);\n\n\t\t  for(int i = 0; i &amp;lt; BACKUP_COUNT; i++)\n\t\t  {\n\t\t\t  uint32_t tmp = HAL_RTCEx_BKUPRead(&amp;amp;hrtc, aBKPDataReg[i]);\n\t\t\t  MessageLen = sprintf((char*)Message, \"%X, \", tmp);\n\t\t\t  HAL_UART_Transmit(&amp;amp;huart2, Message, MessageLen, 100);\n\t\t  }\n\n\t\t  MessageLen = sprintf((char*)Message, \"\\n\\r\");\n\t\t  HAL_UART_Transmit(&amp;amp;huart2, Message, MessageLen, 100);\n\n\t\t  MessageLen = sprintf((char*)Message, \"Write example data to regs:\\n\\r\");\n\t\t  HAL_UART_Transmit(&amp;amp;huart2, Message, MessageLen, 100);\n\n\t\t  for(int i = 0; i &amp;lt; BACKUP_COUNT; i++)\n\t\t  {\n\t\t\t  HAL_RTCEx_BKUPWrite(&amp;amp;hrtc, aBKPDataReg[i], (i * 32));\n\t\t\t  uint32_t tmp = HAL_RTCEx_BKUPRead(&amp;amp;hrtc, aBKPDataReg[i]);\n\t\t\t  MessageLen = sprintf((char*)Message, \"%X, \", tmp);\n\t\t\t  HAL_UART_Transmit(&amp;amp;huart2, Message, MessageLen, 100);\n\t\t  }\n\n\t\t  MessageLen = sprintf((char*)Message, \"\\n\\rWait for Tamper\\n\\r\\n\\r\");\n\t\t  HAL_UART_Transmit(&amp;amp;huart2, Message, MessageLen, 100);\n\n\t\t  RTC_TamperSet();\n\t\t  TamperFlag = 0;\n\t  }\n\n\t  if(GPIO_PIN_RESET == HAL_GPIO_ReadPin(TEST_GPIO_Port, TEST_Pin))\n\t  {\n\t\t while(GPIO_PIN_RESET == HAL_GPIO_ReadPin(TEST_GPIO_Port, TEST_Pin))\n\t\t {}\n\t\t SetRTC();\n\t  }\n    \/* USER CODE END WHILE *\/\n\n    \/* USER CODE BEGIN 3 *\/\n  }<\/pre>\n\n\n\n<p>In the main loop I set the registers and wait for the Tamper event. I connected a button and an external Pull Up to the board so that it works correctly (not that precharged Pull Up, because there\u2019s an LED). After the Tamper interrupt occurs, I only set a flag, and then after checking it I print the register contents, fill them again, and set Tamper once more.<\/p>\n\n\n\n<p>Here is the result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"688\" height=\"440\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper.png\" alt=\"\" class=\"wp-image-1463\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper.png 688w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper-300x192.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper-24x15.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper-36x23.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper-125x80.png 125w\" sizes=\"auto, (max-width: 688px) 100vw, 688px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The registers are nicely cleared \ud83d\ude42 <a href=\"https:\/\/github.com\/lamik\/RTC_F401\/tree\/tamper\" target=\"_blank\" rel=\"noopener\">You can find the full example code here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tamper + Time Stamp in STM32F4<\/h2>\n\n\n\n<p>In the Tamper setup there was something like <em>Time Stamp On Tamper Detection. <\/em>During normal Tamper operation, we clear the registers and that\u2019s it.<\/p>\n\n\n\n<p>Sometimes, however, there may be a need where you\u2019d like to know a bit more about this break-in. For example, when it happened. That\u2019s exactly what combining these two functions enables \u2013 clearing and recording the date and time of the event.<\/p>\n\n\n\n<p>It\u2019s enough to set this one bit in the Tamper configuration to combine these two functions.<\/p>\n\n\n\n<p>Then, in the Tamper handler, it\u2019s enough to read the special Time Stamp registers, which store the information about when it happened and\u2026 that\u2019s it \ud83d\ude42 Two birds with one stone. Simple, right?<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"688\" height=\"450\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_timestamp.png\" alt=\"\" class=\"wp-image-1464\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_timestamp.png 688w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_timestamp-300x196.png 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_timestamp-24x16.png 24w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_timestamp-36x24.png 36w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/03\/rtcf4_tamper_timestamp-122x80.png 122w\" sizes=\"auto, (max-width: 688px) 100vw, 688px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/lamik\/RTC_F401\/tree\/tamper_timestamp\" target=\"_blank\" rel=\"noopener\">You can find the full example code here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><figure><a href=\"https:\/\/sklep.msalamon.pl\/kategoria-produktu\/dev-boardy\/stm32-nucleo\/?utm_source=blog&amp;utm_medium=banner&amp;utm_campaign=rtc_features&amp;utm_content=nucleo\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/msalamon.pl\/wp-content\/uploads\/2020\/07\/Nucleo-64-baner.jpg\" alt=\"\" width=\"1200\" height=\"400\" class=\"aligncenter wp-image-1593 size-full\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/07\/Nucleo-64-baner.jpg 1200w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/07\/Nucleo-64-baner-300x100.jpg 300w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/07\/Nucleo-64-baner-1024x341.jpg 1024w, https:\/\/msalamon.pl\/wp-content\/uploads\/2020\/07\/Nucleo-64-baner-768x256.jpg 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\"><\/a><\/figure><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>As you can see, using the additional features of the built-in RTC is not complicated. Remember that these functions, like the entire clock module, work during battery power. This means that a \u201cbreak-in\u201d during battery operation is recorded in special registers intended for these functions. This is a very valuable property because we don\u2019t always want the MCU to be running at full speed.<\/p>\n\n\n\n<p>In the next post I\u2019ll cover functions related to waking up the microcontroller and\u2026 a human \ud83d\ude42<\/p>\n\n\n\n<p>You\u2019ll find the full project along with the library as usual on my GitHub: <a href=\"https:\/\/github.com\/lamik\/RTC_F401\" target=\"_blank\" rel=\"noopener\">LINK<\/a><\/p>\n\n\n\n<p><span>If you noticed an error, disagree with something, would like to add something important, or simply feel like you\u2019d like to discuss this topic, write a comment. Remember that the discussion should be polite and follow the rules of the Polish language.<\/span><\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;4263&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;0&quot;,&quot;greet&quot;:&quot;&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Additional RTC features in STM32F4 part 1&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            <span class=\"kksr-muted\"><\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>The RTC clock you\u2019ll find in STM32 microcontrollers is used not only for simple timekeeping. Besides this basic function, it also has several other more or less useful ones. I\u2019ve already shown you the backup registers, which can be used to store sensitive data or remember the date in a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3428,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[160],"tags":[175,178,174],"class_list":["post-4263","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stm32","tag-electronics","tag-kursstm32","tag-stm32"],"_links":{"self":[{"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/posts\/4263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/comments?post=4263"}],"version-history":[{"count":3,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/posts\/4263\/revisions"}],"predecessor-version":[{"id":4374,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/posts\/4263\/revisions\/4374"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/media\/3428"}],"wp:attachment":[{"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/media?parent=4263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/categories?post=4263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/tags?post=4263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}