blob: 401b0b6ec4d4620ad9d51845d7d4a417aab185bd [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
Gabor Kerteszeb953f52018-07-17 13:36:28 +020035/* Redirects printf to TFM_DRIVER_STDIO */
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000036__attribute__ ((weak)) int fputc(int ch, FILE *f) {
37 /* Send byte to USART */
38 uart_putc(ch);
39
40 /* Return character written */
41 return ch;
42}
43
Mate Toth-Pal31a2d962018-03-09 13:14:44 +010044int _write(int fd, char * str, int len)
45{
Gabor Kerteszeb953f52018-07-17 13:36:28 +020046 for (int i = 0; i < len; i++) {
Mate Toth-Pal31a2d962018-03-09 13:14:44 +010047 uart_putc(str[i]);
48 }
49 return len;
50}
51
Gabor Kerteszeb953f52018-07-17 13:36:28 +020052void stdio_init(void)
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000053{
54 int32_t ret = ARM_DRIVER_OK;
Gabor Kerteszeb953f52018-07-17 13:36:28 +020055 ret = TFM_DRIVER_STDIO.Initialize(NULL);
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000056 ASSERT_HIGH(ret);
57
Gabor Kerteszeb953f52018-07-17 13:36:28 +020058 ret = TFM_DRIVER_STDIO.Control(ARM_USART_MODE_ASYNCHRONOUS, 115200);
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000059 ASSERT_HIGH(ret);
60}
61
62void uart_putc(unsigned char c)
63{
64 int32_t ret = ARM_DRIVER_OK;
65
Gabor Kerteszeb953f52018-07-17 13:36:28 +020066 ret = TFM_DRIVER_STDIO.Send(&c, 1);
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000067 ASSERT_HIGH(ret);
68}
69
70unsigned char uart_getc(void)
71{
72 unsigned char c = 0;
73 int32_t ret = ARM_DRIVER_OK;
74
Gabor Kerteszeb953f52018-07-17 13:36:28 +020075 ret = TFM_DRIVER_STDIO.Receive(&c, 1);
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000076 ASSERT_HIGH(ret);
77
78 return c;
79}