aboutsummaryrefslogtreecommitdiff
path: root/src/drivers/uart.c
blob: c5514ce9c3eb630ce538f4a57e2a4e095c0065d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <drivers/uart.h>

#define UART_BUFFER_SIZE 0x100
struct UartBuffer {
	char buffer[UART_BUFFER_SIZE];
	unsigned long roffset;
	unsigned long woffset;
} ubuffer;

void uart_init(void)
{
	ubuffer.roffset = 0;
	ubuffer.woffset = 0;
}

// s = zero-terminated string
void* uart_print(char* s)
{
	char* ptr = s;
	while (1) {
		if (*ptr == 0)
			break;
		ubuffer.buffer[ubuffer.woffset] = *ptr;
		if ((ubuffer.woffset+1)%UART_BUFFER_SIZE == ubuffer.roffset)
			return ptr;
		ubuffer.woffset++;
		ubuffer.woffset %= UART_BUFFER_SIZE;
		ptr += 1;
	}
	return 0;
}

void uart_flush(void)
{
	while (ubuffer.roffset != ubuffer.woffset) {
		uart_char(ubuffer.buffer[ubuffer.roffset++]);
		ubuffer.roffset %= UART_BUFFER_SIZE;
	}
}

void uart_10(unsigned long val)
{
	unsigned long t = val;
	unsigned long c;
	static char buffer[11] = "0000000000\0";
	char* dptr = buffer + 9;
	for(int i = 0; i <= 10; i++) {
		c = t%10;
		*dptr = 0x30 + (c&0xF);
		t /= 10;
		if (t==0)
			break;
		dptr -= 1;
	}
	uart_string(dptr);
}

void uart_hexn(unsigned long c_val)
{
	uart_hex(c_val);
	uart_char('\n');
}