]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_sdram.c
Fix warnings
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_sdram.c
1 /*
2  * Copyright (C) 2012-2013, 2015 Czech Technical University in Prague
3  *
4  * Created on: 28.2.2013
5  *
6  * Authors:
7  *     - Michal Horn
8  *     - Carlos Jenkins <carlos@jenkins.co.cr>
9  *
10  * This document contains proprietary information belonging to Czech
11  * Technical University in Prague. Passing on and copying of this
12  * document, and communication of its contents is not permitted
13  * without prior written authorization.
14  *
15  * File : cmd_sdram.c
16  *
17  * Abstract:
18  *      This file contains commands for testing external SDRAM of RPP board.
19  *
20  */
21
22 #include "cmd_sdram.h"
23
24 #ifndef DOCGEN
25
26 #include "rpp/rpp.h"
27 #include <stdlib.h>
28
29 #define BASE_FREQ_MILLIS    1000
30 #define FREQ_VARIABILITY    100
31 #define TEST_TASK_PRIORITY  0
32 #define TEST_TASK_STACK     512
33 #define INPUT_IDLE          50
34
35
36 // Task control
37 extern xSemaphoreHandle rpp_sdr_cmdproc_semaphore;
38 static boolean_t stop_tasks = FALSE;    /**< Flag for stopping all related tasks. */
39 static uint8_t tasks_running = 0;       /**< Task counter - incremented each time task is spawn, decremented when task is deleted */
40
41 /**
42  * @brief Set flag for tasks to finish.
43  *
44  * All tasks are reading the flag during their work and when
45  * they detect this flag is up, they stop the routine, decrement
46  * task counter and delete themselves.
47  */
48 static void wait_tasks_to_finish()
49 {
50         stop_tasks = TRUE;
51         while (tasks_running > 0)
52                 taskYIELD();
53         stop_tasks = FALSE;
54 }
55
56 /**
57  * @brief Wait for user input "q" to quit the loop.
58  */
59 void wait_for_quit()
60 {
61         while (rpp_sci_getc() < 0)
62                 vTaskDelay(INPUT_IDLE / portTICK_RATE_MS);
63 }
64
65
66 /**
67  * FreeRTOS Task that send some noise to the log from time to time.
68  */
69 void sdr_test_task(void *par)
70 {
71         // Calculate wait time in OS ticks
72         static const portTickType freq_ticks = BASE_FREQ_MILLIS / portTICK_RATE_MS;
73         portTickType last_wake_time = xTaskGetTickCount();
74
75         // Initialize rand
76         srand(2097925); // They are random, I swear xD
77
78         uint32_t i = 0;
79         uint32_t variation = 0;
80         int8_t plus_minus = 1;
81         while (!stop_tasks) {
82
83                 // Put something in the log
84                 rpp_sdr_printf((const char *)
85                                            "This is the noise generator at iteration %d "
86                                            "putting some noise value %d.", i, rand()
87                                            );
88                 i++;
89
90                 if (!stop_tasks) {
91                         variation = freq_ticks +
92                                                 (plus_minus * ((rand() % FREQ_VARIABILITY) + 1));
93                         plus_minus = plus_minus * -1;
94                         vTaskDelayUntil(&last_wake_time, variation);
95                 }
96         }
97
98         // Delete myself
99         tasks_running--;
100         vTaskDelete(NULL);
101 }
102
103 /**
104  * SDR Test entry point.
105  */
106 void test_sdr()
107 {
108         /// Configure module
109         // - See note below.
110
111
112         /// Spawn tasks
113         xTaskHandle test_task_handle;
114
115         // Spawn noise task first, and later enable logging. Depending on the time
116         // required the first logs might not be registered, but that's ok, is better
117         // to check if logging could be enabled after data is being sent that enable
118         // command processor (which output messages to the SCI), and that inmediatly
119         // after the noise task could not be created, forcing to close the just
120         // opened command processor.
121         portBASE_TYPE task_created = xTaskCreate(sdr_test_task,
122                                                  "sdr_test_task",
123                                                  TEST_TASK_STACK, NULL, TEST_TASK_PRIORITY,
124                                                  &test_task_handle
125                 );
126
127         if (task_created != pdPASS) {
128
129                 rpp_sci_printf((const char *)
130                                            "ERROR: Problem spawning the test task. "
131                                            "Error code: %d\r\n", (uint32_t)task_created
132                                            );
133
134                 wait_for_quit();
135                 return;
136         }
137         tasks_running++;
138
139         if (rpp_sdr_setup(TRUE) != SUCCESS) {
140                 rpp_sci_printf((const char *)
141                                            "ERROR: Problem enabling the logging module. "
142                                            );
143                 wait_for_quit();
144                 wait_tasks_to_finish();
145                 return;
146         }
147
148         // Wait for the SDR included command processor to finish
149         xSemaphoreTake(rpp_sdr_cmdproc_semaphore, portMAX_DELAY);
150         wait_tasks_to_finish();
151
152         /// Reset module configuration
153         if (rpp_sdr_setup(FALSE) != SUCCESS) {
154                 rpp_sci_printf((const char *)
155                                            "ERROR: Could not stop logging module.\r\n"
156                                            );
157                 wait_for_quit();
158         }
159
160         rpp_sci_printf((const char *)"\r\n");
161
162         return;
163 }
164
165
166 /**
167  * @brief Start SDR test routine
168  *
169  * @param[in]   cmd_io  Pointer to IO stack
170  * @param[in]   des             Pointer to command descriptor
171  * @param[in]   param   Parameters of command
172  * @return always 0
173  */
174 int cmd_do_test_log(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
175 {
176         test_sdr();
177         return 0;
178 }
179
180 /**
181  * @brief       Tests the capacity and fitness of connected SDRAM
182  *
183  * @param[in]   cmd_io  Pointer to IO stack
184  * @param[in]   des             Pointer to command descriptor
185  * @param[in]   param   Parameters of command
186  * @return      0 when OK or error code
187  */
188 int cmd_do_test_ram(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
189 {
190         volatile uint32_t *addrPtr = (uint32_t *)0x80000000U;
191         volatile uint32_t *endAddr = (uint32_t *)0x83FFFFFFU;
192         uint32_t pattern = 0x55555555U;
193         uint32_t cnt = 0;
194         uint32_t errCnt = 0;
195         uint32_t readVal = 0;
196
197         while (addrPtr <= endAddr) {
198                 *addrPtr = pattern;
199                 pattern += 0x55555555U;
200                 addrPtr++;
201         }
202
203         addrPtr = (uint32_t *)0x80000000U;
204         pattern = 0x55555555U;
205         while (addrPtr <= endAddr) {
206                 readVal = *addrPtr;
207                 if (pattern == readVal) cnt++;
208                 else if (errCnt++ <= 10)
209                         rpp_sci_printf("Error at %h\r\n",addrPtr);
210                 else break;
211                 pattern += 0x55555555U;
212                 addrPtr++;
213         }
214
215         cnt = cnt * sizeof(uint32_t) / 1024 / 1024;
216         if (cnt == 0)
217                 rpp_sci_printf("SDRAM not connected.");
218         else
219                 rpp_sci_printf("SDRAM installed: %d MB", cnt);
220
221         return 0;
222 }
223
224 #endif  /* DOCGEN */
225
226 /** Command descriptor for test SDRAM */
227 cmd_des_t const cmd_des_test_sdram = {
228         0, 0,
229         "sdramtest","Test if the SDRAM module is connected and if so, measures its capacity",
230         "### Command syntax ###\n"
231         "\n"
232         "    sdramtest\n"
233         "\n"
234         "### Description ###\n"
235         "\n"
236         "This command tests SDRAM address space by writing and reading a\n"
237         "pattern to/from it. It detects the SDRAM capacity.\n"
238         "\n"
239         "### Example ###\n"
240         "\n"
241         "    --> sdramtest\n"
242         "    SDRAM installed: 64 MB\n",
243         CMD_HANDLER(cmd_do_test_ram), (void *)&cmd_list_sdram
244 };
245
246 /** Command descriptor for test log to SDRAM */
247 cmd_des_t const cmd_des_test_log = {
248         0, 0,
249         "sdramlogtest","Open a command subprocessor for managing SDRAM logging",
250         "### Command syntax ###\n"
251         "\n"
252         "    sdramlogtest\n"
253         "\n"
254         "### Description ###\n"
255         "\n"
256         "The command opens a subcommand processor, which contains testing\n"
257         "commands for logging into SDRAM.\n",
258         CMD_HANDLER(cmd_do_test_log), (void *)&cmd_list_sdram
259 };
260
261 /** List of commands for sdram, defined as external */
262 cmd_des_t const *cmd_list_sdram[] = {
263         &cmd_des_test_sdram,
264         &cmd_des_test_log,
265         NULL
266 };