STM32F10x 學(xué)習(xí)筆記8(USART實(shí)現(xiàn)串口通訊 DMA 方式)
在普通的8位或16位單片機(jī)中很少有包含DMA控制器的,所以可能許多嵌入式程序員對(duì)DMA方式并不熟悉。簡單的說,直接存儲(chǔ)器存取(DMA)用來提供在外設(shè)和存儲(chǔ)器之間或者存儲(chǔ)器和存儲(chǔ)器之間的高速數(shù)據(jù)傳輸。由于無須CPU干預(yù),數(shù)據(jù)可以通過DMA快速地移動(dòng),這就節(jié)省了CPU的資源來做其他操作。
STM32F10x上具有兩個(gè)DMA控制器,共有12個(gè)通道(DMA1有7個(gè)通道,DMA2有5個(gè)通道),每個(gè)通道專門用來管理來自于一個(gè)或多個(gè)外設(shè)對(duì)存儲(chǔ)器訪問的請(qǐng)求。還有一個(gè)仲裁器來協(xié)調(diào)各個(gè)DMA請(qǐng)求的優(yōu)先權(quán)。
按照STM32參考手冊(cè)上的說法:“DMA控制器和Cortex™-M3核心共享系統(tǒng)數(shù)據(jù)總線,執(zhí)行直接存儲(chǔ)器數(shù)據(jù)傳輸。當(dāng)CPU和DMA同時(shí)訪問相同的目標(biāo)(RAM或外設(shè))時(shí),DMA請(qǐng)求會(huì)暫停CPU訪問系統(tǒng)總線達(dá)若干個(gè)周期,總線仲裁器執(zhí)行循環(huán)調(diào)度,以保證CPU至少可以得到一半的系統(tǒng)總線(存儲(chǔ)器或外設(shè))帶寬。”所以我們不必?fù)?dān)心DMA控制器霸占總線資源。CPU總是可以得到一般的總線時(shí)間的。
下面我們以USART2的數(shù)據(jù)發(fā)送為例來介紹DMA。首先由STM32參考手冊(cè)的圖22可知。USART2的發(fā)送功能可以使用DMA1的第7個(gè)通道。
利用的DMA傳輸?shù)脑O(shè)置工作大體可以分為6步:
1.在DMA1_CPAR7寄存器中設(shè)置外設(shè)寄存器的地址。發(fā)生外設(shè)數(shù)據(jù)傳輸請(qǐng)求時(shí),這個(gè)地址將是數(shù)據(jù)傳輸?shù)脑椿蚰繕?biāo)。
2.在DMA1_CMAR7寄存器中設(shè)置數(shù)據(jù)存儲(chǔ)器的地址。發(fā)生外設(shè)數(shù)據(jù)傳輸請(qǐng)求時(shí),傳輸?shù)臄?shù)據(jù)將從這個(gè)地址讀出或?qū)懭脒@個(gè)地址。
3.在DMA1_CNDTR7寄存器中設(shè)置要傳輸?shù)臄?shù)據(jù)量。在每個(gè)數(shù)據(jù)傳輸后,這個(gè)數(shù)值遞減。
4.在DMA1_CCR7寄存器的PL[1:0]位中設(shè)置通道的優(yōu)先級(jí)。
5.在DMA1_CCR7寄存器中設(shè)置數(shù)據(jù)傳輸?shù)姆较?、循環(huán)模式、外設(shè)和存儲(chǔ)器的增量模式、外設(shè)和存儲(chǔ)器的數(shù)據(jù)寬度、傳輸一半產(chǎn)生中斷或傳輸完成產(chǎn)生中斷。
6.設(shè)置DMA1_CCR7寄存器的ENABLE位,啟動(dòng)該通道。
第1步對(duì)應(yīng)的代碼為:
- DMA1_Channel7->CPAR=(uint32_t)&(USART2->DR);
第2步對(duì)應(yīng)的代碼如下,其中p_str是一個(gè)指針,指向要傳輸?shù)臄?shù)據(jù)的首地址:
- DMA1_Channel7->CMAR=(uint32_t)p_str;
- DMA1_Channel7->CNDTR=cnt;
- DMA1_Channel7->CCR|=DMA_Priority_Low;
- DMA1_Channel7->CCR|=DMA_DIR_PeripheralDST|
- DMA_Mode_Normal|
- DMA_PeripheralInc_Disable|
- DMA_MemoryInc_Enable|
- DMA_PeripheralDataSize_Byte|
- DMA_MemoryDataSize_Byte|
- DMA_M2M_Disable;
- DMA1_Channel7->CCR|=DMA_CCR1_EN;
實(shí)際上在這6步之前應(yīng)該還有2步操作。首先設(shè)置DMA之前,要打開DMA的時(shí)鐘:
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
- USARTx->CR3|=USART_DMAReq_Tx;
或者用下面的函數(shù):
- USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);
一旦啟動(dòng)了DMA通道,它既可響應(yīng)連到該通道上的外設(shè)的DMA請(qǐng)求。完成一次DMA傳輸后如何開啟下一次傳輸呢,這個(gè)問題困擾了我好幾天,最后在STM32參考手冊(cè)上發(fā)現(xiàn)如下的一句話:
當(dāng)通道配置為非循環(huán)模式時(shí),傳輸結(jié)束后(即傳輸計(jì)數(shù)變?yōu)?)將不再產(chǎn)生DMA操作。要開始新的DMA傳輸,需要在關(guān)閉DMA通道的情況下,在DMA_CNDTRx寄存器中重新寫入傳輸數(shù)目。
下面先給一個(gè)簡單的示例程序:
- voidUSART2_Init(void)
- {
- GPIO_InitTypeDefGPIO_InitStructure;
- USART_InitTypeDefUSART_InitStructure;
- NVIC_InitTypeDefNVIC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);
- /*ConfigureUSARTTxasalternatefunctionpush-pull*/
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOD,&GPIO_InitStructure);
- /*ConfigureUSARTRxasinputfloating*/
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
- GPIO_Init(GPIOD,&GPIO_InitStructure);
- GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
- 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_Rx|USART_Mode_Tx;
- USART_Init(USART2,&USART_InitStructure);
- USART_Cmd(USART2,ENABLE);
- }
- voidUART2_TX_DMA_Init(uint8_t*p_str,uint16_tcnt)
- {
- //DMA_InitTypeDefDMA_InitStructure;
- //DMA_InitStructure.DMA_PeripheralBaseAddr=(uint32_t)&(USART2->DR);
- //DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)str;
- //DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST;
- //DMA_InitStructure.DMA_BufferSize=14;
- //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_Low;
- //DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
- //DMA_Init(DMA1_Channel7,&DMA_InitStructure);
- DMA1_Channel7->CPAR=(uint32_t)&(USART2->DR);
- DMA1_Channel7->CMAR=(uint32_t)p_str;
- DMA1_Channel7->CNDTR=cnt;
- DMA1_Channel7->CCR=DMA_DIR_PeripheralDST|DMA_Priority_Low|
- DMA_Mode_Normal|DMA_PeripheralInc_Disable|
- DMA_MemoryInc_Enable|DMA_PeripheralDataSize_Byte|
- DMA_MemoryDataSize_Byte|DMA_M2M_Disable;
- }
- uint8_tstr[]="HelloWorld!!!";
- voidTaskStart(void*pdata)
- {
- SysTick_Config(SystemCoreClock/10);
- USART2_Init();
- UART2_TX_DMA_Init(str,14);
- for(;;)
- {
- LED_Spark();
- //DMA_Cmd(DMA1_Channel7,DISABLE);
- DMA1_Channel7->CCR&=(uint16_t)(~DMA_CCR1_EN);
- //DMA_Init(DMA1_Channel7,&DMA_InitStructure);
- DMA1_Channel7->CNDTR=14;
- //DMA_Cmd(DMA1_Channel7,ENABLE);
- DMA1_Channel7->CCR|=DMA_CCR1_EN;
- //USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);
- OSTimeDly(10);
- }
- }
- intmain(void)
- {
- SystemInit();
- LED_Init();
- OSInit();
- OSTaskCreate(TaskStart,(void*)0,&(TaskStartStk[TASK_STK_SIZE-1]),1);
- OSStart();
- for(;;)
- {
- }
- }
下面再說說如何在DMA傳輸完成之后產(chǎn)生中斷。當(dāng)傳輸一半的數(shù)據(jù)后,半傳輸標(biāo)志(HTIF)被置1,當(dāng)設(shè)置了允許半傳輸中斷位(HTIE)時(shí),將產(chǎn)生一個(gè)中斷請(qǐng)求。在數(shù)據(jù)傳輸結(jié)束后,傳輸完成標(biāo)志(TCIF)被置1,當(dāng)設(shè)置了允許傳輸完成中斷位(TCIE)時(shí),將產(chǎn)生一個(gè)中斷請(qǐng)求。
DMA的CCR寄存器中有1位TCIE(Transfercompleteinterruptenable)
該位由軟件設(shè)置和清除。
0:禁止TC中斷
1:允許TC中斷
所以為了使用DMA中斷,我們需要下面的代碼:
- DMA1_Channel7->CCR|=DMA_IT_TC;//Transfercompleteinterruptenable
- NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel7_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=5;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
- NVIC_Init(&NVIC_InitStructure);
下面還是給個(gè)簡單的示例程序,示例程序中還用到了uCOS的信號(hào)量,中斷處理函數(shù)通過信號(hào)量通知Task完成了DMA傳輸:
- #include"stm32f10x.h"
- #include"uart.h"
- #include"led.h"
- #include"COMMRTOS.H"
- #include"ucos_ii.h"
- #defineTASK_STK_SIZE128
- OS_STKTaskStartStk[TASK_STK_SIZE];
- voidUSART2_Init(void)
- {
- GPIO_InitTypeDefGPIO_InitStructure;
- USART_InitTypeDefUSART_InitStructure;
- NVIC_InitTypeDefNVIC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);
- /*ConfigureUSARTTxasalternatefunctionpush-pull*/
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOD,&GPIO_InitStructure);
- /*ConfigureUSARTRxasinputfloating*/
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
- GPIO_Init(GPIOD,&GPIO_InitStructure);
- GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
- 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_Rx|USART_Mode_Tx;
- USART_Init(USART2,&USART_InitStructure);
- USART_Cmd(USART2,ENABLE);
- NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- voidUART2_TX_DMA_Init(uint8_t*p_str,uint16_tcnt)
- {
- //DMA_InitTypeDefDMA_InitStructure;
- //DMA_InitStructure.DMA_PeripheralBaseAddr=(uint32_t)&(USART2->DR);
- //DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)str;
- //DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST;
- //DMA_InitStructure.DMA_BufferSize=14;
- //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_Low;
- //DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
- //DMA_Init(DMA1_Channel7,&DMA_InitStructure);
- DMA1_Channel7->CPAR=(uint32_t)&(USART2->DR);
- DMA1_Channel7->CMAR=(uint32_t)p_str;
- DMA1_Channel7->CNDTR=cnt;
- DMA1_Channel7->CCR=DMA_DIR_PeripheralDST|DMA_Priority_Low|
- DMA_Mode_Normal|DMA_PeripheralInc_Disable|
- DMA_MemoryInc_Enable|DMA_PeripheralDataSize_Byte|
- DMA_MemoryDataSize_Byte|DMA_M2M_Disable;
- }
- OS_EVENT*UART2_DMA_TX_Sem;
- uint8_tstr[]="HelloWorld!!!";
- voidTaskStart(void*pdata)
- {
- unsignedcharerr;
- SysTick_Config(SystemCoreClock/10);
- USART2_Init();
- USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);
- UART2_TX_DMA_Init(str);
- UART2_DMA_TX_Sem=OSSemCreate(1);
- for(;;)
- {
- LED_Spark();
- OSSemPend(UART2_DMA_TX_Sem,0,&err);
- //DMA_Cmd(DMA1_Channel7,DISABLE);
- DMA1_Channel7->CCR&=(uint16_t)(~DMA_CCR1_EN);
- //DMA_Init(DMA1_Channel7,&DMA_InitStructure);
- DMA1_Channel7->CNDTR=14;
- //DMA_Cmd(DMA1_Channel7,ENABLE);
- DMA1_Channel7->CCR|=DMA_CCR1_EN;
- //USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);
- OSTimeDly(10);
- }
- }
- intmain(void)
- {
- SystemInit();
- LED_Init();
- OSInit();
- OSTaskCreate(TaskStart,(void*)0,&(TaskStartStk[TASK_STK_SIZE-1]),1);
- OSStart();
- for(;;)
- {
- }
- }
- voidDMA1_Channel7_IRQHandler(void)
- {
- OS_CPU_SRcpu_sr;
- OS_ENTER_CRITICAL();/*TelluC/OS-IIthatwearestartinganISR*/
- OSIntNesting++;
- OS_EXIT_CRITICAL();
- OSSemPost(UART2_DMA_TX_Sem);
- //UART_PutChar(USART2,+);
- DMA1->IFCR=DMA1_FLAG_TC7;
- OSIntExit();
- }
評(píng)論