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