garfield

飞思卡尔mcf52235 uart-dma

0
阅读(32668)

近来无事,写了一个dma与uart配合收发数据的小程序,开发板用的是自己开发的mcf52235开发板,软件开发环境cw7.0.

6483042594480.rar

#include "dma.h"
#include "mcf52235.h"

char receive[64];

void uart0_init(uint16 baudrate)

{
 uint32 div,SYS_CLOCK;
 SYS_CLOCK=60000000;
 MCF_GPIO_PUAPAR=MCF_GPIO_PUAPAR_UTXD0_UTXD0|MCF_GPIO_PUAPAR_URXD0_URXD0;

 //Reset Transmitter Receiver Mode Register

 MCF_UART0_UCR|=(0 |MCF_UART_UCR_RESET_TX
      |MCF_UART_UCR_RESET_RX
      |MCF_UART_UCR_RESET_MR);

 //No parity,8bit data

 MCF_UART0_UMR1=(0
     |MCF_UART_UMR_PM_NONE
     |MCF_UART_UMR_BC_8);
 //1bit stop

 MCF_UART0_UMR2=(0|MCF_UART_UMR_CM_NORMAL|MCF_UART_UMR_SB_STOP_BITS_1);

  //Set Rx and Tx buad by SYSTERM CLOCK

 MCF_UART0_UCSR=(0|MCF_UART_UCSR_RCS_SYS_CLK|MCF_UART_UCSR_TCS_SYS_CLK);

 //Mask all UART interrupts

 MCF_UART0_UIMR=0;

 //set buad rate

 div=(SYS_CLOCK/32/baudrate);
 MCF_UART0_UBG1=(unsigned char)(div>>8);
 MCF_UART0_UBG2=(unsigned char)(div&0x00ff);
 //Enable Tx/Rx
 MCF_UART0_UCR=(0|MCF_UART_UCR_TX_ENABLED|MCF_UART_UCR_RX_ENABLED);
}

void DMA_init(void)

{

 char *decadd=receive;
 char i;
 for(i=0;i<64;i++)
 {
  receive[i]=0;
 }
 MCF_SCM_DMAREQC=MCF_SCM_DMAREQC_DMAC0(0x8);//UART0 receive
 //source address register
 MCF_DMA0_SAR= 0x4000020C;
  //destination address register
 MCF_DMA_DAR(0)=(uint32)decadd;
 //byte count
 MCF_DMA0_BCR=64;
 MCF_DMA0_DCR=MCF_DMA_DCR_INT //interrupt enable
    |MCF_DMA_DCR_EEXT //enable external request
    |MCF_DMA_DCR_CS //force a single read/write transfer per request
    |MCF_DMA_DCR_SSIZE(MCF_DMA_DCR_SSIZE_BYTE)
    |MCF_DMA_DCR_DINC
    |MCF_DMA_DCR_DSIZE(MCF_DMA_DCR_DSIZE_BYTE);

 //UART0 read
 MCF_SCM_PACR2=MCF_SCM_PACR_ACCESS_CTRL1(5);

 //read/write
 MCF_SCM_GPACR0=MCF_SCM_GPACR_ACCESS_CTRL(6);

 //interrupt enable
 MCF_INTC0_IMRL&=~MCF_INTC_IMRL_MASKALL;
 MCF_INTC0_IMRL&=~MCF_INTC_IMRL_INT_MASK9;

 //channel 0
 MCF_INTC0_ICR09=MCF_INTC_ICR_IP(3)+MCF_INTC_ICR_IL(2);

}

void uart0_putchar(char c)

{

 //Wait until space is available in the FIFO
 while (!(MCF_UART0_USR&MCF_UART_USR_TXRDY)) { ; }

 //Send the character
 MCF_UART0_UTB = (unsigned char)c;

}

unsigned char uart0_getchar()

{

 //Wait until character has been received
 while (!(MCF_UART0_USR & MCF_UART_USR_RXRDY))
  { };

 return MCF_UART0_URB;

}

void uart0_putstr(char *str)

{

 while(*str!=0) { uart0_putchar(*str++); }

}

 

__declspec(interrupt:0) void DMA0_handler(void)//source 9

{

 uint8 int_status = MCF_DMA0_DSR;

 if(int_status & MCF_DMA_DSR_DONE)

 {

  if(int_status & MCF_DMA_DSR_CE) { uart0_putstr("configuration error\n"); }

  else if(int_status & MCF_DMA_DSR_BED) { uart0_putstr("destination bus error\n"); }

  else if(int_status & MCF_DMA_DSR_BES) { uart0_putstr("source bus error\n"); }

  else {

    uart0_putstr("dma0 transfer done\n");

    uart0_putstr(receive);

    }

  //clear DMA0 interrupt

  MCF_DMA0_DCR&=~MCF_DMA_DCR_EEXT;

  MCF_DMA0_DSR |= MCF_DMA_DSR_DONE;

  MCF_DMA_DAR(0)=(uint32)receive;

  MCF_DMA0_BCR=64;

  MCF_DMA0_DCR|=MCF_DMA_DCR_EEXT;

  }

 if(int_status & MCF_DMA_DSR_BSY) { uart0_putstr("busy\n"); }

 if(int_status & MCF_DMA_DSR_REQ) { uart0_putstr("transfer remaining but channel not selected\n");}

}