UART0 RXEDGIE and Serial1 library

Status
Not open for further replies.

ldas

New member
Hello,

I'm trying to enable the RxD Input Active Edge Interrupt on the UART0 module. The idea is that after an active edge interrupt is triggered, the Teensy should read the data that is being received and then do nothing until more data arrives. Later on I want to set up the Teensy to sleep while no data is being received, the interrupt will be responsible for waking the teensy up. Here is my code so far:

Code:
#include <Arduino.h>
#include "kinetis.h"

volatile uint8_t incomingByte; //variable used to save the value of the incomig byte
volatile bool isFlagSet = false;

void blink();
void isr();

int main() {

  pinMode(13, OUTPUT);  //LED pin
  Serial.begin(9600); //start USB serial interface
  Serial1.begin(9600);  //start UART0 module

  //wait for the module to start
  while(!Serial){
    blink();
  }
  uint8_t temp = UART0_BDL;
  //Disable the transmitter and the receiver before changing the configuration
  UART0_C2 &= ~(1<<3);
  UART0_C2 &= ~(1<<2);
  UART0_BDH |= (1<<6);  //Input Active Edge Interrupt Enable
  UART0_BDL |= temp;  //write the same value again to apply changes to UART0_BDH
  UART0_C2 &= ~UART_C2_ILIE;  //deactivate Idle Line Interrupt Enable, receiver full
  UART0_C2 &= ~(1<<5);  //RDRF interrupt and DMA transfer requests disabled.
  //Enable the receiver and transmitter again
  UART0_C2 |= (1<<3);
  UART0_C2 |= (1<<2);
  
  NVIC_SET_PRIORITY(IRQ_UART0_STATUS, 64);
  NVIC_ENABLE_IRQ(IRQ_UART0_STATUS);    //enable interrupt in the NVIC, might be unnecessary

  attachInterruptVector(IRQ_UART0_STATUS, isr); //attach custom ISR 
 
  uint16_t reps = 0;

  while(1){
    
    if(isFlagSet){
      if(Serial1.available() > 0){
        incomingByte = Serial1.read();
        Serial.print("\nReceiving data: ");
        Serial.println(incomingByte);
      } else {
        Serial.print("\rNo data is being received ");
        Serial.println(Serial1.available());
      }
      reps++;
      Serial.println(reps);

      isFlagSet = false;
      //reenable the interrupt
      UART0_BDH |= (1<<6);
      UART0_BDL |= UART0_BDL;
      NVIC_ENABLE_IRQ(IRQ_UART0_STATUS);
      delay(500);
    }
  }
}

void blink(){
  digitalWrite(13, HIGH);
  delay(200);
  digitalWrite(13, LOW);
  delay(200);
}

void isr(){
  __disable_irq();
  NVIC_DISABLE_IRQ(IRQ_UART0_STATUS);
  //disable RXEDGIE
  UART0_BDH &= ~(1<<6);
  UART0_BDL |= UART0_BDL;
  // Clear interrupt flag
  UART0_S2 |= (1<<6);   
  isFlagSet = true;
  __enable_irq();
}

My problem with this code is that after the ISR has been executed and the code in the (isFlagSet){...} never returns the data that is being received. Serial1.available() seems to always return 0. It would be nice if someone can give me some tips to solve the problem. I'm using a Teensy 3.2.
 
Sorry,

I may be missing something, but how would you expect Serial1.available() to work in this case?

I believe you replaced the ISR that Serial1 would use to receive the characters and place them in the software queue that Serial1.available() and Serial1.read() would check to see if there was any data.
 
You were right. I added some code to the interrupt service routine found in Serial1.c. Now the active edge interrupt is being activated and Serial1.available() and Serial1.read() are working. Thanks.
 
Status
Not open for further replies.
Back
Top