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:
|
Miklos Balint | 6cbeba6 | 2018-04-12 17:31:34 +0200 | [diff] [blame] | 74 | /* UART0 is configured as a non-secure peripheral, so it cannot be
|
| 75 | * accessed using its secure alias. Ideally the driver would
|
| 76 | * be configured with the right properties, but for simplicity,
|
| 77 | * use a workaround for now
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 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
|
Miklos Balint | 6cbeba6 | 2018-04-12 17:31:34 +0200 | [diff] [blame] | 82 | memcpy(&ARM_UART0_DEV_S, &ARM_UART0_DEV_NS,
|
| 83 | sizeof(struct arm_uart_dev_t));
|
Avinash Mehta | 788036c | 2018-03-15 12:38:43 +0000 | [diff] [blame] | 84 | #endif
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 85 | Driver_USART = &Driver_USART0;
|
| 86 | break;
|
| 87 | case UART1_CHANNEL:
|
Miklos Balint | 6cbeba6 | 2018-04-12 17:31:34 +0200 | [diff] [blame] | 88 | #ifndef SECURE_UART1
|
| 89 | /* If UART1 is configured as a non-secure peripheral, it cannot be
|
| 90 | * accessed using its secure alias. Ideally the driver would
|
| 91 | * be configured with the right properties, but for simplicity,
|
| 92 | * use a workaround for now
|
| 93 | */
|
| 94 | #ifdef TARGET_MUSCA_A
|
| 95 | memcpy(&UART1_DEV_S, &UART1_DEV_NS, sizeof(struct uart_pl011_dev_t));
|
| 96 | #else
|
| 97 | memcpy(&ARM_UART1_DEV_S, &ARM_UART1_DEV_NS,
|
| 98 | sizeof(struct arm_uart_dev_t));
|
| 99 | #endif
|
| 100 | #endif
|
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 101 | Driver_USART = &Driver_USART1;
|
| 102 | break;
|
| 103 | default:
|
| 104 | ret = ARM_DRIVER_ERROR;
|
| 105 | }
|
| 106 | ASSERT_HIGH(ret);
|
| 107 |
|
| 108 | ret = Driver_USART->Initialize(NULL);
|
| 109 | ASSERT_HIGH(ret);
|
| 110 |
|
| 111 | ret = Driver_USART->Control(ARM_USART_MODE_ASYNCHRONOUS, 115200);
|
| 112 | ASSERT_HIGH(ret);
|
| 113 | }
|
| 114 |
|
| 115 | void uart_putc(unsigned char c)
|
| 116 | {
|
| 117 | int32_t ret = ARM_DRIVER_OK;
|
| 118 |
|
| 119 | ret = Driver_USART->Send(&c, 1);
|
| 120 | ASSERT_HIGH(ret);
|
| 121 | }
|
| 122 |
|
| 123 | unsigned char uart_getc(void)
|
| 124 | {
|
| 125 | unsigned char c = 0;
|
| 126 | int32_t ret = ARM_DRIVER_OK;
|
| 127 |
|
| 128 | ret = Driver_USART->Receive(&c, 1);
|
| 129 | ASSERT_HIGH(ret);
|
| 130 |
|
| 131 | return c;
|
| 132 | }
|