diff options
Diffstat (limited to 'examples/mimxrt6')
| -rw-r--r-- | examples/mimxrt6/Cargo.toml | 21 | ||||
| -rw-r--r-- | examples/mimxrt6/src/bin/button.rs | 29 | ||||
| -rw-r--r-- | examples/mimxrt6/src/bin/crc.rs | 175 | ||||
| -rw-r--r-- | examples/mimxrt6/src/bin/rng.rs | 39 | ||||
| -rw-r--r-- | examples/mimxrt6/src/bin/uart-async.rs | 87 | ||||
| -rw-r--r-- | examples/mimxrt6/src/bin/uart.rs | 55 |
6 files changed, 398 insertions, 8 deletions
diff --git a/examples/mimxrt6/Cargo.toml b/examples/mimxrt6/Cargo.toml index b0c56f003..28de9d273 100644 --- a/examples/mimxrt6/Cargo.toml +++ b/examples/mimxrt6/Cargo.toml | |||
| @@ -3,24 +3,24 @@ name = "embassy-imxrt-examples" | |||
| 3 | version = "0.1.0" | 3 | version = "0.1.0" |
| 4 | edition = "2021" | 4 | edition = "2021" |
| 5 | license = "MIT or Apache-2.0" | 5 | license = "MIT or Apache-2.0" |
| 6 | publish = false | ||
| 6 | 7 | ||
| 7 | [dependencies] | 8 | [dependencies] |
| 8 | cortex-m = { version = "0.7.7", features = ["inline-asm", "critical-section-single-core"] } | 9 | cortex-m = { version = "0.7.7", features = ["inline-asm", "critical-section-single-core"] } |
| 9 | cortex-m-rt = "0.7.3" | 10 | cortex-m-rt = "0.7.3" |
| 10 | defmt = "1.0" | 11 | defmt = "1.0.1" |
| 11 | defmt-rtt = "1.0" | 12 | defmt-rtt = "1.0.0" |
| 12 | 13 | ||
| 13 | embassy-executor = { version = "0.7.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "executor-interrupt", "defmt"] } | 14 | embassy-executor = { version = "0.9.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "executor-interrupt", "defmt"] } |
| 14 | embassy-futures = { version = "0.1.1", path = "../../embassy-futures" } | 15 | embassy-futures = { version = "0.1.2", path = "../../embassy-futures" } |
| 15 | embassy-imxrt = { version = "0.1.0", path = "../../embassy-imxrt", features = ["defmt", "mimxrt685s", "unstable-pac", "time", "time-driver-os-timer"] } | 16 | embassy-imxrt = { version = "0.1.0", path = "../../embassy-imxrt", features = ["defmt", "mimxrt685s", "unstable-pac", "time", "time-driver-os-timer"] } |
| 16 | embassy-time = { version = "0.4", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } | 17 | embassy-time = { version = "0.5.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } |
| 17 | embassy-sync = { version = "0.6.2", path = "../../embassy-sync", features = ["defmt"] } | 18 | embassy-sync = { version = "0.7.2", path = "../../embassy-sync", features = ["defmt"] } |
| 18 | embedded-hal-1 = { package = "embedded-hal", version = "1.0" } | 19 | embedded-hal-1 = { package = "embedded-hal", version = "1.0" } |
| 19 | embedded-hal-async = "1.0.0" | 20 | embedded-hal-async = "1.0.0" |
| 20 | 21 | ||
| 21 | mimxrt600-fcb = "0.2.2" | 22 | mimxrt600-fcb = "0.2.2" |
| 22 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 23 | panic-probe = { version = "1.0.0", features = ["print-defmt"] } |
| 23 | rand = { version = "0.8.5", default-features = false } | ||
| 24 | 24 | ||
| 25 | # cargo build/run | 25 | # cargo build/run |
| 26 | [profile.dev] | 26 | [profile.dev] |
| @@ -59,3 +59,8 @@ incremental = false | |||
| 59 | lto = 'fat' | 59 | lto = 'fat' |
| 60 | opt-level = 3 # <- | 60 | opt-level = 3 # <- |
| 61 | overflow-checks = false # <- | 61 | overflow-checks = false # <- |
| 62 | |||
| 63 | [package.metadata.embassy] | ||
| 64 | build = [ | ||
| 65 | { target = "thumbv8m.main-none-eabihf", artifact-dir = "out/examples/mimxrt6" } | ||
| 66 | ] | ||
diff --git a/examples/mimxrt6/src/bin/button.rs b/examples/mimxrt6/src/bin/button.rs new file mode 100644 index 000000000..efb7f14af --- /dev/null +++ b/examples/mimxrt6/src/bin/button.rs | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::info; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_futures::select::{select, Either}; | ||
| 7 | use embassy_imxrt::gpio; | ||
| 8 | use {defmt_rtt as _, embassy_imxrt_examples as _, panic_probe as _}; | ||
| 9 | |||
| 10 | #[embassy_executor::main] | ||
| 11 | async fn main(_spawner: Spawner) { | ||
| 12 | let p = embassy_imxrt::init(Default::default()); | ||
| 13 | |||
| 14 | let mut user1 = gpio::Input::new(p.PIO1_1, gpio::Pull::None, gpio::Inverter::Disabled); | ||
| 15 | let mut user2 = gpio::Input::new(p.PIO0_10, gpio::Pull::None, gpio::Inverter::Disabled); | ||
| 16 | |||
| 17 | loop { | ||
| 18 | let res = select(user1.wait_for_falling_edge(), user2.wait_for_falling_edge()).await; | ||
| 19 | |||
| 20 | match res { | ||
| 21 | Either::First(()) => { | ||
| 22 | info!("Button `USER1' pressed"); | ||
| 23 | } | ||
| 24 | Either::Second(()) => { | ||
| 25 | info!("Button `USER2' pressed"); | ||
| 26 | } | ||
| 27 | } | ||
| 28 | } | ||
| 29 | } | ||
diff --git a/examples/mimxrt6/src/bin/crc.rs b/examples/mimxrt6/src/bin/crc.rs new file mode 100644 index 000000000..005a250e5 --- /dev/null +++ b/examples/mimxrt6/src/bin/crc.rs | |||
| @@ -0,0 +1,175 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | extern crate embassy_imxrt_examples; | ||
| 5 | |||
| 6 | use defmt::*; | ||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_imxrt::crc::{Config, Crc, Polynomial}; | ||
| 9 | use {defmt_rtt as _, panic_probe as _}; | ||
| 10 | |||
| 11 | #[embassy_executor::main] | ||
| 12 | async fn main(_spawner: Spawner) { | ||
| 13 | let mut p = embassy_imxrt::init(Default::default()); | ||
| 14 | let data = b"123456789"; | ||
| 15 | |||
| 16 | info!("Initializing CRC"); | ||
| 17 | |||
| 18 | // CRC-CCITT | ||
| 19 | let mut crc = Crc::new(p.CRC.reborrow(), Default::default()); | ||
| 20 | let output = crc.feed_bytes(data); | ||
| 21 | defmt::assert_eq!(output, 0x29b1); | ||
| 22 | |||
| 23 | // CRC16-ARC | ||
| 24 | let mut crc = Crc::new( | ||
| 25 | p.CRC.reborrow(), | ||
| 26 | Config { | ||
| 27 | polynomial: Polynomial::Crc16, | ||
| 28 | reverse_in: true, | ||
| 29 | reverse_out: true, | ||
| 30 | complement_out: false, | ||
| 31 | seed: 0, | ||
| 32 | ..Default::default() | ||
| 33 | }, | ||
| 34 | ); | ||
| 35 | let output = crc.feed_bytes(data); | ||
| 36 | defmt::assert_eq!(output, 0xbb3d); | ||
| 37 | |||
| 38 | // CRC16-CMS | ||
| 39 | let mut crc = Crc::new( | ||
| 40 | p.CRC.reborrow(), | ||
| 41 | Config { | ||
| 42 | polynomial: Polynomial::Crc16, | ||
| 43 | reverse_in: false, | ||
| 44 | reverse_out: false, | ||
| 45 | complement_out: false, | ||
| 46 | seed: 0xffff, | ||
| 47 | ..Default::default() | ||
| 48 | }, | ||
| 49 | ); | ||
| 50 | let output = crc.feed_bytes(data); | ||
| 51 | defmt::assert_eq!(output, 0xaee7); | ||
| 52 | |||
| 53 | // CRC16-DDS-110 | ||
| 54 | let mut crc = Crc::new( | ||
| 55 | p.CRC.reborrow(), | ||
| 56 | Config { | ||
| 57 | polynomial: Polynomial::Crc16, | ||
| 58 | reverse_in: false, | ||
| 59 | reverse_out: false, | ||
| 60 | complement_out: false, | ||
| 61 | seed: 0x800d, | ||
| 62 | ..Default::default() | ||
| 63 | }, | ||
| 64 | ); | ||
| 65 | let output = crc.feed_bytes(data); | ||
| 66 | defmt::assert_eq!(output, 0x9ecf); | ||
| 67 | |||
| 68 | // CRC16-MAXIM-DOW | ||
| 69 | let mut crc = Crc::new( | ||
| 70 | p.CRC.reborrow(), | ||
| 71 | Config { | ||
| 72 | polynomial: Polynomial::Crc16, | ||
| 73 | reverse_in: true, | ||
| 74 | reverse_out: true, | ||
| 75 | complement_out: true, | ||
| 76 | seed: 0, | ||
| 77 | ..Default::default() | ||
| 78 | }, | ||
| 79 | ); | ||
| 80 | let output = crc.feed_bytes(data); | ||
| 81 | defmt::assert_eq!(output, 0x44c2); | ||
| 82 | |||
| 83 | // CRC16-MODBUS | ||
| 84 | let mut crc = Crc::new( | ||
| 85 | p.CRC.reborrow(), | ||
| 86 | Config { | ||
| 87 | polynomial: Polynomial::Crc16, | ||
| 88 | reverse_in: true, | ||
| 89 | reverse_out: true, | ||
| 90 | complement_out: false, | ||
| 91 | seed: 0xffff, | ||
| 92 | ..Default::default() | ||
| 93 | }, | ||
| 94 | ); | ||
| 95 | let output = crc.feed_bytes(data); | ||
| 96 | defmt::assert_eq!(output, 0x4b37); | ||
| 97 | |||
| 98 | // CRC32-BZIP2 | ||
| 99 | let mut crc = Crc::new( | ||
| 100 | p.CRC.reborrow(), | ||
| 101 | Config { | ||
| 102 | polynomial: Polynomial::Crc32, | ||
| 103 | reverse_in: false, | ||
| 104 | reverse_out: false, | ||
| 105 | complement_out: true, | ||
| 106 | seed: 0xffff_ffff, | ||
| 107 | ..Default::default() | ||
| 108 | }, | ||
| 109 | ); | ||
| 110 | let output = crc.feed_bytes(data); | ||
| 111 | defmt::assert_eq!(output, 0xfc89_1918); | ||
| 112 | |||
| 113 | // CRC32-CKSUM | ||
| 114 | let mut crc = Crc::new( | ||
| 115 | p.CRC.reborrow(), | ||
| 116 | Config { | ||
| 117 | polynomial: Polynomial::Crc32, | ||
| 118 | reverse_in: false, | ||
| 119 | reverse_out: false, | ||
| 120 | complement_out: true, | ||
| 121 | seed: 0, | ||
| 122 | ..Default::default() | ||
| 123 | }, | ||
| 124 | ); | ||
| 125 | let output = crc.feed_bytes(data); | ||
| 126 | defmt::assert_eq!(output, 0x765e_7680); | ||
| 127 | |||
| 128 | // CRC32-ISO-HDLC | ||
| 129 | let mut crc = Crc::new( | ||
| 130 | p.CRC.reborrow(), | ||
| 131 | Config { | ||
| 132 | polynomial: Polynomial::Crc32, | ||
| 133 | reverse_in: true, | ||
| 134 | reverse_out: true, | ||
| 135 | complement_out: true, | ||
| 136 | seed: 0xffff_ffff, | ||
| 137 | ..Default::default() | ||
| 138 | }, | ||
| 139 | ); | ||
| 140 | let output = crc.feed_bytes(data); | ||
| 141 | defmt::assert_eq!(output, 0xcbf4_3926); | ||
| 142 | |||
| 143 | // CRC32-JAMCRC | ||
| 144 | let mut crc = Crc::new( | ||
| 145 | p.CRC.reborrow(), | ||
| 146 | Config { | ||
| 147 | polynomial: Polynomial::Crc32, | ||
| 148 | reverse_in: true, | ||
| 149 | reverse_out: true, | ||
| 150 | complement_out: false, | ||
| 151 | seed: 0xffff_ffff, | ||
| 152 | ..Default::default() | ||
| 153 | }, | ||
| 154 | ); | ||
| 155 | let output = crc.feed_bytes(data); | ||
| 156 | defmt::assert_eq!(output, 0x340b_c6d9); | ||
| 157 | |||
| 158 | // CRC32-MPEG-2 | ||
| 159 | let mut crc = Crc::new( | ||
| 160 | p.CRC.reborrow(), | ||
| 161 | Config { | ||
| 162 | polynomial: Polynomial::Crc32, | ||
| 163 | reverse_in: false, | ||
| 164 | reverse_out: false, | ||
| 165 | complement_out: false, | ||
| 166 | seed: 0xffff_ffff, | ||
| 167 | ..Default::default() | ||
| 168 | }, | ||
| 169 | ); | ||
| 170 | let output = crc.feed_bytes(data); | ||
| 171 | defmt::assert_eq!(output, 0x0376_e6e7); | ||
| 172 | |||
| 173 | info!("end program"); | ||
| 174 | cortex_m::asm::bkpt(); | ||
| 175 | } | ||
diff --git a/examples/mimxrt6/src/bin/rng.rs b/examples/mimxrt6/src/bin/rng.rs new file mode 100644 index 000000000..9468dd109 --- /dev/null +++ b/examples/mimxrt6/src/bin/rng.rs | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | extern crate embassy_imxrt_examples; | ||
| 5 | |||
| 6 | use defmt::*; | ||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_imxrt::rng::Rng; | ||
| 9 | use embassy_imxrt::{bind_interrupts, peripherals, rng}; | ||
| 10 | use {defmt_rtt as _, panic_probe as _}; | ||
| 11 | |||
| 12 | bind_interrupts!(struct Irqs { | ||
| 13 | RNG => rng::InterruptHandler<peripherals::RNG>; | ||
| 14 | }); | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) { | ||
| 18 | let p = embassy_imxrt::init(Default::default()); | ||
| 19 | |||
| 20 | info!("Initializing RNG"); | ||
| 21 | let mut rng = Rng::new(p.RNG, Irqs); | ||
| 22 | let mut buf = [0u8; 65]; | ||
| 23 | |||
| 24 | // Async interface | ||
| 25 | unwrap!(rng.async_fill_bytes(&mut buf).await); | ||
| 26 | info!("random bytes: {:02x}", buf); | ||
| 27 | |||
| 28 | // RngCore interface | ||
| 29 | let mut random_bytes = [0; 16]; | ||
| 30 | |||
| 31 | let random_u32 = rng.blocking_next_u32(); | ||
| 32 | let random_u64 = rng.blocking_next_u64(); | ||
| 33 | |||
| 34 | rng.blocking_fill_bytes(&mut random_bytes); | ||
| 35 | |||
| 36 | info!("random_u32 {}", random_u32); | ||
| 37 | info!("random_u64 {}", random_u64); | ||
| 38 | info!("random_bytes {}", random_bytes); | ||
| 39 | } | ||
diff --git a/examples/mimxrt6/src/bin/uart-async.rs b/examples/mimxrt6/src/bin/uart-async.rs new file mode 100644 index 000000000..d808d755c --- /dev/null +++ b/examples/mimxrt6/src/bin/uart-async.rs | |||
| @@ -0,0 +1,87 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | extern crate embassy_imxrt_examples; | ||
| 5 | |||
| 6 | use defmt::info; | ||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_imxrt::flexcomm::uart::{self, Async, Uart}; | ||
| 9 | use embassy_imxrt::{bind_interrupts, peripherals}; | ||
| 10 | use embassy_time::Timer; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | bind_interrupts!(struct Irqs { | ||
| 14 | FLEXCOMM2 => uart::InterruptHandler<peripherals::FLEXCOMM2>; | ||
| 15 | FLEXCOMM4 => uart::InterruptHandler<peripherals::FLEXCOMM4>; | ||
| 16 | }); | ||
| 17 | |||
| 18 | const BUFLEN: usize = 16; | ||
| 19 | |||
| 20 | #[embassy_executor::task] | ||
| 21 | async fn usart4_task(mut uart: Uart<'static, Async>) { | ||
| 22 | info!("RX Task"); | ||
| 23 | |||
| 24 | loop { | ||
| 25 | let mut rx_buf = [0; BUFLEN]; | ||
| 26 | uart.read(&mut rx_buf).await.unwrap(); | ||
| 27 | info!("usart4: rx_buf {:02x}", rx_buf); | ||
| 28 | |||
| 29 | Timer::after_millis(10).await; | ||
| 30 | |||
| 31 | let tx_buf = [0xaa; BUFLEN]; | ||
| 32 | uart.write(&tx_buf).await.unwrap(); | ||
| 33 | info!("usart4: tx_buf {:02x}", tx_buf); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | #[embassy_executor::task] | ||
| 38 | async fn usart2_task(mut uart: Uart<'static, Async>) { | ||
| 39 | info!("TX Task"); | ||
| 40 | |||
| 41 | loop { | ||
| 42 | let tx_buf = [0x55; BUFLEN]; | ||
| 43 | uart.write(&tx_buf).await.unwrap(); | ||
| 44 | info!("usart2: tx_buf {:02x}", tx_buf); | ||
| 45 | |||
| 46 | Timer::after_millis(10).await; | ||
| 47 | |||
| 48 | let mut rx_buf = [0x00; BUFLEN]; | ||
| 49 | uart.read(&mut rx_buf).await.unwrap(); | ||
| 50 | info!("usart2: rx_buf {:02x}", rx_buf); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | #[embassy_executor::main] | ||
| 55 | async fn main(spawner: Spawner) { | ||
| 56 | let p = embassy_imxrt::init(Default::default()); | ||
| 57 | |||
| 58 | info!("UART test start"); | ||
| 59 | |||
| 60 | let usart4 = Uart::new_with_rtscts( | ||
| 61 | p.FLEXCOMM4, | ||
| 62 | p.PIO0_29, | ||
| 63 | p.PIO0_30, | ||
| 64 | p.PIO1_0, | ||
| 65 | p.PIO0_31, | ||
| 66 | Irqs, | ||
| 67 | p.DMA0_CH9, | ||
| 68 | p.DMA0_CH8, | ||
| 69 | Default::default(), | ||
| 70 | ) | ||
| 71 | .unwrap(); | ||
| 72 | spawner.spawn(usart4_task(usart4).unwrap()); | ||
| 73 | |||
| 74 | let usart2 = Uart::new_with_rtscts( | ||
| 75 | p.FLEXCOMM2, | ||
| 76 | p.PIO0_15, | ||
| 77 | p.PIO0_16, | ||
| 78 | p.PIO0_18, | ||
| 79 | p.PIO0_17, | ||
| 80 | Irqs, | ||
| 81 | p.DMA0_CH5, | ||
| 82 | p.DMA0_CH4, | ||
| 83 | Default::default(), | ||
| 84 | ) | ||
| 85 | .unwrap(); | ||
| 86 | spawner.spawn(usart2_task(usart2).unwrap()); | ||
| 87 | } | ||
diff --git a/examples/mimxrt6/src/bin/uart.rs b/examples/mimxrt6/src/bin/uart.rs new file mode 100644 index 000000000..1636c958f --- /dev/null +++ b/examples/mimxrt6/src/bin/uart.rs | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | extern crate embassy_imxrt_examples; | ||
| 5 | |||
| 6 | use defmt::info; | ||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_imxrt::flexcomm::uart::{Blocking, Uart, UartRx, UartTx}; | ||
| 9 | use embassy_time::Timer; | ||
| 10 | use {defmt_rtt as _, panic_probe as _}; | ||
| 11 | |||
| 12 | #[embassy_executor::task] | ||
| 13 | async fn usart4_task(mut uart: UartRx<'static, Blocking>) { | ||
| 14 | info!("RX Task"); | ||
| 15 | |||
| 16 | loop { | ||
| 17 | let mut buf = [0; 8]; | ||
| 18 | |||
| 19 | Timer::after_millis(10).await; | ||
| 20 | |||
| 21 | uart.blocking_read(&mut buf).unwrap(); | ||
| 22 | |||
| 23 | let s = core::str::from_utf8(&buf).unwrap(); | ||
| 24 | |||
| 25 | info!("Received '{}'", s); | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | #[embassy_executor::task] | ||
| 30 | async fn usart2_task(mut uart: UartTx<'static, Blocking>) { | ||
| 31 | info!("TX Task"); | ||
| 32 | |||
| 33 | loop { | ||
| 34 | let buf = "Testing\0".as_bytes(); | ||
| 35 | |||
| 36 | uart.blocking_write(buf).unwrap(); | ||
| 37 | |||
| 38 | Timer::after_millis(10).await; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | #[embassy_executor::main] | ||
| 43 | async fn main(spawner: Spawner) { | ||
| 44 | let p = embassy_imxrt::init(Default::default()); | ||
| 45 | |||
| 46 | info!("UART test start"); | ||
| 47 | |||
| 48 | let usart4 = Uart::new_blocking(p.FLEXCOMM4, p.PIO0_29, p.PIO0_30, Default::default()).unwrap(); | ||
| 49 | |||
| 50 | let (_, usart4) = usart4.split(); | ||
| 51 | spawner.spawn(usart4_task(usart4).unwrap()); | ||
| 52 | |||
| 53 | let usart2 = UartTx::new_blocking(p.FLEXCOMM2, p.PIO0_15, Default::default()).unwrap(); | ||
| 54 | spawner.spawn(usart2_task(usart2).unwrap()); | ||
| 55 | } | ||
