alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. All rights reserved. |
| 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | #include "uart_stdout.h" |
| 18 | |
| 19 | #include "device_mps3.h" |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | |
| 23 | #define CNTLQ 0x11 |
| 24 | #define CNTLS 0x13 |
| 25 | #define DEL 0x7F |
| 26 | #define BACKSPACE 0x08 |
| 27 | #define CR 0x0D |
| 28 | #define LF 0x0A |
| 29 | #define ESC 0x1B |
| 30 | |
| 31 | void UartStdOutInit(void) |
| 32 | { |
| 33 | /* NOTE: SystemCoreClock should have been set before initialising UART. */ |
| 34 | CMSDK_UART0->BAUDDIV = SystemCoreClock / 115200; /* => (25 or 32 MHz) / (115200 bps). */ |
| 35 | CMSDK_UART0->CTRL = ((1ul << 0) | /* TX enable. */ |
| 36 | (1ul << 1) ); /* RX enable. */ |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | unsigned char UartPutc(unsigned char my_ch) |
| 41 | { |
| 42 | while ((CMSDK_UART0->STATE & 1)); /* Wait if Transmit Holding register is full. */ |
| 43 | |
| 44 | if (my_ch == '\n') { |
| 45 | CMSDK_UART0->DATA = '\r'; |
| 46 | while ((CMSDK_UART0->STATE & 1)); /* Wait if Transmit Holding register is full. */ |
| 47 | } |
| 48 | |
| 49 | CMSDK_UART0->DATA = my_ch; /* Write to transmit holding register. */ |
| 50 | return (my_ch); |
| 51 | } |
| 52 | |
| 53 | unsigned char UartGetc(void) |
| 54 | { |
| 55 | unsigned char my_ch; |
| 56 | unsigned int cnt; |
| 57 | |
| 58 | /* Wait if Receive Holding register is empty. */ |
| 59 | while (0 == (CMSDK_UART0->STATE & 2)) { |
| 60 | cnt = MPS3_FPGAIO->CLK100HZ / 50; |
| 61 | if (cnt & 0x8) { |
| 62 | MPS3_FPGAIO->LED = 0x01 << (cnt & 0x7); |
| 63 | } |
| 64 | else { |
| 65 | MPS3_FPGAIO->LED = 0x80 >> (cnt & 0x7); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | my_ch = CMSDK_UART0->DATA; |
| 70 | |
| 71 | /* Convert CR to LF. */ |
| 72 | if(my_ch == '\r') { |
| 73 | my_ch = '\n'; |
| 74 | } |
| 75 | |
| 76 | return (my_ch); |
| 77 | } |
| 78 | |
| 79 | bool GetLine(char *lp, unsigned int len) |
| 80 | { |
| 81 | unsigned int cnt = 0; |
| 82 | char c; |
| 83 | |
| 84 | do { |
| 85 | c = UartGetc (); |
| 86 | switch (c) { |
| 87 | case CNTLQ: /* Ignore Control S/Q. */ |
| 88 | case CNTLS: |
| 89 | break; |
| 90 | |
| 91 | case BACKSPACE: |
| 92 | case DEL: |
| 93 | if (cnt == 0) { |
| 94 | break; |
| 95 | } |
| 96 | cnt--; /* Decrement count. */ |
| 97 | lp--; /* Decrement line pointer. */ |
| 98 | UartPutc (0x08); /* Echo backspace. */ |
| 99 | UartPutc (' '); |
| 100 | UartPutc (0x08); |
| 101 | fflush (stdout); |
| 102 | break; |
| 103 | |
| 104 | case ESC: |
| 105 | case 0: |
| 106 | *lp = 0; /* ESC - stop editing line. */ |
| 107 | return false; |
| 108 | |
| 109 | case CR: /* CR - done, stop editing line. */ |
| 110 | *lp = c; |
| 111 | lp++; /* Increment line pointer */ |
| 112 | cnt++; /* and count. */ |
| 113 | c = LF; |
| 114 | default: |
| 115 | UartPutc (*lp = c); /* Echo and store character. */ |
| 116 | fflush (stdout); |
| 117 | lp++; /* Increment line pointer */ |
| 118 | cnt++; /* and count. */ |
| 119 | break; |
| 120 | } |
| 121 | } while (cnt < len - 2 && c != LF); /* Check limit and CR. */ |
| 122 | *lp = 0; /* Mark end of string. */ |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | void UartEndSimulation(int code) |
| 128 | { |
| 129 | UartPutc((char) 0x4); /* End of simulation */ |
| 130 | UartPutc((char) code); /* End of simulation */ |
| 131 | while(1); |
| 132 | } |