• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

usarts.h

Go to the documentation of this file.
00001 /* usart interface header file */
00002 /* Copyright (C) 2009 Holger Dietze
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation; either version 2 of the License, or
00007  *   (at your option) version 3.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License along
00015  *   with this program; if not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef USARTS_H
00019 #define USARTS_H
00020 
00021 #include <stdint.h>
00022 #include <avr/pgmspace.h>
00023 #include <avr/io.h>
00024 
00025 #include "fifo.h"
00026 
00037 /* USART_FRAME_ERROR and USART_PARITY_ERROR intentionally have
00038  * the same values as _BV(FE0) and _BV(UPE0) in UCSRnA. This
00039  * gives simpler machine code in the interrupt function. */
00041 #define USART_FRAME_ERROR (1<<4)
00042 
00043 #define USART_PARITY_ERROR (1<<2)
00044 
00046 #define USART_NO_RX (1<<7)
00047 
00048 #define USART_TIMESTAMPED (1<<6)
00049 
00052 typedef uint32_t baudrate_t;
00053 
00056 typedef struct usart_rxdata_s {
00057   uint8_t data;
00058   uint8_t flags;
00059 } usart_rxdata_t;
00060 
00063 typedef uint8_t usart_timestamp_t;
00064 
00066 typedef struct usart_control_s {
00068   volatile uint8_t * const base;
00070   fifo_t txbuf;
00072   fifo_t rxbuf;
00074   volatile uint8_t * const prr;
00076   uint8_t const prusart; /* bit mask */
00077   volatile uint8_t tx_empty;
00079   uint8_t ts_trigger;
00081   usart_timestamp_t timestamp;
00083   const char * const name;
00084 } usart_control_t;
00085 
00087 typedef enum {
00088   usart_ext, 
00089   usart_mod, 
00090   USARTS_NUMBER,
00091   usart_none = -1,
00092 } hlog_usart_t;
00093 
00094 /* \brief initialize USART
00095  *
00096  * initializes one usart_control_s structure and powers up the
00097  * USART and, if applicable, the level converter.
00098  * \param which selects which USART to use
00099  * \param txbuf pointer to a FIFO buffer used for sending
00100  * \param txsize size of txbuf
00101  * \param rxbuf pointer to a FIFO buffer used for receiving
00102  * \param rxsize size of rxbuf
00103  * \return pointer to the usart_control_t structure used for the selected
00104  *         USART
00105  *
00106  * If txbuf is NULL, the USART will not be available for sending.
00107  * If rxbuf is NULL, the USART will not be available for receiving.
00108  * 
00109  * There will be one and only one usart_control_s structure for
00110  * each usable USART.
00111  */
00112 usart_control_t * usart_init (hlog_usart_t which,
00113                               fifo_item_t * txbuf, fifo_size_t txsize,
00114                               fifo_item_t * rxbuf, fifo_size_t rxsize);
00115 
00122 baudrate_t usart_set_baudrate (usart_control_t * uc, baudrate_t baud);
00123 
00133 baudrate_t usart_change_baudrate (usart_control_t * uc, int8_t incdec);
00134 
00139 void usart_disable (usart_control_t *);
00146 void usart_power (usart_control_t *, uint8_t onoff);
00147 
00153 void usart_enable (usart_control_t * uc);
00154 
00162 usart_rxdata_t usart_recv (usart_control_t * uc);
00163 
00169 inline uint8_t usart_can_recv (usart_control_t * uc)
00170 {
00171   return fifo_get_buffer (&uc->rxbuf) && !fifo_is_empty(&uc->rxbuf);
00172 }
00173 
00182 void usart_send (usart_control_t * uc, uint8_t b);
00183 
00186 void usart_flush (usart_control_t *uc);
00187 
00192 uint8_t usart_ext_is_connected (void);
00193 
00199 inline uint8_t usart_can_send (usart_control_t * uc)
00200 {
00201   return fifo_get_buffer (&uc->txbuf) && !fifo_is_full(&uc->txbuf);
00202 }
00203 
00206 inline size_t usart_send_msg (usart_control_t * uc, uint8_t nonblock,
00207                               const uint8_t * msg, const size_t msglen)
00208 {
00209   size_t cnt = msglen;
00210   if (cnt)
00211     do {
00212       if (nonblock && !usart_can_send (uc))
00213         break;
00214       usart_send (uc, *msg++);
00215     } while (--cnt);
00216   return msglen - cnt;
00217 }
00218 
00228 inline size_t usart_send_msg_P (usart_control_t * uc, uint8_t nonblock,
00229                               const uint8_t * msg, const size_t msglen)
00230 {
00231   size_t cnt = msglen;
00232   if (cnt)
00233     do {
00234       if (nonblock && !usart_can_send (uc))
00235         break;
00236       usart_send (uc, pgm_read_byte (msg++));
00237     } while (--cnt);
00238   return msglen - cnt;
00239 }
00240 
00243 inline void usart_timestamp_set_trigger (usart_control_t * uc, uint8_t trigger)
00244 {
00245   uc->ts_trigger = trigger;
00246 }
00247 
00250 inline usart_timestamp_t usart_get_timestamp (usart_control_t * uc)
00251 {
00252   return uc->timestamp;
00253 }
00254 
00255 #endif

Generated by  doxygen 1.7.1