diff options
| author | Otto Mattik <otto@mattik.org> | 2021-07-08 18:10:55 +0200 |
|---|---|---|
| committer | Otto Mattik <otto@mattik.org> | 2021-07-08 18:10:55 +0200 |
| commit | da34d97efb21719b2b332f8c60b2750d11bcde1f (patch) | |
| tree | 2de9fe89f6d79b8ebfcde64c5e86204e904aedf2 /uart.c | |
| download | armen-da34d97efb21719b2b332f8c60b2750d11bcde1f.tar.gz armen-da34d97efb21719b2b332f8c60b2750d11bcde1f.zip | |
Diffstat (limited to 'uart.c')
| -rw-r--r-- | uart.c | 161 |
1 files changed, 161 insertions, 0 deletions
@@ -0,0 +1,161 @@ +/************************************************************************/ +/* */ +/* File: uart.c */ +/* Description: simple wrapper to serial line device(s). */ +/* Version: 1.0 */ +/* Author: Otto Mattik */ +/* */ +/* (C)Copyright Otto Mattik 2014-2021. */ +/* */ +/* This file is a part of 'armen' (a tiny operating system). */ +/* 'armen' is distributed under the CeCILL-V2.1 licence. For more */ +/* details about this licence, please visit the website cecill.info */ +/* */ +/************************************************************************/ + +#ifndef F_CPU + #error "cpu clock frequency is not defined" +#endif + +#include <stdint.h> +#include <stdarg.h> +#include "core.h" + +#ifdef __AVR_ARCH__ + #include "avr/uart.h" +#endif + +#if defined(UART) && (UART > 0) && (ARMEN_UART_MAX != 0) + + #if (UART > ARMEN_UART_MAX) + #undef UART + #define UART ARMEN_UART_MAX + #endif + #define ARMEN_UARTS UART + + #ifndef UART_RX_BUFFER_SIZE + #define UART_RX_BUFFER_SIZE 4 + #endif + + static struct { + int8_t (*open)(uint8_t,uint8_t); + int8_t (*close)(uint8_t); + int8_t (*read)(uint8_t,uint8_t*); + int8_t (*write)(uint8_t,uint8_t); + #ifdef UART_IOCTL + int8_t (*ioctl)(uint8_t,uint8_t,va_list); + #endif + } devices[ARMEN_UARTS]; + + #ifdef __AVR_ARCH__ + #define AVR_UART_C + #include "avr/uart.c" + #endif + + int8_t uart_open( uint8_t device, uint8_t config ) + { + #ifdef CHECK_PARAM + if( device >= ARMEN_UARTS ) + return( -1 ); + #endif + return( devices[device].open( device, config ) ); + } + + int8_t uart_close( uint8_t device ) + { + #ifdef CHECK_PARAM + if( device >= ARMEN_UARTS ) + return( -1 ); + #endif + return( devices[device].close( device ) ); + } + + int8_t uart_read( uint8_t device, uint8_t* byte ) + { + #ifdef CHECK_PARAM + if( device >= ARMEN_UARTS ) + return( -1 ); + #endif + return( devices[device].read( device, byte ) ); + } + + int8_t uart_write( uint8_t device, uint8_t byte ) + { + #ifdef CHECK_PARAM + if( device >= ARMEN_UARTS ) + return( -1 ); + #endif + return( devices[device].write( device, byte ) ); + } + + #ifdef UART_IOCTL + int8_t uart_ioctl( uint8_t device, uint8_t request, ... ) + { + int ret; + va_list args; + + #ifdef CHECK_PARAM + if( device >= ARMEN_UARTS ) + return( -1 ); + #endif + va_start( args, request ); + ret = devices[device].ioctl( device, request, args ); + va_end( args ); + return( ret ); + } + #endif + + char uart_getchar( uint8_t device ) + { + char c; + + #ifdef CHECK_PARAM + if( device >= ARMEN_UARTS ) + return( -1 ); + #endif + + if( devices[device].read( device, &c ) < 0 ) + c = -1; + return( (char)c ); + } + + void uart_putchar( uint8_t device, char c ) + { + #ifdef CHECK_PARAM + if( device < ARMEN_UARTS ) + #endif + devices[device].write( device, c ); + } + + void uart_putstr( uint8_t device, char* s ) + { + #ifdef CHECK_PARAM + if( device < ARMEN_UARTS ) + #endif + while( *s != 0 ) + devices[device].write( device, *s++ ); + } + + #ifdef ARDUINO + #include <stddef.h> + + extern int vsnprintf(char *__s, size_t __n, const char *__fmt, va_list ap); + + void uart_printf( uint8_t device, char* fmt, ... ) + { + va_list args; + static char buffer[80]; + + #ifdef CHECK_PARAM + if( device < ARMEN_UARTS ) + #endif + { + va_start( args, fmt ); + vsnprintf( buffer, sizeof( buffer ), fmt, args ); + va_end( args ); + uart_putstr( device, buffer ); + } + } + #endif + +#endif |
