首先从最简单的串口驱动开始,下面是本人写的最简单的串口驱动,程序开始先是向串口发送一个固定的字符串,然后又键盘输入字符,537收到之后返回响应的字符。
/*****************************************************************************
* uart.c 适用于blackfin537 ezkit平台
*****************************************************************************/
#include <sysreg.h>
#include "ccblkfn.h"
#include "cdefBF537.h" //寄存器映射的头文件
//此程序是UART的驱动程序
#include <sys/exception.h>
void serial_putc(const char c)
{
/* send a /r for compatibility */
if (c == '/n')
serial_putc('/r');
// WATCHDOG_RESET();
/* wait for the hardware fifo to clear up */
while (!(*pUART0_LSR & THRE))
continue;
/* queue the character for transmission */
*pUART0_THR = c;
asm ("ssync;");
// WATCHDOG_RESET();
/* wait for the byte to be shifted over the line */
while (!(*pUART0_LSR & TEMT))
continue;
}
EX_INTERRUPT_HANDLER(UART_ISR)
{
int data;
if(*pUART0_LSR&0x0001)
{
data=*pUART0_RBR;
if(data==0x0D)
{
*pUART0_THR=0x0A;
asm("ssync;");
while((*pUART0_LSR&0x0020)==0);
}
*pUART0_THR=data;
asm("ssync;");
while((*pUART0_LSR&0x0020)==0);
}
}
void init_interrupt(void)
{
asm("ssync;");
*pUART0_IER = 0x0001; //Enabling Rx, Tx and error interrupts on UART0_IER.
asm("ssync;");
register_handler(ik_ivg10,UART_ISR);
*pSIC_IMASK|=0x0800;
asm("ssync;");
*pIMASK |= 0x00000400;
}
void init_port(void)
{
*pPORTF_FER |= 0x0003;
*pPORTF_FER |= 0x0003;
asm ("ssync;");
*pPORT_MUX &= ~0x0008;
*pPORT_MUX &= ~0x0008;
}
void init_uart(void)
{
*pUART0_GCTL=0x01; //首先使能串口时钟
*pUART0_LCR=0x03; //8个数据位 1个停止位
asm ("ssync;");
//set baudrate
unsigned int divisor = (50000000 / (115200 * 16))+1;
*pUART0_LCR |= DLAB;
asm ("ssync;");
/* Program the divisor to get the baud rate we want */
*pUART0_DLL = divisor;
asm ("ssync;");
*pUART0_DLH = divisor>>8;
*pUART0_LCR &= ~DLAB;
}
int main( void )
{
/* Begin adding your custom code here */
int i=0;
// *pEBIU_AMGCTL=0xff;
init_port();
init_uart();
init_interrupt();
char s=57;
for(i=0;i<10;i++)
{
serial_putc(s);
}
while(1);
return 0;
}