]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - apps/rpp-test-suite/src/_tms570_rpp/main.c
Rework test-suite to be able to build for different platforms
[pes-rpp/rpp-lib.git] / apps / rpp-test-suite / src / _tms570_rpp / main.c
1 /* Copyright (C) 2013, 2014 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         if (       strncmp(string, "adc", BUF_SIZE) == 0)
41                 test_adc();
42         else if (strncmp(string, "dac", BUF_SIZE) == 0)
43                 test_dac();
44         else if (strncmp(string, "can", BUF_SIZE) == 0)
45                 test_can();
46         else if (strncmp(string, "din", BUF_SIZE) == 0)
47                 test_din();
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, "hbr", BUF_SIZE) == 0)
53                 test_hbr();
54         else if (strncmp(string, "hout", BUF_SIZE) == 0)
55                 test_hout();
56         else if (strncmp(string, "lin", BUF_SIZE) == 0)
57                 test_lin();
58         else if (strncmp(string, "lout", BUF_SIZE) == 0)
59                 test_lout();
60         else if (strncmp(string, "mout", BUF_SIZE) == 0)
61                 test_mout();
62         else if (strncmp(string, "sci", BUF_SIZE) == 0)
63                 test_sci();
64         else if (strncmp(string, "sdc", BUF_SIZE) == 0)
65                 test_sdc();
66         else if (strncmp(string, "sdr", BUF_SIZE) == 0)
67                 test_sdr();
68         else if (strncmp(string, "help", BUF_SIZE) == 0) {
69                 rpp_sci_printf((const char *)"Available commands:\r\n");
70                 rpp_sci_printf((const char *)"\thelp - Display this help.\r\n");
71                 rpp_sci_printf((const char *)"\tain  - Test Analog Input.\r\n");
72                 rpp_sci_printf((const char *)"\taout - Test Analog Output.\r\n");
73                 rpp_sci_printf((const char *)"\tcan  - Test CAN communication.\r\n");
74                 rpp_sci_printf((const char *)"\tdin  - Test Digital Inputs.\r\n");
75                 rpp_sci_printf((const char *)"\teth  - Test Ethernet communication.\r\n");
76                 rpp_sci_printf((const char *)"\tfr   - Test FlexRay communication.\r\n");
77                 rpp_sci_printf((const char *)"\thbr  - Test H-Bridge.\r\n");
78                 rpp_sci_printf((const char *)"\thout - Test High Power Output.\r\n");
79                 rpp_sci_printf((const char *)"\tlin  - Test LIN communication.\r\n");
80                 rpp_sci_printf((const char *)"\tlout - Test Digital Outputs.\r\n");
81                 rpp_sci_printf((const char *)"\tmout - Test Power Outputs.\r\n");
82                 rpp_sci_printf((const char *)"\tsci  - Test Serial Communication Interface.\r\n");
83                 rpp_sci_printf((const char *)"\tsdc  - Test SD-Card.\r\n");
84                 rpp_sci_printf((const char *)"\tsdr  - Test SD-RAM.\r\n");
85         }
86         else
87                 // Unknown command, print buffer back
88                 rpp_sci_printf((const char *)
89                                            "ERROR: Unknown command \"%s\"\r\n", string
90                                            );
91 }
92
93
94 /**
95  * Test suite command processor.
96  */
97 void test_cmdproc(void *par)
98 {
99         // Print header
100         rpp_sci_printf((const char *)
101                                    "RPP Library Test Suite.\r\n"
102                                    );
103         rpp_sci_printf((const char *)
104                                    "===========================================================\r\n"
105                                    );
106         rpp_sci_printf((const char *)
107                                    "[Type a module to test or 'help']\r\n"
108                                    );
109
110         rpp_sci_printf((const char *)"%s", prompt);
111
112         uint8_t input = 0;
113         uint8_t buff_index = 0;
114         boolean_t flush = FALSE;
115         while (TRUE) {
116
117                 // Get one character from the user
118                 if (rpp_sci_read_nb(1, &input) != SUCCESS) {
119                         vTaskDelay(INPUT_IDLE / portTICK_RATE_MS);
120                         continue;
121                 }
122
123                 // Backspace and Delete
124                 if (input == 8 || input == 127) {
125                         if (buff_index > 0) {
126                                 buff_index--;
127                                 echo('\b');
128                                 echo(' ' );
129                                 echo('\b');
130                         }
131                         continue;
132                 }
133                 else if (input == 10 || input == 13) {
134                         // Line feed or Carriage return
135                         flush = TRUE;
136                         echo('\r');
137                         echo('\n');
138                 }
139                 else if (isprint(input)) {
140                         // If is any printable character
141
142                         // Store character and increment buffer index
143                         in_buffer[buff_index] = input;
144                         buff_index++;
145                         echo(input);
146
147                         // Check if buffer is full and force flush
148                         if (buff_index == BUF_SIZE - 1)
149                                 flush = TRUE;
150                 }
151                 // All other character are ignored
152
153
154                 // Flush buffer
155                 if (flush) {
156
157                         // Terminate string
158                         in_buffer[buff_index] = '\0';
159
160                         // Execute command
161                         if (buff_index != 0)
162                                 execute_cmd((char *)&in_buffer);
163                         rpp_sci_printf((const char *)"%s", newline);
164                         rpp_sci_printf((const char *)"%s", prompt);
165
166                         // Reset variables
167                         rpp_sci_flush(TRUE);
168                         buff_index = 0;
169                         flush = FALSE;
170                 }
171         }
172 }
173
174
175
176 /// Utilities ------------------------------------------------------------------
177 /**
178  * Wait for user input "q" to quit the loop.
179  */
180 void wait_for_quit()
181 {
182         while (rpp_sci_getc() < 0)
183                 vTaskDelay(INPUT_IDLE / portTICK_RATE_MS);
184 }
185
186
187 /**
188  * Infinite busy loop.
189  *
190  * This function is called after an error has being detected to "stop"
191  * processing.
192  */
193 void busy_infinite_loop()
194 {
195         while (TRUE)
196                 asm (" nop");
197 }
198
199
200
201 /// Main functions -------------------------------------------------------------
202 /**
203  * Application main function
204  *
205  * This function is called after startup.
206  */
207 int main(void)
208 {
209         // Initialize library
210         rpp_init();
211
212         // Speed up the SCI
213         rpp_sci_setup(115200);
214         
215         // Spawn tasks
216         portBASE_TYPE task_created =
217                 xTaskCreate(test_cmdproc,
218                                         (const signed char *)"test_cmdproc",
219                                         1024, NULL, TEST_TASK_PRIORITY, NULL
220                                         );
221
222         if (task_created != pdPASS) {
223                 rpp_sci_printf((const char *)
224                                            "ERROR: Problem allocating memory for command processor to "
225                                            "start. Error code: %d\r\n", (uint32_t)task_created
226                                            );
227                 busy_infinite_loop();
228         }
229
230         // Start Scheduler
231         vTaskStartScheduler();
232
233         // Catch memory problems
234         rpp_sci_printf((const char *)
235                                    "ERROR: Problem allocating memory for scheduler to start.\r\n"
236                                    );
237         busy_infinite_loop();
238
239         return 0;
240 }
241
242
243 /**
244  * FreeRTOS malloc() failed hook.
245  */
246 void vApplicationMallocFailedHook(void)
247 {
248         rpp_sci_printf((const char *)
249                                    "ERROR: Manual memory allocation failed.\r\n"
250                                    );
251 }
252
253
254 /**
255  * FreeRTOS stack overflow hook.
256  */
257 void vApplicationStackOverflowHook(xTaskHandle xTask,
258                                                                    signed portCHAR *pcTaskName)
259 {
260         rpp_sci_printf((const char *)
261                                    "ERROR: Stack overflow : \"%s\".\r\n", pcTaskName
262                                    );
263 }