2262c3e104b6d2e3ab680c8e51ec960bb7fa8016
[simavr] / examples / parts / ac_input.c
1 /*
2         ac_input.c
3
4         Copyright Luki <humbell@ethz.ch>
5         Copyright 2011 Michel Pollet <buserror@gmail.com>
6
7         This file is part of simavr.
8
9         simavr is free software: you can redistribute it and/or modify
10         it under the terms of the GNU General Public License as published by
11         the Free Software Foundation, either version 3 of the License, or
12         (at your option) any later version.
13
14         simavr is distributed in the hope that it will be useful,
15         but WITHOUT ANY WARRANTY; without even the implied warranty of
16         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17         GNU General Public License for more details.
18
19         You should have received a copy of the GNU General Public License
20         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include <stdio.h>
24 #include "sim_avr.h"
25 #include "sim_time.h"
26 #include "ac_input.h"
27
28 #define USECS_PER_SECOND (1000 * 1000)
29 #define HZ (50)
30
31 static avr_cycle_count_t
32 switch_auto(
33                 struct avr_t * avr,
34         avr_cycle_count_t when,
35         void * param)
36 {
37         ac_input_t * b = (ac_input_t *) param;
38         b->value = !b->value;
39         avr_raise_irq(b->irq + IRQ_AC_OUT, b->value);
40         return when + avr_usec_to_cycles(avr, USECS_PER_SECOND / HZ);
41 }
42
43 static const char * name = ">ac_input";
44
45 void ac_input_init(avr_t *avr, ac_input_t *b)
46 {
47         b->irq = avr_alloc_irq(&avr->irq_pool, 0, IRQ_AC_COUNT, &name);
48         b->avr = avr;
49         b->value = 0;
50         avr_cycle_timer_register_usec(avr, USECS_PER_SECOND / HZ, switch_auto, b);
51         printf("ac_input_init period %duS or %d cycles\n",
52                         USECS_PER_SECOND / HZ,
53                         (int)avr_usec_to_cycles(avr, USECS_PER_SECOND / HZ));
54 }