Skip to content

Commit 8ef66c7

Browse files
toyowataadbridge
authored andcommitted
Add alignment check in the flash_program_page
* Add source address word alignment check * malloc and memcpy are called only if data is unaligned * malloc size is now copySize (program page size), rather than whole buffer to be written
1 parent cd07a81 commit 8ef66c7

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,28 @@ int32_t flash_program_page(flash_t *obj, uint32_t address,
115115
const uint32_t copySize = 1024; // should be 256|512|1024|4096
116116
uint8_t *alignedData, *source;
117117

118-
// always malloc outside critical section
119-
alignedData = malloc(size);
120-
if (alignedData == 0) {
121-
return (1);
118+
alignedData = 0;
119+
source = (uint8_t *)data;
120+
121+
// check word boundary
122+
if (((uint32_t)data % 4) != 0) {
123+
// always malloc outside critical section
124+
alignedData = malloc(copySize);
125+
if (alignedData == 0) {
126+
return (1);
127+
}
122128
}
123129

124130
n = GetSecNum(address); // Get Sector Number
125131

126-
memcpy(alignedData, data, size);
127-
source = alignedData;
128-
129132
core_util_critical_section_enter();
130133

131134
while (size) {
135+
if (((uint32_t)data % 4) != 0) {
136+
memcpy(alignedData, source, copySize);
137+
source = alignedData;
138+
}
139+
132140
/*
133141
Prepare_Sector_for_Write command must be exected before
134142
Copy_RAM_to_Flash command.

0 commit comments

Comments
 (0)