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>
|
Avinash Mehta | 788036c | 2018-03-15 12:38:43 +0000 | [diff] [blame] | 22 | #ifdef TARGET_MUSCA_A
|
| 23 | #include "uart_pl011_drv.h"
|
| 24 | #else
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 25 | #include "arm_uart_drv.h"
|
Avinash Mehta | 788036c | 2018-03-15 12:38:43 +0000 | [diff] [blame] | 26 | #endif
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 27 | #include "Driver_USART.h"
|
| 28 |
|
| 29 | #define ASSERT_HIGH(X) assert(X == ARM_DRIVER_OK)
|
| 30 |
|
| 31 | /* Imports USART driver */
|
| 32 | extern ARM_DRIVER_USART Driver_USART0;
|
| 33 | extern ARM_DRIVER_USART Driver_USART1;
|
| 34 |
|
| 35 | /* Struct FILE is implemented in stdio.h. Used to redirect printf to UART */
|
| 36 | FILE __stdout;
|
| 37 |
|
| 38 | /* Redirects printf to UART */
|
| 39 | __attribute__ ((weak)) int fputc(int ch, FILE *f) {
|
| 40 | /* Send byte to USART */
|
| 41 | uart_putc(ch);
|
| 42 |
|
| 43 | /* Return character written */
|
| 44 | return ch;
|
| 45 | }
|
| 46 |
|
Mate Toth-Pal | 31a2d96 | 2018-03-09 13:14:44 +0100 | [diff] [blame^] | 47 | int _write(int fd, char * str, int len)
|
| 48 | {
|
| 49 | for (int i = 0; i < len; i++)
|
| 50 | {
|
| 51 | uart_putc(str[i]);
|
| 52 | }
|
| 53 | return len;
|
| 54 | }
|
| 55 |
|
Avinash Mehta | 788036c | 2018-03-15 12:38:43 +0000 | [diff] [blame] | 56 | #ifdef TARGET_MUSCA_A
|
| 57 | extern struct uart_pl011_dev_t UART0_DEV_S, UART0_DEV_NS;
|
| 58 | extern struct uart_pl011_dev_t UART1_DEV_S, UART1_DEV_NS;
|
| 59 | #else
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 60 | extern struct arm_uart_dev_t ARM_UART0_DEV_S, ARM_UART0_DEV_NS;
|
| 61 | extern struct arm_uart_dev_t ARM_UART1_DEV_S, ARM_UART1_DEV_NS;
|
Avinash Mehta | 788036c | 2018-03-15 12:38:43 +0000 | [diff] [blame] | 62 | #endif
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 63 |
|
| 64 | /* Generic driver to be configured and used */
|
| 65 | ARM_DRIVER_USART *Driver_USART = NULL;
|
| 66 |
|
| 67 | void uart_init(enum uart_channel uchan)
|
| 68 | {
|
| 69 | int32_t ret = ARM_DRIVER_OK;
|
| 70 |
|
| 71 | /* Add a configuration step for the UART channel to use, 0 or 1 */
|
| 72 | switch(uchan) {
|
| 73 | case UART0_CHANNEL:
|
| 74 | /* UART0 is configured as a non-secure peripheral, so we wouldn't be
|
| 75 | * able to access it using its secure alias. Ideally, we would want
|
| 76 | * to use UART1 only from S side as it's a secure peripheral, but for
|
| 77 | * simplicity, leave the option to use UART0 and use a workaround
|
| 78 | */
|
Avinash Mehta | 788036c | 2018-03-15 12:38:43 +0000 | [diff] [blame] | 79 | #ifdef TARGET_MUSCA_A
|
| 80 | memcpy(&UART0_DEV_S, &UART0_DEV_NS, sizeof(struct uart_pl011_dev_t));
|
| 81 | #else
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 82 | memcpy(&ARM_UART0_DEV_S, &ARM_UART0_DEV_NS, sizeof(struct arm_uart_dev_t));
|
Avinash Mehta | 788036c | 2018-03-15 12:38:43 +0000 | [diff] [blame] | 83 | #endif
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 84 | Driver_USART = &Driver_USART0;
|
| 85 | break;
|
| 86 | case UART1_CHANNEL:
|
| 87 | Driver_USART = &Driver_USART1;
|
| 88 | break;
|
| 89 | default:
|
| 90 | ret = ARM_DRIVER_ERROR;
|
| 91 | }
|
| 92 | ASSERT_HIGH(ret);
|
| 93 |
|
| 94 | ret = Driver_USART->Initialize(NULL);
|
| 95 | ASSERT_HIGH(ret);
|
| 96 |
|
| 97 | ret = Driver_USART->Control(ARM_USART_MODE_ASYNCHRONOUS, 115200);
|
| 98 | ASSERT_HIGH(ret);
|
| 99 | }
|
| 100 |
|
| 101 | void uart_putc(unsigned char c)
|
| 102 | {
|
| 103 | int32_t ret = ARM_DRIVER_OK;
|
| 104 |
|
| 105 | ret = Driver_USART->Send(&c, 1);
|
| 106 | ASSERT_HIGH(ret);
|
| 107 | }
|
| 108 |
|
| 109 | unsigned char uart_getc(void)
|
| 110 | {
|
| 111 | unsigned char c = 0;
|
| 112 | int32_t ret = ARM_DRIVER_OK;
|
| 113 |
|
| 114 | ret = Driver_USART->Receive(&c, 1);
|
| 115 | ASSERT_HIGH(ret);
|
| 116 |
|
| 117 | return c;
|
| 118 | }
|