BV UART: add action command

This commit is contained in:
King Kévin 2018-03-22 15:57:49 +01:00
parent 5d820ce6fa
commit 760b8c2a46
1 changed files with 230 additions and 0 deletions

View File

@ -268,8 +268,230 @@ static void busvoodoo_uart_exit(void)
busvoodoo_embedded_pullup(false); // disable embedded pull-ups
}
/** write to UART
* @param[in] value value to write
*/
static void busvoodoo_uart_write(uint16_t value)
{
while ((0==(USART_SR(USART(BUSVOODOO_USART_ID)) & USART_SR_TXE) && !user_input_available)); // wait for transmit buffer to be empty (or user to interrupt)
if (USART_SR(USART(BUSVOODOO_USART_ID)) & USART_SR_TXE) { // we can send a character
// remove unused bits
if (USART_PARITY_NONE==busvoodoo_uart_parity) { // no parity bit in frame
if (8==busvoodoo_uart_databits) { // 8-bit frame
value &= 0xff;
} else { // 9-bit frame
value &= 0x1ff;
}
} else { // MSb is parity bit
if (8==busvoodoo_uart_databits) { // 8-bit frame
value &= 0x7f;
} else { // 9-bit frame
value &= 0xff;
}
}
// send data
busvoodoo_led_red_pulse(BUSVOODOO_LED_PULSE); // pulse red LED to show transmission
usart_send(USART(BUSVOODOO_USART_ID), value); // transmit character
// display data send
printf("write: '%c'/0x", value);
if ((USART_PARITY_NONE==busvoodoo_uart_parity) && 9==busvoodoo_uart_databits) { // case where the final data is 9 bits long
printf("%03x\n", value);
} else {
printf("%02x\n", value);
}
}
}
/** read from UART
*/
static void busvoodoo_uart_read(void)
{
printf("read: ");
while (!(USART_SR(USART(BUSVOODOO_USART_ID)) & USART_SR_RXNE) && !user_input_available); // wait for incoming data to be available (or user input to exit)
if ((USART_SR(USART(BUSVOODOO_USART_ID)) & USART_SR_RXNE)) { // verify if data has been received
busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // enable blue LED to show reception
// get the errors
bool error_noise = (0!=(USART_SR(USART(BUSVOODOO_USART_ID)) & USART_SR_NE)); // read noise error flag
bool error_framing = (0!=(USART_SR(USART(BUSVOODOO_USART_ID)) & USART_SR_FE)); // read frame error flag
bool error_parity = (0!=(USART_SR(USART(BUSVOODOO_USART_ID)) & USART_SR_PE)); // read parity error flag
uint16_t c = usart_recv(USART(BUSVOODOO_USART_ID)); // read received character (also clears the error flags)
// remove unused bits
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;
}
}
// display data
printf("'%c'/0x", c);
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);
}
// display errors
printf("(");
if (error_noise) {
printf("noise");
} else if (error_framing) {
printf("framing");
} else if (error_parity) {
printf("parity");
} else {
printf("no");
}
printf(" error)");
}
printf("\n");
}
/** perform UART action
* @param[in] action action to perform
* @param[in] repetition how many times to perform the action
* @param[in] perform the action (true) or just check it (false)
* @return true if the action has been performed, false if it is malformed
*/
static bool busvoodoo_uart_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
return true;
}
if (1==length && 'r'==action[0]) { // read data
if (!perform) {
return true;
}
for (uint32_t i=0; i<repetition; i++) {
busvoodoo_uart_read(); // read from UART
}
} else if (1==length && 'u'==action[0]) { // sleep us
if (!perform) {
return true;
}
printf("wait for %u us\n", repetition);
sleep_us(repetition); // sleep
} else if (1==length && 'm'==action[0]) { // sleep ms
if (!perform) {
return true;
}
printf("wait for %u ms\n", repetition);
sleep_ms(repetition); // sleep
} else if ('0'==action[0]) { // send digit
if (1==length) { // just send 0
if (!perform) {
return true;
}
for (uint32_t i=0; i<repetition; i++) {
busvoodoo_uart_write(0); // write to UART
}
} else if ('x'==action[1] || 'b'==action[1]) { // send hex/binary
return busvoodoo_uart_action(action+1, repetition, perform); // just retry without leading 0
} else if (action[1]>='0' && action[1]<='9') { // send decimal
return busvoodoo_uart_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<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
}
}
if (!perform) {
return true;
}
uint32_t value = strtol(&action[1], NULL, 16); // get hex value
for (uint32_t i=0; i<repetition; i++) {
busvoodoo_uart_write(value); // write to SPI
}
} 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
}
}
if (!perform) {
return true;
}
uint32_t value = strtol(&action[1], NULL, 2); // get binary value
for (uint32_t i=0; i<repetition; i++) {
busvoodoo_uart_write(value); // write to SPI
}
} 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
}
}
if (!perform) {
return true;
}
uint32_t value = strtol(&action[0], NULL, 10); // get decimal value
for (uint32_t i=0; i<repetition; i++) {
busvoodoo_uart_write(value); // write to SPI
}
} 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; r<repetition; r++) {
for (uint32_t i=1; i<length-1; i++) { // go through string
busvoodoo_uart_write(action[i]); // write to SPI
}
}
} else { // malformed action
return false;
}
return true; // all went well
}
// command handlers
/** command to perform actions
* @param[in] argument actions to perform
*/
static void busvoodoo_uart_command_actions(void* argument)
{
if (NULL==argument || 0==strlen(argument)) {
printf("available actions (separated by space or ,):\n");
printf("0\twrite decimal value\n");
printf("0x0\twrite hexadecimal value\n");
printf("0b0\twrite binary value\n");
printf("\"a\"/'a'\twrite ASCII characters\n");
printf("r\tread value\n");
printf("u/m\twait 1 us/ms\n");
printf(":n\trepeat action n times\n");
return;
}
// copy argument since it will be modified
char* copy = calloc(strlen(argument)+1, sizeof(char));
if (!copy) {
while (true);
}
strncpy(copy, argument, strlen(argument)+1);
// verify and perform actions
if (!busvoodoo_global_actions(copy, false, &busvoodoo_uart_action)) { // verify actions
printf("malformed action(s)\n");
} else { // action are ok
printf("press any key to exit\n");
busvoodoo_global_actions(argument, true, &busvoodoo_uart_action); // perform action
if (user_input_available) { // user interrupted flow
user_input_get(); // discard user input
}
}
free(copy); // release memory
}
/** command to transmit a string
* @param[in] argument string to transmit (CR+LF when none provided)
*/
@ -400,6 +622,14 @@ static void busvoodoo_uart_command_transceive(void* argument)
/** UART menu commands */
static const struct menu_command_t busvoodoo_uart_commands[] = {
{
'a',
"action",
"perform protocol actions",
MENU_ARGUMENT_STRING,
"[actions]",
&busvoodoo_uart_command_actions,
},
{
'r',
"receive",