Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Message

Problems w/ UART

2004-10-07 by peterburdine

I think I may be missing something stupid, but can anyone help me?
I am tring to write to UART0, but it doesn't seem to work.  I am
trying to write a software buffer to help out the one byte hardware
buffer.  Specifically I am using the LPC 2294.  Below is my code.

I am using the Keil toolset (compiler/debugger/simulator).  The code
works perfectly fine when running it on the simulator, but when I try
to run it on the chip, I will only get "ho" instead of "hello".  I put
some break points in it when using JTAG to debug, but the ISR is never
called when running on the real hardware, it is called on the
simulator.  Also, when I debug on JTAG I never se the THRE or TEMT
clear.  Can you see anything obviously wrong?  I think I put in
everything that you need to compile it.

#include <stdio.h>
#define TX_BUFFER_LENGTH 64
#define UART0_THRE			(U0LSR & 0x20)
#define		PINSEL0_00_TxD		(0x01)
#define		PINSEL0_01_GPIO		(0x00 << 2)
#define U0RBR          (*((volatile unsigned char *) 0xE000C000))
#define U0THR          (*((volatile unsigned char *) 0xE000C000))
#define U0IER          (*((volatile unsigned char *) 0xE000C004))
#define U0IIR          (*((volatile unsigned char *) 0xE000C008))
#define U0FCR          (*((volatile unsigned char *) 0xE000C008))
#define U0LCR          (*((volatile unsigned char *) 0xE000C00C))
#define U0MCR          (*((volatile unsigned char *) 0xE000C010))
#define U0LSR          (*((volatile unsigned char *) 0xE000C014))
#define U0MSR          (*((volatile unsigned char *) 0xE000C018))
#define U0SCR          (*((volatile unsigned char *) 0xE000C01C))
#define U0DLL          (*((volatile unsigned char *) 0xE000C000))
#define U0DLM          (*((volatile unsigned char *) 0xE000C004))
#define VICVectAddr6   (*((volatile unsigned long *) 0xFFFFF118))
#define VICVectCntl6   (*((volatile unsigned long *) 0xFFFFF218))
#define VICIntSelect   (*((volatile unsigned long *) 0xFFFFF00C))
#define VICIntEnable   (*((volatile unsigned long *) 0xFFFFF010))
#define VICIntEnClr    (*((volatile unsigned long *) 0xFFFFF014))
static Uint16	txBytes = 0;
static Uint16	txRdPos = 0;
static Uint16	txWrPos = 0;
static Uint16	txBuffer[TX_BUFFER_LENGTH];

void uart0_config (void);
void uart0_isr (void);
void uart0_sendByte (Uint8);
int putchar(int);
int main (void);

void uart0_config (void) {
	// Setup UART 0
	U0LCR = 0x80; // Enable DLAB
	U0DLL = 0x16; // Setup BGR
	U0DLM = 0x05; // ~2400 Bit/s @ 50 Mhz
	U0LCR = 0x03; // Enable UART 0, 8 bits, no parity, 1 stop bit

	// Reset the FIFOs
	U0FCR = 0xC7; // RX FIFO trigger level 8 chars, reset tx and rx fifo,
enable fifos

	// Setup Interrupts for UART0
	U0IER = 0x7;
	VICIntSelect &= 0x0;
	VICVectCntl6 = 0x20 | 6;
	VICVectAddr6 = (unsigned long) uart0_isr;
	VICIntEnable |= (0x1 << 6); // Enable Uart0 Interrupt	
}

void uart0_isr (void) __irq {
	switch((U0IIR>>1)&0x7) {
		// THRE
		case	001:
			if(txBytes != 0) {
				U0THR = txBuffer[txRdPos];
				txRdPos++;
				txRdPos = (txRdPos == TX_BUFFER_LENGTH)?0:txRdPos;
				txBytes--;
			}
			break;
	}
}

void uart0_sendByte(Uint8 byte) {
	// Wait until there is room in the buffer
	while(txBytes == TX_BUFFER_LENGTH);
	VICIntEnClr |= (1 << 6);
	if(!UART0_THRE) {  // Put it in the software buffer 
		txBuffer[txWrPos] = byte;
		txWrPos++;
		txWrPos = (txWrPos == TX_BUFFER_LENGTH)?0:txWrPos;
		txBytes++;
	}
	else { // If the chip buffer is empty, put it there
		U0THR = byte;	
	}
	VICIntEnable |= (1 << 6);
}

int putchar(int a) {
	uart0_sendByte((int)a);
}

int main (void) {
	PINSEL0 = PINSEL0_00_TxD | PINSEL0_01_RxD; 
	printf("hello");
}

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.