前面的例子中,串口的收發(fā)采用中斷模式,雖然在一定程度上解放了CPU,但每個字節(jié)都要中斷一次,在115200波特率下,約8.7uS就要中斷一次,CPU仍然很累。直接存儲器訪問(DMA)方式可以進一步解放CPU,本例采用DAM方式實現(xiàn)每次100字節(jié)數(shù)據(jù)發(fā)送與接收。DMA處理發(fā)送是最有效的方法,因為程序明確知道有多少數(shù)據(jù)要發(fā)送,直接將數(shù)據(jù)存放數(shù)組的首地址和長度交給DMA即可由DAM連續(xù)發(fā)完這些數(shù)據(jù),如果需要可以設(shè)置讓DMA發(fā)完后產(chǎn)生中斷。對于接收,用DMA的問題在于不知道接收多少個數(shù),無法在收到數(shù)據(jù)后通知CPU。一般采用這樣的做法:用DMA收下所有數(shù)據(jù)放到環(huán)形緩沖區(qū)里,但不產(chǎn)生中斷。這樣雖不能通知CPU何時收到了數(shù)據(jù),但確可以收下所有數(shù)據(jù)。每隔一段時間CPU查詢該緩沖區(qū),發(fā)現(xiàn)有數(shù)據(jù)就處理。這樣雖響應(yīng)的及時性差些,但一般場合都是可以接受的。
要使用UART的DMA方式,需做下面3件事情:
1、UART5_C2寄存器的發(fā)送、接收中斷使能,接收使能。
2、UART5_C5寄存器的DMA收和DMA發(fā)使能。
3、設(shè)置DMAMUX,將相應(yīng)請求源(中斷源)映射到相應(yīng)DMA通道,并使能相應(yīng)通道。請求源編號見表3-24。
4、設(shè)置DMA控制器,主要是TCD的設(shè)置,包括源、目的地址、傳輸長度、地址遞增等。
5、如果需要DMA傳輸完成產(chǎn)生中斷,則要NVICISER寄存器使能DMA對應(yīng)中斷,中斷向量表填入中斷服務(wù)程序入口。
6、想發(fā)數(shù)據(jù)的時候設(shè)置UART5_C2的發(fā)送使能,會立即因發(fā)送數(shù)據(jù)寄存器空而產(chǎn)生DMA請求。
示例代碼用通道0處理發(fā)送,完成后產(chǎn)生中斷,中斷服務(wù)程序會再啟動發(fā)送;通道1處理數(shù)據(jù)接收,不產(chǎn)生中斷。因使用了回環(huán),發(fā)送的數(shù)據(jù)都被自身接收到了,可以看出發(fā)送、接收的過程沒有CPU的干預(yù),發(fā)送完100字節(jié)(實際可以很長)才產(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 kHzclock 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 = 312&0xFF;
UART5_C2 = (1<<7)|(1<<5)|(1<<2);//允許收、發(fā)中斷,允許接收
UART5_C5 = (1<<7)|(1<<5);//允許收、發(fā)中斷產(chǎn)生DMA請求
UART5_C1 |= 1<<7;//使用回環(huán)模式
}
void dma0_init()
{
SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK;
DMAMUX_CHCFG0 = (1<<7) | 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)閉模特性
DMA_TCD0_SOFF = 1;//每次操作完源地址,源地址增加1
DMA_TCD0_DOFF = 0;//每次操作完目標地址,目標地址不增加
DMA_TCD0_SLAST = 0;//DMA完成一次輸出之后即major_loop衰減完之后不更改源地址
DMA_TCD0_DLASTSGA = 0;//DMA完成一次輸出之后即major_loop衰減完之后不更改目標地址
DMA_TCD0_CITER_ELINKNO = 100;
DMA_TCD0_BITER_ELINKNO = 100;
DMA_TCD0_CSR = 0;
DMA_TCD0_CSR |= DMA_CSR_INTMAJOR_MASK;
DMA_TCD0_CSR |= DMA_CSR_DREQ_MASK;
NVICISER0 |= 1<<0;//;//使能中斷NVICISERn=1<
DMA_ERQ |= (1 << 0);//啟動
}
void dma1_init()
{
//SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK;
DMAMUX_CHCFG1 = (1<<7) | 12;
//SIM_SCGC7 |= SIM_SCGC7_DMA_MASK;
//DMA_CR = 0;
DMA_TCD1_SADDR = (unsigned long)&UART5_D;//DMA源地址
DMA_TCD1_DADDR = (unsigned long)&uart_rx.buf[0];//DMA目的地址
DMA_TCD1_NBYTES_MLNO = 1;
DMA_TCD1_ATTR = 0;//8位傳送
DMA_TCD1_SOFF = 0;//每次操作完源地址,源地址不增加
DMA_TCD1_DOFF = 1;//每次操作完目標地址,目標地址增加1
DMA_TCD1_SLAST = 0;//DMA完成一次輸出之后即major_loop衰減完之后不更改源地址
DMA_TCD1_DLASTSGA = 0;//DMA完成一次輸出之后即major_loop衰減完之后不更改目標地址
DMA_TCD1_CITER_ELINKNO = 100;
DMA_TCD1_BITER_ELINKNO = 100;
DMA_TCD1_CSR = 0;
DMA_TCD1_CSR &= ~DMA_CSR_INTMAJOR_MASK;
DMA_TCD1_CSR |= DMA_CSR_DREQ_MASK;
DMA_ERQ |= (1 << 1);//啟動
}
int main(void)
{
int i;
MCG_Init();
dma0_init();
dma1_init();
UART_Init();
for(i=0;i<100;i++)
{
uart_tx.buf[i] = i;
uart_rx.buf[i] = 0;
}
uart_tx.index = 1;
uart_rx.index = 0;
printf("Hello (Kinetis) World in 'C' from MK60DX256Z derivative! nr");
UART5_C2 |= 1<<3;
for(;;)
{
}
return 0;
}
void dam0_isr(void)
{
static unsigned char cnt=0;
DMA_INT = 0x1; // clear dma int flag
cnt++;
MEMSet(uart_tx.buf,cnt,100);
DMA_TCD0_SADDR = (unsigned long)&uart_tx.buf[0];//DMA源地址
DMA_ERQ |= (1 << 0);//啟動
//與UART接收對應(yīng)的DMA1未使用中斷,在這里也同時對其重設(shè)目的地址并啟動
DMA_TCD1_DADDR = (unsigned long)&uart_rx.buf[0];
DMA_ERQ |= (1 << 1);//啟動
}
將“kinetis_sysinit.c”的“__vect_table”中16號中斷“(tIsrFunc)UNASSIGNED_ISR”換成“(tIsrFunc)dam0_isr”