summaryrefslogtreecommitdiff
path: root/bsp/es32f0654/libraries/ES32F065x_ALD_StdPeriph_Driver/Source/ald_flash.c
blob: 0b15ad7ca832e9dff8fe51026ca1d94e9c5a8bd6 (plain)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/**
  *********************************************************************************
  *
  * @file    ald_flash.c
  * @brief   FLASH module driver.
  *
  * @version V1.0
  * @date    20 Nov 2017
  * @author  AE Team
  * @note
  *
  * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  *
  *********************************************************************************
  *
  *********************************************************************************
  * @verbatim
  ==============================================================================
                    ##### FLASH Peripheral features #####
  ==============================================================================
  [..]
  Base address is 0x00000000

  [..]
  FLASH have just one programme mode , word programme.
  word programme can programme 8 bytes once ;

  ==============================================================================
                        ##### How to use this driver #####
  ==============================================================================
  [..]
   (#) programme flash using flash_write(uint32_t addr, uint8_t *buf, uint16_t len)
       (++) call the function and supply all the three paraments is needs, addr means
            the first address to write in this operation, buf is a pointer to the data which
        need writing to flash.

   (#) erase flash using flash_erase(uint32_t addr, uint16_t len)
       (++) call the function and supply two paraments, addr is the first address to erase,
            len is the length to erase

   (#) read flash using flash_read(uint32_t *ram_addr, uint32_t addr, uint16_t len)
       (++) read the flash and save to a buffer, ram_addr is the buffer's first address,
            addr is the start reading address in flash, len is the length need read

    @endverbatim
  */

#include "ald_flash.h"


/** @addtogroup ES32FXXX_ALD
  * @{
  */

/** @defgroup FLASH FLASH
  * @brief FLASH module driver
  * @{
  */

#ifdef ALD_FLASH

/** @addtogroup FLASH_Private_Types
  * @{
  */

/* opration buffer, global variable*/
static uint8_t write_buf[FLASH_PAGE_SIZE];
static op_cmd_type OP_CMD = OP_FLASH;

#if defined ( __ICCARM__ )
    #define __RAMFUNC       __ramfunc
#else
    #define __RAMFUNC
#endif

/**
  * @}
  */

/** @defgroup Flash_Private_Functions Flash Private Functions
  *  @brief   Flash Private functions
  * @{
  */

/**
  * @brief  Unlock the flash.
  * @retval Status, see @ref ald_status_t.
  */
__RAMFUNC static ald_status_t flash_unlock(void)
{
    uint16_t i;
    uint16_t op_cmd = OP_CMD;

    if (READ_BIT(MSC->FLASHSR, MSC_FLASHSR_BUSY_MSK))
        return ERROR;

    FLASH_REG_UNLOCK();
    FLASH_IAP_ENABLE();
    FLASH_REQ();

    for (i = 0; i < 0xFFFF; i++)
    {
        if (READ_BIT(MSC->FLASHSR, MSC_FLASHSR_FLASHACK_MSK))
            break;
    }

    return i == 0xFFFF ? ERROR : OK;
}

/**
  * @brief  Lock the flash.
  * @retval Status, see @ref ald_status_t.
  */
__RAMFUNC static ald_status_t flash_lock(void)
{
    uint16_t i;
    uint16_t op_cmd = OP_CMD;

    FLASH_REG_UNLOCK();
    WRITE_REG(MSC->FLASHCR, 0x0);

    for (i = 0; i < 0xFFFF; i++)
    {
        if (!(READ_BIT(MSC->FLASHSR, MSC_FLASHSR_FLASHACK_MSK)))
            break;
    }

    return i == 0xFFFF ? ERROR : OK;
}

/**
  * @brief  Erase one page.
  * @param  addr: The erased page's address
  * @retval Status, see @ref ald_status_t.
  */
__RAMFUNC static ald_status_t flash_page_erase(uint32_t addr)
{
    uint32_t i;
    uint16_t op_cmd = OP_CMD;

    __disable_irq();
    if (flash_unlock() != OK)
        goto end;

    if (op_cmd == OP_FLASH)
    {
        CLEAR_BIT(MSC->FLASHADDR, MSC_FLASHADDR_IFREN_MSK);
        MODIFY_REG(MSC->FLASHADDR, MSC_FLASHADDR_ADDR_MSK, FLASH_PAGE_ADDR(addr) << MSC_FLASHADDR_ADDR_POSS);
    }
    else
    {
        SET_BIT(MSC->FLASHADDR, MSC_FLASHADDR_IFREN_MSK);
        MODIFY_REG(MSC->FLASHADDR, MSC_FLASHADDR_ADDR_MSK, INFO_PAGE_ADDR(addr) << MSC_FLASHADDR_ADDR_POSS);
    }

    WRITE_REG(MSC->FLASHCMD, FLASH_CMD_PE);

    for (i = 0; i < 0xFFFF; i++)
    {
        if (READ_BIT(MSC->FLASHSR, MSC_FLASHSR_BUSY_MSK))
            continue;
        if (READ_BIT(MSC->FLASHSR, MSC_FLASHSR_ADDR_OV_MSK))
            goto end;
        if (READ_BIT(MSC->FLASHSR, MSC_FLASHSR_WRP_FLAG_MSK))
            goto end;
        if (READ_BIT(MSC->FLASHSR, MSC_FLASHSR_SERA_MSK))
            break;
    }

    if (i == 0xFFFF)
        goto end;

    if (flash_lock() == ERROR)
        goto end;

    __enable_irq();
    return OK;
end:

    if (flash_lock() == ERROR)
        while (1);

    __enable_irq();
    return ERROR;
}

/**
  * @brief  Programme a word.
  * @param  addr: The word's address, it is must word align.
  * @param  data: The 8 bytes data be write.
  * @param  len: The number of data be write.
  * @param  fifo: Choose if use fifo.
  * @retval Status, see @ref ald_status_t.
  */
__RAMFUNC static ald_status_t flash_word_program(uint32_t addr, uint32_t data[], uint32_t len, uint32_t fifo)
{
    uint16_t i;
    uint16_t prog_len;
    uint32_t *p_data = data;
    uint16_t op_cmd = OP_CMD;

    __disable_irq();
    if (flash_unlock() != OK)
        goto end;

    if (op_cmd == OP_FLASH)
        CLEAR_BIT(MSC->FLASHADDR, MSC_FLASHADDR_IFREN_MSK);
    else
        SET_BIT(MSC->FLASHADDR, MSC_FLASHADDR_IFREN_MSK);

    MODIFY_REG(MSC->FLASHADDR, MSC_FLASHADDR_ADDR_MSK, addr << MSC_FLASHADDR_ADDR_POSS);
    MODIFY_REG(MSC->FLASHCR, MSC_FLASHCR_FIFOEN_MSK, fifo << MSC_FLASHCR_FIFOEN_POS);

    for (prog_len = 0; prog_len < len; prog_len++)
    {
        if (fifo)
        {
            WRITE_REG(MSC->FLASHFIFO, p_data[0]);
            WRITE_REG(MSC->FLASHFIFO, p_data[1]);
        }
        else
        {
            WRITE_REG(MSC->FLASHDL, p_data[0]);
            WRITE_REG(MSC->FLASHDH, p_data[1]);
            WRITE_REG(MSC->FLASHCMD, FLASH_CMD_WP);
        }

        p_data += 2;

        for (i = 0; i < 0xFFFF; i++)
        {
            if (READ_BIT(MSC->FLASHSR, MSC_FLASHSR_BUSY_MSK))
                continue;
            if (READ_BIT(MSC->FLASHSR, MSC_FLASHSR_PROG_MSK))
                break;
        }
    }
    if (i == 0xFFFF)
        goto end;

    if (flash_lock() == ERROR)
        goto end;

    __enable_irq();
    return OK;
end:
    if (flash_lock() == ERROR)
        while (1);

    __enable_irq();
    return ERROR;
}

/**
  * @brief  Read data from flash, and store in buffer.
  * @param  ram_addr: The stored buffer's address.
  * @param  addr: The start address in flash to read.
  * @param  len: The length of byte to read.
  * @retval Status, see @ref ald_status_t.
  */
__RAMFUNC static ald_status_t __flash_read(uint32_t ram_addr[], uint32_t addr, uint32_t len)
{
    uint32_t i;

    if (!len)
        return ERROR;

    for (i = 0; i < len; i++)
    {
        ram_addr[i] = ((uint32_t *)addr)[i];
    }

    return OK;
}

/**
  * @brief  Check whether the flash between the given address section
  *         have been writen, if it have been writen, return TRUE, else
  *         return FALSE.
  * @param  begin_addr: The begin address.
  * @param  end_addr: The end address.
  * @retval The check result
  *         - TRUE
  *         - FALSE
  */
__RAMFUNC static type_bool_t page_have_writen(uint32_t begin_addr, uint32_t end_addr)
{
    uint8_t *addr_to_read;
    uint8_t value;
    uint32_t index;

    /* Check the parameters */
    assert_param(IS_FLASH_ADDRESS(begin_addr));
    assert_param(IS_FLASH_ADDRESS(end_addr));

    addr_to_read = (uint8_t *)begin_addr;
    index        = begin_addr;
    value        = 0xFF;

    if (begin_addr > end_addr)
        return FALSE;

    while (index++ <= end_addr)
    {
        value = *addr_to_read++;

        if (value != 0xFF)
            break;
    }

    return value == 0xFF ? FALSE : TRUE;
}

/**
  * @}
  */

/** @defgroup FLASH_Exported_Functions FLASH Exported Functions
  * @verbatim
 ===============================================================================
              ##### Flash oprate functions #####
 ===============================================================================
  [..]
    This section provides functions allowing to operate flash, such as read and write.

    @endverbatim
  * @{
  */

/**
  * @brief  Write the give bytes to the given address section.
  * @param  addr: The start address to write.
  * @param  buf: The bytes' address.
  * @param  len: The length to write,and multiple of 2.
  * @retval Status, see @ref ald_status_t.
  */

__RAMFUNC ald_status_t flash_write(uint32_t addr, uint8_t *buf, uint16_t len)
{
    uint32_t index = 0;
    uint32_t para = 0;
    uint32_t index2 = 0;
    uint32_t start_write_addr;
    uint32_t end_write_addr;
    uint32_t start_word_addr;
    uint32_t end_word_addr;
    uint16_t len_to_write;
    uint32_t len_index;
    type_bool_t need_erase_page;

    assert_param(IS_FLASH_ADDRESS(addr));
    assert_param(IS_FLASH_ADDRESS(addr + len - 1));

    len_to_write = len;

    while (len_to_write > 0)
    {
        need_erase_page = FALSE;

        for (index = 0; index < FLASH_PAGE_SIZE; index++)
            write_buf[index] = 0xFF;

        start_write_addr = addr + (len - len_to_write);
        end_write_addr   = addr + len - 1;
        end_write_addr   = FLASH_PAGE_ADDR(start_write_addr) == FLASH_PAGE_ADDR(end_write_addr)
                           ? end_write_addr : FLASH_PAGEEND_ADDR(start_write_addr);
        need_erase_page  = page_have_writen(FLASH_WORD_ADDR(start_write_addr),
                                            FLASH_WORDEND_ADDR(end_write_addr));

        if (need_erase_page)
        {
            if (ERROR == __flash_read((uint32_t *)write_buf, FLASH_PAGE_ADDR(start_write_addr),
                                      FLASH_PAGE_SIZE >> 2))
                return ERROR;

            if (ERROR == flash_page_erase(FLASH_PAGE_ADDR(start_write_addr)))
                return ERROR;

            para   = end_write_addr & (FLASH_PAGE_SIZE - 1);
            index  = start_write_addr & (FLASH_PAGE_SIZE - 1);
            index2 = len - len_to_write;

            while (index <= para)
                write_buf[index++] = buf[index2++];

            index2     = 0;
            index      = FLASH_PAGE_ADDR(start_write_addr);
            para       = FLASH_PAGE_ADDR(start_write_addr) + FLASH_PAGE_SIZE;
            len_index  = FLASH_PAGE_SIZE;
        }
        else
        {
            para   = end_write_addr & (FLASH_PAGE_SIZE - 1);
            index  = start_write_addr & (FLASH_PAGE_SIZE - 1);
            index2 = len - len_to_write;

            while (index <= para)
                write_buf[index++] = buf[index2++];

            start_word_addr = FLASH_WORD_ADDR(start_write_addr);
            end_word_addr   = FLASH_WORDEND_ADDR(end_write_addr);
            index2          = (FLASH_WORD_ADDR(start_word_addr) - FLASH_PAGE_ADDR(start_word_addr));
            index           = start_word_addr;
            len_index       = end_word_addr - start_word_addr + 1;
        }

        if (ERROR == flash_word_program(index, (uint32_t *)(write_buf + index2), (len_index >> 3), FLASH_FIFO))
            return ERROR;

        len_to_write = len_to_write - (end_write_addr - start_write_addr + 1);
    }

    return OK;
}

/**
  * @brief  erase The flash between the given address section.
  * @param  addr: The start address to erase.
  * @param  len: The length to erase.
  * @retval Status, see @ref ald_status_t.
  */
__RAMFUNC ald_status_t flash_erase(uint32_t addr, uint16_t len)
{
    int32_t index;
    int32_t para;
    int32_t start_erase_addr;
    int32_t end_erase_addr;
    uint16_t len_not_erase;
    uint32_t len_index;
    type_bool_t page_need_save;

    assert_param(IS_FLASH_ADDRESS(addr));
    assert_param(IS_FLASH_ADDRESS(addr + len - 1));

    len_not_erase = len;

    while (len_not_erase > 0)
    {
        page_need_save = FALSE;

        start_erase_addr = addr + len - len_not_erase;
        end_erase_addr   = addr + len - 1;
        end_erase_addr   = (FLASH_PAGE_ADDR(start_erase_addr) == FLASH_PAGE_ADDR(end_erase_addr))
                           ? end_erase_addr : FLASH_PAGEEND_ADDR(start_erase_addr);

        if (start_erase_addr != FLASH_PAGE_ADDR(start_erase_addr))
        {
            if (page_have_writen(FLASH_PAGE_ADDR(start_erase_addr), (start_erase_addr - 1)))
                page_need_save = TRUE;
        }
        if (end_erase_addr != FLASH_PAGEEND_ADDR(end_erase_addr))
        {
            if (page_have_writen((end_erase_addr + 1), FLASH_PAGEEND_ADDR(end_erase_addr)))
                page_need_save = TRUE;
        }

        if (page_need_save)
        {
            if (ERROR == __flash_read((uint32_t *)write_buf, FLASH_PAGE_ADDR(start_erase_addr),
                                      FLASH_PAGE_SIZE >> 2))
            {
                __enable_irq();
                return ERROR;
            }
        }

        if (ERROR == flash_page_erase(FLASH_PAGE_ADDR(start_erase_addr)))
        {
            __enable_irq();
            return ERROR;
        }

        if (page_need_save)
        {
            para  = end_erase_addr & (FLASH_PAGE_SIZE - 1);
            index = start_erase_addr & (FLASH_PAGE_SIZE - 1);

            while (index <= para)
                write_buf[index++] = 0xFF;

            index     = FLASH_PAGE_ADDR(start_erase_addr);
            len_index = FLASH_PAGE_SIZE;
            if (ERROR == flash_word_program(index, (uint32_t *)write_buf, (len_index >> 3), FLASH_FIFO))
            {
                __enable_irq();
                return ERROR;
            }
        }
        len_not_erase = len_not_erase - (end_erase_addr - start_erase_addr + 1);
    }

    return OK;
}

/**
  * @brief  read the specified length bytes from flash, and store to the specified area.
  * @param  ram_addr: the specified area to store the reading bytes.
  * @param  addr: the start address.
  * @param  len: the length to read.
  * @retval Status, see @ref ald_status_t.
  */
__RAMFUNC ald_status_t flash_read(uint32_t *ram_addr, uint32_t addr, uint16_t len)
{
    uint32_t temp;

    assert_param(IS_4BYTES_ALIGN(ram_addr));
    assert_param(IS_FLASH_ADDRESS(addr));
    assert_param(IS_FLASH_ADDRESS(addr + len - 1));

    temp = (uint32_t)ram_addr;

    if (((temp & 0x3) != 0) || (((addr) & 0x3) != 0))
        return ERROR;

    return __flash_read(ram_addr, addr, len) == ERROR ? ERROR : OK;
}

/**
  * @}
  */

#endif

/**
  * @}
  */

/**
  * @}
  */