blob: 75e8b180d68be233d5ca454c6c682a783843fd82 [file] [log] [blame]
Marc Moreno Berengue20dab392017-11-29 13:18:58 +00001/*
Avinash Mehta788036c2018-03-15 12:38:43 +00002 * Copyright (c) 2017-2018 ARM Limited
Marc Moreno Berengue20dab392017-11-29 13:18:58 +00003 *
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>
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000022#include "Driver_USART.h"
Gabor Kerteszeb953f52018-07-17 13:36:28 +020023#include "target_cfg.h"
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000024
25#define ASSERT_HIGH(X) assert(X == ARM_DRIVER_OK)
26
27/* Imports USART driver */
Gabor Kerteszeb953f52018-07-17 13:36:28 +020028extern ARM_DRIVER_USART TFM_DRIVER_STDIO;
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000029
Gabor Kerteszeb953f52018-07-17 13:36:28 +020030/* Struct FILE is implemented in stdio.h. Used to redirect printf to
31 * TFM_DRIVER_STDIO
32 */
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000033FILE __stdout;
34
Mate Toth-Pal77e46352018-04-10 15:25:49 +020035static 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{
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000048 /* Send byte to USART */
49 uart_putc(ch);
50
51 /* Return character written */
52 return ch;
53}
Mate Toth-Pal77e46352018-04-10 15:25:49 +020054#elif defined(__GNUC__)
55/* Redirects printf to TFM_DRIVER_STDIO in case of GNUARM */
56int _write(int fd, char *str, int len)
Mate Toth-Pal31a2d962018-03-09 13:14:44 +010057{
Mate Toth-Pal77e46352018-04-10 15:25:49 +020058 int i;
59
60 for (i = 0; i < len; i++) {
61 /* Send byte to USART */
Mate Toth-Pal31a2d962018-03-09 13:14:44 +010062 uart_putc(str[i]);
63 }
Mate Toth-Pal77e46352018-04-10 15:25:49 +020064
65 /* Return the number of characters written */
Mate Toth-Pal31a2d962018-03-09 13:14:44 +010066 return len;
67}
Mate Toth-Pal77e46352018-04-10 15:25:49 +020068#endif
Mate Toth-Pal31a2d962018-03-09 13:14:44 +010069
Gabor Kerteszeb953f52018-07-17 13:36:28 +020070void stdio_init(void)
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000071{
72 int32_t ret = ARM_DRIVER_OK;
Gabor Kerteszeb953f52018-07-17 13:36:28 +020073 ret = TFM_DRIVER_STDIO.Initialize(NULL);
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000074 ASSERT_HIGH(ret);
75
Gabor Kerteszeb953f52018-07-17 13:36:28 +020076 ret = TFM_DRIVER_STDIO.Control(ARM_USART_MODE_ASYNCHRONOUS, 115200);
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000077 ASSERT_HIGH(ret);
78}
79