blob: 48ebe81c827a7fd0a3cca90ad8e6c4084a279db7 [file] [log] [blame]
Edison Ai1c266ae2019-03-20 11:21:21 +08001/*
Mate Toth-Palf8f773f2019-09-11 16:28:08 +02002 * Copyright (c) 2017-2019 ARM Limited
Edison Ai1c266ae2019-03-20 11:21:21 +08003 *
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 <string.h>
22#include "Driver_USART.h"
23#include "target_cfg.h"
Mate Toth-Palf8f773f2019-09-11 16:28:08 +020024#include "device_cfg.h"
Edison Ai1c266ae2019-03-20 11:21:21 +080025
26#define ASSERT_HIGH(X) assert(X == ARM_DRIVER_OK)
27
28/* Imports USART driver */
29extern ARM_DRIVER_USART TFM_DRIVER_STDIO;
30
Edison Ai1c266ae2019-03-20 11:21:21 +080031static void uart_putc(unsigned char c)
32{
33 int32_t ret = ARM_DRIVER_OK;
34
35 ret = TFM_DRIVER_STDIO.Send(&c, 1);
36 ASSERT_HIGH(ret);
37}
38
39/* Redirects printf to TFM_DRIVER_STDIO in case of ARMCLANG*/
40#if defined(__ARMCC_VERSION)
TTornblomc640e072019-06-14 14:33:51 +020041/* Struct FILE is implemented in stdio.h. Used to redirect printf to
42 * TFM_DRIVER_STDIO
43 */
44FILE __stdout;
45
Edison Ai1c266ae2019-03-20 11:21:21 +080046/* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */
47int fputc(int ch, FILE *f)
48{
49 /* Send byte to USART */
50 uart_putc(ch);
51
52 /* Return character written */
53 return ch;
54}
55#elif defined(__GNUC__)
56/* Redirects printf to TFM_DRIVER_STDIO in case of GNUARM */
57int _write(int fd, char *str, int len)
58{
59 int i;
60
61 for (i = 0; i < len; i++) {
62 /* Send byte to USART */
63 uart_putc(str[i]);
64 }
65
66 /* Return the number of characters written */
67 return len;
68}
TTornblomc640e072019-06-14 14:33:51 +020069#elif defined(__ICCARM__)
70int putchar(int ch)
71{
72 /* Send byte to USART */
73 uart_putc(ch);
74
75 /* Return character written */
76 return ch;
77}
Edison Ai1c266ae2019-03-20 11:21:21 +080078#endif
79
80void stdio_init(void)
81{
82 int32_t ret = ARM_DRIVER_OK;
83 ret = TFM_DRIVER_STDIO.Initialize(NULL);
84 ASSERT_HIGH(ret);
85
Mate Toth-Palf8f773f2019-09-11 16:28:08 +020086 ret = TFM_DRIVER_STDIO.Control(ARM_USART_MODE_ASYNCHRONOUS,
87 DEFAULT_UART_BAUDRATE);
Edison Ai1c266ae2019-03-20 11:21:21 +080088 ASSERT_HIGH(ret);
89}
90
91void stdio_uninit(void)
92{
93 int32_t ret = ARM_DRIVER_OK;
94 ret = TFM_DRIVER_STDIO.Uninitialize();
95 ASSERT_HIGH(ret);
96}
97