blob: 5a686972f3f13c332c323d7a01d70ec9ac1acc25 [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>
Mingyang Sun1ec6e262019-10-23 22:24:22 +080021#include <stdint.h>
Edison Ai1c266ae2019-03-20 11:21:21 +080022#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
Mingyang Sun1ec6e262019-10-23 22:24:22 +080031static int uart_send_string(const unsigned char *str, uint32_t len)
Edison Ai1c266ae2019-03-20 11:21:21 +080032{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080033 int32_t ret;
Edison Ai1c266ae2019-03-20 11:21:21 +080034
Mingyang Sun1ec6e262019-10-23 22:24:22 +080035 ret = TFM_DRIVER_STDIO.Send(str, len);
36 if (ret != ARM_DRIVER_OK) {
37 return 0;
38 }
39 /* Add a busy wait after sending. */
40 while (TFM_DRIVER_STDIO.GetStatus().tx_busy);
41
42 return TFM_DRIVER_STDIO.GetTxCount();
Edison Ai1c266ae2019-03-20 11:21:21 +080043}
44
45/* Redirects printf to TFM_DRIVER_STDIO in case of ARMCLANG*/
46#if defined(__ARMCC_VERSION)
TTornblomc640e072019-06-14 14:33:51 +020047/* Struct FILE is implemented in stdio.h. Used to redirect printf to
48 * TFM_DRIVER_STDIO
49 */
50FILE __stdout;
Edison Ai1c266ae2019-03-20 11:21:21 +080051/* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */
52int fputc(int ch, FILE *f)
53{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080054 (void)f;
55
Edison Ai1c266ae2019-03-20 11:21:21 +080056 /* Send byte to USART */
Mingyang Sun1ec6e262019-10-23 22:24:22 +080057 (void)uart_send_string((const unsigned char *)&ch, 1);
Edison Ai1c266ae2019-03-20 11:21:21 +080058
59 /* Return character written */
60 return ch;
61}
62#elif defined(__GNUC__)
63/* Redirects printf to TFM_DRIVER_STDIO in case of GNUARM */
64int _write(int fd, char *str, int len)
65{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080066 (void)fd;
Edison Ai1c266ae2019-03-20 11:21:21 +080067
Mingyang Sun1ec6e262019-10-23 22:24:22 +080068 /* Send string and return the number of characters written */
69 return uart_send_string((const unsigned char *)str, (uint32_t)len);
Edison Ai1c266ae2019-03-20 11:21:21 +080070}
TTornblomc640e072019-06-14 14:33:51 +020071#elif defined(__ICCARM__)
72int putchar(int ch)
73{
74 /* Send byte to USART */
Mingyang Sun1ec6e262019-10-23 22:24:22 +080075 (void)uart_send_string((const unsigned char *)&ch, 1);
TTornblomc640e072019-06-14 14:33:51 +020076
77 /* Return character written */
78 return ch;
79}
Edison Ai1c266ae2019-03-20 11:21:21 +080080#endif
81
82void stdio_init(void)
83{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080084 int32_t ret;
Edison Ai1c266ae2019-03-20 11:21:21 +080085 ret = TFM_DRIVER_STDIO.Initialize(NULL);
86 ASSERT_HIGH(ret);
87
Mingyang Sun1ec6e262019-10-23 22:24:22 +080088 ret = TFM_DRIVER_STDIO.PowerControl(ARM_POWER_FULL);
89 ASSERT_HIGH(ret);
90
Mate Toth-Palf8f773f2019-09-11 16:28:08 +020091 ret = TFM_DRIVER_STDIO.Control(ARM_USART_MODE_ASYNCHRONOUS,
92 DEFAULT_UART_BAUDRATE);
Edison Ai1c266ae2019-03-20 11:21:21 +080093 ASSERT_HIGH(ret);
Mingyang Sun1ec6e262019-10-23 22:24:22 +080094
95 (void)TFM_DRIVER_STDIO.Control(ARM_USART_CONTROL_TX, 1);
Edison Ai1c266ae2019-03-20 11:21:21 +080096}
97
98void stdio_uninit(void)
99{
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800100 int32_t ret;
101
102 (void)TFM_DRIVER_STDIO.PowerControl(ARM_POWER_OFF);
103
Edison Ai1c266ae2019-03-20 11:21:21 +0800104 ret = TFM_DRIVER_STDIO.Uninitialize();
105 ASSERT_HIGH(ret);
106}
107