]> rtime.felk.cvut.cz Git - sysless.git/commitdiff
LPC17xx UART uart0Init accepts baudrate directly in Hz.
authorPavel Pisa <pisa@cmp.felk.cvut.cz>
Sat, 25 Sep 2010 12:46:21 +0000 (14:46 +0200)
committerPavel Pisa <pisa@cmp.felk.cvut.cz>
Sat, 25 Sep 2010 12:46:21 +0000 (14:46 +0200)
Previous computation with UART_BAUD macro results
in inclusion of floating point library, because
PCLK is not constant. It is based on calculation from
system_frequency.

The actual solution is not ideal when ratio is not exact.
Fraction divider should be utilized in such cases.

Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
arch/arm/mach-lpc17xx/libs/hal/hal_machperiph.h
board/arm/lpc17xx-common/libs/bspbase/uart.c
board/arm/lpc17xx-common/libs/bspbase/uart.h

index 9b3a74bb6eb775090cfa5525d972ec26e23d9ced..0b7c5af12f14121b88ecd7dbff9a2a5bc1b6b9d4 100644 (file)
@@ -2,7 +2,7 @@
 #define _HAL_MACHPERIPH_H
 
 extern unsigned int system_frequency;
-#define PCLK (system_frequency/4)
+#define PCLK ((system_frequency+2)/4)
 
 void system_clock_init(void);
 
index ead1798e6570e87c0d4cb9a303ec9189c798607e..6c07d1f6df6d6e4d68f48804d292b0c03de6a3f1 100644 (file)
 
 // #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
@@ -52,11 +54,14 @@ void uart0Init(uint16_t baud, uint8_t mode, uint8_t fmode)
   //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
index 8e7f0a7e80271d2b4ca90ee732b1615a3db23bd9..77327f9904b69be8711da7d28c803b263fdb94d2 100644 (file)
 
 
 ///////////////////////////////////////////////////////////////////////////////
-// 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
@@ -55,7 +52,7 @@
 #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);