5 Right now just handle "write" to the serial port at any speed
6 and printf to the console when '\n' is written.
8 Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
10 This file is part of simavr.
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.
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.
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/>.
29 static void avr_uart_run(avr_t * avr, avr_io_t * port)
31 // printf("%s\n", __FUNCTION__);
34 static uint8_t avr_uart_read(struct avr_t * avr, uint8_t addr, void * param)
36 // avr_uart_t * p = (avr_uart_t *)param;
37 uint8_t v = avr->data[addr];
38 // printf("** PIN%c = %02x\n", p->name, v);
42 static void avr_uart_write(struct avr_t * avr, uint8_t addr, uint8_t v, void * param)
44 avr_uart_t * p = (avr_uart_t *)param;
46 if (addr == p->r_udr) {
47 // printf("UDR%c(%02x) = %02x\n", p->name, addr, v);
48 avr_core_watch_write(avr, addr, v);
50 // if the interupts are not used, still raised the UDRE and TXC flaga
51 avr_raise_interupt(avr, &p->udrc);
52 avr_raise_interupt(avr, &p->txc);
56 buf[l++] = v < ' ' ? '.' : v;
58 if (v == '\n' || l == 127) {
60 printf("\e[32m%s\e[0m\n", buf);
63 if (addr == p->r_ucsra) {
64 // get the bits before the write
65 uint8_t udre = avr_regbit_get(avr, p->udrc.raised);
66 uint8_t txc = avr_regbit_get(avr, p->txc.raised);
68 avr_core_watch_write(avr, addr, v);
70 // if writing one to a one, clear bit
71 if (udre && avr_regbit_get(avr, p->udrc.raised))
72 avr_regbit_clear(avr, p->udrc.raised);
73 if (txc && avr_regbit_get(avr, p->txc.raised))
74 avr_regbit_clear(avr, p->txc.raised);
78 void avr_uart_reset(avr_t * avr, struct avr_io_t *io)
80 avr_uart_t * p = (avr_uart_t *)io;
81 avr_regbit_set(avr, p->udrc.raised);
84 static avr_io_t _io = {
87 .reset = avr_uart_reset,
90 void avr_uart_init(avr_t * avr, avr_uart_t * p)
93 avr_register_io(avr, &p->io);
95 printf("%s UART%c UDR=%02x\n", __FUNCTION__, p->name, p->r_udr);
97 avr_register_io_write(avr, p->r_udr, avr_uart_write, p);
98 avr_register_io_write(avr, p->r_ucsra, avr_uart_write, p);
99 avr_register_io_read(avr, p->r_udr, avr_uart_read, p);