--- /dev/null
+/* Copyright (C) 2013, 2014 Czech Technical University in Prague
+ *
+ * Authors:
+ * - Carlos Jenkins <carlos@jenkins.co.cr>
+ *
+ * This document contains proprietary information belonging to Czech
+ * Technical University in Prague. Passing on and copying of this
+ * document, and communication of its contents is not permitted
+ * without prior written authorization.
+ *
+ * File : main.c
+ * Abstract:
+ * RPP test suite command processor.
+ *
+ * References:
+ * test.h
+ */
+
+
+#include <ctype.h> // isprint()
+#include <string.h> // strncmp()
+
+#include "rpp/rpp.h"
+#include "test.h"
+
+
+/// Command processor task -----------------------------------------------------
+#define INPUT_IDLE 50
+#define BUF_SIZE 80
+static char in_buffer[BUF_SIZE];
+static const char *prompt = "--> ";
+static const char *newline = "\r\n";
+
+
+/**
+ * Compare user string and execute command.
+ */
+void execute_cmd(char *string)
+{
+ if (strncmp(string, "can", BUF_SIZE) == 0)
+ test_can();
+ else if (strncmp(string, "eth", BUF_SIZE) == 0)
+ test_eth();
+ else if (strncmp(string, "fr", BUF_SIZE) == 0)
+ test_fr();
+ else if (strncmp(string, "lin", BUF_SIZE) == 0)
+ test_lin();
+ else if (strncmp(string, "sci", BUF_SIZE) == 0)
+ test_sci();
+ else if (strncmp(string, "help", BUF_SIZE) == 0) {
+ rpp_sci_printf((const char *)"Available commands:\r\n");
+ rpp_sci_printf((const char *)"\thelp - Display this help.\r\n");
+ rpp_sci_printf((const char *)"\tcan - Test CAN communication.\r\n");
+ rpp_sci_printf((const char *)"\teth - Test Ethernet communication.\r\n");
+ rpp_sci_printf((const char *)"\tfr - Test FlexRay communication.\r\n");
+ rpp_sci_printf((const char *)"\tlin - Test LIN communication.\r\n");
+ rpp_sci_printf((const char *)"\tsci - Test Serial Communication Interface.\r\n");
+ }
+ else
+ // Unknown command, print buffer back
+ rpp_sci_printf((const char *)
+ "ERROR: Unknown command \"%s\"\r\n", string
+ );
+}
+
+
+/**
+ * Test suite command processor.
+ */
+void test_cmdproc(void *par)
+{
+ // Print header
+ rpp_sci_printf((const char *)
+ "RPP Library Test Suite.\r\n"
+ );
+ rpp_sci_printf((const char *)
+ "===========================================================\r\n"
+ );
+ rpp_sci_printf((const char *)
+ "[Type a module to test or 'help']\r\n"
+ );
+
+ rpp_sci_printf((const char *)"%s", prompt);
+
+ uint8_t input = 0;
+ uint8_t buff_index = 0;
+ boolean_t flush = FALSE;
+ while (TRUE) {
+
+ // Get one character from the user
+ if (rpp_sci_read_nb(1, &input) != SUCCESS) {
+ vTaskDelay(INPUT_IDLE / portTICK_RATE_MS);
+ continue;
+ }
+
+ // Backspace and Delete
+ if (input == 8 || input == 127) {
+ if (buff_index > 0) {
+ buff_index--;
+ echo('\b');
+ echo(' ' );
+ echo('\b');
+ }
+ continue;
+ }
+ else if (input == 10 || input == 13) {
+ // Line feed or Carriage return
+ flush = TRUE;
+ echo('\r');
+ echo('\n');
+ }
+ else if (isprint(input)) {
+ // If is any printable character
+
+ // Store character and increment buffer index
+ in_buffer[buff_index] = input;
+ buff_index++;
+ echo(input);
+
+ // Check if buffer is full and force flush
+ if (buff_index == BUF_SIZE - 1)
+ flush = TRUE;
+ }
+ // All other character are ignored
+
+
+ // Flush buffer
+ if (flush) {
+
+ // Terminate string
+ in_buffer[buff_index] = '\0';
+
+ // Execute command
+ if (buff_index != 0)
+ execute_cmd((char *)&in_buffer);
+ rpp_sci_printf((const char *)"%s", newline);
+ rpp_sci_printf((const char *)"%s", prompt);
+
+ // Reset variables
+ rpp_sci_flush(TRUE);
+ buff_index = 0;
+ flush = FALSE;
+ }
+ }
+}
+
+
+
+/// Utilities ------------------------------------------------------------------
+/**
+ * Wait for user input "q" to quit the loop.
+ */
+void wait_for_quit()
+{
+ while (rpp_sci_getc() < 0)
+ vTaskDelay(INPUT_IDLE / portTICK_RATE_MS);
+}
+
+
+/**
+ * Infinite busy loop.
+ *
+ * This function is called after an error has being detected to "stop"
+ * processing.
+ */
+void busy_infinite_loop()
+{
+ while (TRUE)
+ asm (" nop");
+}
+
+
+
+/// Main functions -------------------------------------------------------------
+/**
+ * Application main function
+ *
+ * This function is called after startup.
+ */
+int main(void)
+{
+ // Spawn tasks
+ portBASE_TYPE task_created =
+ xTaskCreate(test_cmdproc,
+ (const signed char *)"test_cmdproc",
+ 1024, NULL, TEST_TASK_PRIORITY, NULL
+ );
+
+ if (task_created != pdPASS) {
+ rpp_sci_printf((const char *)
+ "ERROR: Problem allocating memory for command processor to "
+ "start. Error code: %d\r\n", (uint32_t)task_created
+ );
+ busy_infinite_loop();
+ }
+
+ // Start Scheduler
+ vTaskStartScheduler();
+
+ // Catch memory problems
+ rpp_sci_printf((const char *)
+ "ERROR: Problem allocating memory for scheduler to start.\r\n"
+ );
+ busy_infinite_loop();
+
+ return 0;
+}
+
+
+/**
+ * FreeRTOS malloc() failed hook.
+ */
+void vApplicationMallocFailedHook(void)
+{
+ rpp_sci_printf((const char *)
+ "ERROR: Manual memory allocation failed.\r\n"
+ );
+}
+
+
+/**
+ * FreeRTOS stack overflow hook.
+ */
+void vApplicationStackOverflowHook(xTaskHandle xTask,
+ signed portCHAR *pcTaskName)
+{
+ rpp_sci_printf((const char *)
+ "ERROR: Stack overflow : \"%s\".\r\n", pcTaskName
+ );
+}