Adding support for Serial2 and Serial3 on Grand Central M4


#1

Hi David ,

I wanted to put ulisp 4.5a on my grand central m4 , but have compiling issues with serial 2 and 3 :

Compilation error: 'Serial3' was not declared in this scope; did you mean 'Serial'?

When commenting line 2129 the problem seems to be solved , but it’s not really a solution , because there are indeed 4 serial ports.
// #define ULISP_SERIAL3 -> changing this compiles OK on line 2129

Do you have any idea what’s causing this ? I use the latest arduino 2.3.2
Kind regards,
Ronny


#2

I’ve checked on the Adafruit forum, and found this:

Serial2 and Serial3 for Adafruit Grand Central M4

The key paragraph is:

Serial2 and Serial3 instances aren’t defined in the Arduino board support for the Grand Central. However, the support for them appears to be there. I’m playing around with it to see if I can get them working.

It seems that in ARM uLisp 4.4 I only defined the Grand Central as having Serial1, but in 4.4d I changed it to support Serial2 and Serial3 too, but can’t have tested it.

Since it doesn’t look as if Adafruit are going to fix this I should probably revert uLisp to only supporting Serial1 on Grand Central, which is what it says in the uLisp documentation:

http://www.ulisp.com/show?2OH5#metro-m4-grand-central

If you feel brave you can try the fix described in the Adafruit forum!


#3

Hi David ,

Thanks , and sorry for my late reply.
I did some reading on the adafruit forum ( I never noticed it excisted) and found also another topic on this.
I tried one and it seems to work in arduino (not tested in ulisp so far)
I have now 4 serial ports.
I will do tests in ulisp this week to see if it works and also if i2c and spi (like sdcard) remain working because al serial devices are implemented on this so called ‘sercom’ interface. This is/was now to me. I’ll let you know the results in the days coming.

Regards,
Ronny


#4

Hi David ,
I was so brave to do some testing and found this solution.
First you have to change 2 files , variant.cpp and variant.h
In linux they are under :
.arduino/packages/adafruit/hardware/samd/1.7.14/variants/grand_central_m4
In variant.h near the end of the file you find extern Uart Serial1;
Just add after this line:

extern Uart Serial2;
extern Uart Serial3;
extern Uart Serial4;

In variant.cpp at the end of the file, add this:

Uart Serial2( &SERCOM_SERIAL2, PIN_SERIAL2_RX, PIN_SERIAL2_TX, PAD_SERIAL2_RX, PAD_SERIAL2_TX ) ;

void SERCOM4_0_Handler()
{
  Serial2.IrqHandler();
}
void SERCOM4_1_Handler()
{
  Serial2.IrqHandler();
}
void SERCOM4_2_Handler()
{
  Serial2.IrqHandler();
}
void SERCOM4_3_Handler()
{
  Serial2.IrqHandler();
}


Uart Serial3( &SERCOM_SERIAL3, PIN_SERIAL3_RX, PIN_SERIAL3_TX, PAD_SERIAL3_RX, PAD_SERIAL3_TX ) ;

void SERCOM1_0_Handler()
{
  Serial3.IrqHandler();
}
void SERCOM1_1_Handler()
{
  Serial3.IrqHandler();
}
void SERCOM1_2_Handler()
{
  Serial3.IrqHandler();
}
void SERCOM1_3_Handler()
{
  Serial3.IrqHandler();
}

Uart Serial4( &SERCOM_SERIAL4, PIN_SERIAL4_RX, PIN_SERIAL4_TX, PAD_SERIAL4_RX, PAD_SERIAL4_TX ) ;

void SERCOM5_0_Handler()
{
  Serial4.IrqHandler();
}
void SERCOM5_1_Handler()
{
  Serial4.IrqHandler();
}
void SERCOM5_2_Handler()
{
  Serial4.IrqHandler();
}
void SERCOM5_3_Handler()
{
  Serial4.IrqHandler();
}

Save and exit both files.

Now you can compile ulisp without errors about serial ports.

As you can see you can have 4 extra serial ports, but because I didn’t want to change the ulisp program, there will be 3 extra ports, being uart1, 2, and 3.

Because I wanted to be sure that other serial devices (being i2c and spi) kept on working , I wrote a small testprogram. For this you have to make a loopback on the 3 serial ports from tx to rx with a piece of wire. I will give the connections later on.

I also connected a RTC breakout board with a DS3231 real time clock on i2c port , and an sdcard . The testprogram writes a char to a serial port and reads it back. This char is being printed to the terminal. The char is 1 , 2 and 3 representing the number of the port. Then the sdcard is written to and read from and also printed on the terminal. At last the RTC is read and displayed on the terminal.
It all works fine !

Here’s the testprogram :

#|
*************************************** 
Functions to test all sercom devices
***************************************
|#

(defun pc (i) (princ (code-char (+ i (if (<= i 9) 48 55)))))

(defun pb (i) (pc (ash i -4)) (pc (logand i 15)))

(defun clock ()
  (let (hr min sec dow date mon yr)
    
    ;; Read the time from the RTC
    (with-i2c (str #x68)
      (write-byte 0 str)
      (restart-i2c str 7)
      (setq sec (read-byte str))
      (setq min (read-byte str))
      (setq hr (read-byte str))
      (setq dow (read-byte str))
      (setq date (read-byte str))
      (setq mon (read-byte str))
      (setq yr (read-byte str)))
    (pb hr)
    (princ ":")
    (pb min)
    (princ ":")
    (pb sec)
    (princ #\013)
    (pb date)
    (princ "/")
    (pb mon)
    (princ "/")
    (pb yr)
    (princ #\013)
    (princ #\013)
    (delay 250)))
 
(defun testsercom ()
  (loop
   (with-serial (str2 1 192)
     (princ "1" str2)
     (princ (code-char (read-byte str2)))
     (delay 50))
				
   (with-serial (str2 2 576)
     (princ "2" str2)
     (princ (code-char (read-byte str2)))
     (delay 50))
		
   (with-serial (str2 3 1152)
     (princ "3" str2)
     (princ (code-char (read-byte str2)))
     (delay 50))
		
   (with-sd-card (str "Greeting.txt" 2)
     (print "Hello" str))
   (delay 500)
   (with-sd-card (str "Greeting.txt")
     (print (read str)))
   (princ #\013)
   (delay 500)

   (clock)
   (princ #\013)))

The settings for the serial ports are : (numbers as printed on the pcb)
0 = RX serial1
1 = TX serial1

18=TX serial2 on sercom4
19=RX serial2

16=TX serial3 on sercom1
17=RX serial3

not used in ulisp but exist now :
14=TX serial4 on sercom5
15=RX serial4

Hoping this info is useful to other users,
Regards,
Ronny


#5

Here’s a photo where you can see the 3 loopback wires sdcard onboard and rtc breakout


#6

Great work - thanks! I’ve added some formatting to make the listings clearer - please check that it’s OK. David


#7

Thanks David. Looks great. Check ok.
Regards Ronny


#8

Hi Ronny, I’ve fixed this in Release 4.6 so it works without errors on the Grand Central M4, but I’ve added a note on the Adafruit M4 boards page directing people to this post if they want to add Serial2 and Serial3.