1 /* Copyright (C) 2013 Czech Technical University in Prague
4 * - Carlos Jenkins <carlos@jenkins.co.cr>
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.
13 * RPP test suite - module for testing AOUT.
20 #include <math.h> // sin()
25 #define SINE_FREQ_HZ 0.05
26 #define SAMPLE_RATE_HZ 20
27 // Which means 1% increment of duty cycle per sample
30 static boolean_t stop_tasks = FALSE;
31 static uint8_t tasks_running = 0;
34 * FreeRTOS Task that controls the H-Bridge following a sine wave.
36 void hbr_test_task(void *par)
38 rpp_sci_printf((const char *)
39 "H-Bridge Test at %1.2f Hz:\r\n", SINE_FREQ_HZ
41 rpp_sci_printf((const char *)
42 "===========================================================\r\n"
44 rpp_sci_printf((const char *)"Samples: 0");
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();
50 // Constant trigonometric variables
51 const double two_pi = 2.0 * (4.0 * atan(1.0)); // Just 2pi
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.
58 const double step = (two_pi * SINE_FREQ_HZ) / SAMPLE_RATE_HZ;
64 for (i = 0; (i < SAMPLE_RATE_HZ) && !stop_tasks; i++, samples++) {
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"
72 // Wait until next step
74 vTaskDelayUntil(&last_wake_time, freq_ticks);
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
91 * HBR Test entry point.
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"
107 xTaskHandle test_task_handle;
109 portBASE_TYPE task_created = xTaskCreate(hbr_test_task,
110 (const signed char *)"hbr_test_task",
111 TEST_TASK_STACK, NULL, TEST_TASK_PRIORITY,
115 if (task_created != pdPASS) {
117 rpp_sci_printf((const char *)
118 "ERROR: Problem spawning the test task. "
119 "Error code: %d\r\n", (uint32_t)task_created
121 if (rpp_hbr_disable() != SUCCESS)
122 rpp_sci_printf((const char *)
123 "ERROR: Could not stop H-Bridge module.\r\n"
131 // Wait for user exit
134 while (tasks_running > 0)
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"
146 rpp_sci_printf((const char *)"\r\n");