UART: Delay TX interupt a few cycles
[simavr] / simavr / sim / avr_uart.c
1 /*
2         avr_uart.c
3
4         Handles UART access
5         Right now just handle "write" to the serial port at any speed
6         and printf to the console when '\n' is written.
7
8         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
9
10         This file is part of simavr.
11
12         simavr is free software: you can redistribute it and/or modify
13         it under the terms of the GNU General Public License as published by
14         the Free Software Foundation, either version 3 of the License, or
15         (at your option) any later version.
16
17         simavr is distributed in the hope that it will be useful,
18         but WITHOUT ANY WARRANTY; without even the implied warranty of
19         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20         GNU General Public License for more details.
21
22         You should have received a copy of the GNU General Public License
23         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26 #include <stdio.h>
27 #include "avr_uart.h"
28
29 DEFINE_FIFO(uint8_t, uart_fifo, 128);
30
31 static avr_cycle_count_t avr_uart_txc_raise(struct avr_t * avr, avr_cycle_count_t when, void * param)
32 {
33         avr_uart_t * p = (avr_uart_t *)param;
34         if (avr_regbit_get(avr, p->txen)) {
35                 // if the interrupts are not used, still raised the UDRE and TXC flaga
36                 avr_raise_interrupt(avr, &p->udrc);
37                 avr_raise_interrupt(avr, &p->txc);
38         }
39         return 0;
40 }
41
42 static avr_cycle_count_t avr_uart_rxc_raise(struct avr_t * avr, avr_cycle_count_t when, void * param)
43 {
44         avr_uart_t * p = (avr_uart_t *)param;
45         if (avr_regbit_get(avr, p->rxen))
46                 avr_raise_interrupt(avr, &p->rxc);
47         return 0;
48 }
49
50 static uint8_t avr_uart_read(struct avr_t * avr, avr_io_addr_t addr, void * param)
51 {
52         avr_uart_t * p = (avr_uart_t *)param;
53
54         if (!avr_regbit_get(avr, p->rxen)) {
55                 avr->data[addr] = 0;
56                 // made to trigger potential watchpoints
57                 avr_core_watch_read(avr, addr);
58                 return 0;
59         }
60         uint8_t v = uart_fifo_read(&p->input);
61
62         avr->data[addr] = v;
63         // made to trigger potential watchpoints
64         v = avr_core_watch_read(avr, addr);
65
66         if (!uart_fifo_isempty(&p->input))
67                 avr_cycle_timer_register_usec(avr, 100, avr_uart_rxc_raise, p); // should be uart speed dependent
68
69         return v;
70 }
71
72 static void avr_uart_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
73 {
74         avr_uart_t * p = (avr_uart_t *)param;
75
76         if (addr == p->r_udr) {
77                 avr_core_watch_write(avr, addr, v);
78
79                 avr_regbit_clear(avr, p->udrc.raised);
80                 avr_cycle_timer_register_usec(avr, 100, avr_uart_txc_raise, p); // should be uart speed dependent
81
82                 static char buf[128];
83                 static int l = 0;
84                 buf[l++] = v < ' ' ? '.' : v;
85                 buf[l] = 0;
86                 if (v == '\n' || l == 127) {
87                         l = 0;
88                         printf("\e[32m%s\e[0m\n", buf);
89                 }
90 //              printf("UDR%c(%02x) = %02x\n", p->name, addr, v);
91                 // tell other modules we are "outputing" a byte
92                 if (avr_regbit_get(avr, p->txen))
93                         avr_raise_irq(p->io.irq + UART_IRQ_OUTPUT, v);
94         } else {
95                 // get the bits before the write
96                 uint8_t udre = avr_regbit_get(avr, p->udrc.raised);
97                 uint8_t txc = avr_regbit_get(avr, p->txc.raised);
98
99                 avr_core_watch_write(avr, addr, v);
100
101                 // if writing one to a one, clear bit
102                 if (udre && avr_regbit_get(avr, p->udrc.raised))
103                         avr_regbit_clear(avr, p->udrc.raised);
104                 if (txc && avr_regbit_get(avr, p->txc.raised))
105                         avr_regbit_clear(avr, p->txc.raised);
106         }
107 }
108
109 static void avr_uart_irq_input(struct avr_irq_t * irq, uint32_t value, void * param)
110 {
111         avr_uart_t * p = (avr_uart_t *)param;
112         avr_t * avr = p->io.avr;
113
114         // check to see fi receiver is enabled
115         if (!avr_regbit_get(avr, p->rxen))
116                 return;
117
118         if (uart_fifo_isempty(&p->input))
119                 avr_cycle_timer_register_usec(avr, 100, avr_uart_rxc_raise, p); // should be uart speed dependent
120         uart_fifo_write(&p->input, value); // add to fifo
121 }
122
123
124 void avr_uart_reset(struct avr_io_t *io)
125 {
126         avr_uart_t * p = (avr_uart_t *)io;
127         avr_t * avr = p->io.avr;
128         avr_regbit_set(avr, p->udrc.raised);
129         avr_irq_register_notify(p->io.irq + UART_IRQ_INPUT, avr_uart_irq_input, p);
130         avr_cycle_timer_cancel(avr, avr_uart_rxc_raise, p);
131         avr_cycle_timer_cancel(avr, avr_uart_txc_raise, p);
132         uart_fifo_reset(&p->input);
133
134         // DEBUG allow printf without fidding with enabling the uart
135         avr_regbit_set(avr, p->txen);
136
137 }
138
139 static  avr_io_t        _io = {
140         .kind = "uart",
141         .reset = avr_uart_reset,
142 };
143
144 void avr_uart_init(avr_t * avr, avr_uart_t * p)
145 {
146         p->io = _io;
147         avr_register_io(avr, &p->io);
148
149 //      printf("%s UART%c UDR=%02x\n", __FUNCTION__, p->name, p->r_udr);
150
151         // allocate this module's IRQ
152         p->io.irq_count = UART_IRQ_COUNT;
153         p->io.irq = avr_alloc_irq(0, p->io.irq_count);
154         p->io.irq_ioctl_get = AVR_IOCTL_UART_GETIRQ(p->name);
155
156         avr_register_io_write(avr, p->r_udr, avr_uart_write, p);
157         avr_register_io_read(avr, p->r_udr, avr_uart_read, p);
158
159         avr_register_io_write(avr, p->r_ucsra, avr_uart_write, p);
160 }
161