-
Notifications
You must be signed in to change notification settings - Fork 369
Add DMA support for ESP32-C2 and ESP32-S3 #234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| //! SPI loopback test using DMA | ||
| //! | ||
| //! Folowing pins are used: | ||
| //! SCLK GPIO6 | ||
| //! MISO GPIO2 | ||
| //! MOSI GPIO7 | ||
| //! CS GPIO10 | ||
| //! | ||
| //! Depending on your target and the board you are using you have to change the | ||
| //! pins. | ||
| //! | ||
| //! This example transfers data via SPI. | ||
| //! Connect MISO and MOSI pins to see the outgoing data is read as incoming | ||
| //! data. | ||
|
|
||
| #![no_std] | ||
| #![no_main] | ||
|
|
||
| use esp32c2_hal::{ | ||
| clock::ClockControl, | ||
| dma::{DmaPriority, DmaTransferRxTx}, | ||
| gdma::Gdma, | ||
| gpio::IO, | ||
| pac::Peripherals, | ||
| prelude::*, | ||
| spi::{dma::WithDmaSpi2, Spi, SpiMode}, | ||
| timer::TimerGroup, | ||
| Delay, | ||
| Rtc, | ||
| }; | ||
| use esp_backtrace as _; | ||
| use esp_println::println; | ||
| use riscv_rt::entry; | ||
|
|
||
| #[entry] | ||
| fn main() -> ! { | ||
| let peripherals = Peripherals::take().unwrap(); | ||
| let mut system = peripherals.SYSTEM.split(); | ||
| let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); | ||
|
|
||
| // Disable the watchdog timers. For the ESP32-C2, this includes the Super WDT, | ||
| // the RTC WDT, and the TIMG WDT. | ||
| let mut rtc = Rtc::new(peripherals.RTC_CNTL); | ||
| let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); | ||
| let mut wdt0 = timer_group0.wdt; | ||
|
|
||
| rtc.swd.disable(); | ||
| rtc.rwdt.disable(); | ||
| wdt0.disable(); | ||
|
|
||
| let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); | ||
| let sclk = io.pins.gpio6; | ||
| let miso = io.pins.gpio2; | ||
| let mosi = io.pins.gpio7; | ||
| let cs = io.pins.gpio10; | ||
|
|
||
| let dma = Gdma::new(peripherals.DMA, &mut system.peripheral_clock_control); | ||
| let dma_channel = dma.channel0; | ||
|
|
||
| let mut descriptors = [0u32; 8 * 3]; | ||
| let mut rx_descriptors = [0u32; 8 * 3]; | ||
|
|
||
| let mut spi = Spi::new( | ||
| peripherals.SPI2, | ||
| sclk, | ||
| mosi, | ||
| miso, | ||
| cs, | ||
| 100u32.kHz(), | ||
| SpiMode::Mode0, | ||
| &mut system.peripheral_clock_control, | ||
| &clocks, | ||
| ) | ||
| .with_dma(dma_channel.configure( | ||
| false, | ||
| &mut descriptors, | ||
| &mut rx_descriptors, | ||
| DmaPriority::Priority0, | ||
| )); | ||
|
|
||
| let mut delay = Delay::new(&clocks); | ||
|
|
||
| // DMA buffer require a static life-time | ||
| let mut send = buffer1(); | ||
| let mut receive = buffer2(); | ||
| let mut i = 0; | ||
|
|
||
| for (i, v) in send.iter_mut().enumerate() { | ||
| *v = (i % 255) as u8; | ||
| } | ||
|
|
||
| loop { | ||
| send[0] = i; | ||
| send[send.len() - 1] = i; | ||
| i = i.wrapping_add(1); | ||
|
|
||
| let transfer = spi.dma_transfer(send, receive).unwrap(); | ||
| // here we could do something else while DMA transfer is in progress | ||
| // the buffers and spi is moved into the transfer and we can get it back via | ||
| // `wait` | ||
| (receive, send, spi) = transfer.wait(); | ||
| println!( | ||
| "{:x?} .. {:x?}", | ||
| &receive[..10], | ||
| &receive[receive.len() - 10..] | ||
| ); | ||
|
|
||
| delay.delay_ms(250u32); | ||
| } | ||
| } | ||
|
|
||
| fn buffer1() -> &'static mut [u8; 32000] { | ||
| static mut BUFFER: [u8; 32000] = [0u8; 32000]; | ||
| unsafe { &mut BUFFER } | ||
| } | ||
|
|
||
| fn buffer2() -> &'static mut [u8; 32000] { | ||
| static mut BUFFER: [u8; 32000] = [0u8; 32000]; | ||
| unsafe { &mut BUFFER } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.