timer_64led: Brand new example board, opengl display too
[simavr] / examples / parts / hc595.c
1 /*
2         hc595.c
3
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
8         easy to use.
9         
10         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
11
12         This file is part of simavr.
13
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.
18
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.
23
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/>.
26  */
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include "hc595.h"
31
32 /*
33  * called when a SPI byte is sent
34  */
35 static void hc595_spi_in_hook(struct avr_irq_t * irq, uint32_t value, void * param)
36 {
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);
41 }
42
43 /*
44  * called when a LATCH signal is sent
45  */
46 static void hc595_latch_hook(struct avr_irq_t * irq, uint32_t value, void * param)
47 {
48         hc595_t * p = (hc595_t*)param;
49         if (irq->value && !value) {     // falling edge
50                 p->latch = p->value;
51                 avr_raise_irq(p->irq + IRQ_HC595_OUT, p->latch);
52         }
53 }
54
55 /*
56  * called when a RESET signal is sent
57  */
58 static void hc595_reset_hook(struct avr_irq_t * irq, uint32_t value, void * param)
59 {
60         hc595_t * p = (hc595_t*)param;
61         if (irq->value && !value)       // falling edge
62                 p->latch = p->value = 0;
63 }
64
65 void hc595_init(hc595_t *p)
66 {
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);
71         
72         p->latch = p->value = 0;
73 }
74