-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathnvmc.rs
More file actions
202 lines (174 loc) · 6.27 KB
/
nvmc.rs
File metadata and controls
202 lines (174 loc) · 6.27 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! HAL interface to the Non-Volatile Memory Controller (NVMC) peripheral.
use core::ops::Deref;
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
use crate::pac::nvmc;
#[cfg(any(feature = "9160", feature = "5340-app"))]
use crate::pac::nvmc_ns as nvmc;
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
use crate::pac::NVMC;
#[cfg(any(feature = "9160", feature = "5340-app"))]
use crate::pac::NVMC_NS as NVMC;
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
/// Interface to an NVMC instance.
pub struct Nvmc<T: Instance> {
nvmc: T,
storage: &'static mut [u32],
}
impl<T> Nvmc<T>
where
T: Instance,
{
/// Takes ownership of the peripheral and storage area.
pub fn new(nvmc: T, storage: &'static mut [u32]) -> Nvmc<T> {
Self { nvmc, storage }
}
/// Consumes `self` and returns back the raw peripheral and associated storage.
pub fn free(self) -> (T, &'static mut [u32]) {
(self.nvmc, self.storage)
}
fn enable_erase(&self) {
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
self.nvmc.config.write(|w| w.wen().een());
#[cfg(any(feature = "9160", feature = "5340-app"))]
self.nvmc.configns.write(|w| w.wen().een());
}
fn enable_read(&self) {
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
self.nvmc.config.write(|w| w.wen().ren());
#[cfg(any(feature = "9160", feature = "5340-app"))]
self.nvmc.configns.write(|w| w.wen().ren());
}
fn enable_write(&self) {
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
self.nvmc.config.write(|w| w.wen().wen());
#[cfg(any(feature = "9160", feature = "5340-app"))]
self.nvmc.configns.write(|w| w.wen().wen());
}
#[inline]
fn wait_ready(&self) {
while !self.nvmc.ready.read().ready().bit_is_set() {}
}
#[cfg(any(feature = "9160", feature = "5340-app"))]
#[inline]
fn wait_write_ready(&self) {
while !self.nvmc.readynext.read().readynext().bit_is_set() {}
}
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
#[inline]
fn erase_page(&mut self, offset: usize) {
let bits = &mut (self.storage[offset as usize >> 2]) as *mut _ as u32;
self.nvmc.erasepage().write(|w| unsafe { w.bits(bits) });
self.wait_ready();
}
#[cfg(any(feature = "9160", feature = "5340-app"))]
#[inline]
fn erase_page(&mut self, offset: usize) {
self.storage[offset as usize >> 2] = 0xffffffff;
self.wait_ready();
}
#[inline]
fn write_word(&mut self, offset: usize, word: u32) {
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
self.wait_ready();
#[cfg(any(feature = "9160", feature = "5340-app"))]
self.wait_write_ready();
self.storage[offset] = word;
cortex_m::asm::dmb();
}
}
impl<T> ReadNorFlash for Nvmc<T>
where
T: Instance,
{
type Error = NvmcError;
const READ_SIZE: usize = 4;
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
let offset = offset as usize;
let bytes_len = bytes.len();
let read_len = bytes_len + (Self::READ_SIZE - (bytes_len % Self::READ_SIZE));
let target_offset = offset + read_len;
if offset % Self::READ_SIZE == 0 && target_offset <= self.capacity() {
self.wait_ready();
let last_offset = target_offset - Self::READ_SIZE;
for offset in (offset..last_offset).step_by(Self::READ_SIZE) {
let word = self.storage[offset >> 2];
bytes[offset] = (word >> 24) as u8;
bytes[offset + 1] = (word >> 16) as u8;
bytes[offset + 2] = (word >> 8) as u8;
bytes[offset + 3] = (word >> 0) as u8;
}
let offset = last_offset;
let word = self.storage[offset >> 2];
let mut bytes_offset = offset;
if bytes_offset < bytes_len {
bytes[bytes_offset] = (word >> 24) as u8;
bytes_offset += 1;
if bytes_offset < bytes_len {
bytes[bytes_offset] = (word >> 16) as u8;
bytes_offset += 1;
if bytes_offset < bytes_len {
bytes[bytes_offset] = (word >> 8) as u8;
bytes_offset += 1;
if bytes_offset < bytes_len {
bytes[bytes_offset] = (word >> 0) as u8;
}
}
}
}
Ok(())
} else {
Err(NvmcError::Unaligned)
}
}
fn capacity(&self) -> usize {
self.storage.len() << 2
}
}
impl<T> NorFlash for Nvmc<T>
where
T: Instance,
{
const WRITE_SIZE: usize = 4;
const ERASE_SIZE: usize = 4 * 1024;
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
if from as usize % Self::ERASE_SIZE == 0 && to as usize % Self::ERASE_SIZE == 0 {
self.enable_erase();
for offset in (from..to).step_by(Self::ERASE_SIZE) {
self.erase_page(offset as usize >> 2);
}
self.enable_read();
Ok(())
} else {
Err(NvmcError::Unaligned)
}
}
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
let offset = offset as usize;
if offset % Self::WRITE_SIZE == 0 && bytes.len() % Self::WRITE_SIZE == 0 {
self.enable_write();
for offset in (offset..(offset + bytes.len())).step_by(Self::WRITE_SIZE) {
let word = ((bytes[offset] as u32) << 24)
| ((bytes[offset + 1] as u32) << 16)
| ((bytes[offset + 2] as u32) << 8)
| ((bytes[offset + 3] as u32) << 0);
self.write_word(offset >> 2, word);
}
self.enable_read();
Ok(())
} else {
Err(NvmcError::Unaligned)
}
}
}
pub trait Instance: Deref<Target = nvmc::RegisterBlock> + sealed::Sealed {}
impl Instance for NVMC {}
mod sealed {
use super::*;
pub trait Sealed {}
impl Sealed for NVMC {}
}
#[derive(Debug)]
pub enum NvmcError {
/// An operation was attempted on an unaligned boundary
Unaligned,
}