kinetis的UART串口(DMA模式)
前面的例子中,串口的收發(fā)采用中斷模式,雖然在一定程度上解放了CPU,但每個(gè)字節(jié)都要中斷一次,在115200波特率下,約8.7uS就要中斷一次,CPU仍然很累。直接存儲(chǔ)器訪問(wèn)(DMA)方式可以進(jìn)一步解放CPU,本例采用DAM方式實(shí)現(xiàn)每次100字節(jié)數(shù)據(jù)發(fā)送與接收。DMA處理發(fā)送是最有效的方法,因?yàn)槌绦蛎鞔_知道有多少數(shù)據(jù)要發(fā)送,直接將數(shù)據(jù)存放數(shù)組的首地址和長(zhǎng)度交給DMA即可由DAM連續(xù)發(fā)完這些數(shù)據(jù),如果需要可以設(shè)置讓DMA發(fā)完后產(chǎn)生中斷。對(duì)于接收,用DMA的問(wèn)題在于不知道接收多少個(gè)數(shù),無(wú)法在收到數(shù)據(jù)后通知CPU。一般采用這樣的做法:用DMA收下所有數(shù)據(jù)放到環(huán)形緩沖區(qū)里,但不產(chǎn)生中斷。這樣雖不能通知CPU何時(shí)收到了數(shù)據(jù),但確可以收下所有數(shù)據(jù)。每隔一段時(shí)間CPU查詢?cè)摼彌_區(qū),發(fā)現(xiàn)有數(shù)據(jù)就處理。這樣雖響應(yīng)的及時(shí)性差些,但一般場(chǎng)合都是可以接受的。
本文引用地址:http://www.ex-cimer.com/article/148270.htm要使用UART的DMA方式,需做下面3件事情:
1、UART5_C2寄存器的發(fā)送、接收中斷使能,接收使能。
2、UART5_C5寄存器的DMA收和DMA發(fā)使能。
3、設(shè)置DMAMUX,將相應(yīng)請(qǐng)求源(中斷源)映射到相應(yīng)DMA通道,并使能相應(yīng)通道。請(qǐng)求源編號(hào)見(jiàn)表3-24。
4、設(shè)置DMA控制器,主要是TCD的設(shè)置,包括源、目的地址、傳輸長(zhǎng)度、地址遞增等。
5、如果需要DMA傳輸完成產(chǎn)生中斷,則要NVICISER寄存器使能DMA對(duì)應(yīng)中斷,中斷向量表填入中斷服務(wù)程序入口。
6、想發(fā)數(shù)據(jù)的時(shí)候設(shè)置UART5_C2的發(fā)送使能,會(huì)立即因發(fā)送數(shù)據(jù)寄存器空而產(chǎn)生DMA請(qǐng)求。
示例代碼用通道0處理發(fā)送,完成后產(chǎn)生中斷,中斷服務(wù)程序會(huì)再啟動(dòng)發(fā)送;通道1處理數(shù)據(jù)接收,不產(chǎn)生中斷。因使用了回環(huán),發(fā)送的數(shù)據(jù)都被自身接收到了,可以看出發(fā)送、接收的過(guò)程沒(méi)有CPU的干預(yù),發(fā)送完100字節(jié)(實(shí)際可以很長(zhǎng))才產(chǎn)生一次中斷,在此期間MCU可以做各種事情。
下面是完整代碼:
/*
* main implementation: use this 'C' sample to create your own application
*
*/
#define GPIO_PIN_MASK 0x3C000000
#define GPIO_PIN(x) ((1
#include
#include derivative.h /* include peripheral declarations */
struct _uart_buf
{
int index;
char buf[100];
} uart_tx,uart_rx;
void MCG_Init()
{
SIM_SCGC6 |= 0x20000000; //SIM_SCGC6: RTC=1
if ((RTC_CR RTC_CR_OSCE_MASK) == 0u)//Only if the OSCILLATOR is not already enabled
{
RTC_CR = ~0x3C00; //RTC_CR: SC2P=0,SC4P=0,SC8P=0,SC16P=0
RTC_CR |= 0x0100; //RTC_CR: OSCE=1
RTC_CR = ~0x0200; //RTC_CR: CLKO=0
}
/* System clock initialization */
/* SIM_CLKDIV1: OUTDIV1=0,OUTDIV2=1,OUTDIV3=1,OUTDIV4=3,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0 */
SIM_CLKDIV1 = (uint32_t)0x01130000UL; /* Update system prescalers */
/* SIM_SOPT2: PLLFLLSEL=0 */
SIM_SOPT2 = (uint32_t)~0x00010000UL; /* Select FLL as a clock source for various peripherals */
/* SIM_SOPT1: OSC32KSEL=0 */
SIM_SOPT1 = (uint32_t)~0x00080000UL; /* System oscillator drives 32 kHz clock for various peripherals */
/* Switch to FEE Mode */
SIM_SOPT2 |= (uint32_t)0x01UL;// SIM_SOPT2: MCGCLKSEL=1 0-System oscillator (OSCCLK), 1-32 kHz RTC oscillator
MCG_C2 = (uint8_t)0x00U; // MCG_C2: ??=0,??=0,RANGE=0,HGO=0,EREFS=0,LP=0,IRCS=0
MCG_C1 = (uint8_t)0x02U; // MCG_C1: CLKS=0,FRDIV=0,IREFS=0,IRCLKEN=1,IREFSTEN=0
MCG_C4 |= 0xE0; //MCG_C4: DMX32=1,DRST_DRS=3
MCG_C5 = 0x00; // MCG_C5: ??=0,PLLCLKEN=0,PLLSTEN=0,PRDIV=0
MCG_C6 = 0x00;// MCG_C6: LOLIE=0,PLLS=0,CME=0,VDIV=0
while((MCG_S MCG_S_IREFST_MASK) != 0x00U) //Check that the source of the FLL reference clock is the external reference clock.
{
}
while((MCG_S 0x0CU) != 0x00U) // Wait until output of the FLL is selected
{
}
}
void UART_Init()
{
// SIM_SCGC1: UART5=1
SIM_SCGC1 |= (uint32_t)0x0800UL;
// SIM_SCGC5: PORTE=1
SIM_SCGC5 |= (uint32_t)0x2000UL;
// PORTE_PCR9: ISF=0,MUX=3 做UART
PORTE_PCR9 = (uint32_t)((PORTE_PCR9 (uint32_t)~0x01000400UL) | (uint32_t)0x0300UL);
// PORTE_PCR8: ISF=0,MUX=3 做UART
PORTE_PCR8 = (uint32_t)((PORTE_PCR8 (uint32_t)~0x01000400UL) | (uint32_t)0x0300UL);
UART5_C4 = 0x14; //波特率微調(diào)
UART5_BDH = (312>>8) 0x1F;//設(shè)波特率9600bps
UART5_BDL = 3120xFF;
UART5_C2 = (17)|(15)|(12);//允許收、發(fā)中斷,允許接收
UART5_C5 = (17)|(15);//允許收、發(fā)中斷產(chǎn)生DMA請(qǐng)求
UART5_C1 |= 17;//使用回環(huán)模式
}
void dma0_init()
{
SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK;
DMAMUX_CHCFG0 = (17) | 13;
SIM_SCGC7 |= SIM_SCGC7_DMA_MASK;
DMA_CR = 0;
DMA_TCD0_SADDR = (unsigned long)uart_tx.buf[0];//DMA源地址
DMA_TCD0_DADDR = (unsigned long)UART5_D;//DMA目的地址
DMA_TCD0_NBYTES_MLNO = 1;
DMA_TCD0_ATTR = 0;//8位傳送,關(guān)閉模特性
評(píng)論