parts: Updated to name their IRQs
[simavr] / examples / parts / button.c
1 /*
2         button.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 "sim_avr.h"
31 #include "button.h"
32
33 static avr_cycle_count_t
34 button_auto_release(
35                 avr_t * avr,
36                 avr_cycle_count_t when,
37                 void * param)
38 {
39         button_t * b = (button_t *)param;
40         avr_raise_irq(b->irq + IRQ_BUTTON_OUT, 1);
41         printf("button_auto_release\n");
42         return 0;
43 }
44
45 /*
46  * button press. set the "pin" to zerok and register a timer
47  * that will reset it in a few usecs
48  */
49 void
50 button_press(
51                 button_t * b,
52                 uint32_t duration_usec)
53 {
54         avr_cycle_timer_cancel(b->avr, button_auto_release, b);
55         avr_raise_irq(b->irq + IRQ_BUTTON_OUT, 0);// press
56         // register the auto-release
57         avr_cycle_timer_register_usec(b->avr, duration_usec, button_auto_release, b);
58 }
59
60 void
61 button_init(
62                 avr_t *avr,
63                 button_t * b,
64                 const char * name)
65 {
66         b->irq = avr_alloc_irq(&avr->irq_pool, 0, IRQ_BUTTON_COUNT, &name);
67         b->avr = avr;
68 }
69