The test_pio is a firmware for the RaspberryPi Pico board that allows to experiment with the PIOs.
The firmware can be build using make. The generated Files will be placed in the bin folder. For details try
make help
for this to work you (obviously) need to have make installed. Depending on the target you are building for you might need to have additionaltools installed:
-
To build for ARM Cortex-M Targets you need GCC ARM Embedded
-
to flash and program you need OpenOCD
-
to create an *.uf2 file you need elf2uf2
to use the PIO in the firmware there needs to be these things: * pioasm in the tools/ folder * compile_inline_pio_asm.py python script in the tools folder * pios.h and pios.c in the src/hal/ folder * calling init_pios() function (from main). * this in the makefile:
INLPIO = tools/compile_inline_pio_asm.py %.binpio.c : %.pio.c @echo "" @echo "=== compiling $@" $(INLPIO) $< clean: rm -rf $(BIN_FOLDER)* */*.binpio.c */*.inl.pio
Files that use PIO code need to be named with the filename ending in ".pio.c" Example: "squarewave.pio.c"
In such files the PIO code can be be used like this:
#include <stdint.h> #include "squarewave.h" #include "hal/hw/RESETS.h" #include "hal/hw/PADS_BANK0.h" #include "hal/hw/IO_BANK0.h" #include "PIO.h" #include "hal/hw/PIO0.h" static const uint16_t squarewave_program_instructions[] = { /* PIO inline ASM ; ; Copyright (c) 2020 Raspberry Pi (Trading) Ltd. ; ; SPDX-License-Identifier: BSD-3-Clause ; .pio_version 0 // only requires PIO version 0 .program squarewave set pindirs, 1 ; Set pin to output again: set pins, 1 [1] ; Drive pin high and then delay for one cycle set pins, 0 ; Drive pin low jmp again ; Set PC to label `again` PIO inline ASM */ }; bool load_square_wave(const uint32_t loop) { uint32_t i; (void) loop; RESETS->RESET = RESETS->RESET & ~ RESETS_RESET_IO_BANK0_MASK; // take IO_BANK0 out of Reset // load program into PIO for(i = 0; i < (sizeof(squarewave_program_instructions)/sizeof(squarewave_program_instructions[0])); i++) { ((PIO_type*)PIO0)->INSTR_MEM[i] = squarewave_program_instructions[i]; } // device cpu clock by 10 ( 200MHz -> 20 MHz) // PIO code uses 4 cycles per cycle of output signal // 200MHz / 4 = 50 MHz /2.5 = 20 MHz PIO0->SM0_CLKDIV = (uint32_t) (2.5f * (1 << PIO_SM0_CLKDIV_INT_OFFSET)); // GPIO0 PIO0->SM0_PINCTRL = (1 << PIO_SM0_PINCTRL_SET_COUNT_OFFSET) | (0 << PIO_SM0_PINCTRL_SET_BASE_OFFSET); PADS_BANK0->GPIO0 = (PADS_BANK0_GPIO0_DRIVE_12MA << PADS_BANK0_GPIO0_DRIVE_OFFSET) | (1 << PADS_BANK0_GPIO0_SLEWFAST_OFFSET); IO_BANK0->GPIO0_CTRL = 6; // 6 == PIO0; 7 == PIO1 PIO0->CTRL = 1; // switch on State machine 0 return true; }
The pico has 40 pins (1..40) Numbered counter clock wise starting at the USB connector.
+-----+ +-------+ USB +---------+ GPIO 0 -+- 1 +-----+ 40 -+- Vbus GPIO 1 -+- 2 39 -+- Vsys Gnd -+- 3 38 -+- Gnd GPIO 2 -+- 4 37 -+- 3V3_EN GPIO 3 -+- 5 36 -+- 3V3(Out) GPIO 4 -+- 6 35 -+- ADC-Vref GPIO 5 -+- 7 34 -+- GPIO 28 Gnd -+- 8 33 -+- Gnd GPIO 6 -+- 9 32 -+- GPIO 27 GPIO 7 -+- 10 31 -+- GPIO 26 GPIO 8 -+- 11 30 -+- Run = /Reset GPIO 9 -+- 12 29 -+- GPIO 22 Gnd -+- 13 28 -+- Gnd GPIO 10 -+- 14 27 -+- GPIO 21 GPIO 11 -+- 15 26 -+- GPIO 20 GPIO 12 -+- 16 25 -+- GPIO 19 GPIO 13 -+- 17 24 -+- GPIO 18 Gnd -+- 18 23 -+- Gnd GPIO 14 -+- 19 22 -+- GPIO 17 GPIO 15 -+- 20 Debug 21 -+- GPIO 16 +-----------------------+ S G S W n W C d D L I K O
Pin 21: (GPIO 16) Debug Uart TX (UART0)
Pin 22: (GPIO 17) Debug Uart RX (UART0)
This project would not be possible without many other open source projects. This project relies on gcc for ARM Cortex-M, gdb, lcov, doxygen, make and many more. For all this help we want to thank the Linux and open source community.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>
tinyusb is under he following license
The MIT License (MIT) Copyright (c) 2018, hathach (tinyusb.org) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.