Skip to content

Commit 272b9cc

Browse files
chrissnowadbridge
authored andcommitted
Ensure IAP uses word boundary aligned data.
1 parent 1382e61 commit 272b9cc

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#if DEVICE_FLASH
2020
#include "mbed_assert.h"
2121
#include "cmsis.h"
22-
22+
#include <stdlib.h>
23+
#include <string.h>
2324

2425
#define MEMMAP (*((volatile unsigned long *) 0x400FC040))
2526

@@ -109,9 +110,21 @@ typedef void (*IAP_Entry) (unsigned long *cmd, unsigned long *stat);
109110

110111

111112
int32_t flash_program_page(flash_t *obj, uint32_t address,
112-
const uint8_t *data, uint32_t size)
113+
const uint8_t *datain, uint32_t size)
113114
{
115+
uint8_t *data;
114116
unsigned long n;
117+
118+
if ((unsigned long)datain%4==0)//word boundary
119+
{
120+
data = datain;
121+
}
122+
else
123+
{
124+
data = malloc(size);
125+
memcpy(data,datain,size);
126+
}
127+
115128

116129
#if SET_VALID_CODE != 0 // Set valid User Code Signature
117130
if (address == 0) { // Check for Vector Table
@@ -141,6 +154,10 @@ int32_t flash_program_page(flash_t *obj, uint32_t address,
141154
IAP.par[2] = 1024; // Fixed Page Size
142155
IAP.par[3] = CCLK; // CCLK in kHz
143156
IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command
157+
if(data !=datain)//We allocated our own memory
158+
{
159+
free(data);
160+
}
144161
if (IAP.stat) return (1); // Command Failed
145162

146163
return (0); // Finished without Errors

0 commit comments

Comments
 (0)