1808710597ff002aa4a6b2e27f2fa044f7bd3b7f
[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 static avr_cycle_count_t
29 switch_auto(struct avr_t * avr,
30         avr_cycle_count_t when, void * param)
31 {
32         ac_input_t * b = (ac_input_t *) param;
33         b->value = !b->value;
34         avr_raise_irq(b->irq + IRQ_AC_OUT, b->value);
35         return when + avr_usec_to_cycles(avr, 100000 / 50);
36 }
37
38 void ac_input_init(avr_t *avr, ac_input_t *b)
39 {
40         const char * name = ">ac_input";
41         b->irq = avr_alloc_irq(&avr->irq_pool, 0, IRQ_AC_COUNT, &name);
42         b->avr = avr;
43         b->value = 0;
44         avr_cycle_timer_register_usec(avr, 100000 / 50, switch_auto, b);
45         printf("ac_input_init period %duS or %d cycles\n",
46                         100000 / 50, (int)avr_usec_to_cycles(avr, 100000 / 50));
47 }