Skip to content

Commit

Permalink
Comically poor implementation of UART forwarding complete. Need to sp…
Browse files Browse the repository at this point in the history
…lit off into seperate task
  • Loading branch information
danieldegrasse committed Aug 14, 2020
1 parent cd20145 commit 1ab3b7c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 23 deletions.
17 changes: 12 additions & 5 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,17 @@ static int logfile_size(CLIContext *ctx, char **argv, int argc) {
* @return 0 on success, or another value on failure
*/
static int connect_log(CLIContext *ctx, char **argv, int argc) {
UART_Queue_Elem *elem;
// For now, just read from queue.
FORWARD_UART_LOGS = true;
elem = Queue_get(uart_log_queue);
cli_printf(ctx, "Latest char was %c\r\n", elem->data);
char c;
enable_log_forwarding();
while (1)
{
dequeue_logger_data(&c);
// temporary. Just a way to get out of loop.
cli_printf(ctx, "%c", c);
if (c == 'x') {
break;
}
}
disable_log_forwarding();
return 0;
}
2 changes: 1 addition & 1 deletion sd_logger.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ Program.global.uart_console = Task.create("&uart_task_entry", task1Params);
/* Uart logger reads UART data and logs it to the SD card. */
var task2Par = new Task.Params();
task2Par.instance.name = "uart_logger";
task2Par.stackSize = 1024;
task2Par.stackSize = 2048;
Program.global.uart_logger = Task.create("&uart_logger_task_entry", task2Par);
Expand Down
62 changes: 59 additions & 3 deletions uart_logger_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,22 @@
#define LOG_BAUD_RATE 115200
#define UART_LOGDEV Board_UART3

/*
* Statically allocated queue for received UART log data. Used for forward data
* when requested. See "Queue Creation" in cfg file.
*/
extern Queue_Handle uart_log_queue;

// Queue element structure.
typedef struct {
Queue_Elem elem;
char data;
} UART_Queue_Elem;

// If set to true, this task will enqueue UART data as it reads it.
bool FORWARD_UART_LOGS = false;
// Maximum number of chars we will enqueue.
#define MAX_QUEUE 64

static UART_Handle uart;
static UART_Params params;
Expand Down Expand Up @@ -68,7 +82,8 @@ void uart_logger_prebios(void) {
*/
void uart_logger_task_entry(UArg arg0, UArg arg1) {
char read_char;
UART_Queue_Elem queue_element;
UART_Queue_Elem queue_elements[MAX_QUEUE];
int queue_idx = 0;
/*
* Try to mount the SD card, and if it fails wait for the sd_ready
* condition.
Expand All @@ -94,9 +109,16 @@ void uart_logger_task_entry(UArg arg0, UArg arg1) {
}
// If requested via bool, enqueue data as well.
if (FORWARD_UART_LOGS) {
queue_element.data = read_char;
// Use the next statically allocated queue element.
queue_elements[queue_idx].data = read_char;
// Add the element to the queue (atomically).
Queue_put(uart_log_queue, &(queue_element.elem));
Queue_put(uart_log_queue,
&(queue_elements[queue_idx].elem));
queue_idx++;
if (queue_idx >= MAX_QUEUE) {
// Wrap the queue element index back to 0.
queue_idx = 0;
}
}
} else {
System_printf("SD card was unmounted\n");
Expand All @@ -108,4 +130,38 @@ void uart_logger_task_entry(UArg arg0, UArg arg1) {
// Wait for SD card to be remounted.
wait_sd_ready();
}
}

/**
* Enables UART log forwarding. After this function is called, get_log_char()
* will work as expected, since the logger task will be enqueuing data.
*/
void enable_log_forwarding(void) { FORWARD_UART_LOGS = true; }

/**
* Disables UART log forwarding. After this function is called, get_log_char()
* will stop working, since data will not longer be enqueued.
*/
void disable_log_forwarding(void) { FORWARD_UART_LOGS = false; }

/**
* Gets a char of data from the UART logger's queue. Useful if another task
* wants to monitor the UART logger, outside of the SD card writes.
* Notes:
* enable_log_forwarding() should be called to alert the logger task to enqueue
* data it reads, or this function won't work.
* disable_log_forwarding() should be called when the logs are not needed,
* to improve performance.
* If logging is enabled and data is not read periodically, the circular
* queue element buffer will be exhausted and data WILL be lost.
* @param out: char of data returned from queue.
*/
void dequeue_logger_data(char *out) {
UART_Queue_Elem *elem;
/*
* Remove the element atomically
* (we expect other tasks to call this function)
*/
elem = Queue_get(uart_log_queue);
*out = elem->data;
}
41 changes: 27 additions & 14 deletions uart_logger_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,34 @@
#include <stdbool.h>
#include <ti/sysbios/knl/Queue.h>

/*
* Statically allocated queue for received UART log data. Used for forward data
* when requested. See "Queue Creation" in cfg file.
*/
extern Queue_Handle uart_log_queue;
// Declared in uart_logger_task.c
extern bool FORWARD_UART_LOGS;
// Queue element structure.
typedef struct {
Queue_Elem elem;
char data;
} UART_Queue_Elem;

/*
* PreOS Task for UART logger. Sets up uart instance for data transmission,
* This code MUST be called before the BIOS is started.
*/
void uart_logger_prebios(void);
void uart_logger_prebios(void);

/**
* Enables UART log forwarding. After this function is called, get_log_char()
* will work as expected, since the logger task will be enqueuing data.
*/
void enable_log_forwarding(void);

/**
* Disables UART log forwarding. After this function is called, get_log_char()
* will stop working, since data will not longer be enqueued.
*/
void disable_log_forwarding(void);

/**
* Gets a char of data from the UART logger's queue. Useful if another task
* wants to monitor the UART logger, outside of the SD card writes.
* Notes:
* enable_log_forwarding() should be called to alert the logger task to enqueue
* data it reads, or this function won't work.
* disable_log_forwarding() should be called when the logs are not needed,
* to improve performance.
* If logging is enabled and data is not read periodically, the circular
* queue element buffer will be exhausted and data WILL be lost.
* @param out: char of data returned from queue.
*/
void dequeue_logger_data(char *out);

0 comments on commit 1ab3b7c

Please sign in to comment.