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) {
66 * @brief Wait for user input "q" to quit the loop.
70 while(rpp_sci_getc() < 0) {
71 vTaskDelay(INPUT_IDLE / portTICK_RATE_MS);
77 * FreeRTOS Task that send some noise to the log from time to time.
79 void sdr_test_task(void* par)
81 // Calculate wait time in OS ticks
82 static const portTickType freq_ticks = BASE_FREQ_MILLIS / portTICK_RATE_MS;
83 portTickType last_wake_time = xTaskGetTickCount();
86 srand(2097925); // They are random, I swear xD
89 uint32_t variation = 0;
90 int8_t plus_minus = 1;
93 // Put something in the log
94 rpp_sdr_printf((const char*)
95 "This is the noise generator at iteration %d "
96 "putting some noise value %d.", i, rand()
101 variation = freq_ticks +
102 (plus_minus * ((rand() % FREQ_VARIABILITY) + 1));
103 plus_minus = plus_minus * -1;
104 vTaskDelayUntil(&last_wake_time, variation);
114 * SDR Test entry point.
123 xTaskHandle test_task_handle;
125 // Spawn noise task first, and later enable logging. Depending on the time
126 // required the first logs might not be registered, but that's ok, is better
127 // to check if logging could be enabled after data is being sent that enable
128 // command processor (which output messages to the SCI), and that inmediatly
129 // after the noise task could not be created, forcing to close the just
130 // opened command processor.
131 portBASE_TYPE task_created = xTaskCreate(sdr_test_task,
132 (const signed char*)"sdr_test_task",
133 TEST_TASK_STACK, NULL, TEST_TASK_PRIORITY,
137 if(task_created != pdPASS) {
139 rpp_sci_printf((const char*)
140 "ERROR: Problem spawning the test task. "
141 "Error code: %d\r\n", (uint32_t)task_created
149 if(rpp_sdr_setup(TRUE) != SUCCESS) {
150 rpp_sci_printf((const char*)
151 "ERROR: Problem enabling the logging module. "
154 wait_tasks_to_finish();
158 // Wait for the SDR included command processor to finish
159 xSemaphoreTake(rpp_sdr_cmdproc_semaphore, portMAX_DELAY);
160 wait_tasks_to_finish();
162 /// Reset module configuration
163 if(rpp_sdr_setup(FALSE) != SUCCESS) {
164 rpp_sci_printf((const char*)
165 "ERROR: Could not stop logging module.\r\n"
170 rpp_sci_printf((const char*)"\r\n");
177 * @brief Start SDR test routine
179 * Command syntax: testslog
181 * @param[in] cmd_io Pointer to IO stack
182 * @param[in] des Pointer to command descriptor
183 * @param[in] param Parameters of command
186 int cmd_do_test_log(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
192 * @brief Tests the capacity and fitness of connected SDRAM
194 * Command syntax: testsdram
196 * @param[in] cmd_io Pointer to IO stack
197 * @param[in] des Pointer to command descriptor
198 * @param[in] param Parameters of command
199 * @return 0 when OK or error code
201 int cmd_do_test_ram(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
202 volatile uint32_t* addrPtr = (uint32_t*)0x80000000U;
203 volatile uint32_t* endAddr = (uint32_t*)0x83FFFFFFU;
204 uint32_t pattern = 0x55555555U;
207 uint32_t readVal = 0;
209 while (addrPtr <= endAddr) {
211 pattern += 0x55555555U;
215 addrPtr = (uint32_t *)0x80000000U;
216 pattern = 0x55555555U;
217 while (addrPtr <= endAddr) {
219 if (pattern == readVal) cnt++;
220 else if(errCnt++ <= 10) {
221 rpp_sci_printf("Error at %h\r\n",addrPtr);
224 pattern += 0x55555555U;
228 cnt = cnt * sizeof(uint32_t) / 1024 / 1024;
230 rpp_sci_printf("SDRAM not connected.");
233 rpp_sci_printf("SDRAM installed: %d MB", cnt);
241 /** Command descriptor for test SDRAM */
242 cmd_des_t const cmd_des_test_sdram={
244 "testsdram","Test if SDRAM module is connected and if it is, measure capacity",
245 "=== Description ===\n"
247 "This command tests while SDRAM address space by writing and reading a\n"
248 "pattern. It detects valid SDRAM capacity.\n"
250 "=== Command syntax ===\n"
253 CMD_HANDLER(cmd_do_test_ram), (void *)&cmd_list_sdram
256 /** Command descriptor for test log to SDRAM */
257 cmd_des_t const cmd_des_test_log={
259 "testlog","Open command subprocessor for managing SDRAM logging.",
261 CMD_HANDLER(cmd_do_test_log), (void *)&cmd_list_sdram
264 /** List of commands for sdram, defined as external */
265 cmd_des_t const *cmd_list_sdram[]={