当前文档版本为 v0.4.0,您可以访问当前页面的 开发中 版本以获取最近可能的更新。

Driver: DMA M2P&P2M

1 功能概述

dma_m2p2m sampleMemory to UARTUART to Memory 传输为例,演示了 Zephyr DMA Memory-to-Peripheral 和 Peripheral-to-Memory 的使用方法。

2 环境准备

  • PAN1080 EVB一块

  • Micro USB线一条(用于供电和查看串口打印Log)

  • 硬件接线:

    • 使用USB线,将PC USB与EVB MicroUSB(USB->UART)相连

    • 使用杜邦线将EVB上的:

      • UART1 TX与P06相连

      • UART1 RX与P07相连

      • UART0 自身的 TX (P00) 与 RX (P01) 相连

  • PC软件: 串口调试助手(UartAssist)或终端工具(SecureCRT),波特率921600

3 编译和烧录

例程位置:zephyr\samples_panchip\drivers\dma_m2p2m

目前可使用ZAL工具或quick build脚本进行编译和下载。

脚本位置:quick_build_samples\drivers\dma_m2p2m.bat

打开脚本后默认会编译项目,编译完成时,可输入字符进行后续下载等操作:

Input the keyword to continue:
  'b' build                         编译项目
  'r' make clean and rebuild        重新编译项目
  'f' flash download                下载
  'e' erase chip                    擦除芯片
  'o' open project by VS Code       打开 `VS Code`,可查看源码,执行编译下载等
  others exit                       退出
wait input:

4 演示说明

本例程中,同时配置了2个 DMA 通道,其中:

  1. 一个通道配置成 Memory-to-Peripheral 的方式,用于将一个字符串从 Flash 搬移到 UART0 Tx FIFO

  2. 另一个通道配置成 Peripheral-to-Memory 的方式,用于将 UART0 Rx FIFO 接收到的内容搬移到 SRAM

本例程涉及如下的 DMA Driver APIs:

  • dma_request_channel()

  • dma_release_channel()

  • dma_config()

  • dma_start()

  • dma_get_status()

例程执行成功的 Log 如下所示:

*** Booting Zephyr OS build zephyr-v2.7.0-481-ge520fced9ef8  ***
[00:00:00.000,000] <inf> dma_dw: Device DMA_0 initialized
[00:00:00.000,000] <inf> app: DMA M2P2M (Memory to Peripheral to Memory) demo start..
[00:00:00.001,000] <inf> app: Configure and start DMA transfer procedure, chan_id=0
[00:00:00.001,000] <inf> app: Configure and start DMA transfer procedure, chan_id=1
[00:00:00.001,000] <inf> app: DMA m2p status: direction=1, busy=1
[00:00:00.001,000] <inf> app: DMA p2m status: direction=2, busy=1
[00:00:00.002,000] <inf> app: DMA m2p status: direction=1, busy=1
[00:00:00.002,000] <inf> app: DMA p2m status: direction=2, busy=1
[00:00:00.003,000] <inf> app: dma_m2p_transfer_cb: DMA m2p transfer done
[00:00:00.004,000] <inf> app: DMA m2p status: direction=1, busy=0
[00:00:00.004,000] <inf> app: DMA p2m status: direction=2, busy=1
[00:00:00.004,000] <inf> app: dma_p2m_transfer_cb: DMA p2m transfer done
[00:00:00.005,000] <inf> app: DMA m2p status: direction=1, busy=0
[00:00:00.005,000] <inf> app: DMA p2m status: direction=2, busy=0
[00:00:00.006,000] <inf> app: Source string for transfer from Memory to Peripheral (UART_0):
[00:00:00.006,000] <inf> app:   - The quick brown fox jumps over the lazy dog
[00:00:00.006,000] <inf> app: String received from UART_0 with DMA Peripheral-to-Memory procudure:
[00:00:00.006,000] <inf> app:   - The quick brown fox jumps over the lazy dog
[00:00:00.006,000] <inf> app: DMA Data transfer OK, and channel 0 and channel 1 are now ready again for later use.
[00:00:00.006,000] <inf> app: DMA M2P2M (Memory to Peripheral to Memory) demo end.

注意:此例程执行前,请确保已经使用杜邦线将 UART0 自身的 TX (P00) 与 RX (P01) 接在一起。

5 开发者说明

  1. 本例程中使用到了 UART0 外设,但 App 代码中并没做对应的初始化配置,这是因为我们在 PAN1080 EVB 的板级目录下已经通过以下方式使能并初始化了 UART0:

    • Kconfig 中默认使能了 Zephyr UART Driver (CONFIG_SERIAL=y);

    • Devicetree 中默认初始化了 UART0 外设:

      &uart0 {
          current-speed = <115200>;
          pinctrl-0 = <&p0_0_uart0_tx &p0_1_uart0_rx>;
          status = "okay";
      };
      
  2. 在涉及到 Peripheral 的 DMA 配置过程中,需要注意某些外设在使用 DMA 之前,要先开启自己模块内部的 DMA 传输使能开关;而另一些外设可能无需做此操作。对于 PAN1080 SoC 来说:

    • 需要另外配置方可使用 DMA 的外设有:SPI、I2C、ADC

    • 无需额外配置即可使用 DMA 的外设有:UART、I2S

  3. 更多说明请参考 Driver: DMA M2M 中的相关说明。