aboutsummaryrefslogtreecommitdiffstats
path: root/app/vuart.c
blob: 3c4ab79ac4fe3a764fce5208c1cfed5b0d2ab8cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "project.h"

#define RXR       ((0))
#define TXR       ((0))
#define DLLR      ((0))

#define IER       ((1))
#define DLHR      ((1))

#define IIR       ((2))
#define FCR       ((2))

#define LCR       ((3))
#define LCR_WS0           (1<<0)
#define LCR_WS1           (1<<1)
#define LCR_NSTOP         (1<<2)
#define LCR_PARITY_ENABLE (1<<3)
#define LCR_EVEN_PARITY   (1<<4)
#define LCR_STUCK_PARITY  (1<<5)
#define LCR_BREAK         (1<<6)
#define LCR_DLAB          (1<<7)

#define MCR      ((4))
#define MCR_DTR        (1<<0)
#define MCR_RTS        (1<<1)
#define MCR_AUX1       (1<<2)
#define MCR_AUX2       (1<<3)
#define MCR_LOOP       (1<<4)
#define MCR_AUTOFLOW   (1<<5)

#define LSR      ((5))
#define LSR_DA         (1<<0)
#define LSR_OR         (1<<1)
#define LSR_PE         (1<<2)
#define LSR_FE         (1<<3)
#define LSR_BRK        (1<<4)
#define LSR_THRE       (1<<5)
#define LSR_THREI      (1<<6)
#define LSR_FIFOE      (1<<7)

#define MSR   ((6))
#define MSR_DCTS  (1<<0)
#define MSR_DDSR  (1<<1)
#define MSR_TERI  (1<<2)
#define MSR_DDCD  (1<<3)
#define MSR_CTS   (1<<4)
#define MSR_DSR   (1<<5)
#define MSR_RI    (1<<6)
#define MSR_DCD   (1<<7)

#define SR    ((7))


/* 0 */
static uint8_t dllr = 1; /* 115200 baud */

/* 1 */
static uint8_t dlhr = 0;
static uint8_t ier = 0;

/* 2 */
static uint8_t iir = 0;

/* 3 */
static uint8_t lcr = LCR_WS0 | LCR_WS1; /* 8 bits, 1 stop, no parity */

/* 4 */
static uint8_t mcr = MCR_RTS | MCR_DTR; /* RTS and DTR on */

/* 5 */
static uint8_t lsr = LSR_THRE | LSR_THREI;

/* 6 */
static uint8_t msr = MSR_CTS | MSR_DSR | MSR_DCD;

/* 7 */
static uint8_t sr = 0;


static void vuart_xmit (uint8_t c)
{
  led1 = 100;
  ring_write_byte (&usart_tx_ring, c);
  usart_kick();
  ring_write_byte (&cdcacm_tx_ring, c);
}


static int vuart_recv_empty (void)
{
  if (!ring_empty (&usart_rx_ring))
    return 0;

  if (!ring_empty (&cdcacm_rx_ring))
    return 0;

  return 1;
}


static int vuart_recv (uint8_t *c)
{
  *c = 0;

  if (!ring_read_byte (&usart_rx_ring, c)) {
    led2 = 100;
    return 0;
  }

  if (!ring_read_byte (&cdcacm_rx_ring, c)) {
    led2 = 100;
    return 0;
  }

  return -1;
}


static void update_lsr (void)
{
  if (vuart_recv_empty())
    lsr &= ~LSR_DA;

  else
    lsr |= LSR_DA;
}

uint8_t vuart_read (unsigned reg)
{
  uint8_t val;

  switch (reg) {
  case RXR:

    //case DLLR:
    if (! (lcr & LCR_DLAB)) {
      vuart_recv (&val);
      update_lsr();
      return val;
    } else
      return dllr;

  case IER:
    //case DLLH:
    return (lcr & LCR_DLAB) ? dlhr : ier;

  case IIR:
    return iir;

  case LCR:
    return lcr;

  case MCR:
    return mcr;

  case LSR:
    update_lsr();
    val = lsr;
    lsr &= 0xe1;
    return val;

  case MSR:
    val = msr;
    msr &= 0xf0;
    return val;

  case SR:
    return sr;
  }

  return 0;
}


void vuart_write (unsigned reg, uint8_t val)
{
  switch (reg) {
  case RXR:

    //case DLLR:
    if (! (lcr & LCR_DLAB))
      vuart_xmit (val);

    else
      dllr = val;

    break;

  case IER:

    //case DLHR:
    if (! (lcr & LCR_DLAB))
      ier = val & 0xf;

    else
      dlhr = val;

    break;

  case LCR:
    lcr = val;
    break;

  case MCR:
    mcr = val & 0x1f;
    break;

  case SR:
    sr = val;
    break;
  }
}