element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Reading UART Rx Buffer (MKR WiFi 1010 / MKR 1000)
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 16 replies
  • Subscribers 389 subscribers
  • Views 2813 views
  • Users 0 members are here
  • rx
  • uart
  • arduino
Related

Reading UART Rx Buffer (MKR WiFi 1010 / MKR 1000)

kas.lewis
kas.lewis over 5 years ago

Hello All,

 

I'm looking to create my own UART interrupt. For those who need to know why it's because I need to stop the timer as soon as a value comes in. While I know I will get a lot of reasons and explanations why this is not necessary I'm a lot more interested in how to go about doing this.

 

I have to commented out

 

void SERCOM5_Handler()

{

  Serial1.IrqHandler();

}

 

in the variant.cpp file which works to remove the Arduino code from doing what it wants. The issue I'm having now is finding the function Serial.read(). I'm trying to read the uart rx buffer but with Atmel's documentation what it is this is not as easy as it should be.

 

 

I have tried

 

void SERCOM5_Handler()

{

  char temp = SERCOM5->USART.DATA.reg;

  if (temp == 'h' || temp == 'H'){

    digitalWrite(LED_BUILTIN, HIGH);

  }

  if (temp == 'l' || temp == 'L'){

    digitalWrite(LED_BUILTIN, LOW);

  }

}

 

 

without much success. Therefore any help in reading the UART rx register would be very much appreciated.

 

Kas

  • Sign in to reply
  • Cancel

Top Replies

  • BigG
    BigG over 5 years ago +1
    I can see where you are coming from. Usually you could use the "serialEvent()" function but I see that this is not possible with Arduino SAMD Boards. Unortunately, I have no idea why but will be following…
  • michaelkellett
    michaelkellett over 5 years ago +1
    Is (temp == 'h'||temp == 'H') the same as ((temp == 'h')||(temp == 'H')) ? (brackets are free and make code easier to follow, (actually == has precedence over || in C )) How are you debugging this - can…
  • kas.lewis
    kas.lewis over 5 years ago in reply to michaelkellett +1
    Hello michaelkellett I didn't know you can put breakpoints in the Arduino IDE. Truth is there is lots about Arduino I don't know as I usually do bare metal with direct control of al registers and I also…
  • BigG
    BigG over 5 years ago

    I can see where you are coming from. Usually you could use the "serialEvent()" function but I see that this is not possible with Arduino SAMD Boards. Unortunately, I have no idea why but will be following this one.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • michaelkellett
    michaelkellett over 5 years ago

    Is

    (temp == 'h'||temp == 'H')

     

    the same as

     

    ((temp == 'h')||(temp == 'H'))

     

    image ?

    (brackets are free and make code easier to follow, (actually == has precedence over || in C ))

     

    How are you debugging this - can you put a breakpoint in the interrupt handler and see what is in temp at the critical moment ?

     

    Why not modify the original handler ?

     

    MK

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • kas.lewis
    kas.lewis over 5 years ago in reply to michaelkellett

    Hello michaelkellett

     

    I didn't know you can put breakpoints in the Arduino IDE. Truth is there is lots about Arduino I don't know as I usually do bare metal with direct control of al registers and I also usually keep away from Atmel amongst others (not a fan of their documentation).

     

    All that being said your suggestion to modify the original handler is a good one if I could find it. From what I can see the SERCOM5_Handler() calls another function Serial1.IrqHandler() which I'm having a hard time locating... If I find it that would be great. Also the real issue is not so much altering the current handler as figuing out how to read the Rx data register. I might be  a bit closer in getting what I need, if so I'll post my finding here when I get there.

     

    Kas

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • michaelkellett
    michaelkellett over 5 years ago in reply to kas.lewis

    This may sound weird but might be worth a go:

     

    You should be able to run the full Arduino code in the Microchip/Atmel IDE, it uses a Cortex M0 core so there should be ob chip debugging available.

    I don't know if you can get at the pins required.

     

    Is there an Arduino function or library for direct register access ?

     

    MK

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • kas.lewis
    kas.lewis over 5 years ago in reply to michaelkellett

    michaelkellett

     

    Thank you for the suggestion, not really a fan of downloading another IDE (especially since I don't generally use Atmel or Microchip) but I'll see if this will help resolve my issue. Until I do that though I'll share what I've tied so far. There is a lot of commented code but this shows everything I've tried without success. Interestingly enough after sending 'H' or 'h' 3 or so times the software locks up and I have to terminate the Arduino IDE.

     

        #include <avr/interrupt.h> 
        #include <avr/io.h> 
        #include <SERCOM.h>
    
    
    char tempflip = 0;
    #define DATA_SERCOM5           0x42001C00
        
    void setup()
    {
      // initialize serial:
      Serial.begin(9600);
      // initialize digital pin LED_BUILTIN as an output.
      pinMode(LED_BUILTIN, OUTPUT);
    }
    
    
    void loop()
    {
      char temp;
    //  while (Serial.available() > 0) {
    //    temp = Serial.read();
    //    if (temp == 'h' || temp == 'H'){
    //      digitalWrite(LED_BUILTIN, HIGH);
    //    }
    //    else if (temp == 'l' || temp == 'L'){
    //      digitalWrite(LED_BUILTIN, LOW);
    //    }
    //    
    //  }
    }
    
    
    void SERCOM5_Handler()
    {
      readDataUART();
    //  SERCOM5->USART.DATA.reg;
    //  sercom->USART.DATA.bit.DATA;
    //  SERCOM5->USART.DATA.bit.DATA;
    //   unsigned char *status,*intflag,*data;
    //
    //  data=(unsigned char *)DATA_SERCOM5;
    //  status=(unsigned char *)STATUS_SERCOM1;
    //  intflag=(unsigned char *)INTFLAG_SERCOM1;
    //
    //  if((*intflag&RECV)==RECV)                                                           //character received?
    //  {
    //      recv=*data;                                                                                    //fetch received character    
    //      if((*status & 0b00000110)!=0)                                                   //receive errors happened?  
    //      {
    //           *status=6;                                                                                  //reset errors (framing and buffer overflow error)
    //           errors = errors | REC_ERR;                                                  //set global rec error flag in my global error variable not to use the char
    //      }                            
    //    }
    //  uint8_t temp = SERCOM5->USART.DATA.reg;
    //  char  temp = SERCOM5->USART.DATA.reg;
      char temp = SERCOM5->USART.DATA.bit.DATA;
    
    
    //  char temp = SERCOM5->USART.DATA.bit.DATA;
      if (temp == 'h' || temp == 'H'){
        digitalWrite(LED_BUILTIN, HIGH);
      }
      else if (temp == 'l' || temp == 'L'){
        digitalWrite(LED_BUILTIN, LOW);
      }
    
    
    
    
    //  if (tempflip == 1){
    //    digitalWrite(LED_BUILTIN, HIGH);
    //  }
    //  else {
    //    digitalWrite(LED_BUILTIN, LOW);
    //  }
    //  tempflip ^= tempflip;
    //}
    
    
    //void SERCOM4_Hand
    
    
    
    
    
    
    
    
    
    
    
    
    
    {

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • kas.lewis
    kas.lewis over 5 years ago

    CONCLUSION:

     

    I believe the reason why I can't find how to access the UART Rx register directly is because very few people know this as well. I feel this is the same reason when looking on Arduino forums to find this information thee are pages defending the use of Serial.available(). Some users in there defence have even gone so far as to write demos to show that timing and whatever other issue might be brought up is in fact a none issue.

     

    I feel the reality is that it's easier to do all this work then find how to read the Rx register directly. Sadly it seems this will continue as even Microchip can't offer me much in the way of a solid answer other than go through their API and find what I'm looking for myself. If I choose to do that and do find a usable solution I'll "try" post it for others to use as well.

     

    Kas

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • kas.lewis
    kas.lewis over 5 years ago

    Hello All, (michaelkellett)

     

    I think I may have made some progress... It doesn't really help having spent so long with C and left my C++ behind image although I guess this is also found in C.

     

    Have have found some code that may point me in the right direction but pointers and references...

     

    In \AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.4\variants\mkrwifi1010\variant.cpp

    Uart Serial1(&sercom5, PIN_SERIAL1_RX, PIN_SERIAL1_TX, PAD_SERIAL1_RX, PAD_SERIAL1_TX);

     

    In \AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.4\cores\arduino\Uart.cpp

    Uart::Uart(SERCOM *_s, uint8_t _pinRX, uint8_t _pinTX, SercomRXPad _padRX, SercomUartTXPad _padTX, uint8_t _pinRTS, uint8_t _pinCTS)

    {

      sercom = _s;

     

    In \AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.4\cores\arduino\SERCOM.cpp

    uint8_t SERCOM::readDataUART()

     

    In \AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.4\cores\arduino\Uart.cpp

    sercom->readDataUART();

     

    In \AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.4\cores\arduino\SERCOM.cpp

    return sercom->USART.DATA.bit.DATA;

     

    Its this last line I'm having issues with. sercom is a pointer to sercom5, which I assume (I can't find it for sure) is a pointer to the UART registers? I'm not really sure what to make of this line, so I'm putting it out there and hoping someone can help me make heads or tails of this.

     

    Thanks

    Kas

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • michaelkellett
    michaelkellett over 5 years ago in reply to kas.lewis

    I've just ordered an MKR 1010 - in stock at F so should get here tomorrow .

    More stuff to do before Christmas so it  might be a week before I get a look at it.

     

    I did find this:

    https://community.atmel.com/forum/atsamd21-sercom-interrupt-using-asf

     

    If it helps - great.

     

    MK

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • kas.lewis
    kas.lewis over 5 years ago in reply to michaelkellett

    Hello michaelkellett,

     

    I've read over that link now and cant find a clear answer. I've attempted to look for the functions mentioned as well as tried running their code in the Arduino IDE, no dice image. I have also continued to try drill down with in the code to find the callback function that puts the received characters in the ring buffer, also no dice. This is really turning out to be a hunt that doesn't look like will have an easy or quick solution.

     

    If you have any further ideas, I'll be more than happy to entertain them. The one thing I'm trying to avoid is installing Microchips IDE. I currently don't have much space left on my hard drive or the SD card I'm using to extend my storage space. So if this can be resolved with out going that route, I'd greatly appreciate that.

     

    Kas

     

    P.s

    After a bit more reading and digging there maybe something else to try. I'm putting it here for later reference as well as so others can possibly try this out.

     

    There is a file called variant.cpp that has a function

    void SERCOM5_Handler()
    {
      Serial1.IrqHandler();
    }

    this appears to be where the interrupt is handled in real time (this is what would need to be tested) if this is correct then this would be where any additional code can be added to stop timers or get data directly from the UART. I have already tried

    (sercom5).readDataUART();

    in the arduino IDE and it complies. This does not mean it works but there is a better chance then it not even compiling. This might therefore allow me to stop or reset a timer as well as getting received bytes right away. Once tested I'll update this post.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • michaelkellett
    michaelkellett over 5 years ago in reply to kas.lewis

    Hello Kas,

     

    Sorry. I've been busy with lots of other stuff and haven't looked at this since I did the last post.

     

    My initial conclusion was that I just can't see any value (for me at least) in getting bogged down in the Arduino stuff. I can get more value from my time just hitting an ARM or AVR processor in C directly.

     

    I know that's not much help to you -  but good luck with your efforts.

     

    MK

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube