ser.c File Reference

Buffered serial I/O driver. More...

#include "ser.h"
#include "wdt.h"
#include "timer.h"
#include "ser_p.h"
#include "cfg/cfg_ser.h"
#include "cfg/cfg_kern.h"
#include <cfg/debug.h>
#include <mware/formatwr.h>
#include <cpu/power.h>
#include <string.h>

Go to the source code of this file.


Functions

static int ser_putchar (int c, struct Serial *port)
 Insert c in tx FIFO buffer.
static int ser_getchar (struct Serial *port)
 Fetch a character from the rx FIFO buffer.
int ser_getchar_nowait (struct Serial *fd)
 Fetch a character from the rx FIFO buffer.
static size_t ser_read (struct KFile *fd, void *_buf, size_t size)
 Read at most size bytes from port and put them in buf.
static size_t ser_write (struct KFile *fd, const void *_buf, size_t size)
 Write a buffer to serial.
void ser_resync (struct Serial *fd, mtime_t delay)
 Discard input to resynchronize with remote end.
void ser_purge (struct Serial *fd)
 Flush both the RX and TX buffers.
void ser_purgeRx (struct Serial *fd)
 Flush RX buffer.
void ser_purgeTx (struct Serial *fd)
 Flush TX buffer.
static int ser_flush (struct KFile *fd)
 Wait until all pending output is completely transmitted to the other end.
static struct Serialser_open (struct Serial *fd, unsigned int unit)
 Initialize a serial port.
static int ser_close (struct KFile *fd)
 Clean up serial port, disabling the associated hardware.
static struct KFileser_reopen (struct KFile *fd)
 Reopen serial port.
void ser_init (struct Serial *fds, unsigned int unit)
 Init serial driver for unit.
static size_t spimaster_read (struct KFile *fd, void *_buf, size_t size)
 Read data from SPI bus.
static size_t spimaster_write (struct KFile *fd, const void *buf, size_t size)
 Write data to SPI bus.
void spimaster_init (Serial *fds, unsigned int unit)
 Init SPI serial driver unit in master mode.

Detailed Description

Buffered serial I/O driver.

The serial rx interrupt buffers incoming data in a software FIFO to decouple the higher level protocols from the line speed. Outgoing data is buffered as well for better performance. This driver is not optimized for best performance, but it has proved to be fast enough to handle transfer rates up to 38400bps on a 16MHz 80196.

MODULE CONFIGURATION

  • CONFIG_SER_HWHANDSHAKE - set to 1 to enable RTS/CTS handshake. Support is incomplete/untested.
  • CONFIG_SER_TXTIMEOUT - Enable software serial transmission timeouts
Version:
Id
ser.c 1726 2008-08-27 14:18:16Z batt
Author:
Bernie Innocenti <bernie@codewiz.org>

Definition in file ser.c.


Function Documentation

static int ser_flush ( struct KFile fd  )  [static]

Wait until all pending output is completely transmitted to the other end.

Note:
The current implementation only checks the software transmission queue. Any hardware FIFOs are ignored.

Definition at line 336 of file ser.c.

static int ser_getchar ( struct Serial port  )  [static]

Fetch a character from the rx FIFO buffer.

Note:
This function will switch out the calling process if the rx buffer is empty. If the buffer is empty and port->rxtimeout is 0 return EOF immediatly.
Returns:
EOF on error or timeout, c otherwise.

Definition at line 141 of file ser.c.

int ser_getchar_nowait ( struct Serial fd  ) 

Fetch a character from the rx FIFO buffer.

If the buffer is empty, ser_getchar_nowait() returns EOF immediatly.

Note:
Deprecated, use ser_getchar with rx_timeout set to 0.

Definition at line 184 of file ser.c.

static struct Serial* ser_open ( struct Serial fd,
unsigned int  unit 
) [static, read]

Initialize a serial port.

Parameters:
fd KFile Serial struct interface.
unit Serial unit to open. Possible values are architecture dependant.

Definition at line 357 of file ser.c.

static int ser_putchar ( int  c,
struct Serial port 
) [static]

Insert c in tx FIFO buffer.

Note:
This function will switch out the calling process if the tx buffer is full. If the buffer is full and port->txtimeout is 0 return EOF immediatly.
Returns:
EOF on error or timeout, c otherwise.

Definition at line 95 of file ser.c.

static size_t ser_read ( struct KFile fd,
void *  _buf,
size_t  size 
) [static]

Read at most size bytes from port and put them in buf.

Returns:
number of bytes actually read.

Definition at line 200 of file ser.c.

void ser_resync ( struct Serial fd,
mtime_t  delay 
)

Discard input to resynchronize with remote end.

Discard incoming data until the port stops receiving characters for at least delay milliseconds.

Note:
Serial errors are reset before and after executing the purge.

Definition at line 258 of file ser.c.

static size_t ser_write ( struct KFile fd,
const void *  _buf,
size_t  size 
) [static]

Write a buffer to serial.

Returns:
0 if OK, EOF in case of error.

Definition at line 225 of file ser.c.

void spimaster_init ( Serial fds,
unsigned int  unit 
)

Init SPI serial driver unit in master mode.

This interface implements the SPI master protocol over a serial SPI driver. This is needed because normal serial driver send/receive data at the same time. SPI slaves like memories and other peripherals first receive and *then* send response back instead. To achieve this, when we are master and we are *sending*, we have to discard all incoming data. Then, when we want to receive, we must write fake data to SPI to trigger slave devices.

Definition at line 505 of file ser.c.

static size_t spimaster_read ( struct KFile fd,
void *  _buf,
size_t  size 
) [static]

Read data from SPI bus.

Since we are master, we have to trigger slave by sending fake chars on the bus.

Definition at line 453 of file ser.c.