ChibiOS/RT
2.5.1
serial_lld.h
Go to the documentation of this file.
00001 /*
00002     ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
00003                  2011,2012 Giovanni Di Sirio.
00004 
00005     This file is part of ChibiOS/RT.
00006 
00007     ChibiOS/RT is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     ChibiOS/RT is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 /**
00022  * @file    AVR/serial_lld.h
00023  * @brief   AVR low level serial driver header.
00024  *
00025  * @addtogroup SERIAL
00026  * @{
00027  */
00028 
00029 #ifndef _SERIAL_LLD_H_
00030 #define _SERIAL_LLD_H_
00031 
00032 #if HAL_USE_SERIAL || defined(__DOXYGEN__)
00033 
00034 /*===========================================================================*/
00035 /* Driver constants.                                                         */
00036 /*===========================================================================*/
00037 
00038 /*===========================================================================*/
00039 /* Driver pre-compile time settings.                                         */
00040 /*===========================================================================*/
00041 
00042 /**
00043  * @brief   USART0 driver enable switch.
00044  * @details If set to @p TRUE the support for USART0 is included.
00045  * @note    The default is @p FALSE.
00046  */
00047 #if !defined(USE_AVR_USART0) || defined(__DOXYGEN__)
00048 #define USE_AVR_USART0              TRUE
00049 #endif
00050 
00051 /**
00052  * @brief   USART1 driver enable switch.
00053  * @details If set to @p TRUE the support for USART1 is included.
00054  * @note    The default is @p TRUE.
00055  */
00056 #if !defined(USE_AVR_USART1) || defined(__DOXYGEN__)
00057 #define USE_AVR_USART1              TRUE
00058 #endif
00059 
00060 /*===========================================================================*/
00061 /* Derived constants and error checks.                                       */
00062 /*===========================================================================*/
00063 
00064 /*===========================================================================*/
00065 /* Driver data structures and types.                                         */
00066 /*===========================================================================*/
00067 
00068 /**
00069  * @brief   AVR Serial Driver configuration structure.
00070  * @details An instance of this structure must be passed to @p sdStart()
00071  *          in order to configure and start a serial driver operations.
00072  */
00073 typedef struct {
00074   /**
00075    * @brief Initialization value for the BRR register.
00076    */
00077   uint16_t                  sc_brr;
00078   /**
00079    * @brief Number of bits per character (USART_CHAR_SIZE_5 to USART_CHAR_SIZE_9).
00080    */
00081   uint8_t                   sc_bits_per_char;
00082 } SerialConfig;
00083 
00084 /**
00085  * @brief   @p SerialDriver specific data.
00086  */
00087 #define _serial_driver_data                                                 \
00088   _base_asynchronous_channel_data                                           \
00089   /* Driver state.*/                                                        \
00090   sdstate_t                 state;                                          \
00091   /* Input queue.*/                                                         \
00092   InputQueue                iqueue;                                         \
00093   /* Output queue.*/                                                        \
00094   OutputQueue               oqueue;                                         \
00095   /* Input circular buffer.*/                                               \
00096   uint8_t                   ib[SERIAL_BUFFERS_SIZE];                        \
00097   /* Output circular buffer.*/                                              \
00098   uint8_t                   ob[SERIAL_BUFFERS_SIZE];                        \
00099   /* End of the mandatory fields.*/
00100 
00101 /*===========================================================================*/
00102 /* Driver macros.                                                            */
00103 /*===========================================================================*/
00104 
00105 /**
00106  * @brief   Macro for baud rate computation.
00107  * @note    Make sure the final baud rate is within tolerance.
00108  */
00109 #define UBRR(b)     (((F_CPU / b) >> 4) - 1)
00110 
00111 /**
00112  * @brief   Macro for baud rate computation when U2Xn == 1.
00113  * @note    Make sure the final baud rate is within tolerance.
00114  */
00115 #define UBRR2x(b)    (((F_CPU / b) >> 3) - 1)
00116 
00117 /**
00118 * @brief   Macro for baud rate computation.
00119 * @note    Make sure the final baud rate is within tolerance.
00120 * @note    This version uses floating point math for greater accuracy.
00121 */
00122 #define UBRR_F(b)   ((((double) F_CPU / (double) b) / 16.0) - 0.5)
00123 
00124 /**
00125 * @brief   Macro for baud rate computation when U2Xn == 1.
00126 * @note    Make sure the final baud rate is within tolerance.
00127 * @note    This version uses floating point math for greater accuracy.
00128 */
00129 #define UBRR2x_F(b)  ((((double) F_CPU / (double) b) / 8.0) - 0.5)
00130 
00131 #define USART_CHAR_SIZE_5      0
00132 #define USART_CHAR_SIZE_6      1
00133 #define USART_CHAR_SIZE_7      2
00134 #define USART_CHAR_SIZE_8      3
00135 #define USART_CHAR_SIZE_9      4
00136 
00137 /*===========================================================================*/
00138 /* External declarations.                                                    */
00139 /*===========================================================================*/
00140 
00141 #if USE_AVR_USART0 && !defined(__DOXYGEN__)
00142 extern SerialDriver SD1;
00143 #endif
00144 #if USE_AVR_USART1 && !defined(__DOXYGEN__)
00145 extern SerialDriver SD2;
00146 #endif
00147 
00148 #ifdef __cplusplus
00149 extern "C" {
00150 #endif
00151   void sd_lld_init(void);
00152   void sd_lld_start(SerialDriver *sdp, const SerialConfig *config);
00153   void sd_lld_stop(SerialDriver *sdp);
00154 #ifdef __cplusplus
00155 }
00156 #endif
00157 
00158 #endif /* HAL_USE_SERIAL */
00159 
00160 #endif /* _SERIAL_LLD_H_ */
00161 
00162 /** @} */