diff --git a/inc/utils.h b/inc/utils.h index 621af0c2..05ab50fe 100644 --- a/inc/utils.h +++ b/inc/utils.h @@ -1,8 +1,8 @@ -// Different utilities +// General purpose function/macros #ifndef __UTILS_H__ #define __UTILS_H__ -int utils_get_uart_key(); +#define ABSDIFF( x, y ) ( ( x ) >= ( y ) ? ( x ) - ( y ) : ( y ) - ( x ) ) #endif diff --git a/src/platform/at91sam7x/platform.c b/src/platform/at91sam7x/platform.c index d64a9d51..c4f37fd8 100644 --- a/src/platform/at91sam7x/platform.c +++ b/src/platform/at91sam7x/platform.c @@ -16,8 +16,7 @@ #include #include #include - -#define PABS( x ) ( ( x ) >= 0 ? ( x ) : -( x ) ) +#include "utils.h" // ***************************************************************************** // std functions @@ -287,7 +286,7 @@ static u32 platform_timer_set_clock( unsigned id, u32 clock ) unsigned i, mini = 0; for( i = 0; i < 5; i ++ ) - if( PABS( clock - BOARD_MCK / clkdivs[ i ] ) < PABS( clock - BOARD_MCK / clkdivs[ mini ] ) ) + if( ABSDIFF( clock, BOARD_MCK / clkdivs[ i ] ) < ABSDIFF( clock, BOARD_MCK / clkdivs[ mini ] ) ) mini = i; TC_Configure( ( AT91S_TC* )timer_base[ id ], mini | AT91C_TC_WAVE ); return BOARD_MCK / clkdivs[ mini ]; diff --git a/src/platform/lm3s/platform.c b/src/platform/lm3s/platform.c index acdfaa6a..1470a9f8 100644 --- a/src/platform/lm3s/platform.c +++ b/src/platform/lm3s/platform.c @@ -23,6 +23,7 @@ #include "ssi.h" #include "timer.h" #include "pwm.h" +#include "utils.h" // ***************************************************************************** // std function @@ -439,7 +440,6 @@ u32 platform_timer_get_diff_us( unsigned id, timer_data_type end, timer_data_typ // PWMs #define PLATFORM_NUM_PWMS 6 -#define PABS( x ) ( ( x ) < 0 ? -( x ) : ( x ) ) // SYSCTL div data and actual div factors const static u32 pwm_div_ctl[] = { SYSCTL_PWMDIV_1, SYSCTL_PWMDIV_2, SYSCTL_PWMDIV_4, SYSCTL_PWMDIV_8, SYSCTL_PWMDIV_16, SYSCTL_PWMDIV_32, SYSCTL_PWMDIV_64 }; @@ -479,7 +479,7 @@ static u32 platform_pwm_set_clock( u32 clock ) sysclk = SysCtlClockGet(); for( i = min_i = 0; i < sizeof( pwm_div_data ) / sizeof( u8 ); i ++ ) - if( PABS( clock - sysclk / pwm_div_data[ i ] ) < PABS( clock - sysclk / pwm_div_data[ min_i ] ) ) + if( ABSDIFF( clock, sysclk / pwm_div_data[ i ] ) < ABSDIFF( clock, sysclk / pwm_div_data[ min_i ] ) ) min_i = i; SysCtlPWMClockSet( pwm_div_ctl[ min_i ] ); return sysclk / pwm_div_data[ min_i ]; diff --git a/src/platform/lpc288x/platform.c b/src/platform/lpc288x/platform.c index 5e702936..91c22133 100644 --- a/src/platform/lpc288x/platform.c +++ b/src/platform/lpc288x/platform.c @@ -13,6 +13,7 @@ #include "lpc288x.h" #include "target.h" #include "uart.h" +#include "utils.h" // ***************************************************************************** // std functions @@ -189,7 +190,6 @@ static const vu_ptr tmr_value[] = { &T0VALUE, &T1VALUE }; static const vu_ptr tmr_ctrl[] = { &T0CTRL, &T1CTRL }; static const vu_ptr tmr_clr[] = { &T0CLR, &T1CLR }; static const unsigned tmr_prescale[] = { 1, 16, 256, 1 }; -#define TABS( x ) ( ( x ) < 0 ? -( x ) : ( x ) ) // Helper: get timer clock static u32 platform_timer_get_clock( unsigned id ) @@ -203,7 +203,7 @@ static u32 platform_timer_set_clock( unsigned id, u32 clock ) unsigned i, mini = 0; for( i = 0; i < 3; i ++ ) - if( TABS( clock - MAIN_CLOCK / tmr_prescale[ i ] ) < TABS( clock - MAIN_CLOCK / tmr_prescale[ mini ] ) ) + if( ABSDIFF( clock, MAIN_CLOCK / tmr_prescale[ i ] ) < ABSDIFF( clock, MAIN_CLOCK / tmr_prescale[ mini ] ) ) mini = i; *tmr_ctrl[ id ] = ( *tmr_ctrl[ id ] & ~0xB ) | ( mini << 2 ); return MAIN_CLOCK / tmr_prescale[ mini ]; diff --git a/src/platform/lpc288x/uart.c b/src/platform/lpc288x/uart.c index fc462204..de71858a 100644 --- a/src/platform/lpc288x/uart.c +++ b/src/platform/lpc288x/uart.c @@ -7,8 +7,7 @@ #include #include #include - -#define LABS( x ) ( ( x ) < 0 ? -( x ) : ( x ) ) +#include "utils.h" // Structure with UART baud/parameters // Computed used Philip's "baud rate calculator" XLS file @@ -68,7 +67,7 @@ u32 uart_init( u32 baud, int databits, int parity, int stopbits ) // Find correct baud for( i = minpos = 0; i < sizeof( uart_baudinfo ) / sizeof( UART_BAUDDATA ); i ++ ) - if( LABS( baud - uart_baudinfo[ i ].baud ) < LABS( baud - uart_baudinfo[ minpos ].baud ) ) + if( ABSDIFF( baud, uart_baudinfo[ i ].baud ) < ABSDIFF( baud, uart_baudinfo[ minpos ].baud ) ) minpos = i; pdata = uart_baudinfo + minpos;