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

📄 serial.c

📁 RT-Thread是发展中的下一代微内核嵌入式实时操作系统
💻 C
字号:
/* * File      : serial.c * This file is part of RT-Thread RTOS * COPYRIGHT (C) 2006, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at * http://openlab.rt-thread.com/license/LICENSE * * Change Logs: * Date           Author       Notes * 2006-03-13     Triseel      first version migrated on 9200 */#include <rtthread.h>#include <rthw.h>#include "AT9200.h"#include "serial.h"UARTPort_T UARTPorts[UART_MAXNum];/** * @addtogroup AT91RM9200 *//*@{*//** * This function is used to display a string on console, normally, it's * invoked by rt_kprintf * * @param str the displayed string *//*commented by Triseel,**i dont think waiting is a GI, when i magnited on Nucleus, i use FIFO method to fullfil the drv, i think it will be better, :-)*/void rt_console_puts(const char* str){	while (*str)	{		rt_serial_dbg_putc(*str++);	}}/*************************************************** Implemented : Triseel, Date : 06-03-03** Func Name   : UART_SetBaudRate** Comments    : set specified UART port BaudRate** Notes       : the baud rate is relative to CPU master clk, if ur CPU master clk changed, plz change CPU_MASTER_CLK val** Input  Args :**               pargUARTPort : the specified UART port ptr**               argBaudRate  : the wanna BaudRate to set** Output Args :**               none** Rtn    Val  :**               none*************************************************/voidUART_SetBaudRate(UARTPort_T *pargUARTPort, rt_uint32_t argBaudRate){	rt_uint16_t BaudDiv;	BaudDiv=CPU_MASTER_CLK/(argBaudRate*16);	REG_OUT32(pargUARTPort->BaseAddr, AT9200_UART_BRGR_OFF, BaudDiv);}/*************************************************** Implemented : Triseel, Date : 06-03-03** Func Name   : rt_serial_init** Comments    : do specified UART port init** Notes       :** Input  Args :**               pargUARTPort : UARTPort_T ptr, in fact, u should just evaluate the "Port" element as correct val** Output Args :**               none** Rtn    Val  :**               none*************************************************/voidrt_serial_init(UARTPort_T *pargUARTPort){	rt_uint8_t    PortNum;	rt_uint32_t   TempVal;	PortNum=pargUARTPort->Port;	if (PortNum>=UART_MAXNum)		return ;	else	{		if (pargUARTPort->bInited==0)  /*not inited*/		{			UARTPorts[PortNum].Port=PortNum;			if (pargUARTPort->DataBits==DataBits_Reserved)				UARTPorts[PortNum].DataBits=DataBits_8;			else				UARTPorts[PortNum].DataBits=pargUARTPort->DataBits;			if (pargUARTPort->Parity==Parity_Reserved)				UARTPorts[PortNum].Parity=Parity_None;			else				UARTPorts[PortNum].Parity=pargUARTPort->Parity;			if (pargUARTPort->StopBits==StopBits_Reserved)				UARTPorts[PortNum].StopBits=StopBits_1;			else				UARTPorts[PortNum].StopBits=pargUARTPort->StopBits;			UARTPorts[PortNum].BaudRate=pargUARTPort->BaudRate;			switch(PortNum)			{				case UART_DBG :					UARTPorts[PortNum].BaseAddr=AT9200_BASE_DBGU;					UARTPorts[PortNum].Vector=SYS_VEC;					/* config sys & peripheral */					/* read & clear UART int */					TempVal=REG_IN32(UARTPorts[PortNum].BaseAddr, AT9200_UART_CSR_OFF);					/* config PIO */					REG_OUT32(AT9200_PIOA_PDR  , 0x00, (1<<UARTDBG_TXD) | (1<UARTDBG_RXD));  /*as peripheral use*/					REG_OUT32(AT9200_PIOA_ASR  , 0x00, (1<<UARTDBG_TXD) | (1<UARTDBG_RXD));  /*as peripheral A*/					REG_OUT32(AT9200_PIOA_IFER , 0x00, (1<<UARTDBG_TXD) | (1<UARTDBG_RXD));  /*Filter enable*/					REG_OUT32(AT9200_PIOA_IDR  , 0x00, (1<<UARTDBG_TXD) | (1<UARTDBG_RXD));  /*interrupt disable*/					REG_OUT32(AT9200_PIOA_PPUER, 0x00, (1<<UARTDBG_TXD) | (1<UARTDBG_RXD));  /*pull-up resistor enabled*/					/* config PMC */					REG_OUT32(AT9200_PMC_PCER, 0x00, 1<<SYS_VEC);  /*enable UART_DBG PMC*/					break;				case UART_1 :					rt_memcpy(UARTPorts[PortNum].Name, "UART_1", sizeof("UART_1"));					/* semphore not created */					UARTPorts[PortNum].BaseAddr=AT9200_BASE_US0;					UARTPorts[PortNum].Vector=UART0_VEC;					/* config sys & peripheral */					/* read & clear UART int */					TempVal=REG_IN32(UARTPorts[PortNum].BaseAddr, AT9200_UART_CSR_OFF);					/* config PIO */					REG_OUT32(AT9200_PIOA_PDR  , 0x00, (1<<UART0_TXD) | (1<UART0_RXD));  /*as peripheral use*/					REG_OUT32(AT9200_PIOA_ASR  , 0x00, (1<<UART0_TXD) | (1<UART0_RXD));  /*as peripheral A*/					REG_OUT32(AT9200_PIOA_IFER , 0x00, (1<<UART0_TXD) | (1<UART0_RXD));  /*Filter enable*/					REG_OUT32(AT9200_PIOA_IDR  , 0x00, (1<<UART0_TXD) | (1<UART0_RXD));  /*int disable*/					REG_OUT32(AT9200_PIOA_PPUER, 0x00, (1<<UART0_TXD) | (1<UART0_RXD));  /*pull-up resistor enabled*/					/* config AIC */					REG_OUT32(AT9200_AIC_SMR , UART0_VEC*4, 0x20);   /*set int mode & priority*/					REG_OUT32(AT9200_AIC_IECR, 0x00, 1<<UART0_VEC);  /*enable UART0 int*/					/* config PMC */					REG_OUT32(AT9200_PMC_PCER, 0x00, 1<<UART0_VEC);  /*enable UART0 PMC*/					break;				case UART_2 :					rt_memcpy(UARTPorts[PortNum].Name, "UART_2", sizeof("UART_2"));					UARTPorts[PortNum].BaseAddr=AT9200_BASE_US1;					UARTPorts[PortNum].Vector=UART1_VEC;					break;				case UART_3 :					rt_memcpy(UARTPorts[PortNum].Name, "UART_3", sizeof("UART_3"));					UARTPorts[PortNum].BaseAddr=AT9200_BASE_US2;					UARTPorts[PortNum].Vector=UART2_VEC;					break;				case UART_4 :					rt_memcpy(UARTPorts[PortNum].Name, "UART_4", sizeof("UART_4"));					UARTPorts[PortNum].BaseAddr=AT9200_BASE_US3;					UARTPorts[PortNum].Vector=UART3_VEC;					break;			}			/*init UART cmn reg*/			REG_OUT32(UARTPorts[PortNum].BaseAddr, AT9200_UART_CR_OFF , 0xA0);  /*disable Tx & Rx*/			REG_OUT32(UARTPorts[PortNum].BaseAddr, AT9200_UART_CR_OFF , 0xAC);  /*disable Tx & Rx & rst Tx & Rx*/			REG_OUT32(UARTPorts[PortNum].BaseAddr, AT9200_UART_CR_OFF , 0x100); /*rst Status Bits*/			REG_OUT32(UARTPorts[PortNum].BaseAddr, AT9200_UART_IDR_OFF, 0xFFFFFFFF);  /*disable specified UART port all ints*/			UART_SetBaudRate(&UARTPorts[PortNum], UARTPorts[PortNum].BaudRate);  /*Setup baud rate*/			/*config mode & parity, data len, stop bits len*///			TempVal=UARTPorts[PortNum].DataBits | UARTPorts[PortNum].StopBits | UARTPorts[PortNum].Parity;			REG_OUT32(UARTPorts[PortNum].BaseAddr, AT9200_UART_MR_OFF , 0x800);			REG_OUT32(UARTPorts[PortNum].BaseAddr, AT9200_UART_CR_OFF , 0x0C);  /*rst Tx & Rx*/			REG_OUT32(UARTPorts[PortNum].BaseAddr, AT9200_UART_CR_OFF , 0x50);  /*enable Tx & RX*/			if (UARTPorts[PortNum].Port!=UART_DBG)  /*dbg UART will operate at no-int mode*/				REG_OUT32(UARTPorts[PortNum].BaseAddr, AT9200_UART_IER_OFF, AT9200_US_RXRDY_BIT);  /*enable Rx int*/			/*create sem to fullfil protect machenism*/			// UARTPorts[PortNum].Sem=rt_sem_create("UART_Sem", 1, RT_IPC_FLAG_FIFO);			UARTPorts[PortNum].bInited=1;		}	}}/*************************************************** Implemented : Triseel, Date : 06-03-03** Func Name   : rt_serial_getc** Comments    : get a char from UART port** Notes       :** Input  Args :**               none** Output Args :**               none** Rtn    Val  :**               read char*************************************************/charrt_serial_getc(void){	char RxVal='\0';	if (UARTPorts[UART_DBG].bInited)		RxVal=REG_IN32(UARTPorts[UART_DBG].BaseAddr, AT9200_UART_RHR_OFF);	return RxVal;}/*************************************************** Implemented : Triseel, Date : 06-03-03** Func Name   : rt_serial_dbg_getc** Comments    : get a char from UART dbg port** Notes       :** Input  Args :**               none** Output Args :**               none** Rtn    Val  :**               read char*************************************************/rt_uint8_trt_serial_dbg_getc(void){	char RxVal='\0';	if (UARTPorts[UART_DBG].bInited)		RxVal=REG_IN32(UARTPorts[UART_DBG].BaseAddr, AT9200_UART_RHR_OFF);	return RxVal;}/*************************************************** Implemented : Triseel, Date : 06-03-03** Func Name   : rt_serial_dbg_putc** Comments    : tx a char from UART dbg port** Notes       :** Input  Args :**               argTxVal : tx char** Output Args :**               none** Rtn    Val  :**               none*************************************************/voidrt_serial_dbg_putc(const char argTxVal){	if (argTxVal=='\n')		rt_serial_dbg_putc('\r');	while (  !(REG_IN32(UARTPorts[UART_DBG].BaseAddr, AT9200_UART_CSR_OFF)&AT9200_US_TXEMPTY_VAL)  );	REG_OUT32(UARTPorts[UART_DBG].BaseAddr, AT9200_UART_THR_OFF, argTxVal);}/*@}*/

⌨️ 快捷键说明

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