STM32之DMA實例


DMA簡介:

  DMA(Direct Memory Access,直接存儲器存取),是一種可以減輕CPU工作量的數據存取方式,如今被廣泛的使用。它在傳輸數據的同時,CPU可以做其他事,比如數據運算或者響應中斷等,DMA就給CPU分擔了不少的工作量!

DMA工作分析:

                                                                  

如圖,我們可以看到STM32內核,存儲器,外設及DMA的連接,這些硬件最終通過各種各樣的線連接到總線矩陣中,硬件結構之間的數據轉移都經過總線矩陣的協調,使各個外設和諧的使用總線來傳輸數據。

下面看有與沒有DMA的情況下,ADC采集的數據是怎樣存放到SRAM中的?

1.如果沒有DMA,CPU傳輸數據還要以內核作為中轉站,比如要將ADC采集的數據轉移到到SRAM中,這個過程是這樣的:內核通過DCode經過總線矩陣協調,使用AHB把外設ADC采集的數據,然后內核,DCode再通過總線矩陣協調把數據存放到內存SRAM中。

2.有DMA的話,DMA控制器的DMA總線與總線矩陣協調,使用AHB把外設ADC采集的數據經由DMA通道存放到SRAM中,這個數據的傳輸過程中,完全不需要內核的參與,也就是CPU的參與,不過DMA傳輸時要對DMA外設發出請求,才會觸發其工作。

下面我是通過串口通信的例子來學習DMA的!

主函數main.c:

 

  1. #include <stdio.h>
  2. uint8_t sendbuff[500];
  3. uint16_t i;
  4. int main()
  5. {
  6. printf_init();
  7. dma_init();
  8. for(i=0;i<500;i++)
  9. {
  10. sendbuff[i] = 0xaf;
  11. }
  12. USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
  13. LED3_ON;
  14.  
  15. while(1);

這個主函數實現的功能是利用DMA把數據(數組,數組里面的存放了500個AF字符)從內存轉移到外設(串口),最后通過串口傳輸到我們的PC上顯示。為了證明DMA在搬運數據的同時,CPU還可以做其他事,於是將LED3點亮,來測試一下。主函數至於為啥加while(1),才會產生中斷?還不明白。。。

DMA配置dma.c:

 

  1. #include "stm32f10x.h"
  2. #include "stm32f10x_rcc.h"
  3. #include "stm32f10x_usart.h"
  4. #include "stm32f10x_dma.h"
  5. #include "misc.h"
  6. #include "dma.h"
  7. void dma_init()
  8. {
  9.  
  10. DMA_InitTypeDef DMA_InitStructure;
  11. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
  12. NVIC_Config();
  13. /*DMA配置*/
  14. DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base; //串口數據寄存器地址
  15. DMA_InitStructure.DMA_MemoryBaseAddr = ( uint32_t)sendbuff; //內存地址(要傳輸的變量的指針)
  16. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //方向(從內存到外設)
  17. DMA_InitStructure.DMA_BufferSize = 500; //傳輸內容的大小
  18. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外設地址不增
  19. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //內存地址自增
  20. DMA_InitStructure.DMA_PeripheralDataSize =
  21. DMA_PeripheralDataSize_Byte ; //外設數據單位
  22. DMA_InitStructure.DMA_MemoryDataSize =
  23. DMA_MemoryDataSize_Byte ; //內存數據單位
  24. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ; //DMA模式:一次傳輸,循環
  25. DMA_InitStructure.DMA_Priority = DMA_Priority_Medium ; //優先級:高
  26. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //禁止內存到內存的傳輸
  27.  
  28. DMA_Init(DMA1_Channel4, &DMA_InitStructure); //配置DMA1的4通道
  29. DMA_Cmd(DMA1_Channel4,ENABLE);
  30. DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE); //配置DMA發送完成后產生中斷
  31.  
  32. }
  33.  
  34. static void NVIC_Config(void)
  35. {
  36. NVIC_InitTypeDef NVIC_InitStructure;
  37. /*中斷配置*/
  38. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  39.  
  40. NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
  41. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  42. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  43. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  44. NVIC_Init(&NVIC_InitStructure);
  45. }

這里的串口數據寄存器地址配置是參考STM32的datasheet來設置的,USART1_DR_Base是一個宏,#define USART1_DR_Base 0x40013804,至於地址為啥是這個,請看下圖:



存儲器映射圖,找到USART1的基址,再看USART1數據寄存器偏移地址就可以知道串口1的地址了。


至於在配置DMA1通道時,那里為啥是通道4,而不是其它通道,請看下圖:


dma.h:

 

  1. #ifndef _dma_H
  2. #define _dma_H
  3. #include "stm32f10x.h"
  4. #define USART1_DR_Base 0x40013804;
  5. extern uint8_t sendbuff[500];
  6. static void NVIC_Config(void);
  7. void dma_init(void);
  8.  
  9. #endif

串口配置:printf.c

  1. #include "printf.h"
  2. #include "stm32f10x.h"
  3. #include "stm32f10x_rcc.h"
  4. #include "stm32f10x_gpio.h"
  5. #include "stm32f10x_usart.h"
  6. #include "misc.h"
  7.  
  8. int fputc(int ch,FILE *f)
  9. {
  10. while(USART_GetFlagStatus(USART1,USART_FLAG_TC) != SET);
  11. USART_SendData(USART1,( unsigned char)ch);
  12. while(USART_GetFlagStatus(USART1,USART_FLAG_TC) != SET);
  13. return (ch);
  14. }
  15.  
  16. void printf_init(void)
  17. {
  18. GPIO_InitTypeDef GPIO_InitStructure;
  19. USART_InitTypeDef USART_InitStructure;
  20.  
  21. /*config USART clock*/
  22. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  23. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
  24. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
  25. /*USART1 GPIO config*/
  26. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_9;
  27. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF_PP; //復用推挽輸出
  28. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  29. GPIO_Init(GPIOA,&GPIO_InitStructure);
  30.  
  31. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_10;
  32. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IN_FLOATING; //復用開漏輸入
  33. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  34. GPIO_Init(GPIOA,&GPIO_InitStructure);
  35.  
  36. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
  37. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  38. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  39. GPIO_Init(GPIOD,&GPIO_InitStructure);
  40. /*USART1 mode Config*/
  41. USART_InitStructure.USART_BaudRate = 9600;
  42. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  43. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  44. USART_InitStructure.USART_Parity = USART_Parity_No;
  45. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  46. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  47. USART_Init(USART1,&USART_InitStructure);
  48. USART_Cmd(USART1,ENABLE);

printf.h:

 

  1. #ifndef __printf_H
  2. #define __printf_H
  3.  
  4. #include "stm32f10x.h"
  5. #include <stdio.h>
  6. #define LED3_ON GPIO_SetBits(GPIOD,GPIO_Pin_3)
  7. #define LED3_OFF GPIO_ResetBits(GPIOD,GPIO_Pin_3)
  8. void printf_init(void);
  9. int fputc(int ch,FILE *f);
  10.  
  11. #endif

中斷代碼:stm32f10x_it.c

  1. #include "stm32f10x_it.h"
  2. #include "stm32f10x.h"
  3. #include "printf.h"
  4. void DMA1_Channel4_IRQHandler(void)
  5. {
  6. if(DMA_GetFlagStatus(DMA1_FLAG_TC4)==SET)
  7. {
  8. LED3_OFF;
  9. DMA_ClearFlag(DMA1_FLAG_TC4);
  10. }
  11. }

效果圖:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM