blob: 35d4160d1b27249efeca4ee3048d6ff0f44efc51 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
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
31void UartStdOutInit(void)
32{
Kshitij Sisodia105ed712021-10-04 14:31:25 +010033 CMSDK_UART0->BAUDDIV = PERIF_CLK / 115200; /* => (25 or 32 MHz) / (115200 bps). */
alexander3c798932021-03-26 21:42:19 +000034 CMSDK_UART0->CTRL = ((1ul << 0) | /* TX enable. */
35 (1ul << 1) ); /* RX enable. */
36 return;
37}
38
39unsigned char UartPutc(unsigned char my_ch)
40{
41 while ((CMSDK_UART0->STATE & 1)); /* Wait if Transmit Holding register is full. */
42
43 if (my_ch == '\n') {
44 CMSDK_UART0->DATA = '\r';
45 while ((CMSDK_UART0->STATE & 1)); /* Wait if Transmit Holding register is full. */
46 }
47
48 CMSDK_UART0->DATA = my_ch; /* Write to transmit holding register. */
49 return (my_ch);
50}
51
52unsigned char UartGetc(void)
53{
54 unsigned char my_ch;
55 unsigned int cnt;
56
57 /* Wait if Receive Holding register is empty. */
58 while (0 == (CMSDK_UART0->STATE & 2)) {
59 cnt = MPS3_FPGAIO->CLK100HZ / 50;
60 if (cnt & 0x8) {
61 MPS3_FPGAIO->LED = 0x01 << (cnt & 0x7);
62 }
63 else {
64 MPS3_FPGAIO->LED = 0x80 >> (cnt & 0x7);
65 }
66 }
67
68 my_ch = CMSDK_UART0->DATA;
69
70 /* Convert CR to LF. */
71 if(my_ch == '\r') {
72 my_ch = '\n';
73 }
74
75 return (my_ch);
76}
77
78bool GetLine(char *lp, unsigned int len)
79{
80 unsigned int cnt = 0;
81 char c;
82
83 do {
84 c = UartGetc ();
85 switch (c) {
86 case CNTLQ: /* Ignore Control S/Q. */
87 case CNTLS:
88 break;
89
90 case BACKSPACE:
91 case DEL:
92 if (cnt == 0) {
93 break;
94 }
95 cnt--; /* Decrement count. */
96 lp--; /* Decrement line pointer. */
97 UartPutc (0x08); /* Echo backspace. */
98 UartPutc (' ');
99 UartPutc (0x08);
100 fflush (stdout);
101 break;
102
103 case ESC:
104 case 0:
105 *lp = 0; /* ESC - stop editing line. */
106 return false;
107
108 case CR: /* CR - done, stop editing line. */
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100109 UartPutc (*lp = c); /* Echo and store character. */
alexander3c798932021-03-26 21:42:19 +0000110 lp++; /* Increment line pointer */
111 cnt++; /* and count. */
112 c = LF;
Isabella Gottardic5d8bda2021-07-21 10:35:08 +0100113 UartPutc (*lp = c); /* Echo and store character. */
114 fflush (stdout);
115 lp++; /* Increment line pointer */
116 cnt++; /* and count. */
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100117 break;
alexander3c798932021-03-26 21:42:19 +0000118 default:
119 UartPutc (*lp = c); /* Echo and store character. */
120 fflush (stdout);
121 lp++; /* Increment line pointer */
122 cnt++; /* and count. */
123 break;
124 }
125 } while (cnt < len - 2 && c != LF); /* Check limit and CR. */
126 *lp = 0; /* Mark end of string. */
127
128 return true;
129}
130
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100131__attribute__((noreturn)) void UartEndSimulation(int code)
alexander3c798932021-03-26 21:42:19 +0000132{
133 UartPutc((char) 0x4); /* End of simulation */
134 UartPutc((char) code); /* End of simulation */
135 while(1);
136}