/* Copyright (c) 2006 Cliff Lawson Copyright (c) 2006 Carlos Lamas All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef _UTIL_SETBAUD_H_ #define _UTIL_SETBAUD_H_ /************************************************************* * * util/setbaud.h * * This .h requires that on entry values are already defined * for F_CPU and BAUD such as: * * #define F_CPU 11059200 * #define BAUD 38400 * #include * * Assuming that the requested BAUD is valid for the given F_CPU * then UBRRL_VALUE is set to the lower byte of the UBRR_VALUE * and UBRRH_VALUE is set to the upper byte so that they can be * used simply as (depending on IO register naming!): * * UBRRH = UBRRH_VALUE; * UBRRL = UBRRL_VALUE; * * The value USE_2X will be undefined unless the baud can only be reached * using U2X mode in which case it is defined and code should be added * to set the U2X bit: * * #ifdef USE_2X * UCSRA |= (1 << U2X); * #endif * * * NB: Only baud rates with Error% between -BAUD_TOL% and +BAUD_TOL% are * considered "valid" and the U2X=0 is always used where possible. The * U2X=1 values for UBRR are only used when the value for U2X=0 would give * an error out of the +/- BAUD_TOL% range. BAUD_TOL is assigned a default * value of 2, i.e., the predefined tolerance is 2%. Different values are * allowed if BAUD_TOL is defined previously to the inclusion of this * header * * V1.1, 29-8-06, by Cliff Lawson * V1.2, 30-8-06, by Carlos Lamas * *************************************************************/ #ifndef F_CPU #error "setbaud.h requires F_CPU to be defined" #else #ifndef BAUD #error "setbaud.h requires BAUD to be defined" #else #if !(F_CPU) #error "F_CPU must be a constant value" #else #if !(BAUD) #error "BAUD must be a constant value" #else #undef USE_2X /* Baud rate tolerance is 2% unless previously defined */ #ifndef BAUD_TOL #define BAUD_TOL 2 #endif #define UBRR_VALUE (((F_CPU) + 8*(BAUD)) / (16*(BAUD)) -1) #if 100 * (F_CPU) > (16 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) + (BAUD) * (BAUD_TOL)) #define USE_2X #elif 100 * (F_CPU) < (16 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) - (BAUD) * (BAUD_TOL)) #define USE_2X #endif #ifdef USE_2X /* U2X required */ #undef UBRR_VALUE #define UBRR_VALUE (((F_CPU) + 4*(BAUD)) / (8*(BAUD)) -1) #if 100 * (F_CPU) > (8 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) + (BAUD) * (BAUD_TOL)) #warning "Baud rate achieved is higher than allowed" #endif #if 100 * (F_CPU) < (8 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) - (BAUD) * (BAUD_TOL)) #warning "Baud rate achieved is lower than allowed" #endif #endif /* U2X required */ #ifdef UBRR_VALUE #define UBRRL_VALUE (UBRR_VALUE & 0xFF) #define UBRRH_VALUE (UBRR_VALUE >> 8) #endif #endif /* BAUD */ #endif /* F_CPU */ #endif /* defined(BAUD) */ #endif /* defined(F_CPU) */ #endif /* _UTIL_SETBAUD_H_ */