Small module to implement the INT0 .. INT3 interrupts.
These hook up in ioport pin IRQs to trigger the
vectors.
Added vectors for the existing cores.
Signed-off-by: Michel Pollet <buserror@gmail.com>
#include "sim_avr.h"
#include "sim_core_declare.h"
#include "avr_eeprom.h"
+#include "avr_extint.h"
#include "avr_ioport.h"
#include "avr_uart.h"
#include "avr_timer8.h"
static struct mcu_t {
avr_t core;
avr_eeprom_t eeprom;
+ avr_extint_t extint;
avr_ioport_t porta, portb, portc, portd;
avr_uart_t uart0,uart1;
avr_timer8_t timer0,timer2;
.reset = reset,
},
AVR_EEPROM_DECLARE(EE_READY_vect),
+ .extint = {
+ AVR_EXTINT_DECLARE(0, 'D', PD2),
+ AVR_EXTINT_DECLARE(1, 'D', PD3),
+ AVR_EXTINT_DECLARE(2, 'B', PB3),
+ },
.porta = {
.name = 'A', .r_port = PORTA, .r_ddr = DDRA, .r_pin = PINA,
.pcint = {
printf("%s init\n", avr->mmcu);
avr_eeprom_init(avr, &mcu->eeprom);
+ avr_extint_init(avr, &mcu->extint);
avr_ioport_init(avr, &mcu->porta);
avr_ioport_init(avr, &mcu->portb);
avr_ioport_init(avr, &mcu->portc);
printf("%s init\n", avr->mmcu);
avr_eeprom_init(avr, &mcu->eeprom);
+ avr_extint_init(avr, &mcu->extint);
avr_ioport_init(avr, &mcu->portb);
avr_ioport_init(avr, &mcu->portc);
avr_ioport_init(avr, &mcu->portd);
#include "sim_core_declare.h"
#include "avr_eeprom.h"
+#include "avr_extint.h"
#include "avr_ioport.h"
#include "avr_uart.h"
#include "avr_timer8.h"
struct mcu_t {
avr_t core;
avr_eeprom_t eeprom;
+ avr_extint_t extint;
avr_ioport_t portb,portc,portd;
avr_uart_t uart;
avr_timer8_t timer0,timer2;
.reset = mx8_reset,
},
AVR_EEPROM_DECLARE(EE_READY_vect),
+ .extint = {
+ AVR_EXTINT_DECLARE(0, 'D', PD2),
+ AVR_EXTINT_DECLARE(1, 'D', PD3),
+ },
.portb = {
.name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
.pcint = {
printf("%s init\n", avr->mmcu);
avr_eeprom_init(avr, &mcu->eeprom);
+ avr_extint_init(avr, &mcu->extint);
avr_ioport_init(avr, &mcu->portb);
avr_timer8_init(avr, &mcu->timer0);
avr_timer8_init(avr, &mcu->timer1);
#include "sim_core_declare.h"
#include "avr_eeprom.h"
+#include "avr_extint.h"
#include "avr_ioport.h"
#include "avr_timer8.h"
struct mcu_t {
avr_t core;
avr_eeprom_t eeprom;
+ avr_extint_t extint;
avr_ioport_t portb;
avr_timer8_t timer0, timer1;
};
.reset = tx5_reset,
},
AVR_EEPROM_DECLARE(EE_RDY_vect),
+ .extint = {
+ .eint[0] = {
+ .port_ioctl = AVR_IOCTL_IOPORT_GETIRQ('B'),
+ .port_pin = PB2,
+ .isc = { AVR_IO_REGBIT(MCUCR, ISC00), AVR_IO_REGBIT(MCUCR, ISC01) },
+ .vector = {
+ .enable = AVR_IO_REGBIT(GIMSK, INT0),
+ .raised = AVR_IO_REGBIT(GIFR, INTF0),
+ .vector = INT0_vect,
+ },
+ }
+ },
.portb = {
.name = 'B', .r_port = PORTB, .r_ddr = DDRB, .r_pin = PINB,
.pcint = {
--- /dev/null
+/*
+ avr_extint.c
+
+ Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
+
+ This file is part of simavr.
+
+ simavr is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ simavr is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with simavr. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "avr_extint.h"
+
+
+static void avr_extint_irq_notify(struct avr_irq_t * irq, uint32_t value, void * param)
+{
+ avr_extint_t * p = (avr_extint_t *)param;
+ avr_t * avr = p->io.avr;
+
+ uint8_t mode = avr_regbit_get_array(avr, p->eint[irq->irq].isc, 2);
+ int up = !irq->value && value;
+ int down = irq->value && !value;
+ switch (mode) {
+ case 0:
+ // unsuported
+ break;
+ case 1:
+ if (up || down)
+ avr_raise_interrupt(avr, &p->eint[irq->irq].vector);
+ break;
+ case 2:
+ if (down)
+ avr_raise_interrupt(avr, &p->eint[irq->irq].vector);
+ break;
+ case 3:
+ if (up)
+ avr_raise_interrupt(avr, &p->eint[irq->irq].vector);
+ break;
+ }
+}
+
+static void avr_extint_reset(avr_io_t * port)
+{
+ avr_extint_t * p = (avr_extint_t *)port;
+
+ for (int i = 0; i < EXTINT_COUNT; i++) {
+ avr_irq_register_notify(p->io.irq + i, avr_extint_irq_notify, p);
+
+ if (p->eint[i].port_ioctl) {
+ avr_irq_t * irq = avr_io_getirq(p->io.avr,
+ p->eint[i].port_ioctl, p->eint[i].port_pin);
+
+ avr_connect_irq(irq, p->io.irq + i);
+ }
+ }
+}
+
+static avr_io_t _io = {
+ .kind = "extint",
+ .reset = avr_extint_reset,
+};
+
+void avr_extint_init(avr_t * avr, avr_extint_t * p)
+{
+ p->io = _io;
+
+ // allocate this module's IRQ
+ p->io.irq_count = EXTINT_COUNT;
+ p->io.irq = avr_alloc_irq(0, p->io.irq_count);
+ p->io.irq_ioctl_get = AVR_IOCTL_EXTINT_GETIRQ();
+
+ avr_register_io(avr, &p->io);
+ for (int i = 0; i < EXTINT_COUNT; i++)
+ avr_register_vector(avr, &p->eint[i].vector);
+}
+
--- /dev/null
+/*
+ avr_extint.h
+
+ External Interupt Handling (for INT0-3)
+
+ Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
+
+ This file is part of simavr.
+
+ simavr is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ simavr is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with simavr. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef AVR_EXTINT_H_
+#define AVR_EXTINT_H_
+
+#include "sim_avr.h"
+
+
+enum {
+ EXTINT_IRQ_INT0 = 0,
+ EXTINT_IRQ_INT1, EXTINT_IRQ_INT2, EXTINT_IRQ_INT3,
+ EXTINT_COUNT
+};
+
+// Get the internal IRQ corresponding to the INT
+#define AVR_IOCTL_EXTINT_GETIRQ() AVR_IOCTL_DEF('e','x','t','i')
+
+/*
+ * This module is just a "relay" for the pin change IRQ in the IO port
+ * module. We hook up to their IRQ and raise out interrupt vectors as needed
+ *
+ * "isc" is handled, apart from the "level" mode that doesn't make sense here (?)
+ */
+typedef struct avr_extint_t {
+ avr_io_t io;
+
+ struct {
+ avr_regbit_t isc[2]; // interrupt sense control bits
+ avr_int_vector_t vector; // interrupt vector
+
+ uint32_t port_ioctl; // ioctl to use to get port
+ uint8_t port_pin; // pin number in said port
+ } eint[EXTINT_COUNT];
+
+} avr_extint_t;
+
+void avr_extint_init(avr_t * avr, avr_extint_t * p);
+
+// Declares a typical INT into a avr_extint_t in a core.
+// this is a shortcut since INT declarations are pretty standard.
+// The Tinies are slightly different. see sim_tinyx5.h
+#define AVR_EXTINT_DECLARE(_index, _portname, _portpin) \
+ .eint[_index] = { \
+ .port_ioctl = AVR_IOCTL_IOPORT_GETIRQ(_portname), \
+ .port_pin = _portpin, \
+ .isc = { AVR_IO_REGBIT(EICRA, ISC##_index##0), AVR_IO_REGBIT(EICRA, ISC##_index##1) },\
+ .vector = { \
+ .enable = AVR_IO_REGBIT(EIMSK, INT##_index), \
+ .raised = AVR_IO_REGBIT(EIFR, INTF##_index), \
+ .vector = INT##_index##_vect, \
+ },\
+ }
+
+#endif /* AVR_EXTINT_H_ */