Refactor missing prototypes 2 (#14170)
[betaflight.git] / src / platform / APM32 / persistent_apm32.c
blob0a9bda61f0b250c59399fe8937209872592a0fb6
1 /*
2 * This file is part of Betaflight.
4 * Betaflight is free software. You can redistribute this software
5 * and/or modify this software under the terms of the GNU General
6 * Public License as published by the Free Software Foundation,
7 * either version 3 of the License, or (at your option) any later
8 * version.
10 * Betaflight is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this software.
19 * If not, see <http://www.gnu.org/licenses/>.
23 * An implementation of persistent data storage utilizing RTC backup data register.
24 * Retains values written across software resets and boot loader activities.
27 #include <stdint.h>
28 #include "platform.h"
30 #include "drivers/persistent.h"
31 #include "drivers/system.h"
33 #define PERSISTENT_OBJECT_MAGIC_VALUE (('B' << 24)|('e' << 16)|('f' << 8)|('1' << 0))
35 uint32_t persistentObjectRead(persistentObjectId_e id)
37 RTC_HandleTypeDef rtcHandle = { .Instance = RTC };
39 uint32_t value = DAL_RTCEx_BKUPRead(&rtcHandle, id);
41 return value;
44 void persistentObjectWrite(persistentObjectId_e id, uint32_t value)
46 RTC_HandleTypeDef rtcHandle = { .Instance = RTC };
48 DAL_RTCEx_BKUPWrite(&rtcHandle, id, value);
50 #ifdef USE_SPRACING_PERSISTENT_RTC_WORKAROUND
51 // Also write the persistent location used by the bootloader to support DFU etc.
52 if (id == PERSISTENT_OBJECT_RESET_REASON) {
53 // SPRACING firmware sometimes enters DFU mode when MSC mode is requested
54 if (value == RESET_MSC_REQUEST) {
55 value = RESET_NONE;
57 DAL_RTCEx_BKUPWrite(&rtcHandle, PERSISTENT_OBJECT_RESET_REASON_FWONLY, value);
59 #endif
62 static void persistentObjectRTCEnable(void)
64 RTC_HandleTypeDef rtcHandle = { .Instance = RTC };
66 __DAL_RCM_PMU_CLK_ENABLE(); // Enable Access to PMU
68 DAL_PMU_EnableBkUpAccess(); // Disable backup domain protection
70 // We don't need a clock source for RTC itself. Skip it.
72 __DAL_RTC_WRITEPROTECTION_ENABLE(&rtcHandle); // Reset sequence
73 __DAL_RTC_WRITEPROTECTION_DISABLE(&rtcHandle); // Apply sequence
76 void persistentObjectInit(void)
78 // Configure and enable RTC for backup register access
80 persistentObjectRTCEnable();
82 // XXX Magic value checking may be sufficient
84 uint32_t wasSoftReset;
86 wasSoftReset = RCM->CSTS & RCM_CSTS_SWRSTFLG;
88 if (!wasSoftReset || (persistentObjectRead(PERSISTENT_OBJECT_MAGIC) != PERSISTENT_OBJECT_MAGIC_VALUE)) {
89 for (int i = 1; i < PERSISTENT_OBJECT_COUNT; i++) {
90 persistentObjectWrite(i, 0);
92 persistentObjectWrite(PERSISTENT_OBJECT_MAGIC, PERSISTENT_OBJECT_MAGIC_VALUE);