|
| 1 | +/* |
| 2 | + sim_tiny2313a.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 "sim_core_declare.h" |
| 23 | +#include "avr_eeprom.h" |
| 24 | +#include "avr_watchdog.h" |
| 25 | +#include "avr_extint.h" |
| 26 | +#include "avr_ioport.h" |
| 27 | +#include "avr_uart.h" |
| 28 | +#include "avr_timer.h" |
| 29 | +#include "avr_acomp.h" |
| 30 | + |
| 31 | +static void init(struct avr_t * avr); |
| 32 | +static void reset(struct avr_t * avr); |
| 33 | + |
| 34 | +#define _AVR_IO_H_ |
| 35 | +#define __ASSEMBLER__ |
| 36 | +#include "avr/iotn2313a.h" |
| 37 | + |
| 38 | +/* |
| 39 | + * This is a template for all of the tinyx5 devices, hopefully |
| 40 | + */ |
| 41 | +static const struct mcu_t { |
| 42 | + avr_t core; |
| 43 | + avr_eeprom_t eeprom; |
| 44 | + avr_watchdog_t watchdog; |
| 45 | + avr_extint_t extint; |
| 46 | + avr_ioport_t porta, portb, portd; |
| 47 | + avr_uart_t uart; |
| 48 | + avr_timer_t timer0,timer1; |
| 49 | + avr_acomp_t acomp; |
| 50 | +} mcu = { |
| 51 | + .core = { |
| 52 | + .mmcu = "attiny2313a", |
| 53 | + DEFAULT_CORE(2), |
| 54 | + |
| 55 | + .init = init, |
| 56 | + .reset = reset, |
| 57 | + }, |
| 58 | + AVR_EEPROM_DECLARE_8BIT(EEPROM_Ready_vect), |
| 59 | + AVR_WATCHDOG_DECLARE(WDTCR, WDT_OVERFLOW_vect), |
| 60 | + .extint = { |
| 61 | + AVR_EXTINT_TINY_DECLARE(0, 'D', 2, GIFR), |
| 62 | + AVR_EXTINT_TINY_DECLARE(1, 'D', 3, GIFR), |
| 63 | + }, |
| 64 | + .porta = { |
| 65 | + .name = 'A', .r_port = PORTA, .r_ddr = DDRA, .r_pin = PINA, |
| 66 | + .pcint = { |
| 67 | + .enable = AVR_IO_REGBIT(GIMSK, PCIE1), |
| 68 | + .raised = AVR_IO_REGBIT(GIFR, PCIF1), |
| 69 | + .vector = PCINT1_vect, |
| 70 | + }, |
| 71 | + .r_pcint = PCMSK1, |
| 72 | + }, |
| 73 | + .portb = { |
| 74 | + .name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB, |
| 75 | + .pcint = { |
| 76 | + .enable = AVR_IO_REGBIT(GIMSK, PCIE0), |
| 77 | + .raised = AVR_IO_REGBIT(GIFR, PCIF0), |
| 78 | + .vector = PCINT0_vect, |
| 79 | + }, |
| 80 | + .r_pcint = PCMSK0, |
| 81 | + }, |
| 82 | + .portd = { |
| 83 | + .name = 'D', .r_port = PORTD, .r_ddr = DDRD, .r_pin = PIND, |
| 84 | + .pcint = { |
| 85 | + .enable = AVR_IO_REGBIT(GIMSK, PCIE2), |
| 86 | + .raised = AVR_IO_REGBIT(GIFR, PCIF2), |
| 87 | + .vector = PCINT2_vect, |
| 88 | + }, |
| 89 | + .r_pcint = PCMSK2, |
| 90 | + }, |
| 91 | + |
| 92 | + //PRUSART, upe=UPE, no reg/bit name index, no 'C' in RX/TX vector names |
| 93 | + AVR_UART_DECLARE(PRR, PRUSART, UPE, , ), |
| 94 | + |
| 95 | + .timer0 = { |
| 96 | + .name = '0', |
| 97 | + .disabled = AVR_IO_REGBIT(PRR,PRTIM0), |
| 98 | + .wgm = { AVR_IO_REGBIT(TCCR0A, WGM00), AVR_IO_REGBIT(TCCR0A, WGM01), AVR_IO_REGBIT(TCCR0B, WGM02) }, |
| 99 | + .wgm_op = { |
| 100 | + [0] = AVR_TIMER_WGM_NORMAL8(), |
| 101 | + [2] = AVR_TIMER_WGM_CTC(), |
| 102 | + [3] = AVR_TIMER_WGM_FASTPWM8(), |
| 103 | + [7] = AVR_TIMER_WGM_OCPWM(), |
| 104 | + }, |
| 105 | + .cs = { AVR_IO_REGBIT(TCCR0B, CS00), AVR_IO_REGBIT(TCCR0B, CS01), AVR_IO_REGBIT(TCCR0B, CS02) }, |
| 106 | + .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */, AVR_TIMER_EXTCLK_CHOOSE, AVR_TIMER_EXTCLK_CHOOSE }, |
| 107 | + .ext_clock_pin = AVR_IO_REGBIT(PORTD, 4), /* External clock pin */ |
| 108 | + |
| 109 | + .r_tcnt = TCNT0, |
| 110 | + |
| 111 | + .overflow = { |
| 112 | + .enable = AVR_IO_REGBIT(TIMSK, TOIE0), |
| 113 | + .raised = AVR_IO_REGBIT(TIFR, TOV0), |
| 114 | + .vector = TIMER0_OVF_vect, |
| 115 | + }, |
| 116 | + .comp = { |
| 117 | + [AVR_TIMER_COMPA] = { |
| 118 | + .r_ocr = OCR0A, |
| 119 | + .com = AVR_IO_REGBITS(TCCR0A, COM0A0, 0x3), |
| 120 | + .com_pin = AVR_IO_REGBIT(PORTB, 2), |
| 121 | + .interrupt = { |
| 122 | + .enable = AVR_IO_REGBIT(TIMSK, OCIE0A), |
| 123 | + .raised = AVR_IO_REGBIT(TIFR, OCF0A), |
| 124 | + .vector = TIMER0_COMPA_vect, |
| 125 | + }, |
| 126 | + }, |
| 127 | + [AVR_TIMER_COMPB] = { |
| 128 | + .r_ocr = OCR0B, |
| 129 | + .com = AVR_IO_REGBITS(TCCR0A, COM0B0, 0x3), |
| 130 | + .com_pin = AVR_IO_REGBIT(PORTD, 5), |
| 131 | + .interrupt = { |
| 132 | + .enable = AVR_IO_REGBIT(TIMSK, OCIE0B), |
| 133 | + .raised = AVR_IO_REGBIT(TIFR, OCF0B), |
| 134 | + .vector = TIMER0_COMPB_vect, |
| 135 | + } |
| 136 | + } |
| 137 | + }, |
| 138 | + }, |
| 139 | + .timer1 = { |
| 140 | + .name = '1', |
| 141 | + .disabled = AVR_IO_REGBIT(PRR,PRTIM1), |
| 142 | + .wgm = { AVR_IO_REGBIT(TCCR1A, WGM10), AVR_IO_REGBIT(TCCR1A, WGM11), |
| 143 | + AVR_IO_REGBIT(TCCR1B, WGM12), AVR_IO_REGBIT(TCCR1B, WGM13) }, |
| 144 | + .wgm_op = { |
| 145 | + [0] = AVR_TIMER_WGM_NORMAL16(), |
| 146 | + [4] = AVR_TIMER_WGM_CTC(), |
| 147 | + [5] = AVR_TIMER_WGM_FASTPWM8(), |
| 148 | + [6] = AVR_TIMER_WGM_FASTPWM9(), |
| 149 | + [7] = AVR_TIMER_WGM_FASTPWM10(), |
| 150 | + [12] = AVR_TIMER_WGM_ICCTC(), |
| 151 | + [14] = AVR_TIMER_WGM_ICPWM(), |
| 152 | + [15] = AVR_TIMER_WGM_OCPWM(), |
| 153 | + }, |
| 154 | + .cs = { AVR_IO_REGBIT(TCCR1B, CS10), AVR_IO_REGBIT(TCCR1B, CS11), AVR_IO_REGBIT(TCCR1B, CS12) }, |
| 155 | + .cs_div = { 0, 0, 3 /* 8 */, 6 /* 64 */, 8 /* 256 */, 10 /* 1024 */, AVR_TIMER_EXTCLK_CHOOSE, AVR_TIMER_EXTCLK_CHOOSE }, |
| 156 | + .ext_clock_pin = AVR_IO_REGBIT(PORTD, 5), /* External clock pin */ |
| 157 | + |
| 158 | + .r_tcnt = TCNT1L, |
| 159 | + .r_icr = ICR1L, |
| 160 | + .r_icrh = ICR1H, |
| 161 | + .r_tcnth = TCNT1H, |
| 162 | + |
| 163 | + .ices = AVR_IO_REGBIT(TCCR1B, ICES1), |
| 164 | + .icp = AVR_IO_REGBIT(PORTD, 6), |
| 165 | + |
| 166 | + .overflow = { |
| 167 | + .enable = AVR_IO_REGBIT(TIMSK, TOIE1), |
| 168 | + .raised = AVR_IO_REGBIT(TIFR, TOV1), |
| 169 | + .vector = TIMER1_OVF_vect, |
| 170 | + }, |
| 171 | + .icr = { |
| 172 | + .enable = AVR_IO_REGBIT(TIMSK, ICIE1), |
| 173 | + .raised = AVR_IO_REGBIT(TIFR, ICF1), |
| 174 | + .vector = TIMER1_CAPT_vect, |
| 175 | + }, |
| 176 | + .comp = { |
| 177 | + [AVR_TIMER_COMPA] = { |
| 178 | + .r_ocr = OCR1AL, |
| 179 | + .r_ocrh = OCR1AH, // 16 bits timers have two bytes of it |
| 180 | + .com = AVR_IO_REGBITS(TCCR1A, COM1A0, 0x3), |
| 181 | + .com_pin = AVR_IO_REGBIT(PORTB, 3), |
| 182 | + .interrupt = { |
| 183 | + .enable = AVR_IO_REGBIT(TIMSK, OCIE1A), |
| 184 | + .raised = AVR_IO_REGBIT(TIFR, OCF1A), |
| 185 | + .vector = TIMER1_COMPA_vect, |
| 186 | + }, |
| 187 | + }, |
| 188 | + [AVR_TIMER_COMPB] = { |
| 189 | + .r_ocr = OCR1BL, |
| 190 | + .r_ocrh = OCR1BH, |
| 191 | + .com = AVR_IO_REGBITS(TCCR1A, COM1A0, 0x3), |
| 192 | + .com_pin = AVR_IO_REGBIT(PORTB, 4), |
| 193 | + .interrupt = { |
| 194 | + .enable = AVR_IO_REGBIT(TIMSK, OCIE1B), |
| 195 | + .raised = AVR_IO_REGBIT(TIFR, OCF1B), |
| 196 | + .vector = TIMER1_COMPB_vect, |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + }, |
| 201 | + |
| 202 | + .acomp = { |
| 203 | + .r_acsr = ACSR, |
| 204 | + .acis = { AVR_IO_REGBIT(ACSR, ACIS0), AVR_IO_REGBIT(ACSR, ACIS1) }, |
| 205 | + .acic = AVR_IO_REGBIT(ACSR, ACIC), |
| 206 | + .aco = AVR_IO_REGBIT(ACSR, ACO), |
| 207 | + .acbg = AVR_IO_REGBIT(ACSR, ACBG), |
| 208 | + .disabled = AVR_IO_REGBIT(ACSR, ACD), |
| 209 | + |
| 210 | + .timer_name = '1', |
| 211 | + |
| 212 | + .ac = { |
| 213 | + .enable = AVR_IO_REGBIT(ACSR, ACIE), |
| 214 | + .raised = AVR_IO_REGBIT(ACSR, ACI), |
| 215 | + .vector = ANA_COMP_vect, |
| 216 | + } |
| 217 | + }, |
| 218 | + |
| 219 | +}; |
| 220 | + |
| 221 | +static avr_t * make() |
| 222 | +{ |
| 223 | + return avr_core_allocate(&mcu.core, sizeof(struct mcu_t)); |
| 224 | +} |
| 225 | + |
| 226 | +avr_kind_t tiny2313a = { |
| 227 | + .names = { "attiny2313a" }, |
| 228 | + .make = make |
| 229 | +}; |
| 230 | + |
| 231 | +static void init(struct avr_t * avr) |
| 232 | +{ |
| 233 | + struct mcu_t * mcu = (struct mcu_t*)avr; |
| 234 | + |
| 235 | + avr_eeprom_init(avr, &mcu->eeprom); |
| 236 | + avr_watchdog_init(avr, &mcu->watchdog); |
| 237 | + avr_extint_init(avr, &mcu->extint); |
| 238 | + avr_ioport_init(avr, &mcu->porta); |
| 239 | + avr_ioport_init(avr, &mcu->portb); |
| 240 | + avr_ioport_init(avr, &mcu->portd); |
| 241 | + avr_uart_init(avr, &mcu->uart); |
| 242 | + avr_timer_init(avr, &mcu->timer0); |
| 243 | + avr_timer_init(avr, &mcu->timer1); |
| 244 | + avr_acomp_init(avr, &mcu->acomp); |
| 245 | +} |
| 246 | + |
| 247 | +static void reset(struct avr_t * avr) |
| 248 | +{ |
| 249 | +// struct mcu_t * mcu = (struct mcu_t*)avr; |
| 250 | +} |
| 251 | + |
0 commit comments