-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUserFlash.cpp
77 lines (65 loc) · 2 KB
/
UserFlash.cpp
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
/*------------------------------------------------------/
/ Copyright (c) 2021, Elehobica
/ Released under the BSD-2-Clause
/ refer to https://opensource.org/licenses/BSD-2-Clause
/------------------------------------------------------*/
#include "UserFlash.h"
#include <cstdio>
#include <cstring>
#include "pico/flash.h"
void _user_flash_program_core(void* ptr)
{
UserFlash* inst = static_cast<UserFlash*>(ptr);
inst->_program_core();
}
//=================================
// Implementation of UserFlash class
//=================================
UserFlash UserFlash::_instance; // Singleton
UserFlash& UserFlash::instance()
{
return _instance;
}
UserFlash::UserFlash()
{
memcpy(data, flashContents, sizeof(data));
}
UserFlash::~UserFlash()
{
}
void UserFlash::printInfo()
{
printf("=== UserFlash ===\n");
printf(" FlashSize: 0x%x (%d)\n", FlashSize, FlashSize);
printf(" UserReqSize: 0x%x (%d)\n", UserReqSize, UserReqSize);
printf(" EraseSize: 0x%x (%d)\n", EraseSize, EraseSize);
printf(" PagePgrSize: 0x%x (%d)\n", PagePgrSize, PagePgrSize);
printf(" UserFlashOfs: 0x%x (%d)\n", UserFlashOfs, UserFlashOfs);
}
void UserFlash::read(uint32_t flash_ofs, size_t size, void *buf)
{
if (flash_ofs + size <= PagePgrSize) {
memcpy(buf, &flashContents[flash_ofs], size);
}
}
void UserFlash::writeReserve(uint32_t flash_ofs, size_t size, const void *buf)
{
if (flash_ofs + size <= PagePgrSize) {
memcpy(&data[flash_ofs], buf, size);
}
}
void UserFlash::program()
{
// Need to stop interrupt during erase and program
// noted that if core1 is running, it must be stopped also if accessing flash
int result = flash_safe_execute(_user_flash_program_core, this, 100);
if (result != PICO_OK) {
printf("ERROR: failed to program UserFlash\n");
}
}
void UserFlash::_program_core()
{
flash_range_erase(UserFlashOfs, EraseSize);
flash_range_program(UserFlashOfs, data, FLASH_PAGE_SIZE);
memcpy(data, flashContents, sizeof(data));
}