code clean up!
[my-code/arm.git] / betty / uart.c
diff --git a/betty/uart.c b/betty/uart.c
new file mode 100644 (file)
index 0000000..5760e3b
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * uart.c - uart0/1 api
+ *
+ * author: hackbard@hackdaworld.org
+ *
+ */
+
+#include "uart.h"
+
+/*
+ * functions
+ */
+
+void uart0_init(void) {
+
+       /* select pins 0.0 and 0.1 as tx and rx */
+       PINSEL0=(PINSEL0&~(0xf))|0x05;
+
+       /* configure uart 0 */
+       UART0_FCR=0x07;                 // enable fifo
+       UART0_LCR=0x83;                 // set dlab + word length
+       UART0_DLL=0x04;                 // br: 115200
+       UART0_DLM=0x00; 
+       UART0_LCR=0x03;                 // unset dlab
+}
+
+void uart0_send_string(char *txbuf) {
+
+       int i;
+
+       i=0;
+
+       while(txbuf[i]) {
+               UART0_THR=txbuf[i++];
+               /* flush if tx buffer maximum reached */
+               if(!(i%16))
+                       while(!(UART0_LSR&(1<<6)))
+                               continue;
+       }
+       
+       /* flush if \n and \r do not fit in the tx buffer */
+       if(i>14)
+               while(!(UART0_LSR&(1<<6)))
+                       continue;
+
+       UART0_THR='\n';
+       UART0_THR='\r';
+
+       /* flush uart0 anyways */
+       while(!(UART0_LSR&(1<<6)))
+               continue;
+}
+
+void uart0_send_buf16(u16 *buf,int len) {
+
+       int i;
+
+       i=0;
+
+       for(i=0;i<len/2;i++) {
+               if(!(i%8))
+                       while(!(UART0_LSR&(1<<6)))
+                               continue;
+               UART0_THR=buf[i]&0xff;
+               UART0_THR=(buf[i]>>8)&0xff;
+       }
+}
+
+void uart0_send_buf32(u32 *buf,int len) {
+
+       int i;
+
+       i=0;
+
+       for(i=0;i<len/4;i++) {
+               if(!(i%4))
+                       while(!(UART0_LSR&(1<<6)))
+                               continue;
+               UART0_THR=buf[i]&0xff;
+               UART0_THR=(buf[i]>>8)&0xff;
+               UART0_THR=(buf[i]>>16)&0xff;
+               UART0_THR=(buf[i]>>24)&0xff;
+       }
+}
+
+void uart0_send_byte(u8 send) {
+
+       while(!(UART0_LSR&(1<<5)))
+               continue;
+
+       UART0_THR=send;
+}
+
+u8 uart0_get_byte(void) {
+
+       u8 rx;
+
+       while(!(UART0_LSR&(1<<0)))
+               continue;
+
+       rx=UART0_RBR;
+
+       return rx;
+}
+