#include <hal_machperiph.h>
#include "uart.h"
#include "serial_reg.h"
+#include "hal_gpio.h"
/* on LPC17xx: UART0 TX-Pin=P0.2, RX-Pin=P0.3
PINSEL0 has to be set to "UART-Function" = Function "01"
volatile int i;
uint32_t baud_div;
- // setup Pin Function Select Register (Pin Connect Block)
- // make sure old values of Bits 0-4 are masked out and
- // set them according to UART0-Pin-Selection
- PINCON->PINSEL0 = (PINCON->PINSEL0 & ~UART0_PINMASK) | UART0_PINSEL;
+ hal_pin_conf(RXD0_PIN);
+ hal_pin_conf(TXD0_PIN);
- UART0->IER = 0x00; // disable all interrupts
+ LPC_UART0->IER = 0x00; // disable all interrupts
//UART0->IIR = 0x00; // clear interrupt ID register
//UART0->LSR = 0x00; // clear line status register
baud_div = (PCLK + baud / 4) / baud;
// set the baudrate - DLAB must be set to access DLL/DLM
- UART0->LCR = (1<<UART0_LCR_DLAB); // set divisor latches (DLAB)
- UART0->DLL = (uint8_t)baud_div; // set for baud low byte
- UART0->DLM = (uint8_t)(baud_div >> 8); // set for baud high byte
+ LPC_UART0->LCR = (1<<UART0_LCR_DLAB); // set divisor latches (DLAB)
+ LPC_UART0->DLL = (uint8_t)baud_div; // set for baud low byte
+ LPC_UART0->DLM = (uint8_t)(baud_div >> 8); // set for baud high byte
// set the number of characters and other
// user specified operating parameters
// Databits, Parity, Stopbits - Settings in Line Control Register
- UART0->LCR = (mode & ~(1<<UART0_LCR_DLAB)); // clear DLAB "on-the-fly"
+ LPC_UART0->LCR = (mode & ~(1<<UART0_LCR_DLAB)); // clear DLAB "on-the-fly"
// setup FIFO Control Register (fifo-enabled + xx trig)
- UART0->FCR = fmode;
+ LPC_UART0->FCR = fmode;
for(i=0;i<65000;i++);
}
int uart0Putch(int ch)
{
- while (!(UART0->LSR & UART_LSR_THRE)) // wait for TX buffer to empty
+ while (!(LPC_UART0->LSR & UART_LSR_THRE)) // wait for TX buffer to empty
continue; // also either WDOG() or swap()
- UART0->THR = (uint8_t)ch; // put char to Transmit Holding Register
+ LPC_UART0->THR = (uint8_t)ch; // put char to Transmit Holding Register
return (uint8_t)ch; // return char ("stdio-compatible"?)
}
int uart0PutchNW(int ch)
{
- if (!(UART0->LSR & UART_LSR_THRE)) // wait for TX buffer to empty
+ if (!(LPC_UART0->LSR & UART_LSR_THRE)) // wait for TX buffer to empty
return -1; // also either WDOG() or swap()
- UART0->THR = (uint8_t)ch; // put char to Transmit Holding Register
+ LPC_UART0->THR = (uint8_t)ch; // put char to Transmit Holding Register
return (uint8_t)ch; // return char ("stdio-compatible"?)
}
int uart0TxEmpty(void)
{
- return (UART0->LSR & (UART_LSR_THRE | UART_LSR_TEMT)) == (UART_LSR_THRE | UART_LSR_TEMT);
+ return (LPC_UART0->LSR & (UART_LSR_THRE | UART_LSR_TEMT)) == (UART_LSR_THRE | UART_LSR_TEMT);
}
void uart0TxFlush(void)
{
- UART0->FCR |= UART_FCR_CLEAR_XMIT; // clear the TX fifo
+ LPC_UART0->FCR |= UART_FCR_CLEAR_XMIT; // clear the TX fifo
}
/* Returns: character on success, -1 if no character is available */
int uart0Getch(void)
{
- if (UART0->LSR & UART_LSR_DR) // check if character is available
- return UART0->RBR; // return character
+ if (LPC_UART0->LSR & UART_LSR_DR) // check if character is available
+ return LPC_UART0->RBR; // return character
return -1;
}
/* Returns: character on success, waits */
int uart0GetchW(void)
{
- while ( !(UART0->LSR & UART_LSR_DR) ); // wait for character
- return UART0->RBR; // return character
+ while ( !(LPC_UART0->LSR & UART_LSR_DR) ); // wait for character
+ return LPC_UART0->RBR; // return character
}