3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this 
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in 
  15   all copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  32  *  \brief Master include file for the USART peripheral driver. 
  34  *  Driver for the USART subsystem on supported USB AVRs. 
  37 /** \ingroup Group_PeripheralDrivers 
  38  *  @defgroup Group_Serial Serial USART Driver - LUFA/Drivers/Peripheral/Serial.h 
  40  *  \section Sec_Dependencies Module Source Dependencies 
  41  *  The following files must be built with any user project that uses this module: 
  42  *    - LUFA/Drivers/Peripheral/Serial.c <i>(Makefile source module name: LUFA_SRC_SERIAL)</i> 
  44  *  \section Sec_ModDescription Module Description 
  45  *  Hardware serial USART driver. This module provides an easy to use driver for 
  46  *  the setup of and transfer of data over the AVR's USART port. 
  48  *  \section Sec_ExampleUsage Example Usage 
  49  *  The following snippet is an example of how this module may be used within a typical 
  53  *      // Initialise the serial USART driver before first use, with 9600 baud (and no double-speed mode) 
  54  *      Serial_Init(9600, false); 
  56  *      // Send a string through the USART 
  57  *      Serial_TxString("Test String\r\n"); 
  59  *      // Receive a byte through the USART 
  60  *      uint8_t DataByte = Serial_RxByte(); 
  71                 #include <avr/pgmspace.h> 
  74                 #include "../../Common/Common.h" 
  75                 #include "../Misc/TerminalCodes.h" 
  77         /* Enable C linkage for C++ Compilers: */ 
  78                 #if defined(__cplusplus) 
  82         /* Public Interface - May be used in end-application: */ 
  84                         /** Macro for calculating the baud value from a given baud rate when the U2X (double speed) bit is 
  87                         #define SERIAL_UBBRVAL(baud)    ((((F_CPU / 16) + (baud / 2)) / (baud)) - 1) 
  89                         /** Macro for calculating the baud value from a given baud rate when the U2X (double speed) bit is 
  92                         #define SERIAL_2X_UBBRVAL(baud) ((((F_CPU / 8) + (baud / 2)) / (baud)) - 1) 
  94                 /* Function Prototypes: */ 
  95                         /** Transmits a given string located in program space (FLASH) through the USART. 
  97                          *  \param[in] FlashStringPtr  Pointer to a string located in program space. 
  99                         void Serial_TxString_P(const char* FlashStringPtr
) ATTR_NON_NULL_PTR_ARG(1); 
 101                         /** Transmits a given string located in SRAM memory through the USART. 
 103                          *  \param[in] StringPtr  Pointer to a string located in SRAM space. 
 105                         void Serial_TxString(const char* StringPtr
) ATTR_NON_NULL_PTR_ARG(1); 
 107                 /* Inline Functions: */ 
 108                         /** Initializes the USART, ready for serial data transmission and reception. This initializes the interface to 
 109                          *  standard 8-bit, no parity, 1 stop bit settings suitable for most applications. 
 111                          *  \param[in] BaudRate     Serial baud rate, in bits per second. 
 112                          *  \param[in] DoubleSpeed  Enables double speed mode when set, halving the sample time to double the baud rate. 
 114                         static inline void Serial_Init(const uint32_t BaudRate
, 
 115                                                        const bool DoubleSpeed
) 
 117                                 UBRR1  
= (DoubleSpeed ? 
SERIAL_2X_UBBRVAL(BaudRate
) : SERIAL_UBBRVAL(BaudRate
)); 
 119                                 UCSR1C 
= ((1 << UCSZ11
) | (1 << UCSZ10
)); 
 120                                 UCSR1A 
= (DoubleSpeed ? 
(1 << U2X1
) : 0); 
 121                                 UCSR1B 
= ((1 << TXEN1
)  | (1 << RXEN1
)); 
 127                         /** Turns off the USART driver, disabling and returning used hardware to their default configuration. */ 
 128                         static inline void Serial_ShutDown(void) 
 140                         /** Indicates whether a character has been received through the USART. 
 142                          *  \return Boolean true if a character has been received, false otherwise. 
 144                         static inline bool Serial_IsCharReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
; 
 145                         static inline bool Serial_IsCharReceived(void) 
 147                                 return ((UCSR1A 
& (1 << RXC1
)) ? 
true : false); 
 150                         /** Transmits a given byte through the USART. 
 152                          *  \param[in] DataByte  Byte to transmit through the USART. 
 154                         static inline void Serial_TxByte(const char DataByte
) ATTR_ALWAYS_INLINE
; 
 155                         static inline void Serial_TxByte(const char DataByte
) 
 157                                 while (!(UCSR1A 
& (1 << UDRE1
))); 
 161                         /** Receives a byte from the USART. This function blocks until a byte has been 
 162                          *  received; if non-blocking behaviour is required, test for a received character 
 163                          *  beforehand with \ref Serial_IsCharReceived(). 
 165                          *  \return Byte received from the USART. 
 167                         static inline char Serial_RxByte(void) ATTR_ALWAYS_INLINE
; 
 168                         static inline char Serial_RxByte(void) 
 170                                 while (!(UCSR1A 
& (1 << RXC1
))); 
 174         /* Disable C linkage for C++ Compilers: */ 
 175                 #if defined(__cplusplus)