PIC32MZ without Harmony

Register-level C for Microchip's PIC32MZ, with the reasoning behind every setting: clocks, UART, SPI, DMA, audio, SD cards and USB, from first boot to a working device.

Start with the first post All 41 posts by date

Calculators: clock UART SPI I2C timer

A green PIC32MZ dev board with yellow pin headers, an SD card slot, a USB port and an ESP32 module, labelled Scorpio v2.0 by Aidan Mocke
The Scorpio dev board, January 2019

The guide, in reading order

Basics

4 posts

Why to skip Harmony, the configuration fuses, a program skeleton and core timer delays.

  1. A world without Harmony

    Why this blog exists: plain-register PIC32MX and PIC32MZ examples for hobbyists who want to avoid PLIB, Harmony and expensive dev boards.

  2. Configuration without Harmony 1

    How to set the PIC32MZ PLL configuration fuses, peripheral bus clock dividers, prefetch and cache to run at 200 MHz without Harmony, line by line.

  3. A PIC32MZ program skeleton

    A reusable PIC32MZ program skeleton covering the usual startup traps: ANSEL analog defaults, JTAG stealing port A pins, and watchdog and debug fuses.

  4. Delays on PIC32MZ

    How to write millisecond and microsecond delay functions using the PIC32MZ Core Timer, with no timer peripheral setup, then blink an LED.

UART and PPS

1 post

Mapping a UART with Peripheral Pin Select and sending printf output over serial.

  1. UART and Peripheral Pin Select (PPS) on PIC32MZ

    How to map UART5 to pins with Peripheral Pin Select, calculate the baud rate from PBCLK2, and redirect printf by overriding _mon_putc.

SPI, SD cards, FatFs

2 posts

SPI at 50 MHz, then reading files from an SD card with FatFs.

  1. SPI on the PIC32MZ

    How SPI works and how to set up PIC32MZ SPI2 at 50 MHz, covering PPS pins, the baud rate formula and Enhanced Buffer mode.

  2. FATFS on the PIC32MZ

    Step by step setup of the FatFs library with an mmcpic32.c SPI driver to read files from an SD card on the PIC32MZ.

PMP and LCDs

1 post

The Parallel Master Port driving a 16-bit SSD1963 LCD.

  1. PMP and LCDs on the PIC32MZ

    How to configure the Parallel Master Port in master mode 2 and use it to drive a 16-bit SSD1963 LCD and draw an RGB565 image.

Timers, interrupts, PWM

2 posts

Timers, prescalers and interrupt handlers, then hardware PWM for a hobby servo.

  1. Timers and Interrupts on the PIC32MZ

    How PIC32MZ timers, period registers and prescalers work, and how to write a multi-vectored Timer 2 interrupt handler that toggles a pin at 1 kHz.

  2. Pulse Width Modulation (PWM) and servos on the PIC32MZ

    How to generate hardware PWM with the PIC32MZ Output Compare module and Timer 2, and use it to position a hobby servo motor.

Audio: PWM, MP3, I2S

4 posts

Playing sound with PWM, decoding MP3s with Helix, and an I2S DAC for comparison.

  1. Using Pulse Width Modulation (PWM) to play audio

    How to play 44.1 kHz 8-bit audio through a PWM Output Compare channel driven by a timer interrupt, and smooth it with RC low-pass filters.

  2. Playing MP3s using PWM audio

    How to decode SD card MP3 files with the Helix decoder and play them in stereo through two PWM channels using a circular buffer.

  3. The difference between PWM and an I2S DAC

    A short preview comparing oscilloscope captures of PWM audio, RC-filtered PWM audio and a CS4334 I2S DAC output.

  4. I2S and Reference Clocks on the PIC32MZ

    How to drive a CS4334 DAC over PIC32MZ I2S, using Reference Clock Output 1 for MCLK and the SPI baud rate generator for SCLK.

I2C

1 post

The I2C protocol and a working master driver, tested on an MPU-9250.

  1. I2C on the PIC32MZ

    How the I2C protocol works and how to write I2C1 master start, stop, read and write functions on the PIC32MZ, tested on an MPU-9250.

DMA

4 posts

Coherent buffers, IRQ-triggered transfers and pattern matching, building up to fast SD card reads.

  1. Working on DMA

    A progress note on DMA-based SD card reads, with a speed comparison chart and early mmcpic32_dma.c code reaching about 5 MB/s.

  2. Direct Memory Access on the PIC32MZ

    An introduction to PIC32MZ DMA: coherent buffers, virt_to_phys addresses, IRQ-triggered transfers to the PMP, completion interrupts and pattern matching.

  3. DMA SD card reads on the PIC32MZ

    How to read SD card blocks over SPI using two PIC32MZ DMA channels, with pattern matching for the 0xFE token and a state machine.

  4. Updated SPI SD DMA code and DMA Pattern Matching

    An updated, more stable SPI SD card DMA driver with board settings in mmcpic32.h, a read callback, and notes on pattern matching leftovers.

SQI

1 post

The SQI peripheral reading and writing a PSRAM in single and quad-lane mode.

  1. SQI on the PIC32MZ

    How the PIC32MZ SQI peripheral and its buffered registers work, with examples reading ID and writing data to a PSRAM in single and quad-lane mode.

ADC

2 posts

Setting up and triggering ADC inputs, and which inputs share a converter.

  1. ADC on the PIC32MZ

    How the PIC32MZ ADC input classes work and how to set up and software-trigger class 1 inputs AN0 to AN2, step by step.

  2. Shared pins on the PIC32MZ's ADC

    A caution that PIC32MZ ADC inputs AN45 to AN49 share ADC0 to ADC4 with AN0 to AN4, so each pair cannot be sampled together.

Hardware and pins

2 posts

Choosing pins for a project, and the dev board built from those choices.

  1. My PIC32MZ Dev Board

    A tour of the author's 100 mm PIC32MZ dev board with its peripherals, PCB ordering tips, bill of materials, Gerber and Eagle files.

  2. Choosing which pins to use on a PIC32MZ project

    A worked method for assigning PIC32MZ pins for I2S, SPI, I2C and UART peripherals, checking fixed pins, PPS options and errata first.

USB device

10 posts

The USB module from first principles to CDC serial, mass storage and a HID joystick.

  1. USB

    Downloadable Harmony-free USB CDC virtual COM port code for the PIC32MZ, with a descriptor fix and the reference sources used.

  2. Initialising the USB peripheral

    How to initialise the PIC32MZ USB peripheral in High Speed device mode, handle the reset interrupt, and configure endpoints, FIFOs and transfer types.

  3. USB descriptors and how to send and receive data on endpoint 0

    How to receive setup packets and send device, configuration and string descriptors on USB endpoint 0, including byte-wide FIFO access.

  4. Finishing the USB CDC device

    How to finish a PIC32MZ USB CDC device: handling endpoint 0 requests, setting the address, and echoing data over bulk endpoint 2.

  5. USB progress update

    A progress note on a working USB HID keyboard and an SD card USB mass storage device reaching about 3.8 MB/s reads.

  6. USB MSD code

    Downloadable PIC32MZ USB mass storage device code that exposes an SPI-connected SD card to the PC, with the pin settings to change.

  7. How to make a USB Mass Storage Device part 1

    A reference to the SCSI commands, CBW and CSW structures, and Request Sense error handling needed for a USB mass storage device.

  8. How to make a USB Mass Storage Device part 2

    How to implement USB mass storage Read(10) and Write(10) with multi-block SD card transfers, double buffering and USB DMA for faster speeds.

  9. USB HID joystick: it works

    A short announcement that the author's PIC32MZ USB HID joystick works, with KiCad files and source code promised in a few days.

  10. USB HID joystick: schematic, code and KiCad files

    The schematic, board render, C source code and KiCad files for the author's PIC32MZ USB HID joystick.

USB host

4 posts

Running the USB module as a host, from endpoint 0 setup packets to a HID keyboard.

  1. Using the PIC32MZ EF USB module in host mode

    How to start the PIC32MZ USB module in host mode, catch device connection, reset the device and exchange setup packets on endpoint 0.

  2. USB HID Host

    A short announcement that the PIC32MZ USB peripheral now works in host mode with an HID keyboard, without any Harmony code.

  3. USB Host HID Keyboard

    Harmony-free PIC32MZ USB host code that reads an HID boot keyboard, handles shift, caps lock, LEDs and key repeat, and outputs to UART4.

  4. Using USB Endpoint 0 in host mode and how to send handshakes and request packets

    How to use the USBE0CSR0 handshake bits in USB host mode to send setup packets, request data and complete status stages on endpoint 0.

Updates and rants

3 posts

Site updates and one earphone review.

  1. Update and posts to come

    A site update showing the author's new PIC32MZ dev board and listing planned posts on interrupts, PMP, PWM audio, I2S, I2C, MP3 and USB.

  2. Powerbeats Pro Rant / Review

    A user review of the Powerbeats Pro paired with a Galaxy Watch, describing volume resets, unwanted reconnections and weak Bluetooth range.

  3. A quick update!

    A short site update after a 10-month break, explaining the slowdown and promising replies to comments and some new schematics.

Set the clock in five fuses

Every PIC32MZ project starts here: an input clock, a divider, the PLL multiplier and an output divider. Configuration without Harmony explains each line. There are more calculators for baud rates and timers.

200 MHz

24 MHz ÷ 3 = 8 MHz into the PLL, × 50 = 400 MHz, ÷ 2 = 200 MHz.

Inside every limit of the PIC32MZ EF.

#pragma config FPLLIDIV = DIV_3         // 24 / 3 = 8 MHz
#pragma config FPLLRNG = RANGE_5_10_MHZ // For 8 MHz
#pragma config FPLLICLK = PLL_POSC      // POSC feeds the PLL
#pragma config FPLLMULT = MUL_50        // 8 x 50 = 400 MHz
#pragma config FPLLODIV = DIV_2         // 400 / 2 = 200 MHz
#pragma config POSCMOD = EC             // External clock

Built along the way

The underside of the green Scorpio board, with a white scorpion drawn on the solder mask
Scorpio dev board. A PIC32MZ2048EFH144 with an ESP32, SD card, USB and audio, crammed onto 100 by 100 mm because that is the cheapest size to order. My PIC32MZ Dev Board
A red PIC32MZ dev board with yellow pin headers, an SD card slot, a USB port and audio jacks
Libra dev board. A battery charger, LCD port, audio DAC, 8 MB of SPI SRAM, SD card and USB on one board, made to write these posts on. Update and posts to come
A 7-inch LCD beside the red dev board, showing a picture of Tux the penguin
7-inch LCD. An 800 by 480 SSD1963 panel driven 16 bits at a time through the Parallel Master Port. PMP and LCDs on the PIC32MZ
A grid of oscilloscope captures comparing raw PWM audio, PWM through a two-stage RC filter and a CS4334 DAC
PWM against I2S. The same sound on the scope three ways: raw PWM, PWM through a two-stage RC filter, and a CS4334 DAC over I2S. The difference between PWM and an I2S DAC
An oscilloscope capture of an I2C clock line and data line
I2C on the scope. A working I2C master, checked against an MPU-9250 motion sensor and the bus on the scope. I2C on the PIC32MZ
A render of a small green circuit board with a PIC32MZ, a USB-C socket and rows of button headers
USB joystick. A PIC32MZ that a PC sees as a game controller, with its schematic, C source and KiCad files. USB HID joystick: schematic, code and KiCad files

Written by Aidan Mocke

The red Libra dev board on a desk under a 7-inch LCD showing a bar graph, with a small lithium battery wired in

Why no Harmony

I wrote these posts from 2018 to 2024, because nobody seemed to be sharing PIC32MZ code. Every post works from the datasheet and the registers, without Harmony or PLIB, so you can see exactly what each setting does.

English News Korea

I also make English News Korea, a free English newspaper for Korean students, with stories to read and listen to in every issue.

More about this site