Advertisement
Guest User

serial_lld.h

a guest
Mar 23rd, 2017
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.52 KB | None | 0 0
  1. /*
  2.     ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
  3.  
  4.     Licensed under the Apache License, Version 2.0 (the "License");
  5.     you may not use this file except in compliance with the License.
  6.     You may obtain a copy of the License at
  7.  
  8.         http://www.apache.org/licenses/LICENSE-2.0
  9.  
  10.     Unless required by applicable law or agreed to in writing, software
  11.     distributed under the License is distributed on an "AS IS" BASIS,
  12.     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.     See the License for the specific language governing permissions and
  14.     limitations under the License.
  15. */
  16.  
  17. /**
  18.  * @file    AVR/serial_lld.h
  19.  * @brief   AVR low level serial driver header.
  20.  *
  21.  * @addtogroup SERIAL
  22.  * @{
  23.  */
  24.  
  25. #ifndef _SERIAL_LLD_H_
  26. #define _SERIAL_LLD_H_
  27.  
  28. #if HAL_USE_SERIAL || defined(__DOXYGEN__)
  29.  
  30. /*===========================================================================*/
  31. /* Driver constants.                                                         */
  32. /*===========================================================================*/
  33.  
  34. /*===========================================================================*/
  35. /* Driver pre-compile time settings.                                         */
  36. /*===========================================================================*/
  37.  
  38. /**
  39.  * @brief   USART0 driver enable switch.
  40.  * @details If set to @p TRUE the support for USART0 is included.
  41.  * @note    The default is @p FALSE.
  42.  */
  43. #if !defined(AVR_SERIAL_USE_USART0) || defined(__DOXYGEN__)
  44.   #define AVR_SERIAL_USE_USART0              TRUE
  45. #endif
  46.  
  47. /**
  48.  * @brief   USART1 driver enable switch.
  49.  * @details If set to @p TRUE the support for USART1 is included.
  50.  * @note    The default is @p TRUE.
  51.  */
  52. #if !defined(AVR_SERIAL_USE_USART1) || defined(__DOXYGEN__)
  53. #define AVR_SERIAL_USE_USART1              TRUE
  54. #endif
  55.  
  56. /**
  57.  * @brief   Software Serial driver enable switch.
  58.  * @details If set to @p TRUE the support for Software Serial is included.
  59.  * @note    The default is @p FALSE.
  60.  */
  61. #if !defined(AVR_SERIAL_USE_USARTS) || defined(__DOXYGEN__)
  62.   #define AVR_SERIAL_USE_USARTS              FALSE
  63.   #if !defined(AVR_SDS_USE_INT0) || defined(__DOXYGEN__)
  64.     #define AVR_SDS_USE_INT0 TRUE
  65.   #endif
  66. #endif
  67.  
  68. /*===========================================================================*/
  69. /* Derived constants and error checks.                                       */
  70. /*===========================================================================*/
  71.  
  72. /*===========================================================================*/
  73. /* Driver data structures and types.                                         */
  74. /*===========================================================================*/
  75.  
  76. /**
  77.  * @brief   AVR Serial Driver configuration structure.
  78.  * @details An instance of this structure must be passed to @p sdStart()
  79.  *          in order to configure and start a serial driver operations.
  80.  */
  81. typedef struct {
  82.   /**
  83.    * @brief Initialization value for the BRR register.
  84.    */
  85.   uint16_t                  sc_brr;
  86.   /**
  87.    * @brief Number of bits per character (USART_CHAR_SIZE_5 to USART_CHAR_SIZE_9).
  88.    */
  89.   uint8_t                   sc_bits_per_char;
  90.   /**
  91.    * @brief Top value of OCR2A for soft serial.
  92.    */
  93.   uint8_t                   sc_ocr2a;
  94.   /**
  95.    * @brief Bits of TC2 Control Register which selects divider for soft serial.
  96.    */
  97.   uint8_t                   sc_tccr2b_div;
  98. } SerialConfig;
  99.  
  100.  
  101. /**
  102.  * @brief   @p SerialDriver specific data.
  103.  */
  104. #define _serial_driver_data                                                 \
  105.   _base_asynchronous_channel_data                                           \
  106.   /* Driver state.*/                                                        \
  107.   sdstate_t                 state;                                          \
  108.   /* Input queue.*/                                                         \
  109.   input_queue_t             iqueue;                                         \
  110.   /* Output queue.*/                                                        \
  111.   output_queue_t            oqueue;                                         \
  112.   /* Input circular buffer.*/                                               \
  113.   uint8_t                   ib[SERIAL_BUFFERS_SIZE];                        \
  114.   /* Output circular buffer.*/                                              \
  115.   uint8_t                   ob[SERIAL_BUFFERS_SIZE];                        \
  116.   /* End of the mandatory fields.*/
  117.  
  118. /*===========================================================================*/
  119. /* Driver macros.                                                            */
  120. /*===========================================================================*/
  121.  
  122. /**
  123.  * @brief   Macro for baud rate computation.
  124.  * @note    Make sure the final baud rate is within tolerance.
  125.  */
  126. #define UBRR(b)     (((F_CPU / b) >> 4) - 1)
  127.  
  128. /**
  129.  * @brief   Macro for baud rate computation when U2Xn == 1.
  130.  * @note    Make sure the final baud rate is within tolerance.
  131.  */
  132. #define UBRR2x(b)    (((F_CPU / b) >> 3) - 1)
  133.  
  134. /**
  135. * @brief   Macro for baud rate computation.
  136. * @note    Make sure the final baud rate is within tolerance.
  137. * @note    This version uses floating point math for greater accuracy.
  138. */
  139. #define UBRR_F(b)   ((((double) F_CPU / (double) b) / 16.0) - 0.5)
  140.  
  141. /**
  142. * @brief   Macro for baud rate computation when U2Xn == 1.
  143. * @note    Make sure the final baud rate is within tolerance.
  144. * @note    This version uses floating point math for greater accuracy.
  145. */
  146. #define UBRR2x_F(b)  ((((double) F_CPU / (double) b) / 8.0) - 0.5)
  147.  
  148. #define USART_CHAR_SIZE_5      0
  149. #define USART_CHAR_SIZE_6      1
  150. #define USART_CHAR_SIZE_7      2
  151. #define USART_CHAR_SIZE_8      3
  152. #define USART_CHAR_SIZE_9      4
  153.  
  154. /*===========================================================================*/
  155. /* External declarations.                                                    */
  156. /*===========================================================================*/
  157.  
  158. #if AVR_SERIAL_USE_USART0 && !defined(__DOXYGEN__)
  159. extern SerialDriver SD1;
  160. #endif
  161. #if AVR_SERIAL_USE_USART1 && !defined(__DOXYGEN__)
  162. extern SerialDriver SD2;
  163. #endif
  164. #if AVR_SERIAL_USE_USARTS && !defined(__DOXYGEN__)
  165. extern SerialDriver SDS;
  166. #endif
  167.  
  168. #ifdef __cplusplus
  169. extern "C" {
  170. #endif
  171.   void sd_lld_init(void);
  172.   void sd_lld_start(SerialDriver *sdp, const SerialConfig *config);
  173.   void sd_lld_stop(SerialDriver *sdp);
  174. #ifdef __cplusplus
  175. }
  176. #endif
  177.  
  178. #endif /* HAL_USE_SERIAL */
  179.  
  180. #endif /* _SERIAL_LLD_H_ */
  181.  
  182. /** @} */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement