📄 uart2.c
字号:
/*
* File: uart2.c
* Purpose: Provide common ColdFire UART routines for serial IO
*
* Notes: nearly as the same as UART.c and UART.h
* the difference is added to set RXD2 and TXD2
*/
#include "common.h"
#include "uart2.h"
/*
* To do -- enhance to a more complete driver with interrupts, dma, etc.
*/
/********************************************************************/
/*
* Initialize the UART for 8N1 operation, interrupts disabled, and
* no hardware flow-control
*
* Parameters:
* uartch UART channel to initialize
* sysclk UART System Clock (in KHz)
* baud UART baud rate
* settings Initialization parameters
*/
void
uart2_init (int uartch, int sysclk, int baud, int settings)
{
register uint16 ubgs;
MCF_GPIO_PUCPAR |= 0x03;//set RXD2 and TXD2
/*
* Reset Transmitter
*/
MCF_UART_UCR(uartch) = MCF_UART_UCR_RESET_TX;
/*
* Reset Receiver
*/
MCF_UART_UCR(uartch) = MCF_UART_UCR_RESET_RX;
/*
* Reset Mode Register
*/
MCF_UART_UCR(uartch) = MCF_UART_UCR_RESET_MR;
/*
* No parity, 8-bits per character
*/
MCF_UART_UMR(uartch) = (0
| MCF_UART_UMR_PM_NONE
| MCF_UART_UMR_BC_8);
/*
* No echo or loopback, 1 stop bit
*/
MCF_UART_UMR(uartch) = (0
| MCF_UART_UMR_CM_NORMAL
| MCF_UART_UMR_SB_STOP_BITS_1);
/*
* Set Rx and Tx baud by timer
*/
MCF_UART_UCSR(uartch) = (0
| MCF_UART_UCSR_RCS_SYS_CLK
| MCF_UART_UCSR_TCS_SYS_CLK);
/*
* Mask all UART interrupts
*/
MCF_UART_UIMR(uartch) = 0;
/*
* Calculate baud settings
*/
ubgs = (uint16)((sysclk*1000)/(baud * 32));
MCF_UART_UBG1(uartch) = (uint8)((ubgs & 0xFF00) >> 8);
MCF_UART_UBG2(uartch) = (uint8)(ubgs & 0x00FF);
/*
* Enable receiver and transmitter
*/
MCF_UART_UCR(uartch) = (0
| MCF_UART_UCR_TX_ENABLED
| MCF_UART_UCR_RX_ENABLED);
}
/*******************************************************************/
/*
*
*the task of test the uart2 module
*
*function: sent from "a" to "z" into uart2 module.and then wait for
* the uart2's input for output 26 characters again.
*
*
*
*
*
*
*/
/**putchar renew **/
void
uart2_putchar (int channel, char ch)
{
/*set transfer mode*/
MCF_GPIO_PORTTD |= 0x08;
/* Wait until space is available in the FIFO */
while (!(MCF_UART_USR(channel) & MCF_UART_USR_TXRDY))
;
/* Send the character */
MCF_UART_UTB(channel) = (uint8)ch;
/*set recieve mode*/
//MCF_GPIO_PORTTD &= ~MCF_GPIO_OUT_SET;
/* Wait until space is available in the FIFO */
//while (!(MCF_UART_USR(channel) & MCF_UART_USR_TXRDY))
;
/* Send the character */
//MCF_UART_UTB(channel) = (uint8)ch;
}
OS_STK AppTaskUart2Stk[256]; // The TaskStack for the uart2 Task
void AppTaskUart2 (void * pdata)
{
char i = 0;
uart2_init(2,SYS_CLK_KHZ,115200,NULL);
while(1)
{
printf("\nstart to output 26 charater in UART2 port!\n");
for(i=0;i<26;i++)
{
uart2_putchar (2,'a'+i); //output 26 charaters
}
/*
* Special chars like
* '\n' or '\t' are normally converted to the appropriate
* character by the __compiler__. Thus, no need for this
* routine to account for the '\' character.
*/
/*
* This needs to be replaced with something like
* 'out_char()' or call an OS routine.
*/
#ifndef UNIX_DEBUG //out '\n'
uart_putchar (2,0x0D /* CR */);
uart_putchar (2,0x0A /* LF */);
#else
uart_putchar (2,'\n');
#endif
printf("\ninput a charater in uart2 port! let uart2 port these 26 charaters again!\n");
i = uart_getchar (2); //wait for uart2 intput
#ifndef UNIX_DEBUG //out '\n'
uart_putchar (2,0x0D /* CR */);
uart_putchar (2,0x0A /* LF */);
#else
uart_putchar (2,'\n');
#endif
OSTimeDly(50);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -