-
Notifications
You must be signed in to change notification settings - Fork 1
/
flash.H
43 lines (34 loc) · 1.18 KB
/
flash.H
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
#include <libopencm3/stm32/flash.h>
#define FLASH_PAGE_SIZE 1024
#define FLASH_PAGE_NUM_MAX 32
static uint32_t flash_program_data(uint32_t start_address, uint8_t *input_data, uint16_t num_elements) {
uint16_t iter;
uint32_t current_address = start_address;
uint32_t page_address = start_address;
uint32_t flash_status = 0;
/*check if start_address is in proper range*/
if((start_address - FLASH_BASE) >= (FLASH_PAGE_SIZE * (FLASH_PAGE_NUM_MAX+1)))
return 1;
/*calculate current page address*/
if(start_address % FLASH_PAGE_SIZE)
page_address -= (start_address % FLASH_PAGE_SIZE);
flash_unlock();
/*Erasing page*/
flash_erase_page(page_address);
flash_status = flash_get_status_flags();
if(flash_status != FLASH_SR_EOP)
return flash_status;
/*programming flash memory*/
for(iter=0; iter<num_elements; iter += 4)
{
/*programming word data*/
flash_program_word(current_address+iter, *((uint32_t*)(input_data + iter)));
flash_status = flash_get_status_flags();
if(flash_status != FLASH_SR_EOP)
return flash_status;
/*verify if correct data is programmed*/
if(*((uint32_t*)(current_address+iter)) != *((uint32_t*)(input_data + iter)))
return 2;
}
return 0;
}