Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.vscode

.venv
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ The tool will generate all required CMake files, program files and VSCode IDE fi

It will also add example code for any features and optionally for some standard library functions.

## Installation

This script depends on Jinja for templating. You can install this external library globally:

`python3 -m pip install -r requirements.txt`

or in a virtual environment:

1. `python3 -m venv .venv`
2. `source .venv/bin/activate` or `.venv\Scripts\activate.bat` on Windows
3. `pip install -r requirements.txt`

See [the official Python docs](https://docs.python.org/3/library/venv.html) for more information on virtual environments.

## Command line

Running `./pico_project --help` will give a list of the available command line parameters
Expand Down
706 changes: 222 additions & 484 deletions pico_project.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Jinja2
55 changes: 55 additions & 0 deletions templates/cmake.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# CMakeLists.txt GENERATED BY PICO PROJECT GENERATOR

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 "{{ sdk_path }}")

# pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake)

project({{ project_name }} C CXX ASM)

{% if exceptions %}
set(PICO_CXX_ENABLE_EXCEPTIONS 1)
{% endif %}
{% if rtti %}
set(PICO_CXX_ENABLE_RTTI 1)
{% endif %}
{% if configs %}
# Add any PICO_CONFIG entries specified in the Advanced settings
{% for c, v in configs.items() %}
{% set v = v == "True"%}
add_compile_definitions({{c}}={{ 1 if v else 0}})
{% endfor %}
{% endif %}
# initialise the Raspberry Pi Pico SDK
pico_sdk_init()

# add executable. Default name is the project name
{% set extension = ".cpp" if want_cpp else ".c" %}
add_executable({{project_name}} {{project_name}}{{extension}})

pico_set_program_name({{ project_name }} "{{ project_name}}")
pico_set_program_version({{project_name}} "0.1")

{% if want_run_from_ram %}
# no_flash means the target is to run from RAM
pico_set_binary_type({{project_name}} no_flash)
{% endif %}
# control the console output destinations
pico_enable_stdio_uart({{project_name}} {{1 if want_uart else 0}})
pico_enable_stdio_usb({{project_name}} {{1 if want_usb else 0}})

# pull in common dependencies {% if features | length %}and additional hardware support{% endif %}
target_link_libraries({{project_name}}
pico_stdlib
{% for lib in features %}
{{ features_list[loop.index] }}
{% endfor %})

pico_add_extra_outputs({{project_name}})
145 changes: 145 additions & 0 deletions templates/fragments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{% macro uart_define() %}
// UART defines
// By default the stdout UART is `uart0`, so we will use the second one
#define UART_ID uart1
#define BAUD_RATE 9600
// Use pins 4 and 5 for UART1
// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments
#define UART_TX_PIN 4
#define UART_RX_PIN 5
{% endmacro %}

{% macro uart_initialiser() %}
// Set up our UART
uart_init(UART_ID, BAUD_RATE);
// Set the TX and RX pins by using the function select on the GPIO
// Set datasheet for more information on function select
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
{% endmacro %}

{% macro spi_define() %}
// SPI Defines
// We are going to use SPI 0, and allocate it to the following GPIO pins,
// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments,
#define SPI_PORT spi0
#define PIN_MISO 16
#define PIN_CS 17
#define PIN_SCK 18
#define PIN_MOSI 19
{% endmacro %}

{% macro spi_initialiser() %}
// SPI initialisation. This example will use SPI at 1MHz.
spi_init(SPI_PORT, 1000*1000);
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
gpio_set_function(PIN_CS, GPIO_FUNC_SIO);
gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
// Chip select is active-low, so we'll initialise it to a driven-high state
gpio_set_dir(PIN_CS, GPIO_OUT);
gpio_put(PIN_CS, 1);
{% endmacro %}

{% macro i2c_define() %}
// I2C defines
// This example will use I2C0 on GPIO8 (SDA) and GPIO9 (SCL) running at 400KHz.
// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments
#define I2C_PORT i2c0
#define I2C_SDA 8
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kilograham I think this project-generator was created before we added the "board headers" to the Raspberry Pi Pico SDK. Should we now update the pin-numbers used here (for the various interfaces) to match up with https://github.com/raspberrypi/pico-sdk/blob/master/src/boards/include/boards/pico.h ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be a separate issue.

#define I2C_SCL 9
{% endmacro %}

{% macro i2c_initialiser() %}
// I2C Initialisation. Using it at 400Khz.
i2c_init(I2C_PORT, 400*1000);
gpio_set_function(I2C_SDA, GPIO_FUNC_I2C);
gpio_set_function(I2C_SCL, GPIO_FUNC_I2C);
gpio_pull_up(I2C_SDA);
gpio_pull_up(I2C_SCL);
{% endmacro %}

{% macro gpio_define() %}
// GPIO defines
// Example uses GPIO 2
#define GPIO 2
{% endmacro %}

{% macro gpio_initialiser() %}
// GPIO initialisation.
// We will make this GPIO an input, and pull it up by default
gpio_init(GPIO);
gpio_set_dir(GPIO, GPIO_IN);
gpio_pull_up(GPIO);
{% endmacro %}

{% macro interp_define() %}{% endmacro %}

{% macro interp_initialiser() %}
// Interpolator example code
interp_config cfg = interp_default_config();
// Now use the various interpolator library functions for your use case
// e.g. interp_config_clamp(&cfg, true);
// interp_config_shift(&cfg, 2);
// Then set the config
interp_set_config(interp0, 0, &cfg);
{% endmacro %}

{% macro timer_define() %}
int64_t alarm_callback(alarm_id_t id, void *user_data) {
// Put your timeout handler code in here
return 0;
}
{% endmacro %}

{% macro timer_initialiser() %}
// Timer example code - This example fires off the callback after 2000ms
add_alarm_in_ms(2000, alarm_callback, NULL, false);
{% endmacro %}

{% macro watchdog_define() %}
{% endmacro %}

{% macro watchdog_initialiser() %}
// Watchdog example code
if (watchdog_caused_reboot()) {
// Whatever action you may take if a watchdog caused a reboot
}
// Enable the watchdog, requiring the watchdog to be updated every 100ms or the chip will reboot
// second arg is pause on debug which means the watchdog will pause when stepping through code
watchdog_enable(100, 1);
// You need to call this function at least more often than the 100ms in the enable call to prevent a reboot
watchdog_update();
{% endmacro %}

{% macro div_define() %}
{% endmacro %}

{% macro div_initialiser() %}
// Example of using the HW divider. The pico_divider library provides a more user friendly set of APIs
// over the divider (and support for 64 bit divides), and of course by default regular C language integer
// divisions are redirected thru that library, meaning you can just use C level `/` and `%` operators and
// gain the benefits of the fast hardware divider.
int32_t dividend = 123456;
int32_t divisor = -321;
// This is the recommended signed fast divider for general use.
divmod_result_t result = hw_divider_divmod_s32(dividend, divisor);
printf(\%d/%d = %d remainder %d\\n\, dividend, divisor, to_quotient_s32(result), to_remainder_s32(result));
// This is the recommended unsigned fast divider for general use.
int32_t udividend = 123456;
int32_t udivisor = 321;
divmod_result_t uresult = hw_divider_divmod_u32(udividend, udivisor);
printf(\%d/%d = %d remainder %d\\n\, udividend, udivisor, to_quotient_u32(uresult), to_remainder_u32(uresult));
{% endmacro %}

{% macro dma_define() %}{% endmacro %}

{% macro dma_initialiser() %}{% endmacro %}

{% macro pio_define() %}{% endmacro %}

{% macro pio_initialiser() %}{% endmacro %}

{% macro clocks_define() %}{% endmacro %}

{% macro clocks_initialiser() %}{% endmacro %}
24 changes: 24 additions & 0 deletions templates/main.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% import "fragments.txt" as fragments %}
#include <stdio.h>
#include "pico/stdlib.h"
{% for include in includes %}
#include "{{include}}"
{% endfor %}

{% for lib in libraries %}
{% set def = lib | find_define %}
{% if def | length %}
{{ def }}
{% endif %}
{% endfor %}
int main() {
stdio_init_all();
{% for lib in libraries %}
{% set ini = lib | find_initialiser %}
{% if ini | length %}
{{ ini | indent }}
{% endif %}
{% endfor %}
puts("Hello, world!");
return 0;
}
18 changes: 18 additions & 0 deletions templates/vscode/c_properties.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${env:PICO_SDK_PATH}/**"
],
"defines": [],
"compilerPath": "/usr/bin/arm-none-eabi-gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-arm",
"configurationProvider" : "ms-vscode.cmake-tools"
}
],
"version": 4
}
7 changes: 7 additions & 0 deletions templates/vscode/extensions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"recommendations": [
"marus25.cortex-debug",
"ms-vscode.cmake-tools",
"ms-vscode.cpptools"
]
}
29 changes: 29 additions & 0 deletions templates/vscode/launch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Cortex Debug",
"cwd": "${workspaceRoot}",
"executable": "${command:cmake.launchTargetPath}",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"gdbPath": "gdb-multiarch",
"device": "RP2040",
"configFiles": [
"interface/{{deb}}",
"target/rp2040.cfg"
],
"svdFile": "${env:PICO_SDK_PATH}/src/rp2040/hardware_regs/rp2040.svd",
"runToMain": true,
// Give restart the same functionality as runToMain
"postRestartCommands": [
"break main",
"continue"
]
}
]
}
17 changes: 17 additions & 0 deletions templates/vscode/settings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"cmake.configureOnOpen": false,
"cmake.statusbar.advanced": {
"debug" : {
"visibility": "hidden"
},
"launch" : {
"visibility": "hidden"
},
"build" : {
"visibility": "hidden"
},
"buildTarget" : {
"visibility": "hidden"
},
},
}