Skip to content

[Portenta] Finally fix RTC drift and read proper information from the board #563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bootloaders/PORTENTA_H7/portentah7_bootloader_mbed_hs_v2.bin
Binary file not shown.
Binary file modified bootloaders/PORTENTA_H7/portentah7_bootloader_mbed_hs_v2.elf
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,11 @@ class QSPIFBlockDevice : public mbed::BlockDevice {
QSPIF_BP_CLEAR_SR, // Clear protection bits in status register 1
};

protected:
// QSPI Driver Object
mbed::QSPI _qspi;

private:
// Static List of different QSPI based Flash devices csel that already exist
// Each QSPI Flash device csel can have only 1 QSPIFBlockDevice instance
// _devices_mutex is used to lock csel list - only one QSPIFBlockDevice instance per csel is allowed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
uint8_t* bootloader_data = (uint8_t*)(0x801F000);
uint8_t* bootloader_identification = (uint8_t*)(0x80002F0);

#if __has_include("portenta_info.h")
#include "portenta_info.h"
#define GET_OTP_BOARD_INFO
uint8_t* boardInfo();
#endif

void setup() {
Serial.begin(115200);
while (!Serial) {}
Expand All @@ -23,6 +29,20 @@ void setup() {
Serial.println("QSPI size: " + String(bootloader_data[7]) + " MB");
Serial.println("Has Video output: " + String(bootloader_data[8] == 1 ? "Yes" : "No"));
Serial.println("Has Crypto chip: " + String(bootloader_data[9] == 1 ? "Yes" : "No"));

#ifdef GET_OTP_BOARD_INFO
auto info = *((PortentaBoardInfo*)boardInfo());
if (info.magic == 0xB5) {
Serial.println("Secure info version: " + String(info.version));
Serial.println("Secure board revision: " + String(info.revision >> 8) + "." + String(info.revision & 0xFF));
Serial.println("Secure carrier identification: " + String(info.carrier >> 8) + "." + String(info.revision & 0xFF));
Serial.println("Secure vid: 0x" + String(info.vid, HEX));
Serial.println("Secure pid: 0x" + String(info.pid, HEX));
Serial.println("Secure mac: " + String(info.mac_address[0], HEX) + ":" + String(info.mac_address[1], HEX) + ":" +
String(info.mac_address[2], HEX) + ":" + String(info.mac_address[3], HEX) + ":" +
String(info.mac_address[4], HEX) + ":" + String(info.mac_address[5], HEX));
}
#endif
}

String getUSBSpeed(uint8_t flag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
#include "LittleFileSystem.h"
#include "FATFileSystem.h"
#if defined(ARDUINO_PORTENTA_H7_M7)
#include "portenta_info.h"
#include "portenta_bootloader.h"
#include "portenta_lite_bootloader.h"
#include "portenta_lite_connected_bootloader.h"
#include "mcuboot_bootloader.h"
#include "ecdsa-p256-encrypt-key.h"
#include "ecdsa-p256-signing-key.h"
#define GET_OTP_BOARD_INFO
#elif defined(ARDUINO_NICLA_VISION)
#include "nicla_vision_bootloader.h"
#endif
Expand Down Expand Up @@ -42,6 +44,8 @@ uint8_t* bootloader_identification = (uint8_t*)(BOOTLOADER_ADDR + bootloader_ide
const unsigned char* bootloader_ptr = &bootloader_mbed_bin[0];
long bootloader_len = bootloader_mbed_bin_len;

uint8_t* boardInfo();

void setup() {
Serial.begin(115200);
while (!Serial) {}
Expand All @@ -65,6 +69,20 @@ void setup() {
Serial.println("Has Video output: " + String(bootloader_data[8] == 1 ? "Yes" : "No"));
Serial.println("Has Crypto chip: " + String(bootloader_data[9] == 1 ? "Yes" : "No"));

#ifdef GET_OTP_BOARD_INFO
auto info = *((PortentaBoardInfo*)boardInfo());
if (info.magic == 0xB5) {
Serial.println("Secure info version: " + String(info.version));
Serial.println("Secure board revision: " + String(info.revision >> 8) + "." + String(info.revision & 0xFF));
Serial.println("Secure carrier identification: " + String(info.carrier >> 8) + "." + String(info.revision & 0xFF));
Serial.println("Secure vid: 0x" + String(info.vid, HEX));
Serial.println("Secure pid: 0x" + String(info.pid, HEX));
Serial.println("Secure mac: " + String(info.mac_address[0], HEX) + ":" + String(info.mac_address[1], HEX) + ":" +
String(info.mac_address[2], HEX) + ":" + String(info.mac_address[3], HEX) + ":" +
String(info.mac_address[4], HEX) + ":" + String(info.mac_address[5], HEX));
}
#endif

video_available = bootloader_data[8];
wifi_available = bootloader_data[5];

Expand Down
21,099 changes: 10,580 additions & 10,519 deletions libraries/STM32H747_System/examples/STM32H747_manageBootloader/portenta_bootloader.h

Large diffs are not rendered by default.

21,091 changes: 10,576 additions & 10,515 deletions libraries/STM32H747_System/examples/STM32H747_manageBootloader/portenta_lite_bootloader.h

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions libraries/STM32H747_System/src/Portenta_System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Portenta_System.h"
#include "Wire.h"
#include "mbed.h"
#include "SecureQSPI.h"

#define PMIC_ADDRESS 0x08

Expand Down
2 changes: 2 additions & 0 deletions libraries/STM32H747_System/src/Portenta_System.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Portenta_System: public STM32H747
Portenta_System() {};
virtual bool begin();
virtual bool enterLowPower();
String getBoardRevision();
uint16_t getCarrierSpecs();
};

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
From a316902eaa2b438e71a40f0992fc8094058eaa63 Mon Sep 17 00:00:00 2001
From: Martino Facchin <m.facchin@arduino.cc>
Date: Fri, 23 Sep 2022 09:51:20 +0200
Subject: [PATCH] QSPI: make _qspi object protected to allow subclassing

---
.../COMPONENT_QSPIF/include/QSPIF/QSPIFBlockDevice.h | 2 ++
1 file changed, 2 insertions(+)

diff --git a/storage/blockdevice/COMPONENT_QSPIF/include/QSPIF/QSPIFBlockDevice.h b/storage/blockdevice/COMPONENT_QSPIF/include/QSPIF/QSPIFBlockDevice.h
index 2903ecad32..695a396197 100644
--- a/storage/blockdevice/COMPONENT_QSPIF/include/QSPIF/QSPIFBlockDevice.h
+++ b/storage/blockdevice/COMPONENT_QSPIF/include/QSPIF/QSPIFBlockDevice.h
@@ -340,9 +340,11 @@ private:
QSPIF_BP_CLEAR_SR, // Clear protection bits in status register 1
};

+protected:
// QSPI Driver Object
mbed::QSPI _qspi;

+private:
// Static List of different QSPI based Flash devices csel that already exist
// Each QSPI Flash device csel can have only 1 QSPIFBlockDevice instance
// _devices_mutex is used to lock csel list - only one QSPIFBlockDevice instance per csel is allowed
--
2.37.3

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
From 92907dc0a3dec975e250f753fbc72a2b57639565 Mon Sep 17 00:00:00 2001
From: Martino Facchin <m.facchin@arduino.cc>
Date: Wed, 28 Sep 2022 17:27:56 +0200
Subject: [PATCH 178/179] STM32H747: linker: set bootloader_info section for
all bootloaders

---
.../TARGET_STM32H747xI_CM7/TOOLCHAIN_GCC_ARM/STM32H747xI_CM7.ld | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/targets/TARGET_STM/TARGET_STM32H7/TARGET_STM32H747xI/TARGET_STM32H747xI_CM7/TOOLCHAIN_GCC_ARM/STM32H747xI_CM7.ld b/targets/TARGET_STM/TARGET_STM32H7/TARGET_STM32H747xI/TARGET_STM32H747xI_CM7/TOOLCHAIN_GCC_ARM/STM32H747xI_CM7.ld
index 3e84731230..2914967e38 100644
--- a/targets/TARGET_STM/TARGET_STM32H7/TARGET_STM32H747xI/TARGET_STM32H747xI_CM7/TOOLCHAIN_GCC_ARM/STM32H747xI_CM7.ld
+++ b/targets/TARGET_STM/TARGET_STM32H7/TARGET_STM32H747xI/TARGET_STM32H747xI_CM7/TOOLCHAIN_GCC_ARM/STM32H747xI_CM7.ld
@@ -113,7 +113,7 @@ SECTIONS
*(SORT(.dtors.*))
*(.dtors)

-#if defined(MCUBOOT_BOOTLOADER_BUILD) && ( defined(TARGET_PORTENTA_H7_M7) || defined(TARGET_NICLA_VISION))
+#if (defined(MCUBOOT_BOOTLOADER_BUILD) || defined(BOOTLOADER_BUILD)) && (defined(TARGET_PORTENTA_H7_M7) || defined(TARGET_NICLA_VISION))
*ltrans0*.o(.rodata*)
*ltrans1*.o(.rodata*)
*ltrans2*.o(.rodata*)
--
2.37.3

114 changes: 114 additions & 0 deletions patches/0179-STM32-RTC-allow-runtime-clock-source-selection.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
From 98eef4b565d5701b55e58388b14d45f1776e0430 Mon Sep 17 00:00:00 2001
From: Martino Facchin <m.facchin@arduino.cc>
Date: Wed, 28 Sep 2022 17:28:59 +0200
Subject: [PATCH 179/179] STM32: RTC: allow runtime clock source selection

---
targets/TARGET_STM/rtc_api.c | 80 +++++++++++++++++++++---------------
1 file changed, 46 insertions(+), 34 deletions(-)

diff --git a/targets/TARGET_STM/rtc_api.c b/targets/TARGET_STM/rtc_api.c
index 3fb6cc7320..4b0d386e39 100644
--- a/targets/TARGET_STM/rtc_api.c
+++ b/targets/TARGET_STM/rtc_api.c
@@ -44,6 +44,14 @@ static int RTC_inited = 0;

static RTC_HandleTypeDef RtcHandle;

+MBED_WEAK bool isLSEAvailableAndPrecise() {
+#if MBED_CONF_TARGET_LSE_AVAILABLE
+ return true;
+#else
+ return false;
+#endif
+}
+
void rtc_init(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
@@ -73,44 +81,48 @@ void rtc_init(void)
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
error("PeriphClkInitStruct RTC failed with HSE\n");
}
-#elif (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_LSE_OR_LSI) && MBED_CONF_TARGET_LSE_AVAILABLE
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
-#if MBED_CONF_TARGET_LSE_BYPASS
- RCC_OscInitStruct.LSEState = RCC_LSE_BYPASS;
-#else
- RCC_OscInitStruct.LSEState = RCC_LSE_ON;
-#endif
-
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
- error("Cannot initialize RTC with LSE\n");
- }
+#elif (MBED_CONF_TARGET_RTC_CLOCK_SOURCE == USE_RTC_CLK_LSE_OR_LSI)
+
+ // Request if LSE is precise (fallback to WEAK implementation in case)
+ if (isLSEAvailableAndPrecise()) {
+ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
+ RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
+ #if MBED_CONF_TARGET_LSE_BYPASS
+ RCC_OscInitStruct.LSEState = RCC_LSE_BYPASS;
+ #else
+ RCC_OscInitStruct.LSEState = RCC_LSE_ON;
+ #endif
+
+ if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
+ error("Cannot initialize RTC with LSE\n");
+ }

- __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
+ __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);

- PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
- PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
- error("PeriphClkInitStruct RTC failed with LSE\n");
- }
-#else /* Fallback to LSI */
-#if TARGET_STM32WB
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
-#else
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
-#endif
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
- RCC_OscInitStruct.LSIState = RCC_LSI_ON;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
- error("Cannot initialize RTC with LSI\n");
- }
+ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
+ PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
+ if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
+ error("PeriphClkInitStruct RTC failed with LSE\n");
+ }
+ } else {
+ #if TARGET_STM32WB
+ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
+ #else
+ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
+ #endif
+ RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
+ RCC_OscInitStruct.LSIState = RCC_LSI_ON;
+ if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
+ error("Cannot initialize RTC with LSI\n");
+ }

- __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
+ __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);

- PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
- PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
- error("PeriphClkInitStruct RTC failed with LSI\n");
+ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
+ PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
+ if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
+ error("PeriphClkInitStruct RTC failed with LSI\n");
+ }
}
#endif /* MBED_CONF_TARGET_RTC_CLOCK_SOURCE */
#if defined(DUAL_CORE) && (TARGET_STM32H7)
--
2.37.3

Binary file modified variants/PORTENTA_H7_M7/libs/libmbed.a
Binary file not shown.
39 changes: 39 additions & 0 deletions variants/PORTENTA_H7_M7/portenta_info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#define OTP_QSPI_MAGIC 0xB5

typedef struct {
uint8_t magic;
uint8_t version;
union {
uint16_t board_functionalities;
struct {
uint8_t usb_high_speed :1;
uint8_t ethernet :1;
uint8_t wifi :1;
uint8_t video :1;
uint8_t nxp_crypto :1;
uint8_t mchp_crypto :1;
} _board_functionalities;
};
uint16_t revision;
uint16_t carrier;
uint8_t external_ram_size;
uint8_t external_flash_size;
uint16_t vid;
uint16_t pid;
uint8_t mac_address[6];
uint8_t mac_address_2[6];
} PortentaBoardInfo;

typedef struct {
uint8_t magic;
uint8_t version;
uint8_t clock_source;
uint8_t usb_speed;
uint8_t ethernet;
uint8_t wifi;
uint8_t ram_size;
uint8_t qspi_size;
uint8_t video;
uint8_t crypto;
uint8_t extclock;
} PortentaBootloaderInfo;
Loading