Skip to content

Commit b7a455e

Browse files
committed
Add rtc files to fix reading ota configuration when SecondaryBlockDevice is created
1 parent e729dd7 commit b7a455e

File tree

6 files changed

+149
-113
lines changed

6 files changed

+149
-113
lines changed

envie/ota.cpp

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
11
#if MCUBOOT_AS_ENVIE
22

33
#include "ota.h"
4+
#include "rtc.h"
5+
#include "bootutil/bootutil_log.h"
46

5-
//static storageType _storage_type = INVALID;
6-
//static uint32_t _data_offset = 0;
7-
//static uint32_t _update_size = 0;
8-
9-
static storageType _storage_type = SD_FATFS;
10-
static uint32_t _data_offset = 2;
11-
static uint32_t _update_size = MCUBOOT_SLOT_SIZE;
7+
#define SD_OTA_TEST 1
128

9+
void getOTAData(enum storageType* storage_type, uint32_t* data_offset, uint32_t* update_size) {
10+
RTCInit();
11+
/*
12+
* Magic 0x07AA is set by Arduino_Portenta_OTA
13+
* Magic 0xDF59 is set by the loader if RESET_REASON_PIN_RESET
14+
*/
1315

14-
void setOTAData(enum storageType storage_type, uint32_t data_offset, uint32_t update_size) {
15-
_storage_type = storage_type;
16-
_data_offset = data_offset;
17-
_update_size = update_size;
16+
#if SD_OTA_TEST
17+
*storage_type = SD_FATFS;
18+
*data_offset = 2;
19+
*update_size = MCUBOOT_SLOT_SIZE;
20+
return;
21+
#endif
1822

19-
}
20-
void getOTAData(enum storageType* storage_type, uint32_t* data_offset, uint32_t* update_size) {
21-
if(_storage_type == INVALID) {
23+
int magic = RTCGetBKPRegister(RTC_BKP_DR0);
24+
if (magic == 0x07AA) {
25+
// DR1 contains the backing storage type, DR2 the offset in case of raw device / MBR
26+
*storage_type = (storageType)RTCGetBKPRegister(RTC_BKP_DR1);
27+
*data_offset = RTCGetBKPRegister(RTC_BKP_DR2);
28+
*update_size = RTCGetBKPRegister(RTC_BKP_DR3);
29+
//BOOT_LOG_INF("Custom OTA data");
30+
} else {
2231
*storage_type = QSPI_FLASH_FATFS_MBR;
2332
*data_offset = 2;
2433
*update_size = MCUBOOT_SLOT_SIZE;
25-
} else {
26-
*storage_type = _storage_type;
27-
*data_offset = _data_offset;
28-
*update_size = _update_size;
34+
//BOOT_LOG_INF("Default OTA data");
2935
}
36+
return;
37+
38+
//BOOT_LOG_INF("Storage type %d",(int)*storage_type);
39+
//BOOT_LOG_INF("Offset %d",*data_offset);
40+
//BOOT_LOG_INF("Update size %d",*update_size);
3041
}
3142

3243
#endif

envie/ota.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ enum storageType {
3333
#define NO_OTA_FILE (-3)
3434
#define INIT_FAILED (-4)
3535

36-
//int tryOTA(enum storageType storage_type, uint32_t data_offset, uint32_t update_size);
37-
void setOTAData(enum storageType storage_type, uint32_t data_offset, uint32_t update_size);
3836
void getOTAData(enum storageType* storage_type, uint32_t* data_offset, uint32_t* update_size);
3937

40-
#endif //__OTA_H
38+
#endif //__OTA_H

envie/rtc.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#if MCUBOOT_AS_ENVIE
2+
3+
#include "rtc.h"
4+
#include "bootutil/bootutil_log.h"
5+
6+
RTC_HandleTypeDef RtcHandle;
7+
static int rtc_initialized = 0;
8+
9+
10+
void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
11+
{
12+
RCC_OscInitTypeDef RCC_OscInitStruct;
13+
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
14+
15+
/*##-1- Configure LSE as RTC clock source ##################################*/
16+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
17+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
18+
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
19+
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
20+
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
21+
{
22+
return;
23+
}
24+
25+
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
26+
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
27+
if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
28+
{
29+
return;
30+
}
31+
32+
/*##-2- Enable RTC peripheral Clocks #######################################*/
33+
/* Enable RTC Clock */
34+
__HAL_RCC_RTC_ENABLE();
35+
}
36+
37+
#define RTC_ASYNCH_PREDIV 0x7F /* LSE as RTC clock */
38+
#define RTC_SYNCH_PREDIV 0x00FF /* LSE as RTC clock */
39+
40+
void RTC_CalendarBkupInit(void)
41+
{
42+
43+
/*##-1- Configure the RTC peripheral #######################################*/
44+
/* Configure RTC prescaler and RTC data registers */
45+
/* RTC configured as follow:
46+
- Hour Format = Format 24
47+
- Asynch Prediv = Value according to source clock
48+
- Synch Prediv = Value according to source clock
49+
- OutPut = Output Disable
50+
- OutPutPolarity = High Polarity
51+
- OutPutType = Open Drain */
52+
RtcHandle.Instance = RTC;
53+
RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
54+
RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
55+
RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
56+
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
57+
RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
58+
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
59+
60+
if(HAL_RTC_Init(&RtcHandle) != HAL_OK)
61+
{
62+
BOOT_LOG_ERR("HAL_RTC_Init");
63+
return;
64+
}
65+
66+
rtc_initialized = 1;
67+
}
68+
69+
void RTCInit() {
70+
if(!rtc_initialized) {
71+
HAL_RTC_MspInit(&RtcHandle);
72+
RTC_CalendarBkupInit();
73+
}
74+
}
75+
76+
uint32_t RTCGetBKPRegister(uint32_t BackupRegister) {
77+
if(rtc_initialized) {
78+
return HAL_RTCEx_BKUPRead(&RtcHandle, BackupRegister);
79+
} else {
80+
BOOT_LOG_ERR("RTCGetBKPRegister");
81+
return -1;
82+
}
83+
}
84+
85+
uint32_t RTCSetBKPRegister(uint32_t BackupRegister, uint32_t Data) {
86+
if(rtc_initialized) {
87+
HAL_RTCEx_BKUPWrite(&RtcHandle, BackupRegister, Data);
88+
return 0;
89+
} else {
90+
BOOT_LOG_ERR("RTCSetBKPRegister");
91+
return -1;
92+
}
93+
}
94+
95+
#endif

envie/rtc.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __RTC_H
2+
#define __RTC_H
3+
4+
#include <stdint.h>
5+
#include "stm32h7xx_hal.h"
6+
#include "stm32h7xx_hal_rtc.h"
7+
8+
void RTCInit();
9+
uint32_t RTCGetBKPRegister(uint32_t BackupRegister);
10+
uint32_t RTCSetBKPRegister(uint32_t BackupRegister, uint32_t Data);
11+
12+
#endif //__RTC_H

envie/target_init.cpp

Lines changed: 12 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "mbed.h"
44
#include "target_init.h"
55
#include "ota.h"
6+
#include "rtc.h"
67
#include "bootutil/bootutil_log.h"
78

89
// clock source is selected with CLOCK_SOURCE in json config
@@ -30,8 +31,6 @@ volatile uint8_t ledTargetValue = 20;
3031
volatile int8_t ledDirection = 1;
3132
volatile int divisor = 0;
3233

33-
RTC_HandleTypeDef RtcHandle;
34-
3534
DigitalOut led(PK_6);
3635

3736
static inline void LED_pulse(DigitalOut* led)
@@ -40,8 +39,8 @@ static inline void LED_pulse(DigitalOut* led)
4039
return;
4140
}
4241

43-
if (HAL_GetTick() > 500 && double_tap_flag && HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR0) == 0xDF59) {
44-
HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_DR0, 0);
42+
if (HAL_GetTick() > 500 && double_tap_flag && RTCGetBKPRegister(RTC_BKP_DR0) == 0xDF59) {
43+
RTCSetBKPRegister(RTC_BKP_DR0, 0);
4544
double_tap_flag = false;
4645
}
4746

@@ -61,77 +60,20 @@ static inline void LED_pulse(DigitalOut* led)
6160
}
6261
}
6362

64-
void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
65-
{
66-
RCC_OscInitTypeDef RCC_OscInitStruct;
67-
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
68-
69-
/*##-1- Configure LSE as RTC clock source ##################################*/
70-
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
71-
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
72-
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
73-
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
74-
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
75-
{
76-
return;
77-
}
78-
79-
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
80-
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
81-
if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
82-
{
83-
return;
84-
}
85-
86-
/*##-2- Enable RTC peripheral Clocks #######################################*/
87-
/* Enable RTC Clock */
88-
__HAL_RCC_RTC_ENABLE();
89-
}
90-
91-
#define RTC_ASYNCH_PREDIV 0x7F /* LSE as RTC clock */
92-
#define RTC_SYNCH_PREDIV 0x00FF /* LSE as RTC clock */
93-
94-
void RTC_CalendarBkupInit(void)
95-
{
96-
97-
/*##-1- Configure the RTC peripheral #######################################*/
98-
/* Configure RTC prescaler and RTC data registers */
99-
/* RTC configured as follow:
100-
- Hour Format = Format 24
101-
- Asynch Prediv = Value according to source clock
102-
- Synch Prediv = Value according to source clock
103-
- OutPut = Output Disable
104-
- OutPutPolarity = High Polarity
105-
- OutPutType = Open Drain */
106-
RtcHandle.Instance = RTC;
107-
RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
108-
RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
109-
RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
110-
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
111-
RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
112-
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
113-
114-
if(HAL_RTC_Init(&RtcHandle) != HAL_OK)
115-
{
116-
}
117-
}
11863

11964
int target_init(void) {
12065
DigitalIn boot_sel(PI_8,PullDown);
12166

122-
HAL_RTC_MspInit(&RtcHandle);
123-
RTC_CalendarBkupInit();
124-
125-
int magic = HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR0);
126-
BOOT_LOG_DBG("Envie magic 0x%x");
67+
int magic = RTCGetBKPRegister(RTC_BKP_DR0);
68+
BOOT_LOG_DBG("Envie magic 0x%x", magic);
12769

12870
// in case we have been reset let's wait 500 ms to see if user is trying to stay in bootloader
12971
if (ResetReason::get() == RESET_REASON_PIN_RESET) {
13072
// now that we've been reset let's set magic. resetting with this set will
13173
// flag we need to stay in bootloader.
132-
HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_DR0, 0xDF59);
74+
RTCSetBKPRegister(RTC_BKP_DR0, 0xDF59);
13375
HAL_Delay(500);
134-
BOOT_LOG_DBG("Envie magic set 0x%x", HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR0));
76+
BOOT_LOG_DBG("Envie magic set 0x%x", RTCGetBKPRegister( RTC_BKP_DR0));
13577
}
13678

13779
DigitalOut usb_reset(PJ_4, 0);
@@ -223,24 +165,6 @@ int target_init(void) {
223165

224166
HAL_Delay(10);
225167

226-
if (magic == 0x07AA) {
227-
// DR1 contains the backing storage type, DR2 the offset in case of raw device / MBR
228-
storageType storage_type = (storageType)HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR1);
229-
uint32_t offset = HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR2);
230-
uint32_t update_size = HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR3);
231-
//int ota_result =
232-
setOTAData(storage_type, offset, update_size);
233-
/*if (ota_result == 0) {
234-
// clean reboot with success flag
235-
HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_DR0, 0);
236-
HAL_FLASH_Lock();
237-
// wait for external reboot
238-
while (1) {}
239-
} else {
240-
HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_DR0, ota_result);
241-
}*/
242-
}
243-
244168
/* Test if user code is programmed starting from USBD_DFU_APP_DEFAULT_ADD
245169
* address. TODO check MCUBoot header instead.
246170
*/
@@ -250,13 +174,13 @@ int target_init(void) {
250174
|| (((*(__IO uint32_t *) 0x08040000) & 0xFF000000) == 0x38000000);
251175

252176
if (app_valid && magic != 0xDF59 && magic != 0x07AA && boot_sel==0) {
253-
HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_DR0, 0);
177+
RTCSetBKPRegister(RTC_BKP_DR0, 0);
254178
HAL_FLASH_Lock();
255-
BOOT_LOG_DBG("Envie app magic 0x%x", HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR0));
179+
BOOT_LOG_DBG("Envie app magic 0x%x", RTCGetBKPRegister(RTC_BKP_DR0));
256180
return 0;
257181

258182
} else {
259-
BOOT_LOG_DBG("Envie loop magic 0x%x", HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR0));
183+
BOOT_LOG_DBG("Envie loop magic 0x%x", RTCGetBKPRegister(RTC_BKP_DR0));
260184
return 1;
261185
}
262186
}
@@ -272,13 +196,10 @@ extern "C" {
272196
}
273197

274198
void envie_loop(void) {
275-
276-
BOOT_LOG_INF("Application not found. Starting boot loop\n");
277-
278-
HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_DR0, 0);
199+
RTCSetBKPRegister(RTC_BKP_DR0, 0);
279200

280201
SetSysClock_PLL_HSE(1, false);
281-
SystemCoreClockUpdate();;
202+
SystemCoreClockUpdate();
282203

283204
//turnDownEthernet();
284205

envie/target_init.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define __TARGET_INIT_H
33

44
#include "stm32h7xx_hal.h"
5-
#include "stm32h7xx_hal_rtc.h"
65
#include "stm32h7xx_hal_mdma.h"
76
#include "stm32h7xx_hal_qspi.h"
87
#if MCUBOOT_ENVIE_DFU

0 commit comments

Comments
 (0)