forked from Lora-net/SWL2001
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtc_hal_flash.c
335 lines (279 loc) · 10.8 KB
/
smtc_hal_flash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*!
* \file smtc_hal_flash.c
*
* \brief FLASH Hardware Abstraction Layer implementation
*
* The Clear BSD License
* Copyright Semtech Corporation 2021. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted (subject to the limitations in the disclaimer
* below) provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Semtech corporation nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
* THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SEMTECH CORPORATION BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* -----------------------------------------------------------------------------
* --- DEPENDENCIES ------------------------------------------------------------
*/
#include <stdint.h> // C99 types
#include <stdbool.h> // bool type
#include "smtc_hal_flash.h"
#include "stm32l4xx_hal.h"
#include "smtc_hal_dbg_trace.h"
#include <string.h>
/*
* -----------------------------------------------------------------------------
* --- PRIVATE MACROS-----------------------------------------------------------
*/
#ifndef MIN
#define MIN( a, b ) ( ( ( a ) < ( b ) ) ? ( a ) : ( b ) )
#endif
/*
* -----------------------------------------------------------------------------
* --- PRIVATE CONSTANTS -------------------------------------------------------
*/
/*!
* Generic definition
*/
#ifndef SUCCESS
#define SUCCESS 1
#endif
#ifndef FAIL
#define FAIL 0
#endif
/*
* -----------------------------------------------------------------------------
* --- PRIVATE TYPES -----------------------------------------------------------
*/
/*
* -----------------------------------------------------------------------------
* --- PRIVATE VARIABLES -------------------------------------------------------
*/
#if defined( USE_FLASH_READ_MODIFY_WRITE ) || defined( MULTISTACK )
static uint8_t copy_page[FLASH_PAGE_SIZE] = { 0x00 };
#endif
/*
* -----------------------------------------------------------------------------
* --- PRIVATE FUNCTIONS DECLARATION -------------------------------------------
*/
/**
* @brief Gets the page of a given address
* @param Addr: Address of the FLASH Memory
* @retval The page of a given address
*/
static uint32_t flash_get_page( uint32_t Address );
/**
* @brief Gets the bank of a given address
* @param Addr: Address of the FLASH Memory
* @retval The bank of a given address
*/
static uint32_t flash_get_bank( uint32_t Address );
/*
* -----------------------------------------------------------------------------
* --- PUBLIC FUNCTIONS DEFINITION ---------------------------------------------
*/
uint16_t hal_flash_get_page_size( )
{
return FLASH_PAGE_SIZE;
}
uint8_t hal_flash_erase_page( uint32_t addr, uint8_t nb_page )
{
uint8_t status = SUCCESS;
uint8_t hal_status = SUCCESS;
uint32_t FirstUserPage = 0;
uint32_t bank_number = 0;
uint32_t PageError = 0;
uint8_t flash_operation_retry = 0;
FLASH_EraseInitTypeDef EraseInitStruct;
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock( );
/* Clear OPTVERR bit set on virgin samples */
__HAL_FLASH_CLEAR_FLAG( FLASH_SR_OPTVERR );
/* Get the 1st page to erase */
FirstUserPage = flash_get_page( addr );
/* Get the bank */
bank_number = flash_get_bank( addr );
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Page = FirstUserPage;
EraseInitStruct.NbPages = nb_page;
EraseInitStruct.Banks = bank_number;
// SMTC_HAL_TRACE_INFO( "Erase page %u bank %u\r\n", FirstUserPage, bank_number );
/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
you have to make sure that these data are rewritten before they are accessed during code
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
DCRST and ICRST bits in the FLASH_CR register. */
do
{
hal_status = HAL_FLASHEx_Erase( &EraseInitStruct, &PageError );
flash_operation_retry++;
} while( ( hal_status != HAL_OK ) && ( flash_operation_retry < FLASH_OPERATION_MAX_RETRY ) );
if( flash_operation_retry >= FLASH_OPERATION_MAX_RETRY )
{
/*
Error occurred while erase.
User can add here some code to deal with this error.
PageError will contain the faulty and then to know the code error on this ,
user can call function 'HAL_FLASH_GetError()'
*/
SMTC_HAL_TRACE_ERROR( "FLASH_OPERATION_MAX_RETRY\r\n" );
/* Infinite loop */
while( 1 )
{
}
}
else
{
flash_operation_retry = 0;
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
HAL_FLASH_Lock( );
return status;
}
uint32_t hal_flash_write_buffer( uint32_t addr, const uint8_t* buffer, uint32_t size )
{
uint8_t status = SUCCESS;
uint8_t hal_status = SUCCESS;
uint32_t BufferIndex = 0, real_size = 0, AddrEnd = 0;
uint64_t data64 = 0;
uint8_t flash_operation_retry = 0;
/* Complete size for FLASH_TYPEPROGRAM_DOUBLEWORD operation*/
if( ( size % 8 ) != 0 )
{
real_size = size + ( 8 - ( size % 8 ) );
}
else
{
real_size = size;
}
AddrEnd = addr + real_size;
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock( );
/* Clear OPTVERR bit set on virgin samples */
__HAL_FLASH_CLEAR_FLAG( FLASH_SR_OPTVERR );
/* Don't draw outside the lines */
if( AddrEnd > ( FLASH_USER_END_ADDR + 1 ) )
{
status = FAIL;
return status;
}
/* Program the user Flash area word by word
(area defined by FlashUserStartAddr and FLASH_USER_END_ADDR) ***********/
while( addr < AddrEnd )
{
data64 = 0;
for( uint8_t i = 0; i < 8; i++ )
{
data64 += ( ( ( uint64_t ) buffer[BufferIndex + i] ) << ( i * 8 ) );
}
do
{
hal_status = HAL_FLASH_Program( FLASH_TYPEPROGRAM_DOUBLEWORD, addr, data64 );
flash_operation_retry++;
} while( ( hal_status != HAL_OK ) && ( flash_operation_retry < FLASH_OPERATION_MAX_RETRY ) );
if( flash_operation_retry >= FLASH_OPERATION_MAX_RETRY )
{
/* Error occurred while writing data in Flash memory.
User can add here some code to deal with this error */
/* Infinite loop */
while( 1 )
{
}
}
else
{
flash_operation_retry = 0;
/* increment to next double word*/
addr = addr + 8;
BufferIndex = BufferIndex + 8;
}
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
HAL_FLASH_Lock( );
return real_size;
}
void hal_flash_read_buffer( uint32_t addr, uint8_t* buffer, uint32_t size )
{
uint32_t FlashIndex = 0;
__IO uint8_t data8 = 0;
while( FlashIndex < size )
{
data8 = *( __IO uint32_t* ) ( addr + FlashIndex );
buffer[FlashIndex] = data8;
FlashIndex++;
}
}
#if defined( USE_FLASH_READ_MODIFY_WRITE ) || defined( MULTISTACK )
void hal_flash_read_modify_write( uint32_t addr, const uint8_t* buffer, uint32_t size )
{
uint32_t remaining_size = size;
do
{
memset( copy_page, 0xFF, sizeof( copy_page ) );
// Get the page number
uint32_t num_page = flash_get_page( addr );
// Get start address of this NVM page
uint32_t start_addr_page = FLASH_BASE + ( FLASH_PAGE_SIZE * num_page );
// Read data on this NVM page
hal_flash_read_buffer( start_addr_page, copy_page, FLASH_PAGE_SIZE );
// Compute the index where data need to be updated in RAM copy page
uint32_t index = ( addr - start_addr_page ) % FLASH_PAGE_SIZE;
// Compute the size of the data need to be copied without overflow on the next page
uint32_t cpy_size = MIN( remaining_size, FLASH_PAGE_SIZE - index );
memcpy( ©_page[index], &buffer[size - remaining_size], cpy_size );
// Erase NVM page
hal_flash_erase_page( addr, 1 );
// Write the RAM buffer on this NVM page
hal_flash_write_buffer( start_addr_page, copy_page, FLASH_PAGE_SIZE );
// increment address to the next page
addr += FLASH_PAGE_SIZE - index;
// Reduce the amount of data remaining to be written
remaining_size -= cpy_size;
} while( remaining_size != 0 );
}
#endif // USE_FLASH_READ_MODIFY_WRITE || MULTISTACK
/*
* -----------------------------------------------------------------------------
* --- PRIVATE FUNCTIONS DEFINITION --------------------------------------------
*/
static uint32_t flash_get_page( uint32_t Addr )
{
return ( Addr - FLASH_BASE ) / FLASH_PAGE_SIZE;
}
/**
* @brief Gets the bank of a given address
* @param Addr: Address of the FLASH Memory
* @retval The bank of a given address
*/
static uint32_t flash_get_bank( uint32_t Addr )
{
uint32_t page = flash_get_page( Addr );
if( page >= FLASH_PAGE_NUMBER )
{
SMTC_HAL_TRACE_ERROR( "Address out of range: 0x%X\r\n", Addr );
}
return FLASH_BANK_1 + ( page / FLASH_PAGE_PER_BANK );
}
/* --- EOF ------------------------------------------------------------------ */