]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/lib/apps/rpp-test-suite/src/main.c
e1e3ea2cf8fb2ab07496fc1ae47178a5f2ddc20d
[pes-rpp/rpp-simulink.git] / rpp / lib / apps / rpp-test-suite / src / main.c
1 /* Copyright (C) 2013 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Carlos Jenkins <carlos@jenkins.co.cr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * File : main.c
20  * Abstract:
21  *     RPP test suite command processor.
22  *
23  * References:
24  *     test.h
25  */
26
27
28 #include <ctype.h>  // isprint()
29 #include <string.h> // strncmp()
30
31 #include "rpp/rpp.h"
32 #include "test.h"
33
34
35 /// Command processor task -----------------------------------------------------
36 #define INPUT_IDLE 50
37 #define BUF_SIZE   80
38 static char in_buffer[BUF_SIZE];
39 static const char* prompt  = "--> ";
40 static const char* newline = "\r\n";
41
42
43 /**
44  * Compare user string and execute command.
45  */
46 void execute_cmd(char* string)
47 {
48     if(       strncmp(string, "ain" , BUF_SIZE) == 0) {
49         test_ain();
50     } else if(strncmp(string, "aout", BUF_SIZE) == 0) {
51         test_aout();
52     } else if(strncmp(string, "can" , BUF_SIZE) == 0) {
53         test_can();
54     } else if(strncmp(string, "din" , BUF_SIZE) == 0) {
55         test_din();
56     } else if(strncmp(string, "eth" , BUF_SIZE) == 0) {
57         test_eth();
58     } else if(strncmp(string, "fr"  , BUF_SIZE) == 0) {
59         test_fr();
60     } else if(strncmp(string, "hbr" , BUF_SIZE) == 0) {
61         test_hbr();
62     } else if(strncmp(string, "hout", BUF_SIZE) == 0) {
63         test_hout();
64     } else if(strncmp(string, "lin" , BUF_SIZE) == 0) {
65         test_lin();
66     } else if(strncmp(string, "lout", BUF_SIZE) == 0) {
67         test_lout();
68     } else if(strncmp(string, "mout", BUF_SIZE) == 0) {
69         test_mout();
70     } else if(strncmp(string, "sci" , BUF_SIZE) == 0) {
71         test_sci();
72     } else if(strncmp(string, "sdc" , BUF_SIZE) == 0) {
73         test_sdc();
74     } else if(strncmp(string, "sdr" , BUF_SIZE) == 0) {
75         test_sdr();
76     } else if(strncmp(string, "help", BUF_SIZE) == 0) {
77         rpp_sci_printf((const char*)"Available commands:\r\n");
78         rpp_sci_printf((const char*)"\thelp - Display this help.\r\n");
79         rpp_sci_printf((const char*)"\tain  - Test Analog Input.\r\n");
80         rpp_sci_printf((const char*)"\taout - Test Analog Output.\r\n");
81         rpp_sci_printf((const char*)"\tcan  - Test CAN communication.\r\n");
82         rpp_sci_printf((const char*)"\tdin  - Test Digital Inputs.\r\n");
83         rpp_sci_printf((const char*)"\teth  - Test Ethernet communication.\r\n");
84         rpp_sci_printf((const char*)"\tfr   - Test Frame Relay communication.\r\n");
85         rpp_sci_printf((const char*)"\thbr  - Test H-Bridge.\r\n");
86         rpp_sci_printf((const char*)"\thout - Test High Power Output.\r\n");
87         rpp_sci_printf((const char*)"\tlin  - Test LIN communication.\r\n");
88         rpp_sci_printf((const char*)"\tlout - Test Digital Outputs.\r\n");
89         rpp_sci_printf((const char*)"\tmout - Test Power Outputs.\r\n");
90         rpp_sci_printf((const char*)"\tsci  - Test Serial Communication Interface.\r\n");
91         rpp_sci_printf((const char*)"\tsdc  - Test SD-Card.\r\n");
92         rpp_sci_printf((const char*)"\tsdr  - Test SD-RAM.\r\n");
93     } else {
94         // Unknown command, print buffer back
95         rpp_sci_printf((const char*)
96                 "ERROR: Unknown command \"%s\"\r\n", string
97             );
98     }
99 }
100
101
102 /**
103  * Test suite command processor.
104  */
105 void test_cmdproc(void* par)
106 {
107     // Print header
108     rpp_sci_printf((const char*)
109             "RPP Library Test Suite.\r\n"
110         );
111     rpp_sci_printf((const char*)
112             "===========================================================\r\n"
113         );
114     rpp_sci_printf((const char*)
115             "[Type a module to test or 'help']\r\n"
116         );
117
118     rpp_sci_printf((const char*)"%s", prompt);
119
120     uint8_t input = 0;
121     uint8_t buff_index = 0;
122     boolean_t flush = FALSE;
123     while(TRUE) {
124
125         // Get one character from the user
126         if(rpp_sci_read_nb(1, &input) != SUCCESS) {
127             vTaskDelay(INPUT_IDLE / portTICK_RATE_MS);
128             continue;
129         }
130
131         // Backspace and Delete
132         if(input == 8 || input == 127) {
133             if(buff_index > 0) {
134                 buff_index--;
135                 echo('\b');
136                 echo(' ' );
137                 echo('\b');
138             }
139             continue;
140
141         // Line feed or Carriage return
142         } else if(input == 10 || input == 13) {
143             flush = TRUE;
144             echo('\r');
145             echo('\n');
146
147         // If is any printable character
148         } else if(isprint(input)) {
149
150             // Store character and increment buffer index
151             in_buffer[buff_index] = input;
152             buff_index++;
153             echo(input);
154
155             // Check if buffer is full and force flush
156             if(buff_index == BUF_SIZE - 1) {
157                 flush = TRUE;
158             }
159         }
160         // All other character are ignored
161
162
163         // Flush buffer
164         if(flush) {
165
166             // Terminate string
167             in_buffer[buff_index] = '\0';
168
169             // Execute command
170             if(buff_index != 0) {
171                 execute_cmd((char*)&in_buffer);
172             }
173             rpp_sci_printf((const char*)"%s", newline);
174             rpp_sci_printf((const char*)"%s", prompt);
175
176             // Reset variables
177             rpp_sci_flush(TRUE);
178             buff_index = 0;
179             flush = FALSE;
180         }
181     }
182 }
183
184
185
186 /// Utilities ------------------------------------------------------------------
187 /**
188  * Wait for user input "q" to quit the loop.
189  */
190 void wait_for_quit()
191 {
192     while(rpp_sci_getc() < 0) {
193         vTaskDelay(INPUT_IDLE / portTICK_RATE_MS);
194     }
195 }
196
197
198 /**
199  * Infinite busy loop.
200  *
201  * This function is called after an error has being detected to "stop"
202  * processing.
203  */
204 void busy_infinite_loop()
205 {
206     while(TRUE) {
207         asm(" nop");
208     }
209 }
210
211
212
213 /// Main functions -------------------------------------------------------------
214 /**
215  * Application main function
216  *
217  * This function is called after startup.
218  */
219 int main(void)
220 {
221     // Initialize library
222     rpp_init();
223
224     // Speed up the SCI
225     rpp_sci_setup(115200);
226
227     // Spawn tasks
228     portBASE_TYPE task_created =
229         xTaskCreate(test_cmdproc,
230                 (const signed char*)"test_cmdproc",
231                 1024, NULL, TEST_TASK_PRIORITY, NULL
232             );
233
234     if(task_created != pdPASS) {
235         rpp_sci_printf((const char*)
236                 "ERROR: Problem allocating memory for command processor to "
237                 "start. Error code: %d\r\n", (uint32_t)task_created
238             );
239         busy_infinite_loop();
240     }
241
242     // Start Scheduler
243     vTaskStartScheduler();
244
245     // Catch memory problems
246     rpp_sci_printf((const char*)
247             "ERROR: Problem allocating memory for scheduler to start.\r\n"
248         );
249     busy_infinite_loop();
250
251     return 0;
252 }
253
254
255 /**
256  * FreeRTOS malloc() failed hook.
257  */
258 void vApplicationMallocFailedHook(void)
259 {
260     rpp_sci_printf((const char*)
261             "ERROR: Manual memory allocation failed.\r\n"
262         );
263 }
264
265
266 /**
267  * FreeRTOS stack overflow hook.
268  */
269 void vApplicationStackOverflowHook(xTaskHandle xTask,
270                                    signed portCHAR *pcTaskName)
271 {
272     rpp_sci_printf((const char*)
273             "ERROR: Stack overflow : \"%s\".\r\n", pcTaskName
274         );
275 }
276