Serial attach block in a K64F Freedom board

28 May 2015

Goodmorning,

I'm working with a K64F Freedom board with RTOS and I've to receive data from a serial connection. I've used attach function of Serial class and I've defined a routine which is called every time an interrupt in the receiving line is generated. Then I defined a thread where I attach this function before a while(true) loop. Now I've a problem: if a define other threads their execution is locked until a serial character is received and my interrupt routine called. This is a strange behaviour. Please, could anyone help me to solve it?

Thanks in advance, Seo

28 May 2015

When using RTOS, you must use RawSerial instead of Serial

29 May 2015

Thank you so much Kevin!

I've applied the correction you've suggested but no modifications. The entire system is locked!! I'll post some code I've written:

include the mbed library with this snippet

RawSerial WiFi_sh(WIFI_TX, WIFI_RX);
...
void WiFi_th(void const *args);
void WIFI_manage_irq(void);
...
int main() {
    ...
   Thread WiFi_thread(WiFi_th);
    ...
}

void WIFI_manage_irq(void){

    //pc.printf("WIFI_manage_irq...\r\n");
    _Prev = _Curr;
    _Curr = WiFi_sh.getc();
    //pc.putc(_Curr);

    getContainer[mIndex++] = _Curr;

    if(_Prev == 0x0D && _Curr == 0x0A){
        getContainer[mIndex] = 0x0;
        wifi_ser_mutex.lock();
        strcpy(tmp_payload, getContainer);
        wifi_ser_mutex.unlock();
        _Stop = true;
    }
        memset(getContainer, 0, LITTLE_BUFF*sizeof(char));
        mIndex = 0;
        _Prev = 0x0;
        _Curr = 0x0;
}

void WiFi_th(void const *args){

    ...
    WiFi_sh.attach(&WIFI_manage_irq, Serial::RxIrq);

    pc.printf("WiFi_th before running\r\n");

    while(runningWiFi){
    ...
    }
   ...
}

Maybe there is a mistake in my approach but my problem is: where is it! Any ideas?

Thank you very much, Seo

29 May 2015

Hi Luca,

I'm not quite familiar with RTOS-es, but playing with mutex inside ISR (WIFI_manage_irq) is a little bit dangerous? (https://developer.mbed.org/handbook/RTOS#mutex) Also, try to avoid doing many things inside ISR as much as possible, i.e. execute only the crucial stuff. In your case, inside WIFI_manage_irq() just take the received character with WiFi_sh.getc() and increase index with mIndex++; other things related to buffer operations perform outside the ISR.

Regards, Miloje

29 May 2015

Hi Miloje,

thank you very very much for your suggestions. You're right!! I'll restructure my source code to reduce operation inside ISR and avoid mutex usage inside it. Maybe the entire system will work :-).

Regards, Luca

29 May 2015

Hi Luca,

No problem. Just a hint: when you want to apply more changes, go one by one and test your code after each change. In this way you will know where the problem was (or maybe more of them) and if some new will pop up.

Regards, Miloje