From 44f2a2a9521ee295acbe44cf0d2a5dedd179fd37 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 16 Jun 2017 13:57:11 -0400 Subject: [PATCH] command: Move low-level sendf transmission into board code Export a new console_sendf() function from the board code instead of console_get_output() and console_push_output(). This enables more flexibility in how the board specific code produces output. Signed-off-by: Kevin O'Connor --- src/avr/serial.c | 20 +++++++++++++++++--- src/avr/usbserial.c | 19 +++++++++++++++++-- src/command.c | 16 +++++----------- src/command.h | 22 +++++++++++++--------- src/generic/misc.h | 5 +++-- src/pru/main.c | 17 +++++++++++++++-- src/sam3x8e/serial.c | 17 +++++++++++++++-- src/simulator/main.c | 11 ++--------- 8 files changed, 87 insertions(+), 40 deletions(-) diff --git a/src/avr/serial.c b/src/avr/serial.c index 213b10ed..87067c7a 100644 --- a/src/avr/serial.c +++ b/src/avr/serial.c @@ -10,8 +10,9 @@ #include "board/io.h" // readb #include "board/misc.h" // console_get_input #include "command.h" // DECL_CONSTANT -#include "sched.h" // DECL_INIT #include "irq.h" // irq_save +#include "pgm.h" // READP +#include "sched.h" // DECL_INIT static char receive_buf[192]; static uint8_t receive_pos; @@ -139,7 +140,7 @@ console_task(void) DECL_TASK(console_task); // Return an output buffer that the caller may fill with transmit messages -char * +static char * console_get_output(uint8_t len) { uint8_t tpos = readb(&transmit_pos), tmax = readb(&transmit_max); @@ -164,9 +165,22 @@ console_get_output(uint8_t len) } // Accept the given number of bytes added to the transmit buffer -void +static void console_push_output(uint8_t len) { writeb(&transmit_max, readb(&transmit_max) + len); enable_tx_irq(); } + +// Encode and transmit a "response" message +void +console_sendf(const struct command_encoder *ce, va_list args) +{ + uint8_t buf_len = READP(ce->max_size); + char *buf = console_get_output(buf_len); + if (!buf) + return; + uint8_t msglen = command_encodef(buf, buf_len, ce, args); + command_add_frame(buf, msglen); + console_push_output(msglen); +} diff --git a/src/avr/usbserial.c b/src/avr/usbserial.c index 78ef719e..7be94a67 100644 --- a/src/avr/usbserial.c +++ b/src/avr/usbserial.c @@ -7,6 +7,8 @@ #include // USART0_RX_vect #include // memmove #include "../lib/pjrc_usb_serial/usb_serial.h" +#include "command.h" // command_dispatch +#include "pgm.h" // READP #include "sched.h" // DECL_INIT #define USBSERIAL_BUFFER_SIZE 64 @@ -62,7 +64,7 @@ console_task(void) DECL_TASK(console_task); // Return an output buffer that the caller may fill with transmit messages -char * +static char * console_get_output(uint8_t len) { if (len > sizeof(transmit_buf)) @@ -71,9 +73,22 @@ console_get_output(uint8_t len) } // Accept the given number of bytes added to the transmit buffer -void +static void console_push_output(uint8_t len) { usb_serial_write((void*)transmit_buf, len); usb_serial_flush_output(); } + +// Encode and transmit a "response" message +void +console_sendf(const struct command_encoder *ce, va_list args) +{ + uint8_t buf_len = READP(ce->max_size); + char *buf = console_get_output(buf_len); + if (!buf) + return; + uint8_t msglen = command_encodef(buf, buf_len, ce, args); + command_add_frame(buf, msglen); + console_push_output(msglen); +} diff --git a/src/command.c b/src/command.c index 709e75c4..62db0bfd 100644 --- a/src/command.c +++ b/src/command.c @@ -105,7 +105,7 @@ error: } // Encode a message -static uint8_t +uint8_t command_encodef(char *buf, uint8_t buf_len , const struct command_encoder *ce, va_list args) { @@ -169,7 +169,7 @@ error: } // Add header and trailer bytes to a message block -static void +void command_add_frame(char *buf, uint8_t msglen) { buf[MESSAGE_POS_LEN] = msglen; @@ -184,7 +184,7 @@ static uint8_t in_sendf; // Encode and transmit a "response" message void -_sendf(const struct command_encoder *ce, ...) +command_sendf(const struct command_encoder *ce, ...) { if (readb(&in_sendf)) // This sendf call was made from an irq handler while the main @@ -192,17 +192,11 @@ _sendf(const struct command_encoder *ce, ...) return; writeb(&in_sendf, 1); - uint8_t buf_len = READP(ce->max_size); - char *buf = console_get_output(buf_len); - if (!buf) - goto done; va_list args; va_start(args, ce); - uint8_t msglen = command_encodef(buf, buf_len, ce, args); + console_sendf(ce, args); va_end(args); - command_add_frame(buf, msglen); - console_push_output(msglen); -done: + writeb(&in_sendf, 0); return; } diff --git a/src/command.h b/src/command.h index f2d90ee4..148f47be 100644 --- a/src/command.h +++ b/src/command.h @@ -1,6 +1,7 @@ #ifndef __COMMAND_H #define __COMMAND_H +#include // va_list #include #include // uint8_t #include "ctr.h" // DECL_CTR @@ -20,11 +21,11 @@ // Send an output message (and declare a static message type for it) #define output(FMT, args...) \ - _sendf(_DECL_OUTPUT(FMT) , ##args ) + command_sendf(_DECL_OUTPUT(FMT) , ##args ) // Declare a message type and transmit it. #define sendf(FMT, args...) \ - _sendf(_DECL_ENCODER(FMT) , ##args ) + command_sendf(_DECL_ENCODER(FMT) , ##args ) // Shut down the machine (also declares a static string to transmit) #define shutdown(msg) \ @@ -32,13 +33,6 @@ #define try_shutdown(msg) \ sched_try_shutdown(_DECL_STATIC_STR(msg)) -// command.c -struct command_encoder; -void _sendf(const struct command_encoder *ce, ...); -int8_t command_find_block(char *buf, uint8_t buf_len, uint8_t *pop_count); -void command_dispatch(char *buf, uint8_t msglen); - -// out/compile_time_request.c (auto generated file) struct command_encoder { uint8_t msg_id, max_size, num_params; const uint8_t *param_types; @@ -52,6 +46,16 @@ enum { PT_uint32, PT_int32, PT_uint16, PT_int16, PT_byte, PT_string, PT_progmem_buffer, PT_buffer, }; + +// command.c +uint8_t command_encodef(char *buf, uint8_t buf_len + , const struct command_encoder *ce, va_list args); +void command_sendf(const struct command_encoder *ce, ...); +void command_add_frame(char *buf, uint8_t msglen); +int8_t command_find_block(char *buf, uint8_t buf_len, uint8_t *pop_count); +void command_dispatch(char *buf, uint8_t msglen); + +// out/compile_time_request.c (auto generated file) extern const struct command_parser command_index[]; extern const uint8_t command_index_size; extern const uint8_t command_identify_data[]; diff --git a/src/generic/misc.h b/src/generic/misc.h index 9847e9a9..960eb6a2 100644 --- a/src/generic/misc.h +++ b/src/generic/misc.h @@ -1,11 +1,12 @@ #ifndef __GENERIC_MISC_H #define __GENERIC_MISC_H +#include // va_list #include // size_t #include // uint8_t -char *console_get_output(uint8_t len); -void console_push_output(uint8_t len); +struct command_encoder; +void console_sendf(const struct command_encoder *ce, va_list args); uint32_t timer_from_us(uint32_t us); uint8_t timer_is_before(uint32_t time1, uint32_t time2); diff --git a/src/pru/main.c b/src/pru/main.c index cebcaa26..d906b1ad 100644 --- a/src/pru/main.c +++ b/src/pru/main.c @@ -131,7 +131,7 @@ console_task(void) DECL_TASK(console_task); // Return an output buffer that the caller may fill with transmit messages -char * +static char * console_get_output(uint8_t len) { if (len > sizeof(SHARED_MEM->send_data[0].data)) @@ -144,7 +144,7 @@ console_get_output(uint8_t len) } // Accept the given number of bytes added to the transmit buffer -void +static void console_push_output(uint8_t len) { uint32_t send_push_pos = SHARED_MEM->send_push_pos; @@ -154,6 +154,19 @@ console_push_output(uint8_t len) (send_push_pos + 1) % ARRAY_SIZE(SHARED_MEM->send_data)); } +// Encode and transmit a "response" message +void +console_sendf(const struct command_encoder *ce, va_list args) +{ + uint8_t buf_len = ce->max_size; + char *buf = console_get_output(buf_len); + if (!buf) + return; + uint8_t msglen = command_encodef(buf, buf_len, ce, args); + command_add_frame(buf, msglen); + console_push_output(msglen); +} + /**************************************************************** * Allocator diff --git a/src/sam3x8e/serial.c b/src/sam3x8e/serial.c index c0653443..3657d9df 100644 --- a/src/sam3x8e/serial.c +++ b/src/sam3x8e/serial.c @@ -130,7 +130,7 @@ console_task(void) DECL_TASK(console_task); // Return an output buffer that the caller may fill with transmit messages -char * +static char * console_get_output(uint8_t len) { uint32_t tpos = readl(&transmit_pos), tmax = readl(&transmit_max); @@ -155,9 +155,22 @@ console_get_output(uint8_t len) } // Accept the given number of bytes added to the transmit buffer -void +static void console_push_output(uint8_t len) { writel(&transmit_max, readl(&transmit_max) + len); enable_tx_irq(); } + +// Encode and transmit a "response" message +void +console_sendf(const struct command_encoder *ce, va_list args) +{ + uint8_t buf_len = ce->max_size; + char *buf = console_get_output(buf_len); + if (!buf) + return; + uint8_t msglen = command_encodef(buf, buf_len, ce, args); + command_add_frame(buf, msglen); + console_push_output(msglen); +} diff --git a/src/simulator/main.c b/src/simulator/main.c index bdad1a6a..f0e18737 100644 --- a/src/simulator/main.c +++ b/src/simulator/main.c @@ -85,16 +85,9 @@ timer_read_time(void) * Turn stdin/stdout into serial console ****************************************************************/ -// Return an output buffer that the caller may fill with transmit messages -char * -console_get_output(uint8_t len) -{ - return NULL; -} - -// Accept the given number of bytes added to the transmit buffer +// Encode and transmit a "response" message void -console_push_output(uint8_t len) +console_sendf(const struct command_encoder *ce, va_list args) { }