RV32 SoC DS UG
High-Perf RV32 SoC DS UG
RV64 SoC DS UG API and Examples
Embedded IDE UG
Loading...
Searching...
No Matches
print.h
Go to the documentation of this file.
1
2// Copyright (C) 2013-2023 Efinix Inc. All rights reserved.
3//
4// This document contains proprietary information which is
5// protected by copyright. All rights are reserved. This notice
6// refers to original work by Efinix, Inc. which may be derivitive
7// of other work distributed under license of the authors. In the
8// case of derivative work, nothing in this notice overrides the
9// original author's license agreement. Where applicable, the
10// original license agreement is included in it's original
11// unmodified form immediately below this header.
12//
13// WARRANTY DISCLAIMER.
14// THE DESIGN, CODE, OR INFORMATION ARE PROVIDED “AS IS” AND
15// EFINIX MAKES NO WARRANTIES, EXPRESS OR IMPLIED WITH
16// RESPECT THERETO, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES,
17// INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
18// MERCHANTABILITY, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR
19// PURPOSE. SOME STATES DO NOT ALLOW EXCLUSIONS OF AN IMPLIED
20// WARRANTY, SO THIS DISCLAIMER MAY NOT APPLY TO LICENSEE.
21//
22// LIMITATION OF LIABILITY.
23// NOTWITHSTANDING ANYTHING TO THE CONTRARY, EXCEPT FOR BODILY
24// INJURY, EFINIX SHALL NOT BE LIABLE WITH RESPECT TO ANY SUBJECT
25// MATTER OF THIS AGREEMENT UNDER TORT, CONTRACT, STRICT LIABILITY
26// OR ANY OTHER LEGAL OR EQUITABLE THEORY (I) FOR ANY INDIRECT,
27// SPECIAL, INCIDENTAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES OF ANY
28// CHARACTER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF
29// GOODWILL, DATA OR PROFIT, WORK STOPPAGE, OR COMPUTER FAILURE OR
30// MALFUNCTION, OR IN ANY EVENT (II) FOR ANY AMOUNT IN EXCESS, IN
31// THE AGGREGATE, OF THE FEE PAID BY LICENSEE TO EFINIX HEREUNDER
32// (OR, IF THE FEE HAS BEEN WAIVED, $100), EVEN IF EFINIX SHALL HAVE
33// BEEN INFORMED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO
34// NOT ALLOW THE EXCLUSION OR LIMITATION OF INCIDENTAL OR
35// CONSEQUENTIAL DAMAGES, SO THIS LIMITATION AND EXCLUSION MAY NOT
36// APPLY TO LICENSEE.
37//
39#ifndef _PRINT_H_
40#define _PRINT_H_
41
42#include <stdarg.h>
43#include <stdlib.h>
44#include <stdint.h>
45#include <math.h>
46#include <string.h>
47#include "bsp.h"
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53#if (ENABLE_SEMIHOSTING_PRINT == 1)
54 #include "semihosting.h"
55#endif
56
57#if (ENABLE_BSP_PRINTF)
58 static void _putchar(char character){
59 #if (ENABLE_SEMIHOSTING_PRINT == 1)
60 sh_writec(character);
61 #else
62 bsp_putChar(character);
63 #endif // (ENABLE_SEMIHOSTING_PRINT == 1)
64 }
65
66 static void _putchar_s(char *p)
67 {
68 #if (ENABLE_SEMIHOSTING_PRINT == 1)
69 sh_write0(p);
70 #else
71 while (*p)
72 _putchar(*(p++));
73 #endif // (ENABLE_SEMIHOSTING_PRINT == 1)
74 }
75
76 static void bsp_printHex(uint32_t val)
77 {
78 uint32_t digits;
79 digits =8;
80
81 for (int i = (4*digits)-4; i >= 0; i -= 4) {
82 _putchar("0123456789ABCDEF"[(val >> i) % 16]);
83 }
84 }
85
86 static void bsp_printHex_lower(uint32_t val)
87 {
88 uint32_t digits;
89 digits =8;
90
91 for (int i = (4*digits)-4; i >= 0; i -= 4) {
92 _putchar("0123456789abcdef"[(val >> i) % 16]);
93
94 }
95 }
96
97 #if (ENABLE_FLOATING_POINT_SUPPORT)
98 /* reverse: reverse string s in place */
99 static void reverse(char s[])
100 {
101 int i, j, len;
102 char c;
103
104 for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
105 c = s[i];
106 s[i] = s[j];
107 s[j] = c;
108 }
109 }
110
111 /* itos: convert integer n to characters in s */
112 static void itos(int n, char s[])
113 {
114 int i, sign;
115
116 if ((sign = n) < 0) /* record sign */
117 n = -n; /* make n positive */
118 i = 0;
119 do { /* generate digits in reverse order */
120 s[i++] = n % 10 + '0'; /* get next digit */
121 } while ((n /= 10) > 0); /* delete it */
122 if (sign < 0)
123 s[i++] = '-';
124 s[i] = '\0';
125 reverse(s);
126 }
127
128 // Converts a floating-point/double number to a string.
129 static void ftoa(double n, char* res1, char* res2)
130 {
131 float fpart_f;
132 int afterpoint=4;
133
134 // Extract integer part
135 int ipart = (int)n;
136
137 // Extract floating part
138 double fpart = n - (double)ipart;
139
140 // convert integer part to string
141 itos(n, res1);
142
143 // add dot
144 *res2 = '.';
145 res2++;
146
147 // convert fraction part to string
148 fpart_f = (float)fpart * pow(10, afterpoint);
149 if (fpart_f<0)
150 {
151 *res2 = '-';
152 res2++;
153 fpart_f = -(fpart_f);
154 }
155 // handling of 0 after decimal point e.g. 1.003
156 for (int i=afterpoint; i>0; i--)
157 {
158 if ((fpart_f<(1 * pow(10, i-1))) && (fpart_f>0))
159 {
160 *res2='0';
161 res2++;
162 }
163 }
164
165 itos((int)fpart_f, res2);
166 }
167
168 static void print_dec(uint32_t val)
169 {
170 char sval[10];
171 itos(val, sval);
172 _putchar_s(sval);
173
174 }
175 static void print_float(double val)
176 {
177 int i, j, neg;
178 neg=0;
179 i=2;
180 j=19;
181 char sval[21], fval[10];
182 ftoa(val, sval, fval);
183 if (fval[1] == '-')
184 {
185 neg = 1;
186 while (i<10)
187 {
188 fval[i-1] = fval[i];
189 i++;
190 }
191
192 }
193 strcat(sval, fval);
194 if ((sval[0] != '-') && (neg == 1))
195 {
196 while (j>=0)
197 {
198 sval[j+1] = sval[j];
199 j--;
200 }
201 sval[0] = '-';
202 }
203 _putchar_s(sval);
204 }
205
206 #endif //#if (ENABLE_FLOATING_POINT_SUPPORT)
207
208
209
210 static void bsp_printf_c(int c)
211 {
212 _putchar(c);
213 }
214
215 static void bsp_printf_s(char *p)
216 {
217 _putchar_s(p);
218 }
219
220
221
222 static void bsp_printf_d(int val)
223 {
224 char buffer[32];
225 char *p = buffer;
226 if (val < 0) {
227 bsp_printf_c('-');
228 val = -val;
229 }
230 while (val || p == buffer) {
231 *(p++) = '0' + val % 10;
232 val = val / 10;
233 }
234 while (p != buffer)
235 bsp_printf_c(*(--p));
236 }
237
238 static void bsp_printf_x(int val)
239 {
240 int i,digi=2;
241
242 for(i=0;i<8;i++)
243 {
244 if((val & (0xFFFFFFF0 <<(4*i))) == 0)
245 {
246 digi=i+1;
247 break;
248 }
249 }
250 bsp_printHex_lower(val);
251 }
252
253 static void bsp_printf_X(int val)
254 {
255 int i,digi=2;
256
257 for(i=0;i<8;i++)
258 {
259 if((val & (0xFFFFFFF0 <<(4*i))) == 0)
260 {
261 digi=i+1;
262 break;
263 }
264 }
265 bsp_printHex(val);
266 }
267#if (ENABLE_SEMIHOSTING_PRINT == 0)
268 static int bsp_printf(const char *format, ...)
269 {
270 int i;
271 va_list ap;
272
273 va_start(ap, format);
274
275 for (i = 0; format[i]; i++){
276 if (format[i] == '\n') {
277 bsp_printf_c('\r'); // Carriage Return (move to start of line)
278 bsp_printf_c('\n'); // Line Feed (move down)
279 continue;
280 }
281 if (format[i] == '%') {
282 while (format[++i]) {
283 if (format[i] == 'c') {
284 bsp_printf_c(va_arg(ap,int));
285 break;
286 }
287 else if (format[i] == 's') {
288 bsp_printf_s(va_arg(ap,char*));
289 break;
290 }
291 else if (format[i] == 'd') {
292 bsp_printf_d(va_arg(ap,int));
293 break;
294 }
295 else if (format[i] == 'X') {
296 bsp_printf_X(va_arg(ap,int));
297 break;
298 }
299 else if (format[i] == 'x') {
300 bsp_printf_x(va_arg(ap,int));
301 break;
302 }
303#if (ENABLE_FLOATING_POINT_SUPPORT)
304 else if (format[i] == 'f') {
305 print_float(va_arg(ap,double));
306 break;
307 }
308#elif (ENABLE_PRINTF_WARNING)
309 else if (format[i] == 'f') {
310 bsp_printf_s("<Floating point printing not enable. Please Enable it at bsp.h first...>");
311 break;
312 }
313#endif //#if (ENABLE_FLOATING_POINT_SUPPORT)
314 }
315 } else
316 bsp_printf_c(format[i]);
317 }
318 va_end(ap);
319 }
320
321#else // #if (ENABLE_SEMIHOSTING_PRINT == 1)
322 #include "print_full.h"
323 static int bsp_printf(const char* format, ...)
324 {
325 va_list va;
326 va_start(va, format);
327
328 char buffer[MAX_STRING_BUFFER_SIZE];
329 const int ret = _vsnprintf(_out_buffer, buffer, (size_t)-1, format, va);
330 _putchar_s(buffer);
331
332 va_end(va);
333 return ret;
334 }
335
336
337#endif // #if (ENABLE_SEMIHOSTING_PRINT == 0)
338#endif //#if (ENABLE_BSP_PRINTF)
339
340#ifdef __cplusplus
341}
342#endif // C_plusplus
343
344#endif //_PRINT_H_
Board Support Package API definitions.
#define bsp_putChar(c)
Map standard character output to the physical UART.
Definition bsp.h:95
#define MAX_STRING_BUFFER_SIZE
Definition print_full.h:213