-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrp2040_flash.cc
125 lines (99 loc) · 3.47 KB
/
rp2040_flash.cc
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
//---------------------------------------------------------------------------
#include JAVELIN_BOARD_CONFIG
#include "javelin/flash.h"
#include <hardware/flash.h>
#include <hardware/sync.h>
#include <string.h>
//---------------------------------------------------------------------------
extern "C" char __flash_binary_end[];
static bool IsWritableRange(const void *p) { return p >= __flash_binary_end; }
void Flash::EraseBlock(const void *target, size_t size) {
if (!IsWritableRange(target)) {
return;
}
const uint8_t *const t = (const uint8_t *)target;
size_t eraseStart = 0;
size_t eraseEnd = 0;
for (size_t i = 0; i < size; i += 4096) {
if (RequiresErase(t + i, 4096)) {
if (eraseStart != eraseEnd) {
eraseEnd = i + 4096;
} else {
eraseStart = i;
eraseEnd = i + 4096;
}
}
}
const size_t eraseSize = eraseEnd - eraseStart;
if (eraseSize != 0) {
instance.erasedBytes += eraseSize;
const uint32_t interrupts = save_and_disable_interrupts();
flash_range_erase((intptr_t)t + eraseStart - XIP_BASE, eraseSize);
restore_interrupts(interrupts);
}
}
void Flash::WriteBlock(const void *target, const void *data, size_t size) {
if (!IsWritableRange(target)) {
return;
}
const uint8_t *const t = (const uint8_t *)target;
const uint8_t *const d = (const uint8_t *)data;
size_t eraseStart = 0;
size_t eraseEnd = 0;
for (size_t i = 0; i < size; i += 4096) {
if (RequiresErase(t + i, d + i, 4096)) {
if (eraseStart != eraseEnd) {
eraseEnd = i + 4096;
} else {
eraseStart = i;
eraseEnd = i + 4096;
}
}
}
const size_t eraseSize = eraseEnd - eraseStart;
if (eraseSize != 0) {
instance.erasedBytes += eraseSize;
const uint32_t interrupts = save_and_disable_interrupts();
flash_range_erase((intptr_t)t + eraseStart - XIP_BASE, eraseSize);
restore_interrupts(interrupts);
}
size_t programStart = 0;
size_t programEnd = 0;
for (size_t i = 0; i < size; i += 256) {
if (RequiresProgram(t + i, d + i, 256)) {
if (programStart != programEnd) {
programEnd = i + 256;
} else {
programStart = i;
programEnd = i + 256;
}
}
}
const size_t programSize = programEnd - programStart;
if (programSize) {
instance.programmedBytes += programSize;
const uint32_t interrupts = save_and_disable_interrupts();
flash_range_program((intptr_t)t + programStart - XIP_BASE, d + programStart,
programSize);
restore_interrupts(interrupts);
}
// Do a check, to ensure it is programmed accurately.
if (memcmp(target, data, size) != 0) {
// If it didn't, then do a full erase/program cycle.
instance.erasedBytes += size;
instance.programmedBytes += size;
instance.reprogrammedBytes += size;
const uint32_t interrupts = save_and_disable_interrupts();
flash_range_erase((intptr_t)t - XIP_BASE, size);
flash_range_program((intptr_t)t - XIP_BASE, d, size);
restore_interrupts(interrupts);
}
}
//---------------------------------------------------------------------------
bool Flash::IsScriptMemory(const void *start, const void *end) {
const void *byteCodeStart = SCRIPT_BYTE_CODE;
const void *byteCodeEnd = SCRIPT_BYTE_CODE + MAXIMUM_BUTTON_SCRIPT_SIZE;
// Intersect if Max(start, byteCodeStart) < Min(end, byteCodeEnd)
return start < byteCodeEnd && byteCodeStart < end;
}
//---------------------------------------------------------------------------