amebazii/conf/
sysctrl.rs

1use serde::{Deserialize, Serialize};
2use serde_aux::prelude::deserialize_option_number_from_string;
3
4use crate::types::sysctrl::{FlashInfo, ForceOldImage, SpiConfig, SystemData};
5
6use super::DataArray;
7
8/// Configuration for the system data, containing options for various system parameters.
9#[derive(Debug, Serialize, Deserialize)]
10pub struct SystemDataCfg {
11    /// The address of the second OTA partition, if available.
12    #[serde(deserialize_with = "deserialize_option_number_from_string")]
13    pub ota2_addr: Option<u32>,
14    /// The size of the second OTA partition, if available.
15    #[serde(deserialize_with = "deserialize_option_number_from_string")]
16    pub ota2_size: Option<u32>,
17
18    ///  Configuration for the old image trap, typically used to control whether an old image is forced into memory.
19    #[serde(default)]
20    pub old_img_trap: ForceOldImage,
21
22    /// Configuration of the SPI interface, including IO mode and speed.
23    #[serde(default)]
24    pub spi_cfg: SpiConfig,
25
26    /// Information related to the flash memory (e.g., flash ID, size).
27    #[serde(default)]
28    pub flash_info: FlashInfo,
29
30    /// Baud rate for the UART logging interface.
31    #[serde(deserialize_with = "deserialize_option_number_from_string")]
32    pub ulog_baud: Option<u32>,
33
34    /// SPI calibration configuration, stored as raw data.
35    pub spic_calibcfg: Option<DataArray<0x30>>,
36
37    /// Bluetooth parameter data, stored as raw data.
38    pub bt_parameter_data: Option<DataArray<0x20>>,
39}
40
41impl Default for SystemDataCfg {
42    /// Returns the default configuration for `SystemDataCfg`.
43    ///
44    /// # Returns
45    /// A `SystemDataCfg` with default values.
46    fn default() -> Self {
47        Self {
48            ota2_addr: None,
49            ota2_size: None,
50            old_img_trap: ForceOldImage::default(),
51            spi_cfg: SpiConfig::default(),
52            flash_info: FlashInfo::default(),
53            ulog_baud: None,
54            spic_calibcfg: None,
55            bt_parameter_data: None,
56        }
57    }
58}
59
60impl TryInto<SystemData> for SystemDataCfg {
61    type Error = crate::error::Error;
62
63    /// Tries to convert `SystemDataCfg` into a `SystemData` instance.
64    ///
65    /// This method attempts to convert the configuration (`SystemDataCfg`) into a complete
66    /// `SystemData` object. It populates the `SystemData` fields based on the values in the
67    /// configuration struct, ensuring that required fields are filled, and default values are used
68    /// where no data is provided.
69    ///
70    /// # Parameters
71    /// - `self`: The `SystemDataCfg` instance to be converted.
72    ///
73    /// # Returns
74    /// A `Result<SystemData, Error>`:
75    /// - `Ok(SystemData)`: The conversion succeeded, and the data is populated.
76    /// - `Err(Error)`: An error occurred during conversion, for example, invalid data during
77    ///   conversion (e.g., when trying to convert data that doesn't fit the expected type).
78    ///
79    /// # Example
80    /// ```rust
81    /// let config = SystemDataCfg::default();
82    /// let sysdata: SystemData = config.try_into().unwrap();
83    /// ```
84    fn try_into(self) -> Result<SystemData, Self::Error> {
85        let mut sysdata = SystemData::default();
86        sysdata.ota2_addr = self.ota2_addr;
87        sysdata.ota2_size = self.ota2_size;
88        sysdata.old_img_trap = self.old_img_trap;
89        sysdata.spi_cfg = self.spi_cfg;
90        sysdata.flash_info = self.flash_info;
91        sysdata.ulog_baud = self.ulog_baud.unwrap_or(0xFFFF_FFFF);
92
93        if let Some(bt_parameter_data) = self.bt_parameter_data {
94            // revisit: custom error handling
95            sysdata.set_pt_paramdata(Some(bt_parameter_data.data.try_into().unwrap()));
96        }
97
98        if let Some(spic_calibcfg) = self.spic_calibcfg {
99            sysdata.set_spic_calibcfg(Some(spic_calibcfg.data.try_into().unwrap()));
100        }
101
102        Ok(sysdata)
103    }
104}