How I choose which pins to use on a PIC32MZ project
I got an interesting question in my comments yesterday asking how I go about choosing which pins to use when I do a PIC32MZ project. My answer is a bit long and I think it might be useful to other noobs like me so I'm releasing it as a post today. Please note: This isn't based on anything but my own experience with PIC32MZ projects, it is certainly not the only way to to things. It's just the way I do it myself.
Basically, I go through the following steps:
- List all peripherals I will need for the project
- Check which hidden secrets the peripherals have
- Check errata to make sure they all work
- Check which pins each peripheral needs
- Actually choose the pins
List all peripherals I will need for the project
In the example I was given, it was 2 x I2S (using an internally generated reference clock), 3 x UART, 2 x I2C and 2 x SPI on a 144-pin PIC32MZ EFG chip. Furthermore, while I2C and SPI peripherals can both be connected to multiple devices, in the interests of speed (and for the explanation) I'm going to connect each device to a separate SPI and I2C peripheral. The aim will be simple: to map these peripherals to GPIO pins in a way that makes layout easy.
Check which hidden secrets the peripherals have
All the peripherals have certain things that must be kept in mind when they are used. They're not really secrets at all but they may not be immediately obvious when you first set out to use them. For the example, the "hidden secrets" are as follows:
SECRET 1
I2C peripherals are fixed and cannot be moved. Furthermore, I2C SDA and SCL pins require a 4.7k pullup resistor in order for them to meet the I2C standard and work reliably.
SECRET 2
I2S either works off of PBCLK2 or off of REFCLKO1 because REFCLKO1 controls all SPI (which I2S is a subset of). It cannot just use any reference clock. For audio and the frequencies it uses, REFCLKO1 is much more useful. Furthermore, REFCLKO1 is set up by PPS, which further limits out options to the following:

SECRET 3
Although most pins for SPI peripherals can be moved around at will, the clock pins (SCK1 ~ SCK6) cannot be moved. Luckily, if we control the Chip Select (CS) pin of the device ourselves, we can gain a bit more flexibility.
SECRET 4
As I detailed in a post in the past, in order to extract 50Mhz from the SPI port you can only use SPI2 or SPI3 and even then only on specific pins, as follows:

Because of this, I'm going to be putting the SD card on SPI2 (but you could put the LCD there for 50Mhz too if you wish).
Check errata to make sure they all work
Unlike the 32MZ EC series, which was a horrible mess full of bugs, the EF series has relatively few errata. There is, however, one that looks concerning. It's listed as "The I2C module does not function reliably under certain conditions."
It turns out that this problem occurs when we use I2C at a rate of more than 100kHz and have constant I2C transfers of more than 500 bytes. The solution from Microchip, as it has been since the PIC32MX days, is to "bit bang". Which means to implement I2C by yourself and not use their peripheral. That's pretty poor.
In practice I haven't encountered this yet but it is something to bear in mind.
Amusingly, the UART has the following errata: "The UART automatic baud rate feature is intended to set the baud rate during run-time based on external data input. However, this feature does not function." Great work, Microchip. Great work.
Check which pins each peripheral needs
Straightforward stuff here. I'll list the ones relevant to my example.
Each SPI needs:
- MISO (SDI)
- MOSI (SDO)
- CS
- CLK
Each I2C needs:
- SDA
- SCL
Each UART needs:
- RX
- TX
Each I2S needs:
- MCLK (REFCLKO1)
- MOSI (SDO)
- SCK
- LRCK (for which I use Slave Select (SS))
Actually choose the pins
OK, now the part that we actually wanted to do. I like to keep the peripherals separated if possible to make layout easier as I routinely use double-sided boards. For this step I look at two parts of the datasheet, namely the pinout of the device and the PPS Input and Output sections. Bearing that in mind, for this example I'd use something like this:
First, on the one "side" of the PIC32MZ EF 144-pin devices, we have the RH and RK ports, which are totally useless from a PPS perspective. There are also no I2C peripherals on that particular side. However, there is a potential output for REFCLKO1 and three SPI peripherals, so I've put the two I2S ports there like this:
SPI3 (I2S):
- MOSI3 = RD14
- SCK3 = RB14
- SS3 = RF12 (used as LRCK)
- REFOCLK1 = RD15
SPI5 (I2S):
- MOSI5 = RB9
- SCK5 = RF13
- SS5 = RB8 (used as LRCK)
- REFOCLK1 = RD15
As noted earlier, I need two SPI peripherals and one of them must be on SPI2, using RB3 and RB5. There is one I2C peripheral on this same side. I've laid it out like this:
SPI2 (50Mhz):
- SCK2 = RG6
- MISO2 = RB3
- MOSI2 = RB5
- SS2 = RG9 (Chip Select)
I2C4:
- SDA4 = RG7
- SCL4 = RG8
We still need one more I2C and one more SPI peripheral, which I've done as follows:
I2C2:
- SDA2 = RA3
- SCL2 = RA2
SPI4:
- SCK4 = RD10
- MISO4 = RD11
- MOSI4 = RD0
- SS4 = RH12 [See below]
I've chosen RH12 as SS4 (or Chip Select) but it cannot be used as SS4 in the PPS setting. I've done this because I plan to use this for the LCD, which needs me to manually control the Chip Select (CS) line and there are also no convenient PPS mappings for SS4 nearby.
OK, mostly done now. We now need 3 UARTs, which I've done as follows:
UART3:
- U3TX = RD6
- U3RX = RD7
UART4:
- U4TX = RD4
- U4RX = RD5
UART5:
- U5TX = RF0
- U5RX = RF1
In the end, it looks like this in Eagle (click to zoom):
Hope this will be of use to someone :)


13 comments
32bits
Hi Adrian, just found your website, looks very interesting. I have made some projects wih PIC32MX795 and now want to move to a faster processor, mainly for larger TFT display type projects. Two possible choices include RPi and higher level programming or ARM and stay more bare metal and C programming. For the PIC32MZ and comparing to a PIC32, other than base clock speed, how much faster for PMD screen updating / writing would you expect the MZ to run? (as PIC32MX is 80mips and PIC32MZ is 330mips). If you don't want to make your own MZ PCB Mikroe have them, https://www.mikroe.com/easy... this can be used stand alone and also a nice PCB with a 100 pin MZ type which also as a prototype board that can be fitted, https://www.mikroe.com/clic..., just checked and both breakout the PMD pins or have access to them. Perhaps the most interesting question is for SPI displays and not having to use 16 way able for PMD, I have a 7" display working via SPI on a Teensy 3.6 and its very fast and only uses 4 wires to connect. To get a RA8875 display working in SPI with DMA from an PIC32MZ processor would be a worthwhile achievement.
Aidan Author
Hi there and welcome.
First off the 32MZ is much faster than the MX specifically because the chip runs at 200MHz vs 80MHz (or 120MHz for the 32MX470 series) It also has 512kB of RAM vs 128kB for the 32MX795 so it can store a decent sized frame buffer. As far as SPI and DMA I've gotten them to work at close to 50MHz.
It all depends on the resolution. If a frame buffer can fit in 512kB of RAM then it's not hard to get it to work, though SPI will be slower than the PMP, which I've gotten to work at about 100MHz with LCDs.
At any rate, for LCDs the Pi will be massively faster and better than the PIC32 and it even has a huge amount of RAM.
I have gotten 800x480 16-bit LCDs to work on the 32MZ via the Parallel Master Port but don't expect to do any high fps full screen updates (like video playback for example). For that, I'd recommend the Pi :)
32bits
I now have a MikroElectronica clicker 2 board which has an PIC32MZ 200Mhz
MCU and I plan to connect this via PMP over the holidays to a 5"
TFT, then compare to PIC32MX. The display is 5" chipset SSD1963
and has a resolution of 800 x 480, my programming environment is
MikroElectronica C for PIC32 which has the SSD1963 library included.
There is no frame buffer used so I suspect it is just sending one
pixel at a time to draw objects and update the screen, so is very
slow. Using a PIC32MX795 it's possible to use various tricks to
optimise screen updating and for low screen updating, it's kind of
OK. I suspect the PIC32MX will be just over x 2 faster, more feedback
when I make the tests. What would be useful to know is what TFT
chipset support and TFT sizes you have used for the MZ and are you using a Microchips Harmony lib or have you written your own TFT
driver(s) ?
Aidan Author
Oh yeah if you have an 800x480 display then the MZ doesn't have enough RAM for a whole frame :)
The LCD I use is actually a 7" 800x480 one driven by the SSD1963. I wrote my own (very simple) code which you can find on this site in the post on PMP and LCDs.
If you use the code you need to check which pin I used for chip select (CS) but it should work fine.
kfanayo
Hi Aidan, thanks for all your posts on PIC32MZ, they have been very helpful. I'm currently working on a PIC32MZ2048EFG144 and i'm attempting to set it up as a USB device. I'm not having much progress with it, can you please post an example if you have worked on such? Thanks a lot.
Aidan Author
Hi there kfanayo,
I don't have any examples of using it as a USB device but the examples in Harmony worked fine when I tried them with my board. Are you trying to make an HID device like a keyboard? If so, I did have trouble with that too. Are you using a Microchip board or a different one?
kfanayo
Hi, i'm using a custom board with the PIC32 microcontroller and i am trying to make a custom HID device. The goal is to stream live data to another PC app.
Aidan Author
If it's your own board you need to be careful about the frequency crystal you use (and you have to use a crystal to get USB to work). You need to be able to divide the crystal frequency down to 4MHz (if I recall correctly) so the USB can get to 48MHz.
Secondly, have you tried the Harmony examples for USB device? The COM port CDC example usually works for me.
kfanayo
Thanks Aidan. I'll check the crystal settings and try out the Harmony example.
Henry Laredo
Hi Aidan, I want to thank you for this information, it helped me a lot because I was using the I2S but I didn“t notice about the REFCLKO1, and I selected in my board the REFCLKO3 to work with I2S2 in order to work with the codec AK4953, hopefully your post show me the correct way and now it is solved and working fine! PD: I'm using the PIC32MZ2048EFM100 with Harmony V3 and FreeRTOS. Good bye and greetings from Lima PERU.
Aidan Author
Hi Henry, thanks for stopping by and leaving a comment to say it was useful. The PIC32MZ has many nasty traps like this :)
rmoshiri
Hi Aidan, very nice website and great info on PIC32MZ.
I have a DM320010 EVB and a working "Harmony-2" project.
I need to add I2C. Any examples, references or word of advice you could share.
AlPl PaL
Hi Aidan, you did incredible work altogether! Choosing the pins is indeed challenging, especially if you want to connect an LCD to the PMP where the necessary pins are scattered all around the device and several times also "crossed" (not consecutive order but PMD14-PMD15-PMD12-PMD13 etc) which makes life difficult on 2-layer PCBs. Also most devices (PIC18-dsPIC33-PIC32) do not have consecutive port-PIN-positions (eg RB0-15, etc) - you will find them in many different positions around the device. I do not know why Microchip did that (technical reason or a large customer needed this configuration?) but this causes a lot of problems during the routing.
Comments are hosted by Disqus, which loads only when you ask for it.