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
203
204
205
206
207
208
209
210
211
use crate::pac::EXMC;
use crate::rcu::{Rcu, Enable};
use crate::gpio::gpiod::{PD0, PD1, PD4, PD5, PD6, PD7, PD8, PD9, PD10, PD11, PD12, PD13, PD14, PD15};
use crate::gpio::gpioe::{PE0, PE1, PE2, PE3, PE4, PE5, PE6, PE7, PE8, PE9, PE10, PE11, PE12, PE13, PE14, PE15};
use crate::gpio::{Alternate, PushPull};
use vcell::VolatileCell;
const REGION_SIZE: usize = 64*1024*1024;
const REGION_PTR: *const () = 0x6000_0000 as *const ();
pub trait ExmcExt {
fn configure(self, pins: ExmcPins, conf: ExmcConfiguration, timing_conf: ExmcTimingConfiguration, rcu: &mut Rcu) -> Exmc;
}
impl ExmcExt for EXMC {
fn configure(self, pins: ExmcPins, conf: ExmcConfiguration, timing_conf: ExmcTimingConfiguration, rcu: &mut Rcu) -> Exmc {
Exmc::new(self, pins, conf, timing_conf, rcu)
}
}
pub struct Exmc {
regs: EXMC,
pins: ExmcPins,
}
impl Exmc {
pub fn new(regs: EXMC, pins: ExmcPins, conf: ExmcConfiguration, timing_conf: ExmcTimingConfiguration, rcu: &mut Rcu) -> Self {
EXMC::enable(rcu);
regs.snctl0.write(|w| unsafe {
w.nrmux().bit(conf.address_data_mux_enabled);
w.nrtp().bits(conf.memory_type as u8);
w.nrw().bits(conf.databus_width as u8);
w.nren().bit(conf.memory_type == MemoryType::NORFlash);
w.nrwtpol().bit(conf.nwait_polarity == NwaitPolarity::ActiveHigh);
w.wren().bit(conf.memory_write_enabled);
w.nrwten().bit(conf.nwait_signal_enabled);
w.asyncwait().bit(conf.async_wait_enabled);
w
});
regs.sntcfg0.write(|w| unsafe {
w.aset().bits(timing_conf.address_setup_time);
w.ahld().bits(timing_conf.address_hold_time);
w.dset().bits(timing_conf.data_setup_time);
w.buslat().bits(timing_conf.bus_latency)
});
regs.snctl0.modify(|_, w| w.nrbken().set_bit());
Exmc {
regs,
pins,
}
}
pub fn release(self) -> (EXMC, ExmcPins) {
self.regs.snctl0.modify(|_, w| w.nrbken().clear_bit());
(self.regs, self.pins)
}
pub fn as_u8_slice(&self) -> &[VolatileCell<u8>] {
let ptr = REGION_PTR as *const VolatileCell<u8>;
unsafe { core::slice::from_raw_parts(ptr, REGION_SIZE) }
}
pub fn as_u16_slice(&self) -> &[VolatileCell<u16>] {
let ptr = REGION_PTR as *const VolatileCell<u16>;
unsafe { core::slice::from_raw_parts(ptr, REGION_SIZE / 2) }
}
pub fn as_u32_slice(&self) -> &[VolatileCell<u32>] {
let ptr = REGION_PTR as *const VolatileCell<u32>;
unsafe { core::slice::from_raw_parts(ptr, REGION_SIZE / 4) }
}
}
pub struct ExmcConfiguration {
pub async_wait_enabled: bool,
pub nwait_signal_enabled: bool,
pub memory_write_enabled: bool,
pub nwait_polarity: NwaitPolarity,
pub databus_width: DataBusWidth,
pub memory_type: MemoryType,
pub address_data_mux_enabled: bool,
}
#[repr(u8)]
#[derive(Copy, Clone, PartialEq)]
pub enum NwaitPolarity {
ActiveLow = 0,
ActiveHigh = 1,
}
#[repr(u8)]
#[derive(Copy, Clone, PartialEq)]
pub enum DataBusWidth {
Width8Bits = 0,
Width16Bits = 1,
}
#[repr(u8)]
#[derive(Copy, Clone, PartialEq)]
pub enum MemoryType {
SRAM = 0,
PSRAM = 1,
NORFlash = 2,
}
#[derive(Copy, Clone)]
pub struct ExmcTimingConfiguration {
address_setup_time: u8,
address_hold_time: u8,
data_setup_time: u8,
bus_latency: u8,
}
impl Default for ExmcTimingConfiguration {
fn default() -> Self {
Self {
address_setup_time: 0xf,
address_hold_time: 0xf,
data_setup_time: 0xff,
bus_latency: 0xf,
}
}
}
impl ExmcTimingConfiguration {
pub fn address_setup_time(&mut self, hclk_periods: u32) -> &mut Self {
assert!(1 <= hclk_periods && hclk_periods < 16);
self.address_setup_time = (hclk_periods - 1) as u8;
self
}
pub fn address_hold_time(&mut self, hclk_periods: u32) -> &mut Self {
assert!(2 <= hclk_periods && hclk_periods < 16);
self.address_hold_time = (hclk_periods - 1) as u8;
self
}
pub fn data_setup_time(&mut self, hclk_periods: u32) -> &mut Self {
assert!(2 <= hclk_periods && hclk_periods < 256);
self.data_setup_time = (hclk_periods - 1) as u8;
self
}
pub fn bus_latency(&mut self, hclk_periods: u32) -> &mut Self {
assert!(1 <= hclk_periods && hclk_periods < 16);
self.bus_latency = (hclk_periods - 1) as u8;
self
}
}
#[allow(dead_code)]
pub struct ExmcPins {
pub d0: PD14<Alternate<PushPull>>,
pub d1: PD15<Alternate<PushPull>>,
pub d2: PD0<Alternate<PushPull>>,
pub d3: PD1<Alternate<PushPull>>,
pub d4: PE7<Alternate<PushPull>>,
pub d5: PE8<Alternate<PushPull>>,
pub d6: PE9<Alternate<PushPull>>,
pub d7: PE10<Alternate<PushPull>>,
pub d8: Option<PE11<Alternate<PushPull>>>,
pub d9: Option<PE12<Alternate<PushPull>>>,
pub d10: Option<PE13<Alternate<PushPull>>>,
pub d11: Option<PE14<Alternate<PushPull>>>,
pub d12: Option<PE15<Alternate<PushPull>>>,
pub d13: Option<PD8<Alternate<PushPull>>>,
pub d14: Option<PD9<Alternate<PushPull>>>,
pub d15: Option<PD10<Alternate<PushPull>>>,
pub a16: Option<PD11<Alternate<PushPull>>>,
pub a17: Option<PD12<Alternate<PushPull>>>,
pub a18: Option<PD13<Alternate<PushPull>>>,
pub a19: Option<PE3<Alternate<PushPull>>>,
pub a20: Option<PE4<Alternate<PushPull>>>,
pub a21: Option<PE5<Alternate<PushPull>>>,
pub a22: Option<PE6<Alternate<PushPull>>>,
pub a23: Option<PE2<Alternate<PushPull>>>,
pub noe: Option<PD4<Alternate<PushPull>>>,
pub nwe: Option<PD5<Alternate<PushPull>>>,
pub nwait: Option<PD6<Alternate<PushPull>>>,
pub ne0: Option<PD7<Alternate<PushPull>>>,
pub nbl0: Option<PE0<Alternate<PushPull>>>,
pub nbl1: Option<PE1<Alternate<PushPull>>>,
}