-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflash.c
41 lines (36 loc) · 1021 Bytes
/
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
#include "ch.h"
#include "hal.h"
#include "flash.h"
#include "printf.h"
bool flash_read(char *buf, uint8_t len)
{
flash_error_t err;
flash_offset_t addr = CALDATA_ADDR;
err = flashRead(&FLASH_DEV, addr, len, (uint8_t *) buf);
if(err != FLASH_NO_ERROR) {
printf("Error reading flash: %d\r\n", err);
return false;
}
return true;
}
bool flash_write(char *buf, uint8_t len)
{
flash_error_t err;
flash_offset_t addr = CALDATA_ADDR;
err = flashStartEraseSector(&FLASH_DEV, CALDATA_SECTOR);
if(err != FLASH_NO_ERROR) {
printf("Error erasing flash: %d\r\n", err);
return false;
}
err = flashWaitErase((BaseFlash *)&FLASH_DEV);
if(err != FLASH_NO_ERROR) {
printf("Error waiting for flash erase: %d\r\n", err);
return false;
}
err = flashProgram(&FLASH_DEV, addr, len, (uint8_t *) buf);
if(err != FLASH_NO_ERROR) {
printf("Error writing flash: %d\r\n", err);
return false;
}
return true;
}