From 413438eb2330e8b2d9606a2936a169c9dd7916de Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Fri, 2 Sep 2011 04:38:56 +0000
Subject: [PATCH] Add new TWI_BITLENGTH_FROM_FREQ() macro to the AVR8 TWI
 peripheral driver.

Change Delay_MS() to accept a 16-bit parameter rather than an 8-bit parameter for longer possible delays.

Minor documentation improvements.
---
 LUFA/Common/Common.h                       |  4 ++--
 LUFA/Drivers/Peripheral/AVR8/Serial_AVR8.h | 16 ++++++++++++----
 LUFA/Drivers/Peripheral/AVR8/TWI_AVR8.h    | 11 +++++++++++
 3 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/LUFA/Common/Common.h b/LUFA/Common/Common.h
index 43df9fbe9..e9faf7af4 100644
--- a/LUFA/Common/Common.h
+++ b/LUFA/Common/Common.h
@@ -237,8 +237,8 @@
 			 *
 			 *  \param[in] Milliseconds  Number of milliseconds to delay
 			 */
-			static inline void Delay_MS(uint8_t Milliseconds) ATTR_ALWAYS_INLINE;
-			static inline void Delay_MS(uint8_t Milliseconds)
+			static inline void Delay_MS(uint16_t Milliseconds) ATTR_ALWAYS_INLINE;
+			static inline void Delay_MS(uint16_t Milliseconds)
 			{
 				#if (ARCH == ARCH_AVR8)
 				if (GCC_IS_COMPILE_CONST(Milliseconds))
diff --git a/LUFA/Drivers/Peripheral/AVR8/Serial_AVR8.h b/LUFA/Drivers/Peripheral/AVR8/Serial_AVR8.h
index d8216cd24..639b3c3b4 100644
--- a/LUFA/Drivers/Peripheral/AVR8/Serial_AVR8.h
+++ b/LUFA/Drivers/Peripheral/AVR8/Serial_AVR8.h
@@ -97,15 +97,23 @@
 
 	/* Public Interface - May be used in end-application: */
 		/* Macros: */
-			/** Macro for calculating the baud value from a given baud rate when the U2X (double speed) bit is
+			/** Macro for calculating the baud value from a given baud rate when the \c U2X (double speed) bit is
 			 *  not set.
+			 *
+			 *  \param[in] Baud  Target serial UART baud rate.
+			 *
+			 *  \return Closest UBRR register value for the given UART frequency.
 			 */
-			#define SERIAL_UBBRVAL(baud)    ((((F_CPU / 16) + (baud / 2)) / (baud)) - 1)
+			#define SERIAL_UBBRVAL(Baud)    ((((F_CPU / 16) + (Baud / 2)) / (Baud)) - 1)
 
-			/** Macro for calculating the baud value from a given baud rate when the U2X (double speed) bit is
+			/** Macro for calculating the baud value from a given baud rate when the \c U2X (double speed) bit is
 			 *  set.
+			 *
+			 *  \param[in] Baud  Target serial UART baud rate.
+			 *
+			 *  \return Closest UBRR register value for the given UART frequency.
 			 */
-			#define SERIAL_2X_UBBRVAL(baud) ((((F_CPU / 8) + (baud / 2)) / (baud)) - 1)
+			#define SERIAL_2X_UBBRVAL(Baud) ((((F_CPU / 8) + (Baud / 2)) / (Baud)) - 1)
 
 		/* Function Prototypes: */
 			/** Transmits a given string located in program space (FLASH) through the USART.
diff --git a/LUFA/Drivers/Peripheral/AVR8/TWI_AVR8.h b/LUFA/Drivers/Peripheral/AVR8/TWI_AVR8.h
index 705ef530c..0f31eca6f 100644
--- a/LUFA/Drivers/Peripheral/AVR8/TWI_AVR8.h
+++ b/LUFA/Drivers/Peripheral/AVR8/TWI_AVR8.h
@@ -167,6 +167,17 @@
 			/** Bit length prescaler for \ref TWI_Init(). This mask multiplies the TWI bit length prescaler by 64. */
 			#define TWI_BIT_PRESCALE_64      ((1 << TWPS1) | (1 << TWPS0))
 			
+			/** Calculates the length of each bit on the TWI bus for a given target frequency. This may be used with
+			 *  the \ref TWI_Init() function to convert a bus frequency to a number of clocks for the \c BitLength
+			 *  parameter.
+			 *
+			 *  \param[in] Prescaler  Prescaler set on the TWI bus.
+			 *  \param[in] Frequency  Desired TWI bus frequency in Hz.
+			 *
+			 *  \return Bit length in clocks for the given TWI bus frequency at the given prescaler value.
+			 */
+			#define TWI_BITLENGTH_FROM_FREQ(Prescale, Frequency) ((((F_CPU / (Prescale)) / (Frequency)) - 16) / 2)
+
 		/* Enums: */
 			/** Enum for the possible return codes of the TWI transfer start routine and other dependant TWI functions. */
 			enum TWI_ErrorCodes_t
-- 
GitLab