@@ -30,7 +30,11 @@ flash_status flash_erase(uint32_t address)
3030 uint32_t error = 0u ;
3131
3232 erase_init .TypeErase = FLASH_TYPEERASE_PAGES ;
33+ #if defined(STM32L4xx )
34+ erase_init .Page = (address - FLASH_BASE ) / FLASH_PAGE_SIZE ;
35+ #else
3336 erase_init .PageAddress = address ;
37+ #endif
3438#ifdef FLASH_BANK_1
3539 erase_init .Banks = FLASH_BANK_1 ;
3640#endif
@@ -59,7 +63,11 @@ flash_status flash_erase_page(uint32_t address)
5963 uint32_t error = 0u ;
6064
6165 erase_init .TypeErase = FLASH_TYPEERASE_PAGES ;
66+ #if defined(STM32L4xx )
67+ erase_init .Page = (address - FLASH_BASE ) / FLASH_PAGE_SIZE ;
68+ #else
6269 erase_init .PageAddress = address ;
70+ #endif
6371#ifdef FLASH_BANK_1
6472 erase_init .Banks = FLASH_BANK_1 ;
6573#endif
@@ -83,8 +91,45 @@ flash_status flash_erase_page(uint32_t address)
8391 * @param *length: Size of the array.
8492 * @return status: Report about the success of the writing.
8593 */
86- flash_status flash_write (uint32_t address , uint32_t * data , uint32_t length )
87- {
94+ #if defined(STM32L4xx )
95+ flash_status flash_write (uint32_t address , uint32_t * data , uint32_t length ) {
96+ flash_status status = FLASH_OK ;
97+
98+ length = (length + 1 ) >> 1 ; // roundup and convert to double words
99+
100+ HAL_FLASH_Unlock ();
101+
102+ /* Loop through the array. */
103+ for (uint32_t i = 0u ; (i < length ) && (FLASH_OK == status ); i ++ ) {
104+ /* If we reached the end of the memory, then report an error and don't
105+ * do anything else.*/
106+ if (FLASH_APP_END_ADDRESS <= address ) {
107+ status |= FLASH_ERROR_SIZE ;
108+ } else {
109+ /* The actual flashing. If there is an error, then report it. */
110+ if (HAL_OK != HAL_FLASH_Program (FLASH_TYPEPROGRAM_DOUBLEWORD , address ,
111+ * (uint64_t * )data )) {
112+ status |= FLASH_ERROR_WRITE ;
113+ }
114+ /* Read back the content of the memory. If it is wrong, then report an
115+ * error. */
116+ if (((* data ++ ) != (* (volatile uint32_t * )address )) ||
117+ ((* data ++ ) != (* (volatile uint32_t * )(address + sizeof (uint32_t ))))) {
118+ status |= FLASH_ERROR_READBACK ;
119+ }
120+
121+ /* Shift the address by a double word. */
122+ address += sizeof (uint64_t );
123+ }
124+ }
125+
126+ HAL_FLASH_Lock ();
127+
128+ return status ;
129+ }
130+
131+ #else // !STM32L4xx
132+ flash_status flash_write (uint32_t address , uint32_t * data , uint32_t length ) {
88133 flash_status status = FLASH_OK ;
89134
90135 HAL_FLASH_Unlock ();
@@ -101,9 +146,7 @@ flash_status flash_write(uint32_t address, uint32_t *data, uint32_t length)
101146 else
102147 {
103148 /* The actual flashing. If there is an error, then report it. */
104- if (HAL_OK !=
105- HAL_FLASH_Program (FLASH_TYPEPROGRAM_WORD , address , data [i ]))
106- {
149+ if (HAL_OK != HAL_FLASH_Program (FLASH_TYPEPROGRAM_WORD , address , data [i ])) {
107150 status |= FLASH_ERROR_WRITE ;
108151 }
109152 /* Read back the content of the memory. If it is wrong, then report an
@@ -122,6 +165,7 @@ flash_status flash_write(uint32_t address, uint32_t *data, uint32_t length)
122165
123166 return status ;
124167}
168+ #endif // STM32L4xx
125169
126170/**
127171 * @brief This function flashes the memory.
0 commit comments