uart_api.c

来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 129 行

C
129
字号
/**************************************************************************/
/*                                                                        */
/*     Copyright (C) 2005 Oki Electric Industry Co., LTD.                 */
/*                                                                        */
/*     System Name  :  ML67Q4051/ML67Q4061 CPU BOARD                      */
/*     Module Name  :  ML67Q4051/ML67Q4061 uart transmit routines         */
/*     File   Name  :  uart_api.c                                         */
/*     Revision     :  01.00                                              */
/*     Date         :  2005/02/08                                         */
/*                     Initial version                                    */
/*                                                                        */
/**************************************************************************/
/*     UART operation set for 115,200 Baud, no parity, 8 data,            */ 
/*                            1 stop and no flow control                  */
/**************************************************************************/

#include   "ML674061.h"
#include   "common.h"
#include   "irq.h"

/* constants */
#define XON     17
#define XOFF    19

#define FIFOSIZE	(14)

#define    BUFF_SIZE            256
#define    DLM_BAUD             0x00  /* baud = 115,200 */
#define    DLL_BAUD             0x12  /* baud = 115,200 */
/* === setting baud rate value === */
/*     baud  | DLM_BAUD | DLL_BAUD */
/*     1,200 |   0x06   |   0xB7   */
/*     2,400 |   0x03   |   0x5B   */
/*     4,800 |   0x01   |   0xAE   */
/*     9,600 |   0x00   |   0xD7   */
/*    19,200 |   0x00   |   0x6B   */
/*    38,400 |   0x00   |   0x36   */
/*    57,600 |   0x00   |   0x24   */
/*   115,200 |   0x00   |   0x12   */
 
/* functions */
void    init_uart(void);        /* initialize UART */
void	write_uart(char *);    /* Transmit string through UART */
/* global variables */
volatile  unsigned  char  rw_buff[BUFF_SIZE];  /* ring buffer */
volatile  int  write_pointer;
volatile  int  rec_data_counter;
volatile  int  error_flag;  /* 0:no error, 1:overrun error, 2:parity error,
                               3:framing error, 4:overflow error */
/******************************************************************/
/*  Send output string through UART                               */
/*  Function : write_uart                                         */
/*      Parameters                                                */
/*          Input  :  pointer to transmit string                  */
/*          Output :  0                                           */
/******************************************************************/
void	write_uart(char *buf)
{
	UBYTE j;
	static volatile UBYTE i=0;

	do{
		i++;
	     
		if(i == 80) i=0;
	     
		if ((unsigned char)*buf == '\n') /* if new-line-character, insert blank spaces till new-line */
        {
		/* This loop simply writes blank characters till new line starts */
			if(i < 80){ 
				for (j=0;j < (81-i);j++){
		/* loop till transmit FIFO becomes empty */
					while ((get_value(UARTLSR0) & UARTLSR_THRE) != UARTLSR_THRE)
						;
		/* stuff spaces till end of line */
  					put_value(UARTTHR0,' ');
				}
			}
			i=0;
		 }
	     else{
	     /* loop till transmit FIFO becomes empty */
			while ((get_value(UARTLSR0) & UARTLSR_THRE) != UARTLSR_THRE)
				;
 	     /* write characters to UART Transmitter Holding register */
			put_value(UARTTHR0,(unsigned char)*buf);
		}
	     

	}while(*buf++);
	
	i--;	      
	     

	return ;
}

/******************************************************************/
/*  Initialize UART                                               */
/*  Function : init_uart                                          */
/*      Parameters                                                */
/*          Input  :  Nothing                                     */
/*          Output :  Nothing                                     */
/******************************************************************/
void init_uart(void)
{

    UBYTE dll, dlm;

    /* setting of PORT */
    set_wbit(PORTSEL1,0x50000);    /* select secondary function (PB0-1)*/

	dll = 0x12 ;
	dlm = 0x0 ;
	
    put_value(UARTLCR0, UARTLCR_DLAB);
    put_value(UARTDLL0, dll);       /* baud rate */
    put_value(UARTDLM0, dlm);
    put_value(UARTLCR0, UARTLCR_LEN8|UARTLCR_STB1|UARTLCR_PDIS); /* data length:8bit, stop bit:1, parity:none */

    put_value(UARTFCR0, UARTFCR_FE|UARTFCR_RFLV1|    /* FIFO:enable, trigger level:1byte, */
              UARTFCR_RFCLR|UARTFCR_TFCLR);     /* receiver/transmitter FIFO clear */

    put_value(UARTIER0, 0);  /* all interrupts disabled */

    put_value(UARTMCR0, 0);
}

⌨️ 快捷键说明

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