]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - apps/rpp-test-suite/hbr.c
Update adc and spi to the new port interface
[pes-rpp/rpp-lib.git] / apps / rpp-test-suite / hbr.c
1 /* Copyright (C) 2013 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 : aout.c
12  * Abstract:
13  *     RPP test suite - module for testing AOUT.
14  *
15  * References:
16  *     test.h
17  */
18
19
20 #include <math.h> // sin()
21
22 #include "rpp/rpp.h"
23 #include "test.h"
24
25 #define SINE_FREQ_HZ     0.05
26 #define SAMPLE_RATE_HZ     20
27 // Which means 1% increment of duty cycle per sample
28
29 // Task control
30 static boolean_t stop_tasks = FALSE;
31 static uint8_t tasks_running = 0;
32
33 /**
34  * FreeRTOS Task that controls the H-Bridge following a sine wave.
35  */
36 void hbr_test_task(void *par)
37 {
38         rpp_sci_printf((const char *)
39                                    "H-Bridge Test at %1.2f Hz:\r\n", SINE_FREQ_HZ
40                                    );
41         rpp_sci_printf((const char *)
42                                    "===========================================================\r\n"
43                                    );
44         rpp_sci_printf((const char *)"Samples: 0");
45
46         // Calculate wait time in OS ticks
47         static const portTickType freq_ticks = configTICK_RATE_HZ / SAMPLE_RATE_HZ;
48         portTickType last_wake_time = xTaskGetTickCount();
49
50         // Constant trigonometric variables
51         const double two_pi = 2.0 * (4.0 * atan(1.0)); // Just 2pi
52         /*
53          * A little bit of math:
54          * In this example we want to generate a 0.05Hz (one period each 20s)
55          * sinusoid wave with a sampling rate of 20Hz so duty cycle increments or
56          * decrements 1% per sample.
57          */
58         const double step = (two_pi * SINE_FREQ_HZ) / SAMPLE_RATE_HZ;
59
60         uint64_t samples = 0;
61         int i = 0;
62         while (!stop_tasks) {
63
64                 for (i = 0; (i < SAMPLE_RATE_HZ) && !stop_tasks; i++, samples++) {
65
66                         // Update H-Bridge
67                         if (rpp_hbr_control(sin(samples * step)) != SUCCESS)
68                                 rpp_sci_printf((const char *)
69                                                            "ERROR: Input error on H-Bridge control signal.\r\n"
70                                                            );
71
72                         // Wait until next step
73                         if (!stop_tasks)
74                                 vTaskDelayUntil(&last_wake_time, freq_ticks);
75                 }
76
77                 rpp_sci_printf((const char *)
78                                            "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
79                                            "Samples: %lu", samples + 1
80                                            );
81
82         }
83
84         // Delete myself
85         tasks_running--;
86         vTaskDelete(NULL);
87 }
88
89
90 /**
91  * HBR Test entry point.
92  */
93 void test_hbr()
94 {
95         /// Configure module
96         // Configure H-Bridge at default frequency
97         if (rpp_hbr_enable(-1) != SUCCESS) {
98                 rpp_sci_printf((const char *)
99                                            "ERROR: H-Bridge could not be started.\r\n"
100                                            );
101                 wait_for_quit();
102                 return;
103         }
104
105
106         /// Spawn tasks
107         xTaskHandle test_task_handle;
108
109         portBASE_TYPE task_created = xTaskCreate(hbr_test_task,
110                                                                                          (const signed char *)"hbr_test_task",
111                                                                                          TEST_TASK_STACK, NULL, TEST_TASK_PRIORITY,
112                                                                                          &test_task_handle
113                                                                                          );
114
115         if (task_created != pdPASS) {
116
117                 rpp_sci_printf((const char *)
118                                            "ERROR: Problem spawning the test task. "
119                                            "Error code: %d\r\n", (uint32_t)task_created
120                                            );
121                 if (rpp_hbr_disable() != SUCCESS)
122                         rpp_sci_printf((const char *)
123                                                    "ERROR: Could not stop H-Bridge module.\r\n"
124                                                    );
125                 wait_for_quit();
126                 return;
127         }
128         tasks_running++;
129
130
131         // Wait for user exit
132         wait_for_quit();
133         stop_tasks = TRUE;
134         while (tasks_running > 0)
135                 taskYIELD();
136         stop_tasks = FALSE;
137
138
139         /// Reset module configuration
140         if (rpp_hbr_disable() != SUCCESS)
141                 rpp_sci_printf((const char *)
142                                            "ERROR: Could not stop H-Bridge module.\r\n"
143                                            );
144
145
146         rpp_sci_printf((const char *)"\r\n");
147
148         return;
149 }