10 years, 5 months ago.

[RESOLVED] DigitalOut write method during interrupt

Hello everyone,

I have a strange behavior here: In a class constructor I initialize a DigitalOut like this:

declaration

output = new DigitalOut(LED1);

With output declared as private member of my class.

On the other hand I have a Ticker interrupt that tries to do:

interrupt

output->write(1);

When this code is called, the programm crashes.

The thing is that when I declare output direclty in the class file (.cpp) like this:

declaration

DigitalOut output(LED1);

And then replacing the previous code by this one

interrupt

output = 1;

It works!!!

Do you have any idea of why the first code isn't working? I am missing something?

Thank you in advance

My guess would be that output isn't properly initialized, but for that we would need to see your code. If it isn't sensitive, could you publish it as public(unlisted)? (Or if really short copy paste).

posted by Erik - 05 Jun 2014

Hello, The code is here, located in Note::applyDesiredState(). As you can see the "engine" can either activate the output on demand or on tick.

posted by Benoît GEISLER 05 Jun 2014

This is getting to the limits of my c++ abilities but initialization could be the issue. Can you switch engine.h / .cpp to having an array of Note pointers rather than Notes and then initialize them using new Note(pin) in the engine constructor?

posted by Andy A 06 Jun 2014

Thank you for your reply, I'll try this ASAP!

posted by Benoît GEISLER 06 Jun 2014

You were right, I needed to declare:

Note * notes[5]

and init the notes like this:

note[1] = new Note(LED1)
posted by Benoît GEISLER 06 Jun 2014
Be the first to answer this question.