cores: Added ATTiny2313
[simavr] / simavr / cores / sim_tiny2313.c
1 /*
2         sim_tiny2313.c
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include </usr/include/stdio.h>
23 #include "sim_core_declare.h"
24 #include "avr_eeprom.h"
25 #include "avr_extint.h"
26 #include "avr_ioport.h"
27 #include "avr_uart.h"
28 #include "avr_timer.h"
29
30 static void init(struct avr_t * avr);
31 static void reset(struct avr_t * avr);
32
33 #define _AVR_IO_H_
34 #define __ASSEMBLER__
35 #include "avr/iotn2313.h"
36
37 /*
38  * This is a template for all of the tinyx5 devices, hopefully
39  */
40 static struct mcu_t {
41         avr_t core;
42         avr_eeprom_t    eeprom;
43         avr_extint_t    extint;
44         avr_ioport_t    portb, portd;
45         avr_uart_t              uart;
46         avr_timer_t             timer0,timer1;
47 } mcu = {
48         .core = {
49                 .mmcu = "attiny2313",
50                 DEFAULT_CORE(2),
51
52                 .init = init,
53                 .reset = reset,
54         },
55         AVR_EEPROM_DECLARE_8BIT(EEPROM_READY_vect),
56         .extint = {
57                 AVR_EXTINT_TINY_DECLARE(0, 'D', 2, EIFR),
58                 AVR_EXTINT_TINY_DECLARE(1, 'D', 3, EIFR),
59         },
60         .portb = {
61                 .name = 'B',  .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
62                 .pcint = {
63                         .enable = AVR_IO_REGBIT(GIMSK, PCIE),
64                         .raised = AVR_IO_REGBIT(EIFR, PCIF),
65                         .vector = PCINT_vect,
66                 },
67                 .r_pcint = PCMSK,
68         },
69         .portd = {      // port D has no PCInts..
70                 .name = 'D', .r_port = PORTD, .r_ddr = DDRD, .r_pin = PIND,
71         },
72         .uart = {
73                 // no PRR register on the 2313
74                 //.disabled = AVR_IO_REGBIT(PRR,PRUSART0),
75                 .name = '0',
76                 .r_udr = UDR,
77
78                 .txen = AVR_IO_REGBIT(UCSRB, TXEN),
79                 .rxen = AVR_IO_REGBIT(UCSRB, RXEN),
80
81                 .r_ucsra = UCSRA,
82                 .r_ucsrb = UCSRB,
83                 .r_ucsrc = UCSRC,
84                 .r_ubrrl = UBRRL,
85                 .r_ubrrh = UBRRH,
86                 .rxc = {
87                         .enable = AVR_IO_REGBIT(UCSRB, RXCIE),
88                         .raised = AVR_IO_REGBIT(UCSRA, RXC),
89                         .vector = USART_RX_vect,
90                 },
91                 .txc = {
92                         .enable = AVR_IO_REGBIT(UCSRB, TXCIE),
93                         .raised = AVR_IO_REGBIT(UCSRA, TXC),
94                         .vector = USART_TX_vect,
95                 },
96                 .udrc = {
97                         .enable = AVR_IO_REGBIT(UCSRB, UDRIE),
98                         .raised = AVR_IO_REGBIT(UCSRA, UDRE),
99                         .vector = USART_UDRE_vect,
100                 },
101         },
102         .timer0 = {
103                 .name = '0',
104                 .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
105                 .wgm_op = {
106                         [0] = AVR_TIMER_WGM_NORMAL8(),
107                         [2] = AVR_TIMER_WGM_CTC(),
108                         [3] = AVR_TIMER_WGM_FASTPWM(),
109                         [7] = AVR_TIMER_WGM_FASTPWM(),
110                 },
111                 .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) },
112                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
113
114                 .r_ocra = OCR0A,
115                 .r_ocrb = OCR0B,
116                 .r_tcnt = TCNT0,
117
118                 .overflow = {
119                         .enable = AVR_IO_REGBIT(TIMSK, TOIE0),
120                         .raised = AVR_IO_REGBIT(TIFR, TOV0),
121                         .vector = TIMER0_OVF_vect,
122                 },
123                 .compa = {
124                         .enable = AVR_IO_REGBIT(TIMSK, OCIE0A),
125                         .raised = AVR_IO_REGBIT(TIFR, OCF0A),
126                         .vector = TIMER0_COMPA_vect,
127                 },
128                 .compb = {
129                         .enable = AVR_IO_REGBIT(TIMSK, OCIE0B),
130                         .raised = AVR_IO_REGBIT(TIFR, OCF0B),
131                         .vector = TIMER0_COMPB_vect,
132                 },
133         },
134         .timer1 = {
135                 .name = '1',
136         //      .disabled = AVR_IO_REGBIT(PRR,PRTIM1),
137                 .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11),
138                                         AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) },
139                 .wgm_op = {
140                         [0] = AVR_TIMER_WGM_NORMAL16(),
141                         [4] = AVR_TIMER_WGM_CTC(),
142                         [5] = AVR_TIMER_WGM_FASTPWM(),
143                         [6] = AVR_TIMER_WGM_FASTPWM(),
144                         [7] = AVR_TIMER_WGM_FASTPWM(),
145                 },
146                 .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) },
147                 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */  /* External clock T1 is not handled */},
148
149                 .r_ocra = OCR1AL,
150                 .r_ocrb = OCR1BL,
151                 .r_tcnt = TCNT1L,
152
153                 .r_ocrah = OCR1AH,      // 16 bits timers have two bytes of it
154                 .r_ocrbh = OCR1BH,
155                 .r_tcnth = TCNT1H,
156
157                 .overflow = {
158                         .enable = AVR_IO_REGBIT(TIMSK, TOIE1),
159                         .raised = AVR_IO_REGBIT(TIFR, TOV1),
160                         .vector = TIMER1_OVF_vect,
161                 },
162                 .compa = {
163                         .enable = AVR_IO_REGBIT(TIMSK, OCIE1A),
164                         .raised = AVR_IO_REGBIT(TIFR, OCF1A),
165                         .vector = TIMER1_COMPA_vect,
166                 },
167                 .compb = {
168                         .enable = AVR_IO_REGBIT(TIMSK, OCIE1B),
169                         .raised = AVR_IO_REGBIT(TIFR, OCF1B),
170                         .vector = TIMER1_COMPB_vect,
171                 },
172                 .icr = {
173                         .enable = AVR_IO_REGBIT(TIMSK, ICIE1),
174                         .raised = AVR_IO_REGBIT(TIFR, ICF1),
175                         .vector = TIMER1_CAPT_vect,
176                 },
177         },
178 };
179
180 static avr_t * make()
181 {
182         return &mcu.core;
183 }
184
185 avr_kind_t tiny2313 = {
186         .names = { "attiny2313", "attiny2313v" },
187         .make = make
188 };
189
190 static void init(struct avr_t * avr)
191 {
192         struct mcu_t * mcu = (struct mcu_t*)avr;
193
194         printf("%s init\n", avr->mmcu);
195
196         avr_eeprom_init(avr, &mcu->eeprom);
197         avr_ioport_init(avr, &mcu->portb);
198         avr_ioport_init(avr, &mcu->portd);
199         avr_uart_init(avr, &mcu->uart);
200         avr_timer_init(avr, &mcu->timer0);
201         avr_timer_init(avr, &mcu->timer1);
202 }
203
204 static void reset(struct avr_t * avr)
205 {
206 //      struct mcu_t * mcu = (struct mcu_t*)avr;
207 }
208