1 /* Copyright (C) 2013 Czech Technical University in Prague
4 * - Carlos Jenkins <carlos@jenkins.co.cr>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * SD-RAN logging RPP API implementation file.
25 * RPP API documentation.
31 #if rppCONFIG_INCLUDE_SDR == 1
33 #if rppCONFIG_DRV == 1
38 #define rpp_sci_putc(x) putchar(x)
39 #define rpp_sci_printf(x) printf(x)
43 /// Memory management variables ------------------------------------------------
44 static xSemaphoreHandle memory_mutex;
45 static uint32_t memory_size = 0;
46 static uint8_t* memory_start = NULL;
47 static uint8_t* memory_end = NULL;
48 static uint8_t* memory_current = NULL;
52 /// Command processor task -----------------------------------------------------
55 // Buffer to store incomming command
56 static char in_buffer[BUF_SIZE];
58 // SCI log command processor task
59 void rpp_sdr_cmdproc(void *p)
61 static const char* prompt = "--> ";
63 "Log control: %dkB available.\r\n",
64 (rpp_sdr_available() / 1024)
66 rpp_sci_printf("========================================\r\n");
67 rpp_sci_printf(prompt);
70 uint8_t buff_index = 0;
71 boolean_t flush = FALSE;
74 // Get one character from the user
75 if(rpp_sci_read_nb(1, &input) != SUCCESS) {
80 // Stop flushing if one character is received
84 rpp_sci_printf("\r\n");
85 rpp_sci_printf(prompt);
89 // Backspace and Delete
90 if(input == 8 || input == 127) {
95 // Line feed or Carriage return
96 } else if(input == 10 || input == 13) {
99 // If is any printable character
100 } else if(isprint(input)) {
102 // Store character and increment buffer index
103 in_buffer[buff_index] = input;
106 // Check if buffer is full and force flush
107 if(buff_index == BUF_SIZE - 1) {
111 // All other character are ignored
117 in_buffer[buff_index] = '\0';
121 if(buff_index == 0) {
122 rpp_sci_printf("\r\n");
123 rpp_sci_printf(prompt);
126 } else if(strncmp(in_buffer, "help", BUF_SIZE) == 0) {
128 "Available commands: \r\n"
135 } else if(strncmp(in_buffer, "log", BUF_SIZE) == 0) {
139 } else if(strncmp(in_buffer, "clear", BUF_SIZE) == 0) {
141 rpp_sci_printf("Done.\r\n");
142 rpp_sci_printf(prompt);
145 } else if(strncmp(in_buffer, "available", BUF_SIZE) == 0) {
147 "%d kB of %d kB available.\r\n",
148 rpp_sdr_available() / 1024,
152 // Unknow command, print buffer back
155 "ERROR: Unknown command \"%s\"\r\n",
157 rpp_sci_printf(prompt);
170 /// Show log task --------------------------------------------------------------
171 // Semaphore to order the show task to start flushing the log
172 static xSemaphoreHandle show_semaphore;
174 // Flag to check if log show task is flushing the log
175 static boolean_t show_flushing = FALSE;
178 void rpp_sdr_showtask(void *p)
185 // Wait semaphore to start
186 xSemaphoreTake(show_semaphore, portMAX_DELAY);
187 current = memory_start;
189 // Iterate until the end of the log
190 while(show_flushing &&
191 (current != memory_current) &&
192 (current < memory_end)) { // Just in case
194 // Print characters in this memory location.
195 // Ignores non-printable characters except \r and \n
197 if((byte == '\r') || (byte == '\n') || isprint(byte)) {
203 // If user waited to finish
205 rpp_sci_printf("\r\n");
206 rpp_sci_printf(prompt);
207 show_flushing = FALSE;
214 /// Public API -----------------------------------------------------------------
215 // Flag to check if SDR module is initialized
216 static boolean_t initialized = FALSE;
218 // Initialize SDR module
219 int8_t rpp_sdr_init()
226 // Create memory write mutex
227 memory_mutex = xSemaphoreCreateMutex();
229 // Create log show semaphore
230 vSemaphoreCreateBinary(show_semaphore);
232 // Define memory bounds
233 #if rppCONFIG_DRV == 1
234 memory_size = RPP_SDR_ADDR_END - RPP_SDR_ADDR_START + 1;
235 memory_start = (uint8_t*)RPP_SDR_ADDR_START;
236 memory_end = (uint8_t*)RPP_SDR_ADDR_END;
238 // Allocate 1MB for test
239 memory_size = 1024*1024;
240 memory_start = (uint8_t*)malloc(memory_size);
241 memory_end = (uint8_t*)((uint32_t)memory_start + memory_size - 1);
243 memory_current = memory_start;
249 // General flag to check if logging is enabled
250 static boolean_t log_enabled = FALSE;
252 // Task handle for command processor task
253 static xTaskHandle cmdproc_handle;
255 // Task handle for log show task
256 static xTaskHandle show_handle;
258 // Enable/Disable logging
259 int8_t rpp_sdr_setup(boolean_t enable)
261 // Just in case user ignore everything
267 if(log_enabled == enable) {
271 // Shut down is requested
272 if(log_enabled && !enable) {
275 // Stop show task if running
278 vTaskDelete(cmdproc_handle);
279 vTaskDelete(show_handle);
281 // Startup is requested
284 xTaskCreate(rpp_sdr_cmdproc,
285 "rpp_sdr_cmdproc", 512, NULL, 2, &cmdproc_handle);
286 xTaskCreate(rpp_sdr_showtask,
287 "rpp_sdr_showtask", 512, NULL, 2, &show_handle);
295 uint32_t rpp_sdr_available()
300 return ((uint32_t)memory_end - (uint32_t)memory_current) + 1;
304 // Store something to the log, if logging is enabled
305 int32_t rpp_sdr_printf(const char* format, ...)
311 /// Format user string
312 char str[MAX_BUFFER_LEN];
316 va_start(argList, format);
318 length = vsnprintf(str, sizeof(str), format, argList);
328 // uint32_t max value is 4294967295 (10 digits) + [] + ' ' + '\0' = 14
331 hdr_length = vsnprintf(hdr, sizeof(hdr), "[%10d] ", xTaskGetTickCount());
341 xSemaphoreTake(memory_mutex, portMAX_DELAY);
342 while((memory_current != memory_end) && (i < hdr_length)) {
344 *memory_current = hdr[i];
352 /// Write user string
354 while((memory_current != memory_end) && (i < length)) {
356 *memory_current = str[i];
364 const static char trl[2] = {'\r', '\n'};
366 while((memory_current != memory_end) && (i < sizeof(trl))) {
368 *memory_current = trl[i];
375 xSemaphoreGive(memory_mutex);
380 // Clears log. Will also stop the show task
381 int8_t rpp_sdr_clear();
387 // Stop log show flushing if running
392 // Check if log is already empty
393 if(memory_current == memory_start) {
397 // Reset memory pointer
398 memory_current = memory_start;
404 // Starts/Stops the task that sends the log to the SCI
405 int8_t rpp_sdr_show(boolean_t start)
412 if(start == show_flushing) {
416 // Log flush stop requested
417 if(show_flushing && !start) {
418 show_flushing = FALSE;
420 // Log flush start requested
422 show_flushing = TRUE;
423 xSemaphoreGive(show_semaphore);
430 #endif /* rppCONFIG_INCLUDE_SDR */