blob: 075a2b77ec4753ca66aa55774027586e15a3a06d [file] [log] [blame]
Edison Ai1c266ae2019-03-20 11:21:21 +08001/*
Kevin Peng524e3ec2020-05-29 10:28:26 +08002 * Copyright (c) 2017-2020 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>
Edison Ai1c266ae2019-03-20 11:21:21 +080021#include "Driver_USART.h"
22#include "target_cfg.h"
Mate Toth-Palf8f773f2019-09-11 16:28:08 +020023#include "device_cfg.h"
Edison Ai1c266ae2019-03-20 11:21:21 +080024
25#define ASSERT_HIGH(X) assert(X == ARM_DRIVER_OK)
26
27/* Imports USART driver */
Kevin Peng524e3ec2020-05-29 10:28:26 +080028#if DOMAIN_NS == 1U
29extern ARM_DRIVER_USART NS_DRIVER_STDIO;
30#define STDIO_DRIVER NS_DRIVER_STDIO
31#else
Edison Ai1c266ae2019-03-20 11:21:21 +080032extern ARM_DRIVER_USART TFM_DRIVER_STDIO;
Kevin Peng524e3ec2020-05-29 10:28:26 +080033#define STDIO_DRIVER TFM_DRIVER_STDIO
34#endif
Edison Ai1c266ae2019-03-20 11:21:21 +080035
Mingyang Sunc6b458b2019-12-19 15:07:34 +080036int stdio_output_string(const unsigned char *str, uint32_t len)
Edison Ai1c266ae2019-03-20 11:21:21 +080037{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080038 int32_t ret;
Edison Ai1c266ae2019-03-20 11:21:21 +080039
Kevin Peng524e3ec2020-05-29 10:28:26 +080040 ret = STDIO_DRIVER.Send(str, len);
Mingyang Sun1ec6e262019-10-23 22:24:22 +080041 if (ret != ARM_DRIVER_OK) {
42 return 0;
43 }
44 /* Add a busy wait after sending. */
Kevin Peng524e3ec2020-05-29 10:28:26 +080045 while (STDIO_DRIVER.GetStatus().tx_busy);
Mingyang Sun1ec6e262019-10-23 22:24:22 +080046
Kevin Peng524e3ec2020-05-29 10:28:26 +080047 return STDIO_DRIVER.GetTxCount();
Edison Ai1c266ae2019-03-20 11:21:21 +080048}
49
Kevin Peng524e3ec2020-05-29 10:28:26 +080050/* Redirects printf to STDIO_DRIVER in case of ARMCLANG*/
Edison Ai1c266ae2019-03-20 11:21:21 +080051#if defined(__ARMCC_VERSION)
TTornblomc640e072019-06-14 14:33:51 +020052/* Struct FILE is implemented in stdio.h. Used to redirect printf to
Kevin Peng524e3ec2020-05-29 10:28:26 +080053 * STDIO_DRIVER
TTornblomc640e072019-06-14 14:33:51 +020054 */
55FILE __stdout;
Edison Ai1c266ae2019-03-20 11:21:21 +080056/* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */
57int fputc(int ch, FILE *f)
58{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080059 (void)f;
60
Edison Ai1c266ae2019-03-20 11:21:21 +080061 /* Send byte to USART */
Mingyang Sunc6b458b2019-12-19 15:07:34 +080062 (void)stdio_output_string((const unsigned char *)&ch, 1);
Edison Ai1c266ae2019-03-20 11:21:21 +080063
64 /* Return character written */
65 return ch;
66}
67#elif defined(__GNUC__)
Kevin Peng524e3ec2020-05-29 10:28:26 +080068/* Redirects printf to STDIO_DRIVER in case of GNUARM */
Edison Ai1c266ae2019-03-20 11:21:21 +080069int _write(int fd, char *str, int len)
70{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080071 (void)fd;
Edison Ai1c266ae2019-03-20 11:21:21 +080072
Mingyang Sun1ec6e262019-10-23 22:24:22 +080073 /* Send string and return the number of characters written */
Mingyang Sunc6b458b2019-12-19 15:07:34 +080074 return stdio_output_string((const unsigned char *)str, (uint32_t)len);
Edison Ai1c266ae2019-03-20 11:21:21 +080075}
TTornblomc640e072019-06-14 14:33:51 +020076#elif defined(__ICCARM__)
77int putchar(int ch)
78{
79 /* Send byte to USART */
Mingyang Sunc6b458b2019-12-19 15:07:34 +080080 (void)stdio_output_string((const unsigned char *)&ch, 1);
TTornblomc640e072019-06-14 14:33:51 +020081
82 /* Return character written */
83 return ch;
84}
Edison Ai1c266ae2019-03-20 11:21:21 +080085#endif
86
87void stdio_init(void)
88{
Mingyang Sun1ec6e262019-10-23 22:24:22 +080089 int32_t ret;
Kevin Peng524e3ec2020-05-29 10:28:26 +080090 ret = STDIO_DRIVER.Initialize(NULL);
Edison Ai1c266ae2019-03-20 11:21:21 +080091 ASSERT_HIGH(ret);
92
Kevin Peng524e3ec2020-05-29 10:28:26 +080093 ret = STDIO_DRIVER.PowerControl(ARM_POWER_FULL);
Mingyang Sun1ec6e262019-10-23 22:24:22 +080094 ASSERT_HIGH(ret);
95
Kevin Peng524e3ec2020-05-29 10:28:26 +080096 ret = STDIO_DRIVER.Control(ARM_USART_MODE_ASYNCHRONOUS,
97 DEFAULT_UART_BAUDRATE);
Edison Ai1c266ae2019-03-20 11:21:21 +080098 ASSERT_HIGH(ret);
Mingyang Sun1ec6e262019-10-23 22:24:22 +080099
Kevin Peng524e3ec2020-05-29 10:28:26 +0800100 (void)STDIO_DRIVER.Control(ARM_USART_CONTROL_TX, 1);
Edison Ai1c266ae2019-03-20 11:21:21 +0800101}
102
103void stdio_uninit(void)
104{
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800105 int32_t ret;
106
Kevin Peng524e3ec2020-05-29 10:28:26 +0800107 (void)STDIO_DRIVER.PowerControl(ARM_POWER_OFF);
Mingyang Sun1ec6e262019-10-23 22:24:22 +0800108
Kevin Peng524e3ec2020-05-29 10:28:26 +0800109 ret = STDIO_DRIVER.Uninitialize();
Edison Ai1c266ae2019-03-20 11:21:21 +0800110 ASSERT_HIGH(ret);
111}