forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash.c
132 lines (110 loc) · 4.29 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
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
126
127
128
129
130
131
132
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Ayke van Laethem
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/mpconfig.h"
#if (MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE || MICROPY_MBFS) && BLUETOOTH_SD
#include "drivers/flash.h"
#include "drivers/bluetooth/ble_drv.h"
#include "nrf_soc.h"
// Rotates bits in `value` left `shift` times.
STATIC inline uint32_t rotate_left(uint32_t value, uint32_t shift) {
return (value << shift) | (value >> (32 - shift));
}
STATIC volatile flash_state_t flash_operation_state = FLASH_STATE_BUSY;
STATIC void operation_init(void) {
flash_operation_state = FLASH_STATE_BUSY;
}
void flash_operation_finished(flash_state_t result) {
flash_operation_state = result;
}
STATIC bool operation_wait(uint32_t result) {
if (ble_drv_stack_enabled() != 1) {
// SoftDevice is not enabled, no event will be generated.
return result == NRF_SUCCESS;
}
if (result != NRF_SUCCESS) {
// In all other (non-success) cases, the command hasn't been
// started and no event will be generated.
return false;
}
// Wait until the event has been generated.
while (flash_operation_state == FLASH_STATE_BUSY) {
sd_app_evt_wait();
}
// Now we can safely continue, flash operation has completed.
return flash_operation_state == FLASH_STATE_SUCCESS;
}
void flash_write_byte(uint32_t address, uint8_t b) {
uint32_t address_aligned = address & ~3;
// Value to write - leave all bits that should not change at 0xff.
uint32_t value = 0xffffff00 | b;
// Rotate bits in value to an aligned position.
value = rotate_left(value, (address & 3) * 8);
while (1) {
operation_init();
uint32_t result = sd_flash_write((uint32_t*)address_aligned, &value, 1);
if (operation_wait(result)) break;
}
}
void flash_page_erase(uint32_t pageaddr) {
while (1) {
operation_init();
uint32_t result = sd_flash_page_erase(pageaddr / FLASH_PAGESIZE);
if (operation_wait(result)) break;
}
}
void flash_write_bytes(uint32_t dst, const uint8_t *src, uint32_t num_bytes) {
const uint8_t *src_end = src + num_bytes;
// sd_flash_write does not accept unaligned addresses so we have to
// work around that by writing all unaligned addresses byte-by-byte.
// Write first bytes to align the write address.
while (src != src_end && (dst & 0b11)) {
flash_write_byte(dst, *src);
dst++;
src++;
}
// Write as many words as possible.
// dst is now aligned, src possibly not.
while (src_end - src >= 4) {
uint8_t buf[4] __attribute__((aligned(4)));
for (int i = 0; i < 4; i++) {
buf[i] = ((uint8_t*)src)[i];
}
operation_init();
uint32_t result = sd_flash_write((uint32_t*)dst, (const uint32_t*)&buf, 1);
if (operation_wait(result)) {
// If it is successfully written, go to the next word.
src += 4;
dst += 4;
}
}
// Write remaining unaligned bytes.
while (src != src_end) {
flash_write_byte(dst, *src);
dst++;
src++;
}
}
#endif // (MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE || MICROPY_MBFS) && BLUETOOTH_SD