Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Grand Central M4 Express, example code for additional UARTs?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Grand Central M4 Express, example code for additional UARTs?

by tomjennings on Tue Jul 07, 2020 7:31 pm

I'm stumped trying to configure SERCOMs to put UART devices on pins 14, 15, 16, 17. I've searched as best I can for examples but can't find any. Has anyone actually done this successfully?

I'm trying to emulate the serial device pinouts of the Mega 2560 (and Due, and other Mega-format boards) since I have a "shield" that stacks on it and expects UARTs there.

tomjennings
 
Posts: 78
Joined: Thu Aug 17, 2006 1:21 am

Re: Grand Central M4 Express, example code for additional UA

by User_UMjT7KxnxP8YN8 on Thu Jul 09, 2020 1:17 am

You may want to check out Atmel Studio 7, which includes examples for many features of the MCU used on the Grand Central. It's based on Visual Studio.

You can also use AtmelStart to create projects that contain the files you need, then examine those to files to understand how to work with the ATSAMD51J19A peripherals.

The other thing I learned from is looking at the startup code and Arduino's built-in libraries. You can find the latest version (in Windows 10) at C:\Users\[your user name here]\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.5.14\cores\arduino. There you'll find startup.c, which will show you how to address peripherals, and SERCOM.cpp.

Other useful files you'll learn from examining include samd51j19a.h and the various peripheral component and instance include files, such as sercom.h, located at C:\Users\[your user name here]\\AppData\Local\Arduino15\packages\arduino\tools\CMSIS-Atmel\1.2.0\CMSIS\Device\ATMEL\samd51\include. These files contain the constant definitions you'll need to configure and use your new UART.

Let me know if you have any specific questions and I'll help if I can.

User_UMjT7KxnxP8YN8
 
Posts: 153
Joined: Tue Jul 17, 2018 1:28 pm

Re: Grand Central M4 Express, example code for additional UA

by tomjennings on Thu Jul 09, 2020 1:43 am

I've got four years of development into a real-time car multi-processor system, I'd rather not switch code dev platforms in order to get the serial devices working.

I am capable of delving in the CPU datasheet and puzzling it all out from that, but it's that on top of working out Adafruit's and Ardunio's new code and source system around this board, when all I'm looking for is to make the Arduino Serial objects usable on the board.

The only Arduino-specific info I can find on how you're using SERCOMs in the Arduino system is the LEARN page for a Feather using SAMD21 and there are enough differences in SERCOM allocation to make that not useful.

In the short term, I'm simply using Due's. I was drawn to the G.C. M4 Express for it's peripherals (SDcard, .... SERCOMs) and to get away from the 5V Mega's onto a platform with some life but I can't figure out how to debug the (lack of) symptoms in the sole example of creating Serial2 and Serial3 that appears in another thread on this forum.

The older Mega board doesn't need "example" code for Serial2 etc because the existing Serial object "just works" on it. Given that the GC M4 Express needs core Arduino mods, to be honest, I sort of expect Adafruit to maybe provide some board-specific hints or... a working example.

Or maybe I'm the only person who's ever asked for two serials in addition to the USB port. That seems silly. The board's been out 18 months now, and there is no additional detailed data available past Dec 2018? Or am I missing something? (Which would be great, because then I can just go RTFM! :-)

tomjennings
 
Posts: 78
Joined: Thu Aug 17, 2006 1:21 am

Re: Grand Central M4 Express, example code for additional UA

by User_UMjT7KxnxP8YN8 on Thu Jul 09, 2020 9:31 am

1. Don't let my username name mislead you into thinking I work for Adafruit. I don't.

2. Not suggesting you change IDEs; just noting that looking at example code in AS7 would provide examples that aren't available in Arduino. The easiest way to do this is to go to https://start.atmel.com and create a project that includes a UART, then click "View Code". Start in the /hpl directory to see the high level sercom functions, then go to /hri to see the lower level code.

3. I found the resources I pointed out to you when I integrated the TRNG, RTC, WDT and AES peripherals into my projects, and thought they might be useful to you. When I began working on them I found that nobody had done anything with them either.

Good luck, and when you get it working please share on github and post a link to it here so the next person who wants to do this doesn't have to reinvent the wheel.

User_UMjT7KxnxP8YN8
 
Posts: 153
Joined: Tue Jul 17, 2018 1:28 pm

Re: Grand Central M4 Express, example code for additional UA

by westfw on Thu Jul 09, 2020 12:03 pm

There are some details in previous messages in this forum.
It’s similar to the tutorial for samd21, but you need to define additional interrupt handlers.
I don’t think that the datasheet is very helpful with this, because the main issue is attaching the existing arduino core code to new arduino core objects. The code needed to actually manipulate the sercoms is already there.

westfw
 
Posts: 1711
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Grand Central M4 Express, example code for additional UA

by tomjennings on Thu Jul 09, 2020 12:03 pm

Adafruit_UMjT7KxnxP8YN8 wrote:1. Don't let my username name mislead you into thinking I work for Adafruit. I don't.


I did assume that! lol

Yeah, this morning I'm going to try to get to the root of this. I found some SAMD51 stuff, buried, in another Adafruit page, I now realize I hadn't changed the multiplexer within my sketch, etc.

I'm wondering if my expectation for some minimal "provided" (define...) support code is reasonable, or not. I went back to the product page... it does say "Mega pinout", and mentions the major peripherals, and though not clearly stated, the four Mega serial ports *are* port of the Mega pinout... but this is hardly an ISO standard, it's a collection of workalikes. We know that peripherals vary, eg. analog ins and references. But a loose basic functionality is implied, and delivered, by "most" workalikes.

The SERCOM stuff and the multiplexers are complex, even for experienced programmers. And a lot of what is missing -- and really does need to be provided by the board maker (your namesake :-) is what SERCOMs and mux junk and other feetch are already allocated for other "Arduino" functions. I can infer which SERCOMs are in use but it's not super clear for someone like me, having to familiarize with a new chip, new board, etc.

tomjennings
 
Posts: 78
Joined: Thu Aug 17, 2006 1:21 am

Re: Grand Central M4 Express, example code for additional UA

by User_UMjT7KxnxP8YN8 on Thu Jul 09, 2020 4:00 pm

Defining new interrupt handlers is straightforward; just include a function in your code that overrides the default dummy handler defined in atsamd51j19a.h.

For SERCOM, the handlers are

void SERCOM0_0_Handler ( void );
void SERCOM0_1_Handler ( void );
void SERCOM0_2_Handler ( void );
void SERCOM0_3_Handler ( void );
void SERCOM1_0_Handler ( void );
void SERCOM1_1_Handler ( void );
void SERCOM1_2_Handler ( void );
void SERCOM1_3_Handler ( void );
void SERCOM2_0_Handler ( void );
void SERCOM2_1_Handler ( void );
void SERCOM2_2_Handler ( void );
void SERCOM2_3_Handler ( void );
void SERCOM3_0_Handler ( void );
void SERCOM3_1_Handler ( void );
void SERCOM3_2_Handler ( void );
void SERCOM3_3_Handler ( void );
void SERCOM4_0_Handler ( void );
void SERCOM4_1_Handler ( void );
void SERCOM4_2_Handler ( void );
void SERCOM4_3_Handler ( void );
void SERCOM5_0_Handler ( void );
void SERCOM5_1_Handler ( void );
void SERCOM5_2_Handler ( void );
void SERCOM5_3_Handler ( void );

You can find an example where I did this for the Watchdog Timer (WDT) at https://github.com/SapientHetero/Watchdog-Timer-for-ATSAMD51J19A/blob/master/wdtFunctions.c. It's called WDT_Handler().

User_UMjT7KxnxP8YN8
 
Posts: 153
Joined: Tue Jul 17, 2018 1:28 pm

Re: Grand Central M4 Express, example code for additional UA

by westfw on Fri Jul 10, 2020 5:24 am

(Oh. Same poster, same problem. Here's the same response, in case other people look in only one place.)

Here's a hack that should make things easier. I did SOME testing on a Grand Central, but thorough testing of multiple serial ports is a bit beyond my hardware and patience...
Let us know if there are problems (or if it works.)

/
Code: Select all | TOGGLE FULL SIZE
*
 * sercomlib.h
 * July 2020, by Bill Westfield
 * Released to the public domain.
 *
 * This defines macros to pair up "un-used" sercom interfaces with Arduino driver-level
 *  definitions like UART/HardwareSerial, on SAMD51-based boards.
 * 
 *  Use it like:
 *     sercom_UseSerial(sercomNumber, SerialNumber)
 *  It's all relatively ugly pre-processor magic, and it
 *  assumes that symbols like sercomN, (from variant.cpp)
 *  and PIN_SERIALn_TX, PIN_SERIALn_TX (from variant.h)
 *  are already defined (True for Adafruit's Grand Central Station board,
 *  but not for some others.
 *  SerialN and the SERCOMn_x_Handler() end up being defined.
 *     
 *  At some point, this might get expanded to allow I2C and SPI as well,
 *  and support other chip families.
 */
#include <variant.h>
#define sercom_UseSerial(_sercom, _serialNum) \
  Uart Serial##_serialNum( &sercom##_sercom, PIN_SERIAL##_serialNum##_RX, PIN_SERIAL##_serialNum##_TX, SERCOM_RX_PAD_1, UART_TX_PAD_0 ) ; \
  void SERCOM##_sercom##_0_Handler(void) {  Serial##_serialNum.IrqHandler(); } \
  void SERCOM##_sercom##_1_Handler(void) {  Serial##_serialNum.IrqHandler(); } \
  void SERCOM##_sercom##_2_Handler(void) {  Serial##_serialNum.IrqHandler(); } \
  void SERCOM##_sercom##_3_Handler(void) {  Serial##_serialNum.IrqHandler(); }


And a sample sketch:
Code: Select all | TOGGLE FULL SIZE
#include "sercomlib.h"
/*
 *  July 2020 by Bill Westfield.  Released to the public domain.
 */
/*
 *  Attach the extra SERCOM peripherals to Serial Ports (UART mode.)
 *  Note that for Grand Central, the required PIN_SERIALn_RX/etc defines are already in variant.h
 *  For other boards, it may be necessary to define them.
 */
//              SERCOM#, Serial#
sercom_UseSerial(  4,      2)   // Serial2 - pins 18 and 19
sercom_UseSerial(  1,      3)   // Serial3 - pins 16 and 17
sercom_UseSerial(  5,      4)   // Serial4 - pins 14 and 15


void setup() {
  // Start ports at different speeds to demo their independence.
  Serial.begin(9600);
  Serial1.begin(19200);     // Serial1 (pins 0 and 1) alread defined.
  Serial2.begin(38400);
  Serial3.begin(115200);
  Serial4.begin(9600);
  while (!Serial)           // Wait for USB Serial to finish initialized
    ;
}

void loop() {
  Serial.println("This is Serial");
 
  Serial1.println("This a Serial 1");    // Check Transmit
  while (Serial1.available()) {          // Check receive
    Serial1.print("Serial 1 read ");
    Serial1.println(Serial1.read());
  }
  Serial2.println("This a Serial 2");
  while (Serial2.available()) {
    Serial2.print("Serial 2 read ");
    Serial2.println(Serial2.read());
  }
  Serial3.println("This a Serial 3");
  while (Serial3.available()) {
    Serial3.print("Serial 3 read ");
    Serial3.println(Serial3.read());
  }
  Serial4.println("This a Serial 4");
  while (Serial4.available()) {
    Serial4.print("Serial 4 read ");
    Serial4.println(Serial4.read());
  }
  delay(1000);
}

westfw
 
Posts: 1711
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Grand Central M4 Express, example code for additional UA

by 204maker on Fri Jul 10, 2020 10:15 am

I was under the impression that Grand Central M4 matches the Serial pins on the MEGA by default. However, my multiple serial code failed to compile when I tried to use Serial 2 and 3 on Grand Central M4 flagging that Serial 2 and 3 were not defined. It would have been nice to have the GCM4 match the pin functions on the MEGA when using Arduino.

I asked a question about the Serial pins on the GCM4 in this week’s Ask an Engineer, it was mentioned that Serial 2 and 3 exists for GCM4. In my case, it did not work with Arduino by default even if I have the latest FW.

I saw a guide for adding Serial on SAMD21 with notes added for SAMD51.

Thanks for sharing your code.

204maker
 
Posts: 1
Joined: Wed Jul 08, 2020 1:45 pm

Re: Grand Central M4 Express, example code for additional UA

by tomjennings on Mon Jul 13, 2020 2:37 pm

OK! WORKING!

Thanks to excellent macros from forum member westfw the four "Mega" serials are now available easily. See this thread: viewtopic.php?f=63&t=167207

Requires some additional code in setup(), and inclusion of some macros, for now just add a tab to your sketch and copy them in etc. Example test program in htat thread.

tomjennings
 
Posts: 78
Joined: Thu Aug 17, 2006 1:21 am

Re: Grand Central M4 Express, example code for additional UA

by westfw on Tue Jul 14, 2020 4:41 am

Hmm. The natural place to incorporate this in the cores would seem to be in the variant.h itself.
But if I do that, the current implementation is more general than it needs to be. The user interaction could just be:
Code: Select all | TOGGLE FULL SIZE
SERIAL2_ENABLE
SERIAL3_ENABLE
  :
etc


There is a chance for interference with the SPI and TWI libraries; everything is fine if neither of those is included, but if they ARE included they snatch up all of the Sercoms that are defined to be usable by those libraries up through xxx_INTERFACES_COUNT, whether they're all used or not. Sometimes there isn't any alternative (monster_m4sk has two SPI displays, for example, and GCSm4 has the 2nd SPI port on SERCOM2 for the uSD card.) In other cases, not so much (WIRE on grand central will grab pins 24/25 (SERCOM 6)) for a 2nd I2C port, even when those pins need not be committed.

westfw
 
Posts: 1711
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Grand Central M4 Express, example code for additional UA

by tomjennings on Thu Jul 16, 2020 2:27 pm

westfw wrote:Hmm. The natural place to incorporate this in the cores would seem to be in the variant.h itself.
... The user interaction could just be:
Code: Select all | TOGGLE FULL SIZE
SERIAL2_ENABLE
SERIAL3_ENABLE
  :
etc


That would be nice, but...


westfw wrote:There is a chance for interference with the SPI and TWI libraries; everything is fine if neither of those is included, but if they ARE included they snatch up all of the Sercoms that are defined to be usable by those libraries up through xxx_INTERFACES_COUNT, whether they're all used or not.


Specific: Hmm... my current project uses two SERCOM serials, I2C, and includes the SPI lib, and so far so good. But I haven't yet used SPI... will it steal my SERCOMs when I invoke it?

General: I should have noticed this up front... the "free" SERCOMs are not free as in freely available. In fact they are a scarce resource, and 70 years of software history tells us that contention for scarce resources is a serious problem. Something as simple as a tabular map of SERCOM/library definitions would probably be sufficient. Do you know this offhand? Adafruit must, somewhere, as they need to prevent their own libs from colliding. eg. a second I2C requires SERCOMx, etc.


westfw wrote:But if I do that, the current implementation is more general than it needs to be.


Maybe not, if it allows more fine grained control over allocation to avoid these other collisions.

tomjennings
 
Posts: 78
Joined: Thu Aug 17, 2006 1:21 am

Please be positive and constructive with your questions and comments.