-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdream_rdir.c
executable file
·243 lines (196 loc) · 6.3 KB
/
stdream_rdir.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*!\file stdream_rdir.c
** \author SMFSW
** \copyright MIT (c) 2017-2024, SMFSW
** \brief Stream redirection
** \note define ITM_REDIRECT in compiler defines for stings to be printed to ITM0 port
** \note define UART_REDIRECT and DBG_SERIAL in compiler defines with an UART instance to send printf likes strings to UART
** \warning By default when using syscalls, stdout stream is buffered, meaning that output will only be processed when
** '\\n' (new line) character is sent to buffer. To override this behavior, \ref stdout_no_buffer has to be called once
** in init routine to disable buffering, thus processing characters as they come.
*/
/****************************************************************/
#include <stdio.h>
#include "sarmfsw.h"
#include "UART_term.h"
#include "stdream_rdir.h"
/****************************************************************/
#if (defined(UART_REDIRECT) || defined(ITM_REDIRECT)) && !STDREAM_RDIR_SND_SYSCALLS
#warning "For ITM/UART stream redirection, you should consider using syscalls. printf likes shadowing way is only kept for backward compatibility!"
char dbg_msg_out[SZ_DBG_OUT] = ""; //!< stdream buffer for output
#endif
#if (defined(UART_REDIRECT) || defined(ITM_REDIRECT)) && STDREAM_RDIR_SND_SYSCALLS
#if defined(USE_IO_PUTCHAR)
/*!\brief syscalls implementation of __io_putchar
** \strong Strong implementation of __io_putchar for UART & ITM char send to stream
** \param[in] ch - character to send to stream
** \return char written to stream (-1 otherwise)
** \retval -1 is returned in case of failure (UART)
**/
int __io_putchar(int ch)
{
#if defined(ITM) && defined(ITM_REDIRECT)
ITM_SendChar(ch);
#endif
#if defined(HAL_UART_MODULE_ENABLED) && defined(UART_REDIRECT)
const char snd = ch;
if (UART_Term_Send(dbg_uart, &snd, 1)) { return -1; }
#endif
return ch;
}
#else
/*!\brief syscalls implementation of _write
** \strong Strong implementation of _write for UART & ITM string send to stream
** \param[in] file - unused
** \param[in] ptr - pointer to string to send to stream
** \param[in] len - length of string to send to stream
** \return length written to stream (-1 otherwise)
** \retval -1 is returned in case of failure (UART)
**/
int _write(int file, char * ptr, int len)
{
#if defined(ITM) && defined(ITM_REDIRECT)
ITM_port_send(ptr, len, 0);
#endif
#if defined(HAL_UART_MODULE_ENABLED) && defined(UART_REDIRECT)
if (UART_Term_Send(dbg_uart, ptr, len)) { return -1; }
#endif
// for (int DataIdx = 0 ; DataIdx < len ; DataIdx++)
// {
// __io_putchar(*ptr++);
// }
return len;
}
#endif
#endif
#if defined(UART_REDIRECT) && STDREAM_RDIR_RCV_SYSCALLS
// FIXME: bad implementation (and not working)
/*!\brief syscalls implementation of __io_getchar
** \strong Strong implementation of __io_getchar for UART char received from stream
** \return char received from stream (-1 otherwise)
** \retval -1 is returned in case of failure (UART)
**/
int __io_getchar(void)
{
static bool init = false;
int ch;
if (!init)
{
//setvbuf(stdin, NULL, _IONBF, 0);
init = true;
}
#if defined(HAL_UART_MODULE_ENABLED) && defined(UART_REDIRECT)
//if (HAL_UART_Receive(dbg_uart, (uint8_t *) &ch, 1, 1)) { return -1; }
#endif
return ch;
}
///*!\brief syscalls implementation of _read
//** \strong Strong implementation of _read for UART string received from stream
//** \param[in] file - unused
//** \param[in] ptr - pointer to string to receive from stream
//** \param[in] len - length of string to receive from stream
//** \return length received from stream (-1 otherwise)
//** \retval -1 is returned in case of failure (UART)
//**/
//int _read(int file, char * ptr, int len)
//{
// for (int DataIdx = 0 ; DataIdx < len ; DataIdx++)
// {
// *ptr++ = __io_getchar();
// }
// return len;
//}
#endif
/********************/
/*** ITM TRANSMIT ***/
/********************/
#if defined(ITM)
void NONNULL__ ITM_port_send(char * str, const size_t len, const int port)
{
for (char * pStr = str ; pStr < &str[len] ; pStr++)
{
while (ITM->PORT[port].u32 == 0); // Wait for port to be ready
ITM->PORT[port].u8 = *pStr;
}
}
#if !STDREAM_RDIR_SND_SYSCALLS
/***********************/
/*** ITM REDIRECTION ***/
/***********************/
/*!\brief Sends string to ITM0 port
** \param[in] str - pointer to string to send
** \param[in] len - length of string
**/
static void NONNULL__ ITM_send(char * str, const size_t len)
{
for (char * pStr = str ; pStr < &str[len] ; pStr++)
{
ITM_SendChar(*pStr);
}
}
int NONNULL__ printf_ITM(char * str, ...)
{
va_list args;
va_start(args, str);
vsprintf(dbg_msg_out, str, args);
va_end(args);
ITM_send(dbg_msg_out, strlen(dbg_msg_out));
str_clr(dbg_msg_out); // Empty string
return 0;
}
int NONNULL__ vprintf_ITM(char * str, va_list args)
{
vsprintf(dbg_msg_out, str, args);
ITM_send(dbg_msg_out, strlen(dbg_msg_out));
str_clr(dbg_msg_out); // Empty string
return 0;
}
#endif
#endif
#if !STDREAM_RDIR_SND_SYSCALLS
/***************************/
/*** GENERAL REDIRECTION ***/
/***************************/
int NONNULL__ printf_redir(char * str, ...)
{
va_list args;
#if defined(UART_REDIRECT)
if (UART_Term_Wait_Ready(dbg_uart) != ERROR_OK) { return -1; }
#endif
va_start(args, str);
vsprintf(dbg_msg_out, str, args);
va_end(args);
#if defined(ITM_REDIRECT) || defined(UART_REDIRECT)
uint16_t len = strlen(dbg_msg_out);
#endif
#if defined(ITM_REDIRECT) && defined(ITM)
ITM_send(dbg_msg_out, len);
#endif
#if defined(UART_REDIRECT)
UART_Term_Send(dbg_uart, dbg_msg_out, len);
#if !defined(STDREAM__UART_TX_IT)
str_clr(dbg_msg_out); // Empty string
#endif
#endif
return 0;
}
int NONNULL__ vprintf_redir(char * str, va_list args)
{
#if defined(UART_REDIRECT)
if (UART_Term_Wait_Ready(dbg_uart) != ERROR_OK) { return -1; }
#endif
vsprintf(dbg_msg_out, str, args);
#if defined(ITM_REDIRECT) || defined(UART_REDIRECT)
uint16_t len = strlen(dbg_msg_out);
#endif
#if defined(ITM_REDIRECT) && defined(ITM)
ITM_send(dbg_msg_out, len);
#endif
#if defined(UART_REDIRECT)
UART_Term_Send(dbg_uart, dbg_msg_out, len);
#if !defined(STDREAM__UART_TX_IT)
str_clr(dbg_msg_out); // Empty string
#endif
#endif
return 0;
}
#endif