4 Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
6 This file is part of simavr.
8 simavr is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 simavr is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with simavr. If not, see <http://www.gnu.org/licenses/>.
25 static uint8_t avr_twi_read(struct avr_t * avr, avr_io_addr_t addr, void * param)
27 avr_twi_t * p = (avr_twi_t *)param;
28 // uint8_t v = p->input_data_register;
29 // p->input_data_register = 0;
30 // printf("avr_twi_read = %02x\n", v);
34 static void avr_twi_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
36 avr_twi_t * p = (avr_twi_t *)param;
38 if (addr == p->r_spdr) {
39 // printf("avr_twi_write = %02x\n", v);
40 avr_core_watch_write(avr, addr, v);
42 if (avr_regbit_get(avr, p->spe)) {
43 // in master mode, any byte is sent as it comes..
44 if (avr_regbit_get(avr, p->mstr)) {
45 avr_raise_irq(p->io.irq + TWI_IRQ_OUTPUT, v);
52 static void avr_twi_irq_input(struct avr_irq_t * irq, uint32_t value, void * param)
54 avr_twi_t * p = (avr_twi_t *)param;
55 avr_t * avr = p->io.avr;
57 // check to see if we are enabled
58 if (!avr_regbit_get(avr, p->twen))
61 // double buffer the input.. ?
62 p->input_data_register = value;
63 avr_raise_interrupt(avr, &p->twi);
66 // 'output' the byte only when we received one...
67 if (!avr_regbit_get(avr, p->mstr)) {
68 avr_raise_irq(p->io.irq + TWI_IRQ_OUTPUT, avr->data[p->r_spdr]);
74 // handle a data write, after a (re)start
75 static int twi_slave_write(struct twi_slave_t* p, uint8_t v)
80 // handle a data read, after a (re)start
81 static uint8_t twi_slave_read(struct twi_slave_t* p)
87 static int avr_twi_ioctl(struct avr_io_t * port, uint32_t ctl, void * io_param)
89 avr_twi_t * p = (avr_twi_t *)port;
92 if (ctl == AVR_IOCTL_TWI_GETSLAVE(p->name)) {
93 *(twi_slave_t**)io_param = &p->slave;
94 } else if (ctl == AVR_IOCTL_TWI_GETBUS(p->name)) {
95 *(twi_bus_t**)io_param = &p->bus;
101 void avr_twi_reset(struct avr_io_t *io)
103 avr_twi_t * p = (avr_twi_t *)io;
104 //avr_irq_register_notify(p->io.irq + TWI_IRQ_INPUT, avr_twi_irq_input, p);
107 static avr_io_t _io = {
109 .reset = avr_twi_reset,
110 .ioctl = avr_twi_ioctl,
113 void avr_twi_init(avr_t * avr, avr_twi_t * p)
116 avr_register_io(avr, &p->io);
117 avr_register_vector(avr, &p->twi);
118 // p->slave = slave_driver; // get default callbacks
119 twi_slave_init(&p->slave, 0, p);
120 twi_bus_init(&p->bus);
122 //printf("%s TWI%c init\n", __FUNCTION__, p->name);
124 // allocate this module's IRQ
125 avr_io_setirqs(&p->io, AVR_IOCTL_TWI_GETIRQ(p->name), TWI_IRQ_COUNT, NULL);
127 avr_register_io_write(avr, p->r_twdr, avr_twi_write, p);
128 avr_register_io_read(avr, p->r_twdr, avr_twi_read, p);