📄 simple_serial.c
字号:
/********************************************************************
*
* File Name:
* ---------
* test_tl16c750.c
*
*
* Author:
* -------
* zhang dahai
*
* Email:
* ------
* olivercheung@126.com
*
* QQ & MSN;
* ---------
* 334654529, olivercheung2005@yahoo.com.cn
*
* Modification History:
* -----------------
* 2006-7-19, zhang dahai, created(09-04 13:30).
*
********************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include "simple_serial.h"
/*44b0 sfr base address*/
#define REGBASE 0x01c00000
/*Memory Controller Registers*/
#define BWSCON *(volatile unsigned int *)(REGBASE+0x080000)
/*I/O Ports Registers */
#define PCONA *(volatile unsigned int *)(REGBASE+0x120000)
#define PDATA *(volatile unsigned int *)(REGBASE+0x120004)
#define PCONB *(volatile unsigned int *)(REGBASE+0x120008)
#define PDATB *(volatile unsigned int *)(REGBASE+0x12000c)
#define PCONC *(volatile unsigned int *)(REGBASE+0x120010)
#define PDATC *(volatile unsigned int *)(REGBASE+0x120014)
#define PUPC *(volatile unsigned int *)(REGBASE+0x120018)
#define PCOND *(volatile unsigned int *)(REGBASE+0x12001c)
#define PDATD *(volatile unsigned int *)(REGBASE+0x120020)
#define PUPD *(volatile unsigned int *)(REGBASE+0x120024)
#define PCONE *(volatile unsigned int *)(REGBASE+0x120028)
#define PDATE *(volatile unsigned int *)(REGBASE+0x12002c)
#define PUPE *(volatile unsigned int *)(REGBASE+0x120030)
#define PCONF *(volatile unsigned int *)(REGBASE+0x120034)
#define PDATF *(volatile unsigned int *)(REGBASE+0x120038)
#define PUPF *(volatile unsigned int *)(REGBASE+0x12003c)
#define PCONG *(volatile unsigned int *)(REGBASE+0x120040)
#define PDATG *(volatile unsigned int *)(REGBASE+0x120044)
#define PUPG *(volatile unsigned int *)(REGBASE+0x120048)
#define SPUCR *(volatile unsigned int *)(REGBASE+0x12004c)
#define EXTINT *(volatile unsigned int *)(REGBASE+0x120050)
#define EXTINPND *(volatile unsigned int *)(REGBASE+0x120054)
/*Interrupt Controller Registers */
#define INTCON *(volatile unsigned int *)(REGBASE+0x200000)
#define INTPND *(volatile unsigned int *)(REGBASE+0x200004)
#define INTMOD *(volatile unsigned int *)(REGBASE+0x200008)
#define INTMSK *(volatile unsigned int *)(REGBASE+0x20000c)
#define I_PSLV *(volatile unsigned int *)(REGBASE+0x200010)
#define I_PMST *(volatile unsigned int *)(REGBASE+0x200014)
#define I_CSLV *(volatile unsigned int *)(REGBASE+0x200018)
#define I_CMST *(volatile unsigned int *)(REGBASE+0x20001c)
#define I_ISPR *(volatile unsigned int *)(REGBASE+0x200020)
#define I_ISPC *(volatile unsigned int *)(REGBASE+0x200024)
#define F_ISPR *(volatile unsigned int *)(REGBASE+0x200038)
#define F_ISPC *(volatile unsigned int *)(REGBASE+0x20003c)
#define BUFSIZE 100
static void config_bwscon_and_port_and_extint (void)
{
long temp;
/*bus width config for fpga and tl16c750*/
temp = BWSCON ;
BWSCON = ( temp & ( ~0xFF00 ) ) | 0x4400 ; /*nGCS3: TL16C750. 8bit mode*/
/*nGCS2: FPGA. 8bit mode*/
/* Configuration Port Control Register*/
/* Port A */
PCONA = 0x3FF;
/* Port B */
PCONB = 0x7FF;
/* Port C */
PCONC = 0XFF000000;
/* Port D */
PCOND = 0x5000;
PDATD = 0xC0;
/* Port E */
PCONE = 0x00000028;
/* Port F */
PCONF = 0x00124b2a;
/* Port G */
PCONG = 0x00000030; /* PG2--INT750--EINT2*/
/*INTMSK*/
temp = INTMSK ;
INTMSK = temp | 0x800064; /*open UTXD1,URXD1,EXTINT2,I2C*/
/* Configure TL16C750 interupt*/
temp = EXTINT;
EXTINT = ( temp & ( ~0x700 ) ) | 0x100 ;
}
/*============================================
Description: we need to configure LCR,BAUD RATE,MCR,FCR,IER for
tl16c750.
=============================================*/
static void config_tl16c750(void)
{
/*line control registe=0000 1011*/
/*8 bits, 1 stop bit, odd*/
TL16C750_LCR = LCR_8_CHR_BIT | LCR_1_STOP_BIT ; // | LCR_EN_PARITY ;
/*Baud Rate*/
/*DLAB=1*/
TL16C750_LCR = TL16C750_LCR | LCR_DLAB ;
/*divisor = XIN frequency input ÷(desired baud rate ×16)*/
TL16C750_DLSB = ( XTALL / ( DESIRED_BAUD_RATE * 16 ) ) & 0x00FF ;
TL16C750_DMSB = ( XTALL / ( DESIRED_BAUD_RATE * 16 ) ) >> 8 ;
/*Modem Control Register=0010 0010*/
/* Auto-RTS and auto-CTS enabled*/
//TL16C750_MCR = MCR_EN_AUTO_FLOW;
/*FIFO Interrupt Control Register=0100 0001*/
/*64bytes mode, trigger level=56, enable the transmit and receive FIFOs*/
TL16C750_LCR = TL16C750_LCR & ( ~LCR_DLAB) ; /*DLAB=0*/
//TL16C750_FCR = FCR_64_BYTE_MODE |FCR_56_TRIGGER_LEVEL_64 |FCR_FIFO_ENALBE;
TL16C750_FCR = 0 ;
/*Interrupt Enable Register=0000 0101*/
/*DLAB=0*/
TL16C750_LCR = TL16C750_LCR & ( ~LCR_DLAB) ;
/*enable received data available interrupt, and receiver line status interrupt; disable THRE interrupt*/
TL16C750_IER = IER_EN_RDA_INT | IER_EN_RLS_INT ;
}
int main(void)
{
unsigned char temp;
unsigned char buf[BUFSIZE];
PDATD = 0xc0;
printf("start to test the driver for tl16c750.\n\n");
printf("configure arm......");
config_bwscon_and_port_and_extint();
printf("done\n\n");
printf("configure TL16C750......");
config_tl16c750();
printf("done\n\n");
PDATD = 0x00;
while( 1)
{
//PDATD = 0x80;
temp = TL16C750_LSR ;
printf("LSR = 0x%x\n", temp);
if( ( temp & LSR_DATA_READY ) == 1 )
{
printf("receiving char......");
temp = TL16C750_FIFO ;
printf("done!\n");
printf("the char is 0x%x\n",temp);
printf("sending back the char to host......");
TL16C750_FIFO = temp ;
while( ( TL16C750_LSR & LSR_THRE ) == 0) ;
printf("done!\n");
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -