Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Building an RPi4 HAT - how to use the serial port

Tue Nov 05, 2019 10:32 am

I am now going to design a HAT PCB, which will plug into the GPIO pins (2x20 connector).
I will use this for a project where I have used an USB/RS232 converter to implement the RS232 serial connection during prototyping.
But now since I am making the PCB I thought that the built-in serial port would be a better solution since it should be available on the GPIO pins somewhere (GPIO14-15?). It will be used at 38400 or 19200 baud. No extra bulky USB cable needed.

Questions to make sure I have it right:
1) Are the pins to use GPIO14 (TxD) and GPIO15 (RxD)?
It seems so in an old RPi pin chart I have.

2) What is the Linux device name for this serial port? Needed for the software setup.
I have found a no less than 64 /dev/ttyxx where xx is a number 0..63 plus tty, ttyAMA0 and ttyprintk...
Confusing.

3) Do I have to do something special to enable the serial port/disallow other use of it?
For example I have read that Linux uses a serial port for terminal data during startup, is this on the built-in port?

4) Is the signal level 3.3V digital on these pins?
Probably so, right? I plan on using a MAX3232 level converter for RS232. It converts to/from 3.3 V.
Bo Berglund
Sweden

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Building an RPi4 HAT - how to use the serial port

Tue Nov 05, 2019 10:47 am

Bosse_B wrote:
Tue Nov 05, 2019 10:32 am
I am now going to design a HAT PCB, which will plug into the GPIO pins (2x20 connector).
I will use this for a project where I have used an USB/RS232 converter to implement the RS232 serial connection during prototyping.
But now since I am making the PCB I thought that the built-in serial port would be a better solution since it should be available on the GPIO pins somewhere (GPIO14-15?). It will be used at 38400 or 19200 baud. No extra bulky USB cable needed.

Questions to make sure I have it right:
1) Are the pins to use GPIO14 (TxD) and GPIO15 (RxD)?
It seems so in an old RPi pin chart I have.

2) What is the Linux device name for this serial port? Needed for the software setup.
I have found a no less than 64 /dev/ttyxx where xx is a number 0..63 plus tty, ttyAMA0 and ttyprintk...
Confusing.

3) Do I have to do something special to enable the serial port/disallow other use of it?
For example I have read that Linux uses a serial port for terminal data during startup, is this on the built-in port?

4) Is the signal level 3.3V digital on these pins?
Probably so, right? I plan on using a MAX3232 level converter for RS232. It converts to/from 3.3 V.


Relevant Raspberry Pi documentation here: https://www.raspberrypi.org/documentati ... on/uart.md

Current header pinout documentation here: https://pinout.xyz/

Raspberry Pi HAT documentation: https://www.raspberrypi.org/blog/introd ... y-pi-hats/
https://github.com/raspberrypi/hats

Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Re: Building an RPi4 (HAT) - how to use the serial port

Tue Nov 05, 2019 3:02 pm

Thanks for the reply, I just got back home to read it...

So, I did not know that "HAT" is a term defining a specific type of add-on board including an identification EEPROM on board for the operating system to use to set up drivers and such....
Since I am not intending for my board to ever be sold separately I will not use the HAT term from now...

But I still want to make the interface board such that my software can use it including the serial port.

I also forgot that I had in mind to put an RTC circuit on the board too since the system will probably be running without Internet connectivity. I intend to provide a mobile broadband modem via USB to the system, but lack of phone coverage might mean that it will not have somewhere to sync up time with after startup. So an RTC module is also needed, hopefully I can integrate such on the board as well. The measurements are in need of fairly accurate time.

Will continue reading about the serial port problems...
Bo Berglund
Sweden

Bosse_B
Posts: 981
Joined: Thu Jan 30, 2014 9:53 am

Re: Building an RPi4 HAT - how to use the serial port

Thu Nov 07, 2019 5:20 pm

After looking around a bit on the net I have reached the conclusion that I should add an EEPROM to the board to act as the ID memory.
New questions regarding this:

1) Is a 4K EEPROM sufficient?
We have such a part in use on other boards already so it would be good to reuse the same part on the RPi board too (the other boards are standalone PIC microcontroller boards). Or does the RPi HAT ID EEPROM require more storage than 4 kBytes?

2) Do we need to "register" with the Pi Foundation or similar to be able to use an ID EEPROM on a HAT designed board?
Apparently one needs a "Vendor ID" embedded in the data....

3) Is there a "cook-book" style Howto for board designers on how to configure the data in an ID EEPROM?
Like an "RPi HAT ID creation for Dummies"...

4) Is there a way to program the HAT ID chip on board when connected to an RPi through the header connector or does one have to do that using some kind of fixture accessing the I2C pins in a different way than from pins 27/28?

My board will comprise the following items:
- A TTL/RS232 level shifter for the RPi serial port on GPIO14-15 so we can avoid using USB-Serial modules in the design.
- Two or three 5V relays activated through GPIO pins and FET drivers.
- An RTC chip (DS3231M) with a battery connected to I2C SDA/SCL on pins 3 and 5 (GPIO2-GPIO3)

At least the RTC chip and the serial port should be recognized by Raspbian on startup via the HAT ID so that time setting and serial port configuration are correctly handled based on the HAT ID memory content.
Bo Berglund
Sweden

trejan
Posts: 2134
Joined: Tue Jul 02, 2019 2:28 pm

Re: Building an RPi4 HAT - how to use the serial port

Thu Nov 07, 2019 6:11 pm

Bosse_B wrote:
Thu Nov 07, 2019 5:20 pm
1) Is a 4K EEPROM sufficient?
We have such a part in use on other boards already so it would be good to reuse the same part on the RPi board too (the other boards are standalone PIC microcontroller boards). Or does the RPi HAT ID EEPROM require more storage than 4 kBytes?
4096x8 is the minimum but you should use one of the types listed in https://github.com/raspberrypi/hats/blo ... ash.sh#L21
Bosse_B wrote:
Thu Nov 07, 2019 5:20 pm
2) Do we need to "register" with the Pi Foundation or similar to be able to use an ID EEPROM on a HAT designed board?
No. https://www.raspberrypi.org/forums/view ... 13#p590956
Bosse_B wrote:
Thu Nov 07, 2019 5:20 pm
3) Is there a "cook-book" style Howto for board designers on how to configure the data in an ID EEPROM?
Like an "RPi HAT ID creation for Dummies"...
https://github.com/raspberrypi/hats and the tools in https://github.com/raspberrypi/hats/tre ... epromutils
Bosse_B wrote:
Thu Nov 07, 2019 5:20 pm
4) Is there a way to program the HAT ID chip on board when connected to an RPi through the header connector
Yes. https://github.com/raspberrypi/hats/blo ... epflash.sh
Bosse_B wrote:
Thu Nov 07, 2019 5:20 pm
My board will comprise the following items:
- A TTL/RS232 level shifter for the RPi serial port on GPIO14-15 so we can avoid using USB-Serial modules in the design.
- Two or three 5V relays activated through GPIO pins and FET drivers.
- An RTC chip (DS3231M) with a battery connected to I2C SDA/SCL on pins 3 and 5 (GPIO2-GPIO3)

At least the RTC chip and the serial port should be recognized by Raspbian on startup via the HAT ID so that time setting and serial port configuration are correctly handled based on the HAT ID memory content.
You'll need to setup the GPIO map in the EEPROM and add a suitable devicetree blob as well.

Return to “General discussion”