Hardware interrupts on Kl25z and interrupt priority?

27 Jun 2013

Hi folks,

I have a code snippet wherein the KL25z I have a interruptin interrupt defined on the pin PTD1 , but somehow its not getting executed and I fear the priority is low!

here is the code:

#include "mbed.h"
#include "KS0108.h"
#include "Arial12.h"
#include "SystemFont5x7.h"
#include "image.h"
#include "brew.h"
#include "rtos.h"
#include "menu_stop.h"
#include "menu_alert.h"
#include "menu_status.h"
#include "menu_realview.h"
#include "menu_temp.h"
#include "ReceiverIR.h"
#include "ds1307.h"


// KS0108 (PinName _RST,PinName _DI, PinName _RW, PinName _E, PinName _CS1, PinName _CS2, PinName DB0, PinName DB1, PinName DB2, PinName DB3, PinName DB4, PinName DB5, PinName DB6, PinName DB7);

KS0108  display(PTC3,PTD6, PTE31, PTA17, PTC5, PTC4, PTA16, PTC17, PTC16, PTC13, PTC12, PTC11, PTC10, PTC6);
Timeout flowmtr1_wait;
Timer totalflow1;
ReceiverIR ir_rx(PTA13);
Serial pc(USBTX, USBRX); // tx, rx
InterruptIn flowmtr1(PTD1);
bool menu_alert = false, menu_realview = false, menu_status = false,
     menu_temp = false, menu_stop = false, start = false, phase1 = false,
     phase2 = false, phase3 = false, step1 = false, step2 = false, step3 = false, flowmeterON = false;
bool overflow = false, _heat_HLT = false;
uint8_t buf[16];
DS1307 my1307(PTE0,PTE1); // start DS1307 class and give it pins for connections of the DS1307 device
DigitalOut v1(PTD5);//Valve 1 from which the water goes to HLT and is the valce connected to the tap
DigitalOut v2(PTD0);//Valve 2 which is connected to HLT
DigitalOut v3(PTD1);//Valve 3 which is connected to MLT's T piping
DigitalOut p1(PTD2);//1st pump

DigitalIn overflow_HLT(PTD5);

int sec = 0;
int min = 0;
int hours = 0;
int day = 0;
int date = 0;
int month = 0;
int year = 0;
volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;
int phase1RunCnt, phase2RunCnt, phase3RunCnt, check_min;

void rpm()
{
    char in[50];
    NbTopsFan++;  //This function measures the rising and falling edge of the
    sprintf(in, "%d is ", NbTopsFan);
    pc.printf(in);
}
void waiter()
{

}

void test_rw(int test)
{
    if (test == 0) pc.printf("Last R/W operaion passed!\n\r");
    else pc.printf("Last R/W operation failed!\n\r");
}

void  phase1_phase2_step3()
{
    v3 = 0xff;
    p1 = 0xff;
    test_rw(my1307.gettime( &sec, &min, &hours, &day, &date, &month, &year));
    check_min = min;
    step3 = true;
}

void flowmeter1(int qty)
{
    int n;
    totalflow1.start();
    NbTopsFan = 0;   //Set NbTops to 0 ready for calculations
    //flowmtr1.rise(&rpm);
    flowmtr1_wait.attach(&waiter, 1.0);   //Wait 1 second
    //flowmtr1.rise(NULL);
    Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate Minute
    n = totalflow1.read();
    int sec_to_min = n/60;
    int result = Calc*sec_to_min;
    if (result == qty) {
        v2=0x00;
        p1=0x00;
        phase1_phase2_step3();
    }
}

void phase1_step2()
{
    v2 = 0xff;
    p1 = 0xff;
    flowmeterON = true;
}

void waterValve()
{
    phase1RunCnt++;
    v1 = 0xff;
    overflow = 1;
}

void phase1_phase2_step3_timecheck()
{
    test_rw(my1307.gettime( &sec, &min, &hours, &day, &date, &month, &year));
    if(check_min + 30 == min) {


        if(phase1 == true)phase1 = 0,phase2 = 1,phase3=0;
        if(phase2 == true)phase1 = 0,phase2 = 0,phase3=1;
        if(phase3 == true)phase1 = 0,phase2 = 0,phase3=0;
        waterValve();
        step3 = 0;
    }
}

void _heat_check_HLT(int heat)  //start retrieving temp from HLT tank
{


}

void overflow_check()
{
    if(!overflow_HLT) {
        v1 = 0x00;
        overflow = 0;
        _heat_HLT = 1;
    }
}

/*void led2_thread(void const *args)
{
    while (true) {
        pc.printf("lcd thread");
        display.FullScreenBMP(Menu_trial_startbut_Stop);
        Thread::wait(2);
        display.FullScreenBMP(Menu_trial_startbut_Temp);
        Thread::wait(2);
        display.FullScreenBMP(Menu_trial_startbut_RealTimeView);
        Thread::wait(2);
        display.FullScreenBMP(Menu_trial_startbut_Alert);
        Thread::wait(2);
        display.FullScreenBMP(Menu_trial_startbut_Status);
        Thread::wait(2);
        if(menu_alert) {

        } else if(menu_realview) {

        } else if(menu_status) {

        } else if(menu_temp) {

        } else if(menu_stop) {

        }
    }
}*/

void LCDrunner()
{

    display.FullScreenBMP(Menu_trial_startbut_Stop);
    Thread::wait(2);
    display.FullScreenBMP(Menu_trial_startbut_Temp);
    Thread::wait(2);
    display.FullScreenBMP(Menu_trial_startbut_RealTimeView);
    Thread::wait(2);
    display.FullScreenBMP(Menu_trial_startbut_Alert);
    Thread::wait(2);
    display.FullScreenBMP(Menu_trial_startbut_Status);
    Thread::wait(2);

}

void rtc()
{
    test_rw(my1307.gettime( &sec, &min, &hours, &day, &date, &month, &year));
    pc.printf("seconds read are %.2D \n\r",sec);
    pc.printf("min read are %.2D \n\r",min);
    pc.printf("hour read are %.2D \n\r",hours);
    pc.printf("day read are %.2D \n\r",day);
    pc.printf("date read are %.2D \n\r",date);
    pc.printf("month read are %.2D \n\r",month);
    pc.printf("year read are %.2D \n\r",year);
    pc.printf("SerPrinter");
}

int main()
{
    //Thread thread(led2_thread);
    display.ClearScreen();
    display.FullScreenBMP(pic);
    wait(2);
    display.FullScreenBMP(brew1);
    wait(2);
    LCDrunner();
    RemoteIR::Format format;
    memset(buf, 0x00, sizeof(buf));
    flowmtr1.rise(&rpm);
    while(1) {
        if (ir_rx.getState() == ReceiverIR::Received) {
            ir_rx.getData(&format, buf, sizeof(buf) * 8);
            pc.printf("\n\rgot>> ");
            //for (int i=0; i<sizeof(buf); i++) {
            pc.printf("0x%02X, ", buf[0]);
            //pc.printf("%i",i);
            //}
            switch (format) {
                case RemoteIR::UNKNOWN:
                    pc.printf("????");
                    break;
                case RemoteIR::NEC:
                    pc.printf("NEC");
                    break;
                case RemoteIR::AEHA:
                    pc.printf("AEHA");
                    break;
                case RemoteIR::SONY:
                    pc.printf("SONY");
                    break;
            }
        }
        if(overflow == 1) {
            overflow_check();
        }
        if(_heat_HLT == 1) {
            if(phase1 == true) _heat_check_HLT(122);
            else if(phase2 == true) _heat_check_HLT(154);
            else if(phase3 == true) _heat_check_HLT(168);
        }
        if(step3 == true) {
            phase1_phase2_step3_timecheck();
        }
        if(flowmeterON == true) {
            if(phase1 == true)flowmeter1(20);
            if(phase2 == true)flowmeter1(18);
            if(phase3 == true)flowmeter1(20);
        }
        LCDrunner();
        pc.printf("here");
    }
}

// Example of how to put Text on LCD
//display.DrawBitmap(mikro_test,5,5,BLACK);
/*display.GotoXY(5,0);
display.SelectFont(System5x7,BLACK,ReadData);

display.GotoXY(1,16);
display.PrintString("System5x7");
wait(3);

display.GotoXY(2,32);
display.SelectFont(Arial12,BLACK,ReadData);
display.PrintString("Arial 14");
wait(3);*/

HOW CAN I ensure the priority of PTD1 is high and/or is there anyother wrong?

27 Jun 2013

It is handy to try to test it with less code to find out when it is going wrong.

But here it seems you use RTOS. When you do that you are not allowed to use printf in interrupt routines. See: http://mbed.org/handbook/RTOS

27 Jun 2013

I have commented out the RTOS definition routines, so it shouldn't be a problem in there.

RTOS isn't working fine with GLCD library ,the threads dont run for no reason if I put graphic making code for the GLCD

27 Jun 2013

ok checked after removing the printf from the IRQ routine still routine isnt working at all!?

27 Jun 2013

This works flawlessly:

#include "mbed.h"

DigitalOut myled(LED1);
Ticker flowmtr1_wait;
Timer totalflow1;
Serial pc(USBTX, USBRX); // tx, rx
InterruptIn flowmtr1(PTD2);
volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;
char cVal[32];

void rpm()
{
    NbTopsFan++;  //This function measures the rising and falling edge of the
    //pc.printf("interr");
}
void waiter()
{


}

void flowmeter1(int qty)
{

    //Wait 1 second
    //flowmtr1.rise(&rpm);

}

int main()
{
    //flowmtr1.rise(&rpm);
    
    while(1) {
        NbTopsFan = 0;   //Set NbTops to 0 ready for calculations
        flowmtr1.rise(&rpm);
        wait(1.0);
        //flowmtr1_wait.attach(&waiter, 1.0);
        flowmtr1.rise(NULL);
        Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate Minute
        sprintf(cVal,"%d is",Calc);
        pc.printf(cVal);
    }
}


27 Jun 2013

In your first code example PTD1 is used twice:

InterruptIn flowmtr1(PTD1);

DigitalOut v3(PTD1);//Valve 3 which is connected to MLT's T piping

That cant work..

Note that PTD1 is also connected to the Blue LED on the KL25z. That may be a problem for your sensor.

01 Jul 2013

It's now working I changed the InterruptIn on PTA5