-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.c
72 lines (60 loc) · 1.18 KB
/
serial.c
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
63
64
65
66
67
68
69
70
71
72
#include <sys/stdint.h>
#include <scifreg.h>
#include "serial.h"
char serial_getchar() {
startOver:
if(SHREG_SCSSR2 & 0x9C) {
SHREG_SCSSR2 &= 0x63;
goto startOver;
}
if(SHREG_SCLSR2 & 0x01) {
SHREG_SCLSR2 ^= SHREG_SCLSR2;
SHREG_SCSSR2 &= 0x63;
goto startOver;
}
if(!(SHREG_SCSSR2 & 0x02)) {
goto startOver;
}
char result = SHREG_SCFRDR2;
SHREG_SCSSR2 &= 0xFD;
return result;
}
int serial_getline(char *buf, int len) {
/* TODO: handle backspace */
char c;
int i;
for(i = 0; i < len - 1; i++) {
c = serial_getchar();
serial_putchar(c);
if(c == '\r') {
serial_putchar('\n');
break;
}
buf[i] = c;
}
buf[i] = '\0';
return i;
}
void serial_putchar(const char c) {
/* Spin lock until transmit enable flag is set */
while(!(SHREG_SCSSR2 & 0x20));
/* Load the character into the fifo register */
SHREG_SCFTDR2 = c;
/* Set flags to send byte */
SHREG_SCSSR2 &= 0x9F;
}
void serial_print(const char *string) {
while(*string) {
serial_putchar(*string++);
}
}
void serial_init(void) {
/* Reverse engineered voodoo */
SHREG_SCSCR2 = 0;
SHREG_SCFCR2 = 6;
SHREG_SCSSR2 = 0;
SHREG_SCSCR2 = 2;
SHREG_SCSMR2 = 0;
SHREG_SCFCR2 = 0;
SHREG_SCSCR2 = 0x32;
}