Skip to content

Commit 2bc49a7

Browse files
committed
mcu/stm32: Fix hal_nvreg for STM32F1
This fixes 3 issues for hal_nvreg for STM32F1 devices. - STM32F1 registers are 16 bits not 32 like in other series. - Backup registers for ST HAL for STM32F1 start from 1 not 0. - PWR clock needs to be turned on if it was not before Signed-off-by: Jerzy Kasenberg <[email protected]>
1 parent a52f357 commit 2bc49a7

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

hw/mcu/stm/stm32_common/src/hal_nvreg.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* under the License.
1818
*/
1919

20+
#include <stdbool.h>
21+
#include <os/mynewt.h>
2022
#include <hal/hal_nvreg.h>
2123
#include <mcu/stm32_hal.h>
2224

@@ -30,23 +32,38 @@
3032
#define HAL_NVREG_MAX (0)
3133
#endif
3234

35+
#if MYNEWT_VAL_MCU_STM32F1
36+
#define HAL_NVREG_WIDTH_BYTES 2
37+
#define HAL_NVREG_START_INDEX 1
38+
#else
3339
/* RTC backup registers are 32-bits wide */
3440
#define HAL_NVREG_WIDTH_BYTES (4)
41+
#define HAL_NVREG_START_INDEX 0
42+
#endif
3543

3644
void
3745
hal_nvreg_write(unsigned int reg, uint32_t val)
3846
{
3947
#if PWR_ENABLED
4048
RTC_HandleTypeDef hrtc = { .Instance = RTC };
4149
if (reg < HAL_NVREG_MAX) {
50+
#if defined(__HAL_RCC_PWR_IS_CLK_ENABLED)
51+
bool disable_pwr = __HAL_RCC_PWR_IS_CLK_ENABLED();
52+
__HAL_RCC_PWR_CLK_ENABLE();
53+
#endif
4254
#if defined(__HAL_RCC_BKP_CLK_ENABLE)
4355
__HAL_RCC_BKP_CLK_ENABLE();
4456
#endif
4557
HAL_PWR_EnableBkUpAccess();
46-
HAL_RTCEx_BKUPWrite(&hrtc, reg, val);
58+
HAL_RTCEx_BKUPWrite(&hrtc, reg + HAL_NVREG_START_INDEX, val);
4759
HAL_PWR_DisableBkUpAccess();
4860
#if defined(__HAL_RCC_BKP_CLK_DISABLE)
4961
__HAL_RCC_BKP_CLK_DISABLE();
62+
#endif
63+
#if defined(__HAL_RCC_PWR_IS_CLK_ENABLED)
64+
if (disable_pwr) {
65+
__HAL_RCC_PWR_CLK_DISABLE();
66+
}
5067
#endif
5168
}
5269
#endif
@@ -60,7 +77,7 @@ hal_nvreg_read(unsigned int reg)
6077
RTC_HandleTypeDef hrtc = { .Instance = RTC };
6178
if (reg < HAL_NVREG_MAX) {
6279
HAL_PWR_EnableBkUpAccess();
63-
val = HAL_RTCEx_BKUPRead(&hrtc, reg);
80+
val = HAL_RTCEx_BKUPRead(&hrtc, reg + HAL_NVREG_START_INDEX);
6481
HAL_PWR_DisableBkUpAccess();
6582
}
6683
#endif

0 commit comments

Comments
 (0)