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

📄 uart.c

📁 IARSOURCECODE是基于LPC2478嵌入式软件IAR EWARM V4.42的应用实例代码
💻 C
字号:
/*****************************************************************************
 *   uart.c:  UART API file for NXP LPC24xx Family Microprocessors
 *
 *   Copyright(C) 2006, NXP Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2006.07.12  ver 1.00    Prelimnary version, first Release
 *
******************************************************************************/
#include "LPC2468.h"                        /* LPC24xx definitions */
#include "type.h"
#include "target.h"
#include "irq.h"
#include "timer.h"
#include "uart.h"
#include <stdarg.h>
#include <string.h>

#define UART_TIMEOUT 100                         // 100ms收不到数据则超时
unsigned long uart_timeout = UART_TIMEOUT;

/* 串口初始化 */
DWORD UARTInit( DWORD baudrate )
{
    DWORD Fdiv;
    PINSEL0 = 0x00000050;       /* RxD0 and TxD0 */

    U0LCR = 0x83;		/* 8 bits, no Parity, 1 Stop bit */
    Fdiv = ( Fpclk / 16 ) / baudrate ;	/*baud rate */
    U0DLM = Fdiv / 256;							
    U0DLL = Fdiv % 256;
    U0LCR = 0x03;		/* DLAB = 0,禁止访问DL寄存器 */
    U0FCR = 0x07;		/* Enable and reset TX and RX FIFO. */

    return (TRUE);
}

/* 串口打印一个字符 */
void putch( char data )
{
	static char od = 0;
	if ( data == '\n' && od != '\r') putch('\r');
	while ( !( U0LSR & 0x40 ) );                      //还有数据未发送完,等待
	U0THR = data;					 // 发送数据
	od = data;
}

/* 轮询串口 */
char PollUart(char * pc)
{
	if(U0LSR & 0x01)
	{
	  *pc = U0RBR;
	  return 1;
	}
	return 0;
}

/* 串口接收一个字符 */
char getch(void)
{
	char c;
	while(!PollUart(&c));
          return c;
}

/* 带超时机制的字符接收 */
char getch_time(void)
{
    char ch;
    while((U0LSR & 0x01) == 0)
    {
      if(--uart_timeout == 0)break;
      delayMs(0,1);
    }
    if(uart_timeout == 0)return(0);
    ch = U0RBR;
    uart_timeout = UART_TIMEOUT;
    return(ch);
}

/* 打印字符串 */
void puts(char * s)
{
	while(*s) putch(*s++);
}

/* 接收字符串 */
void gets(char * s)
{
    while( 1 )
    {
      *s = getch();
      if(*s == '\0')break;      // 接收到的数据是否为结束符
      s++;                      // 未做边界检查,可能会有危险
    }}

/*---------------------printf and support routines ---------------------*/
int vsprintf(char * /*s*/, const char * /*format*/, va_list /*arg*/);
	
void printf(char *fmt, ...)
{
	va_list ap;
	char string[256];	
	
	va_start(ap, fmt);
	vsprintf(string, fmt, ap);
	
	puts(string);
	va_end(ap);
}

// 从串口接收数据到内存,可以利用其进行PC到ARM板的文件传输
unsigned long LoadFromUart( unsigned long address )
{
	unsigned char * buf ;
        unsigned long download_addr = 0x40000000;
        unsigned long download_len = 0;

	download_addr = address;

	buf = ( unsigned char * ) download_addr;

	printf( "Downing file from uart to 0x%x...\n" , download_addr );
        buf[download_len++] = getch();

        uart_timeout = UART_TIMEOUT;
	while ( uart_timeout )buf[download_len++] = getch_time();         // 碰到超时时认为传输结束

	printf( "\nReceived %d bytes success.\n" , download_len );

        return(download_len);
}

// 运行一个程序
void RunProgram( int argc , char* argv[] )
{
//	UTILS u = {getch,putch,puts,printf,Delay,SetInterrupt};
	UTILS u = {getch,putch,puts,printf};
//	int ( *fp ) ( PUTILS) = ( int ( * ) (PUTILS) ) download_addr;
//	u.printf("running from 0x%0x\n",download_addr);
//	( *fp ) ( &u );		//add by lqm
}

// 从串口接收程序然后运行
void UartLoadRun( int argc , char* argv[] )
{
//	LoadFromUart( argc , argv );
	RunProgram( 0 , 0 );
}

/******************************************************************************
**                            End Of File
******************************************************************************/

⌨️ 快捷键说明

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