This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

UART RX interrupt

Hi, I am using the STR912FA and I am trying to implement the UART communication using interrupts. I am using the interrupt only for receiving data, but for some reason I never get an interrupt when writing a character in the hyperterminal ...
The transmition works ok using the printf to the hyperterminal.
Does someone see something wrong in my code?
Thanks a lot!

This is my UART initialization:

// UART  Module
  UART_InitStructure.UART_WordLength = UART_WordLength_8D;
  UART_InitStructure.UART_StopBits = UART_StopBits_1;
  UART_InitStructure.UART_Parity = UART_Parity_No ;
  UART_InitStructure.UART_BaudRate = 115200;
  UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
  UART_InitStructure.UART_Mode = UART_Mode_Tx_Rx;
  UART_InitStructure.UART_FIFO = UART_FIFO_Disable;
  UART_DeInit(UART0);
  UART_Init(UART0, &UART_InitStructure);
  UART_Cmd(UART0, ENABLE);
  UART_ITConfig(UART0, UART_IT_Receive, ENABLE);
  VIC_DeInit();
  VIC_Config(UART0_ITLine, VIC_IRQ, 2);
  VIC_ITCmd(UART0_ITLine, ENABLE);

This is the interrupt (That I never get):

void UART0_IRQHandler(void)
{
 /*clear receive interrupt flag*/
  UART_ClearITPendingBit(UART0, UART_IT_Receive);
  //UART_ClearITPendingBit(UART0, UART_IT_Transmit);
  /*send back the received character*/
  while(UART_GetFlagStatus(UART0, UART_FLAG_TxFIFOFull) != RESET);
  UART_SendData(UART0, UART_ReceiveData(UART0));
}

This is my byte transmition (works):

int MyLowLevelPutchar(int x)
{
    UART_SendData(UART0, x);
    while(UART_GetFlagStatus(UART0, UART_FLAG_TxFIFOFull) == SET);

  return x;
}

  • Sorry, can't help with this processor. But just a question. Wouldn't it be better to swap the test and the insert lines in this function:

    int MyLowLevelPutchar(int x) {
        UART_SendData(UART0, x);
        while(UART_GetFlagStatus(UART0, UART_FLAG_TxFIFOFull) == SET);
    
        return x;
    }
    


    Now, you don't test if there is room for one more chacter before you insert. But on the other hand, you never leave this function until there is room for one more. That means that you may busy-loop where you could have let the processor do something else.

    It is way more common to do the busy-loop before the insert - then it will only loop if you did not manage to consume enough processor time somewhere else until the FIFO got room for more data.

  • Thanks for your answer.

    After changing the code in order to test the condition first my transmition just stop working !?

    int MyLowLevelPutchar(int x)
    {
        while(UART_GetFlagStatus(UART0, UART_FLAG_TxFIFOFull) == SET)
        {
        UART_SendData(UART0, x);
        }
      return x;
    }
    

    but one thing, in order to check this flag UART_FLAG_TxFIFOFull I need to have the fifo mode enabled right? And I don't, so maybe that is why I never send anything ...

    My problem with the UART Rx still the same ... can't get an interrupt for the received data :(

  • You changed your code in an incorrect way.

    You should have an empty loop while the FIFO is full (just like the original code had) but _before_ the insert.

    First after you end this loop should you do the insert. Now you changed the code to do the insert inside a conditional.

        // Wait until room for at least one more character in FIFO
        while(UART_GetFlagStatus(UART0,UART_FLAG_TxFIFOFull) == SET) ;
        // Insert character in FIFO and return.
        UART_SendData(UART0, x);
        return x;