11 years, 4 months ago.

Using NVIC_DisableIRQ(UART2_IRQn); still allows me to see input from pins 27/28

Hi, I must be misunderstanding something in how interrupts are masked, if anyone could take a look at my code and give me any clarification that would be much appreciated. In short, I have a gps chip attached to pins 27/28 of mbed, which I understand correspond to UART2. Im trying to disable the interrupts from those pins right after main() starts, yet I am still able to see the data from GPS in my terminal. Interrupt code was based on Interrupt Serial article on the site.

#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx
Serial serialDevice(p28, p27);  // tx, rx
//Ticker timer;
int baudRate = 38400;


/*Prototypes for the interrupt routines, attached to serialDevice.*/
void Tx_interrupt();
void Rx_interrupt();

volatile char rx_char;


int main() {
        NVIC_DisableIRQ(UART2_IRQn);
    //pc.printf("Started at %i baud rate, InterruptDriverSerialDevice Dec 18\n", baudRate);

    
        wait(1);
        pc.baud(baudRate);
        wait(1);
        //pc.format(8,ParityNone,1);
        wait(1);
        serialDevice.baud(baudRate);        
        wait(1);
        serialDevice.attach(&Rx_interrupt, Serial::RxIrq);
        serialDevice.attach(&Tx_interrupt, Serial::TxIrq);
        
        
        pc.printf("Interrupt Routines set up, program starting\n");
        
        for (i = 0; i < strlen(welcomeString); i++){
             pc.putc(welcomeString[i]);
        }
        pc.putc('\n');
        //NVIC_EnableIRQ(UART2_IRQn);
        
             while (1){
               //do nothing
                
            }
}


void Rx_interrupt() {
  
    /*Loop if there is stuff to read and space in buffer */
    while ((serialDevice.readable())) {
        //store the char in the buffer
        rx_char = serialDevice.getc();
        pc.putc(rx_char);
    }
   
    
}


void Tx_interrupt() {
  
    //while (serialDevice.writeable()) {}
}

2 Answers

11 years, 4 months ago.

Try disabling the IRQ right after you attach them. If you want to be sure they don't get called while attaching them, you can use those 2:

__disable_irq();
__enable_irq();

It is just a guess from me, but I wouldnt be surprised if the attach functions automatically enables IRQs. However since you can't know if the serial class internally also uses the interrupts for something it would be better to just not attach them if you don't need them, so why not do that? You can detach them also by attaching a NULL pointer.

Accepted Answer

Hi Eric, thank you for the suggestion, indeed the problem was trying to disable them before having them attached. After I reordered the NVIC_DisableIRQ(UART2_IRQn); call after the attach() functions it started working as expected.

posted by Evgheni Cr 19 Dec 2012
11 years, 1 month ago.

Does it work for mbed nxp1768, because i have a big problem which is:

UART1=P9,P10 UART2=P13,P14 UART3=P27,P28???

i'm not if i have the correct idea of pins distributions?

Nop that isn't correct, here are the pins: http://mbed.org/users/daan/notebook/uart-_uidx-and-pins/

posted by Erik - 12 Mar 2013