+/rtos
git clone https://git.julia.blue/+/rtos
fa38042
master
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 () { 22do { 23fifo_try_put (rxfifo ,ROM_UARTCharGet (UART0_BASE )); 24 }while (ROM_UARTCharsAvail (UART0_BASE )); 25} 26 27static void sw_to_hw_fifo () { 28uint8_t temp ; 29while (ROM_UARTSpaceAvail (UART0_BASE )) { 30if (!fifo_try_get (txfifo ,& temp )) { 31break ; 32 } 33ROM_UARTCharPutNonBlocking (UART0_BASE ,temp ); 34 } 35} 36 37void uart0_handler (void ) { 38uint32_t source = ROM_UARTIntStatus (UART0_BASE , false); 39if (source & (UART_INT_RX |UART_INT_RT )) { 40ROM_UARTIntClear (UART0_BASE ,UART_INT_RX |UART_INT_RT ); 41hw_to_sw_fifo (); 42 } 43if (source & UART_INT_TX ) { 44ROM_UARTIntClear (UART0_BASE ,UART_INT_TX ); 45sw_to_hw_fifo (); 46 } 47} 48 49void uart_init (void ) { 50txfifo = fifo_new (128 ); 51rxfifo = fifo_new (128 ); 52ROM_SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOA ); 53ROM_SysCtlPeripheralEnable (SYSCTL_PERIPH_UART0 ); 54ROM_GPIOPinConfigure (GPIO_PA0_U0RX ); 55ROM_GPIOPinConfigure (GPIO_PA1_U0TX ); 56ROM_GPIOPinTypeUART (GPIO_PORTA_BASE ,GPIO_PIN_1 |GPIO_PIN_0 ); 57ROM_UARTConfigSetExpClk (UART0_BASE ,ROM_SysCtlClockGet (),115200 , 58UART_CONFIG_WLEN_8 |UART_CONFIG_STOP_ONE | 59UART_CONFIG_PAR_NONE ); 60ROM_UARTFIFOEnable (UART0_BASE ); 61ROM_IntPrioritySet (INT_UART0 ,0 <<5 );// priority is high 3 bits 62ROM_UARTFIFOLevelSet (UART0_BASE ,UART_FIFO_TX1_8 ,UART_FIFO_RX1_8 ); 63#ifdef USE_OUTPUT_BUFFER 64ROM_UARTIntEnable (UART0_BASE ,UART_INT_TX |UART_INT_RX |UART_INT_RT ); 65#else 66ROM_UARTIntEnable (UART0_BASE ,UART_INT_RX |UART_INT_RT ); 67#endif 68 69ROM_IntEnable (INT_UART0 ); 70ROM_UARTEnable (UART0_BASE ); 71} 72 73void uart_change_speed (uint32_t baud ) { 74ROM_UARTConfigSetExpClk (UART0_BASE ,ROM_SysCtlClockGet (),baud , 75UART_CONFIG_WLEN_8 |UART_CONFIG_STOP_ONE | 76UART_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 82if (fifo_empty (txfifo )&& ROM_UARTCharPutNonBlocking (UART0_BASE ,x )) { 83return true; 84 } 85while (fifo_full (txfifo )) {OS_Suspend (); } 86uint32_t crit = start_critical (); 87fifi_try_put (txfifo ,x ); 88end_critical (crit ); 89sw_to_hw_fifo (); 90return true; 91#else 92uint32_t blocking_crit = start_critical (); 93while (!ROM_UARTCharPutNonBlocking (UART0_BASE ,x )) {} 94end_critical (blocking_crit ); 95return true; 96#endif 97} 98 99char getchar (void ) { 100uint8_t temp ; 101while (!fifo_try_get (rxfifo ,& temp )) {OS_Suspend (); }; 102return temp ; 103} 104 105void uart_puts (const char * str ) { 106while (* str ) {uart_putchar (* str ++ ); } 107uart_putchar ('\n' ); 108uart_putchar ('\r' ); 109} 110 111uint16_t gets (char * str ,uint16_t max ) { 112char temp = '\0' ; 113uint16_t count = 0 ; 114while (temp != '\n' && temp != '\r' && max -- ) { 115temp = getchar (); 116str [count ++ ]= temp ; 117 } 118str [count ]= '\0' ; 119return count ; 120} 121 122uint16_t readline (char * str ,uint16_t max ) { 123int received = 0 ; 124char current = '\0' ; 125while (max -- ) { 126// TODO: left/right, tab, history 127switch (current = getchar ()) { 128case '\n' : 129case '\r' : 130uart_putchar ('\n' ); 131uart_putchar ('\r' ); 132* str = '\0' ; 133return ++ received ; 134case 127 : 135if (received > 0 ) { 136uart_putchar ('\b' ); 137uart_putchar (' ' ); 138uart_putchar ('\b' ); 139-- str ; 140-- received ; 141 } 142break ; 143default : 144uart_putchar (current ); 145* str ++ = current ; 146++ received ; 147 } 148 } 149*-- str = '\0' ; 150return received ; 151}