blob: dea538f3768948672d73e66cbfe7394e70309c4f [file] [log] [blame]
Edison Ai1c266ae2019-03-20 11:21:21 +08001/*
2 * Copyright (c) 2017-2018 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 <string.h>
22#include "Driver_USART.h"
23#include "target_cfg.h"
24
25#define ASSERT_HIGH(X) assert(X == ARM_DRIVER_OK)
26
27/* Imports USART driver */
28extern ARM_DRIVER_USART TFM_DRIVER_STDIO;
29
30/* Struct FILE is implemented in stdio.h. Used to redirect printf to
31 * TFM_DRIVER_STDIO
32 */
33FILE __stdout;
34
35static void uart_putc(unsigned char c)
36{
37 int32_t ret = ARM_DRIVER_OK;
38
39 ret = TFM_DRIVER_STDIO.Send(&c, 1);
40 ASSERT_HIGH(ret);
41}
42
43/* Redirects printf to TFM_DRIVER_STDIO in case of ARMCLANG*/
44#if defined(__ARMCC_VERSION)
45/* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */
46int fputc(int ch, FILE *f)
47{
48 /* Send byte to USART */
49 uart_putc(ch);
50
51 /* Return character written */
52 return ch;
53}
54#elif defined(__GNUC__)
55/* Redirects printf to TFM_DRIVER_STDIO in case of GNUARM */
56int _write(int fd, char *str, int len)
57{
58 int i;
59
60 for (i = 0; i < len; i++) {
61 /* Send byte to USART */
62 uart_putc(str[i]);
63 }
64
65 /* Return the number of characters written */
66 return len;
67}
68#endif
69
70void stdio_init(void)
71{
72 int32_t ret = ARM_DRIVER_OK;
73 ret = TFM_DRIVER_STDIO.Initialize(NULL);
74 ASSERT_HIGH(ret);
75
76 ret = TFM_DRIVER_STDIO.Control(ARM_USART_MODE_ASYNCHRONOUS, 115200);
77 ASSERT_HIGH(ret);
78}
79
80void stdio_uninit(void)
81{
82 int32_t ret = ARM_DRIVER_OK;
83 ret = TFM_DRIVER_STDIO.Uninitialize();
84 ASSERT_HIGH(ret);
85}
86