+/rtos

git clone https://git.julia.blue/+/rtos

Joseph RyanFinish remote interpreter fixesfa38042

master
4.1 KiB151 linesraw
1#include "OS.h"
2#include "fifo.h"
3#include "interrupts.h"
4#include "std.h"
5#include "tivaware/gpio.h"
6#include "tivaware/hw_ints.h"
7#include "tivaware/hw_memmap.h"
8#include "tivaware/pin_map.h"
9#include "tivaware/rom.h"
10#include "tivaware/sysctl.h"
11#include "tivaware/uart.h"
12#include <stdarg.h>
13#include <stdint.h>
14
15// Speeds up transmission but annoying when debugging
16// #define USE_OUTPUT_BUFFER
17
18static FIFO* txfifo;
19static FIFO* rxfifo;
20
21static void hw_to_sw_fifo() {
22    do {
23        fifo_try_put(rxfifo, ROM_UARTCharGet(UART0_BASE));
24    } while (ROM_UARTCharsAvail(UART0_BASE));
25}
26
27static void sw_to_hw_fifo() {
28    uint8_t temp;
29    while (ROM_UARTSpaceAvail(UART0_BASE)) {
30        if (!fifo_try_get(txfifo, &temp)) {
31            break;
32        }
33        ROM_UARTCharPutNonBlocking(UART0_BASE, temp);
34    }
35}
36
37void uart0_handler(void) {
38    uint32_t source = ROM_UARTIntStatus(UART0_BASE, false);
39    if (source & (UART_INT_RX | UART_INT_RT)) {
40        ROM_UARTIntClear(UART0_BASE, UART_INT_RX | UART_INT_RT);
41        hw_to_sw_fifo();
42    }
43    if (source & UART_INT_TX) {
44        ROM_UARTIntClear(UART0_BASE, UART_INT_TX);
45        sw_to_hw_fifo();
46    }
47}
48
49void uart_init(void) {
50    txfifo = fifo_new(128);
51    rxfifo = fifo_new(128);
52    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
53    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
54    ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
55    ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
56    ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_1 | GPIO_PIN_0);
57    ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), 115200,
58                            UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
59                                UART_CONFIG_PAR_NONE);
60    ROM_UARTFIFOEnable(UART0_BASE);
61    ROM_IntPrioritySet(INT_UART0, 0 << 5); // priority is high 3 bits
62    ROM_UARTFIFOLevelSet(UART0_BASE, UART_FIFO_TX1_8, UART_FIFO_RX1_8);
63#ifdef USE_OUTPUT_BUFFER
64    ROM_UARTIntEnable(UART0_BASE, UART_INT_TX | UART_INT_RX | UART_INT_RT);
65#else
66    ROM_UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);
67#endif
68
69    ROM_IntEnable(INT_UART0);
70    ROM_UARTEnable(UART0_BASE);
71}
72
73void uart_change_speed(uint32_t baud) {
74    ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), baud,
75                            UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
76                                UART_CONFIG_PAR_NONE);
77}
78
79bool uart_putchar(char x) {
80#ifdef USE_OUTPUT_BUFFER
81    // Skip the buffer and go straight to the hardware fifo if possible
82    if (fifo_empty(txfifo) && ROM_UARTCharPutNonBlocking(UART0_BASE, x)) {
83        return true;
84    }
85    while (fifo_full(txfifo)) { OS_Suspend(); }
86    uint32_t crit = start_critical();
87    fifi_try_put(txfifo, x);
88    end_critical(crit);
89    sw_to_hw_fifo();
90    return true;
91#else
92    uint32_t blocking_crit = start_critical();
93    while (!ROM_UARTCharPutNonBlocking(UART0_BASE, x)) {}
94    end_critical(blocking_crit);
95    return true;
96#endif
97}
98
99char getchar(void) {
100    uint8_t temp;
101    while (!fifo_try_get(rxfifo, &temp)) { OS_Suspend(); };
102    return temp;
103}
104
105void uart_puts(const char* str) {
106    while (*str) { uart_putchar(*str++); }
107    uart_putchar('\n');
108    uart_putchar('\r');
109}
110
111uint16_t gets(char* str, uint16_t max) {
112    char temp = '\0';
113    uint16_t count = 0;
114    while (temp != '\n' && temp != '\r' && max--) {
115        temp = getchar();
116        str[count++] = temp;
117    }
118    str[count] = '\0';
119    return count;
120}
121
122uint16_t readline(char* str, uint16_t max) {
123    int received = 0;
124    char current = '\0';
125    while (max--) {
126        // TODO: left/right, tab, history
127        switch (current = getchar()) {
128        case '\n':
129        case '\r':
130            uart_putchar('\n');
131            uart_putchar('\r');
132            *str = '\0';
133            return ++received;
134        case 127:
135            if (received > 0) {
136                uart_putchar('\b');
137                uart_putchar(' ');
138                uart_putchar('\b');
139                --str;
140                --received;
141            }
142            break;
143        default:
144            uart_putchar(current);
145            *str++ = current;
146            ++received;
147        }
148    }
149    *--str = '\0';
150    return received;
151}