UART Interrupt won’t work

armcinterruptsrs485uart

I have a problem when trying to test send and receive lines on RS485 communication, anyone know what may cause a problem here? It works without interrupt but with interrupt won't work. I am working on NXP LPC1788, and trying to make an interrupt echo program for RS485 (what I send, it sends me back to terminal). Thanks in advance.

int main(void) {

    uart2()

    NVIC_EnableIRQ(UART2_IRQn); 

    while(1) {

    } //while   

} //main

void receive(void) {

    SMR_485RECEIVE; //Receive data flow
    delay_1_ms();
    delay_1_ms();   
    receive_byte = LPC_UART2->RBR;
    }

void send(void) {

    SMR_485TRANSMIT; //Transmit data flow
    delay_1_ms();
    delay_1_ms();
    LPC_UART2->THR = receive_byte;
    }

void UART2_IRQHandler(void) __irq {

    BYTE tip = LPC_UART2->IIR & 0x06;       
    if((tip == 0x04) && !(LPC_UART2->RBR == '\0')){
        receive(); 
        delay_1_ms();
        send();
        }       

    }

If I'm right before sending I need to call SMR_485TRANSMIT and then store BYTE to output data register THR, when I want to receive byte I call SMR_485RECEIVE and then store data from input data register RBR to variable receive_byte. I deleted delays of 1ms and my code looks like this right now:

void receive(void) {


    SMR_485RECEIVE;
    delay_1_micro();
    receive_byte = LPC_UART2->RBR;
    }

void send(void) {


    SMR_485TRANSMIT;//Transmit data flow
    delay_1_micro();
    LPC_UART2->THR = receive_byte;
    }

void UART2_IRQHandler(void) __irq {

    BYTE tip = LPC_UART2->IIR & 0x06;       
    if(tip == 0x04){
        receive();
        delay_1_micro();
        send();
        }       
    }

But it won't work, I dont understand whats the problem in my code when switching direction of data flow?

Best Answer

Your Interrupt handler tries to read RBR twice (once in the handler, once in receive). That won't work, the RBR reads one byte from the FIFO each time.

The RS485 switching also seems to be incorrect. You ususally switch to transmit mode immediately before sending a byte, and you switch back to receive in the THRE interrupt when transmission is over.

The 1ms delays will be problematic with baud rates > 2400, causing lost frames.