|
本帖最后由 niuniu 于 2015-6-9 03:00 編輯
在做實驗之前,首先必須明白什么是DMA,DMA的作用又體現在哪里。
DMA,即直接內存存儲,在一些數據的傳輸中,采用DMA方式,從而將CPU解放出來。讓CPU有足夠的時間處理其他的事情。
stm32使用DMA的相關操作:
1、DMA的配置
要配置的有DMA傳輸通道選擇,傳輸的成員和方向、普通模式還是循環模式等等。
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
//DMA設置:
//設置DMA源:內存地址&串口數據寄存器地址
//方向:內存-->外設
//每次傳輸位:8bit
//傳輸大小DMA_BufferSize=SENDBUFF_SIZE
//地址自增模式:外設地址不增,內存地址自增1
//DMA模式:一次傳輸,非循環
//優先級:中
DMA_DeInit(DMA1_Channel4);//串口1的DMA傳輸通道是通道4
DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//外設作為DMA的目的端
DMA_InitStructure.DMA_BufferSize = SENDBUFF_SIZE;//傳輸大小
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外設地址不增加
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//內存地址自增1
DMA_InitStructure.DMA_PeripheralDataSize =DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize =DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
//DMA_Mode_Normal(只傳送一次), DMA_Mode_Circular(不停地傳送)
DMA_InitStructure.DMA_Priority =DMA_Priority_Medium;//(DMA傳送優先級為中等)
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
}
注:
1、傳輸通道:通過查表,串口1的發送對應的是DMA的通道4,所以此處選擇通道4.
2、DMA傳輸方式:
(1)DMA_Mode_Normal,正常模式,當一次DMA數據傳輸完后,停止DMA傳送,對于上例而言,就是DMA_PeripheralDataSize_Byte個字節的傳送完成后,就停止傳送。
(2) DMA_Mode_Circular
循環模式,當傳輸完一次后,重新接著傳送,永不停息。
2、外設的DMA方式設置
將串口1設置成DMA模式:
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
3、待傳輸數據的定義和初始化
#define SENDBUFF_SIZE 10240
vu8 SendBuff[SENDBUFF_SIZE];
for(i=0;i<SENDBUFF_SIZE;i++)
{
SendBuff[ i] = i+'0';
}
4、開始DMA傳輸(使能對應的DMA通道)
DMA_Cmd(DMA1_Channel4,ENABLE);
5、DMA傳輸的完成
while(DMA_GetFlagStatus(DMA1_FLAG_TC4) ==RESET)
{
LED_1_REV; //LED翻轉
Delay(); //浪費時間
}
當傳輸完成后,就會跳出上面的死循環。
STM32筆記(四)DMA、USART的演示
該連載作者九九的博客
| | [url=http://gg.eefocus.com/www/delivery/ck.php?oaparams=2__bannerid=490__zoneid=168__cb=f8d92504ab__oadest=http://ad-apac.doubleclick.net/jump/N4481.eefocus/B4562968.7;sz=150x300;ord=[timestamp]?][/url]
原帖由私奔于2009-01-04 14:30發表:
這里有個小小的例子,來演示DMA模塊與系統程序并行工作。
用串口以低波特率發送一個10K的數據,花費近10s時間,此時按照以往方法,CPU要不斷等待數據發送、送數據;或者送數據、進中斷、送數據,處理起來比較消耗時間。
使用了DMA功能以后,用戶程序中只需配置好DMA,開啟傳輸后,再也不需要操心,10K數據完成后會有標志位或中斷產生,期間可以做任何想做的事,非常方便。
這個是相應的代碼例子,基于STM32F103VBT6
#include"stm32f10x_lib.h"
#include "stdio.h"
#defineUSART1_DR_Base 0x40013804
#defineSENDBUFF_SIZE 10240
vu8 SendBuff[SENDBUFF_SIZE];
vu8 RecvBuff[10];
vu8 recv_ptr;
voidRCC_Configuration(void);
voidGPIO_Configuration(void);
voidNVIC_Configuration(void);
voidDMA_Configuration(void);
voidUSART1_Configuration(void);
intfputc(intch, FILE*f);
voidDelay(void);
intmain(void)
{
u16 i;
#ifdef DEBUG
debug();
#endif
recv_ptr =0;
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
DMA_Configuration();
USART1_Configuration();
printf("\r\nSystemStart...\r\n");
printf("Initialling SendBuff...\r\n");
for(i=0;i {
SendBuff[ i] = i&0xff;
}
printf("Initialsuccess!\r\nWaiting for transmission...\r\n");
//發送去數據已經準備好,按下按鍵即開始傳輸
while(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_3));
printf("Start DMAtransmission!\r\n");
//這里是開始DMA傳輸前的一些準備工作,將USART1模塊設置成DMA方式工作
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
//開始一次DMA傳輸!
DMA_Cmd(DMA1_Channel4, ENABLE);
//等待DMA傳輸完成,此時我們來做另外一些事,點燈
//實際應用中,傳輸數據期間,可以執行另外的任務
while(DMA_GetFlagStatus(DMA1_FLAG_TC4)== RESET)
{
LED_1_REV; //LED翻轉
Delay(); //浪費時間
}
//DMA傳輸結束后,自動關閉了DMA通道,而無需手動關閉
//下面的語句被注釋
//DMA_Cmd(DMA1_Channel4,DISABLE);
printf("\r\nDMAtransmission successful!\r\n");
while (1)
{
}
}
intfputc(intch, FILE*f)
{
//USART_SendData(USART1, (u8)ch);
USART1->DR = (u8) ch;
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET)
{
}
return ch;
}
voidDelay(void)
{
u32 i;
for(i=0;i<0xF0000;i++);
return;
}
voidRCC_Configuration(void)
{
ErrorStatusHSEStartUpStatus;
//使能外部晶振
RCC_HSEConfig(RCC_HSE_ON);
//等待外部晶振穩定
HSEStartUpStatus = RCC_WaitForHSEStartUp();
//如果外部晶振啟動成功,則進行下一步操作
if(HSEStartUpStatus==SUCCESS)
{
//設置HCLK(AHB時鐘)=SYSCLK
RCC_HCLKConfig(RCC_SYSCLK_Div1);
//PCLK1(APB1) =HCLK/2
RCC_PCLK1Config(RCC_HCLK_Div2);
//PCLK2(APB2) =HCLK
RCC_PCLK2Config(RCC_HCLK_Div1);
//FLASH時序控制
//推薦值:SYSCLK =0~24MHz Latency=0
// SYSCLK = 24~48MHz Latency=1
// SYSCLK = 48~72MHz Latency=2
FLASH_SetLatency(FLASH_Latency_2);
//開啟FLASH預取指功能
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
//PLL設置 SYSCLK/1 *9 = 8*1*9 = 72MHz
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
//啟動PLL
RCC_PLLCmd(ENABLE);
//等待PLL穩定
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)== RESET);
//系統時鐘SYSCLK來自PLL輸出
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//切換時鐘后等待系統時鐘穩定
while(RCC_GetSYSCLKSource()!=0x08);
}
//下面是給各模塊開啟時鐘
//啟動GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB| \
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD,\
ENABLE);
//啟動AFIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//啟動USART1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//啟動DMA時鐘
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}
voidGPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//PC口4567腳設置GPIO輸出,推挽2M
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6| GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//KEY2 KEY3JOYKEY
//位于PD口的3 411-15腳,使能設置為輸入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_11| GPIO_Pin_12 |\
GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
//USART1_TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1_RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
voidNVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
// Set the Vector Table baselocation at 0x20000000
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
// Set the Vector Table baselocation at 0x08000000
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
//設置NVIC優先級分組為Group2:0-3搶占式優先級,0-3的響應式優先級
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//串口接收中斷打開
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
voidUSART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx |USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
voidDMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
//DMA設置:
//設置DMA源:內存地址&串口數據寄存器地址
//方向:內存-->外設
//每次傳輸位:8bit
//傳輸大小DMA_BufferSize=SENDBUFF_SIZE
//地址自增模式:外設地址不增,內存地址自增1
//DMA模式:一次傳輸,非循環
//優先級:中
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = SENDBUFF_SIZE;
DMA_InitStructure.DMA_PeripheralInc =DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize =DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize =DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
}
|
|
|