diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-05-30 22:42:49 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-05-30 22:42:49 +0200 |
| commit | c327c6cd6fc3c11cfaf83cf64591940d401c5f6b (patch) | |
| tree | 1e9f693a74b01ae930caa145ae282c24276510e3 /cyw43/src/lib.rs | |
| parent | 3cc0ec654a1e46fe7b8fa168942452303343cd84 (diff) | |
cyw43: move crate to subdir.
Diffstat (limited to 'cyw43/src/lib.rs')
| -rw-r--r-- | cyw43/src/lib.rs | 236 |
1 files changed, 236 insertions, 0 deletions
diff --git a/cyw43/src/lib.rs b/cyw43/src/lib.rs new file mode 100644 index 000000000..fd11f3674 --- /dev/null +++ b/cyw43/src/lib.rs | |||
| @@ -0,0 +1,236 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![allow(incomplete_features)] | ||
| 4 | #![feature(async_fn_in_trait, type_alias_impl_trait, concat_bytes)] | ||
| 5 | #![deny(unused_must_use)] | ||
| 6 | |||
| 7 | // This mod MUST go first, so that the others see its macros. | ||
| 8 | pub(crate) mod fmt; | ||
| 9 | |||
| 10 | mod bus; | ||
| 11 | mod consts; | ||
| 12 | mod countries; | ||
| 13 | mod events; | ||
| 14 | mod ioctl; | ||
| 15 | mod structs; | ||
| 16 | |||
| 17 | mod control; | ||
| 18 | mod nvram; | ||
| 19 | mod runner; | ||
| 20 | |||
| 21 | use core::slice; | ||
| 22 | |||
| 23 | use embassy_net_driver_channel as ch; | ||
| 24 | use embedded_hal_1::digital::OutputPin; | ||
| 25 | use events::Events; | ||
| 26 | use ioctl::IoctlState; | ||
| 27 | |||
| 28 | use crate::bus::Bus; | ||
| 29 | pub use crate::bus::SpiBusCyw43; | ||
| 30 | pub use crate::control::{Control, Error as ControlError}; | ||
| 31 | pub use crate::runner::Runner; | ||
| 32 | pub use crate::structs::BssInfo; | ||
| 33 | |||
| 34 | const MTU: usize = 1514; | ||
| 35 | |||
| 36 | #[allow(unused)] | ||
| 37 | #[derive(Clone, Copy, PartialEq, Eq)] | ||
| 38 | enum Core { | ||
| 39 | WLAN = 0, | ||
| 40 | SOCSRAM = 1, | ||
| 41 | SDIOD = 2, | ||
| 42 | } | ||
| 43 | |||
| 44 | impl Core { | ||
| 45 | fn base_addr(&self) -> u32 { | ||
| 46 | match self { | ||
| 47 | Self::WLAN => CHIP.arm_core_base_address, | ||
| 48 | Self::SOCSRAM => CHIP.socsram_wrapper_base_address, | ||
| 49 | Self::SDIOD => CHIP.sdiod_core_base_address, | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | #[allow(unused)] | ||
| 55 | struct Chip { | ||
| 56 | arm_core_base_address: u32, | ||
| 57 | socsram_base_address: u32, | ||
| 58 | socsram_wrapper_base_address: u32, | ||
| 59 | sdiod_core_base_address: u32, | ||
| 60 | pmu_base_address: u32, | ||
| 61 | chip_ram_size: u32, | ||
| 62 | atcm_ram_base_address: u32, | ||
| 63 | socram_srmem_size: u32, | ||
| 64 | chanspec_band_mask: u32, | ||
| 65 | chanspec_band_2g: u32, | ||
| 66 | chanspec_band_5g: u32, | ||
| 67 | chanspec_band_shift: u32, | ||
| 68 | chanspec_bw_10: u32, | ||
| 69 | chanspec_bw_20: u32, | ||
| 70 | chanspec_bw_40: u32, | ||
| 71 | chanspec_bw_mask: u32, | ||
| 72 | chanspec_bw_shift: u32, | ||
| 73 | chanspec_ctl_sb_lower: u32, | ||
| 74 | chanspec_ctl_sb_upper: u32, | ||
| 75 | chanspec_ctl_sb_none: u32, | ||
| 76 | chanspec_ctl_sb_mask: u32, | ||
| 77 | } | ||
| 78 | |||
| 79 | const WRAPPER_REGISTER_OFFSET: u32 = 0x100000; | ||
| 80 | |||
| 81 | // Data for CYW43439 | ||
| 82 | const CHIP: Chip = Chip { | ||
| 83 | arm_core_base_address: 0x18003000 + WRAPPER_REGISTER_OFFSET, | ||
| 84 | socsram_base_address: 0x18004000, | ||
| 85 | socsram_wrapper_base_address: 0x18004000 + WRAPPER_REGISTER_OFFSET, | ||
| 86 | sdiod_core_base_address: 0x18002000, | ||
| 87 | pmu_base_address: 0x18000000, | ||
| 88 | chip_ram_size: 512 * 1024, | ||
| 89 | atcm_ram_base_address: 0, | ||
| 90 | socram_srmem_size: 64 * 1024, | ||
| 91 | chanspec_band_mask: 0xc000, | ||
| 92 | chanspec_band_2g: 0x0000, | ||
| 93 | chanspec_band_5g: 0xc000, | ||
| 94 | chanspec_band_shift: 14, | ||
| 95 | chanspec_bw_10: 0x0800, | ||
| 96 | chanspec_bw_20: 0x1000, | ||
| 97 | chanspec_bw_40: 0x1800, | ||
| 98 | chanspec_bw_mask: 0x3800, | ||
| 99 | chanspec_bw_shift: 11, | ||
| 100 | chanspec_ctl_sb_lower: 0x0000, | ||
| 101 | chanspec_ctl_sb_upper: 0x0100, | ||
| 102 | chanspec_ctl_sb_none: 0x0000, | ||
| 103 | chanspec_ctl_sb_mask: 0x0700, | ||
| 104 | }; | ||
| 105 | |||
| 106 | pub struct State { | ||
| 107 | ioctl_state: IoctlState, | ||
| 108 | ch: ch::State<MTU, 4, 4>, | ||
| 109 | events: Events, | ||
| 110 | } | ||
| 111 | |||
| 112 | impl State { | ||
| 113 | pub fn new() -> Self { | ||
| 114 | Self { | ||
| 115 | ioctl_state: IoctlState::new(), | ||
| 116 | ch: ch::State::new(), | ||
| 117 | events: Events::new(), | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 123 | pub enum PowerManagementMode { | ||
| 124 | /// Custom, officially unsupported mode. Use at your own risk. | ||
| 125 | /// All power-saving features set to their max at only a marginal decrease in power consumption | ||
| 126 | /// as oppposed to `Aggressive`. | ||
| 127 | SuperSave, | ||
| 128 | |||
| 129 | /// Aggressive power saving mode. | ||
| 130 | Aggressive, | ||
| 131 | |||
| 132 | /// The default mode. | ||
| 133 | PowerSave, | ||
| 134 | |||
| 135 | /// Performance is prefered over power consumption but still some power is conserved as opposed to | ||
| 136 | /// `None`. | ||
| 137 | Performance, | ||
| 138 | |||
| 139 | /// Unlike all the other PM modes, this lowers the power consumption at all times at the cost of | ||
| 140 | /// a much lower throughput. | ||
| 141 | ThroughputThrottling, | ||
| 142 | |||
| 143 | /// No power management is configured. This consumes the most power. | ||
| 144 | None, | ||
| 145 | } | ||
| 146 | |||
| 147 | impl Default for PowerManagementMode { | ||
| 148 | fn default() -> Self { | ||
| 149 | Self::PowerSave | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | impl PowerManagementMode { | ||
| 154 | fn sleep_ret_ms(&self) -> u16 { | ||
| 155 | match self { | ||
| 156 | PowerManagementMode::SuperSave => 2000, | ||
| 157 | PowerManagementMode::Aggressive => 2000, | ||
| 158 | PowerManagementMode::PowerSave => 200, | ||
| 159 | PowerManagementMode::Performance => 20, | ||
| 160 | PowerManagementMode::ThroughputThrottling => 0, // value doesn't matter | ||
| 161 | PowerManagementMode::None => 0, // value doesn't matter | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | fn beacon_period(&self) -> u8 { | ||
| 166 | match self { | ||
| 167 | PowerManagementMode::SuperSave => 255, | ||
| 168 | PowerManagementMode::Aggressive => 1, | ||
| 169 | PowerManagementMode::PowerSave => 1, | ||
| 170 | PowerManagementMode::Performance => 1, | ||
| 171 | PowerManagementMode::ThroughputThrottling => 0, // value doesn't matter | ||
| 172 | PowerManagementMode::None => 0, // value doesn't matter | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | fn dtim_period(&self) -> u8 { | ||
| 177 | match self { | ||
| 178 | PowerManagementMode::SuperSave => 255, | ||
| 179 | PowerManagementMode::Aggressive => 1, | ||
| 180 | PowerManagementMode::PowerSave => 1, | ||
| 181 | PowerManagementMode::Performance => 1, | ||
| 182 | PowerManagementMode::ThroughputThrottling => 0, // value doesn't matter | ||
| 183 | PowerManagementMode::None => 0, // value doesn't matter | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | fn assoc(&self) -> u8 { | ||
| 188 | match self { | ||
| 189 | PowerManagementMode::SuperSave => 255, | ||
| 190 | PowerManagementMode::Aggressive => 10, | ||
| 191 | PowerManagementMode::PowerSave => 10, | ||
| 192 | PowerManagementMode::Performance => 1, | ||
| 193 | PowerManagementMode::ThroughputThrottling => 0, // value doesn't matter | ||
| 194 | PowerManagementMode::None => 0, // value doesn't matter | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | fn mode(&self) -> u32 { | ||
| 199 | match self { | ||
| 200 | PowerManagementMode::ThroughputThrottling => 1, | ||
| 201 | PowerManagementMode::None => 0, | ||
| 202 | _ => 2, | ||
| 203 | } | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | pub type NetDriver<'a> = ch::Device<'a, MTU>; | ||
| 208 | |||
| 209 | pub async fn new<'a, PWR, SPI>( | ||
| 210 | state: &'a mut State, | ||
| 211 | pwr: PWR, | ||
| 212 | spi: SPI, | ||
| 213 | firmware: &[u8], | ||
| 214 | ) -> (NetDriver<'a>, Control<'a>, Runner<'a, PWR, SPI>) | ||
| 215 | where | ||
| 216 | PWR: OutputPin, | ||
| 217 | SPI: SpiBusCyw43, | ||
| 218 | { | ||
| 219 | let (ch_runner, device) = ch::new(&mut state.ch, [0; 6]); | ||
| 220 | let state_ch = ch_runner.state_runner(); | ||
| 221 | |||
| 222 | let mut runner = Runner::new(ch_runner, Bus::new(pwr, spi), &state.ioctl_state, &state.events); | ||
| 223 | |||
| 224 | runner.init(firmware).await; | ||
| 225 | |||
| 226 | ( | ||
| 227 | device, | ||
| 228 | Control::new(state_ch, &state.events, &state.ioctl_state), | ||
| 229 | runner, | ||
| 230 | ) | ||
| 231 | } | ||
| 232 | |||
| 233 | fn slice8_mut(x: &mut [u32]) -> &mut [u8] { | ||
| 234 | let len = x.len() * 4; | ||
| 235 | unsafe { slice::from_raw_parts_mut(x.as_mut_ptr() as _, len) } | ||
| 236 | } | ||
