choosing register to turn off and ON for interrupt

Thread Starter

yef smith

Joined Aug 2, 2020
147
Hello, in the example code bellow they turn off IEN_RXDATAV in thehandler and enale it back in the main.
Why we enable and disable RXDATAV in IEN?
Why we cant turn on and off in IF?
what is the logic in this?
Thanks.

1603647285603.png
1603647312253.png

Code:
#include "em_device.h"
#include "em_cmu.h"
#include "em_gpio.h"
#include "em_usart.h"
#include "em_chip.h"
#include <stdint.h>
#include <stdbool.h>
#include "em_emu.h"
#include "bsp.h"
#include "bsp_trace.h"


uint8_t received_flag=0;//
// Receive data buffer
uint8_t buffer;
uint8_t tx_buffer=0x6F;
uint8_t tx_buffer_new=0x06;


void USART0_RX_IRQHandler(void)
{
    received_flag=1;
  // Get the character just received
  buffer = USART0->RXDATA;

  switch (buffer)
        {
        case 'a':

            tx_buffer_new=0x0F;
          break;

        case 'b':
            tx_buffer_new=0x2F;
        break;

        case 'c':
                    tx_buffer_new=0x4F;
         break;

        default:

        break;

        }//end switch
  // Clear the requesting interrupt before exiting the handler
  USART_IntClear(USART0, USART_IF_RXDATAV);
}



int main(void)
{
  uint32_t i;

  // Chip errata
  CHIP_Init();
  CMU_ClockEnable(cmuClock_GPIO, true);
  CMU_ClockEnable(cmuClock_USART0, true);


  //EFR32fg14 LOC2 page 157 data sheet TX P0
  GPIO_PinModeSet(gpioPortA,2, gpioModePushPull, 1);

//EFR32fg14 LOC2 page 157 data sheet RX P2
  GPIO_PinModeSet(gpioPortA,3, gpioModeInput, 0);

  GPIO_PinModeSet(gpioPortA, 5, gpioModePushPull, 1);


  // Default asynchronous initializer (115.2 Kbps, 8N1, no flow control)
  USART_InitAsync_TypeDef init = USART_INITASYNC_DEFAULT;

  // Configure and enable USART0
  USART_InitAsync(USART0, &init);
  //datasheet page 157 location2  rx portA pin3(P2) ,tx port A pin2(P0)
  USART0->ROUTELOC0 = USART_ROUTELOC0_RXLOC_LOC2 | USART_ROUTELOC0_TXLOC_LOC2;
  USART0->ROUTEPEN |= USART_ROUTEPEN_TXPEN | USART_ROUTEPEN_RXPEN;


  // Enable NVIC USART sources
  NVIC_ClearPendingIRQ(USART0_RX_IRQn);
  NVIC_EnableIRQ(USART0_RX_IRQn);
  NVIC_ClearPendingIRQ(USART0_TX_IRQn);
  NVIC_EnableIRQ(USART0_TX_IRQn);



  while (1)
  {

      if (received_flag==1)
      {
          USART_Tx(USART0,tx_buffer_new);
          USART_Tx(USART0,'\n');
          tx_buffer=tx_buffer_new;
          received_flag=0;
      }
      else
      {
          USART_Tx(USART0,tx_buffer);
                    USART_Tx(USART0,'\n');
      }
      for(i=0;i<115;i++)
      {
            GPIO_PinOutSet(gpioPortA,5);
            GPIO_PinOutClear(gpioPortA,5);
       }

    // Enable receive data valid interrupt
    USART_IntEnable(USART0, USART_IEN_RXDATAV);







  }
}
 

mckenney

Joined Nov 10, 2018
101
The Rx buffer is size 1, so when a byte arrives it becomes full. received_flag could be interpreted as a boolean or a buffer-contents count.

This code disables the Rx interrupt when the Rx buffer is full, and main re-enables it when it makes space available in the buffer.

Another approach might be to leave the Rx interrupt enabled, and just throw away the Rx byte if the Rx buffer is full.
 

Thread Starter

yef smith

Joined Aug 2, 2020
147
Hello , in the EFR32FG14 manual shown bellow TXBL flag is 1 when TXBL is empty.
So I need to check if TXBL=0 (full buffer) before i do
buffer = USART0->RXDATA;
But why we need to check if its full?
It seems that the new data just overides the new one.
Thanks.


1603695366156.png
1603695513044.png
 

MrChips

Joined Oct 2, 2009
22,917
All interrupts from this device (USART0) are channeled to one ISR (interrupt service routine).
You cannot assume that the interrupt is from the receiver. You need to determine what is the source of the interrupt.
 

mckenney

Joined Nov 10, 2018
101
If the variable "buffer" is full and a new byte arrives, you can:
1) Accept it from the UART and overwrite the variable
2) Accept it from the UART and throw it away
3) Leave it pending out in the UART, in hopes that you'll empty the variable before yet another byte arrives.
The people who wrote the Example chose option (3).

There's also:
4) Make "buffer" more than one byte long and store successive bytes in it. This isn't a "forever" thing, but it does give you some burst capability (thus the term "buffer").
 
Top