GDB working, some more source massaging
[simavr] / simavr / sim / avr_spi.c
1 /*
2         avr_spi.c
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
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.
12
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.
17
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/>.
20  */
21
22 #include <stdio.h>
23 #include "avr_spi.h"
24
25 static void avr_spi_run(avr_t * avr, avr_io_t * port)
26 {
27 //      printf("%s\n", __FUNCTION__);
28 }
29
30 static uint8_t avr_spi_read(struct avr_t * avr, uint8_t addr, void * param)
31 {
32         avr_spi_t * p = (avr_spi_t *)param;
33         uint8_t v = avr->data[addr];
34 //      printf("** PIN%c = %02x\n", p->name, v);
35         return v;
36 }
37
38 static void avr_spi_write(struct avr_t * avr, uint8_t addr, uint8_t v, void * param)
39 {
40         avr_spi_t * p = (avr_spi_t *)param;
41
42         if (addr == p->r_spdr) {
43         //      printf("UDR%c(%02x) = %02x\n", p->name, addr, v);
44                 avr_core_watch_write(avr, addr, v);
45         }
46 }
47
48 static void avr_spi_irq_input(avr_t * avr, struct avr_irq_t * irq, uint32_t value, void * param)
49 {
50         avr_spi_t * p = (avr_spi_t *)param;
51
52         // check to see fi receiver is enabled
53         if (!avr_regbit_get(avr, p->spe))
54                 return;
55
56         // double buffer the input.. ?
57 }
58
59 void avr_spi_reset(avr_t * avr, struct avr_io_t *io)
60 {
61         avr_spi_t * p = (avr_spi_t *)io;
62         avr_irq_register_notify(avr, p->io.irq + SPI_IRQ_INPUT, avr_spi_irq_input, p);
63 }
64
65 static  avr_io_t        _io = {
66         .kind = "spi",
67         .run = avr_spi_run,
68         .reset = avr_spi_reset,
69 };
70
71 void avr_spi_init(avr_t * avr, avr_spi_t * p)
72 {
73         p->io = _io;
74         avr_register_io(avr, &p->io);
75
76         printf("%s SPI%c init\n", __FUNCTION__, p->name);
77
78         // allocate this module's IRQ
79         p->io.irq_count = SPI_IRQ_COUNT;
80         p->io.irq = avr_alloc_irq(avr, 0, p->io.irq_count);
81         p->io.irq_ioctl_get = AVR_IOCTL_SPI_GETIRQ(p->name);
82
83         avr_register_io_write(avr, p->r_spdr, avr_spi_write, p);
84         avr_register_io_read(avr, p->r_spdr, avr_spi_read, p);
85 }
86