From dfcdd512d1148804fa8b4cbe1d240a1eee02c901 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Mon, 9 Mar 2020 14:47:02 +0100 Subject: [PATCH] busvoodoo_uart_generic: minor, put spaces around operators --- lib/busvoodoo_uart_generic.c | 194 +++++++++++++++++------------------ 1 file changed, 97 insertions(+), 97 deletions(-) diff --git a/lib/busvoodoo_uart_generic.c b/lib/busvoodoo_uart_generic.c index ac86875..bfe4b65 100644 --- a/lib/busvoodoo_uart_generic.c +++ b/lib/busvoodoo_uart_generic.c @@ -78,7 +78,7 @@ static bool busvoodoo_uart_generic_pullup = false; bool busvoodoo_uart_generic_configure(const struct busvoodoo_uart_generic_specific_t* conf) { busvoodoo_uart_generic_specific = NULL; // reset specific information - if (NULL==conf) { + if (NULL == conf) { return false; } if (!conf->usart || !conf->usart_rcc || !conf->usart_rst) { @@ -90,7 +90,7 @@ bool busvoodoo_uart_generic_configure(const struct busvoodoo_uart_generic_specif if (conf->hwflowctl && (!conf->rts_rcc || !conf->cts_rcc)) { return false; } - if (conf->timer && (!conf->timer_rcc || !conf->timer_rst || !conf->timer_port_rcc || !(NVIC_TIM2_IRQ==conf->timer_nvic_irq || NVIC_TIM3_IRQ==conf->timer_nvic_irq || NVIC_TIM4_IRQ==conf->timer_nvic_irq || NVIC_TIM5_IRQ==conf->timer_nvic_irq))) { + if (conf->timer && (!conf->timer_rcc || !conf->timer_rst || !conf->timer_port_rcc || !(NVIC_TIM2_IRQ == conf->timer_nvic_irq || NVIC_TIM3_IRQ == conf->timer_nvic_irq || NVIC_TIM4_IRQ == conf->timer_nvic_irq || NVIC_TIM5_IRQ == conf->timer_nvic_irq))) { return false; } busvoodoo_uart_generic_specific = conf; @@ -99,11 +99,11 @@ bool busvoodoo_uart_generic_configure(const struct busvoodoo_uart_generic_specif bool busvoodoo_uart_generic_setup(char** prefix, const char* line) { - if (NULL==busvoodoo_uart_generic_specific) { // there is nothing to configure + if (NULL == busvoodoo_uart_generic_specific) { // there is nothing to configure return true; } bool complete = false; // is the setup complete - if (NULL==line) { // first call + if (NULL == line) { // first call busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_NONE; // re-start configuration } switch (busvoodoo_uart_generic_setting) { @@ -113,31 +113,31 @@ bool busvoodoo_uart_generic_setup(char** prefix, const char* line) busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_BAUDRATE; break; case BUSVOODOO_UART_SETTING_BAUDRATE: - if (NULL==line || 0==strlen(line)) { // use default setting + if (NULL == line || 0 == strlen(line)) { // use default setting busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_DATABITS; // go to next setting } else { // setting provided uint32_t baudrate = atoi(line); // parse setting - if (baudrate>0 && baudrate<=2000000) { // check setting + if (baudrate > 0 && baudrate <= 2000000) { // check setting busvoodoo_uart_generic_baudrate = baudrate; // remember setting busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_DATABITS; // go to next setting } } - if (BUSVOODOO_UART_SETTING_DATABITS==busvoodoo_uart_generic_setting) { // if next setting + if (BUSVOODOO_UART_SETTING_DATABITS == busvoodoo_uart_generic_setting) { // if next setting snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "data bits (5-8) [%u]", busvoodoo_uart_generic_databits); // prepare next setting *prefix = busvoodoo_global_string; // display next setting } break; case BUSVOODOO_UART_SETTING_DATABITS: - if (NULL==line || 0==strlen(line)) { // use default setting + if (NULL == line || 0 == strlen(line)) { // use default setting busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_PARITY; // go to next setting - } else if (1==strlen(line)) { // setting provided + } else if (1 == strlen(line)) { // setting provided uint8_t databits = atoi(line); // parse setting - if (databits>=5 && databits<=8) { // check setting + if (databits >= 5 && databits <= 8) { // check setting busvoodoo_uart_generic_databits = databits; // remember setting busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_PARITY; // go to next setting } } - if (BUSVOODOO_UART_SETTING_PARITY==busvoodoo_uart_generic_setting) { // if next setting + if (BUSVOODOO_UART_SETTING_PARITY == busvoodoo_uart_generic_setting) { // if next setting printf("1) none\n"); printf("2) even\n"); printf("3) odd\n"); @@ -148,43 +148,43 @@ bool busvoodoo_uart_generic_setup(char** prefix, const char* line) } break; case BUSVOODOO_UART_SETTING_PARITY: - if (NULL==line || 0==strlen(line)) { // use default setting + if (NULL == line || 0 == strlen(line)) { // use default setting busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_STOPBITS; // go to next setting - } else if (1==strlen(line)) { // setting provided + } else if (1 == strlen(line)) { // setting provided uint8_t parity = atoi(line); // parse setting - if (parity>0 && parity<6) { // check setting - busvoodoo_uart_generic_parity = parity-1; + if (parity > 0 && parity < 6) { // check setting + busvoodoo_uart_generic_parity = parity - 1; busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_STOPBITS; // go to next setting } } - if (BUSVOODOO_UART_SETTING_STOPBITS==busvoodoo_uart_generic_setting) { // if next setting + if (BUSVOODOO_UART_SETTING_STOPBITS == busvoodoo_uart_generic_setting) { // if next setting printf("1) 0.5\n"); printf("2) 1\n"); printf("3) 1.5\n"); printf("4) 2\n"); - snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "stop bits (1,2,3,4) [%c]", USART_STOPBITS_0_5==busvoodoo_uart_generic_stopbits ? '1' : (USART_STOPBITS_1==busvoodoo_uart_generic_stopbits ? '2' : (USART_STOPBITS_1_5==busvoodoo_uart_generic_stopbits ? '3' : '4'))); // prepare next setting + snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "stop bits (1,2,3,4) [%c]", USART_STOPBITS_0_5 == busvoodoo_uart_generic_stopbits ? '1' : (USART_STOPBITS_1 == busvoodoo_uart_generic_stopbits ? '2' : (USART_STOPBITS_1_5 == busvoodoo_uart_generic_stopbits ? '3' : '4'))); // prepare next setting *prefix = busvoodoo_global_string; // display next setting } break; case BUSVOODOO_UART_SETTING_STOPBITS: - if (NULL==line || 0==strlen(line)) { // use default setting + if (NULL == line || 0 == strlen(line)) { // use default setting busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_HWFLOWCTL; // go to next setting - } else if (1==strlen(line)) { // setting provided - if ('1'==line[0]) { // 0.5 stop bits + } else if (1 == strlen(line)) { // setting provided + if ('1' == line[0]) { // 0.5 stop bits busvoodoo_uart_generic_stopbits = USART_STOPBITS_0_5; // remember setting busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_HWFLOWCTL; // go to next setting - } else if ('2'==line[0]) { // 1 stop bits + } else if ('2' == line[0]) { // 1 stop bits busvoodoo_uart_generic_stopbits = USART_STOPBITS_1; // remember setting busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_HWFLOWCTL; // go to next setting - } else if ('3'==line[0]) { // 1.5 stop bits + } else if ('3' == line[0]) { // 1.5 stop bits busvoodoo_uart_generic_stopbits = USART_STOPBITS_1_5; // remember setting busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_HWFLOWCTL; // go to next setting - } else if ('4'==line[0]) { // 2 stop bits + } else if ('4' == line[0]) { // 2 stop bits busvoodoo_uart_generic_stopbits = USART_STOPBITS_2; // remember setting busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_HWFLOWCTL; // go to next setting } } - if (BUSVOODOO_UART_SETTING_HWFLOWCTL==busvoodoo_uart_generic_setting) { // if next setting + if (BUSVOODOO_UART_SETTING_HWFLOWCTL == busvoodoo_uart_generic_setting) { // if next setting if (!busvoodoo_uart_generic_specific->hwflowctl) { // hardware flow control is not supported #if BUSVOODOO_HARDWARE_VERSION != 2 busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_DRIVE; // go to next setting @@ -201,11 +201,11 @@ bool busvoodoo_uart_generic_setup(char** prefix, const char* line) } break; case BUSVOODOO_UART_SETTING_HWFLOWCTL: - if (NULL==line || 0==strlen(line)) { // use default setting + if (NULL == line || 0 == strlen(line)) { // use default setting busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_DRIVE; // go to next setting - } else if (1==strlen(line)) { // setting provided - if ('1'==line[0] || '2'==line[0]) { // setting provided - busvoodoo_uart_generic_hwflowctl = ('2'==line[0]); // remember setting + } else if (1 == strlen(line)) { // setting provided + if ('1' == line[0] || '2' == line[0]) { // setting provided + busvoodoo_uart_generic_hwflowctl = ('2' == line[0]); // remember setting #if BUSVOODOO_HARDWARE_VERSION != 2 busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_DRIVE; // go to next setting #else @@ -215,7 +215,7 @@ bool busvoodoo_uart_generic_setup(char** prefix, const char* line) } #if BUSVOODOO_HARDWARE_VERSION != 2 setting_drive: - if (BUSVOODOO_UART_SETTING_DRIVE==busvoodoo_uart_generic_setting) { // if next setting + if (BUSVOODOO_UART_SETTING_DRIVE == busvoodoo_uart_generic_setting) { // if next setting if (!busvoodoo_uart_generic_specific->multidrive) { busvoodoo_uart_generic_drive = true; // only push-pull driving mode is supported busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_DONE; // go to next setting @@ -229,19 +229,19 @@ setting_drive: } break; case BUSVOODOO_UART_SETTING_DRIVE: - if (NULL==line || 0==strlen(line)) { // use default setting + if (NULL == line || 0 == strlen(line)) { // use default setting busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_DONE; // go to next setting - } else if (1==strlen(line)) { // setting provided + } else if (1 == strlen(line)) { // setting provided uint8_t drive = atoi(line); // parse setting - if (1==drive || 2==drive || 3==drive) { // check setting - busvoodoo_uart_generic_drive = (1==drive); // remember setting - busvoodoo_uart_generic_pullup = (2==drive); // remember setting + if (1 == drive || 2 == drive || 3 == drive) { // check setting + busvoodoo_uart_generic_drive = (1 == drive); // remember setting + busvoodoo_uart_generic_pullup = (2 == drive); // remember setting busvoodoo_uart_generic_setting = BUSVOODOO_UART_SETTING_DONE; // go to next setting } } #endif setting_done: - if (BUSVOODOO_UART_SETTING_DONE==busvoodoo_uart_generic_setting) { // we have all settings, configure UART + if (BUSVOODOO_UART_SETTING_DONE == busvoodoo_uart_generic_setting) { // we have all settings, configure UART rcc_periph_clock_enable(RCC_AFIO); // enable clock for USART alternate function rcc_periph_clock_enable(busvoodoo_uart_generic_specific->usart_rcc); // enable clock for USART peripheral rcc_periph_reset_pulse(busvoodoo_uart_generic_specific->usart_rst); // reset USART peripheral @@ -301,7 +301,7 @@ setting_done: void busvoodoo_uart_generic_exit(void) { - if (NULL==busvoodoo_uart_generic_specific) { + if (NULL == busvoodoo_uart_generic_specific) { return; } usart_disable(busvoodoo_uart_generic_specific->usart); // disable USART @@ -325,13 +325,13 @@ void busvoodoo_uart_generic_exit(void) */ static void busvoodoo_uart_generic_write(uint8_t value) { - if (NULL==busvoodoo_uart_generic_specific) { + if (NULL == busvoodoo_uart_generic_specific) { return; } if (busvoodoo_uart_generic_specific->tx_pre) { (*busvoodoo_uart_generic_specific->tx_pre)(); } - while ((0==((USART_SR(busvoodoo_uart_generic_specific->usart)) & USART_SR_TXE) && !user_input_available)); // wait for transmit buffer to be empty (or user to interrupt) + while ((0 == ((USART_SR(busvoodoo_uart_generic_specific->usart)) & USART_SR_TXE) && !user_input_available)); // wait for transmit buffer to be empty (or user to interrupt) if ((USART_SR(busvoodoo_uart_generic_specific->usart)) & USART_SR_TXE) { // we can send data // send data busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show transmission @@ -339,7 +339,7 @@ static void busvoodoo_uart_generic_write(uint8_t value) // display data send printf("write: '%c'/0x%02x\n", value, value); } - while ((0==((USART_SR(busvoodoo_uart_generic_specific->usart)) & USART_SR_TC) && !user_input_available)); // wait for transfer to be complete + while ((0 == ((USART_SR(busvoodoo_uart_generic_specific->usart)) & USART_SR_TC) && !user_input_available)); // wait for transfer to be complete if (user_input_available) { // user interrupted flow user_input_get(); // discard user input } @@ -352,7 +352,7 @@ static void busvoodoo_uart_generic_write(uint8_t value) */ static void busvoodoo_uart_generic_read(void) { - if (NULL==busvoodoo_uart_generic_specific) { + if (NULL == busvoodoo_uart_generic_specific) { return; } if (busvoodoo_uart_generic_specific->rx_pre) { @@ -399,47 +399,47 @@ static void busvoodoo_uart_generic_read(void) static bool busvoodoo_uart_generic_action(const char* action, uint32_t repetition, bool perform) { uint32_t length = strlen(action); // remember length since it will be used a number of times - if (NULL==action || 0==length) { // there is nothing to do + if (NULL == action || 0 == length) { // there is nothing to do return true; } - if (1==length && 'r'==action[0]) { // read data + if (1 == length && 'r' == action[0]) { // read data if (!perform) { return true; } - for (uint32_t i=0; i='0' && action[1]<='9') { // send decimal - return busvoodoo_uart_generic_action(action+1, repetition, perform); // just retry without leading 0 + } else if ('x' == action[1] || 'b' == action[1]) { // send hex/binary + return busvoodoo_uart_generic_action(action + 1, repetition, perform); // just retry without leading 0 + } else if (action[1] >= '0' && action[1] <= '9') { // send decimal + return busvoodoo_uart_generic_action(action + 1, repetition, perform); // just retry without leading 0 } else { // malformed action return false; } - } else if ('x'==action[0] && length>1) { // send hexadecimal value - for (uint32_t i=1; i='0' && action[i]<='9') || (action[i]>='a' && action[i]<='f') || (action[i]>='A' && action[i]<='F'))) { // check for hexadecimal character + } else if ('x' == action[0] && length > 1) { // send hexadecimal value + for (uint32_t i = 1; i < length; i++) { // check string + if (!((action[i] >= '0' && action[i] <= '9') || (action[i] >= 'a' && action[i] <= 'f') || (action[i] >= 'A' && action[i] <= 'F'))) { // check for hexadecimal character return false; // not an hexadecimal string } } @@ -450,9 +450,9 @@ static bool busvoodoo_uart_generic_action(const char* action, uint32_t repetitio for (uint32_t i=0; i1) { // send binary value - for (uint32_t i=1; i'1') { // check for binary character + } else if ('b' == action[0] && length>1) { // send binary value + for (uint32_t i = 1; i < length; i++) { // check string + if (action[i] < '0' || action[i] > '1') { // check for binary character return false; // not a binary string } } @@ -460,12 +460,12 @@ static bool busvoodoo_uart_generic_action(const char* action, uint32_t repetitio return true; } uint32_t value = strtol(&action[1], NULL, 2); // get binary value - for (uint32_t i=0; i='1' && action[0]<='9') { // send decimal value - for (uint32_t i=1; i'9') { // check for decimal character + } else if (action[0] >= '1' && action[0] <= '9') { // send decimal value + for (uint32_t i = 1; i < length; i++) { // check string + if (action[i] < '0' || action[i] > '9') { // check for decimal character return false; // not a decimal string } } @@ -473,15 +473,15 @@ static bool busvoodoo_uart_generic_action(const char* action, uint32_t repetitio return true; } uint32_t value = strtol(&action[0], NULL, 10); // get decimal value - for (uint32_t i=0; i=2 && ('"'==action[0] || '\''==action[0]) && (action[length-1]==action[0])) { // send ASCII character + } else if (length >= 2 && ('"' == action[0] || '\'' == action[0]) && (action[length - 1] == action[0])) { // send ASCII character if (!perform) { return true; } - for (uint32_t r=0; rtx_pre) { (*busvoodoo_uart_generic_specific->tx_pre)(); } - if (NULL==argument || 0==strlen(argument)) { // nothing to transmit + if (NULL == argument || 0 == strlen(argument)) { // nothing to transmit argument = "\r\n"; // transmit CR+LF } printf("press any key to exit\n"); - for (uint16_t i=0; ((char*)(argument))[i] && !user_input_available; i++) { - while ((0==(USART_SR(busvoodoo_uart_generic_specific->usart) & USART_SR_TXE) && !user_input_available)); // wait for transmit buffer to be empty + for (uint16_t i = 0; ((char*)(argument))[i] && !user_input_available; i++) { + while ((0 == (USART_SR(busvoodoo_uart_generic_specific->usart) & USART_SR_TXE) && !user_input_available)); // wait for transmit buffer to be empty if (USART_SR(busvoodoo_uart_generic_specific->usart) & USART_SR_TXE) { // we can send a character printf("%c", ((char*)(argument))[i]); // echo character to transmit busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show transmission usart_enhanced_send(busvoodoo_uart_generic_specific->usart, ((char*)(argument))[i]); // transmit character } } - while ((0==((USART_SR(busvoodoo_uart_generic_specific->usart)) & USART_SR_TC) && !user_input_available)); // wait for transfer to be complete + while ((0 == ((USART_SR(busvoodoo_uart_generic_specific->usart)) & USART_SR_TC) && !user_input_available)); // wait for transfer to be complete if (user_input_available) { // user interrupted flow user_input_get(); // discard user input } @@ -571,17 +571,17 @@ static void busvoodoo_uart_generic_command_receive(void* argument) { bool display_hex = false; // display in hex bool display_bin = false; // display in bin - if (NULL!=argument && strlen(argument)>0) { - if (0==strcmp(argument, "h") || 0==strcmp(argument, "hex")) { // user wants hexadecimal display + if (NULL != argument && strlen(argument) > 0) { + if (0 == strcmp(argument, "h") || 0 == strcmp(argument, "hex")) { // user wants hexadecimal display display_hex = true; // remember to display in hexadecimal - } else if (0==strcmp(argument, "b") || 0==strcmp(argument, "bin")) { // user wants binary display + } else if (0 == strcmp(argument, "b") || 0 == strcmp(argument, "bin")) { // user wants binary display display_bin = true; // remember to display in binary } else { printf("malformed argument\n"); return; } } - if (NULL==busvoodoo_uart_generic_specific) { + if (NULL == busvoodoo_uart_generic_specific) { return; } if (busvoodoo_uart_generic_specific->rx_pre) { @@ -614,7 +614,7 @@ static void busvoodoo_uart_generic_command_receive(void* argument) static void busvoodoo_uart_generic_command_transceive(void* argument) { (void)argument; // we won't use the argument - if (NULL==busvoodoo_uart_generic_specific) { + if (NULL == busvoodoo_uart_generic_specific) { return; } if (busvoodoo_uart_generic_specific->rx_pre) { @@ -626,26 +626,26 @@ static void busvoodoo_uart_generic_command_transceive(void* argument) while (true) { // check for escape sequence if (user_input_available) { // check if user wants to transmit something char c = user_input_get(); // get user input - if (0x1b==c) { // user pressed escape - if (0x1b!=last_c) { // this is the first escape press + if (0x1b == c) { // user pressed escape + if (0x1b != last_c) { // this is the first escape press esc_count = 0; } esc_count++; // increment escape count } last_c = c; // remember key press - if (esc_count<5) { // check for escape sequence + if (esc_count < 5) { // check for escape sequence if (busvoodoo_uart_generic_specific->rx_post) { (*busvoodoo_uart_generic_specific->rx_post)(); } if (busvoodoo_uart_generic_specific->tx_pre) { (*busvoodoo_uart_generic_specific->tx_pre)(); } - while ((0==(USART_SR(busvoodoo_uart_generic_specific->usart) & USART_SR_TXE) && !user_input_available)); // wait for transmit buffer to be empty + while ((0 == (USART_SR(busvoodoo_uart_generic_specific->usart) & USART_SR_TXE) && !user_input_available)); // wait for transmit buffer to be empty if (USART_SR(busvoodoo_uart_generic_specific->usart) & USART_SR_TXE) { // we can send a character usart_enhanced_send(busvoodoo_uart_generic_specific->usart, c); // send user character busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // enable blue LED to show transmission } - while ((0==((USART_SR(busvoodoo_uart_generic_specific->usart)) & USART_SR_TC) && !user_input_available)); // wait for transfer to be complete + while ((0 == ((USART_SR(busvoodoo_uart_generic_specific->usart)) & USART_SR_TC) && !user_input_available)); // wait for transfer to be complete if (busvoodoo_uart_generic_specific->tx_post) { (*busvoodoo_uart_generic_specific->tx_post)(); } @@ -719,7 +719,7 @@ void TIM_ISR(2)(void) static uint32_t pulse = UINT32_MAX; // measured pulse duration (MAX is an invalid values) if (timer_get_flag(busvoodoo_uart_generic_specific->timer, TIM_SR_UIF)) { // overflow update event happened timer_clear_flag(busvoodoo_uart_generic_specific->timer, TIM_SR_UIF); // clear flag - if (pulse>(UINT32_MAX-0x10000)) { // we can't measure longer pulser (and baud rate < 0.017 bps make no sense) + if (pulse > (UINT32_MAX - 0x10000)) { // we can't measure longer pulser (and baud rate < 0.017 bps make no sense) pulse = UINT32_MAX; // invalidate measured pulse } else { pulse += 0x10000; // account for the 16-bit timer limit @@ -731,8 +731,8 @@ void TIM_ISR(2)(void) } if (timer_get_flag(busvoodoo_uart_generic_specific->timer, busvoodoo_uart_generic_specific->timer_sr_ccif)) { uint16_t edge = *busvoodoo_uart_generic_specific->timer_ccr; // retrieve captured value (clears flag) - if (UINT32_MAX!=pulse) { // only calculate pulse if previous edge is valid - pulse = ((pulse&0xffff0000)+edge)-(pulse&0xffff); // calculate pulse duration + if (UINT32_MAX != pulse) { // only calculate pulse if previous edge is valid + pulse = ((pulse & 0xffff0000) + edge) - (pulse & 0xffff); // calculate pulse duration if (pulsetimer_rst); // reset timer state timer_disable_counter(busvoodoo_uart_generic_specific->timer); // disable timer to configure it timer_set_mode(busvoodoo_uart_generic_specific->timer, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up - timer_set_prescaler(busvoodoo_uart_generic_specific->timer, 1-1); // don't use prescale so to get the most precise measurement + timer_set_prescaler(busvoodoo_uart_generic_specific->timer, 1 - 1); // don't use prescale so to get the most precise measurement timer_ic_set_input(busvoodoo_uart_generic_specific->timer, busvoodoo_uart_generic_specific->timer_ic, busvoodoo_uart_generic_specific->timer_ic_in_ti); // configure the input capture ICx to use the right channel TIn timer_ic_set_filter(busvoodoo_uart_generic_specific->timer, busvoodoo_uart_generic_specific->timer_ic, TIM_IC_OFF); // use no filter input to keep precise timing /* ideally we would trigger on any edge, allowing to measure the bit width (on 010 or 101 bit pattern) and calculate the correct baud rate. @@ -824,7 +824,7 @@ static void busvoodoo_uart_generic_command_detect(void* argument) while (!user_input_available) { if (reset_state) { // reset the configuration rx_errors = 0; - for (uint8_t i=0; iusart); // clear input buffer and allow flag to be set @@ -834,14 +834,14 @@ static void busvoodoo_uart_generic_command_detect(void* argument) if (pulse_flag) { // new pulse duration has been measured pulse_flag = false; // clear flag printf("u"); - uint32_t baudrate = rcc_ahb_frequency/(pulse_duration/2); // calculate baud rate based on measured timing - if (baudrate>uart_baudrate+100) { // new higher baud rate detected + uint32_t baudrate = rcc_ahb_frequency / (pulse_duration / 2); // calculate baud rate based on measured timing + if (baudrate > uart_baudrate + 100) { // new higher baud rate detected uart_baudrate = baudrate; // save new baud rate - if (uart_baudrate>=1200) { // ensure minimum hardware supported baud rate is respected + if (uart_baudrate >= 1200) { // ensure minimum hardware supported baud rate is respected // search for closest standard baud rate uint32_t standard_baudrate = 0; - for (uint8_t i=0; i=baudrates[i]*0.9 && uart_baudrate<=baudrates[i]*1.1) { // measured baud rate matches standard baud rate within factor + for (uint8_t i = 0; i < LENGTH(baudrates); i++) { + if (uart_baudrate >= baudrates[i] * 0.9 && uart_baudrate <= baudrates[i] * 1.1) { // measured baud rate matches standard baud rate within factor standard_baudrate = baudrates[i]; // remember matching baud rate break; // stop searching for matching baud rate } @@ -861,12 +861,12 @@ static void busvoodoo_uart_generic_command_detect(void* argument) if (USART_SR(busvoodoo_uart_generic_specific->usart) & (USART_SR_NE|USART_SR_FE)) { // error on UART received usart_recv(busvoodoo_uart_generic_specific->usart); // clear input buffer and flags rx_errors++; // increment number of errors - if (rx_errors>=5) { // the format seems wrong + if (rx_errors >= 5) { // the format seems wrong // the threshold must be high enough so the UART peripheral has enough opportunities to synchronize to the start bit (just after and idle frame) // two high probable frame error causes: // - when set to 9 data-bits with high speed 8 data-bits traffic incoming: the next start bit comes right after the stop bit of and 8-bit frame, which is interpreted as faulty 9 data-bits frame stop bit // - when set to 8 data-bits with 9 data-bits (8+1 parity) traffic incoming: the low parity bit is interpreted as faulty stop-bit - uart_databits = ((8==uart_databits) ? 9 : 8); // switch between 8 and 9-bit packets + uart_databits = ((8 == uart_databits) ? 9 : 8); // switch between 8 and 9-bit packets usart_disable(busvoodoo_uart_generic_specific->usart); // disable UART before reconfiguring usart_set_databits(busvoodoo_uart_generic_specific->usart, uart_databits); // set new data width reset_state = true; @@ -939,7 +939,7 @@ static void busvoodoo_uart_generic_command_detect(void* argument) bool no_valid_configuration = true; uint8_t new_valid_configuration = LENGTH(uart_configurations); char parity = '?'; - for (uint8_t i=0; i