uart.c
226 lines
| 4.4 KiB
| text/x-c
|
CLexer
r71 | /*------------------------------------------------------------------------------ | |||
-- This file is a part of the libuc, microcontroler library | ||||
-- Copyright (C) 2013, Alexis Jeandet | ||||
-- | ||||
-- This program is free software; you can redistribute it and/or modify | ||||
-- it under the terms of the GNU General Public License as published by | ||||
-- the Free Software Foundation; either version 3 of the License, or | ||||
-- (at your option) any later version. | ||||
-- | ||||
-- This program is distributed in the hope that it will be useful, | ||||
-- but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
-- GNU General Public License for more details. | ||||
-- | ||||
-- You should have received a copy of the GNU General Public License | ||||
-- along with this program; if not, write to the Free Software | ||||
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||||
------------------------------------------------------------------------------- | ||||
-- Author : Alexis Jeandet | ||||
-- Mail : alexis.jeandet@member.fsf.org | ||||
-------------------------------------------------------------------------------*/ | ||||
#include <uart.h> | ||||
#include <gpio.h> | ||||
#include <stdio.h> | ||||
int _uartstrsetpos(streamdevice* device,int pos); | ||||
int _uartstrread(streamdevice* device,void* data,int size, int n); | ||||
int _uartstrwrite(streamdevice* device,void* data,int size, int n); | ||||
streamdevice_ops UART_OPS= | ||||
{ | ||||
.write = &_uartstrwrite, | ||||
.read = &_uartstrread, | ||||
.setpos= &_uartstrsetpos, | ||||
.close = NULL | ||||
}; | ||||
uart_t uartopen(int count) | ||||
{ | ||||
return -1; | ||||
} | ||||
uart_t uartopenandconfig(int count, uint32_t cfg, uint32_t speed, uint32_t TXpin, uint32_t RXpin, uint32_t RTSpin, uint32_t CTSpin) | ||||
{ | ||||
uart_t dev= uartopen(count); | ||||
uartsetconfig(dev,cfg,speed); | ||||
uartsetpins(dev,TXpin,RXpin,RTSpin,CTSpin); | ||||
return dev; | ||||
} | ||||
int uartclose(uart_t uart) | ||||
{ | ||||
return 1; | ||||
} | ||||
int uartsetpins(uart_t uart,uint32_t TXpin,uint32_t RXpin,uint32_t RTSpin,uint32_t CTSpin) | ||||
{ | ||||
if(uart >5)return -1; | ||||
if(uart <0)return -1; | ||||
return 1; | ||||
} | ||||
int uartsetconfig(uart_t uart, uint32_t cfg, uint32_t speed) | ||||
{ | ||||
int res=1; | ||||
uartdisable(uart); | ||||
uartsetspeed(uart,speed); | ||||
uartsetparity(uart,cfg & UARTPARITYMASK); | ||||
uartsetdatabits(uart,cfg & UARTBITSMASK); | ||||
uartsetstopbits(uart,cfg & UARTSTOPBITSMASK); | ||||
uartenable(uart); | ||||
return res; | ||||
} | ||||
int uartenable(uart_t uart) | ||||
{ | ||||
if((uart<6)&&(uart>=0)) | ||||
{ | ||||
return 1; | ||||
} | ||||
return -1; | ||||
} | ||||
int uartdisable(uart_t uart) | ||||
{ | ||||
if((uart<6)&&(uart>=0)) | ||||
{ | ||||
return 1; | ||||
} | ||||
return -1; | ||||
} | ||||
int uartsetspeed(uart_t uart,uint32_t speed) | ||||
{ | ||||
if((uart<6)&&(uart>=0)) | ||||
{ | ||||
return 1; | ||||
} | ||||
return -1; | ||||
} | ||||
int uartsetparity(uart_t uart,uartparity_t parity) | ||||
{ | ||||
if((uart<6)&&(uart>=0)) | ||||
{ | ||||
return 1; | ||||
} | ||||
return -1; | ||||
} | ||||
int uartsetdatabits(uart_t uart,uartbits_t databits) | ||||
{ | ||||
if((uart<6)&&(uart>=0)) | ||||
{ | ||||
return 1; | ||||
} | ||||
return -1; | ||||
} | ||||
int uartsetstopbits(uart_t uart,uartstopbits_t stopbits) | ||||
{ | ||||
if((uart<6)&&(uart>=0)) | ||||
{ | ||||
return 1; | ||||
} | ||||
return -1; | ||||
} | ||||
int uartputc(uart_t uart,char c) | ||||
{ | ||||
if((uart<6)&&(uart>=0)) | ||||
{ | ||||
return 1; | ||||
} | ||||
return -1; | ||||
} | ||||
char uartgetc(uart_t uart) | ||||
{ | ||||
if((uart<6)&&(uart>=0)) | ||||
{ | ||||
return 0; | ||||
} | ||||
return -1; | ||||
} | ||||
int uartputs(uart_t uart,char* s) | ||||
{ | ||||
while (*s) uartputc(uart,*s++); | ||||
return 1; | ||||
} | ||||
int uartgets(uart_t uart,char* s) | ||||
{ | ||||
do | ||||
{ | ||||
(*s) = uartgetc(uart); | ||||
} | ||||
while(*s++); | ||||
return 1; | ||||
} | ||||
int uartputnc(uart_t uart,char* c,int n) | ||||
{ | ||||
int l=0; | ||||
while(l<n) | ||||
{ | ||||
uartputc(uart,*c++); | ||||
l++; | ||||
} | ||||
return n; | ||||
} | ||||
int uartgetnc(uart_t uart,char* c,int n) | ||||
{ | ||||
int l=0; | ||||
while(l<n) | ||||
{ | ||||
*c++=uartgetc(uart); | ||||
l++; | ||||
} | ||||
return n; | ||||
} | ||||
int uartavailiabledata(uart_t uart) | ||||
{ | ||||
if((uart<6)&&(uart>=0)) | ||||
{ | ||||
return 1; | ||||
} | ||||
return -1; | ||||
} | ||||
int _uartstrwrite(streamdevice* device,void* data,int size, int n) | ||||
{ | ||||
return uartputnc((uart_t) device->_stream,(char*) data,size*n); | ||||
} | ||||
int _uartstrread(streamdevice* device,void* data,int size, int n) | ||||
{ | ||||
return uartgetnc((uart_t) device->_stream,(char*) data,size*n); | ||||
} | ||||
int _uartstrsetpos(streamdevice* device,int pos) | ||||
{ | ||||
return 1; | ||||
} | ||||
int uartmkstreamdev(uart_t uart,streamdevice* strdev) | ||||
{ | ||||
strdev->_stream = (UHANDLE)uart; | ||||
strdev->ops = &UART_OPS; | ||||
strdev->streamPt = 0; | ||||
return 1; | ||||
} | ||||