4 This defines a sample for a very simple "peripheral"
5 that can talk to an AVR core.
6 It is in fact a bit more involved than strictly necessary,
7 but is made to demonstrante a few useful features that are
10 Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
12 This file is part of simavr.
14 simavr is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
19 simavr is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with simavr. If not, see <http://www.gnu.org/licenses/>.
33 * called when a SPI byte is sent
35 static void hc595_spi_in_hook(struct avr_irq_t * irq, uint32_t value, void * param)
37 hc595_t * p = (hc595_t*)param;
38 // send "old value" to any chained one..
39 avr_raise_irq(p->irq + IRQ_HC595_SPI_BYTE_OUT, p->value);
40 p->value = (p->value << 8) | (value & 0xff);
44 * called when a LATCH signal is sent
46 static void hc595_latch_hook(struct avr_irq_t * irq, uint32_t value, void * param)
48 hc595_t * p = (hc595_t*)param;
49 if (irq->value && !value) { // falling edge
51 avr_raise_irq(p->irq + IRQ_HC595_OUT, p->latch);
56 * called when a RESET signal is sent
58 static void hc595_reset_hook(struct avr_irq_t * irq, uint32_t value, void * param)
60 hc595_t * p = (hc595_t*)param;
61 if (irq->value && !value) // falling edge
62 p->latch = p->value = 0;
65 void hc595_init(hc595_t *p)
67 p->irq = avr_alloc_irq(0, IRQ_HC595_COUNT);
68 avr_irq_register_notify(p->irq + IRQ_HC595_SPI_BYTE_IN, hc595_spi_in_hook, p);
69 avr_irq_register_notify(p->irq + IRQ_HC595_IN_LATCH, hc595_latch_hook, p);
70 avr_irq_register_notify(p->irq + IRQ_HC595_IN_RESET, hc595_reset_hook, p);
72 p->latch = p->value = 0;