2 * Copyright (C) 2012-2013 Czech Technical University in Prague
4 * Created on: 28.2.2013
8 * - Carlos Jenkins <carlos@jenkins.co.cr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * This file contains commands for testing external SDRAM of RPP board.
30 #include "cmd_sdram.h"
37 #define BASE_FREQ_MILLIS 1000
38 #define FREQ_VARIABILITY 100
39 #define TEST_TASK_PRIORITY 0
40 #define TEST_TASK_STACK 512
45 extern xSemaphoreHandle rpp_sdr_cmdproc_semaphore;
46 static boolean_t stop_tasks = FALSE; /**< Flag for stopping all related tasks. */
47 static uint8_t tasks_running = 0; /**< Task counter - incremented each time task is spawn, decremented when task is deleted */
50 * @brief Set flag for tasks to finish.
52 * All tasks are reading the flag during their work and when
53 * they detect this flag is up, they stop the routine, decrement
54 * task counter and delete themselves.
56 static void wait_tasks_to_finish()
59 while (tasks_running > 0)
65 * @brief Wait for user input "q" to quit the loop.
69 while (rpp_sci_getc() < 0)
70 vTaskDelay(INPUT_IDLE / portTICK_RATE_MS);
75 * FreeRTOS Task that send some noise to the log from time to time.
77 void sdr_test_task(void *par)
79 // Calculate wait time in OS ticks
80 static const portTickType freq_ticks = BASE_FREQ_MILLIS / portTICK_RATE_MS;
81 portTickType last_wake_time = xTaskGetTickCount();
84 srand(2097925); // They are random, I swear xD
87 uint32_t variation = 0;
88 int8_t plus_minus = 1;
91 // Put something in the log
92 rpp_sdr_printf((const char*)
93 "This is the noise generator at iteration %d "
94 "putting some noise value %d.", i, rand()
99 variation = freq_ticks +
100 (plus_minus * ((rand() % FREQ_VARIABILITY) + 1));
101 plus_minus = plus_minus * -1;
102 vTaskDelayUntil(&last_wake_time, variation);
112 * SDR Test entry point.
121 xTaskHandle test_task_handle;
123 // Spawn noise task first, and later enable logging. Depending on the time
124 // required the first logs might not be registered, but that's ok, is better
125 // to check if logging could be enabled after data is being sent that enable
126 // command processor (which output messages to the SCI), and that inmediatly
127 // after the noise task could not be created, forcing to close the just
128 // opened command processor.
129 portBASE_TYPE task_created = xTaskCreate(sdr_test_task,
130 (const signed char*)"sdr_test_task",
131 TEST_TASK_STACK, NULL, TEST_TASK_PRIORITY,
135 if (task_created != pdPASS) {
137 rpp_sci_printf((const char*)
138 "ERROR: Problem spawning the test task. "
139 "Error code: %d\r\n", (uint32_t)task_created
147 if (rpp_sdr_setup(TRUE) != SUCCESS) {
148 rpp_sci_printf((const char*)
149 "ERROR: Problem enabling the logging module. "
152 wait_tasks_to_finish();
156 // Wait for the SDR included command processor to finish
157 xSemaphoreTake(rpp_sdr_cmdproc_semaphore, portMAX_DELAY);
158 wait_tasks_to_finish();
160 /// Reset module configuration
161 if (rpp_sdr_setup(FALSE) != SUCCESS) {
162 rpp_sci_printf((const char*)
163 "ERROR: Could not stop logging module.\r\n"
168 rpp_sci_printf((const char*)"\r\n");
175 * @brief Start SDR test routine
177 * @param[in] cmd_io Pointer to IO stack
178 * @param[in] des Pointer to command descriptor
179 * @param[in] param Parameters of command
182 int cmd_do_test_log(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
189 * @brief Tests the capacity and fitness of connected SDRAM
191 * @param[in] cmd_io Pointer to IO stack
192 * @param[in] des Pointer to command descriptor
193 * @param[in] param Parameters of command
194 * @return 0 when OK or error code
196 int cmd_do_test_ram(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
198 volatile uint32_t *addrPtr = (uint32_t*)0x80000000U;
199 volatile uint32_t *endAddr = (uint32_t*)0x83FFFFFFU;
200 uint32_t pattern = 0x55555555U;
203 uint32_t readVal = 0;
205 while (addrPtr <= endAddr) {
207 pattern += 0x55555555U;
211 addrPtr = (uint32_t*)0x80000000U;
212 pattern = 0x55555555U;
213 while (addrPtr <= endAddr) {
215 if (pattern == readVal) cnt++;
216 else if (errCnt++ <= 10)
217 rpp_sci_printf("Error at %h\r\n",addrPtr);
219 pattern += 0x55555555U;
223 cnt = cnt * sizeof(uint32_t) / 1024 / 1024;
225 rpp_sci_printf("SDRAM not connected.");
227 rpp_sci_printf("SDRAM installed: %d MB", cnt);
234 /** Command descriptor for test SDRAM */
235 cmd_des_t const cmd_des_test_sdram = {
237 "sdramtest","Test if the SDRAM module is connected and if so, measures its capacity",
238 "### Command syntax ###\n"
242 "### Description ###\n"
244 "This command tests SDRAM address space by writing and reading a\n"
245 "pattern to/from it. It detects the SDRAM capacity.\n"
250 " SDRAM installed: 64 MB\n",
251 CMD_HANDLER(cmd_do_test_ram), (void*)&cmd_list_sdram
254 /** Command descriptor for test log to SDRAM */
255 cmd_des_t const cmd_des_test_log = {
257 "sdramlogtest","Open a command subprocessor for managing SDRAM logging",
258 "### Command syntax ###\n"
262 "### Description ###\n"
264 "The command opens a subcommand processor, which contains testing\n"
265 "commands for logging into SDRAM.\n",
266 CMD_HANDLER(cmd_do_test_log), (void*)&cmd_list_sdram
269 /** List of commands for sdram, defined as external */
270 cmd_des_t const *cmd_list_sdram[] = {