diff options
| author | JuliDi <[email protected]> | 2023-06-21 11:52:53 +0200 |
|---|---|---|
| committer | JuliDi <[email protected]> | 2023-06-21 11:52:53 +0200 |
| commit | fdb3c3d6ffad31ffc61c66d0d8a6f4db70a1c32b (patch) | |
| tree | e15a31a2850e2d98284b33e0b4cc44e2b16cdbe5 | |
| parent | 56ab6d9f143ecc3041ac9726e621eedea729ca4d (diff) | |
| parent | 2e625138ff8384a96f1f8a4d7a9c557b50353836 (diff) | |
Merge remote-tracking branch 'upstream/main'
30 files changed, 688 insertions, 79 deletions
| @@ -133,16 +133,16 @@ cargo batch \ | |||
| 133 | --- build --release --manifest-path examples/stm32u5/Cargo.toml --target thumbv8m.main-none-eabihf --out-dir out/examples/stm32u5 \ | 133 | --- build --release --manifest-path examples/stm32u5/Cargo.toml --target thumbv8m.main-none-eabihf --out-dir out/examples/stm32u5 \ |
| 134 | --- build --release --manifest-path examples/stm32wb/Cargo.toml --target thumbv7em-none-eabihf --out-dir out/examples/stm32wb \ | 134 | --- build --release --manifest-path examples/stm32wb/Cargo.toml --target thumbv7em-none-eabihf --out-dir out/examples/stm32wb \ |
| 135 | --- build --release --manifest-path examples/stm32wl/Cargo.toml --target thumbv7em-none-eabihf --out-dir out/examples/stm32wl \ | 135 | --- build --release --manifest-path examples/stm32wl/Cargo.toml --target thumbv7em-none-eabihf --out-dir out/examples/stm32wl \ |
| 136 | --- build --release --manifest-path examples/boot/application/nrf/Cargo.toml --target thumbv7em-none-eabi --features embassy-nrf/nrf52840 --out-dir out/examples/boot/nrf --bin b \ | 136 | --- build --release --manifest-path examples/boot/application/nrf/Cargo.toml --target thumbv7em-none-eabi --features embassy-nrf/nrf52840,skip-include --out-dir out/examples/boot/nrf \ |
| 137 | --- build --release --manifest-path examples/boot/application/nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features embassy-nrf/nrf9160-ns --out-dir out/examples/boot/nrf --bin b \ | 137 | --- build --release --manifest-path examples/boot/application/nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features embassy-nrf/nrf9160-ns,skip-include --out-dir out/examples/boot/nrf \ |
| 138 | --- build --release --manifest-path examples/boot/application/rp/Cargo.toml --target thumbv6m-none-eabi --out-dir out/examples/boot/rp --bin b \ | 138 | --- build --release --manifest-path examples/boot/application/rp/Cargo.toml --target thumbv6m-none-eabi --features skip-include --out-dir out/examples/boot/rp \ |
| 139 | --- build --release --manifest-path examples/boot/application/stm32f3/Cargo.toml --target thumbv7em-none-eabi --out-dir out/examples/boot/stm32f3 --bin b \ | 139 | --- build --release --manifest-path examples/boot/application/stm32f3/Cargo.toml --target thumbv7em-none-eabi --features skip-include --out-dir out/examples/boot/stm32f3 \ |
| 140 | --- build --release --manifest-path examples/boot/application/stm32f7/Cargo.toml --target thumbv7em-none-eabi --out-dir out/examples/boot/stm32f7 --bin b \ | 140 | --- build --release --manifest-path examples/boot/application/stm32f7/Cargo.toml --target thumbv7em-none-eabi --features skip-include --out-dir out/examples/boot/stm32f7 \ |
| 141 | --- build --release --manifest-path examples/boot/application/stm32h7/Cargo.toml --target thumbv7em-none-eabi --out-dir out/examples/boot/stm32h7 --bin b \ | 141 | --- build --release --manifest-path examples/boot/application/stm32h7/Cargo.toml --target thumbv7em-none-eabi --features skip-include --out-dir out/examples/boot/stm32h7 \ |
| 142 | --- build --release --manifest-path examples/boot/application/stm32l0/Cargo.toml --target thumbv6m-none-eabi --out-dir out/examples/boot/stm32l0 --bin b \ | 142 | --- build --release --manifest-path examples/boot/application/stm32l0/Cargo.toml --target thumbv6m-none-eabi --features skip-include --out-dir out/examples/boot/stm32l0 \ |
| 143 | --- build --release --manifest-path examples/boot/application/stm32l1/Cargo.toml --target thumbv7m-none-eabi --out-dir out/examples/boot/stm32l1 --bin b \ | 143 | --- build --release --manifest-path examples/boot/application/stm32l1/Cargo.toml --target thumbv7m-none-eabi --features skip-include --out-dir out/examples/boot/stm32l1 \ |
| 144 | --- build --release --manifest-path examples/boot/application/stm32l4/Cargo.toml --target thumbv7em-none-eabi --out-dir out/examples/boot/stm32l4 --bin b \ | 144 | --- build --release --manifest-path examples/boot/application/stm32l4/Cargo.toml --target thumbv7em-none-eabi --features skip-include --out-dir out/examples/boot/stm32l4 \ |
| 145 | --- build --release --manifest-path examples/boot/application/stm32wl/Cargo.toml --target thumbv7em-none-eabihf --out-dir out/examples/boot/stm32wl --bin b \ | 145 | --- build --release --manifest-path examples/boot/application/stm32wl/Cargo.toml --target thumbv7em-none-eabihf --features skip-include --out-dir out/examples/boot/stm32wl \ |
| 146 | --- build --release --manifest-path examples/boot/bootloader/nrf/Cargo.toml --target thumbv7em-none-eabi --features embassy-nrf/nrf52840 \ | 146 | --- build --release --manifest-path examples/boot/bootloader/nrf/Cargo.toml --target thumbv7em-none-eabi --features embassy-nrf/nrf52840 \ |
| 147 | --- build --release --manifest-path examples/boot/bootloader/nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features embassy-nrf/nrf9160-ns \ | 147 | --- build --release --manifest-path examples/boot/bootloader/nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features embassy-nrf/nrf9160-ns \ |
| 148 | --- build --release --manifest-path examples/boot/bootloader/rp/Cargo.toml --target thumbv6m-none-eabi \ | 148 | --- build --release --manifest-path examples/boot/bootloader/rp/Cargo.toml --target thumbv6m-none-eabi \ |
diff --git a/embassy-boot/boot/src/firmware_updater/asynch.rs b/embassy-boot/boot/src/firmware_updater/asynch.rs index 0b3f88313..20731ee0a 100644 --- a/embassy-boot/boot/src/firmware_updater/asynch.rs +++ b/embassy-boot/boot/src/firmware_updater/asynch.rs | |||
| @@ -56,6 +56,16 @@ impl<DFU: NorFlash, STATE: NorFlash> FirmwareUpdater<DFU, STATE> { | |||
| 56 | } | 56 | } |
| 57 | } | 57 | } |
| 58 | 58 | ||
| 59 | // Make sure we are running a booted firmware to avoid reverting to a bad state. | ||
| 60 | async fn verify_booted(&mut self, aligned: &mut [u8]) -> Result<(), FirmwareUpdaterError> { | ||
| 61 | assert_eq!(aligned.len(), STATE::WRITE_SIZE); | ||
| 62 | if self.get_state(aligned).await? == State::Boot { | ||
| 63 | Ok(()) | ||
| 64 | } else { | ||
| 65 | Err(FirmwareUpdaterError::BadState) | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 59 | /// Obtain the current state. | 69 | /// Obtain the current state. |
| 60 | /// | 70 | /// |
| 61 | /// This is useful to check if the bootloader has just done a swap, in order | 71 | /// This is useful to check if the bootloader has just done a swap, in order |
| @@ -98,6 +108,8 @@ impl<DFU: NorFlash, STATE: NorFlash> FirmwareUpdater<DFU, STATE> { | |||
| 98 | assert_eq!(_aligned.len(), STATE::WRITE_SIZE); | 108 | assert_eq!(_aligned.len(), STATE::WRITE_SIZE); |
| 99 | assert!(_update_len <= self.dfu.capacity() as u32); | 109 | assert!(_update_len <= self.dfu.capacity() as u32); |
| 100 | 110 | ||
| 111 | self.verify_booted(_aligned).await?; | ||
| 112 | |||
| 101 | #[cfg(feature = "ed25519-dalek")] | 113 | #[cfg(feature = "ed25519-dalek")] |
| 102 | { | 114 | { |
| 103 | use ed25519_dalek::{PublicKey, Signature, SignatureError, Verifier}; | 115 | use ed25519_dalek::{PublicKey, Signature, SignatureError, Verifier}; |
| @@ -217,8 +229,16 @@ impl<DFU: NorFlash, STATE: NorFlash> FirmwareUpdater<DFU, STATE> { | |||
| 217 | /// # Safety | 229 | /// # Safety |
| 218 | /// | 230 | /// |
| 219 | /// Failing to meet alignment and size requirements may result in a panic. | 231 | /// Failing to meet alignment and size requirements may result in a panic. |
| 220 | pub async fn write_firmware(&mut self, offset: usize, data: &[u8]) -> Result<(), FirmwareUpdaterError> { | 232 | pub async fn write_firmware( |
| 233 | &mut self, | ||
| 234 | aligned: &mut [u8], | ||
| 235 | offset: usize, | ||
| 236 | data: &[u8], | ||
| 237 | ) -> Result<(), FirmwareUpdaterError> { | ||
| 221 | assert!(data.len() >= DFU::ERASE_SIZE); | 238 | assert!(data.len() >= DFU::ERASE_SIZE); |
| 239 | assert_eq!(aligned.len(), STATE::WRITE_SIZE); | ||
| 240 | |||
| 241 | self.verify_booted(aligned).await?; | ||
| 222 | 242 | ||
| 223 | self.dfu.erase(offset as u32, (offset + data.len()) as u32).await?; | 243 | self.dfu.erase(offset as u32, (offset + data.len()) as u32).await?; |
| 224 | 244 | ||
| @@ -232,7 +252,14 @@ impl<DFU: NorFlash, STATE: NorFlash> FirmwareUpdater<DFU, STATE> { | |||
| 232 | /// | 252 | /// |
| 233 | /// Using this instead of `write_firmware` allows for an optimized API in | 253 | /// Using this instead of `write_firmware` allows for an optimized API in |
| 234 | /// exchange for added complexity. | 254 | /// exchange for added complexity. |
| 235 | pub async fn prepare_update(&mut self) -> Result<&mut DFU, FirmwareUpdaterError> { | 255 | /// |
| 256 | /// # Safety | ||
| 257 | /// | ||
| 258 | /// The `aligned` buffer must have a size of STATE::WRITE_SIZE, and follow the alignment rules for the flash being written to. | ||
| 259 | pub async fn prepare_update(&mut self, aligned: &mut [u8]) -> Result<&mut DFU, FirmwareUpdaterError> { | ||
| 260 | assert_eq!(aligned.len(), STATE::WRITE_SIZE); | ||
| 261 | self.verify_booted(aligned).await?; | ||
| 262 | |||
| 236 | self.dfu.erase(0, self.dfu.capacity() as u32).await?; | 263 | self.dfu.erase(0, self.dfu.capacity() as u32).await?; |
| 237 | 264 | ||
| 238 | Ok(&mut self.dfu) | 265 | Ok(&mut self.dfu) |
| @@ -255,13 +282,14 @@ mod tests { | |||
| 255 | let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 4096, 8>::default()); | 282 | let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 4096, 8>::default()); |
| 256 | let state = Partition::new(&flash, 0, 4096); | 283 | let state = Partition::new(&flash, 0, 4096); |
| 257 | let dfu = Partition::new(&flash, 65536, 65536); | 284 | let dfu = Partition::new(&flash, 65536, 65536); |
| 285 | let mut aligned = [0; 8]; | ||
| 258 | 286 | ||
| 259 | let update = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66]; | 287 | let update = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66]; |
| 260 | let mut to_write = [0; 4096]; | 288 | let mut to_write = [0; 4096]; |
| 261 | to_write[..7].copy_from_slice(update.as_slice()); | 289 | to_write[..7].copy_from_slice(update.as_slice()); |
| 262 | 290 | ||
| 263 | let mut updater = FirmwareUpdater::new(FirmwareUpdaterConfig { dfu, state }); | 291 | let mut updater = FirmwareUpdater::new(FirmwareUpdaterConfig { dfu, state }); |
| 264 | block_on(updater.write_firmware(0, to_write.as_slice())).unwrap(); | 292 | block_on(updater.write_firmware(&mut aligned, 0, to_write.as_slice())).unwrap(); |
| 265 | let mut chunk_buf = [0; 2]; | 293 | let mut chunk_buf = [0; 2]; |
| 266 | let mut hash = [0; 20]; | 294 | let mut hash = [0; 20]; |
| 267 | block_on(updater.hash::<Sha1>(update.len() as u32, &mut chunk_buf, &mut hash)).unwrap(); | 295 | block_on(updater.hash::<Sha1>(update.len() as u32, &mut chunk_buf, &mut hash)).unwrap(); |
diff --git a/embassy-boot/boot/src/firmware_updater/blocking.rs b/embassy-boot/boot/src/firmware_updater/blocking.rs index 551150c4f..f03f53e4d 100644 --- a/embassy-boot/boot/src/firmware_updater/blocking.rs +++ b/embassy-boot/boot/src/firmware_updater/blocking.rs | |||
| @@ -58,6 +58,16 @@ impl<DFU: NorFlash, STATE: NorFlash> BlockingFirmwareUpdater<DFU, STATE> { | |||
| 58 | } | 58 | } |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | // Make sure we are running a booted firmware to avoid reverting to a bad state. | ||
| 62 | fn verify_booted(&mut self, aligned: &mut [u8]) -> Result<(), FirmwareUpdaterError> { | ||
| 63 | assert_eq!(aligned.len(), STATE::WRITE_SIZE); | ||
| 64 | if self.get_state(aligned)? == State::Boot { | ||
| 65 | Ok(()) | ||
| 66 | } else { | ||
| 67 | Err(FirmwareUpdaterError::BadState) | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 61 | /// Obtain the current state. | 71 | /// Obtain the current state. |
| 62 | /// | 72 | /// |
| 63 | /// This is useful to check if the bootloader has just done a swap, in order | 73 | /// This is useful to check if the bootloader has just done a swap, in order |
| @@ -100,6 +110,8 @@ impl<DFU: NorFlash, STATE: NorFlash> BlockingFirmwareUpdater<DFU, STATE> { | |||
| 100 | assert_eq!(_aligned.len(), STATE::WRITE_SIZE); | 110 | assert_eq!(_aligned.len(), STATE::WRITE_SIZE); |
| 101 | assert!(_update_len <= self.dfu.capacity() as u32); | 111 | assert!(_update_len <= self.dfu.capacity() as u32); |
| 102 | 112 | ||
| 113 | self.verify_booted(_aligned)?; | ||
| 114 | |||
| 103 | #[cfg(feature = "ed25519-dalek")] | 115 | #[cfg(feature = "ed25519-dalek")] |
| 104 | { | 116 | { |
| 105 | use ed25519_dalek::{PublicKey, Signature, SignatureError, Verifier}; | 117 | use ed25519_dalek::{PublicKey, Signature, SignatureError, Verifier}; |
| @@ -219,8 +231,15 @@ impl<DFU: NorFlash, STATE: NorFlash> BlockingFirmwareUpdater<DFU, STATE> { | |||
| 219 | /// # Safety | 231 | /// # Safety |
| 220 | /// | 232 | /// |
| 221 | /// Failing to meet alignment and size requirements may result in a panic. | 233 | /// Failing to meet alignment and size requirements may result in a panic. |
| 222 | pub fn write_firmware(&mut self, offset: usize, data: &[u8]) -> Result<(), FirmwareUpdaterError> { | 234 | pub fn write_firmware( |
| 235 | &mut self, | ||
| 236 | aligned: &mut [u8], | ||
| 237 | offset: usize, | ||
| 238 | data: &[u8], | ||
| 239 | ) -> Result<(), FirmwareUpdaterError> { | ||
| 223 | assert!(data.len() >= DFU::ERASE_SIZE); | 240 | assert!(data.len() >= DFU::ERASE_SIZE); |
| 241 | assert_eq!(aligned.len(), STATE::WRITE_SIZE); | ||
| 242 | self.verify_booted(aligned)?; | ||
| 224 | 243 | ||
| 225 | self.dfu.erase(offset as u32, (offset + data.len()) as u32)?; | 244 | self.dfu.erase(offset as u32, (offset + data.len()) as u32)?; |
| 226 | 245 | ||
| @@ -234,7 +253,13 @@ impl<DFU: NorFlash, STATE: NorFlash> BlockingFirmwareUpdater<DFU, STATE> { | |||
| 234 | /// | 253 | /// |
| 235 | /// Using this instead of `write_firmware` allows for an optimized API in | 254 | /// Using this instead of `write_firmware` allows for an optimized API in |
| 236 | /// exchange for added complexity. | 255 | /// exchange for added complexity. |
| 237 | pub fn prepare_update(&mut self) -> Result<&mut DFU, FirmwareUpdaterError> { | 256 | /// |
| 257 | /// # Safety | ||
| 258 | /// | ||
| 259 | /// The `aligned` buffer must have a size of STATE::WRITE_SIZE, and follow the alignment rules for the flash being written to. | ||
| 260 | pub fn prepare_update(&mut self, aligned: &mut [u8]) -> Result<&mut DFU, FirmwareUpdaterError> { | ||
| 261 | assert_eq!(aligned.len(), STATE::WRITE_SIZE); | ||
| 262 | self.verify_booted(aligned)?; | ||
| 238 | self.dfu.erase(0, self.dfu.capacity() as u32)?; | 263 | self.dfu.erase(0, self.dfu.capacity() as u32)?; |
| 239 | 264 | ||
| 240 | Ok(&mut self.dfu) | 265 | Ok(&mut self.dfu) |
| @@ -264,7 +289,8 @@ mod tests { | |||
| 264 | to_write[..7].copy_from_slice(update.as_slice()); | 289 | to_write[..7].copy_from_slice(update.as_slice()); |
| 265 | 290 | ||
| 266 | let mut updater = BlockingFirmwareUpdater::new(FirmwareUpdaterConfig { dfu, state }); | 291 | let mut updater = BlockingFirmwareUpdater::new(FirmwareUpdaterConfig { dfu, state }); |
| 267 | updater.write_firmware(0, to_write.as_slice()).unwrap(); | 292 | let mut aligned = [0; 8]; |
| 293 | updater.write_firmware(&mut aligned, 0, to_write.as_slice()).unwrap(); | ||
| 268 | let mut chunk_buf = [0; 2]; | 294 | let mut chunk_buf = [0; 2]; |
| 269 | let mut hash = [0; 20]; | 295 | let mut hash = [0; 20]; |
| 270 | updater | 296 | updater |
diff --git a/embassy-boot/boot/src/firmware_updater/mod.rs b/embassy-boot/boot/src/firmware_updater/mod.rs index a37984a3a..55ce8f363 100644 --- a/embassy-boot/boot/src/firmware_updater/mod.rs +++ b/embassy-boot/boot/src/firmware_updater/mod.rs | |||
| @@ -26,6 +26,8 @@ pub enum FirmwareUpdaterError { | |||
| 26 | Flash(NorFlashErrorKind), | 26 | Flash(NorFlashErrorKind), |
| 27 | /// Signature errors. | 27 | /// Signature errors. |
| 28 | Signature(signature::Error), | 28 | Signature(signature::Error), |
| 29 | /// Bad state. | ||
| 30 | BadState, | ||
| 29 | } | 31 | } |
| 30 | 32 | ||
| 31 | #[cfg(feature = "defmt")] | 33 | #[cfg(feature = "defmt")] |
| @@ -34,6 +36,7 @@ impl defmt::Format for FirmwareUpdaterError { | |||
| 34 | match self { | 36 | match self { |
| 35 | FirmwareUpdaterError::Flash(_) => defmt::write!(fmt, "FirmwareUpdaterError::Flash(_)"), | 37 | FirmwareUpdaterError::Flash(_) => defmt::write!(fmt, "FirmwareUpdaterError::Flash(_)"), |
| 36 | FirmwareUpdaterError::Signature(_) => defmt::write!(fmt, "FirmwareUpdaterError::Signature(_)"), | 38 | FirmwareUpdaterError::Signature(_) => defmt::write!(fmt, "FirmwareUpdaterError::Signature(_)"), |
| 39 | FirmwareUpdaterError::BadState => defmt::write!(fmt, "FirmwareUpdaterError::BadState"), | ||
| 37 | } | 40 | } |
| 38 | } | 41 | } |
| 39 | } | 42 | } |
diff --git a/embassy-boot/boot/src/lib.rs b/embassy-boot/boot/src/lib.rs index 45a87bd0e..016362b86 100644 --- a/embassy-boot/boot/src/lib.rs +++ b/embassy-boot/boot/src/lib.rs | |||
| @@ -51,6 +51,8 @@ impl<const N: usize> AsMut<[u8]> for AlignedBuffer<N> { | |||
| 51 | 51 | ||
| 52 | #[cfg(test)] | 52 | #[cfg(test)] |
| 53 | mod tests { | 53 | mod tests { |
| 54 | #![allow(unused_imports)] | ||
| 55 | |||
| 54 | use embedded_storage::nor_flash::{NorFlash, ReadNorFlash}; | 56 | use embedded_storage::nor_flash::{NorFlash, ReadNorFlash}; |
| 55 | #[cfg(feature = "nightly")] | 57 | #[cfg(feature = "nightly")] |
| 56 | use embedded_storage_async::nor_flash::NorFlash as AsyncNorFlash; | 58 | use embedded_storage_async::nor_flash::NorFlash as AsyncNorFlash; |
| @@ -120,9 +122,13 @@ mod tests { | |||
| 120 | dfu: flash.dfu(), | 122 | dfu: flash.dfu(), |
| 121 | state: flash.state(), | 123 | state: flash.state(), |
| 122 | }); | 124 | }); |
| 123 | block_on(updater.write_firmware(0, &UPDATE)).unwrap(); | 125 | block_on(updater.write_firmware(&mut aligned, 0, &UPDATE)).unwrap(); |
| 124 | block_on(updater.mark_updated(&mut aligned)).unwrap(); | 126 | block_on(updater.mark_updated(&mut aligned)).unwrap(); |
| 125 | 127 | ||
| 128 | // Writing after marking updated is not allowed until marked as booted. | ||
| 129 | let res: Result<(), FirmwareUpdaterError> = block_on(updater.write_firmware(&mut aligned, 0, &UPDATE)); | ||
| 130 | assert!(matches!(res, Err::<(), _>(FirmwareUpdaterError::BadState))); | ||
| 131 | |||
| 126 | let flash = flash.into_blocking(); | 132 | let flash = flash.into_blocking(); |
| 127 | let mut bootloader = BootLoader::new(BootLoaderConfig { | 133 | let mut bootloader = BootLoader::new(BootLoaderConfig { |
| 128 | active: flash.active(), | 134 | active: flash.active(), |
| @@ -188,7 +194,7 @@ mod tests { | |||
| 188 | dfu: flash.dfu(), | 194 | dfu: flash.dfu(), |
| 189 | state: flash.state(), | 195 | state: flash.state(), |
| 190 | }); | 196 | }); |
| 191 | block_on(updater.write_firmware(0, &UPDATE)).unwrap(); | 197 | block_on(updater.write_firmware(&mut aligned, 0, &UPDATE)).unwrap(); |
| 192 | block_on(updater.mark_updated(&mut aligned)).unwrap(); | 198 | block_on(updater.mark_updated(&mut aligned)).unwrap(); |
| 193 | 199 | ||
| 194 | let flash = flash.into_blocking(); | 200 | let flash = flash.into_blocking(); |
| @@ -230,7 +236,7 @@ mod tests { | |||
| 230 | dfu: flash.dfu(), | 236 | dfu: flash.dfu(), |
| 231 | state: flash.state(), | 237 | state: flash.state(), |
| 232 | }); | 238 | }); |
| 233 | block_on(updater.write_firmware(0, &UPDATE)).unwrap(); | 239 | block_on(updater.write_firmware(&mut aligned, 0, &UPDATE)).unwrap(); |
| 234 | block_on(updater.mark_updated(&mut aligned)).unwrap(); | 240 | block_on(updater.mark_updated(&mut aligned)).unwrap(); |
| 235 | 241 | ||
| 236 | let flash = flash.into_blocking(); | 242 | let flash = flash.into_blocking(); |
diff --git a/embassy-stm32/src/can/bxcan.rs b/embassy-stm32/src/can/bxcan.rs index 85f6e99ac..88eef528f 100644 --- a/embassy-stm32/src/can/bxcan.rs +++ b/embassy-stm32/src/can/bxcan.rs | |||
| @@ -1,22 +1,106 @@ | |||
| 1 | use core::future::poll_fn; | ||
| 2 | use core::marker::PhantomData; | ||
| 1 | use core::ops::{Deref, DerefMut}; | 3 | use core::ops::{Deref, DerefMut}; |
| 4 | use core::task::Poll; | ||
| 2 | 5 | ||
| 3 | pub use bxcan; | 6 | pub use bxcan; |
| 7 | use bxcan::{Data, ExtendedId, Frame, Id, StandardId}; | ||
| 4 | use embassy_hal_common::{into_ref, PeripheralRef}; | 8 | use embassy_hal_common::{into_ref, PeripheralRef}; |
| 9 | use futures::FutureExt; | ||
| 5 | 10 | ||
| 6 | use crate::gpio::sealed::AFType; | 11 | use crate::gpio::sealed::AFType; |
| 12 | use crate::interrupt::typelevel::Interrupt; | ||
| 13 | use crate::pac::can::vals::{Lec, RirIde}; | ||
| 7 | use crate::rcc::RccPeripheral; | 14 | use crate::rcc::RccPeripheral; |
| 8 | use crate::{peripherals, Peripheral}; | 15 | use crate::time::Hertz; |
| 16 | use crate::{interrupt, peripherals, Peripheral}; | ||
| 17 | |||
| 18 | /// Interrupt handler. | ||
| 19 | pub struct TxInterruptHandler<T: Instance> { | ||
| 20 | _phantom: PhantomData<T>, | ||
| 21 | } | ||
| 22 | |||
| 23 | impl<T: Instance> interrupt::typelevel::Handler<T::TXInterrupt> for TxInterruptHandler<T> { | ||
| 24 | unsafe fn on_interrupt() { | ||
| 25 | T::regs().tsr().write(|v| { | ||
| 26 | v.set_rqcp(0, true); | ||
| 27 | v.set_rqcp(1, true); | ||
| 28 | v.set_rqcp(2, true); | ||
| 29 | }); | ||
| 30 | |||
| 31 | T::state().tx_waker.wake(); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | pub struct Rx0InterruptHandler<T: Instance> { | ||
| 36 | _phantom: PhantomData<T>, | ||
| 37 | } | ||
| 38 | |||
| 39 | impl<T: Instance> interrupt::typelevel::Handler<T::RX0Interrupt> for Rx0InterruptHandler<T> { | ||
| 40 | unsafe fn on_interrupt() { | ||
| 41 | // info!("rx0 irq"); | ||
| 42 | Can::<T>::receive_fifo(RxFifo::Fifo0); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | pub struct Rx1InterruptHandler<T: Instance> { | ||
| 47 | _phantom: PhantomData<T>, | ||
| 48 | } | ||
| 49 | |||
| 50 | impl<T: Instance> interrupt::typelevel::Handler<T::RX1Interrupt> for Rx1InterruptHandler<T> { | ||
| 51 | unsafe fn on_interrupt() { | ||
| 52 | // info!("rx1 irq"); | ||
| 53 | Can::<T>::receive_fifo(RxFifo::Fifo1); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | pub struct SceInterruptHandler<T: Instance> { | ||
| 58 | _phantom: PhantomData<T>, | ||
| 59 | } | ||
| 60 | |||
| 61 | impl<T: Instance> interrupt::typelevel::Handler<T::SCEInterrupt> for SceInterruptHandler<T> { | ||
| 62 | unsafe fn on_interrupt() { | ||
| 63 | // info!("sce irq"); | ||
| 64 | let msr = T::regs().msr(); | ||
| 65 | let msr_val = msr.read(); | ||
| 66 | |||
| 67 | if msr_val.erri() { | ||
| 68 | msr.modify(|v| v.set_erri(true)); | ||
| 69 | T::state().err_waker.wake(); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 9 | 73 | ||
| 10 | pub struct Can<'d, T: Instance> { | 74 | pub struct Can<'d, T: Instance> { |
| 11 | can: bxcan::Can<BxcanInstance<'d, T>>, | 75 | can: bxcan::Can<BxcanInstance<'d, T>>, |
| 12 | } | 76 | } |
| 13 | 77 | ||
| 78 | #[derive(Debug)] | ||
| 79 | pub enum BusError { | ||
| 80 | Stuff, | ||
| 81 | Form, | ||
| 82 | Acknowledge, | ||
| 83 | BitRecessive, | ||
| 84 | BitDominant, | ||
| 85 | Crc, | ||
| 86 | Software, | ||
| 87 | BusOff, | ||
| 88 | BusPassive, | ||
| 89 | BusWarning, | ||
| 90 | } | ||
| 91 | |||
| 14 | impl<'d, T: Instance> Can<'d, T> { | 92 | impl<'d, T: Instance> Can<'d, T> { |
| 15 | /// Creates a new Bxcan instance, blocking for 11 recessive bits to sync with the CAN bus. | 93 | /// Creates a new Bxcan instance, keeping the peripheral in sleep mode. |
| 94 | /// You must call [Can::enable_non_blocking] to use the peripheral. | ||
| 16 | pub fn new( | 95 | pub fn new( |
| 17 | peri: impl Peripheral<P = T> + 'd, | 96 | peri: impl Peripheral<P = T> + 'd, |
| 18 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, | 97 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, |
| 19 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, | 98 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, |
| 99 | _irqs: impl interrupt::typelevel::Binding<T::TXInterrupt, TxInterruptHandler<T>> | ||
| 100 | + interrupt::typelevel::Binding<T::RX0Interrupt, Rx0InterruptHandler<T>> | ||
| 101 | + interrupt::typelevel::Binding<T::RX1Interrupt, Rx1InterruptHandler<T>> | ||
| 102 | + interrupt::typelevel::Binding<T::SCEInterrupt, SceInterruptHandler<T>> | ||
| 103 | + 'd, | ||
| 20 | ) -> Self { | 104 | ) -> Self { |
| 21 | into_ref!(peri, rx, tx); | 105 | into_ref!(peri, rx, tx); |
| 22 | 106 | ||
| @@ -26,30 +110,242 @@ impl<'d, T: Instance> Can<'d, T> { | |||
| 26 | T::enable(); | 110 | T::enable(); |
| 27 | T::reset(); | 111 | T::reset(); |
| 28 | 112 | ||
| 29 | Self { | 113 | { |
| 30 | can: bxcan::Can::builder(BxcanInstance(peri)).enable(), | 114 | use crate::pac::can::vals::{Errie, Fmpie, Tmeie}; |
| 115 | |||
| 116 | T::regs().ier().write(|w| { | ||
| 117 | // TODO: fix metapac | ||
| 118 | |||
| 119 | w.set_errie(Errie(1)); | ||
| 120 | w.set_fmpie(0, Fmpie(1)); | ||
| 121 | w.set_fmpie(1, Fmpie(1)); | ||
| 122 | w.set_tmeie(Tmeie(1)); | ||
| 123 | }); | ||
| 124 | |||
| 125 | T::regs().mcr().write(|w| { | ||
| 126 | // Enable timestamps on rx messages | ||
| 127 | |||
| 128 | w.set_ttcm(true); | ||
| 129 | }); | ||
| 31 | } | 130 | } |
| 32 | } | ||
| 33 | 131 | ||
| 34 | /// Creates a new Bxcan instance, keeping the peripheral in sleep mode. | 132 | unsafe { |
| 35 | /// You must call [Can::enable_non_blocking] to use the peripheral. | 133 | T::TXInterrupt::unpend(); |
| 36 | pub fn new_disabled( | 134 | T::TXInterrupt::enable(); |
| 37 | peri: impl Peripheral<P = T> + 'd, | 135 | |
| 38 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, | 136 | T::RX0Interrupt::unpend(); |
| 39 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, | 137 | T::RX0Interrupt::enable(); |
| 40 | ) -> Self { | 138 | |
| 41 | into_ref!(peri, rx, tx); | 139 | T::RX1Interrupt::unpend(); |
| 140 | T::RX1Interrupt::enable(); | ||
| 141 | |||
| 142 | T::SCEInterrupt::unpend(); | ||
| 143 | T::SCEInterrupt::enable(); | ||
| 144 | } | ||
| 42 | 145 | ||
| 43 | rx.set_as_af(rx.af_num(), AFType::Input); | 146 | rx.set_as_af(rx.af_num(), AFType::Input); |
| 44 | tx.set_as_af(tx.af_num(), AFType::OutputPushPull); | 147 | tx.set_as_af(tx.af_num(), AFType::OutputPushPull); |
| 45 | 148 | ||
| 46 | T::enable(); | 149 | let can = bxcan::Can::builder(BxcanInstance(peri)).leave_disabled(); |
| 47 | T::reset(); | 150 | Self { can } |
| 151 | } | ||
| 152 | |||
| 153 | pub fn set_bitrate(&mut self, bitrate: u32) { | ||
| 154 | let bit_timing = Self::calc_bxcan_timings(T::frequency(), bitrate).unwrap(); | ||
| 155 | self.can.modify_config().set_bit_timing(bit_timing).leave_disabled(); | ||
| 156 | } | ||
| 48 | 157 | ||
| 49 | Self { | 158 | /// Queues the message to be sent but exerts backpressure |
| 50 | can: bxcan::Can::builder(BxcanInstance(peri)).leave_disabled(), | 159 | pub async fn write(&mut self, frame: &Frame) -> bxcan::TransmitStatus { |
| 160 | poll_fn(|cx| { | ||
| 161 | T::state().tx_waker.register(cx.waker()); | ||
| 162 | if let Ok(status) = self.can.transmit(frame) { | ||
| 163 | return Poll::Ready(status); | ||
| 164 | } | ||
| 165 | |||
| 166 | Poll::Pending | ||
| 167 | }) | ||
| 168 | .await | ||
| 169 | } | ||
| 170 | |||
| 171 | pub async fn flush(&self, mb: bxcan::Mailbox) { | ||
| 172 | poll_fn(|cx| { | ||
| 173 | T::state().tx_waker.register(cx.waker()); | ||
| 174 | if T::regs().tsr().read().tme(mb.index()) { | ||
| 175 | return Poll::Ready(()); | ||
| 176 | } | ||
| 177 | |||
| 178 | Poll::Pending | ||
| 179 | }) | ||
| 180 | .await; | ||
| 181 | } | ||
| 182 | |||
| 183 | /// Returns a tuple of the time the message was received and the message frame | ||
| 184 | pub async fn read(&mut self) -> Result<(u16, bxcan::Frame), BusError> { | ||
| 185 | poll_fn(|cx| { | ||
| 186 | T::state().err_waker.register(cx.waker()); | ||
| 187 | if let Poll::Ready((time, frame)) = T::state().rx_queue.recv().poll_unpin(cx) { | ||
| 188 | return Poll::Ready(Ok((time, frame))); | ||
| 189 | } else if let Some(err) = self.curr_error() { | ||
| 190 | return Poll::Ready(Err(err)); | ||
| 191 | } | ||
| 192 | |||
| 193 | Poll::Pending | ||
| 194 | }) | ||
| 195 | .await | ||
| 196 | } | ||
| 197 | |||
| 198 | fn curr_error(&self) -> Option<BusError> { | ||
| 199 | let err = { T::regs().esr().read() }; | ||
| 200 | if err.boff() { | ||
| 201 | return Some(BusError::BusOff); | ||
| 202 | } else if err.epvf() { | ||
| 203 | return Some(BusError::BusPassive); | ||
| 204 | } else if err.ewgf() { | ||
| 205 | return Some(BusError::BusWarning); | ||
| 206 | } else if let Some(err) = err.lec().into_bus_err() { | ||
| 207 | return Some(err); | ||
| 51 | } | 208 | } |
| 209 | None | ||
| 52 | } | 210 | } |
| 211 | |||
| 212 | unsafe fn receive_fifo(fifo: RxFifo) { | ||
| 213 | let state = T::state(); | ||
| 214 | let regs = T::regs(); | ||
| 215 | let fifo_idx = match fifo { | ||
| 216 | RxFifo::Fifo0 => 0usize, | ||
| 217 | RxFifo::Fifo1 => 1usize, | ||
| 218 | }; | ||
| 219 | let rfr = regs.rfr(fifo_idx); | ||
| 220 | let fifo = regs.rx(fifo_idx); | ||
| 221 | |||
| 222 | loop { | ||
| 223 | // If there are no pending messages, there is nothing to do | ||
| 224 | if rfr.read().fmp() == 0 { | ||
| 225 | return; | ||
| 226 | } | ||
| 227 | |||
| 228 | let rir = fifo.rir().read(); | ||
| 229 | let id = if rir.ide() == RirIde::STANDARD { | ||
| 230 | Id::from(StandardId::new_unchecked(rir.stid())) | ||
| 231 | } else { | ||
| 232 | let stid = (rir.stid() & 0x7FF) as u32; | ||
| 233 | let exid = rir.exid() & 0x3FFFF; | ||
| 234 | let id = (stid << 18) | (exid as u32); | ||
| 235 | Id::from(ExtendedId::new_unchecked(id)) | ||
| 236 | }; | ||
| 237 | let data_len = fifo.rdtr().read().dlc() as usize; | ||
| 238 | let mut data: [u8; 8] = [0; 8]; | ||
| 239 | data[0..4].copy_from_slice(&fifo.rdlr().read().0.to_ne_bytes()); | ||
| 240 | data[4..8].copy_from_slice(&fifo.rdhr().read().0.to_ne_bytes()); | ||
| 241 | |||
| 242 | let time = fifo.rdtr().read().time(); | ||
| 243 | let frame = Frame::new_data(id, Data::new(&data[0..data_len]).unwrap()); | ||
| 244 | |||
| 245 | rfr.modify(|v| v.set_rfom(true)); | ||
| 246 | |||
| 247 | /* | ||
| 248 | NOTE: consensus was reached that if rx_queue is full, packets should be dropped | ||
| 249 | */ | ||
| 250 | let _ = state.rx_queue.try_send((time, frame)); | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | pub const fn calc_bxcan_timings(periph_clock: Hertz, can_bitrate: u32) -> Option<u32> { | ||
| 255 | const BS1_MAX: u8 = 16; | ||
| 256 | const BS2_MAX: u8 = 8; | ||
| 257 | const MAX_SAMPLE_POINT_PERMILL: u16 = 900; | ||
| 258 | |||
| 259 | let periph_clock = periph_clock.0; | ||
| 260 | |||
| 261 | if can_bitrate < 1000 { | ||
| 262 | return None; | ||
| 263 | } | ||
| 264 | |||
| 265 | // Ref. "Automatic Baudrate Detection in CANopen Networks", U. Koppe, MicroControl GmbH & Co. KG | ||
| 266 | // CAN in Automation, 2003 | ||
| 267 | // | ||
| 268 | // According to the source, optimal quanta per bit are: | ||
| 269 | // Bitrate Optimal Maximum | ||
| 270 | // 1000 kbps 8 10 | ||
| 271 | // 500 kbps 16 17 | ||
| 272 | // 250 kbps 16 17 | ||
| 273 | // 125 kbps 16 17 | ||
| 274 | let max_quanta_per_bit: u8 = if can_bitrate >= 1_000_000 { 10 } else { 17 }; | ||
| 275 | |||
| 276 | // Computing (prescaler * BS): | ||
| 277 | // BITRATE = 1 / (PRESCALER * (1 / PCLK) * (1 + BS1 + BS2)) -- See the Reference Manual | ||
| 278 | // BITRATE = PCLK / (PRESCALER * (1 + BS1 + BS2)) -- Simplified | ||
| 279 | // let: | ||
| 280 | // BS = 1 + BS1 + BS2 -- Number of time quanta per bit | ||
| 281 | // PRESCALER_BS = PRESCALER * BS | ||
| 282 | // ==> | ||
| 283 | // PRESCALER_BS = PCLK / BITRATE | ||
| 284 | let prescaler_bs = periph_clock / can_bitrate; | ||
| 285 | |||
| 286 | // Searching for such prescaler value so that the number of quanta per bit is highest. | ||
| 287 | let mut bs1_bs2_sum = max_quanta_per_bit - 1; | ||
| 288 | while (prescaler_bs % (1 + bs1_bs2_sum) as u32) != 0 { | ||
| 289 | if bs1_bs2_sum <= 2 { | ||
| 290 | return None; // No solution | ||
| 291 | } | ||
| 292 | bs1_bs2_sum -= 1; | ||
| 293 | } | ||
| 294 | |||
| 295 | let prescaler = prescaler_bs / (1 + bs1_bs2_sum) as u32; | ||
| 296 | if (prescaler < 1) || (prescaler > 1024) { | ||
| 297 | return None; // No solution | ||
| 298 | } | ||
| 299 | |||
| 300 | // Now we have a constraint: (BS1 + BS2) == bs1_bs2_sum. | ||
| 301 | // We need to find such values so that the sample point is as close as possible to the optimal value, | ||
| 302 | // which is 87.5%, which is 7/8. | ||
| 303 | // | ||
| 304 | // Solve[(1 + bs1)/(1 + bs1 + bs2) == 7/8, bs2] (* Where 7/8 is 0.875, the recommended sample point location *) | ||
| 305 | // {{bs2 -> (1 + bs1)/7}} | ||
| 306 | // | ||
| 307 | // Hence: | ||
| 308 | // bs2 = (1 + bs1) / 7 | ||
| 309 | // bs1 = (7 * bs1_bs2_sum - 1) / 8 | ||
| 310 | // | ||
| 311 | // Sample point location can be computed as follows: | ||
| 312 | // Sample point location = (1 + bs1) / (1 + bs1 + bs2) | ||
| 313 | // | ||
| 314 | // Since the optimal solution is so close to the maximum, we prepare two solutions, and then pick the best one: | ||
| 315 | // - With rounding to nearest | ||
| 316 | // - With rounding to zero | ||
| 317 | let mut bs1 = ((7 * bs1_bs2_sum - 1) + 4) / 8; // Trying rounding to nearest first | ||
| 318 | let mut bs2 = bs1_bs2_sum - bs1; | ||
| 319 | core::assert!(bs1_bs2_sum > bs1); | ||
| 320 | |||
| 321 | let sample_point_permill = 1000 * ((1 + bs1) / (1 + bs1 + bs2)) as u16; | ||
| 322 | if sample_point_permill > MAX_SAMPLE_POINT_PERMILL { | ||
| 323 | // Nope, too far; now rounding to zero | ||
| 324 | bs1 = (7 * bs1_bs2_sum - 1) / 8; | ||
| 325 | bs2 = bs1_bs2_sum - bs1; | ||
| 326 | } | ||
| 327 | |||
| 328 | // Check is BS1 and BS2 are in range | ||
| 329 | if (bs1 < 1) || (bs1 > BS1_MAX) || (bs2 < 1) || (bs2 > BS2_MAX) { | ||
| 330 | return None; | ||
| 331 | } | ||
| 332 | |||
| 333 | // Check if final bitrate matches the requested | ||
| 334 | if can_bitrate != (periph_clock / (prescaler * (1 + bs1 + bs2) as u32)) { | ||
| 335 | return None; | ||
| 336 | } | ||
| 337 | |||
| 338 | // One is recommended by DS-015, CANOpen, and DeviceNet | ||
| 339 | let sjw = 1; | ||
| 340 | |||
| 341 | // Pack into BTR register values | ||
| 342 | Some((sjw - 1) << 24 | (bs1 as u32 - 1) << 16 | (bs2 as u32 - 1) << 20 | (prescaler as u32 - 1)) | ||
| 343 | } | ||
| 344 | } | ||
| 345 | |||
| 346 | enum RxFifo { | ||
| 347 | Fifo0, | ||
| 348 | Fifo1, | ||
| 53 | } | 349 | } |
| 54 | 350 | ||
| 55 | impl<'d, T: Instance> Drop for Can<'d, T> { | 351 | impl<'d, T: Instance> Drop for Can<'d, T> { |
| @@ -76,14 +372,52 @@ impl<'d, T: Instance> DerefMut for Can<'d, T> { | |||
| 76 | } | 372 | } |
| 77 | 373 | ||
| 78 | pub(crate) mod sealed { | 374 | pub(crate) mod sealed { |
| 375 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | ||
| 376 | use embassy_sync::channel::Channel; | ||
| 377 | use embassy_sync::waitqueue::AtomicWaker; | ||
| 378 | |||
| 379 | pub struct State { | ||
| 380 | pub tx_waker: AtomicWaker, | ||
| 381 | pub err_waker: AtomicWaker, | ||
| 382 | pub rx_queue: Channel<CriticalSectionRawMutex, (u16, bxcan::Frame), 32>, | ||
| 383 | } | ||
| 384 | |||
| 385 | impl State { | ||
| 386 | pub const fn new() -> Self { | ||
| 387 | Self { | ||
| 388 | tx_waker: AtomicWaker::new(), | ||
| 389 | err_waker: AtomicWaker::new(), | ||
| 390 | rx_queue: Channel::new(), | ||
| 391 | } | ||
| 392 | } | ||
| 393 | } | ||
| 394 | |||
| 79 | pub trait Instance { | 395 | pub trait Instance { |
| 80 | const REGISTERS: *mut bxcan::RegisterBlock; | 396 | const REGISTERS: *mut bxcan::RegisterBlock; |
| 81 | 397 | ||
| 82 | fn regs() -> &'static crate::pac::can::Can; | 398 | fn regs() -> &'static crate::pac::can::Can; |
| 399 | fn state() -> &'static State; | ||
| 83 | } | 400 | } |
| 84 | } | 401 | } |
| 85 | 402 | ||
| 86 | pub trait Instance: sealed::Instance + RccPeripheral {} | 403 | pub trait TXInstance { |
| 404 | type TXInterrupt: crate::interrupt::typelevel::Interrupt; | ||
| 405 | } | ||
| 406 | |||
| 407 | pub trait RX0Instance { | ||
| 408 | type RX0Interrupt: crate::interrupt::typelevel::Interrupt; | ||
| 409 | } | ||
| 410 | |||
| 411 | pub trait RX1Instance { | ||
| 412 | type RX1Interrupt: crate::interrupt::typelevel::Interrupt; | ||
| 413 | } | ||
| 414 | |||
| 415 | pub trait SCEInstance { | ||
| 416 | type SCEInterrupt: crate::interrupt::typelevel::Interrupt; | ||
| 417 | } | ||
| 418 | |||
| 419 | pub trait InterruptableInstance: TXInstance + RX0Instance + RX1Instance + SCEInstance {} | ||
| 420 | pub trait Instance: sealed::Instance + RccPeripheral + InterruptableInstance + 'static {} | ||
| 87 | 421 | ||
| 88 | pub struct BxcanInstance<'a, T>(PeripheralRef<'a, T>); | 422 | pub struct BxcanInstance<'a, T>(PeripheralRef<'a, T>); |
| 89 | 423 | ||
| @@ -99,10 +433,39 @@ foreach_peripheral!( | |||
| 99 | fn regs() -> &'static crate::pac::can::Can { | 433 | fn regs() -> &'static crate::pac::can::Can { |
| 100 | &crate::pac::$inst | 434 | &crate::pac::$inst |
| 101 | } | 435 | } |
| 436 | |||
| 437 | fn state() -> &'static sealed::State { | ||
| 438 | static STATE: sealed::State = sealed::State::new(); | ||
| 439 | &STATE | ||
| 440 | } | ||
| 102 | } | 441 | } |
| 103 | 442 | ||
| 104 | impl Instance for peripherals::$inst {} | 443 | impl Instance for peripherals::$inst {} |
| 105 | 444 | ||
| 445 | foreach_interrupt!( | ||
| 446 | ($inst,can,CAN,TX,$irq:ident) => { | ||
| 447 | impl TXInstance for peripherals::$inst { | ||
| 448 | type TXInterrupt = crate::interrupt::typelevel::$irq; | ||
| 449 | } | ||
| 450 | }; | ||
| 451 | ($inst,can,CAN,RX0,$irq:ident) => { | ||
| 452 | impl RX0Instance for peripherals::$inst { | ||
| 453 | type RX0Interrupt = crate::interrupt::typelevel::$irq; | ||
| 454 | } | ||
| 455 | }; | ||
| 456 | ($inst,can,CAN,RX1,$irq:ident) => { | ||
| 457 | impl RX1Instance for peripherals::$inst { | ||
| 458 | type RX1Interrupt = crate::interrupt::typelevel::$irq; | ||
| 459 | } | ||
| 460 | }; | ||
| 461 | ($inst,can,CAN,SCE,$irq:ident) => { | ||
| 462 | impl SCEInstance for peripherals::$inst { | ||
| 463 | type SCEInterrupt = crate::interrupt::typelevel::$irq; | ||
| 464 | } | ||
| 465 | }; | ||
| 466 | ); | ||
| 467 | |||
| 468 | impl InterruptableInstance for peripherals::$inst {} | ||
| 106 | }; | 469 | }; |
| 107 | ); | 470 | ); |
| 108 | 471 | ||
| @@ -143,3 +506,36 @@ foreach_peripheral!( | |||
| 143 | 506 | ||
| 144 | pin_trait!(RxPin, Instance); | 507 | pin_trait!(RxPin, Instance); |
| 145 | pin_trait!(TxPin, Instance); | 508 | pin_trait!(TxPin, Instance); |
| 509 | |||
| 510 | trait Index { | ||
| 511 | fn index(&self) -> usize; | ||
| 512 | } | ||
| 513 | |||
| 514 | impl Index for bxcan::Mailbox { | ||
| 515 | fn index(&self) -> usize { | ||
| 516 | match self { | ||
| 517 | bxcan::Mailbox::Mailbox0 => 0, | ||
| 518 | bxcan::Mailbox::Mailbox1 => 1, | ||
| 519 | bxcan::Mailbox::Mailbox2 => 2, | ||
| 520 | } | ||
| 521 | } | ||
| 522 | } | ||
| 523 | |||
| 524 | trait IntoBusError { | ||
| 525 | fn into_bus_err(self) -> Option<BusError>; | ||
| 526 | } | ||
| 527 | |||
| 528 | impl IntoBusError for Lec { | ||
| 529 | fn into_bus_err(self) -> Option<BusError> { | ||
| 530 | match self { | ||
| 531 | Lec::STUFF => Some(BusError::Stuff), | ||
| 532 | Lec::FORM => Some(BusError::Form), | ||
| 533 | Lec::ACK => Some(BusError::Acknowledge), | ||
| 534 | Lec::BITRECESSIVE => Some(BusError::BitRecessive), | ||
| 535 | Lec::BITDOMINANT => Some(BusError::BitDominant), | ||
| 536 | Lec::CRC => Some(BusError::Crc), | ||
| 537 | Lec::CUSTOM => Some(BusError::Software), | ||
| 538 | _ => None, | ||
| 539 | } | ||
| 540 | } | ||
| 541 | } | ||
diff --git a/embassy-stm32/src/dma/bdma.rs b/embassy-stm32/src/dma/bdma.rs index 0ad20579a..162ca9adb 100644 --- a/embassy-stm32/src/dma/bdma.rs +++ b/embassy-stm32/src/dma/bdma.rs | |||
| @@ -338,6 +338,7 @@ impl<'a, C: Channel> Transfer<'a, C> { | |||
| 338 | 338 | ||
| 339 | pub fn blocking_wait(mut self) { | 339 | pub fn blocking_wait(mut self) { |
| 340 | while self.is_running() {} | 340 | while self.is_running() {} |
| 341 | self.request_stop(); | ||
| 341 | 342 | ||
| 342 | // "Subsequent reads and writes cannot be moved ahead of preceding reads." | 343 | // "Subsequent reads and writes cannot be moved ahead of preceding reads." |
| 343 | fence(Ordering::SeqCst); | 344 | fence(Ordering::SeqCst); |
diff --git a/embassy-stm32/src/dma/gpdma.rs b/embassy-stm32/src/dma/gpdma.rs index c600df92d..b7bcf7795 100644 --- a/embassy-stm32/src/dma/gpdma.rs +++ b/embassy-stm32/src/dma/gpdma.rs | |||
| @@ -252,6 +252,7 @@ impl<'a, C: Channel> Transfer<'a, C> { | |||
| 252 | super::dmamux::configure_dmamux(&mut *this.channel, request); | 252 | super::dmamux::configure_dmamux(&mut *this.channel, request); |
| 253 | 253 | ||
| 254 | ch.cr().write(|w| w.set_reset(true)); | 254 | ch.cr().write(|w| w.set_reset(true)); |
| 255 | ch.fcr().write(|w| w.0 = 0xFFFF_FFFF); // clear all irqs | ||
| 255 | ch.llr().write(|_| {}); // no linked list | 256 | ch.llr().write(|_| {}); // no linked list |
| 256 | ch.tr1().write(|w| { | 257 | ch.tr1().write(|w| { |
| 257 | w.set_sdw(data_size.into()); | 258 | w.set_sdw(data_size.into()); |
diff --git a/examples/boot/application/nrf/Cargo.toml b/examples/boot/application/nrf/Cargo.toml index 5939a43b1..b98f73f39 100644 --- a/examples/boot/application/nrf/Cargo.toml +++ b/examples/boot/application/nrf/Cargo.toml | |||
| @@ -24,3 +24,4 @@ cortex-m-rt = "0.7.0" | |||
| 24 | [features] | 24 | [features] |
| 25 | ed25519-dalek = ["embassy-boot/ed25519-dalek"] | 25 | ed25519-dalek = ["embassy-boot/ed25519-dalek"] |
| 26 | ed25519-salty = ["embassy-boot/ed25519-salty"] | 26 | ed25519-salty = ["embassy-boot/ed25519-salty"] |
| 27 | skip-include = [] | ||
diff --git a/examples/boot/application/nrf/src/bin/a.rs b/examples/boot/application/nrf/src/bin/a.rs index 06c237781..021d77f3b 100644 --- a/examples/boot/application/nrf/src/bin/a.rs +++ b/examples/boot/application/nrf/src/bin/a.rs | |||
| @@ -12,6 +12,9 @@ use embassy_nrf::wdt::{self, Watchdog}; | |||
| 12 | use embassy_sync::mutex::Mutex; | 12 | use embassy_sync::mutex::Mutex; |
| 13 | use panic_reset as _; | 13 | use panic_reset as _; |
| 14 | 14 | ||
| 15 | #[cfg(feature = "skip-include")] | ||
| 16 | static APP_B: &[u8] = &[0, 1, 2, 3]; | ||
| 17 | #[cfg(not(feature = "skip-include"))] | ||
| 15 | static APP_B: &[u8] = include_bytes!("../../b.bin"); | 18 | static APP_B: &[u8] = include_bytes!("../../b.bin"); |
| 16 | 19 | ||
| 17 | #[embassy_executor::main] | 20 | #[embassy_executor::main] |
| @@ -55,13 +58,13 @@ async fn main(_spawner: Spawner) { | |||
| 55 | button.wait_for_any_edge().await; | 58 | button.wait_for_any_edge().await; |
| 56 | if button.is_low() { | 59 | if button.is_low() { |
| 57 | let mut offset = 0; | 60 | let mut offset = 0; |
| 61 | let mut magic = [0; 4]; | ||
| 58 | for chunk in APP_B.chunks(4096) { | 62 | for chunk in APP_B.chunks(4096) { |
| 59 | let mut buf: [u8; 4096] = [0; 4096]; | 63 | let mut buf: [u8; 4096] = [0; 4096]; |
| 60 | buf[..chunk.len()].copy_from_slice(chunk); | 64 | buf[..chunk.len()].copy_from_slice(chunk); |
| 61 | updater.write_firmware(offset, &buf).await.unwrap(); | 65 | updater.write_firmware(&mut magic, offset, &buf).await.unwrap(); |
| 62 | offset += chunk.len(); | 66 | offset += chunk.len(); |
| 63 | } | 67 | } |
| 64 | let mut magic = [0; 4]; | ||
| 65 | updater.mark_updated(&mut magic).await.unwrap(); | 68 | updater.mark_updated(&mut magic).await.unwrap(); |
| 66 | led.set_high(); | 69 | led.set_high(); |
| 67 | cortex_m::peripheral::SCB::sys_reset(); | 70 | cortex_m::peripheral::SCB::sys_reset(); |
diff --git a/examples/boot/application/rp/Cargo.toml b/examples/boot/application/rp/Cargo.toml index 4a2c5dd8f..007b6839c 100644 --- a/examples/boot/application/rp/Cargo.toml +++ b/examples/boot/application/rp/Cargo.toml | |||
| @@ -29,6 +29,7 @@ debug = [ | |||
| 29 | "embassy-boot-rp/defmt", | 29 | "embassy-boot-rp/defmt", |
| 30 | "panic-probe" | 30 | "panic-probe" |
| 31 | ] | 31 | ] |
| 32 | skip-include = [] | ||
| 32 | 33 | ||
| 33 | [profile.release] | 34 | [profile.release] |
| 34 | debug = true | 35 | debug = true |
diff --git a/examples/boot/application/rp/src/bin/a.rs b/examples/boot/application/rp/src/bin/a.rs index 69850069b..c8497494c 100644 --- a/examples/boot/application/rp/src/bin/a.rs +++ b/examples/boot/application/rp/src/bin/a.rs | |||
| @@ -18,7 +18,11 @@ use panic_probe as _; | |||
| 18 | #[cfg(feature = "panic-reset")] | 18 | #[cfg(feature = "panic-reset")] |
| 19 | use panic_reset as _; | 19 | use panic_reset as _; |
| 20 | 20 | ||
| 21 | #[cfg(feature = "skip-include")] | ||
| 22 | static APP_B: &[u8] = &[0, 1, 2, 3]; | ||
| 23 | #[cfg(not(feature = "skip-include"))] | ||
| 21 | static APP_B: &[u8] = include_bytes!("../../b.bin"); | 24 | static APP_B: &[u8] = include_bytes!("../../b.bin"); |
| 25 | |||
| 22 | const FLASH_SIZE: usize = 2 * 1024 * 1024; | 26 | const FLASH_SIZE: usize = 2 * 1024 * 1024; |
| 23 | 27 | ||
| 24 | #[embassy_executor::main] | 28 | #[embassy_executor::main] |
| @@ -43,7 +47,7 @@ async fn main(_s: Spawner) { | |||
| 43 | let mut buf: AlignedBuffer<4096> = AlignedBuffer([0; 4096]); | 47 | let mut buf: AlignedBuffer<4096> = AlignedBuffer([0; 4096]); |
| 44 | defmt::info!("preparing update"); | 48 | defmt::info!("preparing update"); |
| 45 | let writer = updater | 49 | let writer = updater |
| 46 | .prepare_update() | 50 | .prepare_update(&mut buf.0[..1]) |
| 47 | .map_err(|e| defmt::warn!("E: {:?}", defmt::Debug2Format(&e))) | 51 | .map_err(|e| defmt::warn!("E: {:?}", defmt::Debug2Format(&e))) |
| 48 | .unwrap(); | 52 | .unwrap(); |
| 49 | defmt::info!("writer created, starting write"); | 53 | defmt::info!("writer created, starting write"); |
diff --git a/examples/boot/application/stm32f3/Cargo.toml b/examples/boot/application/stm32f3/Cargo.toml index 24abd90d4..5b3faf8f8 100644 --- a/examples/boot/application/stm32f3/Cargo.toml +++ b/examples/boot/application/stm32f3/Cargo.toml | |||
| @@ -26,3 +26,4 @@ defmt = [ | |||
| 26 | "embassy-stm32/defmt", | 26 | "embassy-stm32/defmt", |
| 27 | "embassy-boot-stm32/defmt", | 27 | "embassy-boot-stm32/defmt", |
| 28 | ] | 28 | ] |
| 29 | skip-include = [] | ||
diff --git a/examples/boot/application/stm32f3/src/bin/a.rs b/examples/boot/application/stm32f3/src/bin/a.rs index c94676f09..c0a11d699 100644 --- a/examples/boot/application/stm32f3/src/bin/a.rs +++ b/examples/boot/application/stm32f3/src/bin/a.rs | |||
| @@ -13,6 +13,9 @@ use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | |||
| 13 | use embassy_sync::mutex::Mutex; | 13 | use embassy_sync::mutex::Mutex; |
| 14 | use panic_reset as _; | 14 | use panic_reset as _; |
| 15 | 15 | ||
| 16 | #[cfg(feature = "skip-include")] | ||
| 17 | static APP_B: &[u8] = &[0, 1, 2, 3]; | ||
| 18 | #[cfg(not(feature = "skip-include"))] | ||
| 16 | static APP_B: &[u8] = include_bytes!("../../b.bin"); | 19 | static APP_B: &[u8] = include_bytes!("../../b.bin"); |
| 17 | 20 | ||
| 18 | #[embassy_executor::main] | 21 | #[embassy_executor::main] |
| @@ -31,13 +34,13 @@ async fn main(_spawner: Spawner) { | |||
| 31 | let mut updater = FirmwareUpdater::new(config); | 34 | let mut updater = FirmwareUpdater::new(config); |
| 32 | button.wait_for_falling_edge().await; | 35 | button.wait_for_falling_edge().await; |
| 33 | let mut offset = 0; | 36 | let mut offset = 0; |
| 37 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | ||
| 34 | for chunk in APP_B.chunks(2048) { | 38 | for chunk in APP_B.chunks(2048) { |
| 35 | let mut buf: [u8; 2048] = [0; 2048]; | 39 | let mut buf: [u8; 2048] = [0; 2048]; |
| 36 | buf[..chunk.len()].copy_from_slice(chunk); | 40 | buf[..chunk.len()].copy_from_slice(chunk); |
| 37 | updater.write_firmware(offset, &buf).await.unwrap(); | 41 | updater.write_firmware(magic.as_mut(), offset, &buf).await.unwrap(); |
| 38 | offset += chunk.len(); | 42 | offset += chunk.len(); |
| 39 | } | 43 | } |
| 40 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | ||
| 41 | updater.mark_updated(magic.as_mut()).await.unwrap(); | 44 | updater.mark_updated(magic.as_mut()).await.unwrap(); |
| 42 | led.set_low(); | 45 | led.set_low(); |
| 43 | cortex_m::peripheral::SCB::sys_reset(); | 46 | cortex_m::peripheral::SCB::sys_reset(); |
diff --git a/examples/boot/application/stm32f7/Cargo.toml b/examples/boot/application/stm32f7/Cargo.toml index 529a01aad..b6a6f9cd8 100644 --- a/examples/boot/application/stm32f7/Cargo.toml +++ b/examples/boot/application/stm32f7/Cargo.toml | |||
| @@ -16,6 +16,7 @@ defmt = { version = "0.3", optional = true } | |||
| 16 | defmt-rtt = { version = "0.4", optional = true } | 16 | defmt-rtt = { version = "0.4", optional = true } |
| 17 | panic-reset = { version = "0.1.1" } | 17 | panic-reset = { version = "0.1.1" } |
| 18 | embedded-hal = { version = "0.2.6" } | 18 | embedded-hal = { version = "0.2.6" } |
| 19 | embedded-storage = "0.3.0" | ||
| 19 | 20 | ||
| 20 | cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] } | 21 | cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] } |
| 21 | cortex-m-rt = "0.7.0" | 22 | cortex-m-rt = "0.7.0" |
| @@ -26,3 +27,4 @@ defmt = [ | |||
| 26 | "embassy-stm32/defmt", | 27 | "embassy-stm32/defmt", |
| 27 | "embassy-boot-stm32/defmt", | 28 | "embassy-boot-stm32/defmt", |
| 28 | ] | 29 | ] |
| 30 | skip-include = [] | ||
diff --git a/examples/boot/application/stm32f7/src/bin/a.rs b/examples/boot/application/stm32f7/src/bin/a.rs index fc2702c91..dea682a96 100644 --- a/examples/boot/application/stm32f7/src/bin/a.rs +++ b/examples/boot/application/stm32f7/src/bin/a.rs | |||
| @@ -2,6 +2,8 @@ | |||
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 3 | #![feature(type_alias_impl_trait)] |
| 4 | 4 | ||
| 5 | use core::cell::RefCell; | ||
| 6 | |||
| 5 | #[cfg(feature = "defmt-rtt")] | 7 | #[cfg(feature = "defmt-rtt")] |
| 6 | use defmt_rtt::*; | 8 | use defmt_rtt::*; |
| 7 | use embassy_boot_stm32::{AlignedBuffer, BlockingFirmwareUpdater, FirmwareUpdaterConfig}; | 9 | use embassy_boot_stm32::{AlignedBuffer, BlockingFirmwareUpdater, FirmwareUpdaterConfig}; |
| @@ -9,8 +11,13 @@ use embassy_executor::Spawner; | |||
| 9 | use embassy_stm32::exti::ExtiInput; | 11 | use embassy_stm32::exti::ExtiInput; |
| 10 | use embassy_stm32::flash::{Flash, WRITE_SIZE}; | 12 | use embassy_stm32::flash::{Flash, WRITE_SIZE}; |
| 11 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | 13 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; |
| 14 | use embassy_sync::blocking_mutex::Mutex; | ||
| 15 | use embedded_storage::nor_flash::NorFlash; | ||
| 12 | use panic_reset as _; | 16 | use panic_reset as _; |
| 13 | 17 | ||
| 18 | #[cfg(feature = "skip-include")] | ||
| 19 | static APP_B: &[u8] = &[0, 1, 2, 3]; | ||
| 20 | #[cfg(not(feature = "skip-include"))] | ||
| 14 | static APP_B: &[u8] = include_bytes!("../../b.bin"); | 21 | static APP_B: &[u8] = include_bytes!("../../b.bin"); |
| 15 | 22 | ||
| 16 | #[embassy_executor::main] | 23 | #[embassy_executor::main] |
| @@ -27,16 +34,16 @@ async fn main(_spawner: Spawner) { | |||
| 27 | 34 | ||
| 28 | let config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash); | 35 | let config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash); |
| 29 | let mut updater = BlockingFirmwareUpdater::new(config); | 36 | let mut updater = BlockingFirmwareUpdater::new(config); |
| 30 | let mut writer = updater.prepare_update().unwrap(); | 37 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); |
| 38 | let writer = updater.prepare_update(magic.as_mut()).unwrap(); | ||
| 31 | button.wait_for_rising_edge().await; | 39 | button.wait_for_rising_edge().await; |
| 32 | let mut offset = 0; | 40 | let mut offset = 0; |
| 33 | let mut buf = AlignedBuffer([0; 4096]); | 41 | let mut buf = AlignedBuffer([0; 4096]); |
| 34 | for chunk in APP_B.chunks(4096) { | 42 | for chunk in APP_B.chunks(4096) { |
| 35 | buf.as_mut()[..chunk.len()].copy_from_slice(chunk); | 43 | buf.as_mut()[..chunk.len()].copy_from_slice(chunk); |
| 36 | writer.write(offset, buf.as_ref()).unwrap(); | 44 | writer.write(offset, buf.as_ref()).unwrap(); |
| 37 | offset += chunk.len(); | 45 | offset += chunk.len() as u32; |
| 38 | } | 46 | } |
| 39 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | ||
| 40 | updater.mark_updated(magic.as_mut()).unwrap(); | 47 | updater.mark_updated(magic.as_mut()).unwrap(); |
| 41 | led.set_low(); | 48 | led.set_low(); |
| 42 | cortex_m::peripheral::SCB::sys_reset(); | 49 | cortex_m::peripheral::SCB::sys_reset(); |
diff --git a/examples/boot/application/stm32h7/Cargo.toml b/examples/boot/application/stm32h7/Cargo.toml index d7539a53f..0a7e19b1d 100644 --- a/examples/boot/application/stm32h7/Cargo.toml +++ b/examples/boot/application/stm32h7/Cargo.toml | |||
| @@ -16,6 +16,7 @@ defmt = { version = "0.3", optional = true } | |||
| 16 | defmt-rtt = { version = "0.4", optional = true } | 16 | defmt-rtt = { version = "0.4", optional = true } |
| 17 | panic-reset = { version = "0.1.1" } | 17 | panic-reset = { version = "0.1.1" } |
| 18 | embedded-hal = { version = "0.2.6" } | 18 | embedded-hal = { version = "0.2.6" } |
| 19 | embedded-storage = "0.3.0" | ||
| 19 | 20 | ||
| 20 | cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] } | 21 | cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] } |
| 21 | cortex-m-rt = "0.7.0" | 22 | cortex-m-rt = "0.7.0" |
| @@ -26,3 +27,4 @@ defmt = [ | |||
| 26 | "embassy-stm32/defmt", | 27 | "embassy-stm32/defmt", |
| 27 | "embassy-boot-stm32/defmt", | 28 | "embassy-boot-stm32/defmt", |
| 28 | ] | 29 | ] |
| 30 | skip-include = [] | ||
diff --git a/examples/boot/application/stm32h7/src/bin/a.rs b/examples/boot/application/stm32h7/src/bin/a.rs index 1a54464d0..719176692 100644 --- a/examples/boot/application/stm32h7/src/bin/a.rs +++ b/examples/boot/application/stm32h7/src/bin/a.rs | |||
| @@ -2,6 +2,8 @@ | |||
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 3 | #![feature(type_alias_impl_trait)] |
| 4 | 4 | ||
| 5 | use core::cell::RefCell; | ||
| 6 | |||
| 5 | #[cfg(feature = "defmt-rtt")] | 7 | #[cfg(feature = "defmt-rtt")] |
| 6 | use defmt_rtt::*; | 8 | use defmt_rtt::*; |
| 7 | use embassy_boot_stm32::{AlignedBuffer, BlockingFirmwareUpdater, FirmwareUpdaterConfig}; | 9 | use embassy_boot_stm32::{AlignedBuffer, BlockingFirmwareUpdater, FirmwareUpdaterConfig}; |
| @@ -9,8 +11,13 @@ use embassy_executor::Spawner; | |||
| 9 | use embassy_stm32::exti::ExtiInput; | 11 | use embassy_stm32::exti::ExtiInput; |
| 10 | use embassy_stm32::flash::{Flash, WRITE_SIZE}; | 12 | use embassy_stm32::flash::{Flash, WRITE_SIZE}; |
| 11 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | 13 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; |
| 14 | use embassy_sync::blocking_mutex::Mutex; | ||
| 15 | use embedded_storage::nor_flash::NorFlash; | ||
| 12 | use panic_reset as _; | 16 | use panic_reset as _; |
| 13 | 17 | ||
| 18 | #[cfg(feature = "skip-include")] | ||
| 19 | static APP_B: &[u8] = &[0, 1, 2, 3]; | ||
| 20 | #[cfg(not(feature = "skip-include"))] | ||
| 14 | static APP_B: &[u8] = include_bytes!("../../b.bin"); | 21 | static APP_B: &[u8] = include_bytes!("../../b.bin"); |
| 15 | 22 | ||
| 16 | #[embassy_executor::main] | 23 | #[embassy_executor::main] |
| @@ -26,17 +33,17 @@ async fn main(_spawner: Spawner) { | |||
| 26 | led.set_high(); | 33 | led.set_high(); |
| 27 | 34 | ||
| 28 | let config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash); | 35 | let config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash); |
| 36 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | ||
| 29 | let mut updater = BlockingFirmwareUpdater::new(config); | 37 | let mut updater = BlockingFirmwareUpdater::new(config); |
| 30 | let mut writer = updater.prepare_update().unwrap(); | 38 | let writer = updater.prepare_update(magic.as_mut()).unwrap(); |
| 31 | button.wait_for_rising_edge().await; | 39 | button.wait_for_rising_edge().await; |
| 32 | let mut offset = 0; | 40 | let mut offset = 0; |
| 33 | let mut buf = AlignedBuffer([0; 4096]); | 41 | let mut buf = AlignedBuffer([0; 4096]); |
| 34 | for chunk in APP_B.chunks(4096) { | 42 | for chunk in APP_B.chunks(4096) { |
| 35 | buf.as_mut()[..chunk.len()].copy_from_slice(chunk); | 43 | buf.as_mut()[..chunk.len()].copy_from_slice(chunk); |
| 36 | writer.write(offset, buf.as_ref()).unwrap(); | 44 | writer.write(offset, buf.as_ref()).unwrap(); |
| 37 | offset += chunk.len(); | 45 | offset += chunk.len() as u32; |
| 38 | } | 46 | } |
| 39 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | ||
| 40 | updater.mark_updated(magic.as_mut()).unwrap(); | 47 | updater.mark_updated(magic.as_mut()).unwrap(); |
| 41 | led.set_low(); | 48 | led.set_low(); |
| 42 | cortex_m::peripheral::SCB::sys_reset(); | 49 | cortex_m::peripheral::SCB::sys_reset(); |
diff --git a/examples/boot/application/stm32l0/Cargo.toml b/examples/boot/application/stm32l0/Cargo.toml index e90da259b..998df4dc0 100644 --- a/examples/boot/application/stm32l0/Cargo.toml +++ b/examples/boot/application/stm32l0/Cargo.toml | |||
| @@ -26,3 +26,4 @@ defmt = [ | |||
| 26 | "embassy-stm32/defmt", | 26 | "embassy-stm32/defmt", |
| 27 | "embassy-boot-stm32/defmt", | 27 | "embassy-boot-stm32/defmt", |
| 28 | ] | 28 | ] |
| 29 | skip-include = [] | ||
diff --git a/examples/boot/application/stm32l0/src/bin/a.rs b/examples/boot/application/stm32l0/src/bin/a.rs index 4033ac590..ce80056e6 100644 --- a/examples/boot/application/stm32l0/src/bin/a.rs +++ b/examples/boot/application/stm32l0/src/bin/a.rs | |||
| @@ -4,22 +4,26 @@ | |||
| 4 | 4 | ||
| 5 | #[cfg(feature = "defmt-rtt")] | 5 | #[cfg(feature = "defmt-rtt")] |
| 6 | use defmt_rtt::*; | 6 | use defmt_rtt::*; |
| 7 | use embassy_boot_stm32::{AlignedBuffer, FirmwareUpdater}; | 7 | use embassy_boot_stm32::{AlignedBuffer, FirmwareUpdater, FirmwareUpdaterConfig}; |
| 8 | use embassy_embedded_hal::adapter::BlockingAsync; | 8 | use embassy_embedded_hal::adapter::BlockingAsync; |
| 9 | use embassy_executor::Spawner; | 9 | use embassy_executor::Spawner; |
| 10 | use embassy_stm32::exti::ExtiInput; | 10 | use embassy_stm32::exti::ExtiInput; |
| 11 | use embassy_stm32::flash::{Flash, WRITE_SIZE}; | 11 | use embassy_stm32::flash::{Flash, WRITE_SIZE}; |
| 12 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | 12 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; |
| 13 | use embassy_sync::mutex::Mutex; | ||
| 13 | use embassy_time::{Duration, Timer}; | 14 | use embassy_time::{Duration, Timer}; |
| 14 | use panic_reset as _; | 15 | use panic_reset as _; |
| 15 | 16 | ||
| 17 | #[cfg(feature = "skip-include")] | ||
| 18 | static APP_B: &[u8] = &[0, 1, 2, 3]; | ||
| 19 | #[cfg(not(feature = "skip-include"))] | ||
| 16 | static APP_B: &[u8] = include_bytes!("../../b.bin"); | 20 | static APP_B: &[u8] = include_bytes!("../../b.bin"); |
| 17 | 21 | ||
| 18 | #[embassy_executor::main] | 22 | #[embassy_executor::main] |
| 19 | async fn main(_spawner: Spawner) { | 23 | async fn main(_spawner: Spawner) { |
| 20 | let p = embassy_stm32::init(Default::default()); | 24 | let p = embassy_stm32::init(Default::default()); |
| 21 | let flash = Flash::new_blocking(p.FLASH); | 25 | let flash = Flash::new_blocking(p.FLASH); |
| 22 | let mut flash = BlockingAsync::new(flash); | 26 | let flash = Mutex::new(BlockingAsync::new(flash)); |
| 23 | 27 | ||
| 24 | let button = Input::new(p.PB2, Pull::Up); | 28 | let button = Input::new(p.PB2, Pull::Up); |
| 25 | let mut button = ExtiInput::new(button, p.EXTI2); | 29 | let mut button = ExtiInput::new(button, p.EXTI2); |
| @@ -28,18 +32,19 @@ async fn main(_spawner: Spawner) { | |||
| 28 | 32 | ||
| 29 | led.set_high(); | 33 | led.set_high(); |
| 30 | 34 | ||
| 31 | let mut updater = FirmwareUpdater::default(); | 35 | let config = FirmwareUpdaterConfig::from_linkerfile(&flash); |
| 36 | let mut updater = FirmwareUpdater::new(config); | ||
| 32 | button.wait_for_falling_edge().await; | 37 | button.wait_for_falling_edge().await; |
| 33 | let mut offset = 0; | 38 | let mut offset = 0; |
| 39 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | ||
| 34 | for chunk in APP_B.chunks(128) { | 40 | for chunk in APP_B.chunks(128) { |
| 35 | let mut buf: [u8; 128] = [0; 128]; | 41 | let mut buf: [u8; 128] = [0; 128]; |
| 36 | buf[..chunk.len()].copy_from_slice(chunk); | 42 | buf[..chunk.len()].copy_from_slice(chunk); |
| 37 | updater.write_firmware(offset, &buf, &mut flash, 128).await.unwrap(); | 43 | updater.write_firmware(magic.as_mut(), offset, &buf).await.unwrap(); |
| 38 | offset += chunk.len(); | 44 | offset += chunk.len(); |
| 39 | } | 45 | } |
| 40 | 46 | ||
| 41 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | 47 | updater.mark_updated(magic.as_mut()).await.unwrap(); |
| 42 | updater.mark_updated(&mut flash, magic.as_mut()).await.unwrap(); | ||
| 43 | led.set_low(); | 48 | led.set_low(); |
| 44 | Timer::after(Duration::from_secs(1)).await; | 49 | Timer::after(Duration::from_secs(1)).await; |
| 45 | cortex_m::peripheral::SCB::sys_reset(); | 50 | cortex_m::peripheral::SCB::sys_reset(); |
diff --git a/examples/boot/application/stm32l1/Cargo.toml b/examples/boot/application/stm32l1/Cargo.toml index 8ac0fac85..10b58c172 100644 --- a/examples/boot/application/stm32l1/Cargo.toml +++ b/examples/boot/application/stm32l1/Cargo.toml | |||
| @@ -26,3 +26,4 @@ defmt = [ | |||
| 26 | "embassy-stm32/defmt", | 26 | "embassy-stm32/defmt", |
| 27 | "embassy-boot-stm32/defmt", | 27 | "embassy-boot-stm32/defmt", |
| 28 | ] | 28 | ] |
| 29 | skip-include = [] | ||
diff --git a/examples/boot/application/stm32l1/src/bin/a.rs b/examples/boot/application/stm32l1/src/bin/a.rs index 00ddda636..1e9bf3cb9 100644 --- a/examples/boot/application/stm32l1/src/bin/a.rs +++ b/examples/boot/application/stm32l1/src/bin/a.rs | |||
| @@ -10,9 +10,13 @@ use embassy_executor::Spawner; | |||
| 10 | use embassy_stm32::exti::ExtiInput; | 10 | use embassy_stm32::exti::ExtiInput; |
| 11 | use embassy_stm32::flash::{Flash, WRITE_SIZE}; | 11 | use embassy_stm32::flash::{Flash, WRITE_SIZE}; |
| 12 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | 12 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; |
| 13 | use embassy_sync::mutex::Mutex; | ||
| 13 | use embassy_time::{Duration, Timer}; | 14 | use embassy_time::{Duration, Timer}; |
| 14 | use panic_reset as _; | 15 | use panic_reset as _; |
| 15 | 16 | ||
| 17 | #[cfg(feature = "skip-include")] | ||
| 18 | static APP_B: &[u8] = &[0, 1, 2, 3]; | ||
| 19 | #[cfg(not(feature = "skip-include"))] | ||
| 16 | static APP_B: &[u8] = include_bytes!("../../b.bin"); | 20 | static APP_B: &[u8] = include_bytes!("../../b.bin"); |
| 17 | 21 | ||
| 18 | #[embassy_executor::main] | 22 | #[embassy_executor::main] |
| @@ -31,15 +35,15 @@ async fn main(_spawner: Spawner) { | |||
| 31 | let config = FirmwareUpdaterConfig::from_linkerfile(&flash); | 35 | let config = FirmwareUpdaterConfig::from_linkerfile(&flash); |
| 32 | let mut updater = FirmwareUpdater::new(config); | 36 | let mut updater = FirmwareUpdater::new(config); |
| 33 | button.wait_for_falling_edge().await; | 37 | button.wait_for_falling_edge().await; |
| 38 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | ||
| 34 | let mut offset = 0; | 39 | let mut offset = 0; |
| 35 | for chunk in APP_B.chunks(128) { | 40 | for chunk in APP_B.chunks(128) { |
| 36 | let mut buf: [u8; 128] = [0; 128]; | 41 | let mut buf: [u8; 128] = [0; 128]; |
| 37 | buf[..chunk.len()].copy_from_slice(chunk); | 42 | buf[..chunk.len()].copy_from_slice(chunk); |
| 38 | updater.write_firmware(offset, &buf).await.unwrap(); | 43 | updater.write_firmware(magic.as_mut(), offset, &buf).await.unwrap(); |
| 39 | offset += chunk.len(); | 44 | offset += chunk.len(); |
| 40 | } | 45 | } |
| 41 | 46 | ||
| 42 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | ||
| 43 | updater.mark_updated(magic.as_mut()).await.unwrap(); | 47 | updater.mark_updated(magic.as_mut()).await.unwrap(); |
| 44 | led.set_low(); | 48 | led.set_low(); |
| 45 | Timer::after(Duration::from_secs(1)).await; | 49 | Timer::after(Duration::from_secs(1)).await; |
diff --git a/examples/boot/application/stm32l4/Cargo.toml b/examples/boot/application/stm32l4/Cargo.toml index ec79acdeb..713a6527e 100644 --- a/examples/boot/application/stm32l4/Cargo.toml +++ b/examples/boot/application/stm32l4/Cargo.toml | |||
| @@ -26,3 +26,4 @@ defmt = [ | |||
| 26 | "embassy-stm32/defmt", | 26 | "embassy-stm32/defmt", |
| 27 | "embassy-boot-stm32/defmt", | 27 | "embassy-boot-stm32/defmt", |
| 28 | ] | 28 | ] |
| 29 | skip-include = [] | ||
diff --git a/examples/boot/application/stm32l4/src/bin/a.rs b/examples/boot/application/stm32l4/src/bin/a.rs index 54579e4ac..a514ab5be 100644 --- a/examples/boot/application/stm32l4/src/bin/a.rs +++ b/examples/boot/application/stm32l4/src/bin/a.rs | |||
| @@ -10,8 +10,12 @@ use embassy_executor::Spawner; | |||
| 10 | use embassy_stm32::exti::ExtiInput; | 10 | use embassy_stm32::exti::ExtiInput; |
| 11 | use embassy_stm32::flash::{Flash, WRITE_SIZE}; | 11 | use embassy_stm32::flash::{Flash, WRITE_SIZE}; |
| 12 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | 12 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; |
| 13 | use embassy_sync::mutex::Mutex; | ||
| 13 | use panic_reset as _; | 14 | use panic_reset as _; |
| 14 | 15 | ||
| 16 | #[cfg(feature = "skip-include")] | ||
| 17 | static APP_B: &[u8] = &[0, 1, 2, 3]; | ||
| 18 | #[cfg(not(feature = "skip-include"))] | ||
| 15 | static APP_B: &[u8] = include_bytes!("../../b.bin"); | 19 | static APP_B: &[u8] = include_bytes!("../../b.bin"); |
| 16 | 20 | ||
| 17 | #[embassy_executor::main] | 21 | #[embassy_executor::main] |
| @@ -29,15 +33,15 @@ async fn main(_spawner: Spawner) { | |||
| 29 | let config = FirmwareUpdaterConfig::from_linkerfile(&flash); | 33 | let config = FirmwareUpdaterConfig::from_linkerfile(&flash); |
| 30 | let mut updater = FirmwareUpdater::new(config); | 34 | let mut updater = FirmwareUpdater::new(config); |
| 31 | button.wait_for_falling_edge().await; | 35 | button.wait_for_falling_edge().await; |
| 36 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | ||
| 32 | let mut offset = 0; | 37 | let mut offset = 0; |
| 33 | for chunk in APP_B.chunks(2048) { | 38 | for chunk in APP_B.chunks(2048) { |
| 34 | let mut buf: [u8; 2048] = [0; 2048]; | 39 | let mut buf: [u8; 2048] = [0; 2048]; |
| 35 | buf[..chunk.len()].copy_from_slice(chunk); | 40 | buf[..chunk.len()].copy_from_slice(chunk); |
| 36 | updater.write_firmware(offset, &buf).await.unwrap(); | 41 | updater.write_firmware(magic.as_mut(), offset, &buf).await.unwrap(); |
| 37 | offset += chunk.len(); | 42 | offset += chunk.len(); |
| 38 | } | 43 | } |
| 39 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | 44 | updater.mark_updated(magic.as_mut()).await.unwrap(); |
| 40 | updater.mark_updated(&mut flash, magic.as_mut()).await.unwrap(); | ||
| 41 | led.set_low(); | 45 | led.set_low(); |
| 42 | cortex_m::peripheral::SCB::sys_reset(); | 46 | cortex_m::peripheral::SCB::sys_reset(); |
| 43 | } | 47 | } |
diff --git a/examples/boot/application/stm32wl/Cargo.toml b/examples/boot/application/stm32wl/Cargo.toml index dfaece6cf..4c8bbd73f 100644 --- a/examples/boot/application/stm32wl/Cargo.toml +++ b/examples/boot/application/stm32wl/Cargo.toml | |||
| @@ -26,3 +26,4 @@ defmt = [ | |||
| 26 | "embassy-stm32/defmt", | 26 | "embassy-stm32/defmt", |
| 27 | "embassy-boot-stm32/defmt", | 27 | "embassy-boot-stm32/defmt", |
| 28 | ] | 28 | ] |
| 29 | skip-include = [] | ||
diff --git a/examples/boot/application/stm32wl/src/bin/a.rs b/examples/boot/application/stm32wl/src/bin/a.rs index 0c6fa05f9..52a197a5c 100644 --- a/examples/boot/application/stm32wl/src/bin/a.rs +++ b/examples/boot/application/stm32wl/src/bin/a.rs | |||
| @@ -4,21 +4,25 @@ | |||
| 4 | 4 | ||
| 5 | #[cfg(feature = "defmt-rtt")] | 5 | #[cfg(feature = "defmt-rtt")] |
| 6 | use defmt_rtt::*; | 6 | use defmt_rtt::*; |
| 7 | use embassy_boot_stm32::{AlignedBuffer, FirmwareUpdater}; | 7 | use embassy_boot_stm32::{AlignedBuffer, FirmwareUpdater, FirmwareUpdaterConfig}; |
| 8 | use embassy_embedded_hal::adapter::BlockingAsync; | 8 | use embassy_embedded_hal::adapter::BlockingAsync; |
| 9 | use embassy_executor::Spawner; | 9 | use embassy_executor::Spawner; |
| 10 | use embassy_stm32::exti::ExtiInput; | 10 | use embassy_stm32::exti::ExtiInput; |
| 11 | use embassy_stm32::flash::{Flash, WRITE_SIZE}; | 11 | use embassy_stm32::flash::{Flash, WRITE_SIZE}; |
| 12 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | 12 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; |
| 13 | use embassy_sync::mutex::Mutex; | ||
| 13 | use panic_reset as _; | 14 | use panic_reset as _; |
| 14 | 15 | ||
| 16 | #[cfg(feature = "skip-include")] | ||
| 17 | static APP_B: &[u8] = &[0, 1, 2, 3]; | ||
| 18 | #[cfg(not(feature = "skip-include"))] | ||
| 15 | static APP_B: &[u8] = include_bytes!("../../b.bin"); | 19 | static APP_B: &[u8] = include_bytes!("../../b.bin"); |
| 16 | 20 | ||
| 17 | #[embassy_executor::main] | 21 | #[embassy_executor::main] |
| 18 | async fn main(_spawner: Spawner) { | 22 | async fn main(_spawner: Spawner) { |
| 19 | let p = embassy_stm32::init(Default::default()); | 23 | let p = embassy_stm32::init(Default::default()); |
| 20 | let flash = Flash::new_blocking(p.FLASH); | 24 | let flash = Flash::new_blocking(p.FLASH); |
| 21 | let mut flash = Mutex::new(BlockingAsync::new(flash)); | 25 | let flash = Mutex::new(BlockingAsync::new(flash)); |
| 22 | 26 | ||
| 23 | let button = Input::new(p.PA0, Pull::Up); | 27 | let button = Input::new(p.PA0, Pull::Up); |
| 24 | let mut button = ExtiInput::new(button, p.EXTI0); | 28 | let mut button = ExtiInput::new(button, p.EXTI0); |
| @@ -30,15 +34,15 @@ async fn main(_spawner: Spawner) { | |||
| 30 | let mut updater = FirmwareUpdater::new(config); | 34 | let mut updater = FirmwareUpdater::new(config); |
| 31 | button.wait_for_falling_edge().await; | 35 | button.wait_for_falling_edge().await; |
| 32 | //defmt::info!("Starting update"); | 36 | //defmt::info!("Starting update"); |
| 37 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | ||
| 33 | let mut offset = 0; | 38 | let mut offset = 0; |
| 34 | for chunk in APP_B.chunks(2048) { | 39 | for chunk in APP_B.chunks(2048) { |
| 35 | let mut buf: [u8; 2048] = [0; 2048]; | 40 | let mut buf: [u8; 2048] = [0; 2048]; |
| 36 | buf[..chunk.len()].copy_from_slice(chunk); | 41 | buf[..chunk.len()].copy_from_slice(chunk); |
| 37 | // defmt::info!("Writing chunk at 0x{:x}", offset); | 42 | // defmt::info!("Writing chunk at 0x{:x}", offset); |
| 38 | updater.write_firmware(offset, &buf).await.unwrap(); | 43 | updater.write_firmware(magic.as_mut(), offset, &buf).await.unwrap(); |
| 39 | offset += chunk.len(); | 44 | offset += chunk.len(); |
| 40 | } | 45 | } |
| 41 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | ||
| 42 | updater.mark_updated(magic.as_mut()).await.unwrap(); | 46 | updater.mark_updated(magic.as_mut()).await.unwrap(); |
| 43 | //defmt::info!("Marked as updated"); | 47 | //defmt::info!("Marked as updated"); |
| 44 | led.set_low(); | 48 | led.set_low(); |
diff --git a/examples/stm32f4/src/bin/can.rs b/examples/stm32f4/src/bin/can.rs index e8377b9a1..da8955053 100644 --- a/examples/stm32f4/src/bin/can.rs +++ b/examples/stm32f4/src/bin/can.rs | |||
| @@ -4,12 +4,21 @@ | |||
| 4 | 4 | ||
| 5 | use cortex_m_rt::entry; | 5 | use cortex_m_rt::entry; |
| 6 | use defmt::*; | 6 | use defmt::*; |
| 7 | use embassy_stm32::bind_interrupts; | ||
| 7 | use embassy_stm32::can::bxcan::filter::Mask32; | 8 | use embassy_stm32::can::bxcan::filter::Mask32; |
| 8 | use embassy_stm32::can::bxcan::{Fifo, Frame, StandardId}; | 9 | use embassy_stm32::can::bxcan::{Fifo, Frame, StandardId}; |
| 9 | use embassy_stm32::can::Can; | 10 | use embassy_stm32::can::{Can, Rx0InterruptHandler, Rx1InterruptHandler, SceInterruptHandler, TxInterruptHandler}; |
| 10 | use embassy_stm32::gpio::{Input, Pull}; | 11 | use embassy_stm32::gpio::{Input, Pull}; |
| 12 | use embassy_stm32::peripherals::CAN1; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | 13 | use {defmt_rtt as _, panic_probe as _}; |
| 12 | 14 | ||
| 15 | bind_interrupts!(struct Irqs { | ||
| 16 | CAN1_RX0 => Rx0InterruptHandler<CAN1>; | ||
| 17 | CAN1_RX1 => Rx1InterruptHandler<CAN1>; | ||
| 18 | CAN1_SCE => SceInterruptHandler<CAN1>; | ||
| 19 | CAN1_TX => TxInterruptHandler<CAN1>; | ||
| 20 | }); | ||
| 21 | |||
| 13 | #[entry] | 22 | #[entry] |
| 14 | fn main() -> ! { | 23 | fn main() -> ! { |
| 15 | info!("Hello World!"); | 24 | info!("Hello World!"); |
| @@ -23,7 +32,7 @@ fn main() -> ! { | |||
| 23 | let rx_pin = Input::new(&mut p.PA11, Pull::Up); | 32 | let rx_pin = Input::new(&mut p.PA11, Pull::Up); |
| 24 | core::mem::forget(rx_pin); | 33 | core::mem::forget(rx_pin); |
| 25 | 34 | ||
| 26 | let mut can = Can::new(p.CAN1, p.PA11, p.PA12); | 35 | let mut can = Can::new(p.CAN1, p.PA11, p.PA12, Irqs); |
| 27 | 36 | ||
| 28 | can.modify_filters().enable_bank(0, Fifo::Fifo0, Mask32::accept_all()); | 37 | can.modify_filters().enable_bank(0, Fifo::Fifo0, Mask32::accept_all()); |
| 29 | 38 | ||
diff --git a/tests/stm32/Cargo.toml b/tests/stm32/Cargo.toml index 487ef4626..365f631b7 100644 --- a/tests/stm32/Cargo.toml +++ b/tests/stm32/Cargo.toml | |||
| @@ -7,7 +7,7 @@ autobins = false | |||
| 7 | 7 | ||
| 8 | [features] | 8 | [features] |
| 9 | stm32f103c8 = ["embassy-stm32/stm32f103c8", "not-gpdma"] # Blue Pill | 9 | stm32f103c8 = ["embassy-stm32/stm32f103c8", "not-gpdma"] # Blue Pill |
| 10 | stm32f429zi = ["embassy-stm32/stm32f429zi", "chrono", "not-gpdma"] # Nucleo "sdmmc" | 10 | stm32f429zi = ["embassy-stm32/stm32f429zi", "chrono", "can", "not-gpdma"] # Nucleo "sdmmc" |
| 11 | stm32g071rb = ["embassy-stm32/stm32g071rb", "not-gpdma"] # Nucleo | 11 | stm32g071rb = ["embassy-stm32/stm32g071rb", "not-gpdma"] # Nucleo |
| 12 | stm32c031c6 = ["embassy-stm32/stm32c031c6", "not-gpdma"] # Nucleo | 12 | stm32c031c6 = ["embassy-stm32/stm32c031c6", "not-gpdma"] # Nucleo |
| 13 | stm32g491re = ["embassy-stm32/stm32g491re", "not-gpdma"] # Nucleo | 13 | stm32g491re = ["embassy-stm32/stm32g491re", "not-gpdma"] # Nucleo |
| @@ -18,6 +18,7 @@ stm32u585ai = ["embassy-stm32/stm32u585ai"] # IoT board | |||
| 18 | 18 | ||
| 19 | sdmmc = [] | 19 | sdmmc = [] |
| 20 | chrono = ["embassy-stm32/chrono", "dep:chrono"] | 20 | chrono = ["embassy-stm32/chrono", "dep:chrono"] |
| 21 | can = [] | ||
| 21 | ble = ["dep:embassy-stm32-wpan"] | 22 | ble = ["dep:embassy-stm32-wpan"] |
| 22 | not-gpdma = [] | 23 | not-gpdma = [] |
| 23 | 24 | ||
| @@ -53,6 +54,11 @@ path = "src/bin/tl_mbox.rs" | |||
| 53 | required-features = [ "ble",] | 54 | required-features = [ "ble",] |
| 54 | 55 | ||
| 55 | [[bin]] | 56 | [[bin]] |
| 57 | name = "can" | ||
| 58 | path = "src/bin/can.rs" | ||
| 59 | required-features = [ "can",] | ||
| 60 | |||
| 61 | [[bin]] | ||
| 56 | name = "gpio" | 62 | name = "gpio" |
| 57 | path = "src/bin/gpio.rs" | 63 | path = "src/bin/gpio.rs" |
| 58 | required-features = [] | 64 | required-features = [] |
diff --git a/tests/stm32/src/bin/can.rs b/tests/stm32/src/bin/can.rs new file mode 100644 index 000000000..33d63d546 --- /dev/null +++ b/tests/stm32/src/bin/can.rs | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | // required-features: can | ||
| 6 | |||
| 7 | #[path = "../common.rs"] | ||
| 8 | mod common; | ||
| 9 | use common::*; | ||
| 10 | use embassy_executor::Spawner; | ||
| 11 | use embassy_stm32::bind_interrupts; | ||
| 12 | use embassy_stm32::can::bxcan::filter::Mask32; | ||
| 13 | use embassy_stm32::can::bxcan::{Fifo, Frame, StandardId}; | ||
| 14 | use embassy_stm32::can::{Can, Rx0InterruptHandler, Rx1InterruptHandler, SceInterruptHandler, TxInterruptHandler}; | ||
| 15 | use embassy_stm32::gpio::{Input, Pull}; | ||
| 16 | use embassy_stm32::peripherals::CAN1; | ||
| 17 | use {defmt_rtt as _, panic_probe as _}; | ||
| 18 | |||
| 19 | bind_interrupts!(struct Irqs { | ||
| 20 | CAN1_RX0 => Rx0InterruptHandler<CAN1>; | ||
| 21 | CAN1_RX1 => Rx1InterruptHandler<CAN1>; | ||
| 22 | CAN1_SCE => SceInterruptHandler<CAN1>; | ||
| 23 | CAN1_TX => TxInterruptHandler<CAN1>; | ||
| 24 | }); | ||
| 25 | |||
| 26 | #[embassy_executor::main] | ||
| 27 | async fn main(_spawner: Spawner) { | ||
| 28 | let mut p = embassy_stm32::init(config()); | ||
| 29 | info!("Hello World!"); | ||
| 30 | |||
| 31 | // HW is connected as follows: | ||
| 32 | // PB13 -> PD0 | ||
| 33 | // PB12 -> PD1 | ||
| 34 | |||
| 35 | // The next two lines are a workaround for testing without transceiver. | ||
| 36 | // To synchronise to the bus the RX input needs to see a high level. | ||
| 37 | // Use `mem::forget()` to release the borrow on the pin but keep the | ||
| 38 | // pull-up resistor enabled. | ||
| 39 | let rx_pin = Input::new(&mut p.PD0, Pull::Up); | ||
| 40 | core::mem::forget(rx_pin); | ||
| 41 | |||
| 42 | let mut can = Can::new(p.CAN1, p.PD0, p.PD1, Irqs); | ||
| 43 | |||
| 44 | info!("Configuring can..."); | ||
| 45 | |||
| 46 | can.modify_filters().enable_bank(0, Fifo::Fifo0, Mask32::accept_all()); | ||
| 47 | |||
| 48 | can.set_bitrate(1_000_000); | ||
| 49 | can.modify_config() | ||
| 50 | .set_loopback(true) // Receive own frames | ||
| 51 | .set_silent(true) | ||
| 52 | // .set_bit_timing(0x001c0003) | ||
| 53 | .enable(); | ||
| 54 | |||
| 55 | info!("Can configured"); | ||
| 56 | |||
| 57 | let mut i: u8 = 0; | ||
| 58 | loop { | ||
| 59 | let tx_frame = Frame::new_data(unwrap!(StandardId::new(i as _)), [i]); | ||
| 60 | |||
| 61 | info!("Transmitting frame..."); | ||
| 62 | can.write(&tx_frame).await; | ||
| 63 | |||
| 64 | info!("Receiving frame..."); | ||
| 65 | let (time, rx_frame) = can.read().await.unwrap(); | ||
| 66 | |||
| 67 | info!("loopback time {}", time); | ||
| 68 | info!("loopback frame {=u8}", rx_frame.data().unwrap()[0]); | ||
| 69 | |||
| 70 | i += 1; | ||
| 71 | if i > 10 { | ||
| 72 | break; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | info!("Test OK"); | ||
| 77 | cortex_m::asm::bkpt(); | ||
| 78 | } | ||
diff --git a/tests/stm32/src/bin/usart_dma.rs b/tests/stm32/src/bin/usart_dma.rs index 50dd2893e..c34d9574b 100644 --- a/tests/stm32/src/bin/usart_dma.rs +++ b/tests/stm32/src/bin/usart_dma.rs | |||
| @@ -69,24 +69,27 @@ async fn main(_spawner: Spawner) { | |||
| 69 | const LEN: usize = 128; | 69 | const LEN: usize = 128; |
| 70 | let mut tx_buf = [0; LEN]; | 70 | let mut tx_buf = [0; LEN]; |
| 71 | let mut rx_buf = [0; LEN]; | 71 | let mut rx_buf = [0; LEN]; |
| 72 | for i in 0..LEN { | ||
| 73 | tx_buf[i] = i as u8; | ||
| 74 | } | ||
| 75 | 72 | ||
| 76 | let (mut tx, mut rx) = usart.split(); | 73 | let (mut tx, mut rx) = usart.split(); |
| 77 | 74 | ||
| 78 | let tx_fut = async { | 75 | for n in 0..42 { |
| 79 | tx.write(&tx_buf).await.unwrap(); | 76 | for i in 0..LEN { |
| 80 | }; | 77 | tx_buf[i] = (i ^ n) as u8; |
| 81 | let rx_fut = async { | 78 | } |
| 82 | rx.read(&mut rx_buf).await.unwrap(); | 79 | |
| 83 | }; | 80 | let tx_fut = async { |
| 81 | tx.write(&tx_buf).await.unwrap(); | ||
| 82 | }; | ||
| 83 | let rx_fut = async { | ||
| 84 | rx.read(&mut rx_buf).await.unwrap(); | ||
| 85 | }; | ||
| 84 | 86 | ||
| 85 | // note: rx needs to be polled first, to workaround this bug: | 87 | // note: rx needs to be polled first, to workaround this bug: |
| 86 | // https://github.com/embassy-rs/embassy/issues/1426 | 88 | // https://github.com/embassy-rs/embassy/issues/1426 |
| 87 | join(rx_fut, tx_fut).await; | 89 | join(rx_fut, tx_fut).await; |
| 88 | 90 | ||
| 89 | assert_eq!(tx_buf, rx_buf); | 91 | assert_eq!(tx_buf, rx_buf); |
| 92 | } | ||
| 90 | 93 | ||
| 91 | info!("Test OK"); | 94 | info!("Test OK"); |
| 92 | cortex_m::asm::bkpt(); | 95 | cortex_m::asm::bkpt(); |
