From 4bafbcff2efb5e3142a45edb0b354ce1c8a7c2b6 Mon Sep 17 00:00:00 2001 From: Anders Montonen Date: Tue, 3 Jul 2018 01:42:21 +0300 Subject: [PATCH] uart.c: Refactor to use constant expressions instead of ifdefs --- Firmware/uart.c | 298 ++++++++++++++++++++++++++---------------------- 1 file changed, 161 insertions(+), 137 deletions(-) diff --git a/Firmware/uart.c b/Firmware/uart.c index 38977214..f746a9f0 100644 --- a/Firmware/uart.c +++ b/Firmware/uart.c @@ -24,6 +24,34 @@ #include "proc_menu.h" #include "uart2.h" +enum { + BP_V3, + BP_V4 +}; + +extern void msg_dummy(void); + +#if defined(BUSPIRATEV3) + +static const int BP_VER = BP_V3; +extern long getlong(long def, int min, long max, int x); +extern bool bp_binary_io_pullup_control(uint8_t); +#define BP_BUTTON_ISDOWN() 0 +#define MSG_BAUD_DETECTION_SELECTED msg_dummy() +#define MSG_UART_NORMAL_TO_EXIT msg_dummy() + +#elif defined(BUSPIRATEV4) + +static const int BP_VER = BP_V4; +static int FTDI_CTS_DIR; +static int FTDI_RTS_DIR; +static int FTDI_RTS; +static int FTDI_CTS; +#define MSG_UART_POSSIBLE_OVERFLOW msg_dummy() +#define MSG_UART_RESET_TO_EXIT msg_dummy() + +#endif + extern mode_configuration_t mode_configuration; extern command_t last_command; extern bool command_error; @@ -41,9 +69,7 @@ typedef struct { uint8_t stop_bits : 1; uint8_t receive_polarity : 1; uint8_t echo_uart : 1; -#ifdef BUSPIRATEV4 uint8_t autobaud : 1; -#endif /* BUSPIRATEV4 */ } UARTSettings; static UARTSettings uart_settings = {0}; @@ -144,9 +170,9 @@ void uart_setup_prepare(void) { // autobaud detection; multi uses unsigned long abd = 0; -#ifdef BUSPIRATEV4 - uart_settings.autobaud = OFF; -#endif /* BUSPIRATEV4 */ + if (BP_VER == BP_V4) { + uart_settings.autobaud = OFF; + } consumewhitechars(); speed = getint(); @@ -200,40 +226,40 @@ void uart_setup_prepare(void) { BPMSG1133; -#if defined(BUSPIRATEV4) - // BPv4 Mode; has custom BAUD entry and auto-baud detection + if (BP_VER == BP_V4) { + // BPv4 Mode; has custom BAUD entry and auto-baud detection - mode_configuration.speed = getnumber(1, 1, 11, 0) - 1; // get user reply + mode_configuration.speed = getnumber(1, 1, 11, 0) - 1; // get user reply - if (mode_configuration.speed == 10) { - mode_configuration.speed = 8; // Set to 115200 for now - abd = 1; // trigger to run baud detection - uart_settings.autobaud = ON; - MSG_BAUD_DETECTION_SELECTED; + if (mode_configuration.speed == 10) { + mode_configuration.speed = 8; // Set to 115200 for now + abd = 1; // trigger to run baud detection + uart_settings.autobaud = ON; + MSG_BAUD_DETECTION_SELECTED; } - if (mode_configuration.speed == 9) { - BPMSG1248; // say input custom BAUD rate - abd = getlong(115200, 1, 999999, 0); // get the baud rate from user - abd = (((32000000 / abd) / 8) - 1); // calculate BRG - brg = abd; // set BRG - abd = 0; // set abd to 0; so 'Auto Baud Detection' routine isnt ran below - // hack hack hakc - U2BRG = brg; // passing the brg variable to U2BRG so the UARTsetup_exc can - // use it to start UART2setup.. - } -#else - // Normal mode; input BRG and no autobaud detection - mode_configuration.speed = getnumber(1, 1, 10, 0) - 1; // get user reply - - if (mode_configuration.speed == 9) { - BPMSG1248; - brg = getnumber(34, 1, 32767, 0); - // hack hack hack - U2BRG = brg; // passing the brg variable to U2BRG so the UARTsetup_exc can - // use it to start UART2setup.. + if (mode_configuration.speed == 9) { + BPMSG1248; // say input custom BAUD rate + abd = getlong(115200, 1, 999999, 0); // get the baud rate from user + abd = (((32000000 / abd) / 8) - 1); // calculate BRG + brg = abd; // set BRG + abd = 0; // set abd to 0; so 'Auto Baud Detection' routine isnt ran below + // hack hack hakc + U2BRG = brg; // passing the brg variable to U2BRG so the UARTsetup_exc can + // use it to start UART2setup.. + } + } else { + // Normal mode; input BRG and no autobaud detection + mode_configuration.speed = getnumber(1, 1, 10, 0) - 1; // get user reply + + if (mode_configuration.speed == 9) { + BPMSG1248; + brg = getnumber(34, 1, 32767, 0); + // hack hack hack + U2BRG = brg; // passing the brg variable to U2BRG so the UARTsetup_exc can + // use it to start UART2setup.. + } } -#endif /* BUSPIRATEV4 */ BPMSG1199; uart_settings.databits_parity = getnumber(1, 1, 4, 0) - 1; @@ -273,34 +299,34 @@ void uart_setup_execute(void) { uart2_enable(); -#ifdef BUSPIRATEV4 - unsigned long abd; - int brg; - if (uart_settings.autobaud == ON) { - uart2_disable(); - bpBR; - abd = uart_get_closest_common_rate(uart_get_baud_rate(false)); - bpBR; - - if (abd == 0) { - uart2_setup(UART_BRG_SPEED[8], mode_configuration.high_impedance, - uart_settings.receive_polarity, uart_settings.databits_parity, - uart_settings.stop_bits); - } else { - mode_configuration.speed = 9; - abd = (((32000000 / abd) / 8) - 1); - brg = abd; - uart2_setup(brg, mode_configuration.high_impedance, - uart_settings.receive_polarity, uart_settings.databits_parity, - uart_settings.stop_bits); + if (BP_VER == BP_V4) { + unsigned long abd; + int brg; + if (uart_settings.autobaud == ON) { + uart2_disable(); + bpBR; + abd = uart_get_closest_common_rate(uart_get_baud_rate(false)); + bpBR; + + if (abd == 0) { + uart2_setup(UART_BRG_SPEED[8], mode_configuration.high_impedance, + uart_settings.receive_polarity, uart_settings.databits_parity, + uart_settings.stop_bits); + } else { + mode_configuration.speed = 9; + abd = (((32000000 / abd) / 8) - 1); + brg = abd; + uart2_setup(brg, mode_configuration.high_impedance, + uart_settings.receive_polarity, uart_settings.databits_parity, + uart_settings.stop_bits); + } + uart2_enable(); + } + } else { + if (U2BRG < U1BRG) { + MSG_UART_POSSIBLE_OVERFLOW; } - uart2_enable(); - } -#else - if (U2BRG < U1BRG) { - MSG_UART_POSSIBLE_OVERFLOW; } -#endif /* BUSPIRATEV4 */ } inline void uart_cleanup(void) { uart2_disable(); } @@ -312,27 +338,26 @@ void uart_run_macro(const uint16_t macro) { break; case UART_MACRO_BRIDGE_WITH_FLOW_CONTROL: -#ifdef BUSPIRATEV3 - /* FTDI CTS and RTS setup. */ - FTDI_CTS_DIR = OUTPUT; - FTDI_RTS_DIR = INPUT; - - /* External CTS and RTS setup. */ - BP_CS_DIR = INPUT; - BP_CLK_DIR = OUTPUT; -#else -/* Do nothing for v4? */ -#endif /* BUSPIRATEV3 */ + if (BP_VER == BP_V3) { + /* FTDI CTS and RTS setup. */ + FTDI_CTS_DIR = OUTPUT; + FTDI_RTS_DIR = INPUT; + + /* External CTS and RTS setup. */ + BP_CS_DIR = INPUT; + BP_CLK_DIR = OUTPUT; + } + /* Do nothing for v4? */ /* Intentional fall-through. */ case UART_MACRO_TRANSPARENT_BRIDGE: BPMSG1204; -#ifdef BUSPIRATEV4 - MSG_UART_NORMAL_TO_EXIT; -#else - MSG_UART_RESET_TO_EXIT; -#endif /* BUSPIRATEV4 */ + if (BP_VER == BP_V4) { + MSG_UART_NORMAL_TO_EXIT; + } else { + MSG_UART_RESET_TO_EXIT; + } if (!agree()) { break; @@ -344,44 +369,44 @@ void uart_run_macro(const uint16_t macro) { U2STAbits.OERR = OFF; for (;;) { -#ifdef BUSPIRATEV4 - if (BP_BUTTON_ISDOWN()) { - break; - } + if (BP_VER == BP_V4) { + if (BP_BUTTON_ISDOWN()) { + break; + } - if (U2STAbits.URXDA == ON) { - user_serial_transmit_character(U2RXREG); - } -#else - if ((U2STAbits.URXDA == ON) && (U1STAbits.UTXBF == OFF)) { - U1TXREG = U2RXREG; + if (U2STAbits.URXDA == ON) { + user_serial_transmit_character(U2RXREG); + } + } else { + if ((U2STAbits.URXDA == ON) && (U1STAbits.UTXBF == OFF)) { + U1TXREG = U2RXREG; + } } -#endif /* BUSPIRATEV4 */ if (user_serial_ready_to_read() && (U2STAbits.UTXBF == OFF)) { U2TXREG = user_serial_read_byte(); } -#ifdef BUSPIRATEV3 - if (U2STAbits.OERR || U1STAbits.OERR) { - U2STAbits.OERR = OFF; - U1STAbits.OERR = OFF; - BP_LEDMODE = LOW; - } -#else - if (U2STAbits.OERR) { - U2STAbits.OERR = OFF; - BP_LEDMODE = LOW; + if (BP_VER == BP_V3) { + if (U2STAbits.OERR || U1STAbits.OERR) { + U2STAbits.OERR = OFF; + U1STAbits.OERR = OFF; + BP_LEDMODE = LOW; + } + } else { + if (U2STAbits.OERR) { + U2STAbits.OERR = OFF; + BP_LEDMODE = LOW; + } } -#endif /* BUSPIRATEV3 */ -#ifdef BUSPIRATEV3 - if (macro == UART_MACRO_BRIDGE_WITH_FLOW_CONTROL) { - /* Relay flow control bits. */ - BP_CLK = FTDI_RTS; - FTDI_CTS = BP_CS; + if (BP_VER == BP_V3) { + if (macro == UART_MACRO_BRIDGE_WITH_FLOW_CONTROL) { + /* Relay flow control bits. */ + BP_CLK = FTDI_RTS; + FTDI_CTS = BP_CS; + } } -#endif /* BUSPIRATEV3 */ } break; @@ -391,19 +416,19 @@ void uart_run_macro(const uint16_t macro) { U2STAbits.OERR = OFF; for (;;) { -#ifdef BUSPIRATEV4 - if (BP_BUTTON_ISDOWN()) { - break; - } + if (BP_VER == BP_V4) { + if (BP_BUTTON_ISDOWN()) { + break; + } - if (uart2_rx_ready()) { - user_serial_transmit_character(uart2_rx()); - } -#else - if ((U2STAbits.URXDA == ON) && (U1STAbits.UTXBF == OFF)) { - U1TXREG = U2RXREG; + if (uart2_rx_ready()) { + user_serial_transmit_character(uart2_rx()); + } + } else { + if ((U2STAbits.URXDA == ON) && (U1STAbits.UTXBF == OFF)) { + U1TXREG = U2RXREG; + } } -#endif /* BUSPIRATEV4 */ if (user_serial_ready_to_read()) { volatile uint16_t dummy; @@ -418,11 +443,11 @@ void uart_run_macro(const uint16_t macro) { uart2_disable(); uart_get_baud_rate(false); uart2_enable(); -#ifdef BUSPIRATEV3 - if (U2BRG < U1BRG) { - MSG_UART_POSSIBLE_OVERFLOW; + if (BP_VER == BP_V3) { + if (U2BRG < U1BRG) { + MSG_UART_POSSIBLE_OVERFLOW; + } } -#endif /* BUSPIRATEV3 */ break; default: @@ -779,32 +804,31 @@ void binUART(void) { REPORT_IO_SUCCESS(); U2STAbits.OERR = OFF; for (;;) { - -#ifdef BUSPIRATEV4 - if (U2STAbits.URXDA == ON) { - user_serial_transmit_character(U2RXREG); - } -#else - if ((U2STAbits.URXDA == 1) && (U1STAbits.UTXBF == 0)) { - U1TXREG = U2RXREG; + if (BP_VER == BP_V4) { + if (U2STAbits.URXDA == ON) { + user_serial_transmit_character(U2RXREG); + } + } else { + if ((U2STAbits.URXDA == 1) && (U1STAbits.UTXBF == 0)) { + U1TXREG = U2RXREG; + } } -#endif /* BUSPIRATEV4 */ - + if (user_serial_ready_to_read() && (U2STAbits.UTXBF == 0)) { U2TXREG = user_serial_read_byte(); } } break; - + default: REPORT_IO_FAILURE(); break; } break; - + case 0b00010000: { size_t counter; - + input_byte &= 0b00001111; input_byte++; REPORT_IO_SUCCESS(); @@ -821,12 +845,12 @@ void binUART(void) { REPORT_IO_SUCCESS(); break; -#ifdef BUSPIRATEV4 case 0b01010000: - user_serial_transmit_character(bp_binary_io_pullup_control(input_byte)); + if (BP_VER == BP_V4) { + user_serial_transmit_character(bp_binary_io_pullup_control(input_byte)); + } break; -#endif /* BUSPIRATEV4 */ - + case 0b01100000: input_byte &= 0b00001111; if (input_byte > 0b1010) { @@ -840,7 +864,7 @@ void binUART(void) { uart2_enable(); REPORT_IO_SUCCESS(); break; - + case 0b10000000: case 0b10010000: uart_settings.databits_parity = 0;