libc3: Update
[simavr] / simavr / cores / sim_90usb162.c
1 /*
2         sim_90usb162.c
3
4         Copyright 2012 Torbjorn Tyridal <ttyridal@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 <stdio.h>
23 #include "sim_avr.h"
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"
30 #include "avr_uart.h"
31 #include "avr_adc.h"
32 #include "avr_timer.h"
33 #include "avr_spi.h"
34 #include "avr_usb.h"
35
36 void usb162_init(struct avr_t * avr);
37 void usb162_reset(struct avr_t * avr);
38
39 #define _AVR_IO_H_
40 #define __ASSEMBLER__
41 #include "avr/iousb162.h"
42
43 struct mcu_t {
44         avr_t                    core;
45         avr_eeprom_t    eeprom;
46         avr_flash_t     selfprog;
47         avr_watchdog_t  watchdog;
48         avr_extint_t    extint;
49         avr_ioport_t    portb, portc, portd;
50         avr_uart_t              uart1;
51         avr_timer_t             timer0,timer1;
52         avr_spi_t               spi;
53         avr_usb_t               usb;
54 } mcu_usb162 = {
55         .core = {
56                 .mmcu = "at90usb162",
57                 DEFAULT_CORE(4),
58
59                 .init = usb162_init,
60                 .reset = usb162_reset,
61         },
62         AVR_EEPROM_DECLARE(EE_READY_vect),
63         AVR_SELFPROG_DECLARE(SPMCSR, SPMEN, SPM_READY_vect),
64         AVR_WATCHDOG_DECLARE(WDTCSR, WDT_vect),
65         .extint = {
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),
74         },
75         .portb = {
76                 .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
77                 .pcint = {
78                         .enable = AVR_IO_REGBIT(PCICR, PCIE0),
79                         .raised = AVR_IO_REGBIT(PCIFR, PCIF0),
80                         .vector = PCINT0_vect,
81                 },
82                 .r_pcint = PCMSK0,
83         },
84         .portc = {
85                 .name = 'C', .r_port = PORTC, .r_ddr = DDRC, .r_pin = PINC,
86                 .pcint = {
87                         .enable = AVR_IO_REGBIT(PCICR, PCIE1),
88                         .raised = AVR_IO_REGBIT(PCIFR, PCIF1),
89                         .vector = PCINT1_vect,
90                 },
91                 .r_pcint = PCMSK1,
92         },
93         .portd = {
94                 .name = 'D', .r_port = PORTD, .r_ddr = DDRD, .r_pin = PIND,
95         },
96
97         .uart1 = {
98                 .disabled = AVR_IO_REGBIT(PRR1,PRUSART1),
99                 .name = '1',
100                 .r_udr = UDR1,
101
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
106
107                 .r_ucsra = UCSR1A,
108                 .r_ucsrb = UCSR1B,
109                 .r_ucsrc = UCSR1C,
110                 .r_ubrrl = UBRR1L,
111                 .r_ubrrh = UBRR1H,
112                 .rxc = {
113                         .enable = AVR_IO_REGBIT(UCSR1B, RXCIE1),
114                         .raised = AVR_IO_REGBIT(UCSR1A, RXC1),
115                         .vector = USART1_RX_vect,
116                 },
117                 .txc = {
118                         .enable = AVR_IO_REGBIT(UCSR1B, TXCIE1),
119                         .raised = AVR_IO_REGBIT(UCSR1A, TXC1),
120                         .vector = USART1_TX_vect,
121                 },
122                 .udrc = {
123                         .enable = AVR_IO_REGBIT(UCSR1B, UDRIE1),
124                         .raised = AVR_IO_REGBIT(UCSR1A, UDRE1),
125                         .vector = USART1_UDRE_vect,
126                 },
127         },
128         .timer0 = {
129                 .name = '0',
130                 .disabled = AVR_IO_REGBIT(PRR0,PRTIM0),
131                 .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) },
132                 .wgm_op = {
133                         [0] = AVR_TIMER_WGM_NORMAL8(),
134                         [2] = AVR_TIMER_WGM_CTC(),
135                         [3] = AVR_TIMER_WGM_FASTPWM8(),
136                         [7] = AVR_TIMER_WGM_OCPWM(),
137                 },
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 */ },
140
141                 .r_tcnt = TCNT0,
142
143                 .overflow = {
144                         .enable = AVR_IO_REGBIT(TIMSK0, TOIE0),
145                         .raised = AVR_IO_REGBIT(TIFR0, TOV0),
146                         .vector = TIMER0_OVF_vect,
147                 },
148                 .comp = {
149                         [AVR_TIMER_COMPA] = {
150                                 .r_ocr = OCR0A,
151                                 .com = AVR_IO_REGBITS(TCCR0A, COM0A0, 0x3),
152                                 .com_pin = AVR_IO_REGBIT(PORTD, 6),
153                                 .interrupt = {
154                                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0A),
155                                         .raised = AVR_IO_REGBIT(TIFR0, OCF0A),
156                                         .vector = TIMER0_COMPA_vect,
157                                 },
158                         },
159                         [AVR_TIMER_COMPB] = {
160                                 .r_ocr = OCR0B,
161                                 .com = AVR_IO_REGBITS(TCCR0A, COM0B0, 0x3),
162                                 .com_pin = AVR_IO_REGBIT(PORTD, 5),
163                                 .interrupt = {
164                                         .enable = AVR_IO_REGBIT(TIMSK0, OCIE0B),
165                                         .raised = AVR_IO_REGBIT(TIFR0, OCF0B),
166                                         .vector = TIMER0_COMPB_vect,
167                                 }
168                         }
169                 }
170         },
171         .timer1 = {
172                 .name = '1',
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) },
176                 .wgm_op = {
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(),
185                 },
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 */},
188
189                 .r_tcnt = TCNT1L,
190                 .r_tcnth = TCNT1H,
191                 .r_icr = ICR1L,
192                 .r_icrh = ICR1H,
193
194                 .ices = AVR_IO_REGBIT(TCCR1B, ICES1),
195                 .icp = AVR_IO_REGBIT(PORTB, 0),
196
197                 .overflow = {
198                         .enable = AVR_IO_REGBIT(TIMSK1, TOIE1),
199                         .raised = AVR_IO_REGBIT(TIFR1, TOV1),
200                         .vector = TIMER1_OVF_vect,
201                 },
202                 .icr = {
203                         .enable = AVR_IO_REGBIT(TIMSK1, ICIE1),
204                         .raised = AVR_IO_REGBIT(TIFR1, ICF1),
205                         .vector = TIMER1_CAPT_vect,
206                 },
207                 .comp = {
208                         [AVR_TIMER_COMPA] = {
209                                 .r_ocr = OCR1AL,
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),
213                                 .interrupt = {
214                                         .enable = AVR_IO_REGBIT(TIMSK1, OCIE1A),
215                                         .raised = AVR_IO_REGBIT(TIFR1, OCF1A),
216                                         .vector = TIMER1_COMPA_vect,
217                                 },
218                         },
219                         [AVR_TIMER_COMPB] = {
220                                 .r_ocr = OCR1BL,
221                                 .r_ocrh = OCR1BH,
222                                 .com = AVR_IO_REGBITS(TCCR1A, COM1B0, 0x3),
223                                 .com_pin = AVR_IO_REGBIT(PORTB, 2),
224                                 .interrupt = {
225                                         .enable = AVR_IO_REGBIT(TIMSK1, OCIE1B),
226                                         .raised = AVR_IO_REGBIT(TIFR1, OCF1B),
227                                         .vector = TIMER1_COMPB_vect,
228                                 },
229                         },
230                 },
231         },
232         .spi = {
233
234                 .r_spdr = SPDR,
235                 .r_spcr = SPCR,
236                 .r_spsr = SPSR,
237
238                 .spe = AVR_IO_REGBIT(SPCR, SPE),
239                 .mstr = AVR_IO_REGBIT(SPCR, MSTR),
240
241                 .spr = { AVR_IO_REGBIT(SPCR, SPR0), AVR_IO_REGBIT(SPCR, SPR1), AVR_IO_REGBIT(SPSR, SPI2X) },
242                 .spi = {
243                         .enable = AVR_IO_REGBIT(SPCR, SPIE),
244                         .raised = AVR_IO_REGBIT(SPSR, SPIF),
245                         .vector = SPI_STC_vect,
246                 },
247         },
248         .usb = {
249                 .name='1',
250                 .disabled=AVR_IO_REGBIT(PRR1, PRUSB),// bit in the PRR
251
252                 .usbrf=AVR_IO_REGBIT(MCUSR,USBRF),      // bit in the MCUSR
253
254                 .r_usbcon = USBCON,
255                 .r_pllcsr=PLLCSR,
256
257                 .usb_com_vect=USB_COM_vect,
258                 .usb_gen_vect=USB_GEN_vect,
259         },
260 };
261
262 static avr_t * make()
263 {
264         return &mcu_usb162.core;
265 }
266
267 avr_kind_t usb162 = {
268         .names = { "at90usb162" },
269         .make = make
270 };
271
272 void usb162_init(struct avr_t * avr)
273 {
274         struct mcu_t * mcu = (struct mcu_t*)avr;
275
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);
288 }
289
290 void usb162_reset(struct avr_t * avr)
291 {
292 //      struct mcu_t * mcu = (struct mcu_t*)avr;
293 }