@@ -112,42 +112,56 @@ int32_t flash_program_page(flash_t *obj, uint32_t address,
112
112
const uint8_t * data , uint32_t size )
113
113
{
114
114
unsigned long n ;
115
+ const uint32_t copySize = 1024 ; // should be 256|512|1024|4096
116
+ uint8_t * alignedData , * source ;
117
+
115
118
// always malloc outside critical section
116
- uint8_t * alignedData = malloc (size );
119
+ alignedData = malloc (size );
120
+ if (alignedData == 0 ) {
121
+ return (1 );
122
+ }
117
123
118
124
n = GetSecNum (address ); // Get Sector Number
119
125
120
- core_util_critical_section_enter ();
121
- IAP .cmd = 50 ;// Prepare Sector for Write
122
- IAP .par [0 ] = n ;// Start Sector
123
- IAP .par [1 ] = n ;// End Sector
124
- IAP_Call (& IAP .cmd , & IAP .stat );// Call IAP Command
125
- if (IAP .stat ) {
126
- return (1 ); // Command Failed
127
- }
126
+ memcpy (alignedData , data , size );
127
+ source = alignedData ;
128
128
129
- IAP .cmd = 51 ; // Copy RAM to Flash
130
- IAP .par [0 ] = address ;// Destination Flash Address
129
+ core_util_critical_section_enter ();
131
130
132
- if ((unsigned long )data %4 == 0 ) { // Word boundary
133
- IAP .par [1 ] = (unsigned long )data ;// Source RAM Address
134
- } else {
135
- memcpy (alignedData ,data ,size );
136
- IAP .par [1 ] = (unsigned long )alignedData ; // Source RAM Address
131
+ while (size ) {
132
+ /*
133
+ Prepare_Sector_for_Write command must be exected before
134
+ Copy_RAM_to_Flash command.
135
+ */
136
+ IAP .cmd = 50 ; // Prepare Sector for Write
137
+ IAP .par [0 ] = n ; // Start Sector
138
+ IAP .par [1 ] = n ; // End Sector
139
+ IAP_Call (& IAP .cmd , & IAP .stat ); // Call IAP Command
140
+ if (IAP .stat ) {
141
+ return (1 ); // Command Failed
142
+ }
143
+
144
+ IAP .cmd = 51 ; // Copy RAM to Flash
145
+ IAP .par [0 ] = address ; // Destination Flash Address
146
+ IAP .par [1 ] = (unsigned long )source ; // Source RAM Address
147
+ IAP .par [2 ] = copySize ; // number of bytes to be written
148
+ IAP .par [3 ] = CCLK ; // CCLK in kHz
149
+ IAP_Call (& IAP .cmd , & IAP .stat ); // Call IAP Command
150
+ if (IAP .stat ) {
151
+ return (1 ); // Command Failed
152
+ }
153
+
154
+ source += copySize ;
155
+ size -= copySize ;
156
+ address += copySize ;
137
157
}
138
158
139
- IAP .par [2 ] = 1024 ; // Fixed Page Size
140
- IAP .par [3 ] = CCLK ;// CCLK in kHz
141
- IAP_Call (& IAP .cmd , & IAP .stat );// Call IAP Command
142
159
core_util_critical_section_exit ();
143
160
144
- if (alignedData != 0 ) { // We allocated our own memory
161
+ if (alignedData != 0 ) { // We allocated our own memory
145
162
free (alignedData );
146
163
}
147
164
148
- if (IAP .stat ) {
149
- return (1 ); // Command Failed
150
- }
151
165
return (0 ); // Finished without Errors
152
166
}
153
167
0 commit comments