tinyusb  0.4
Click here to lend your support to tinyusb donation and make a donation at pledgie.com
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
printf_retarget.c
Go to the documentation of this file.
1 /**************************************************************************/
37 /**************************************************************************/
38 
39 #include "board.h"
40 
41 #if CFG_PRINTF_TARGET != PRINTF_TARGET_SEMIHOST
42 
43 #if CFG_PRINTF_TARGET == PRINTF_TARGET_UART
44  #define retarget_getchar() board_uart_getchar()
45  #define retarget_putchar(c) board_uart_putchar(c);
46 #elif CFG_PRINTF_TARGET == PRINTF_TARGET_SWO
47  volatile int32_t ITM_RxBuffer; // keil variable to read from SWO
48  #define retarget_getchar() ITM_ReceiveChar()
49  #define retarget_putchar(c) ITM_SendChar(c)
50 #else
51  #error Target is not implemented yet
52 #endif
53 
54 //--------------------------------------------------------------------+
55 // LPCXPRESSO / RED SUITE
56 //--------------------------------------------------------------------+
57 #if defined __CODE_RED
58 
59 #if CFG_PRINTF_TARGET == PRINTF_TARGET_SWO
60  #error author does not know how to retarget SWO with lpcxpresso/red-suite
61 #endif
62 
63 // Called by bottom level of printf routine within RedLib C library to write
64 // a character. With the default semihosting stub, this would write the character
65 // to the debugger console window . But this version writes
66 // the character to the UART.
67 int __sys_write (int iFileHandle, char *buf, int length)
68 {
69  (void) iFileHandle;
70 
71  for (int i=0; i<length; i++)
72  {
73  if (buf[i] == '\n') retarget_putchar('\r');
74 
75  retarget_putchar( buf[i] );
76  }
77 
78  return length;
79 }
80 
81 // Called by bottom level of scanf routine within RedLib C library to read
82 // a character. With the default semihosting stub, this would read the character
83 // from the debugger console window (which acts as stdin). But this version reads
84 // the character from the UART.
85 int __sys_readc (void)
86 {
87  return (int) retarget_getchar();
88 }
89 
90 //--------------------------------------------------------------------+
91 // KEIL
92 //--------------------------------------------------------------------+
93 #elif defined __CC_ARM // keil
94 
95 struct __FILE {
96  uint32_t handle;
97 };
98 
99 void _ttywrch(int ch)
100 {
101  if ( ch == '\n' ) retarget_putchar('\r');
102 
103  retarget_putchar(ch);
104 }
105 
106 int fgetc(FILE *f)
107 {
108  return retarget_getchar();
109 }
110 
111 int fputc(int ch, FILE *f)
112 {
113  _ttywrch(ch);
114  return ch;
115 }
116 
117 //--------------------------------------------------------------------+
118 // IAR
119 //--------------------------------------------------------------------+
120 #elif defined __ICCARM__ // TODO could not able to retarget to UART with IAR
121 
122 #if CFG_PRINTF_TARGET == PRINTF_TARGET_UART
123 #include <stddef.h>
124 
125 size_t __write(int handle, const unsigned char *buf, size_t length)
126 {
127  /* Check for the command to flush all handles */
128  if (handle == -1) return 0;
129 
130  /* Check for stdout and stderr (only necessary if FILE descriptors are enabled.) */
131  if (handle != 1 && handle != 2) return -1;
132 
133  for (size_t i=0; i<length; i++)
134  {
135  if (buf[i] == '\n') retarget_putchar('\r');
136 
137  retarget_putchar( buf[i] );
138  }
139 
140  return length;
141 }
142 
143 size_t __read(int handle, unsigned char *buf, size_t bufSize)
144 {
145  /* Check for stdin (only necessary if FILE descriptors are enabled) */
146  if (handle != 0) return -1;
147 
148  size_t i;
149  for (i=0; i<bufSize; i++)
150  {
151  uint8_t ch = board_uart_getchar();
152  if (ch == 0) break;
153  buf[i] = ch;
154  }
155 
156  return i;
157 }
158 
159 #endif
160 
161 #endif
162 
163 #endif // CFG_PRINTF_TARGET != PRINTF_TARGET_SEMIHOST
uint8_t board_uart_getchar(void)
Get a character input from UART.