Skip to content

Commit 3f30296

Browse files
committed
Flash support: Add flash support for LPC54114 & LPC546XX
Signed-off-by: Mahesh Mahadevan <[email protected]>
1 parent b84627f commit 3f30296

File tree

3 files changed

+137
-2
lines changed

3 files changed

+137
-2
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
62+
uint32_t status;
63+
int32_t ret = -1;
64+
uint8_t buf[FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES];
65+
66+
if (address == 0) { // Check for Vector Table
67+
n = *((unsigned long *)(data + 0)) +
68+
*((unsigned long *)(data + 1)) +
69+
*((unsigned long *)(data + 2)) +
70+
*((unsigned long *)(data + 3)) +
71+
*((unsigned long *)(data + 4)) +
72+
*((unsigned long *)(data + 5)) +
73+
*((unsigned long *)(data + 6));
74+
*((unsigned long *)(data + 7)) = 0 - n; // Signature at Reserved Vector
75+
}
76+
77+
/* Copy into a local buffer to ensure address is word-aligned */
78+
memcpy(&buf, data, FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES);
79+
80+
/* We need to prevent flash accesses during program operation */
81+
core_util_critical_section_enter();
82+
83+
sector_number = address / FSL_FEATURE_SYSCON_FLASH_SECTOR_SIZE_BYTES; // Get Sector Number
84+
85+
status = FLASHIAP_PrepareSectorForWrite(sector_number, sector_number);
86+
if (status == kStatus_FLASHIAP_Success) {
87+
status = FLASHIAP_CopyRamToFlash(address, (uint32_t *)&buf,
88+
FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES, SystemCoreClock);
89+
if (status == kStatus_FLASHIAP_Success) {
90+
ret = 0;
91+
}
92+
}
93+
94+
core_util_critical_section_exit();
95+
96+
return ret;
97+
}
98+
99+
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
100+
{
101+
uint32_t sectorsize = MBED_FLASH_INVALID_SIZE;
102+
uint32_t devicesize = FSL_FEATURE_SYSCON_FLASH_SIZE_BYTES;
103+
uint32_t startaddr = 0;
104+
105+
if ((address >= startaddr) && (address < (startaddr + devicesize))) {
106+
sectorsize = FSL_FEATURE_SYSCON_FLASH_SECTOR_SIZE_BYTES;
107+
}
108+
109+
return sectorsize;
110+
}
111+
112+
uint32_t flash_get_page_size(const flash_t *obj)
113+
{
114+
return FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES;
115+
}
116+
117+
uint32_t flash_get_start_address(const flash_t *obj)
118+
{
119+
uint32_t startaddr = 0;
120+
121+
return startaddr;
122+
}
123+
124+
uint32_t flash_get_size(const flash_t *obj)
125+
{
126+
return FSL_FEATURE_SYSCON_FLASH_SIZE_BYTES;
127+
}
128+
129+
#endif

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/objects.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ struct spi_s {
5858
uint8_t bits;
5959
};
6060

61+
#if DEVICE_FLASH
62+
struct flash_s {
63+
uint8_t dummy;
64+
};
65+
#endif
66+
6167
#include "gpio_object.h"
6268

6369
#ifdef __cplusplus

targets/targets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@
742742
"macros": ["CPU_LPC54114J256BD64_cm4", "FSL_RTOS_MBED"],
743743
"inherits": ["Target"],
744744
"detect_code": ["1054"],
745-
"device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "RTC", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
745+
"device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "RTC", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "FLASH"],
746746
"release_versions": ["2", "5"],
747747
"device_name" : "LPC54114J256BD64"
748748
},
@@ -753,7 +753,7 @@
753753
"is_disk_virtual": true,
754754
"macros": ["CPU_LPC54628J512ET180", "FSL_RTOS_MBED"],
755755
"inherits": ["Target"],
756-
"device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "RTC", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
756+
"device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "RTC", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "FLASH"],
757757
"features": ["LWIP"],
758758
"release_versions": ["2", "5"],
759759
"device_name" : "LPC54628J512ET180"

0 commit comments

Comments
 (0)