esp_hal/psram/
esp32s31.rs1use super::{EXTMEM_ORIGIN, PsramSize};
4use crate::{
5 clock::ll::{ClockTree, PsramFunctionClockConfig, PsramInstance},
6 peripherals::HP_SYS_CLKRST,
7};
8
9mod oct_hex;
10
11#[cfg(all(idle_frequency_scaling, psram_idle_low_speed_switch))]
12pub(crate) use oct_hex::low_speed;
13
14#[derive(Copy, Clone, Debug, Default, PartialEq)]
16#[cfg_attr(feature = "defmt", derive(defmt::Format))]
17#[instability::unstable]
18pub struct PsramConfig {
19 pub size: PsramSize,
21
22 pub timing: PsramTimingParams,
24 }
28
29#[derive(Copy, Clone, Debug, PartialEq)]
34#[cfg_attr(feature = "defmt", derive(defmt::Format))]
35pub struct PsramTimingParams {
36 pub clock_source: PsramFunctionClockConfig,
38
39 pub clock: u32,
41
42 pub mr0_rl: u8,
44
45 pub mr4_wl: u8,
47
48 pub rd_dummy_bits: u8,
52
53 pub wr_dummy_bits: u8,
57
58 pub reg_dummy_bits: u32,
60}
61
62impl Default for PsramTimingParams {
63 fn default() -> Self {
64 Self::MHZ_200
65 }
66}
67
68impl PsramTimingParams {
69 pub const MHZ_20: Self = Self {
71 clock_source: PsramFunctionClockConfig::Mpll,
72 clock: 20,
73 mr0_rl: 2,
74 mr4_wl: 2,
75 rd_dummy_bits: 17,
76 wr_dummy_bits: 7,
77 reg_dummy_bits: 8,
78 };
79
80 pub const MHZ_80: Self = Self {
82 clock_source: PsramFunctionClockConfig::Mpll,
83 clock: 80,
84 mr0_rl: 2,
85 mr4_wl: 2,
86 rd_dummy_bits: 17,
87 wr_dummy_bits: 7,
88 reg_dummy_bits: 8,
89 };
90
91 pub const MHZ_125: Self = Self {
93 clock_source: PsramFunctionClockConfig::Mpll,
94 clock: 125,
95 mr0_rl: 2,
96 mr4_wl: 2,
97 rd_dummy_bits: 17,
98 wr_dummy_bits: 7,
99 reg_dummy_bits: 8,
100 };
101
102 pub const MHZ_200: Self = Self {
104 clock_source: PsramFunctionClockConfig::Mpll,
105 clock: 200,
106 mr0_rl: 4,
107 mr4_wl: 1,
108 rd_dummy_bits: 25,
109 wr_dummy_bits: 11,
110 reg_dummy_bits: 12,
111 };
112
113 pub const MHZ_250: Self = Self {
115 clock_source: PsramFunctionClockConfig::Mpll,
116 clock: 250,
117 mr0_rl: 6,
118 mr4_wl: 3,
119 rd_dummy_bits: 33,
120 wr_dummy_bits: 15,
121 reg_dummy_bits: 16,
122 };
123}
124
125#[crate::ram]
127pub(crate) fn init_psram(config: &mut PsramConfig) -> bool {
128 enable_psram_mspi();
130 reset_psram_mspi();
131
132 ClockTree::with(|clocks| {
133 PsramInstance::Psram.configure_function_clock(clocks, config.timing.clock_source);
134 PsramInstance::Psram.request_function_clock(clocks);
135 });
136
137 if !oct_hex::set_bus_clock(config.timing.clock) {
139 return false;
140 }
141 oct_hex::enable_dll();
142 oct_hex::psram_pad_init(false); oct_hex::set_cs_timing();
144
145 oct_hex::init_mr_registers(&config.timing, false);
147
148 if config.size.is_auto() {
149 config.size = PsramSize::Size(oct_hex::psram_detect_size(&config.timing));
150 }
151
152 oct_hex::configure_psram_mspi(&config.timing, false);
154 oct_hex::mmu_map_psram(config.size.get());
155
156 true
157}
158
159pub(crate) fn map_psram(config: PsramConfig) -> core::ops::Range<usize> {
160 let start = EXTMEM_ORIGIN;
161 start..start + config.size.get()
162}
163
164fn enable_psram_mspi() {
165 HP_SYS_CLKRST::regs()
166 .psram_ctrl0()
167 .modify(|_, w| w.sys_clk_en().set_bit());
168}
169
170fn reset_psram_mspi() {
171 HP_SYS_CLKRST::regs().psram_ctrl0().modify(|_, w| {
172 w.axi_rst_en().set_bit();
173 w.apb_rst_en().set_bit()
174 });
175 HP_SYS_CLKRST::regs().psram_ctrl0().modify(|_, w| {
176 w.axi_rst_en().clear_bit();
177 w.apb_rst_en().clear_bit()
178 });
179}