stm32f4xx/chip_specific/
flash.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright OxidOS Automotive SRL.
4//
5// Author: Ioan-Cristian CÎRSTEA <ioan.cirstea@oxidos.io>
6
7//! Chip-specific flash code
8
9use core::fmt::Debug;
10
11pub trait FlashChipSpecific {
12    type FlashLatency: RegisterToFlashLatency + Clone + Copy + PartialEq + Debug + Into<u32>;
13
14    // The number of wait cycles depends on two factors: system clock frequency and the supply
15    // voltage. Currently, this method assumes 2.7-3.6V voltage supply (default value).
16    // TODO: Take into account the power supply
17    //
18    // The number of wait cycles varies from chip to chip
19    fn get_number_wait_cycles_based_on_frequency(frequency_mhz: usize) -> Self::FlashLatency;
20}
21
22pub trait RegisterToFlashLatency {
23    fn convert_register_to_enum(flash_latency_register: u32) -> Self;
24}
25
26#[derive(Copy, Clone, PartialEq, Debug)]
27pub enum FlashLatency16 {
28    Latency0,
29    Latency1,
30    Latency2,
31    Latency3,
32    Latency4,
33    Latency5,
34    Latency6,
35    Latency7,
36    Latency8,
37    Latency9,
38    Latency10,
39    Latency11,
40    Latency12,
41    Latency13,
42    Latency14,
43    Latency15,
44}
45
46impl RegisterToFlashLatency for FlashLatency16 {
47    fn convert_register_to_enum(flash_latency_register: u32) -> Self {
48        match flash_latency_register {
49            0 => Self::Latency0,
50            1 => Self::Latency1,
51            2 => Self::Latency2,
52            3 => Self::Latency3,
53            4 => Self::Latency4,
54            5 => Self::Latency5,
55            6 => Self::Latency6,
56            7 => Self::Latency7,
57            8 => Self::Latency8,
58            9 => Self::Latency9,
59            10 => Self::Latency10,
60            11 => Self::Latency11,
61            12 => Self::Latency12,
62            13 => Self::Latency13,
63            14 => Self::Latency14,
64            // The hardware supports 4-bit flash latency
65            _ => Self::Latency15,
66        }
67    }
68}
69
70impl From<FlashLatency16> for u32 {
71    fn from(val: FlashLatency16) -> Self {
72        val as u32
73    }
74}