]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - apps/rpp-test-suite/ain.c
0603a3e6df1faa49c5eac4dc5a024ce92478fe68
[pes-rpp/rpp-lib.git] / apps / rpp-test-suite / ain.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 : ain.c
12  * Abstract:
13  *     RPP test suite - module for testing AIN.
14  *
15  * References:
16  *     test.h
17  */
18
19
20 #include "rpp/rpp.h"
21 #include "test.h"
22
23 #define FREQ_MILLIS      100
24
25
26 // Task control
27 static boolean_t stop_tasks = FALSE;
28 static uint8_t tasks_running = 0;
29
30 /**
31  * FreeRTOS Task that read analog inputs and prints them on the SCI.
32  */
33 void adc_test_task(void *par)
34 {
35         rpp_sci_printf((const char *)
36                                    "Analog Inputs Test [1-12]:\r\n"
37                                    );
38         rpp_sci_printf((const char *)
39                                    "===========================================================\r\n"
40                                    );
41         rpp_sci_printf((const char *)
42                                    "   1    2    3    4    5    6    7    8    9   10   11   12\r\n"
43                        // 4095 4095 4095 4095 4095 4095 4095 4095 4095 4095 4095 4095
44                                    );
45
46         // Calculate wait time in OS ticks
47         static const portTickType freq_ticks = FREQ_MILLIS / portTICK_RATE_MS;
48         portTickType last_wake_time = xTaskGetTickCount();
49
50 #ifndef TARGET_POSIX
51         while (!stop_tasks) {
52
53                 // Update inputs
54                 rpp_adc_update();
55                 if (stop_tasks)    // This printfs are really expensive, avoid
56                         continue;      // to run them if application is requested to stop.
57
58                 // Print inputs
59                 // Terminal needs to be at least 59 chars long
60                 rpp_sci_printf((const char *)
61                                            "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
62                                            "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
63                                            );
64
65                 rpp_sci_printf((const char *)
66                                            "%4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d",
67                                            rpp_adc_get(1),
68                                            rpp_adc_get(2),
69                                            rpp_adc_get(3),
70                                            rpp_adc_get(4),
71                                            rpp_adc_get(5),
72                                            rpp_adc_get(6),
73                                            rpp_adc_get(7),
74                                            rpp_adc_get(8),
75                                            rpp_adc_get(9),
76                                            rpp_adc_get(10),
77                                            rpp_adc_get(11),
78                                            rpp_adc_get(12)
79                                            );
80
81                 // Wait until next step
82                 if (!stop_tasks)
83                         vTaskDelayUntil(&last_wake_time, freq_ticks);
84         }
85 #endif /* TARGET_POSIX */
86
87         // Delete myself
88         tasks_running--;
89         vTaskDelete(NULL);
90 }
91
92
93 /**
94  * ADC Test entry point.
95  */
96 void test_adc()
97 {
98         /// Configure module
99         // - Not needed
100
101
102         /// Spawn tasks
103         xTaskHandle test_task_handle;
104
105         portBASE_TYPE task_created = xTaskCreate(adc_test_task,
106                                                                                          "adc_test_task",
107                                                                                          TEST_TASK_STACK, NULL, TEST_TASK_PRIORITY,
108                                                                                          &test_task_handle
109                                                                                          );
110
111         if (task_created != pdPASS) {
112
113                 rpp_sci_printf((const char *)
114                                            "ERROR: Problem spawning the test task. "
115                                            "Error code: %d\r\n", (uint32_t)task_created
116                                            );
117                 wait_for_quit();
118                 return;
119
120         }
121         tasks_running++;
122
123
124         // Wait for user exit
125         // Note: Not an easy task. If we preempt delete the task from this context
126         // using vTaskDelete and the task is holding a semaphore, like in the middle
127         // of a SCI send, printf, or similar, the semaphore will never be released
128         // and the application will deadlock in the next call to that function.
129         // As a general rule of thumb just allow vTaskDelete on the tasks themselves
130         // and implement a synchronization mecanism. Here we set flag to notify them
131         // that they should stop and then force change context each time this gets
132         // executed until all the tasks deleted themselves.
133         wait_for_quit();
134         stop_tasks = TRUE;
135         while (tasks_running > 0)
136                 taskYIELD();
137         stop_tasks = FALSE;
138
139
140         /// Reset module configuration
141         // - Not required
142
143
144         rpp_sci_printf((const char *)"\r\n");
145
146         return;
147 }