Commit 6f97e9a2 authored by Federico Vaga's avatar Federico Vaga Committed by Adam Wujek

liblinux: vuart implemntation

Signed-off-by: Federico Vaga's avatarFederico Vaga <federico.vaga@cern.ch>
parent e1527a28
......@@ -6,7 +6,7 @@
LIB = libwhiterabbit.a
LOBJ := vuart.o
CFLAGS = -Wall -ggdb -O2 -fPIC -I./
CFLAGS = -Wall -ggdb -O2 -fPIC -Werror -I./ -I../include
LDFLAGS = -L. -lwhiterabbit
%: %.c $(LIB)
......
......@@ -7,8 +7,8 @@
struct wr_vuart {
int fd;
unsigned int offset;
void *vuart_mmap;
void *mmap;
volatile struct UART_WB *base;
};
/**
......
......@@ -3,11 +3,18 @@
*/
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <hw/wb_uart.h>
#include "libwhiterabbit.h"
#define MAX_SIZE 0x100000
/**
* It opens a White Rabbit Virtual UART
* @parma[in] resource_path path to the resource file which can be mmapped
......@@ -21,17 +28,44 @@
*/
struct wr_vuart *wr_vuart_open(char *resource_path, unsigned int offset)
{
struct wr_vuart *vuart;
vuart = malloc(sizeof(struct wr_vuart));
if (!vuart)
goto out_alloc;
vuart->fd = open(resource_path, O_RDWR | O_SYNC);
if (vuart->fd <=0)
goto out_open;
vuart->mmap = mmap(NULL, MAX_SIZE & ~(getpagesize() - 1),
PROT_READ | PROT_WRITE,
MAP_SHARED, vuart->fd, 0);
if ((long)vuart->mmap == -1)
goto out_mmap;
vuart->base = vuart->mmap + offset;
return vuart;
out_mmap:
close(vuart->fd);
out_open:
free(vuart);
out_alloc:
return NULL;
}
/**
* It releases the resources allocated by wr_vuart_open(). The vuart will
* not be available anymore
* not be available anymore and the token will be invalidated
* @param[in,out] vuart token from wr_vuart_open()
*/
void wr_vuart_close(struct wr_vuart *vuart)
{
close(vuart->fd);
free(vuart);
}
......@@ -45,7 +79,17 @@ void wr_vuart_close(struct wr_vuart *vuart)
*/
size_t wr_vuart_read(struct wr_vuart *vuart, char *buf, size_t size)
{
return 0;
size_t s = size, n_rx = 0;
int c;
while(s--) {
c = wr_vuart_rx(vuart);
if(c < 0)
return n_rx;
*buf++ = c;
n_rx ++;
}
return n_rx;
}
......@@ -59,7 +103,13 @@ size_t wr_vuart_read(struct wr_vuart *vuart, char *buf, size_t size)
*/
size_t wr_vuart_write(struct wr_vuart *vuart, char *buf, size_t size)
{
return 0;
size_t s = size;
while(s--)
wr_vuart_tx(vuart, *buf++);
return size;
}
......@@ -71,7 +121,9 @@ size_t wr_vuart_write(struct wr_vuart *vuart, char *buf, size_t size)
*/
int wr_vuart_rx(struct wr_vuart *vuart)
{
return 0;
int rdr = vuart->base->RDR;
return (rdr & UART_HOST_RDR_RDY) ? UART_HOST_RDR_DATA_R(rdr) : -1;
}
......@@ -81,4 +133,7 @@ int wr_vuart_rx(struct wr_vuart *vuart)
*/
void wr_vuart_tx(struct wr_vuart *vuart, int data)
{
while(vuart->base->SR & UART_SR_RX_RDY)
;
vuart->base->HOST_TDR = UART_HOST_TDR_DATA_W(data);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment