// #warning "this is a reduced version of the R O Software code"
+#include <hal_machperiph.h>
#include "uart.h"
#include "serial_reg.h"
-/* on LPC210x: UART0 TX-Pin=P0.2, RX-Pin=P0.1
+/* on LPC17xx: UART0 TX-Pin=P0.2, RX-Pin=P0.3
PINSEL0 has to be set to "UART-Function" = Function "01"
for Pin 0.2 and 0.3 */
-
-#define PINSEL_BITPIN0 4
-#define PINSEL_BITPIN1 8
+
+#define PINSEL_BIT_TXD0 (2*2)
+#define PINSEL_BIT_RXD0 (3*2)
#define PINSEL_FIRST_ALT_FUNC 1
#define PINSEL_SECOND_ALT_FUNC 2
-// Values of Bits 0-3 in PINSEL to activate UART0
-#define UART0_PINSEL ((PINSEL_FIRST_ALT_FUNC<<PINSEL_BITPIN0)|(PINSEL_FIRST_ALT_FUNC<<PINSEL_BITPIN1))
-// Mask of Bits 0-4
-#define UART0_PINMASK (0x000000F0) /* PINSEL0 Mask for UART0 */
+// Values of Bits 4-7 in PINSEL to activate UART0
+#define UART0_PINSEL ((PINSEL_FIRST_ALT_FUNC<<PINSEL_BIT_TXD0)|(PINSEL_FIRST_ALT_FUNC<<PINSEL_BIT_RXD0))
+// Mask of Bits 4-7
+#define UART0_PINMASK ((3<<PINSEL_BIT_TXD0)|(3<<PINSEL_BIT_RXD0))
// U0_LCR devisor latch bit
#define UART0_LCR_DLAB 7
* fmode - see typical fmodes (uart.h)
* NOTE: uart0Init(UART_BAUD(9600), UART_8N1, UART_FIFO_8);
*/
-void uart0Init(uint16_t baud, uint8_t mode, uint8_t fmode)
+void uart0Init(uint32_t baud, uint8_t mode, uint8_t fmode)
{
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
//UART0->IIR = 0x00; // clear interrupt ID register
//UART0->LSR = 0x00; // clear line status register
+ baud *= 16;
+ 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; // set for baud low byte
- UART0->DLM = (uint8_t)(baud >> 8); // set for baud high byte
-
+ UART0->DLL = (uint8_t)baud_div; // set for baud low byte
+ 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
///////////////////////////////////////////////////////////////////////////////
-// use the following macros to determine the 'baud' parameter values
-// for uart0Init() and uart1Init()
-// CAUTION - 'baud' SHOULD ALWAYS BE A CONSTANT or
-// a lot of code will be generated.
-// Baud-Rate is calculated based on pclk (VPB-clock)
-// the devisor must be 16 times the desired baudrate
-#define UART_BAUD(baud) (uint16_t)((PCLK / ((baud) * 16.0)) + 0.5)
+// Prehistoric solutions slect baudrates according to some magic constants
+// Use SI defined Hz/Baud units for selection of baudrate directly for
+// uart0Init() and uart1Init()
+#define UART_BAUD(baud) (baud)
///////////////////////////////////////////////////////////////////////////////
// Definitions for typical UART 'baud' settings
#define UART_FIFO_8 (uint8_t)(UART_FCR_ENABLE_FIFO + UART_FCR_TRIGGER_8)
#define UART_FIFO_14 (uint8_t)(UART_FCR_ENABLE_FIFO + UART_FCR_TRIGGER_14)
-void uart0Init(uint16_t baud, uint8_t mode, uint8_t fmode);
+void uart0Init(uint32_t baud, uint8_t mode, uint8_t fmode);
int uart0Putch(int ch);
uint16_t uart0Space(void);
const char *uart0Puts(const char *string);