⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stdio_uart.c

📁 ADI-BF533 DSP的启动初始化配置源代码
💻 C
字号:
///////////////////////////////////////////////////////////////
// 
//  STDIO_UART
//
// Creates new STDIO Device Driver for UART I/O
//
//(C) Copyright 2004 - Analog Devices, Inc.  All rights reserved.
//File Name:        stdio_uart.c
//Date Modified:	03/19/04	
//
//Hardware:		ADSP-BF533 EZ-KIT Lite Board
//
//Special Connections:  
//		- EIA-232 Serial Cable (1:1)
//		- Terminal Program (i.e. Hyperterminal for Windows)
//		- 2400 bits per second (default)
//		- 8-bit, no parity, 1 stop bit
//		- no handshake
//		- echo off
//
//Purpose:	
//          - Registering New STDIO Device Driver for UART I/0
//          - Redirect Stdin/Stdout to New STDIO Device Driver
//          - (optional)Autobaud Detection using GP Timer 2
//	
//Steps:
//          - Build and Run 
//          - Open a Terminal Program (i.e. Hyperterminal for Windows)
//          - "Hello World" will print to Terminal Program
//	      - "Type in something, press <Enter>, and I'll repeat it backwards!"
//
///////////////////////////////////////////////////////////////

#include <device.h>
#include <ccblkfn.h>
#include <cdefBF533.h>
#include <sys/exception.h>
#include <stdio.h>
#include <defBF533.h>

#define _SCLK_ 54

int   UART_init  (struct DevEntry *dev);
int   UART_open  (const char *name, int mode);
int   UART_close (int fd);
int   UART_write (int fd, unsigned char *buf, int size);
int   UART_read  (int fd, unsigned char *buf, int size);
long  UART_seek  (int fd, long offset, int whence);


DevEntry UART_DevEntry =
{
	0,
	NULL,
	UART_init,
	UART_open,
	UART_close,
	UART_write,
	UART_read,
	UART_seek,
	
	dev_not_claimed,
	dev_not_claimed,
	dev_not_claimed
};


void UART_inititialize(int divisor);
void UART_disable(void);
int  UART_detectAutobaud(void);
void UART_waitForTransferCompletion(void);
void UART_putc(char c);
void UART_puts(char *c, int Max);
char UART_getc(void);
int  UART_gets(char *str, int max);


///////////////////////////////////////////////////////////////
int   UART_init  ( struct DevEntry *dev )
{
#define __divisor__ _SCLK_ * 1000000 / (115200 * 16 )
	UART_inititialize(__divisor__);		
	return 0;
}
///////////////////////////////////////////////////////////////
int   UART_open  (const char *name, int mode)
{
	return 0;
}
///////////////////////////////////////////////////////////////
int   UART_write (int fd, unsigned char *buf, int size)
{
	
	int i = 0;
	
	for ( ; i < size; ++i)	UART_putc( *buf++ );
		
	UART_waitForTransferCompletion();
		
	return 0;
}
///////////////////////////////////////////////////////////////
int   UART_read  (int fd, unsigned char *buf, int size)
{
	fflush( (FILE *) fd );  //for printf
	int result = UART_gets( (char *) buf, size );
	return result;
}

///////////////////////////////////////////////////////////////
long   UART_seek  (int fd, long offset, int whence)
{
	return 0;
}

///////////////////////////////////////////////////////////////
int   UART_close (int fd)
{
	UART_disable();
	return 0;
}
///////////////////////////////////////////////////////////////
//Configures UART in 8 data bits, no parity, 1 stop bit mode.
///////////////////////////////////////////////////////////////

void UART_inititialize(int divisor)
{
#if 1
	// enable UART clock. 
	*pUART_GCTL = UCEN;
	
	// Read period value and apply formula:  divisor = period/16*8
	// Write result to the two 8-bit DL registers (DLH:DLL).
	*pUART_LCR = DLAB;
	
	*pUART_DLL = divisor;

	*pUART_DLH = divisor>>8;

	// Clear DLAB again and set UART frame to 8 bits, no parity, 1 stop bit. 

	*pUART_LCR = WLS(8);
	//ssync();
	//asm("ssync;");
#else

	asm("[--sp] = r0;");
	asm("[--sp] = p0;");

	asm("P0.L	= UART_GCTL &0xFFFF;");
	asm("P0.H	= (UART_GCTL >> 16) & 0xFFFF;");
	asm("R0		= 0x01;");
	asm("W[P0]	= R0;");
	asm("ssync;");
	asm("P0.L	= UART_LCR &0xFFFF;");
	asm("P0.H	= (UART_LCR >> 16) & 0xFFFF;");
	asm("R0		= 0x80;");
	asm("W[P0]	= R0;");
	asm("ssync;");
	asm("P0.L	= UART_DLL &0xFFFF;");
	asm("P0.H	= (UART_DLL >> 16) & 0xFFFF;");
	asm("R0		= 0x1D;");
	asm("W[P0]	= R0;");
	asm("ssync;");
	asm("P0.L	= UART_DLH &0xFFFF;");
	asm("P0.H	= (UART_DLH >> 16) & 0xFFFF;");
	asm("R0		= 0x00;");
	asm("W[P0]	= R0;");
	asm("ssync;");
	asm("P0.L	= UART_LCR &0xFFFF;");
	asm("P0.H	= (UART_LCR >> 16) & 0xFFFF;");
	asm("R0		= 0x03;");
	asm("W[P0]	= R0;");
	asm("ssync;");
	
	asm("p0 = [sp++];");
	asm("r0 = [sp++];");
	asm("rts;");	
	
#endif		
}
///////////////////////////////////////////////////////////////
//disable UART clock, after polling the TEMT bit
///////////////////////////////////////////////////////////////
void UART_disable(void)
{
	UART_waitForTransferCompletion();
	*pUART_GCTL = 0;
}
///////////////////////////////////////////////////////////////
//poll the TEMT bit in LSR reg and wait until all data shifted out.
///////////////////////////////////////////////////////////////
void UART_waitForTransferCompletion(void)
{	
	while (!(*pUART_LSR & TEMT)) { }; // wait
	ssync();
}
///////////////////////////////////////////////////////////////
//transmit character by polling the THRE bit in the LSR register.
///////////////////////////////////////////////////////////////
void UART_putc(char c)
{
	while (!(*pUART_LSR & THRE)) { }; //wait
	*pUART_THR = c;		
}
//////////////////////////////////////////////////////////////
void UART_puts(char *c, int Max)
{
	int Count;
	Count = Max;
	while (Count)
	{	
		UART_putc(*c);
		c++;
		Count--;
	}
}
///////////////////////////////////////////////////////////////
//receive character by polling the DR bit in the LSR register.
///////////////////////////////////////////////////////////////
char UART_getc(void)
{
	char c;
	while (!(*pUART_LSR & DR)) { }; //wait
	c = *pUART_RBR;
	return c;
}
///////////////////////////////////////////////////////////////
//receive an LF ('ENTER') by polling the DR bit in the LSR register.
///////////////////////////////////////////////////////////////
int UART_gets(char *str, int max)
{
	int i;
	for(i = 0; i < max; i++)
	{
		str[i] = UART_getc();
/*		if (str[i] == 13)
		{
			return i+1;
		}*/
	}
	return max;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -