Skip to content

Commit 1c2874a

Browse files
committed
save
1 parent b06f0a1 commit 1c2874a

17 files changed

+942
-31
lines changed

Bootloader/Adapters/Inc/flash_adapter.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@
4040

4141
#ifdef EXTERNAL_FLASH // Selecting where will firmware go
4242

43-
// TODO: add support for external flash chip
44-
#define FIRMWARE_FLASH_SIZE_LIMIT (100000U)
45-
#define PACKET_SIZE 256U //TODO: this time size is 256 and 64 byte package can fit in, Check in the future.
43+
#define FIRMWARE_FLASH_SIZE_LIMIT (100000U)
44+
#define PACKET_SIZE (256U)
4645

4746
#else
4847
#define FIRMWARE_FLASH_SIZE_LIMIT (FLASH_SIZE - 0x8000u) //!< Max available flash size for firmware. Flash bank size - FW start address
@@ -68,8 +67,8 @@
6867
#define MAGIC_KEY_ADDRESS_FLASH (0x08020200UL) //!< Flash address in internal flash for communication between bootloader and firmware
6968
#define MAGIC_KEY_ADDRESS_RAM (0x20070000UL) //!< Flash address in ram for communication between bootloader and firmware
7069
#elif defined(STM32N6xx)
71-
#define FLASH_FIRMWARE_ADDRESS (0x08020000UL) //!< Flash address where firmware will be written
72-
#define FLASH_BOOTLOADER_ADDRESS (0x08000000UL) //!< Flash address where bootloader will be written
70+
#define FLASH_FIRMWARE_ADDRESS (0x70200000UL) //!< Flash address where firmware will be written
71+
#define FLASH_BOOTLOADER_ADDRESS (0x70100000UL) //!< Flash address where bootloader (SSBL) will be written
7372
#define RAM_FIRMWARE_ADDRESS (0x34000000UL) //!< RAM address where firmware will be written
7473
#else // UnitTest
7574
#define FLASH_FIRMWARE_ADDRESS (0x08020000UL) //!< Flash address where firmware will be written
@@ -92,5 +91,8 @@ bool FlashAdapter_setReadProtection(bool enable);
9291
/* proprietary code readout protection */
9392
bool FlashAdapter_setPCROP(bool enable, uint32_t protect_address_start, uint32_t protect_address_end);
9493

94+
#ifdef EXTERNAL_FLASH
95+
void FlashAdapter_init(void);
96+
#endif
9597

9698
#endif /* BOOTLOADER_INC_FLASH_ADAPTER_H_ */

Bootloader/Adapters/Src/flash_adapter.c

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,110 @@ HAL_StatusTypeDef ActivateProtection(FLASH_OBProgramInitTypeDef* ob_struct, uint
5858
#endif
5959

6060
#ifdef EXTERNAL_FLASH
61+
62+
#define MX25UM51245G_SECTOR_SIZE 0xFFF
63+
64+
#include "extmem_manager.h"
65+
#include "stm32_sfdp_driver_api.h"
66+
67+
extern BOOTStatus_TypeDef MapMemory(void);
68+
69+
extern EXTMEM_DefinitionTypeDef extmem_list_config[1];
70+
71+
XSPI_HandleTypeDef hxspi2;
72+
73+
void
74+
FlashAdapter_init(void) {
75+
XSPIM_CfgTypeDef sXspiManagerCfg = {0};
76+
77+
/* XSPI2 parameter configuration*/
78+
hxspi2.Instance = XSPI2;
79+
hxspi2.Init.FifoThresholdByte = 4;
80+
hxspi2.Init.MemoryMode = HAL_XSPI_SINGLE_MEM;
81+
hxspi2.Init.MemoryType = HAL_XSPI_MEMTYPE_MACRONIX;
82+
hxspi2.Init.MemorySize = HAL_XSPI_SIZE_1GB;
83+
hxspi2.Init.ChipSelectHighTimeCycle = 1;
84+
hxspi2.Init.FreeRunningClock = HAL_XSPI_FREERUNCLK_DISABLE;
85+
hxspi2.Init.ClockMode = HAL_XSPI_CLOCK_MODE_0;
86+
hxspi2.Init.WrapSize = HAL_XSPI_WRAP_NOT_SUPPORTED;
87+
hxspi2.Init.ClockPrescaler = 0;
88+
hxspi2.Init.SampleShifting = HAL_XSPI_SAMPLE_SHIFT_NONE;
89+
hxspi2.Init.DelayHoldQuarterCycle = HAL_XSPI_DHQC_ENABLE;
90+
hxspi2.Init.ChipSelectBoundary = HAL_XSPI_BONDARYOF_NONE;
91+
hxspi2.Init.MaxTran = 0;
92+
hxspi2.Init.Refresh = 0;
93+
hxspi2.Init.MemorySelect = HAL_XSPI_CSSEL_NCS1;
94+
if (HAL_XSPI_Init(&hxspi2) != HAL_OK) {
95+
Error_Handler();
96+
}
97+
sXspiManagerCfg.nCSOverride = HAL_XSPI_CSSEL_OVR_NCS1;
98+
sXspiManagerCfg.IOPort = HAL_XSPIM_IOPORT_2;
99+
sXspiManagerCfg.Req2AckTime = 1;
100+
if (HAL_XSPIM_Config(&hxspi2, &sXspiManagerCfg, HAL_XSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
101+
Error_Handler();
102+
}
103+
104+
MX_EXTMEM_MANAGER_Init();
105+
MapMemory();
106+
}
107+
61108
bool
62109
FlashAdapter_erase(uint32_t firmware_size, uint32_t flash_address) {
63-
//return W25q_dynamicErase(firmware_size, flash_address);
64-
return true;
110+
111+
bool success = false;
112+
113+
uint32_t address = (flash_address - 0x70000000UL);
114+
115+
EXTMEM_DRIVER_NOR_SFDP_Disable_MemoryMappedMode(&(extmem_list_config->NorSfdpObject));
116+
117+
EXTMEM_StatusTypeDef retr = EXTMEM_EraseSector(EXTMEMORY_1, address, firmware_size);
118+
119+
if (EXTMEM_OK == retr) {
120+
success = true;
121+
}
122+
123+
EXTMEM_DRIVER_NOR_SFDP_Enable_MemoryMappedMode(&(extmem_list_config->NorSfdpObject));
124+
125+
return success;
65126
}
66127

67128
bool
68129
FlashAdapter_blockErase(uint32_t address) {
69-
//return W25q_blockErase64k(address);
70-
return true;
130+
return false;
71131
}
72132

73133
bool
74134
FlashAdapter_program(uint32_t address, uint8_t* buffer, uint32_t length) {
75-
//return W25q_quadPageProgram(address, buffer, length);
76-
return true;
135+
136+
bool success = false;
137+
EXTMEM_DRIVER_NOR_SFDP_StatusTypeDef ret = EXTMEM_DRIVER_NOR_SFDP_OK;
138+
139+
address -= 0x70000000UL;
140+
141+
ret = EXTMEM_DRIVER_NOR_SFDP_Disable_MemoryMappedMode(&(extmem_list_config->NorSfdpObject));
142+
143+
if (EXTMEM_DRIVER_NOR_SFDP_OK == ret) {
144+
ret = EXTMEM_DRIVER_NOR_SFDP_Write(&(extmem_list_config->NorSfdpObject), address, buffer, length);
145+
146+
if (EXTMEM_DRIVER_NOR_SFDP_OK == ret) {
147+
EXTMEM_DRIVER_NOR_SFDP_Enable_MemoryMappedMode(&(extmem_list_config->NorSfdpObject));
148+
}
149+
}
150+
151+
if (EXTMEM_DRIVER_NOR_SFDP_OK == ret) {
152+
success = true;
153+
}
154+
155+
return success;
77156
}
78157

79158
bool
80159
FlashAdapter_readBytes(uint32_t address, uint8_t* buffer, uint32_t length) {
81-
//return W25q_readBytes(address, buffer, length);
82-
return true;
160+
bool success = true;
161+
// cppcheck-suppress misra-c2012-11.6; address is received as uint32_t
162+
(void*)memcpy((void*)buffer, (void*)address, length);
163+
164+
return success;
83165
}
84166

85167
bool

Bootloader/Adapters/Src/gpio_adapter.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ GpioAdapter_init(void) {
4747
__HAL_RCC_GPIOF_CLK_ENABLE();
4848
#if defined(STM32N657xx)
4949
__HAL_RCC_GPIOG_CLK_ENABLE();
50+
__HAL_RCC_GPION_CLK_ENABLE();
5051
#endif
5152

5253
#if defined(LED1_Pin) && defined(LED1_Port) && defined(LED_OFF)

Bootloader/Inc/signature.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ typedef struct signature {
4949

5050
//! Enumeration for different signatures
5151
typedef enum signatureType_ENUM {
52-
signatureType_FIRMWARE_FLASH = 0x00, //!< New firmware for FLASH
53-
signatureType_FIRMWARE_RAM = 0x01, //!< Firmware for RAM
54-
signatureType_BOOTLOADER_FLASH = 0x02, //!< New bootloader for FLASH
55-
signatureType_BOOTLOADER_RAM = 0x03, //!< Bootloader for RAM
56-
signatureType_UNKNOWN = 0xFF, //!< Not existing or unknown signature
52+
signatureType_FIRMWARE_FLASH = 0x00, //!< New firmware for FLASH
53+
signatureType_FIRMWARE_RAM = 0x01, //!< Firmware for RAM
54+
signatureType_BOOTLOADER_INT_FLASH = 0x02, //!< New bootloader for Internal FLASH
55+
signatureType_BOOTLOADER_RAM = 0x03, //!< Bootloader for RAM
56+
signatureType_BOOTLOADER_EXT_FLASH = 0x04, //!< New bootloader for External FLASH
57+
signatureType_UNKNOWN = 0xFF, //!< Not existing or unknown signature
5758
} signatureType_E;
5859

5960
signatureType_E Signature_verification(const signature_S* signature);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* USER CODE BEGIN Header */
2+
/**
3+
******************************************************************************
4+
* @file : extmem_manager.c
5+
* @version : 1.0.0
6+
* @brief : This file implements the extmem configuration
7+
******************************************************************************
8+
* @attention
9+
*
10+
* Copyright (c) 2025 STMicroelectronics.
11+
* All rights reserved.
12+
*
13+
* This software is licensed under terms that can be found in the LICENSE file
14+
* in the root directory of this software component.
15+
* If no LICENSE file comes with this software, it is provided AS-IS.
16+
*
17+
******************************************************************************
18+
*/
19+
/* USER CODE END Header */
20+
21+
/* Includes ------------------------------------------------------------------*/
22+
#include "extmem_manager.h"
23+
#include <string.h>
24+
25+
/* USER CODE BEGIN Includes */
26+
27+
/* USER CODE END Includes */
28+
29+
/* USER CODE BEGIN PV */
30+
/* Private variables ---------------------------------------------------------*/
31+
32+
/* USER CODE END PV */
33+
34+
/* USER CODE BEGIN PFP */
35+
/* Private function prototypes -----------------------------------------------*/
36+
37+
/* USER CODE END PFP */
38+
39+
/*
40+
* -- Insert your variables declaration here --
41+
*/
42+
/* USER CODE BEGIN 0 */
43+
44+
/* USER CODE END 0 */
45+
46+
/*
47+
* -- Insert your external function declaration here --
48+
*/
49+
/* USER CODE BEGIN 1 */
50+
51+
/* USER CODE END 1 */
52+
53+
/**
54+
* Init External memory manager
55+
* @retval None
56+
*/
57+
void
58+
MX_EXTMEM_MANAGER_Init(void) {
59+
60+
/* USER CODE BEGIN MX_EXTMEM_Init_PreTreatment */
61+
62+
/* USER CODE END MX_EXTMEM_Init_PreTreatment */
63+
64+
/* Initialization of the memory parameters */
65+
memset(extmem_list_config, 0x0, sizeof(extmem_list_config));
66+
67+
/* EXTMEMORY_1 */
68+
extmem_list_config[0].MemType = EXTMEM_NOR_SFDP;
69+
extmem_list_config[0].Handle = (void*)&hxspi2;
70+
extmem_list_config[0].ConfigType = EXTMEM_LINK_CONFIG_8LINES;
71+
72+
EXTMEM_Init(EXTMEMORY_1, HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_XSPI2));
73+
74+
/* USER CODE BEGIN MX_EXTMEM_Init_PostTreatment */
75+
76+
/* USER CODE END MX_EXTMEM_Init_PostTreatment */
77+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* USER CODE BEGIN Header */
2+
/**
3+
******************************************************************************
4+
* @file : extmem_manager.h
5+
* @version : 1.0.0
6+
* @brief : Header for secure_manager_api.c file.
7+
******************************************************************************
8+
* @attention
9+
*
10+
* Copyright (c) 2025 STMicroelectronics.
11+
* All rights reserved.
12+
*
13+
* This software is licensed under terms that can be found in the LICENSE file
14+
* in the root directory of this software component.
15+
* If no LICENSE file comes with this software, it is provided AS-IS.
16+
*
17+
******************************************************************************
18+
*/
19+
/* USER CODE END Header */
20+
21+
/* Define to prevent recursive inclusion -------------------------------------*/
22+
#ifndef __MX_EXTMEM__H__
23+
#define __MX_EXTMEM__H__
24+
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
/* Includes ------------------------------------------------------------------*/
30+
#include "stm32_extmem_conf.h"
31+
32+
/* USER CODE BEGIN INCLUDE */
33+
34+
/* USER CODE END INCLUDE */
35+
36+
/* Private variables ---------------------------------------------------------*/
37+
/* USER CODE BEGIN PV */
38+
39+
/* USER CODE END PV */
40+
41+
/* Private function prototypes -----------------------------------------------*/
42+
/* USER CODE BEGIN PFP */
43+
44+
/* USER CODE END PFP */
45+
46+
/*
47+
* -- Insert your variables declaration here --
48+
*/
49+
/* USER CODE BEGIN VARIABLES */
50+
51+
/* USER CODE END VARIABLES */
52+
53+
void MX_EXTMEM_MANAGER_Init(void);
54+
55+
/*
56+
* -- Insert functions declaration here --
57+
*/
58+
/* USER CODE BEGIN FD */
59+
60+
/* USER CODE END FD */
61+
62+
#ifdef __cplusplus
63+
}
64+
#endif
65+
66+
#endif /* __MX_EXTMEM__H__ */
67+

0 commit comments

Comments
 (0)