BV UART: add hex and bin display in receive command

This commit is contained in:
King Kévin 2018-03-22 15:05:09 +01:00
parent d8f04beb3f
commit 5d820ce6fa
1 changed files with 54 additions and 7 deletions

View File

@ -296,17 +296,64 @@ static void busvoodoo_uart_command_transmit(void* argument)
}
/** command to receive data
* @param[in] argument no argument required
* @param[in] argument in which format to display
*/
static void busvoodoo_uart_command_receive(void* argument)
{
(void)argument; // we won't use the 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
display_hex = true; // remember to display in hexadecimal
} 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;
}
}
printf("press any key to exit\n");
while (!user_input_available) { // check for user input to exit
if ((USART_SR(USART(BUSVOODOO_USART_ID)) & USART_SR_RXNE)) { // verify if data has been received
char c = usart_recv(USART(BUSVOODOO_USART_ID)); // receive character
uint16_t c = usart_recv(USART(BUSVOODOO_USART_ID)); // receive character
busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // enable blue LED to show reception
printf("%c", c); // print received character
// remove unused bits (ignore parity bit)
if (USART_PARITY_NONE==busvoodoo_uart_parity) { // no parity bit in frame
if (8==busvoodoo_uart_databits) { // 8-bit frame
c &= 0xff;
} else { // 9-bit frame
c &= 0x1ff;
}
} else { // MSb is parity bit
if (8==busvoodoo_uart_databits) { // 8-bit frame
c &= 0x7f;
} else { // 9-bit frame
c &= 0xff;
}
}
if (display_hex) { // display data in hex
if ((USART_PARITY_NONE==busvoodoo_uart_parity) && 9==busvoodoo_uart_databits) { // case where the final data is 9 bits long
printf("%03x ", c);
} else {
printf("%02x ", c);
}
} else if (display_bin) { // display data in binary
if (USART_PARITY_NONE==busvoodoo_uart_parity) {
if (8==busvoodoo_uart_databits) { // 8-bit frame
printf("%08b ", c);
} else { // 9-bit frame
printf("%09b ", c);
}
} else { // one bit is a parity bit
if (8==busvoodoo_uart_databits) { // 8-bit frame
printf("%07b ", c);
} else { // 9-bit frame
printf("%08b ", c);
}
}
} else { // display in ASCII
printf("%c", c); // print received character
}
}
}
user_input_get(); // discard user input
@ -356,9 +403,9 @@ static const struct menu_command_t busvoodoo_uart_commands[] = {
{
'r',
"receive",
"show incoming data",
MENU_ARGUMENT_NONE,
NULL,
"show incoming data [in hexadecimal or binary]",
MENU_ARGUMENT_STRING,
"[hex|bin]",
&busvoodoo_uart_command_receive,
},
{