sam3x8e: Integrate serial console functions

Now that console_get_input(), console_pop_input(),
console_get_output() and console_push_output() are local functions,
integrate them into their callers.  This simplifies the code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2017-06-20 11:50:19 -04:00
parent e681369a1a
commit 849096d5f3
1 changed files with 30 additions and 48 deletions

View File

@ -82,17 +82,9 @@ enable_tx_irq(void)
* Console access functions
****************************************************************/
// Return a buffer (and length) containing any incoming messages
static char *
console_get_input(uint8_t *plen)
{
*plen = readl(&receive_pos);
return receive_buf;
}
// Remove from the receive buffer the given number of bytes
static void
console_pop_input(uint8_t len)
console_pop_input(uint32_t len)
{
uint32_t copied = 0;
for (;;) {
@ -119,58 +111,48 @@ console_pop_input(uint8_t len)
void
console_task(void)
{
uint8_t buf_len, pop_count;
char *buf = console_get_input(&buf_len);
int8_t ret = command_find_block(buf, buf_len, &pop_count);
uint8_t pop_count;
uint32_t rpos = readl(&receive_pos);
int8_t ret = command_find_block(receive_buf, rpos, &pop_count);
if (ret > 0)
command_dispatch(buf, pop_count);
command_dispatch(receive_buf, pop_count);
if (ret)
console_pop_input(pop_count);
}
DECL_TASK(console_task);
// Return an output buffer that the caller may fill with transmit messages
static char *
console_get_output(uint8_t len)
// Encode and transmit a "response" message
void
console_sendf(const struct command_encoder *ce, va_list args)
{
// Verify space for message
uint32_t tpos = readl(&transmit_pos), tmax = readl(&transmit_max);
if (tpos >= tmax) {
tpos = tmax = 0;
writel(&transmit_max, 0);
writel(&transmit_pos, 0);
}
if (tmax + len <= sizeof(transmit_buf))
return &transmit_buf[tmax];
if (tmax - tpos + len > sizeof(transmit_buf))
return NULL;
// Disable TX irq and move buffer
writel(&transmit_max, 0);
tpos = readl(&transmit_pos);
tmax -= tpos;
memmove(&transmit_buf[0], &transmit_buf[tpos], tmax);
writel(&transmit_pos, 0);
writel(&transmit_max, tmax);
enable_tx_irq();
return &transmit_buf[tmax];
}
uint32_t max_size = ce->max_size;
if (tmax + max_size > sizeof(transmit_buf)) {
if (tmax + max_size - tpos > sizeof(transmit_buf))
// Not enough space for message
return;
// Disable TX irq and move buffer
writel(&transmit_max, 0);
tpos = readl(&transmit_pos);
tmax -= tpos;
memmove(&transmit_buf[0], &transmit_buf[tpos], tmax);
writel(&transmit_pos, 0);
writel(&transmit_max, tmax);
enable_tx_irq();
}
// Accept the given number of bytes added to the transmit buffer
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);
// Generate message
char *buf = &transmit_buf[tmax];
uint32_t msglen = command_encodef(buf, max_size, ce, args);
command_add_frame(buf, msglen);
console_push_output(msglen);
// Start message transmit
writel(&transmit_max, tmax + msglen);
enable_tx_irq();
}