X-Git-Url: https://hackdaworld.org/gitweb/?a=blobdiff_plain;f=betty%2Fuart.c;fp=betty%2Fuart.c;h=5760e3b45dd4467ec29b680ee6f26286ee1022b1;hb=b4ca71fc214ba3c58cec25661ba1f81cf7b1b871;hp=0000000000000000000000000000000000000000;hpb=d6814c7408b23db8a441f731b44d4b638df24c64;p=my-code%2Farm.git diff --git a/betty/uart.c b/betty/uart.c new file mode 100644 index 0000000..5760e3b --- /dev/null +++ b/betty/uart.c @@ -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>8)&0xff; + } +} + +void uart0_send_buf32(u32 *buf,int len) { + + int i; + + i=0; + + for(i=0;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; +} +