首先從最簡單的串口驅動開始,下面是本人寫的最簡單的串口驅動,程序開始先是向串口發(fā)送一個固定的字符串,然后又鍵盤輸入字符,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個數(shù)據(jù)位 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;
}