13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
-
16
+ #if DEVICE_FLASH
17
17
#include "mbed_critical.h"
18
18
19
19
#include "flash_api.h"
@@ -106,20 +106,10 @@ typedef void (*IAP_Entry) (unsigned long *cmd, unsigned long *stat);
106
106
#define IAP_Call ((IAP_Entry) 0x1FFF1FF1)
107
107
108
108
int32_t flash_program_page (flash_t * obj , uint32_t address ,
109
- const uint8_t * datain , uint32_t size )
109
+ const uint8_t * data , uint32_t size )
110
110
{
111
- uint8_t * data ;
112
111
unsigned long n ;
113
-
114
- if ((unsigned long )datain %4 == 0 )//word boundary
115
- {
116
- data = datain ;
117
- }
118
- else
119
- {
120
- data = malloc (size );
121
- memcpy (data ,datain ,size );
122
- }
112
+ uint8_t * alignedData = 0 ;
123
113
124
114
n = GetSecNum (address );// Get Sector Number
125
115
@@ -131,15 +121,27 @@ int32_t flash_program_page(flash_t *obj, uint32_t address,
131
121
132
122
IAP .cmd = 51 ;// Copy RAM to Flash
133
123
IAP .par [0 ] = address ;// Destination Flash Address
134
- IAP .par [1 ] = (unsigned long )data ;// Source RAM Address
124
+
125
+ if ((unsigned long )data %4 == 0 )// Word boundary
126
+ {
127
+ IAP .par [1 ] = (unsigned long )data ;// Source RAM Address
128
+ }
129
+ else
130
+ {
131
+ alignedData = malloc (size );
132
+ memcpy (alignedData ,data ,size );
133
+ IAP .par [1 ] = (unsigned long )alignedData ;// Source RAM Address
134
+ }
135
135
136
136
IAP .par [2 ] = 1024 ;// Fixed Page Size
137
137
IAP .par [3 ] = CCLK ;// CCLK in kHz
138
138
IAP_Call (& IAP .cmd , & IAP .stat );// Call IAP Command
139
- if (data != datain )//We allocated our own memory
139
+
140
+ if (alignedData != 0 )//We allocated our own memory
140
141
{
141
- free (data );
142
+ free (alignedData );
142
143
}
144
+
143
145
if (IAP .stat ) return (1 );// Command Failed
144
146
145
147
return (0 );// Finished without Errors
0 commit comments