forked from grblHAL/RP2040
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash.c
61 lines (41 loc) · 1.77 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
flash.c - driver code for RP2040 ARM processor
Part of grblHAL
Copyright (c) 2021-2022 Terje Io
This code reads/writes the whole RAM-based emulated EPROM contents from/to flash
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
/*
IMPORTANT! If both cores are in use synchronization has to be added since flash cannot be read during programming
See 4.1.8. hardware_flash in the SDK documentation.
*/
#include <string.h>
#include "hardware/flash.h"
#include "hardware/regs/addressmap.h"
#include "driver.h"
#define FLASH_TARGET_OFFSET (2016 * 1024) // Last 32K
static const uint8_t *flash_target = (const uint8_t *)(XIP_BASE + FLASH_TARGET_OFFSET); // Last page start adress
bool memcpy_from_flash (uint8_t *dest)
{
memcpy(dest, flash_target, hal.nvs.size);
return true;
}
bool memcpy_to_flash (uint8_t *source)
{
if (!memcmp(source, flash_target, hal.nvs.size))
return true;
__disable_irq();
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE); // 4K
flash_range_program(FLASH_TARGET_OFFSET, source, FLASH_PAGE_SIZE * (hal.nvs.size / FLASH_PAGE_SIZE + (hal.nvs.size % FLASH_PAGE_SIZE ? 1 :0)));
__enable_irq();
return !memcmp(source, flash_target, hal.nvs.size);
}