Commit 31e1760b authored by Gallacchi Mattia's avatar Gallacchi Mattia
Browse files

Add example projects


Change build script to be more user friendly

Signed-off-by: default avatarmattia.gallacchi <mattia.gallacchi@he-arc.ch>
parent 5febee2b
Loading
Loading
Loading
Loading
+101 −4
Original line number Diff line number Diff line
@@ -2,12 +2,113 @@

This repo shows how to use the Raspberry Pi Pico SDK.

## Init submodules 

You will need the **pico-sdk** to compile the code in this repo. Use the following command to clone it with all its submodules:

```bash
git submodule update --recursive --init pico-sdk
```

## Raspberry Pi Documentation

[Raspberry Pi Pico C/C++ SDK][1]
[Raspberry Pi Pico Quick Start][2]
[Raspberry Pi Pico W (WiFi) Quick start][3]

## Projects

[SHT30 sensor](sht30/README.md): Application that acquires Humidity and Temperature values from a SHT30 sensor.
[Blinky](blinky/README.md): Application to blink a led
[PWM](pwm/README.md): Application to generate a PWM signal.

### Build 

A utility script allows to build and deploy the projects. Usage:

```bash
Build Raspberry Pi Pico projects

Syntax: ./build.sh [-b|c|d|f|h|p] <args>
[ -b ]        Build project.
[ -c ]        Clean
[ -d ]        Launch debugger
[ -f ]        Flash board
[ -h ]        Help
[ -p PATH ]   Set project path
```

Example:

```bash
./build.sh -bf -p blinky/
```

Will build and flash the blinky application on the Raspberry Pi Pico.
If you want to manually build your project checkout this [chapter](#tools).

## Add new project

If you want to add a project you will need to create a new project folder containing the following basic files.

```
new_project
|
|---.config
|---CMakeLists.txt
|---pico_sdk_import.cmake
|---main.cpp
|---...
```

The *pico_sdk_import.cmake* just need to be copied to your project folder no modification required.

### Config file

This files contains two values:
- BOARD_TYPE: Defines the type of board to which the code will be compiled for.
- SRC_PATH: Relative path to the source code

Example:

```bash
BOARD_TYPE=grove_pico_w
SRC_PATH=src
```

A list of supported boards is available [here](pico-sdk/src/boards/include/). You can also define your own
like I did for the *SHT30* app. Checkout this [file](sht30/src/grove_pico_w.h)

### Cmakelists

Here is a basic example for the Pico without WiFi:

```bash
cmake_minimum_required(VERSION 3.13)

include(pico_sdk_import.cmake)

project(blinky C CXX ASM)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

list(APPEND PICO_BOARD_HEADER_DIRS ${CMAKE_CURRENT_LIST_DIR})

pico_sdk_init()

file(GLOB SRC *.cpp)
add_executable(${PROJECT_NAME} ${SRC})

pico_enable_stdio_usb(${PROJECT_NAME} 0)
pico_enable_stdio_uart(${PROJECT_NAME} 1)

target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(${PROJECT_NAME} pico_stdlib)

pico_add_extra_outputs(${PROJECT_NAME})
```

## Tools

**This guide as been tested for Ubuntu 22.04**
@@ -138,10 +239,6 @@ Run the build script with the *-f* option:

> :warning: This command must be performed on your host machine not in a running docker container

## Projects

[SHT30 sensor](sht30/README.md)


[1]:https://datasheets.raspberrypi.com/pico/raspberry-pi-pico-c-sdk.pdf?_gl=1*155jvvo*_ga*MTk5NjQ0ODQwNy4xNzAyNjI5NzEx*_ga_22FD70LWDS*MTcwNDI5MDg1OS4zLjEuMTcwNDI5MTA0Ny4wLjAuMA..
[2]:https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf?_gl=1*i8b34r*_ga*MTk5NjQ0ODQwNy4xNzAyNjI5NzEx*_ga_22FD70LWDS*MTcwNDI5MDg1OS4zLjEuMTcwNDI5MTEzNy4wLjAuMA..

blinky/CMakeLists.txt

0 → 100644
+23 −0
Original line number Diff line number Diff line
cmake_minimum_required(VERSION 3.13)

include(pico_sdk_import.cmake)

project(blinky C CXX ASM)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

list(APPEND PICO_BOARD_HEADER_DIRS ${CMAKE_CURRENT_LIST_DIR})

pico_sdk_init()

file(GLOB SRC *.cpp)
add_executable(${PROJECT_NAME} ${SRC})

pico_enable_stdio_usb(${PROJECT_NAME} 0)
pico_enable_stdio_uart(${PROJECT_NAME} 1)

target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(${PROJECT_NAME} pico_stdlib)

pico_add_extra_outputs(${PROJECT_NAME})
 No newline at end of file

blinky/README.md

0 → 100644
+7 −0
Original line number Diff line number Diff line
# Blinky

Sample application to showcase the usage of *repeating timer* to make a led blink.

## Wiring

![Hardware connection](../img/pico_debug_wiring.png)
 No newline at end of file

blinky/main.cpp

0 → 100644
+43 −0
Original line number Diff line number Diff line
#include <string.h>
#include <stdio.h>
#include "pico/stdlib.h"

#define LED_DELAY_MS    250

#ifndef PICO_DEFAULT_LED_PIN
#error Blinky example requires a board with a LED
#else
#define LED_PIN     PICO_DEFAULT_LED_PIN
#endif

bool led_callback(struct repeating_timer *t)
{
    static bool value = true;

    gpio_put(LED_PIN, value);
    value = !value;

    return true;
}

int main()
{
    struct repeating_timer *timer = new struct repeating_timer;

    stdio_init_all();

    printf("Hello from blinky example\n");
    
    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);

    if(!add_repeating_timer_ms(LED_DELAY_MS, led_callback, NULL, timer)) {
        printf("Error: Failed to setup the timer");
        return 1;
    }

    while(1)
        tight_loop_contents();

    return 0;
}
 No newline at end of file
Loading