Skip to content

Commit bb2a155

Browse files
committed
MCUXpresso: Update LPC HAL flash driver
The flash driver for the LPC55S69 is different from prior LPC family. Move the Flash HAL driver to SoC specific folder Signed-off-by: Mahesh Mahadevan <[email protected]>
1 parent 5853af7 commit bb2a155

File tree

4 files changed

+296
-1
lines changed

4 files changed

+296
-1
lines changed

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/objects.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct spi_s {
5959
uint8_t bits;
6060
};
6161

62-
#if DEVICE_FLASH
62+
#if DEVICE_FLASH && !defined(TARGET_FLASH_CMSIS_ALGO)
6363
struct flash_s {
6464
uint8_t dummy;
6565
};
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "flash_api.h"
18+
#include "mbed_critical.h"
19+
20+
// This file is automagically generated
21+
22+
#if DEVICE_FLASH
23+
24+
#include "flash_data.h"
25+
#include "partition_ARMCM33.h"
26+
#include "fsl_iap.h"
27+
28+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
29+
30+
#define SECURE_FLASH_START_ADDR 0x10000000
31+
32+
static flash_config_t flash_config;
33+
34+
// This is a flash algo binary blob. It is PIC (position independent code) that should be stored in RAM
35+
static uint32_t FLASH_ALGO[] = {
36+
0xf240b580, 0xf2c0000c, 0x44480000, 0xf862f000, 0xbf182800, 0xbd802001, 0x47702000, 0xf240b510,
37+
0xf2c0040c, 0xf2460400, 0xeb09636c, 0xf6c60004, 0x21003365, 0x2218f44f, 0xf86ef000, 0xbf182800,
38+
0xeb09bd10, 0x21000004, 0x2218f44f, 0x4010e8bd, 0xb8b2f000, 0xf240b5b0, 0xf2c0050c, 0xeb090500,
39+
0x690a0105, 0xf2464604, 0xf6c6636c, 0x46083365, 0xf0004621, 0x2800f851, 0xbdb0bf18, 0x0005eb09,
40+
0x46216902, 0x40b0e8bd, 0xb896f000, 0xb085b5f0, 0x070cf240, 0x4606460d, 0x0700f2c0, 0x0007eb09,
41+
0x462b4631, 0xf0004614, 0xb108f85f, 0xbdf0b005, 0xf10da804, 0x90000c0c, 0x0007eb09, 0x462a4631,
42+
0xf8cd4623, 0xf000c004, 0xb005f88d, 0x0000bdf0, 0xe7fdbe00, 0x1100f241, 0x3100f2c1, 0x29006809,
43+
0x2260d00c, 0x680a6282, 0x0308f240, 0x2207f3c2, 0x0300f2c0, 0x2003f849, 0x47086849, 0x3048f240,
44+
0x0000f2c0, 0x3155f240, 0x0100f2c0, 0x44794478, 0xf7ff2273, 0xbf00ffdd, 0x0c08f240, 0x0c00f2c0,
45+
0xc00cf859, 0x0f00f1bc, 0xf241d00b, 0xf2c11c00, 0xf8dc3c00, 0xf1bcc000, 0xd0070f00, 0xc008f8dc,
46+
0xf2444760, 0xf2c11c3b, 0x47603c00, 0x20f8f240, 0x0000f2c0, 0x3105f240, 0x0100f2c0, 0x44794478,
47+
0xf7ff2284, 0xbf00ffb5, 0x0c08f240, 0x0c00f2c0, 0xc00cf859, 0x0f00f1bc, 0xf241d00b, 0xf2c11c00,
48+
0xf8dc3c00, 0xf1bcc000, 0xd0070f00, 0xc00cf8dc, 0xf2444760, 0xf2c11c9d, 0x47603c00, 0x20a8f240,
49+
0x0000f2c0, 0x21b5f240, 0x0100f2c0, 0x44794478, 0xf7ff2294, 0xbf00ff8d, 0x1300f241, 0x3300f2c1,
50+
0x2b00681b, 0x691bd001, 0xf2404718, 0xf2c0207a, 0xf2400000, 0xf2c02187, 0x44780100, 0x229c4479,
51+
0xff76f7ff, 0x0c08f240, 0x0c00f2c0, 0xc00cf859, 0x0f00f1bc, 0xf241d00b, 0xf2c11c00, 0xf8dc3c00,
52+
0xf1bcc000, 0xd0070f00, 0xc014f8dc, 0xf2444760, 0xf2c12c7d, 0x47603c00, 0x202cf240, 0x0000f2c0,
53+
0x2139f240, 0x0100f2c0, 0x44794478, 0xf7ff22b0, 0xbf00ff4f, 0x1300f241, 0x3300f2c1, 0x2b00681b,
54+
0x699bd001, 0xf2404718, 0xf2c010fe, 0xf2400000, 0xf2c0210b, 0x44780100, 0x22b94479, 0xff38f7ff,
55+
0x1100f241, 0x3100f2c1, 0x29006809, 0x69c9d001, 0xf2404708, 0xf2c010d2, 0xf2400000, 0xf2c011df,
56+
0x44780100, 0x22c34479, 0xff22f7ff, 0x1100f241, 0x3100f2c1, 0x29006809, 0x6a09d001, 0xf2404708,
57+
0xf2c010a6, 0xf2400000, 0xf2c011b3, 0x44780100, 0x22ca4479, 0xff0cf7ff, 0x1100f241, 0x3100f2c1,
58+
0x29006809, 0xf644d004, 0xf2c11151, 0x47083100, 0x1074f240, 0x0000f2c0, 0x1181f240, 0x0100f2c0,
59+
0x44794478, 0xf7ff22d0, 0xbf00fef3, 0x230bf644, 0x3300f2c1, 0xbf004718, 0x1300f241, 0x3300f2c1,
60+
0x2b00681b, 0x6a5bd001, 0xf2404718, 0xf2c0103a, 0xf2400000, 0xf2c01147, 0x44780100, 0x22de4479,
61+
0xfed6f7ff, 0x1c00f241, 0x3c00f2c1, 0xc000f8dc, 0x0f00f1bc, 0xf8dcd002, 0x4760c02c, 0x1008f240,
62+
0x0000f2c0, 0x1115f240, 0x0100f2c0, 0x44794478, 0xf7ff22e5, 0xbf00febd, 0x1200f241, 0x3200f2c1,
63+
0x2a006812, 0x6b12d001, 0xf2404710, 0xf2c000da, 0xf2400000, 0xf2c001e7, 0x44780100, 0x22ec4479,
64+
0xfea6f7ff, 0x1200f241, 0x3200f2c1, 0x2a006812, 0x6b52d001, 0xf2404710, 0xf2c000ae, 0xf2400000,
65+
0xf2c001bb, 0x44780100, 0x22f34479, 0xfe90f7ff, 0x1300f241, 0x3300f2c1, 0x2b00681b, 0x6b9bd001,
66+
0xf2404718, 0xf2c00082, 0xf2400000, 0xf2c0018f, 0x44780100, 0x22fa4479, 0xfe7af7ff, 0x5cb3f644,
67+
0x3c00f2c1, 0xbf004760, 0x6c15f644, 0x3c00f2c1, 0xbf004760, 0x1200f241, 0x3200f2c1, 0x2a006812,
68+
0x6a92d001, 0xf2404710, 0xf2c0003e, 0xf2400000, 0xf2c0014b, 0x44780100, 0xf44f4479, 0xf7ff7287,
69+
0xbf00fe57, 0x1c00f241, 0x3c00f2c1, 0xc000f8dc, 0x0f00f1bc, 0xf8dcd002, 0x4760c040, 0x0008f240,
70+
0x0000f2c0, 0x0115f240, 0x0100f2c0, 0x44794478, 0x1215f240, 0xfe3cf7ff, 0x53414c46, 0x50415f48,
71+
0x52545f49, 0x2e004545, 0x2e2e2f2e, 0x2f2e2e2f, 0x72756f73, 0x6e2f6563, 0x6c2f7078, 0x35356370,
72+
0x2f393653, 0x5f6c7366, 0x2e706169, 0x00000063, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
73+
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
74+
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000
75+
};
76+
77+
static const flash_algo_t flash_algo_config = {
78+
.init = 0x1,
79+
.uninit = 0x19,
80+
.erase_sector = 0x55,
81+
.program_page = 0x8d,
82+
.static_base = 0x490,
83+
.algo_blob = FLASH_ALGO
84+
};
85+
86+
static const sector_info_t sectors_info[] = {
87+
{SECURE_FLASH_START_ADDR, 0x8000},
88+
};
89+
90+
static const flash_target_config_t flash_target_config = {
91+
.page_size = 0x200,
92+
.flash_start = SECURE_FLASH_START_ADDR,
93+
.flash_size = SECURE_ROM_SIZE,
94+
.sectors = sectors_info,
95+
.sector_info_count = sizeof(sectors_info) / sizeof(sector_info_t)
96+
};
97+
98+
/* Non-secure flash */
99+
static const sector_info_t sectors_info_ns[] = {
100+
{CODE_FLASH_START_NS, 0x8000},
101+
};
102+
103+
/* Non-secure flash */
104+
static const flash_target_config_t flash_target_config_ns = {
105+
.page_size = 0x200,
106+
.flash_start = CODE_FLASH_START_NS,
107+
.flash_size = FSL_FEATURE_SYSCON_FLASH_SIZE_BYTES - SECURE_ROM_SIZE,
108+
.sectors = sectors_info_ns,
109+
.sector_info_count = sizeof(sectors_info_ns) / sizeof(sector_info_t)
110+
};
111+
112+
void flash_set_target_config(flash_t *obj)
113+
{
114+
obj->flash_algo = &flash_algo_config;
115+
obj->target_config = &flash_target_config;
116+
obj->target_config_ns = &flash_target_config_ns;
117+
FLASH_Init(&flash_config);
118+
}
119+
120+
MBED_NONSECURE_ENTRY int32_t flash_read(flash_t *obj, uint32_t address, uint8_t *data, uint32_t size)
121+
{
122+
/* Check if flash is empty */
123+
if (FLASH_VerifyErase(&flash_config, address, size) == kStatus_FLASH_Success) {
124+
memset(data, 0xFF, size);
125+
return 0;
126+
}
127+
128+
memcpy(data, (const void *)address, size);
129+
return 0;
130+
}
131+
132+
#endif // #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
133+
#endif
134+
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "flash_api.h"
17+
#include "mbed_critical.h"
18+
19+
#if DEVICE_FLASH
20+
21+
#include "fsl_flashiap.h"
22+
23+
int32_t flash_init(flash_t *obj)
24+
{
25+
return 0;
26+
}
27+
28+
int32_t flash_free(flash_t *obj)
29+
{
30+
return 0;
31+
}
32+
33+
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
34+
{
35+
uint32_t n;
36+
uint32_t status;
37+
int32_t ret = -1;
38+
39+
/* We need to prevent flash accesses during erase operation */
40+
core_util_critical_section_enter();
41+
42+
n = address / FSL_FEATURE_SYSCON_FLASH_SECTOR_SIZE_BYTES; // Get Sector Number
43+
44+
status = FLASHIAP_PrepareSectorForWrite(n, n);
45+
if (status == kStatus_FLASHIAP_Success) {
46+
status = FLASHIAP_EraseSector(n, n, SystemCoreClock);
47+
if (status == kStatus_FLASHIAP_Success) {
48+
ret = 0;
49+
}
50+
}
51+
52+
core_util_critical_section_exit();
53+
54+
return ret;
55+
}
56+
57+
int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size)
58+
{
59+
uint32_t n;
60+
uint32_t sector_number;
61+
uint32_t num_of_bytes = size;
62+
uint32_t status;
63+
int32_t ret = -1;
64+
65+
if (address == 0) { // Check for Vector Table
66+
n = *((unsigned long *)(data + 0)) +
67+
*((unsigned long *)(data + 1)) +
68+
*((unsigned long *)(data + 2)) +
69+
*((unsigned long *)(data + 3)) +
70+
*((unsigned long *)(data + 4)) +
71+
*((unsigned long *)(data + 5)) +
72+
*((unsigned long *)(data + 6));
73+
*((unsigned long *)(data + 7)) = 0 - n; // Signature at Reserved Vector
74+
}
75+
76+
/* We need to prevent flash accesses during program operation */
77+
core_util_critical_section_enter();
78+
79+
sector_number = address / FSL_FEATURE_SYSCON_FLASH_SECTOR_SIZE_BYTES; // Get Sector Number
80+
81+
status = FLASHIAP_PrepareSectorForWrite(sector_number, sector_number);
82+
if (status == kStatus_FLASHIAP_Success) {
83+
/* Check if the number of bytes to write is aligned to page size */
84+
if (size % FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES) {
85+
uint8_t page_buffer[FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES] = { 0 };
86+
uint32_t remaining_bytes = 0;
87+
88+
/* Find the number of pages and remaining bytes */
89+
num_of_bytes = FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES * (size / FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES);
90+
remaining_bytes = (size - num_of_bytes);
91+
92+
/* Copy the remaining bytes into a temp buffer whose size is page-aligned */
93+
memcpy(page_buffer, data + num_of_bytes, remaining_bytes);
94+
95+
if (num_of_bytes) {
96+
/* First write page size aligned bytes of data */
97+
status = FLASHIAP_CopyRamToFlash(address, (uint32_t *)data, num_of_bytes, SystemCoreClock);
98+
if (status == kStatus_FLASHIAP_Success) {
99+
/* Prepare the next write for the remaining data */
100+
status = FLASHIAP_PrepareSectorForWrite(sector_number, sector_number);
101+
}
102+
}
103+
104+
/* Write the remaining data */
105+
if (status == kStatus_FLASHIAP_Success) {
106+
status = FLASHIAP_CopyRamToFlash((address + num_of_bytes), (uint32_t *)page_buffer, FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES, SystemCoreClock);
107+
if (status == kStatus_FLASHIAP_Success) {
108+
ret = 0;
109+
}
110+
}
111+
} else {
112+
status = FLASHIAP_CopyRamToFlash(address, (uint32_t *)data, num_of_bytes, SystemCoreClock);
113+
if (status == kStatus_FLASHIAP_Success) {
114+
ret = 0;
115+
}
116+
}
117+
}
118+
119+
core_util_critical_section_exit();
120+
121+
return ret;
122+
}
123+
124+
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
125+
{
126+
uint32_t sectorsize = MBED_FLASH_INVALID_SIZE;
127+
uint32_t devicesize = FSL_FEATURE_SYSCON_FLASH_SIZE_BYTES;
128+
uint32_t startaddr = 0;
129+
130+
if ((address >= startaddr) && (address < (startaddr + devicesize))) {
131+
sectorsize = FSL_FEATURE_SYSCON_FLASH_SECTOR_SIZE_BYTES;
132+
}
133+
134+
return sectorsize;
135+
}
136+
137+
uint32_t flash_get_page_size(const flash_t *obj)
138+
{
139+
return FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES;
140+
}
141+
142+
uint32_t flash_get_start_address(const flash_t *obj)
143+
{
144+
uint32_t startaddr = 0;
145+
146+
return startaddr;
147+
}
148+
149+
uint32_t flash_get_size(const flash_t *obj)
150+
{
151+
return FSL_FEATURE_SYSCON_FLASH_SIZE_BYTES;
152+
}
153+
154+
uint8_t flash_get_erase_value(const flash_t *obj)
155+
{
156+
(void)obj;
157+
158+
return 0xFF;
159+
}
160+
161+
#endif

0 commit comments

Comments
 (0)