4 Copyright 2012 Torbjorn Tyridal <ttyridal@gmail.com>
6 This file is part of simavr.
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.
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.
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/>.
24 #include "sim_core_declare.h"
25 #include "avr_eeprom.h"
26 #include "avr_flash.h"
27 #include "avr_watchdog.h"
28 #include "avr_extint.h"
29 #include "avr_ioport.h"
32 #include "avr_timer.h"
36 void usb162_init(struct avr_t * avr);
37 void usb162_reset(struct avr_t * avr);
41 #include "avr/iousb162.h"
47 avr_watchdog_t watchdog;
49 avr_ioport_t portb, portc, portd;
51 avr_timer_t timer0,timer1;
60 .reset = usb162_reset,
62 AVR_EEPROM_DECLARE(EE_READY_vect),
63 AVR_SELFPROG_DECLARE(SPMCSR, SPMEN, SPM_READY_vect),
64 AVR_WATCHDOG_DECLARE(WDTCSR, WDT_vect),
66 AVR_EXTINT_DECLARE(0, 'D', PD0),
67 AVR_EXTINT_DECLARE(1, 'D', PD1),
68 AVR_EXTINT_DECLARE(2, 'D', PD2),
69 AVR_EXTINT_DECLARE(3, 'D', PD3),
70 AVR_EXTINT_DECLARE(4, 'C', PC7),
71 AVR_EXTINT_DECLARE(5, 'D', PD4),
72 AVR_EXTINT_DECLARE(6, 'D', PD6),
73 AVR_EXTINT_DECLARE(7, 'D', PD7),
76 .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
78 .enable = AVR_IO_REGBIT(PCICR, PCIE0),
79 .raised = AVR_IO_REGBIT(PCIFR, PCIF0),
80 .vector = PCINT0_vect,
85 .name = 'C', .r_port = PORTC, .r_ddr = DDRC, .r_pin = PINC,
87 .enable = AVR_IO_REGBIT(PCICR, PCIE1),
88 .raised = AVR_IO_REGBIT(PCIFR, PCIF1),
89 .vector = PCINT1_vect,
94 .name = 'D', .r_port = PORTD, .r_ddr = DDRD, .r_pin = PIND,
98 .disabled = AVR_IO_REGBIT(PRR1,PRUSART1),
102 .txen = AVR_IO_REGBIT(UCSR1B, TXEN1),
103 .rxen = AVR_IO_REGBIT(UCSR1B, RXEN1),
104 .ucsz = AVR_IO_REGBITS(UCSR1C, UCSZ10, 0x3), // 2 bits
105 .ucsz2 = AVR_IO_REGBIT(UCSR1B, UCSZ12), // 1 bits
113 .enable = AVR_IO_REGBIT(UCSR1B, RXCIE1),
114 .raised = AVR_IO_REGBIT(UCSR1A, RXC1),
115 .vector = USART1_RX_vect,
118 .enable = AVR_IO_REGBIT(UCSR1B, TXCIE1),
119 .raised = AVR_IO_REGBIT(UCSR1A, TXC1),
120 .vector = USART1_TX_vect,
123 .enable = AVR_IO_REGBIT(UCSR1B, UDRIE1),
124 .raised = AVR_IO_REGBIT(UCSR1A, UDRE1),
125 .vector = USART1_UDRE_vect,
130 .disabled = AVR_IO_REGBIT(PRR0,PRTIM0),
131 .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
133 [0] = AVR_TIMER_WGM_NORMAL8(),
134 [2] = AVR_TIMER_WGM_CTC(),
135 [3] = AVR_TIMER_WGM_FASTPWM8(),
136 [7] = AVR_TIMER_WGM_OCPWM(),
138 .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) },
139 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ },
144 .enable = AVR_IO_REGBIT(TIMSK0, TOIE0),
145 .raised = AVR_IO_REGBIT(TIFR0, TOV0),
146 .vector = TIMER0_OVF_vect,
149 [AVR_TIMER_COMPA] = {
151 .com = AVR_IO_REGBITS(TCCR0A, COM0A0, 0x3),
152 .com_pin = AVR_IO_REGBIT(PORTD, 6),
154 .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A),
155 .raised = AVR_IO_REGBIT(TIFR0, OCF0A),
156 .vector = TIMER0_COMPA_vect,
159 [AVR_TIMER_COMPB] = {
161 .com = AVR_IO_REGBITS(TCCR0A, COM0B0, 0x3),
162 .com_pin = AVR_IO_REGBIT(PORTD, 5),
164 .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B),
165 .raised = AVR_IO_REGBIT(TIFR0, OCF0B),
166 .vector = TIMER0_COMPB_vect,
173 .disabled = AVR_IO_REGBIT(PRR0,PRTIM1),
174 .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11),
175 AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) },
177 [0] = AVR_TIMER_WGM_NORMAL16(),
178 [4] = AVR_TIMER_WGM_CTC(),
179 [5] = AVR_TIMER_WGM_FASTPWM8(),
180 [6] = AVR_TIMER_WGM_FASTPWM9(),
181 [7] = AVR_TIMER_WGM_FASTPWM10(),
182 [12] = AVR_TIMER_WGM_ICCTC(),
183 [14] = AVR_TIMER_WGM_ICPWM(),
184 [15] = AVR_TIMER_WGM_OCPWM(),
186 .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) },
187 .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */ /* External clock T1 is not handled */},
194 .ices = AVR_IO_REGBIT(TCCR1B, ICES1),
195 .icp = AVR_IO_REGBIT(PORTB, 0),
198 .enable = AVR_IO_REGBIT(TIMSK1, TOIE1),
199 .raised = AVR_IO_REGBIT(TIFR1, TOV1),
200 .vector = TIMER1_OVF_vect,
203 .enable = AVR_IO_REGBIT(TIMSK1, ICIE1),
204 .raised = AVR_IO_REGBIT(TIFR1, ICF1),
205 .vector = TIMER1_CAPT_vect,
208 [AVR_TIMER_COMPA] = {
210 .r_ocrh = OCR1AH, // 16 bits timers have two bytes of it
211 .com = AVR_IO_REGBITS(TCCR1A, COM1A0, 0x3),
212 .com_pin = AVR_IO_REGBIT(PORTB, 1),
214 .enable = AVR_IO_REGBIT(TIMSK1, OCIE1A),
215 .raised = AVR_IO_REGBIT(TIFR1, OCF1A),
216 .vector = TIMER1_COMPA_vect,
219 [AVR_TIMER_COMPB] = {
222 .com = AVR_IO_REGBITS(TCCR1A, COM1B0, 0x3),
223 .com_pin = AVR_IO_REGBIT(PORTB, 2),
225 .enable = AVR_IO_REGBIT(TIMSK1, OCIE1B),
226 .raised = AVR_IO_REGBIT(TIFR1, OCF1B),
227 .vector = TIMER1_COMPB_vect,
238 .spe = AVR_IO_REGBIT(SPCR, SPE),
239 .mstr = AVR_IO_REGBIT(SPCR, MSTR),
241 .spr = { AVR_IO_REGBIT(SPCR, SPR0), AVR_IO_REGBIT(SPCR, SPR1), AVR_IO_REGBIT(SPSR, SPI2X) },
243 .enable = AVR_IO_REGBIT(SPCR, SPIE),
244 .raised = AVR_IO_REGBIT(SPSR, SPIF),
245 .vector = SPI_STC_vect,
250 .disabled=AVR_IO_REGBIT(PRR1, PRUSB),// bit in the PRR
252 .usbrf=AVR_IO_REGBIT(MCUSR,USBRF), // bit in the MCUSR
257 .usb_com_vect=USB_COM_vect,
258 .usb_gen_vect=USB_GEN_vect,
262 static avr_t * make()
264 return avr_core_allocate(&mcu_usb162.core, sizeof(struct mcu_t));
267 avr_kind_t usb162 = {
268 .names = { "at90usb162" },
272 void usb162_init(struct avr_t * avr)
274 struct mcu_t * mcu = (struct mcu_t*)avr;
276 avr_eeprom_init(avr, &mcu->eeprom);
277 avr_flash_init(avr, &mcu->selfprog);
278 avr_extint_init(avr, &mcu->extint);
279 avr_watchdog_init(avr, &mcu->watchdog);
280 avr_ioport_init(avr, &mcu->portb);
281 avr_ioport_init(avr, &mcu->portc);
282 avr_ioport_init(avr, &mcu->portd);
283 avr_uart_init(avr, &mcu->uart1);
284 avr_timer_init(avr, &mcu->timer0);
285 avr_timer_init(avr, &mcu->timer1);
286 avr_spi_init(avr, &mcu->spi);
287 avr_usb_init(avr, &mcu->usb);
290 void usb162_reset(struct avr_t * avr)
292 // struct mcu_t * mcu = (struct mcu_t*)avr;