00001 #ifndef __UART0_H__ 00002 #define __UART0_H__ 00003 /* ============================================================================= 00004 00005 Copyright (c) 2006 Pieter Conradie [www.piconomic.co.za] 00006 All rights reserved. 00007 00008 Redistribution and use in source and binary forms, with or without 00009 modification, are permitted provided that the following conditions are met: 00010 00011 * Redistributions of source code must retain the above copyright 00012 notice, this list of conditions and the following disclaimer. 00013 00014 * Redistributions in binary form must reproduce the above copyright 00015 notice, this list of conditions and the following disclaimer in 00016 the documentation and/or other materials provided with the 00017 distribution. 00018 00019 * Neither the name of the copyright holders nor the names of 00020 contributors may be used to endorse or promote products derived 00021 from this software without specific prior written permission. 00022 00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00029 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00030 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00031 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00032 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 POSSIBILITY OF SUCH DAMAGE. 00034 00035 Title: Interrupt-driven, ring buffered UART0 module 00036 Author(s): Pieter Conradie 00037 Creation Date: 2007-03-31 00038 Revision Info: $Id: uart0.h 116 2010-06-20 21:52:14Z pieterconradie $ 00039 00040 ============================================================================= */ 00041 00042 /** 00043 * @ingroup AVR 00044 * @defgroup UART0 uart0.h : Interrupt-driven, ring buffered UART0 module 00045 * 00046 * Driver that initialises UART0 and allows buffered communication. 00047 * 00048 * Files: avr\uart0.h & avr\uart0.c 00049 * 00050 * This is a driver for the UART0 peripheral that provides buffered serial 00051 * communication facilities. It uses two ring (circular) buffers for 00052 * data to be sent and data received. If the receive buffer is 00053 * full, extra received data is discarded. If more transmit data is 00054 * specified than can be buffered, then only the number that can be buffered 00055 * is accepted and the rest ignored. 00056 * 00057 * The size of the receive and transmit buffer can be set separately, 00058 * up to a maximum size of 256 bytes each. The default frame format is 8 data 00059 * bits, no parity, 1 stop bit, but this can easily be changed in uart0_init(). 00060 * 00061 * @note The maximum number of bytes stored is one less than buffer size. 00062 * 00063 * @par Example: 00064 * @include uart0_test.c 00065 * 00066 * @{ 00067 */ 00068 00069 /* _____STANDARD INCLUDES____________________________________________________ */ 00070 00071 /* _____PROJECT INCLUDES_____________________________________________________ */ 00072 #include "common.h" 00073 00074 /* _____DEFINITIONS__________________________________________________________ */ 00075 00076 /* _____TYPE DEFINITIONS_____________________________________________________ */ 00077 typedef enum 00078 { 00079 UART0_NO_PARITY = 0, 00080 UART0_ODD_PARITY, 00081 UART0_EVEN_PARITY 00082 } uart0_parity_t; 00083 00084 /* _____GLOBAL VARIABLES_____________________________________________________ */ 00085 00086 /* _____GLOBAL FUNCTION DECLARATIONS_________________________________________ */ 00087 /** 00088 * Initialise UART0 peripheral, buffers and interrupt handlers. 00089 * 00090 * @param baud Baud rate in bits/s 00091 * @param data_bits Data bits (5,6,7 or 8) 00092 * @param parity Parity(NONE, ODD or EVEN) 00093 * @param stop_bits Stop bits (1 or 2) 00094 */ 00095 extern void uart0_init(u32_t baud, 00096 u8_t data_bits, 00097 uart0_parity_t parity, 00098 u8_t stop_bits); 00099 00100 /** 00101 * Change to new BAUD rate. 00102 * 00103 * Calculates and sets the new 16-bit UBBR register value. No check is 00104 * performed that the actual BAUD rate is within tolerance of the specified 00105 * BAUD rate. 00106 * 00107 * UBBR = F_OSC / (16 * BAUD) - 1 (for Asynchronous Normal Mode; U2X flag = 0) 00108 * 00109 * @param[in] baud New BAUD rate 00110 */ 00111 extern void uart0_change_baud(u32_t baud); 00112 00113 /** 00114 * See if there is received data in the receive buffer. 00115 * 00116 * @retval TRUE There is received data in the receive buffer 00117 * @retval FALSE The receive buffer is empty 00118 */ 00119 extern bool_t uart0_rx_buffer_empty(void); 00120 00121 /** 00122 * See if received byte is available and store it in specified location. 00123 * 00124 * @param[out] data Pointer to location where data byte must be stored 00125 * 00126 * @retval TRUE Received byte is stored in specified location 00127 * @retval FALSE No received data available (receive buffer empty) 00128 */ 00129 extern bool_t uart0_get_rx_byte(u8_t *data); 00130 00131 /** 00132 * Copy received data from ring buffer into specified buffer. 00133 * 00134 * @param[out] buffer Buffer to copy received data into 00135 * @param[in] max_buf_size Maximum number of received bytes to copy into buffer 00136 * 00137 * @return u8_t Number of received bytes copied into buffer 00138 */ 00139 extern u8_t uart0_get_rx_data(u8_t *buffer, 00140 u8_t max_buf_size); 00141 00142 /** 00143 * See if transmit ring buffer can accept more data. 00144 * 00145 * @retval TRUE Transmit ring buffer is full 00146 * @retval FALSE Transmit ring buffer has space for at least one byte 00147 */ 00148 extern bool_t uart0_tx_buffer_full(void); 00149 00150 /** 00151 * See if transmit ring buffer is empty. 00152 * 00153 * @note Buffer may be empty, but UART peripheral may still be busy 00154 * with the transmission of the last byte in the buffer. 00155 * @see uart0_tx_finished. 00156 * 00157 * @retval TRUE Transmit ring buffer is empty 00158 * @retval FALSE Transmit ring buffer has space for at least one byte 00159 */ 00160 extern bool_t uart0_tx_buffer_empty(void); 00161 00162 /** 00163 * See if all transmission has finished, including last byte. 00164 * 00165 * This functions is usefull for communication standards like RS-485 00166 * where the mode must be changed manually from TX to RX after transmission. 00167 * 00168 * @retval TRUE Transmision completely finished 00169 * @retval FALSE Busy with transmission 00170 */ 00171 extern bool_t uart0_tx_finished(void); 00172 00173 /** 00174 * Buffer one byte for transmission. 00175 * 00176 * @param[in] data Byte to be transmitted 00177 * 00178 * @retval TRUE Byte has been buffered 00179 * @retval FALSE Byte has not been buffered, because transmit buffer is full 00180 */ 00181 extern bool_t uart0_tx_byte(u8_t data); 00182 00183 /** 00184 * Buffer byte(s) for transmission. 00185 * 00186 * @note The transmit ring buffer may not be able to hold all of the specified 00187 * data. 00188 * 00189 * @param[in] data Buffer containing data for transmission 00190 * @param[in] bytes_to_send Number of bytes in buffer to be transmitted 00191 * 00192 * @return u8_t The actual number of bytes buffered for transmission. 00193 */ 00194 extern u8_t uart0_tx_data(const u8_t *data, 00195 u8_t bytes_to_send); 00196 /* _____MACROS_______________________________________________________________ */ 00197 00198 /** 00199 * @} 00200 */ 00201 #endif
1.6.3