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/>.
34 * called when a SPI byte is sent
36 static void hc595_spi_in_hook(struct avr_irq_t * irq, uint32_t value, void * param)
38 hc595_t * p = (hc595_t*)param;
39 // send "old value" to any chained one..
40 avr_raise_irq(p->irq + IRQ_HC595_SPI_BYTE_OUT, p->value);
41 p->value = (p->value << 8) | (value & 0xff);
45 * called when a LATCH signal is sent
47 static void hc595_latch_hook(struct avr_irq_t * irq, uint32_t value, void * param)
49 hc595_t * p = (hc595_t*)param;
50 if (irq->value && !value) { // falling edge
52 avr_raise_irq(p->irq + IRQ_HC595_OUT, p->latch);
57 * called when a RESET signal is sent
59 static void hc595_reset_hook(struct avr_irq_t * irq, uint32_t value, void * param)
61 hc595_t * p = (hc595_t*)param;
62 if (irq->value && !value) // falling edge
63 p->latch = p->value = 0;
66 const char * irq_names[IRQ_HC595_COUNT] = {
67 [IRQ_HC595_SPI_BYTE_IN] = "8<hc595.in",
68 [IRQ_HC595_SPI_BYTE_OUT] = "8>hc595.chain",
69 [IRQ_HC595_IN_LATCH] = "<hc595.latch",
70 [IRQ_HC595_IN_RESET] = "<hc595.reset",
71 [IRQ_HC595_OUT] = "8>hc595.out",
79 p->irq = avr_alloc_irq(&avr->irq_pool, 0, IRQ_HC595_COUNT, irq_names);
80 avr_irq_register_notify(p->irq + IRQ_HC595_SPI_BYTE_IN, hc595_spi_in_hook, p);
81 avr_irq_register_notify(p->irq + IRQ_HC595_IN_LATCH, hc595_latch_hook, p);
82 avr_irq_register_notify(p->irq + IRQ_HC595_IN_RESET, hc595_reset_hook, p);
84 p->latch = p->value = 0;