|
| 1 | +/** |
| 2 | + ****************************************************************************** |
| 3 | + * @file backup.h |
| 4 | + * @author fpistm |
| 5 | + * @brief Header for backup domain driver |
| 6 | + ****************************************************************************** |
| 7 | + * @attention |
| 8 | + * |
| 9 | + * <h2><center>© COPYRIGHT(c) 2019 STMicroelectronics</center></h2> |
| 10 | + * |
| 11 | + * Redistribution and use in source and binary forms, with or without modification, |
| 12 | + * are permitted provided that the following conditions are met: |
| 13 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer. |
| 15 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 16 | + * this list of conditions and the following disclaimer in the documentation |
| 17 | + * and/or other materials provided with the distribution. |
| 18 | + * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| 19 | + * may be used to endorse or promote products derived from this software |
| 20 | + * without specific prior written permission. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 23 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 26 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 28 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 30 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | + * |
| 33 | + ****************************************************************************** |
| 34 | + */ |
| 35 | + |
| 36 | +/* Define to prevent recursive inclusion -------------------------------------*/ |
| 37 | +#ifndef __BACKUP_H |
| 38 | +#define __BACKUP_H |
| 39 | + |
| 40 | +/* Includes ------------------------------------------------------------------*/ |
| 41 | +#include "stm32_def.h" |
| 42 | +#include "stm32yyxx_ll_rtc.h" |
| 43 | + |
| 44 | +#ifdef __cplusplus |
| 45 | +extern "C" { |
| 46 | +#endif |
| 47 | + |
| 48 | +/* Exported macro ------------------------------------------------------------*/ |
| 49 | +#if (!defined(STM32F0xx) && !defined(STM32F3xx) && !defined(STM32L0xx) &&\ |
| 50 | + !defined(STM32L1xx) && !defined(STM32L4xx)) || defined(RTC_BACKUP_SUPPORT) |
| 51 | +#if !defined(STM32L412xx) && !defined(STM32L422xx) |
| 52 | +#define ENABLE_BACKUP_SUPPORT |
| 53 | +#endif |
| 54 | +#endif |
| 55 | + |
| 56 | +#if !defined(RTC_BKP_INDEX) && defined(ENABLE_BACKUP_SUPPORT) |
| 57 | +#define RTC_BKP_INDEX LL_RTC_BKP_DR1 |
| 58 | +#else |
| 59 | +#define RTC_BKP_INDEX 0 |
| 60 | +#endif |
| 61 | +#ifndef RTC_BKP_VALUE |
| 62 | +#define RTC_BKP_VALUE 0x32F2 |
| 63 | +#endif |
| 64 | + |
| 65 | + |
| 66 | +/* Exported functions ------------------------------------------------------- */ |
| 67 | +static inline void resetBackupRegister(void) |
| 68 | +{ |
| 69 | +#ifdef HAL_PWR_MODULE_ENABLED |
| 70 | + /* Enable access to the RTC registers */ |
| 71 | + HAL_PWR_EnableBkUpAccess(); |
| 72 | + /** |
| 73 | + * Write twice the value to flush the APB-AHB bridge |
| 74 | + * This bit shall be written in the register before writing the next one |
| 75 | + */ |
| 76 | + HAL_PWR_EnableBkUpAccess(); |
| 77 | +#endif |
| 78 | + __HAL_RCC_BACKUPRESET_FORCE(); |
| 79 | + __HAL_RCC_BACKUPRESET_RELEASE(); |
| 80 | +} |
| 81 | + |
| 82 | +static inline void enableBackupRegister(void) |
| 83 | +{ |
| 84 | + /* Enable Power Clock */ |
| 85 | +#ifndef STM32H7xx |
| 86 | + if (__HAL_RCC_PWR_IS_CLK_DISABLED()) { |
| 87 | + __HAL_RCC_PWR_CLK_ENABLE(); |
| 88 | + } |
| 89 | +#endif |
| 90 | +#ifdef HAL_PWR_MODULE_ENABLED |
| 91 | + /* Allow access to Backup domain */ |
| 92 | + HAL_PWR_EnableBkUpAccess(); |
| 93 | +#endif |
| 94 | +#ifdef STM32F1xx |
| 95 | + /* Enable BKP CLK enable for backup registers */ |
| 96 | + __HAL_RCC_BKP_CLK_ENABLE(); |
| 97 | +#endif |
| 98 | +} |
| 99 | + |
| 100 | +static inline void disableBackupRegister(void) |
| 101 | +{ |
| 102 | +#ifdef HAL_PWR_MODULE_ENABLED |
| 103 | + /* Forbid access to Backup domain */ |
| 104 | + HAL_PWR_DisableBkUpAccess(); |
| 105 | +#endif |
| 106 | +#ifdef STM32F1xx |
| 107 | + /* Disable BKP CLK enable for backup registers */ |
| 108 | + __HAL_RCC_BKP_CLK_DISABLE(); |
| 109 | +#endif |
| 110 | + /* Disable Power Clock */ |
| 111 | +#ifndef STM32H7xx |
| 112 | + if (!__HAL_RCC_PWR_IS_CLK_DISABLED()) { |
| 113 | + __HAL_RCC_PWR_CLK_DISABLE(); |
| 114 | + } |
| 115 | +#endif |
| 116 | +} |
| 117 | + |
| 118 | +static inline void setBackupRegister(uint32_t index, uint32_t value) |
| 119 | +{ |
| 120 | + |
| 121 | +#if defined(STM32F1xx) |
| 122 | + LL_RTC_BKP_SetRegister(BKP, index, value); |
| 123 | +#else |
| 124 | +#ifdef ENABLE_BACKUP_SUPPORT |
| 125 | + LL_RTC_BAK_SetRegister(RTC, index, value); |
| 126 | +#else |
| 127 | + UNUSED(index); |
| 128 | + UNUSED(value); |
| 129 | +#endif |
| 130 | +#endif |
| 131 | +} |
| 132 | + |
| 133 | +static inline uint32_t getBackupRegister(uint32_t index) |
| 134 | +{ |
| 135 | +#if defined(STM32F1xx) |
| 136 | + return LL_RTC_BKP_GetRegister(BKP, index); |
| 137 | +#else |
| 138 | +#ifdef ENABLE_BACKUP_SUPPORT |
| 139 | + return LL_RTC_BAK_GetRegister(RTC, index); |
| 140 | +#else |
| 141 | + UNUSED(index); |
| 142 | + return 0; |
| 143 | +#endif |
| 144 | +#endif |
| 145 | +} |
| 146 | + |
| 147 | +#ifdef __cplusplus |
| 148 | +} |
| 149 | +#endif |
| 150 | + |
| 151 | +#endif /* __BACKUP_H */ |
| 152 | + |
| 153 | +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
0 commit comments