📄 serial.c
字号:
/*----------------------------------------------------------------------------
* Name: serial.c
* Purpose: serial port handling
* Version: V1.00
*----------------------------------------------------------------------------
* This file is part of the uVision/ARM development tools.
* This software may only be used under the terms of a valid, current,
* end user licence from KEIL for a compatible version of KEIL software
* development tools. Nothing else gives you the right to use it.
*
* Copyright (c) 2005-2007 Keil Software.
*---------------------------------------------------------------------------*/
#include "type.h"
#include <LPC23xx.H> /* LPC23xx definitions */
#include "serial.h"
#define UART1 /* Use UART 1 */
#ifdef UART0 /* If UART 0 is used */
#define UxFCR U0FCR
#define UxFDR U0FDR
#define UxLCR U0LCR
#define UxDLL U0DLL
#define UxDLM U0DLM
#define UxLSR U0LSR
#define UxMSR U0MSR
#define UxTHR U0THR
#define UxRBR U0RBR
#elif defined(UART1) /* If UART 1 is used */
#define UxFCR U1FCR
#define UxFDR U1FDR
#define UxLCR U1LCR
#define UxDLL U1DLL
#define UxDLM U1DLM
#define UxLSR U1LSR
#define UxMSR U1MSR
#define UxTHR U1THR
#define UxRBR U1RBR
#endif
/*----------------------------------------------------------------------------
initialize the buffer
*---------------------------------------------------------------------------*/
void buf_Init (buf_t *buf, char *buffer) {
buf->in = 0;
buf->out = 0;
buf->buf = buffer;
}
/*----------------------------------------------------------------------------
put a character into the buffer
*---------------------------------------------------------------------------*/
BOOL buf_Put (buf_t *buf, char c) {
int next;
next = (buf->in + 1) % SER_BUF_SIZE; /* check if space is available */
if (next == buf->out) { /* buffer full */
return (FALSE);
}
buf->buf[buf->in] = c;
buf->in = next;
return (TRUE);
}
/*----------------------------------------------------------------------------
get a character from the buffer
*---------------------------------------------------------------------------*/
BOOL buf_Get (buf_t *buf, char *c) {
int next;
if (buf->in == buf->out) { /* check if data is available */
return (FALSE);
}
next = (buf->out + 1) % SER_BUF_SIZE;
*c = buf->buf[buf->out];
buf->out = next;
return (TRUE);
}
/*----------------------------------------------------------------------------
check if space is in the buffer
*---------------------------------------------------------------------------*/
int buf_Free (buf_t *buf) {
int used;
used = (SER_BUF_SIZE + buf->in - buf->out) % SER_BUF_SIZE;
return (SER_BUF_SIZE - 1 - used);
}
/*----------------------------------------------------------------------------
set the line coding
*---------------------------------------------------------------------------*/
void ser_SetLineCoding (DWORD baudrate, BYTE databits, BYTE parity, BYTE stopbits) {
BYTE lcr_p, lcr_s, lcr_d;
DWORD dll;
switch (databits) {
case 5: /* 5 Data bits */
lcr_d = 0x00;
break;
case 6: /* 6 Data bits */
lcr_d = 0x01;
break;
case 7: /* 7 Data bits */
lcr_d = 0x02;
break;
case 8: /* 8 Data bits */
default:
lcr_d = 0x03;
break;
}
switch (stopbits) {
case 1: /* 1,5 Stop bits */
case 2: /* 2 Stop bits */
lcr_s = 0x04;
break;
case 0: /* 1 Stop bit */
default:
lcr_s = 0x00;
break;
}
switch (parity) {
case 1: /* Parity Odd */
lcr_p = 0x08;
break;
case 2: /* Parity Even */
lcr_p = 0x18;
break;
case 3: /* Parity Mark */
lcr_p = 0x28;
break;
case 4: /* Parity Space */
lcr_p = 0x38;
break;
case 0: /* Parity None */
default:
lcr_p = 0x00;
break;
}
/* Note that the pclk is 24,0 MHz. (48.0 MHz / 2) */
/* 24 MHz PCLK generates also rates for 115200, 57600 baud */
dll = ((24000000UL / 16UL) ) / baudrate;
UxFDR = 0x00; /* Fractional divider not used */
UxLCR = 0x80 | lcr_d | lcr_p | lcr_s; /* Data bits, Parity, Stop bit */
UxDLL = dll; /* Baud Rate @ 24 MHZ PCLK */
UxDLM = (dll >> 8); /* High divisor latch */
UxLCR = 0x00 | lcr_d | lcr_p | lcr_s; /* DLAB = 0 */
}
/*----------------------------------------------------------------------------
initialize the serial interface
*---------------------------------------------------------------------------*/
void ser_Init (void) { /* Initialize Serial Interface */
#ifdef UART0
PINSEL0 |= 0x00000050; /* Enable TxD0 and RxD0 */
#elif defined (UART1)
PINSEL0 |= 0x40000000; /* Enable TxD1 */
PINSEL1 |= 0x00000001; /* Enable RxD1 */
#endif
ser_SetLineCoding (9600, 8, 0, 0); /* set the initial LineCoding */
}
/*----------------------------------------------------------------------------
read the line status register
*---------------------------------------------------------------------------*/
unsigned char ser_GetLSR (void) {
return (UxLSR);
}
/*----------------------------------------------------------------------------
read the modem status register
*---------------------------------------------------------------------------*/
unsigned char ser_GetMSR (void) {
return (UxMSR);
}
/*----------------------------------------------------------------------------
read a character from serial interface
*---------------------------------------------------------------------------*/
int ser_GetChar (void) {
while (!(UxLSR & 0x01));
return (UxRBR);
}
/*----------------------------------------------------------------------------
put a caracter to the serial interface
*---------------------------------------------------------------------------*/
int ser_PutChar (int ch) {
if (ch == '\n') {
while (!(U0LSR & 0x20));
UxTHR = '\r'; /* output CR */
}
while (!(UxLSR & 0x20));
return (UxTHR = ch);
}
/*----------------------------------------------------------------------------
put a string to the serial interface
*---------------------------------------------------------------------------*/
int ser_PutString (const char *s) {
while (*s) {
ser_PutChar(*s++);
}
ser_PutChar('\n');
return (TRUE);
}
/*----------------------------------------------------------------------------
check if a character is available at the serial interface
*---------------------------------------------------------------------------*/
BOOL ser_AvailChar (void) {
if ((UxLSR & 0x01) == 1)
return (TRUE);
else
return (FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -