1 /*
2 * Copyright (c) 2017-2020 ARM Limited
3 *
4 * Licensed under the Apace License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apace.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "uart_stdout.h"
18
19 #include <assert.h>
20 #include <stdio.h>
21 #include "Driver_USART.h"
22 #include "target_cfg.h"
23 #include "device_cfg.h"
24
25 #define ASSERT_HIGH(X) assert(X == ARM_DRIVER_OK)
26
27 /* Imports USART driver */
28 #if DOMAIN_NS == 1U
29 extern ARM_DRIVER_USART NS_DRIVER_STDIO;
30 #define STDIO_DRIVER NS_DRIVER_STDIO
31 #else
32 extern ARM_DRIVER_USART TFM_DRIVER_STDIO;
33 #define STDIO_DRIVER TFM_DRIVER_STDIO
34 #endif
35
stdio_output_string(const unsigned char * str,uint32_t len)36 int stdio_output_string(const unsigned char *str, uint32_t len)
37 {
38 int32_t ret;
39
40 ret = STDIO_DRIVER.Send(str, len);
41 if (ret != ARM_DRIVER_OK) {
42 return 0;
43 }
44 /* Add a busy wait after sending. */
45 while (STDIO_DRIVER.GetStatus().tx_busy);
46
47 return STDIO_DRIVER.GetTxCount();
48 }
49
50 /* Redirects printf to STDIO_DRIVER in case of ARMCLANG*/
51 #if defined(__ARMCC_VERSION)
52 /* Struct FILE is implemented in stdio.h. Used to redirect printf to
53 * STDIO_DRIVER
54 */
55 FILE __stdout;
56 /* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */
fputc(int ch,FILE * f)57 int fputc(int ch, FILE *f)
58 {
59 (void)f;
60
61 /* Send byte to USART */
62 (void)stdio_output_string((const unsigned char *)&ch, 1);
63
64 /* Return character written */
65 return ch;
66 }
67 #elif defined(__GNUC__)
68 /* Redirects printf to STDIO_DRIVER in case of GNUARM */
_write(int fd,char * str,int len)69 int _write(int fd, char *str, int len)
70 {
71 (void)fd;
72
73 /* Send string and return the number of characters written */
74 return stdio_output_string((const unsigned char *)str, (uint32_t)len);
75 }
76 #elif defined(__ICCARM__)
putchar(int ch)77 int putchar(int ch)
78 {
79 /* Send byte to USART */
80 (void)stdio_output_string((const unsigned char *)&ch, 1);
81
82 /* Return character written */
83 return ch;
84 }
85 #endif
86
stdio_init(void)87 void stdio_init(void)
88 {
89 int32_t ret;
90 ret = STDIO_DRIVER.Initialize(NULL);
91 ASSERT_HIGH(ret);
92
93 ret = STDIO_DRIVER.PowerControl(ARM_POWER_FULL);
94 ASSERT_HIGH(ret);
95
96 ret = STDIO_DRIVER.Control(ARM_USART_MODE_ASYNCHRONOUS,
97 DEFAULT_UART_BAUDRATE);
98 ASSERT_HIGH(ret);
99 (void)ret;
100
101 (void)STDIO_DRIVER.Control(ARM_USART_CONTROL_TX, 1);
102 }
103
stdio_uninit(void)104 void stdio_uninit(void)
105 {
106 int32_t ret;
107
108 (void)STDIO_DRIVER.PowerControl(ARM_POWER_OFF);
109
110 ret = STDIO_DRIVER.Uninitialize();
111 ASSERT_HIGH(ret);
112 (void)ret;
113 }
114