InterruptIn no longer work when I am try to SPI

13 Jun 2011

Does anybody have any idea whey when I added the "SPI spi(p5, p6, p7);" the interrupt will no longer work? Could anybody tell me what went wrong and how to get around this? Please help.

#include "mbed.h"

InterruptIn dataReady(p30);

SPI spi(p5,p6,p7); // mosi, miso, sclk  <-- The moment when this is added the interrupt routing will not be called? Why?
//DigitalOut cs_n(p8);

Serial pc(USBTX, USBRX);

void displayData() {
    pc.printf (" Interrupt occurred\n\r");
}

int main() {
//    pc.printf ("Chip Select is High for 10us\n\r");
//    cs_n = 1;
//    wait_us(10);
//    spi.format(16,3);
//    spi.frequency(2000000);
   
    pc.printf ("Interrupt Test Begin \n\r");
    
    dataReady.rise(&displayData);  // attach the address of the flip function to the rising edge
    
}

14 Jun 2011

Welcome to the "I've been bitten by putting a printf statement inside a callback" club :-)

Try arranging your program a little differently, and see if things improve. Specifically, move the printf out of the callback and into the main() section. Just have the callback set a flag that can be tested in main().

For example:

#include "mbed.h"

InterruptIn dataReady(p30);

SPI spi(p5,p6,p7); // mosi, miso, sclk  <-- The moment when this is added the interrupt routing will not be called? Why?
//DigitalOut cs_n(p8);

Serial pc(USBTX, USBRX);

int interruptflag = 0;

void displayData() {
     interruptflag = 1;    // Let the main loop know that an interrupt occurred
}

int main() {
//    pc.printf ("Chip Select is High for 10us\n\r");
//    cs_n = 1;
//    wait_us(10);
//    spi.format(16,3);
//    spi.frequency(2000000);
   
    pc.printf ("Interrupt Test Begin \n\r");
    
    dataReady.rise(&displayData);  // attach the address of the flip function to the rising edge

    while(1) {
         if(interruptflag) {
            interruptflag = 0;
            pc.printf (" Interrupt occurred\n\r");
         }
    }   
}

For a detailed discussion of related issues, check out Andy Kirkham's article.

14 Jun 2011

Hexley Ball. Wonderful! You example compiled successfuly and work. I really appreciate your help. Thank you. Yes, I am a new member of the club now ;-)

02 Aug 2011

I have tried using the structure of your code for my project but it is showing rubbish result. Instead of printf, I am using the onboard LED to tell me if there is a trigger.

#include "mbed.h"

PwmOut led(LED1);
DigitalOut iled(LED2);
PwmOut ind(LED3);

InterruptIn trig(p16);

int trigVal = 0;

void up() {
    trigVal = 1;
    iled = 1;
}
/*void down() {
    iled = 0;
    led = 0.5;
}*/

int main() {    
    ind.period(1);
    ind = 0.5;
    
    led.period(1);
    led = 0.5;
    while(1) {
        trig.rise(&up);
        //wait(4);
        if (trigVal) {
            trigVal = 0;            
            led = 0.2;
        }
        
        /*trig.fall(down);
        wait(4);*/
    }   
}

The result is that LED2 shows that it has been triggered and the whole program remains in the if() loop without breaking off. The duty cycle is at 0.2 without going back to its original 0.5.

02 Aug 2011

I have solved my problem. Thanks for the code. This is the final code.

#include "mbed.h"

PwmOut led(LED1);
DigitalOut iled(LED2);

InterruptIn trig(p16);

int trigVal = 0;

void up() {
    trigVal = 1;
    iled = 1;    
}

int main() {    
    led.period(1);
    led = 0.5;
    
    trig.rise(&up);
    while(1) {             
        if (trigVal) {
            trigVal = 0;
            iled = 0;            
            led = 0.2;
        }
        wait(1);
        led = 0.5;             
    }   
}