Simple mbed library with macros

Dependents:   SimpleTimer SimpleUART SimpleTimer Stoppuhr1

Revision:
0:aa3c3d1a5918
Child:
3:0b94bf95c552
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/serial.h	Sun Nov 07 11:28:38 2010 +0000
@@ -0,0 +1,52 @@
+#ifndef __SERIAL_H__
+#define __SERIAL_H__
+
+#include "interrupt.h"
+#include "mbed_globals.h"
+
+/** Serial port (Choose UARTn (0,2,3)) **/
+#define UART_BASE LPC_UART0_BASE
+
+/** Registers **/
+// UARTn Receiver Buffer Register
+#define UnRBR 0x00
+// UARTn Transmit Holding Register
+#define UnTHR 0x00
+// UARTn Interrupt Enable Register
+#define UnIER 0x04
+// UARTn Interrupt Identification Register
+#define UnIIR 0x08
+// UARTn Line Status Register
+#define UnLSR 0x14
+
+/** Bits **/
+// RBR Interrupt Enable (UnIER, 14.4.4 p302)
+#define RBR_INT_BIT 0
+// Receiver Data Ready (UnLSR, 14.4.8 p306)
+#define RDR_BIT 0
+// Transmitter Holding Register Empty (UnLSR, 14.4.8 p306)
+#define THRE_BIT 5
+
+/** Macros **/
+#define SERIAL_PUTCHAR(c)           while (GET_BIT_VALUE(UART_BASE + UnLSR, THRE_BIT) == 0); \
+                                    SET_REGISTER8(UART_BASE + UnTHR, c)
+
+#define SERIAL_DATA_TO_READ()       (GET_BIT_VALUE(UART_BASE + UnLSR, RDR_BIT) == 1)
+
+#define SERIAL_GETCHAR()            GET_REGISTER8(UART_BASE + UnRBR)
+
+// See 14.4.5 p303
+inline int SERIAL_CHECK_INTERRUPT(void) {
+    unsigned int serialStatus = GET_REGISTER32(UART_BASE + UnIIR);
+
+    if (serialStatus & 1) // IntStatus, 1 = No Interrupt is pending.
+        return 0;
+
+    serialStatus = (serialStatus >> 1) & 0x3; // IntId, 2 = More than threshold data to read, 6 = Some caracters to read
+    if (serialStatus != 2 && serialStatus != 6)
+        return 0;
+
+    return 1;
+}
+
+#endif
\ No newline at end of file