]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - apps/rpp-test-suite/main.c
Update adc and spi to the new port interface
[pes-rpp/rpp-lib.git] / apps / rpp-test-suite / main.c
1 /* Copyright (C) 2013, 2014, 2015 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Carlos Jenkins <carlos@jenkins.co.cr>
5  *
6  * This document contains proprietary information belonging to Czech
7  * Technical University in Prague. Passing on and copying of this
8  * document, and communication of its contents is not permitted
9  * without prior written authorization.
10  *
11  * File : main.c
12  * Abstract:
13  *     RPP test suite command processor.
14  *
15  * References:
16  *     test.h
17  */
18
19
20 #include <ctype.h>  // isprint()
21 #include <string.h> // strncmp()
22
23 #include "rpp/rpp.h"
24 #include "test.h"
25
26
27 /// Command processor task -----------------------------------------------------
28 #define INPUT_IDLE 50
29 #define BUF_SIZE   80
30 static char in_buffer[BUF_SIZE];
31 static const char *prompt  = "--> ";
32 static const char *newline = "\r\n";
33
34
35 /**
36  * Compare user string and execute command.
37  */
38 void execute_cmd(char *string)
39 {
40 #ifndef TARGET_POSIX
41         if (     strncmp(string, "adc", BUF_SIZE) == 0)
42                 test_adc();
43         else
44 #endif /* TARGET_POSIX */
45         if (     strncmp(string, "can", BUF_SIZE) == 0)
46                 test_can();
47 #ifdef TARGET_POSIX
48         else if (strncmp(string, "eth", BUF_SIZE) == 0)
49                 test_eth();
50         else if (strncmp(string, "fr", BUF_SIZE) == 0)
51                 test_fr();
52         else if (strncmp(string, "lin", BUF_SIZE) == 0)
53                 test_lin();
54 #endif /* TARGET_POSIX */
55         else if (strncmp(string, "sci", BUF_SIZE) == 0)
56                 test_sci();
57         else if (strncmp(string, "help", BUF_SIZE) == 0) {
58                 rpp_sci_printf((const char *)"Available commands:\r\n");
59                 rpp_sci_printf((const char *)"\thelp - Display this help.\r\n");
60 #ifndef TARGET_POSIX
61                 rpp_sci_printf((const char *)"\tain  - Test Analog Input.\r\n");
62 #endif /* ! TARGET_POSIX */
63                 rpp_sci_printf((const char *)"\tcan  - Test CAN communication.\r\n");
64 #ifdef TARGET_POSIX
65                 rpp_sci_printf((const char *)"\teth  - Test Ethernet communication.\r\n");
66                 rpp_sci_printf((const char *)"\tfr   - Test FlexRay communication.\r\n");
67                 rpp_sci_printf((const char *)"\tlin  - Test LIN communication.\r\n");
68 #endif /* TARGET_POSIX */
69                 rpp_sci_printf((const char *)"\tsci  - Test Serial Communication Interface.\r\n");
70         }
71         else
72                 // Unknown command, print buffer back
73                 rpp_sci_printf((const char *)
74                                            "ERROR: Unknown command \"%s\"\r\n", string
75                                            );
76 }
77
78
79 /**
80  * Test suite command processor.
81  */
82 void test_cmdproc(void *par)
83 {
84         // Print header
85         rpp_sci_printf((const char *)
86                                    "RPP Library Test Suite.\r\n"
87                                    );
88         rpp_sci_printf((const char *)
89                                    "===========================================================\r\n"
90                                    );
91         rpp_sci_printf((const char *)
92                                    "[Type a module to test or 'help']\r\n"
93                                    );
94
95         rpp_sci_printf((const char *)"%s", prompt);
96
97         uint8_t input = 0;
98         uint8_t buff_index = 0;
99         boolean_t flush = FALSE;
100         while (TRUE) {
101
102                 // Get one character from the user
103                 if (rpp_sci_read_nb(1, &input) != SUCCESS) {
104                         vTaskDelay(INPUT_IDLE / portTICK_RATE_MS);
105                         continue;
106                 }
107
108                 // Backspace and Delete
109                 if (input == 8 || input == 127) {
110                         if (buff_index > 0) {
111                                 buff_index--;
112                                 echo('\b');
113                                 echo(' ' );
114                                 echo('\b');
115                         }
116                         continue;
117                 }
118                 else if (input == 10 || input == 13) {
119                         // Line feed or Carriage return
120                         flush = TRUE;
121                         echo('\r');
122                         echo('\n');
123                 }
124                 else if (isprint(input)) {
125                         // If is any printable character
126
127                         // Store character and increment buffer index
128                         in_buffer[buff_index] = input;
129                         buff_index++;
130                         echo(input);
131
132                         // Check if buffer is full and force flush
133                         if (buff_index == BUF_SIZE - 1)
134                                 flush = TRUE;
135                 }
136                 // All other character are ignored
137
138
139                 // Flush buffer
140                 if (flush) {
141
142                         // Terminate string
143                         in_buffer[buff_index] = '\0';
144
145                         // Execute command
146                         if (buff_index != 0)
147                                 execute_cmd((char *)&in_buffer);
148                         rpp_sci_printf((const char *)"%s", newline);
149                         rpp_sci_printf((const char *)"%s", prompt);
150
151                         // Reset variables
152                         rpp_sci_flush(TRUE);
153                         buff_index = 0;
154                         flush = FALSE;
155                 }
156         }
157 }
158
159
160
161 /// Utilities ------------------------------------------------------------------
162 /**
163  * Wait for user input "q" to quit the loop.
164  */
165 void wait_for_quit()
166 {
167         while (rpp_sci_getc() < 0)
168                 vTaskDelay(INPUT_IDLE / portTICK_RATE_MS);
169 }
170
171
172 /**
173  * Infinite busy loop.
174  *
175  * This function is called after an error has being detected to "stop"
176  * processing.
177  */
178 void busy_infinite_loop()
179 {
180         while (TRUE)
181                 asm (" nop");
182 }
183
184
185
186 /// Main functions -------------------------------------------------------------
187 /**
188  * Application main function
189  *
190  * This function is called after startup.
191  */
192 int main(void)
193 {
194         // Initialize library
195         rpp_init();
196
197 #ifndef TARGET_POSIX
198         // Speed up the SCI
199         rpp_sci_setup(115200);
200 #endif /* ! TARGET_POSIX */
201         // Spawn tasks
202         portBASE_TYPE task_created =
203                 xTaskCreate(test_cmdproc,
204                                         (const signed char *)"test_cmdproc",
205                                         1024, NULL, TEST_TASK_PRIORITY, NULL
206                                         );
207
208         if (task_created != pdPASS) {
209                 rpp_sci_printf((const char *)
210                                            "ERROR: Problem allocating memory for command processor to "
211                                            "start. Error code: %d\r\n", (uint32_t)task_created
212                                            );
213                 busy_infinite_loop();
214         }
215
216         // Start Scheduler
217         vTaskStartScheduler();
218
219         // Catch memory problems
220         rpp_sci_printf((const char *)
221                                    "ERROR: Problem allocating memory for scheduler to start.\r\n"
222                                    );
223         busy_infinite_loop();
224
225         return 0;
226 }
227
228
229 /**
230  * FreeRTOS malloc() failed hook.
231  */
232 void vApplicationMallocFailedHook(void)
233 {
234         rpp_sci_printf((const char *)
235                                    "ERROR: Manual memory allocation failed.\r\n"
236                                    );
237 }
238
239
240 /**
241  * FreeRTOS stack overflow hook.
242  */
243 void vApplicationStackOverflowHook(xTaskHandle xTask,
244                                                                    signed portCHAR *pcTaskName)
245 {
246         rpp_sci_printf((const char *)
247                                    "ERROR: Stack overflow : \"%s\".\r\n", pcTaskName
248                                    );
249 }