// Copyright (c) 2013 Matthias Blankertz #include #include "uart.h" static volatile unsigned char *_uart_sreg = (unsigned char *)0x08010021; static volatile unsigned char *_uart_dreg = (unsigned char *)0x08010020; static bool _uart_writefull() { return ((*_uart_sreg&0x2)==0x2); } static bool _uart_readempty() { return ((*_uart_sreg&0x4)==0x4); } void uart_writeb(unsigned char b) { while(_uart_writefull()) {} *_uart_dreg = b; } void uart_writes(const char *str) { while(*str) uart_writeb(*str++); } unsigned char uart_readb() { while(_uart_readempty()) {} return *_uart_dreg; } // Read from uart until a newline is recvd. The recvd line is written into buf // as a 0-terminated string. If more than max bytes are read, the bytes already // read are returned in buf and the function returns false. bool uart_readline(char *buf, int max) { int i = 0; while(i < max-1) { buf[i] = uart_readb(); if(buf[i] == '\n') { buf[i] = '\0'; return true; } ++i; } buf[i] = '\0'; return false; }