Skip to content

Commit 77d9e12

Browse files
committed
Add ISR print functions PrintFromISR() and VPrintFromISR()
The functions PrintFromISR() and VPrintFromISR() perform formatted output from ISRs. Don't use them outside of an ISR. The functions do not synchronize with the output of the regular print functions of any task.
1 parent 91c5644 commit 77d9e12

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

src/FormattedIO.c

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
#include "FormattedIO.h"
6+
#include <StrPrintf.h>
67
#include <stdio.h>
78
#include "Serial.h"
89

@@ -54,13 +55,93 @@ Print(const char* fmt, ...)
5455
return res;
5556
}
5657

58+
typedef struct
59+
{
60+
char* mBuf;
61+
size_t mLen;
62+
} StrBuf;
63+
64+
static int
65+
PutStrBuf(void* aParam, int aChar)
66+
{
67+
StrBuf* buf = aParam;
68+
69+
if (!buf->mLen) {
70+
return -1;
71+
} else if (buf->mLen == 1) {
72+
aChar = '\0'; /* always terminate string buffer */
73+
}
74+
75+
*buf->mBuf = aChar;
76+
++buf->mBuf;
77+
--buf->mLen;
78+
79+
return 1;
80+
}
81+
5782
int
5883
VPrint(const char* fmt, va_list ap)
5984
{
6085
char buf[128];
61-
int res = vsnprintf(buf, sizeof(buf), fmt, ap);
86+
StrBuf strBuf = {
87+
.mBuf = buf,
88+
.mLen = sizeof(buf)
89+
};
90+
int res = vStrXPrintf(PutStrBuf, &strBuf, fmt, ap);
91+
if (res < 0) {
92+
return -1;
93+
}
94+
uint32_t len = sizeof(buf) - strBuf.mLen;
95+
res = PrintIPC(len, buf);
96+
if (res < 0) {
97+
return -1;
98+
}
99+
return len;
100+
}
101+
102+
static int
103+
PutSerial(void* aParam, int aChar)
104+
{
105+
SerialPutChar(aChar);
106+
return 0;
107+
}
108+
109+
int
110+
_Print(const char* fmt, ...)
111+
{
112+
va_list ap;
113+
114+
va_start(ap, fmt);
115+
int res = _VPrint(fmt, ap);
116+
va_end(ap);
117+
118+
return res;
119+
}
120+
121+
int
122+
_VPrint(const char* fmt, va_list ap)
123+
{
124+
int res = vStrXPrintf(PutSerial, NULL, fmt, ap);
62125
if (res < 0) {
63126
return -1;
64127
}
65-
return PrintIPC(res, buf);
128+
return res;
129+
}
130+
131+
int
132+
PrintFromISR(const char* fmt, ...)
133+
{
134+
va_list ap;
135+
136+
va_start(ap, fmt);
137+
int res = VPrintFromISR(fmt, ap);
138+
va_end(ap);
139+
140+
return res;
141+
}
142+
143+
int
144+
VPrintFromISR(const char* fmt, va_list ap)
145+
{
146+
return _VPrint(fmt, ap);
66147
}

src/FormattedIO.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,17 @@ Print(const char* fmt, ...);
1111

1212
int
1313
VPrint(const char* fmt, va_list ap);
14+
15+
int
16+
PrintFromISR(const char* fmt, ...);
17+
18+
int
19+
VPrintFromISR(const char* fmt, va_list ap);
20+
21+
/* Internal functions for debugging; don't use in production code. */
22+
23+
int
24+
_Print(const char* fmt, ...);
25+
26+
int
27+
_VPrint(const char* fmt, va_list ap);

0 commit comments

Comments
 (0)