Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 1 | /*
|
Avinash Mehta | 788036c | 2018-03-15 12:38:43 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 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>
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 22 | #include "Driver_USART.h"
|
Gabor Kertesz | eb953f5 | 2018-07-17 13:36:28 +0200 | [diff] [blame] | 23 | #include "target_cfg.h"
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 24 |
|
| 25 | #define ASSERT_HIGH(X) assert(X == ARM_DRIVER_OK)
|
| 26 |
|
| 27 | /* Imports USART driver */
|
Gabor Kertesz | eb953f5 | 2018-07-17 13:36:28 +0200 | [diff] [blame] | 28 | extern ARM_DRIVER_USART TFM_DRIVER_STDIO;
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 29 |
|
Gabor Kertesz | eb953f5 | 2018-07-17 13:36:28 +0200 | [diff] [blame] | 30 | /* Struct FILE is implemented in stdio.h. Used to redirect printf to
|
| 31 | * TFM_DRIVER_STDIO
|
| 32 | */
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 33 | FILE __stdout;
|
| 34 |
|
Mate Toth-Pal | 77e4635 | 2018-04-10 15:25:49 +0200 | [diff] [blame^] | 35 | static 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 */
|
| 46 | int fputc(int ch, FILE *f)
|
| 47 | {
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 48 | /* Send byte to USART */
|
| 49 | uart_putc(ch);
|
| 50 |
|
| 51 | /* Return character written */
|
| 52 | return ch;
|
| 53 | }
|
Mate Toth-Pal | 77e4635 | 2018-04-10 15:25:49 +0200 | [diff] [blame^] | 54 | #elif defined(__GNUC__)
|
| 55 | /* Redirects printf to TFM_DRIVER_STDIO in case of GNUARM */
|
| 56 | int _write(int fd, char *str, int len)
|
Mate Toth-Pal | 31a2d96 | 2018-03-09 13:14:44 +0100 | [diff] [blame] | 57 | {
|
Mate Toth-Pal | 77e4635 | 2018-04-10 15:25:49 +0200 | [diff] [blame^] | 58 | int i;
|
| 59 |
|
| 60 | for (i = 0; i < len; i++) {
|
| 61 | /* Send byte to USART */
|
Mate Toth-Pal | 31a2d96 | 2018-03-09 13:14:44 +0100 | [diff] [blame] | 62 | uart_putc(str[i]);
|
| 63 | }
|
Mate Toth-Pal | 77e4635 | 2018-04-10 15:25:49 +0200 | [diff] [blame^] | 64 |
|
| 65 | /* Return the number of characters written */
|
Mate Toth-Pal | 31a2d96 | 2018-03-09 13:14:44 +0100 | [diff] [blame] | 66 | return len;
|
| 67 | }
|
Mate Toth-Pal | 77e4635 | 2018-04-10 15:25:49 +0200 | [diff] [blame^] | 68 | #endif
|
Mate Toth-Pal | 31a2d96 | 2018-03-09 13:14:44 +0100 | [diff] [blame] | 69 |
|
Gabor Kertesz | eb953f5 | 2018-07-17 13:36:28 +0200 | [diff] [blame] | 70 | void stdio_init(void)
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 71 | {
|
| 72 | int32_t ret = ARM_DRIVER_OK;
|
Gabor Kertesz | eb953f5 | 2018-07-17 13:36:28 +0200 | [diff] [blame] | 73 | ret = TFM_DRIVER_STDIO.Initialize(NULL);
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 74 | ASSERT_HIGH(ret);
|
| 75 |
|
Gabor Kertesz | eb953f5 | 2018-07-17 13:36:28 +0200 | [diff] [blame] | 76 | ret = TFM_DRIVER_STDIO.Control(ARM_USART_MODE_ASYNCHRONOUS, 115200);
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 77 | ASSERT_HIGH(ret);
|
| 78 | }
|
| 79 |
|