📄 uart1.c
字号:
/******************************************************************************************
Uart1.c (v1.0)
-------------------------------------------------------------------------------------
This code is from the book:
"Embedded Internet: TCP/IP Basics, Implementation and Applications" by Sergio Scaglia
[Pearson Education, 2006 - ISBN: 0-32-130638-4]
This code is copyright (c) 2006 by Sergio Scaglia, and it may only be used for educational
purposes. For commercial use, please contact me at sscaglia@intramarket.com.ar
For more information and updates, please visit www.embeddedinternet.org
******************************************************************************************/
#include "sysClock.h"
#include "uart1.h"
#include <iolpc2129.h>
#include <stdio.h>
#define CR 0x0D
#define IN_BUF_LEN 2048 // Must be Power of 2 ...
static char in_buf[IN_BUF_LEN];
static unsigned short inbuf_in_ptr=0;
static unsigned short inbuf_out_ptr=0;
#if ((IN_BUF_LEN & (IN_BUF_LEN - 1)) != 0)
#error IN_BUF_LEN must be power of 2!
#endif
void uart1_irq(void) { // Generate Interrupt
volatile char IIR;
while (((IIR = U1IIR) & 0x01) == 0) {
if ((inbuf_in_ptr - inbuf_out_ptr) < IN_BUF_LEN) {
in_buf[inbuf_in_ptr & (IN_BUF_LEN - 1)] = U1RBR;
inbuf_in_ptr++;
}
else {
printf("Error: Input Buffer exhausted! Increment IN_BUF_LEN\n");
break;
}
}
VICVectAddr = 0; // Acknowledge Interrupt
}
void UART1_init(unsigned int baud) {
volatile char dummy;
unsigned int divisor = (pClkFreq()/4) / (16 * baud);
U1LCR = 0x83; /* 8 bit, 1 stop bit, no parity, enable DLAB */
U1DLL = divisor & 0xFF;
U1DLM = (divisor >> 8) & 0xFF;
U1LCR &= ~0x80; /* Disable DLAB */
PINSEL0 = (PINSEL0 & ~0xF0000) | 0x50000;
U1FCR = 1;
U1IER = 0; // Disable RDA interrupt
VICVectAddr1 = (unsigned int)uart1_irq; // set interrupt vector in 1
VICVectCntl1 = 0x20 | 7; // use it for UART 1 Interrupt
VICIntEnable = 0x00000080; // Enable UART 1 Interrupt
dummy = U1IIR; // Read IrqID - Required to Get Interrupts Started
U1IER = 1; // Enable UART1 RX
}
void putchar1(int ch) {
if (ch == '\n') {
while (!(U1LSR & 0x20));
U1THR = CR;
}
while (!(U1LSR & 0x20));
U1THR = ch;
}
int getchar1 (void) {
if (inbuf_out_ptr < inbuf_in_ptr)
return in_buf[(inbuf_out_ptr++) & (IN_BUF_LEN - 1)];
else {
return (-1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -