]> rtime.felk.cvut.cz Git - arc.git/blob - common/ramlog.c
Regenerated examples. Removed linux board. Added default cross compiler for TI.
[arc.git] / common / ramlog.c
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 \r
16 /**\r
17  * A very simple ramlog.\r
18  *\r
19  * Features:\r
20  * - Prints to a ram space in section ".ramlog"\r
21  * - The size is configurable using CFG_RAMLOG_SIZE (default is 1000)\r
22  * - The ramlog section should not be cleared by the linkfile if one wants\r
23  *   to have a ramlog that survives reset.\r
24  * - Session support\r
25  *\r
26  * Assumes some c-lib support:\r
27  * - The clib support should be able to open a file called "ramlog".\r
28  * - Printing to that file will print to the ramlog.\r
29  *\r
30  * HEADER\r
31  *\r
32  *   The ramlog uses a very simple header.\r
33  *\r
34  *  Byte   0    1    2    3\r
35  *       +----+----+----+----+\r
36  *       | 01 |CNT | X  |    |\r
37  *       +----+----+----+----+\r
38  *\r
39  *   Byte 0 is 01\r
40  *   Byte 1 is a counter that incremented for each time the ramlog is started.\r
41  *   Byte 2 is not defined yet.\r
42  *   Byte 3 is 01\r
43  *\r
44  *   01 is used since it's control sign and not a printable char.\r
45  *\r
46  * STRATEGY FOR SESSION\r
47  *   Cases:\r
48  *   1. The ramlog is empty, ie the header is not there at first position\r
49  *   2. The header is in first poistion\r
50  *      - look for the next?\r
51  *      - Use saved inforation?\r
52  *\r
53  *\r
54  */\r
55 \r
56 #include <stdio.h>\r
57 #include <stdarg.h>\r
58 #include "MemMap.h"\r
59 \r
60 \r
61 \r
62 #ifndef CFG_RAMLOG_SIZE\r
63 #define CFG_RAMLOG_SIZE  2000\r
64 #endif\r
65 \r
66 #define RAMLOG_MAGIC      1\r
67 \r
68 \r
69 SECTION_RAMLOG static unsigned char ramlog[CFG_RAMLOG_SIZE];\r
70 #if defined(CFG_RAMLOG_SESSION)\r
71 SECTION_RAMLOG static unsigned ramlog_curr;\r
72 SECTION_RAMLOG static unsigned ramlog_session;\r
73 #else\r
74 static unsigned ramlog_curr = 0;\r
75 #endif\r
76 \r
77 #define RAMLOG_FILENO  (FILE *)3\r
78 \r
79 \r
80 /**\r
81  * Print a char to the ramlog\r
82  * @param c\r
83  */\r
84 void ramlog_chr( char c ) {\r
85   ramlog[ramlog_curr++] = c;\r
86   if( ramlog_curr >= CFG_RAMLOG_SIZE ) {\r
87           ramlog_curr = 0;\r
88   }\r
89 }\r
90 \r
91 /**\r
92  * Print a string to the ramlog\r
93  * @param str\r
94  */\r
95 void ramlog_puts( char *str ) {\r
96 \r
97   while(*str!=0) {\r
98         ramlog_chr(*str++);\r
99   }\r
100   ramlog_chr('\n');\r
101 }\r
102 \r
103 /**\r
104  * Formatted print for the ramlog.\r
105  *\r
106  * @param format The format string.\r
107  */\r
108 void ramlog_printf( const char *format, ... ) {\r
109 \r
110         // Fast and ugly ramlog support.\r
111         volatile int rv;\r
112         va_list args;\r
113         va_start(args,format);\r
114 \r
115         rv = vfprintf(RAMLOG_FILENO, format, args);\r
116         va_end(args);\r
117 }\r
118 \r
119 \r
120 /**\r
121  * Initialize the ramlog. Must be called before any other ramlog functions.\r
122  */\r
123 void ramlog_init()\r
124 {\r
125 \r
126 #if defined(CFG_RAMLOG_SESSION)\r
127         char buf[32];\r
128         /* Check for existing session */\r
129         if( (ramlog[0] != RAMLOG_MAGIC) || (ramlog[3] != RAMLOG_MAGIC) ) {\r
130                 ramlog_curr = 0;\r
131                 ramlog_session = 0;\r
132         } else {\r
133                 /*  Search the ramlog */\r
134         }\r
135     ramlog_session++;\r
136     simple_sprintf(buf, "Session (%d)\n", ramlog_session);\r
137     ramlog_puts(buf);\r
138 \r
139 #else\r
140         ramlog_curr = 0;\r
141 #endif\r
142 }\r