Skip to content

Instantly share code, notes, and snippets.

@sonnny
Last active December 29, 2023 01:19
Show Gist options
  • Select an option

  • Save sonnny/1c17a46068a6789c70c491f4dae5367e to your computer and use it in GitHub Desktop.

Select an option

Save sonnny/1c17a46068a6789c70c491f4dae5367e to your computer and use it in GitHub Desktop.
demo of pico pio minimal initialization done in main instead of .pio file, includes CMakeLists.txt and instruction on how to generate a pico project
//
// pio_minimal.c
// demo for minimal pio
// pio initialize is in main
// you normally see pio init in .pio file
//
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "blink.pio.h"
int main(){
PIO pio = pio0;
uint32_t sm = 0;
uint32_t offset = pio_add_program(pio, &blink_program);
pio_gpio_init(pio, 12); // led on pin 12
pio_sm_set_consecutive_pindirs(pio, sm, 12, 1, true);
pio_sm_config c = blink_program_get_default_config(offset);
// slow down clock to see led toggling
// 5 is system clock
// 2000 hz is about the slowest you can run pio
sm_config_set_clkdiv(&c, clock_get_hz(5) / 2000);
//sm_config_set_clkdiv(&c, 800000000);
sm_config_set_set_pins(&c, 12, 1);
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;; filename: blink.pio
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.program blink
.wrap_target
set pins, 1
nop [30]
nop [30]
nop [30]
nop [30]
set pins, 0
nop [30]
nop [30]
nop [30]
nop [30]
.wrap
#
# CMakeLists.txt
#
# generate new pico project
# cd pico/pico-project-generator
# export PICO_SDK_PATH=/home/sonny/pico/pico-sdk
# python3 pico_project.py --gui
# set project name
# set location of project
# click checkbox for Library Options (PIO interface)
#
# to generate .uf2 for pico
# cd project_name
# cd build
# add to CMakeLists.txt below add_executable
# pico_generate_pio_header(pio_minimal ${CMAKE_CURRENT_LIST_DIR}/blink.pio)
#
# cmake ..
# make
# under build directory you will find .uf2 file
#
# while holding boot button
# ground run pin momentarily
# let go of boot button
#
# on file manager you should have RP2 mounted
# drag and drop the .uf2 from build directory of your project
#
# Generated Cmake Pico project file
cmake_minimum_required(VERSION 3.13)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
# Initialise pico_sdk from installed location
# (note this can come from environment, CMake cache etc)
set(PICO_SDK_PATH "/home/sonny/pico/pico-sdk")
set(PICO_BOARD pico CACHE STRING "Board type")
# Pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake)
if (PICO_SDK_VERSION_STRING VERSION_LESS "1.4.0")
message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.4.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()
project(pio_minimal C CXX ASM)
# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()
# Add executable. Default name is the project name, version 0.1
add_executable(pio_minimal pio_minimal.c )
pico_generate_pio_header(pio_minimal ${CMAKE_CURRENT_LIST_DIR}/blink.pio)
pico_set_program_name(pio_minimal "pio_minimal")
pico_set_program_version(pio_minimal "0.1")
pico_enable_stdio_uart(pio_minimal 1)
pico_enable_stdio_usb(pio_minimal 0)
# Add the standard library to the build
target_link_libraries(pio_minimal
pico_stdlib)
# Add the standard include files to the build
target_include_directories(pio_minimal PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts or any other standard includes, if required
)
# Add any user requested libraries
target_link_libraries(pio_minimal
hardware_pio
)
pico_add_extra_outputs(pio_minimal)
@sonnny
Copy link
Author

sonnny commented Dec 28, 2023

demo of pico pio minimal initialization done in main instead of .pio file, includes CMakeLists.txt and instruction on how to generate a pico project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment