@@ -58,7 +58,7 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
58
58
{
59
59
uint32_t n ;
60
60
uint32_t sector_number ;
61
-
61
+ uint32_t num_of_bytes = size ;
62
62
uint32_t status ;
63
63
int32_t ret = -1 ;
64
64
@@ -80,10 +80,39 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
80
80
81
81
status = FLASHIAP_PrepareSectorForWrite (sector_number , sector_number );
82
82
if (status == kStatus_FLASHIAP_Success ) {
83
- status = FLASHIAP_CopyRamToFlash (address , (uint32_t * )data ,
84
- FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES , SystemCoreClock );
85
- if (status == kStatus_FLASHIAP_Success ) {
86
- ret = 0 ;
83
+ /* Check if the number of bytes to write is aligned to page size */
84
+ if (size % FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES ) {
85
+ uint8_t page_buffer [FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES ] = { 0 };
86
+ uint32_t remaining_bytes = 0 ;
87
+
88
+ /* Find the number of pages and remaining bytes */
89
+ num_of_bytes = FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES * (size / FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES );
90
+ remaining_bytes = (size - num_of_bytes );
91
+
92
+ /* Copy the remaining bytes into a temp buffer whose size is page-aligned */
93
+ memcpy (page_buffer , data + num_of_bytes , remaining_bytes );
94
+
95
+ if (num_of_bytes ) {
96
+ /* First write page size aligned bytes of data */
97
+ status = FLASHIAP_CopyRamToFlash (address , (uint32_t * )data , num_of_bytes , SystemCoreClock );
98
+ if (status == kStatus_FLASHIAP_Success ) {
99
+ /* Prepare the next write for the remaining data */
100
+ status = FLASHIAP_PrepareSectorForWrite (sector_number , sector_number );
101
+ }
102
+ }
103
+
104
+ /* Write the remaining data */
105
+ if (status == kStatus_FLASHIAP_Success ) {
106
+ status = FLASHIAP_CopyRamToFlash ((address + num_of_bytes ), (uint32_t * )page_buffer , FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES , SystemCoreClock );
107
+ if (status == kStatus_FLASHIAP_Success ) {
108
+ ret = 0 ;
109
+ }
110
+ }
111
+ } else {
112
+ status = FLASHIAP_CopyRamToFlash (address , (uint32_t * )data , num_of_bytes , SystemCoreClock );
113
+ if (status == kStatus_FLASHIAP_Success ) {
114
+ ret = 0 ;
115
+ }
87
116
}
88
117
}
89
118
0 commit comments