/RPi-P2D2-Support

Useful information for using P2D2 (parallax Propeller V2) with Raspberry Pi

Primary LanguagePythonGNU General Public License v3.0GPL-3.0

RPi-P2D2-Support - Quickly learn how to configure your RPi for use with your P2

Project Maintenance

GitHub Activity

License: GPL v3

Useful information for using P2D2 (parallax Propeller V2) with Raspberry Pi.

I'm continually adding to this document as I "Learn things."

As you likely already know Peter Jakacki's P2D2 Boards are arriving with a 3rd header, matching the Raspberry Pi GPIO pinout, outboard of the two P2D2 headers already present. This allows the P2D2 to be plugged into the Raspbery Pi just like many other RPi Hats.

The P2D2 Plugs into the RPi GPIO Header and provides the following connections:

The Hardware

Pins that are connected to the RPi

By plugging the P2D2 onto the RPi header the board is immediately connected to a number of useful interfaces and a new world of opportunity.

RPi Interface # of Pins Purpose P2 Pins
I2C(1) 2 pins SDA, SCL P0, P2
1-wire 1 pin 1-wire P4
UART(0) 2 pins TxD, RxD P1, P3
PCM 1 pin? PCM CLK P5
SPI(0) 5 pins MOSI, MISO, SCLK, !CE0, !CE1 P12-P16
I2C ID 2 Pins ID_SC, ID_SD P17, P20
GPIOs 15 pins Unclaimed I/O P6-P11, P19, P21-P26, P28, P30

Diagram of connections to RPi

Here is the header as shown in Peter's P2D2 r5 documents:

Pinout

Things we are learning:

RPi Interface max clock freq
I2C(1) 400 KHz
1-wire ~16.3 Kbps (163 Kbps overdrive mode) bps because not standard clock! NOTE WikipediA
UART(0) PL011 3 MHz NOTE forum thread
UART(0) miniUART ~31.25 MHz (acutal baud lower than expected and is based on VPU clock so it can be affected by temperature throttleing or overclocking) NOTE forum thread
SPI(0) 125 MHz
I2C ID 400 KHz

I'll keep adjusting this table as I learn more...

Needed Reference As you are adjusting configuration of your RPi you will frequently find yourself using the raspi-config tool. The best reference I've found for this is: The raspi-config Tool

Likewise as you are wandering the varous interfaces looking for more details checkout pinout.xyz an interactive GPIO header with descriptions of each interface.


If you like my work and/or this has helped you in some way then feel free to help me out for a couple of ☕'s or 🍕 slices!

coffee


The Raspberry Pi (RPi)

Raspberry Pi's are fairly inexpensive devices and all software for them, operating system, applications are free. RPi's such at the RPi4 with its 8GB ram is touted to be a nearly desktop class linux machine. It is well connected, offering WiFi, wired ethernet, and bluetooth interfaces, USB 2.0 and 3.0. It has wonderfully high resolution (up to 4k 60fps) HDMI output and it has world class open source data analysis and visualization tools available at no cost (open source.)

There many different models of the Raspberry Pi. It can be hard to know what capabilities you have on a specific instance of RPi that you may have.

Here are two sources i like to use when I need more detail on one of mine:

The WikipediA page: Raspberry Pi

and the FAQ Rasperrypi.org page: FAQs which leads me to many more useful details.

Configuration Opportunities

We will continually be identifying fun and unique ways to use this hardware pairing. Here i'm starting a list of obvious opportunities I see I'm studying all connections for this pairing.

This will be an ever growing list of things that are possible now with a bit of configuration of the PRi and maybe some lightweight code for the P2 or the RPi:

  • Debug console while running apps on P2D2.
  • Root access console to the RPi for apps running on the P2D2.
  • Boot time ID ROM emulation so the RPi can do HAT (Hardware on Top) runtime configuration of drivers.
  • ... many more tba ...

Then there are things we'd like to do but have yet to figure out how:

  • Boot-time access to TAQOZ on the P2 from the RPi - would allow us to debug P2 attached hardware using TAQOZ.
  • ... more as we find ...

Hardware Interaction leading to configuration Choices

As with any embedded system with many I/O choices sometimes tradeoffs have to be made to use certain capabilities (to get one to operate maybe you have to disable another, etc.) I put this README together so I could annotate here for us all any such tradeoff issues I find. So far, the Serial port is the one where we have more choices to make. The following sections (one for each interface) provide any details I've found to date.

NOTE: the number of SPI, I2C, or UARTs you have depends upon the model of Broadcom SoC chip used on your RPi [BCM2711, BCM2835, BCM2836, BCM2837A0, BCM2837B0, etc.]. Go to The computer hardware section of the RPi FAQ to determing which you have!

Or, have a little fun with entering this on your RPi in question:

cat /proc/cpuinfo | grep Hardware

(your RPi whill tell you which BCM chip it has!)

NOTE: Turn on Interfaces you need. The hardware interfaces at the GPIO connector are generally turned off by default. You will want to use raspi-config to enable each of the interfaces you wish to use.

Language Choices: I tend to use Python or ANSI C for interacting with the various devices as there are a lot of rich examples and library support. However there are a number of other languages you could choose to use.

NOTE: While most software including Python itself is installed using apt-get install {package} Some of the lesser used Python packages will need to be installed using using pip(3) install {package} as they may not be prepackaged for apt-get installation.

Interface: I2C0

Enable the I2C interface using raspi-config. From what I can find, by default the I2C clock is 100KHz. This can be adjusted up to a max of 400KHz. I1 Also, there are posts that suggest that I2C on the RPi can only work at these two frequencies.I2

There are

By default some useful I2C tools are not installed. Install them with:

sudo apt-get update
sudo apt-get install i2c-tools

The most commonly used Python library appears to be SMBus which is installable on our RPi's using sudo apt-get install python3-smbus. The project website has examples of use and there are many other open source projects using it as well which serve as good examples.

I2C Clock Speed

we modify the I2C clock speed by adjusting values in the in /boot/config.txt file. Look for:

dtparam=i2c_arm=on

and add a new line setting the desired clock speed:

dtparam=i2c_arm=on
i2c_arm_baudrate=400000

This selects our new 400Kb/s rate.

Please remember that after adjusting these /boot/ files your changes do not take affect until you reboot the RPi.

I2C Enabled Check

It is easy to tell if you already have the I2C interface enabled using a simply command:

ls /dev/i2c*
# which yields somthing like (only if the driver is loaded):
crw-rw---- 1 root i2c 89, 1 Sep  1 13:17 /dev/i2c-1

NOTE: Our I2C0 interface is /dev/i2c-1.

Example, working Python

Dual I2C and SPI Python Library for AS3935 I have this code running for my lightning detector reporting script. Part of my Lightning Detector MQTT2HA Daemon project

Interface: 1-Wire

Enable the 1-wire interface using raspi-config. There are options for enabling a pull-up on the GPIO pin but most people just wire up their own external resistor. The driver should default to the GPIO4 pin but it never hurts to double check. O1

Interface: Serial: UART0

There are two hardware UARTs: miniUART and PL011 that are configured to be primary and secondary on RPi3 and RPI4. (earlier models are different!) U1 and four additional UARTs [2-5] U2.

MiniUART (less capable, but fully meets our normal debugging needs)

The miniUART is configured to be the primary and is by default routed to UART0 (at the GPIO connector), it is disabled but is configured, when enabled, to be a serial console with full command line access. The clock for this miniUART is derived from the CPU clock so it can be affected by temperature throttleing or overclocking. (It appears on device files: /dev/serial0, /dev/ttyS0)

Considerations for use:

  • This MiniUART is easily capable of our P2 deafult 2MBit 8N1 configuration (it can go higher)
  • We need to fix the cpu clock fequency so it can not cause our miniUART to change frequencies while we are using it.
  • We need to set the input clock frequency to the miniUART to get to our 2Mbit rate

PL011 UART (more capable)

The second and more capable UART is the PL011 UART. This is configured to be the Bluetooth LE serial transceiver and is the secondary serial device. (It appears on device files: /dev/serial01, /dev/ttyAMA0)

Considerations for use:

  • The PL011 UART is also capable of our P2 deafult 2MBit 8N1 configuration (it too can go higher)
  • If we want to use this UART as UART0 then we need to flip the miniUART to handle the Bluetooth LE device and PL011 to be the primary connecting it to UART0 at our GPIO header. Alternatively, we can just disable the bluetooth to cause this to happen as well.

Serial Boot-time Configuration

Two files are involved in configuration of serial port use: /boot/config.txt and /boot/cmdline.txt

As an RPi user it is probably cleaner to make as much configuration change as you can using 'sudo raspi-config' utility. With this utility you can enable the primary serial port (disabled by default) and you can disable the serial console use of the primary serial port.

You will need to fix clock frequency (and/or configure which UARTs do what) by modifying the files directly. You can modify these files by hand using 'vi' or 'nano' as your editor but this is slightly more error prone so we want to be very deliberate here. (You don't want typo's or mis-spellings in these files as stuff just doesn't work right where there are...)

To get to our 2Mb/s we need to adjust the clock fed to the UART. The setting needs to be at least 16X faster than the baud rate we want for our uart. The default is only 3MHz which results in just a little over 115200. So at the default setting, the max UART baud rate is 115200. U3

Therefore by adding

init_uart_clock=32000000

to /boot/config.txt we can now select a 2Mb/s rate. (yes, testing shows this works!)

Please remember that after adjusting these /boot/ files your changes do not take affect until you reboot the RPi.

Interface: SPI0

Enable the SPI interface using raspi-config. From general information found at raspberrypi.org S1 we see that there are three SPI controllers but only SPI0 is available at our header. We can configure the SPI mode (Standard, Bidirectional and LoSSI.) The driver supports a number of SCLK speeds but this has changed over time.S2 There is a description of the 2nd SPI1 device appearing on the 40-pin header. S3

The most commonly used Python library appears to be Spidev which is installable on our RPi's using sudo apt-get install python3-spidev. The project website has examples of use and there are many other open source projects using it as well which serve as good examples.

SPI Enabled Check

It is easy to tell if you already have the SPI interface enabled using a simply command:

ls /dev/spi*
# which yields somthing like (only if the driver is loaded):
crw-rw---- 1 root spi 153, 0 Aug 31 18:23 /dev/spidev0.0
crw-rw---- 1 root spi 153, 1 Aug 31 18:23 /dev/spidev0.1

NOTE: Our SPI0 interface is /dev/spidev0.0.

Example, working Python

SPI is bit of a different animal on Python as you have to construct the read-write primitives for your device. Her are some of my notes: Theory of Operations: SPI for AS3935

Dual I2C and SPI Python Library for AS3935 I have this code running for my lightning detector reporting script. Part of my Lightning Detector MQTT2HA Daemon project

Interface: I2C SD/SC (Hat ID ROM)

... TBA ...

Interface: Non-tasked GPIOs

If you are using the remaining 15 non-purposed GPIOs then when you decide the purpose and configuration needed for a pin you can set a boot-time configuration entry so the pin you need will be configured correctly from boot. Entries to do this are placed in /boot/config.txt G1

Special Setup Notes

Run RPi's remotely using VNC

To set up your raspbery pi to display it's desktop remotely there are a couple of adjustments you'll want to make:

  1. Enable VNC (SSH too if you use it)
  2. If no display attached, config RPi for headless operation
  3. set display resolution as desired I use
  4. if using RP4 disable special driver blocking screen resizing [^6]

The first (1) enable VNC(SSH), (2) enable headless mode and (3) set resolution are handled in raspi-config:

  • sudo raspi-config
  • arrow down to "Interfacing Options" and press return
  • arrow down to "P2 SSH" and press return
  • TAB so that < YES > is selected and press return
  • press return for confirmation < OK > and you're back at the main screen
  • arrow down to "Interfacing Options" and press return
  • arrow down to "P3 VNC" and press return
  • TAB so that < YES > is selected and press return
  • press return for confirmation < OK > and you're back at the main screen
  • arrow down to "Advanced Options" and press return
  • arrow down to "A5 Resolution" and press return
  • Arrow down to "DMT mode 82 1920x1080 60Hz 16:9" (or select the mode you want) and press return
  • press return "OK" on confirmation screen (you should arrive back at the main screen)
  • press TAB, TAB to select < finish > and press return

(now your RPi will reboot if changes were made.)

For (4) I found that on RPi4's there's a video driver enabled by default which interferes and is enabled in /boot/config.txt V1:

[pi4]
# Enable DRM VC4 V3d driver on top of the dispmanx display stack
droverlay=vc4-fkms-v3d
max_framebuffers=2 

I commented out all of these lines and my RP4 can now display a larger screen when run by VNC.

Detailed I/O Reference for your Raspberry Pi

Much more detailed information on each of the interfaces can be found in the ARM Peripherals document for your model of RPi.

The various hardware models of the RPi have different Broadcom SoC's providing the I/O peripheral set. You can identify which your RPi is built with by doing:

cat /proc/cpuinfo | grep Hardware

Many of the latest models use the BCM2835. Here's a link to the BCM2835 ARM Peripherals SoC Document

If this isn't the chip that your RPi contains, doing a simple search for your BCM#### will quickly get you to the same doc for your RPi model.


FOOTNOTES

(click the return arrow to get back to the referencing text)

[I1]: Raspberry Pi Spy website: Change Raspberry Pi I2C Bus Speed this shows /boot/config.txt changes to make to affect I2C master clock.

[I2]: Raspberry Pi Forums: Raspberry Pi3 I2C baud rate setting (Scroll down to Sat Aug 04, 2018 8:33 am post.)

[O1]: Raspberry Pi Forums: 1-Wire Setup Questions various answers but the raspi-config method is the easiest and configures the extra dirver loading, etc.

[U1]: Raspberry Pi Documentation: UARTs

[U2]: Raspberry Pi Documentation - UART configuration overlays. Scroll down to: "UARTs and Device Tree"

[U3]: Raspberry Pi Forums: Can the UART go faster than 115200? Lot's of repeat information in here along with the details we need.

[S1]: Raspberry Pi Documentation: SPI

[S2]: Raspberry Pi Forum Thread: More SPI Speeds

[S3]: eLinux.org Raspberry Pi Page: RPi SPI

[G1]: Raspberry Pi Documentation: GPIO Control in config.txt read this to learn entry needed for each of the 15 GPIO pins you want to boot-time configure.

[V1]: Raspberry Pi Forums: Set VNC resolution? (Scroll down to Thu Jan 04,2018 10:20pm post.)


Credits

...TBD...


Disclaimer and Legal

Raspberry Pi is registered trademark of Raspberry Pi (Trading) Ltd.

Parallax, Propeller Spin, and the Parallax and Propeller Hat logos are trademarks of Parallax Inc., dba Parallax Semiconductor

This project is a community project not for commercial use.

This project is in no way affiliated with, authorized, maintained, sponsored or endorsed by Raspberry Pi (Trading) Ltd. or any of its affiliates or subsidiaries.

Likewise, This project is in no way affiliated with, authorized, maintained, sponsored or endorsed by Parallax Inc., dba Parallax Semiconductor or any of its affiliates or subsidiaries.