modm-io / modm

modm: a C++23 library generator for AVR and ARM Cortex-M devices
https://modm.io
Mozilla Public License 2.0
733 stars 131 forks source link

External interrupt causes immediate reset on Arduino Mega 2560 #1061

Closed mbait closed 1 year ago

mbait commented 1 year ago
#include <modm/board.hpp>
#include <modm/io/iostream.hpp>

#include <modm/processing.hpp>
#include <modm/processing/protothread.hpp>
#include <modm/processing/timer.hpp>

using namespace Board;
using namespace std::chrono_literals;

MODM_ISR(INT4)
{
  Board::D19::acknowledgeExternalInterruptFlag();
  Board::LedD13::toggle();
}

int
main()
{
  Board::initialize();
  Board::LedD13::setOutput();
  Board::D19::setInput(Gpio::InputType::PullUp);
  Board::D19::setInputTrigger(Gpio::InputTrigger::RisingEdge);
  Board::D19::enableExternalInterrupt();

  serialStream << "started" << modm::endl;
  for(;;);
}

The following code being run on Arudino Mega 2560 exhibits weird behaviour: once the desider pin becomes high, the chip resets and starts the program from scratch. A similar setup in Arduino IDE performs just fine.

mbait commented 1 year ago

I realized a mismatch between the pin setup and the ISR provided, and, according to the datasheet, an unimplemented interrupt vector causes reset.