new global_logger, used in AVR_LOG(), default is stdout/stderr
[simavr] / simavr / cores / sim_tiny13.c
1 /*
2         sim_tiny13.c
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5                              Jon Escombe <lists@dresco.co.uk>
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 "sim_avr.h"
24 #include "sim_core_declare.h"
25 #include "avr_eeprom.h"
26 #include "avr_watchdog.h"
27 #include "avr_extint.h"
28 #include "avr_ioport.h"
29 #include "avr_timer.h"
30
31 #define _AVR_IO_H_
32 #define __ASSEMBLER__
33 #include "avr/iotn13.h"
34
35 static void init(struct avr_t * avr);
36 static void reset(struct avr_t * avr);
37
38
39 static const struct mcu_t {
40         avr_t core;
41         avr_eeprom_t    eeprom;
42         avr_watchdog_t  watchdog;
43         avr_extint_t    extint;
44         avr_ioport_t    portb;
45         avr_timer_t             timer0;
46 } mcu = {
47         .core = {
48                 .mmcu = "attiny13",
49
50                 /*
51                  * tiny13 has no extended fuse byte, so can not use DEFAULT_CORE macro
52                  */
53                 .ramend = RAMEND,
54                 .flashend = FLASHEND,
55                 .e2end = E2END,
56                 .vector_size = 2,
57 // Disable signature for now, for ubuntu, gentoo and other using old avr toolchain
58 #ifdef SIGNATURE_0
59                 .signature = { SIGNATURE_0,SIGNATURE_1,SIGNATURE_2 },
60                 .fuse = { LFUSE_DEFAULT, HFUSE_DEFAULT },
61 #endif
62                 .init = init,
63                 .reset = reset,
64         },
65         AVR_EEPROM_DECLARE_8BIT(EE_RDY_vect),
66         // tiny13 has different names for these...
67         #define WDIF WDTIF
68         #define WDIE WDTIE
69         AVR_WATCHDOG_DECLARE(WDTCR, WDT_vect),
70         .extint = {
71                 AVR_EXTINT_TINY_DECLARE(0, 'B', 1, GIFR),
72         },
73         .portb = {
74                 .name = 'B',  .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
75                 .pcint = {
76                         .enable = AVR_IO_REGBIT(GIMSK, PCIE),
77                         .raised = AVR_IO_REGBIT(GIFR, PCIF),
78                         .vector = PCINT0_vect,
79                 },
80                 .r_pcint = PCMSK,
81         },
82         .timer0 = {
83                 .name = '0',
84                 .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
85                 .wgm_op = {
86                         [0] = AVR_TIMER_WGM_NORMAL8(),
87                         [2] = AVR_TIMER_WGM_CTC(),
88                         [3] = AVR_TIMER_WGM_FASTPWM8(),
89                         [7] = AVR_TIMER_WGM_OCPWM(),
90                 },
91                 .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) },
92                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
93
94                 .r_tcnt = TCNT0,
95
96                 .overflow = {
97                         .enable = AVR_IO_REGBIT(TIMSK0, TOIE0),
98                         .raised = AVR_IO_REGBIT(TIFR0, TOV0),
99                         .vector = TIM0_OVF_vect,
100                 },
101                 .comp = {
102                         [AVR_TIMER_COMPA] = {
103                                 .r_ocr = OCR0A,
104                                 .interrupt = {
105                                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A),
106                                         .raised = AVR_IO_REGBIT(TIFR0, OCF0A),
107                                         .vector = TIM0_COMPA_vect,
108                                 }
109                         },
110                         [AVR_TIMER_COMPB] = {
111                                 .r_ocr = OCR0B,
112                                 .interrupt = {
113                                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B),
114                                         .raised = AVR_IO_REGBIT(TIFR0, OCF0B),
115                                         .vector = TIM0_COMPB_vect,
116                                 }
117                         }
118                 }
119         }
120 };
121
122 static avr_t * make()
123 {
124         return avr_core_allocate(&mcu.core, sizeof(struct mcu_t));
125 }
126
127 avr_kind_t tiny13 = {
128         .names = { "attiny13", "attiny13a" },
129         .make = make
130 };
131
132 static void init(struct avr_t * avr)
133 {
134         struct mcu_t * mcu = (struct mcu_t*)avr;
135
136         avr_eeprom_init(avr, &mcu->eeprom);
137         avr_watchdog_init(avr, &mcu->watchdog);
138         avr_extint_init(avr, &mcu->extint);
139         avr_ioport_init(avr, &mcu->portb);
140         avr_timer_init(avr, &mcu->timer0);
141 }
142
143 static void reset(struct avr_t * avr)
144 {
145 //      struct mcu_t * mcu = (struct mcu_t*)avr;
146 }