Skip to content

Commit 4607874

Browse files
committed
Initial Release
0 parents  commit 4607874

File tree

245 files changed

+21157
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

245 files changed

+21157
-0
lines changed

Diff for: .gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.idea
2+
.vscode
3+
_deps
4+
cmake-*
5+
build
6+
.DS_Store
7+
*.pdf

Diff for: CMakeLists.txt

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
3+
# Pull in PICO SDK (must be before project)
4+
include(pico_sdk_import.cmake)
5+
6+
project(pico_examples C CXX ASM)
7+
set(CMAKE_C_STANDARD 11)
8+
set(CMAKE_CXX_STANDARD 17)
9+
10+
set(PICO_EXAMPLES_PATH ${PROJECT_SOURCE_DIR})
11+
12+
# Initialize the SDK
13+
pico_sdk_init()
14+
15+
include(example_auto_set_url.cmake)
16+
# Add blink example
17+
add_subdirectory(blink)
18+
19+
# Add hello world example
20+
add_subdirectory(hello_world)
21+
22+
# Hardware-specific examples in subdirectories:
23+
add_subdirectory(adc)
24+
add_subdirectory(clocks)
25+
add_subdirectory(cmake)
26+
add_subdirectory(divider)
27+
add_subdirectory(dma)
28+
add_subdirectory(flash)
29+
add_subdirectory(gpio)
30+
add_subdirectory(i2c)
31+
add_subdirectory(interp)
32+
add_subdirectory(multicore)
33+
add_subdirectory(picoboard)
34+
add_subdirectory(pio)
35+
add_subdirectory(pwm)
36+
add_subdirectory(reset)
37+
add_subdirectory(rtc)
38+
add_subdirectory(spi)
39+
add_subdirectory(system)
40+
add_subdirectory(timer)
41+
add_subdirectory(uart)
42+
add_subdirectory(usb)
43+
add_subdirectory(watchdog)

Diff for: LICENSE.TXT

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd.
2+
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
4+
following conditions are met:
5+
6+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
7+
disclaimer.
8+
9+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
10+
disclaimer in the documentation and/or other materials provided with the distribution.
11+
12+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
13+
derived from this software without specific prior written permission.
14+
15+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
16+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Diff for: README.md

+196
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
# PICO SDK Examples
2+
3+
## Getting started
4+
5+
See [Getting Started with the Raspberry Pi Pico](https://rptl.io/pico-get-started) and the README in the [pico-sdk](https://github.com/raspberrypi/pico-sdk) for information
6+
on getting up and running.
7+
8+
### First Examples
9+
10+
App | Description | Link to prebuilt UF2
11+
---|---|---
12+
[hello_serial](hello_world/serial) | The obligatory Hello World program for Pico (Output over serial version) | https://rptl.io/pico-hello-serial
13+
[hello_usb](hello_world/usb) | The obligatory Hello World program for Pico (Output over USB version) | https://rptl.io/pico-hello-usb
14+
[blink](blink) | Blink an LED on and off. | https://rptl.io/pico-blink
15+
16+
### ADC
17+
18+
App|Description
19+
---|---
20+
[hello_adc](adc/hello_adc)|Display the voltage from an ADC input.
21+
[joystick_display](adc/joystick_display)|Display a Joystick X/Y input based on two ADC inputs.
22+
[adc_console](adc/adc_console)|An interactive shell for playing with the ADC. Includes example of free-running capture mode.
23+
24+
### Clocks
25+
26+
App|Description
27+
---|---
28+
[hello_48MHz](clocks/hello_48MHz)| Change the system clock frequency to 48 MHz while running.
29+
[hello_gpout](clocks/hello_gpout)| Use the general purpose clock outputs (GPOUT) to drive divisions of internal clocks onto GPIO outputs.
30+
[hello_resus](clocks/hello_resus)| Enable the clock resuscitate feature, "accidentally" stop the system clock, and show how we recover.
31+
32+
### CMake
33+
34+
App|Description
35+
---|---
36+
[build_variants](cmake/build_variants)| Builds two version of the same app with different configurations
37+
38+
### DMA
39+
40+
App|Description
41+
---|---
42+
[hello_dma](dma/hello_dma)| Use the DMA to copy data in memory.
43+
[control_blocks](dma/control_blocks)| Build a control block list, to program a longer sequence of DMA transfers to the UART.
44+
[channel_irq](dma/channel_irq)| Use an IRQ handler to reconfigure a DMA channel, in order to continuously drive data through a PIO state machine.
45+
46+
47+
### Flash
48+
49+
App|Description
50+
---|---
51+
[cache_perfctr](flash/cache_perfctr)| Read and clear the cache performance counters. Show how they are affected by different types of flash reads.
52+
[nuke](flash/nuke)| Obliterate the contents of flash. An example of a NO_FLASH binary (UF2 loaded directly into SRAM and runs in-place there). A useful utility to drag and drop onto your Pico if the need arises.
53+
[program](flash/program)| Erase a flash sector, program one flash page, and read back the data.
54+
[xip_stream](flash/xip_stream)| Stream data using the XIP stream hardware, which allows data to be DMA'd in the background whilst executing code from flash.
55+
[ssi_dma](flash/ssi_dma)| DMA directly from the flash interface (continuous SCK clocking) for maximum bulk read performance.
56+
57+
### GPIO
58+
59+
App|Description
60+
---|---
61+
[hello_7segment](gpio/hello_7segment) | Use the GPIOs to drive a seven segment LED display.
62+
[hello_gpio_irq](gpio/hello_gpio_irq) | Register an interrupt handler to run when a GPIO is toggled.
63+
[dht_sensor](gpio/dht_sensor) | Use GPIO to bitbang the serial protocol for a DHT temperature/humidity sensor.
64+
65+
See also: [blink](blink), blinking an LED attached to a GPIO.
66+
67+
### HW divider
68+
69+
App|Description
70+
---|---
71+
[hello_divider](divider) | Show how to directly access the hardware integer dividers, in case AEABI injection is disabled.
72+
73+
74+
### I2C
75+
76+
App|Description
77+
---|---
78+
[bus_scan](i2c/bus_scan) | Scan the I2C bus for devices and display results.
79+
[lcd_1602_i2c](i2c/lcd_1602_i2c) | Display some text on a generic 16x2 character LCD display, via I2C.
80+
[mpu6050_i2c](i2c/mpu6050_i2c) | Read acceleration and angular rate values from a MPU6050 accelerometer/gyro, attached to an I2C bus.
81+
82+
### Interpolator
83+
84+
App|Description
85+
---|---
86+
[hello_interp](interp/hello_interp) | A bundle of small examples, showing how to access the core-local interpolator hardware, and use most of its features.
87+
88+
### Multicore
89+
90+
App|Description
91+
---|---
92+
[hello_multicore](multicore/hello_multicore) | Launch a function on the second core, printf some messages on each core, and pass data back and forth through the mailbox FIFOs.
93+
[multicore_fifo_irqs](multicore/multicore_fifo_irqs) | On each core, register and interrupt handler for the mailbox FIFOs. Show how the interrupt fires when that core receives a message.
94+
[multicore_runner](multicore/multicore_runner) | Set up the second core to accept, and run, any function pointer pushed into its mailbox FIFO. Push in a few pieces of code and get answers back.
95+
96+
### Pico Board
97+
98+
App|Description
99+
---|---
100+
[blinky](picoboard/blinky)| Blink "hello, world" in Morse code on Pico's LED
101+
[button](picoboard/button)| Use Pico's BOOTSEL button as a regular button input, by temporarily suspending flash access.
102+
103+
### PIO
104+
105+
App|Description
106+
---|---
107+
[hello_pio](pio/hello_pio)| Absolutely minimal example showing how to control an LED by pushing values into a PIO FIFO.
108+
[apa102](pio/apa102)| Rainbow pattern on on a string of APA102 addressable RGB LEDs.
109+
[differential_manchester](pio/differential_manchester)| Send and receive differential Manchester-encoded serial (BMC).
110+
[hub75](pio/hub75)| Display an image on a 128x64 HUB75 RGB LED matrix.
111+
[i2c](pio/i2c)| Scan an I2C bus.
112+
[logic_analyser](pio/logic_analyser)| Use PIO and DMA to capture a logic trace of some GPIOs, whilst a PWM unit is driving them.
113+
[manchester_encoding](pio/manchester_encoding)| Send and receive Manchester-encoded serial.
114+
[pio_blink](pio/pio_blink)| Set up some PIO state machines to blink LEDs at different frequencies, according to delay counts pushed into their FIFOs.
115+
[pwm](pio/pwm)| Pulse width modulation on PIO. Use it to gradually fade the brightness of an LED.
116+
[spi](pio/spi)| Use PIO to erase, program and read an external SPI flash chip. A second example runs a loopback test with all four CPHA/CPOL combinations.
117+
[squarewave](pio/squarewave)| Drive a fast square wave onto a GPIO. This example accesses low-level PIO registers directly, instead of using the SDK functions.
118+
[st7789_lcd](pio/st7789_lcd)| Set up PIO for 62.5 Mbps serial output, and use this to display a spinning image on a ST7789 serial LCD.
119+
[uart_rx](pio/uart_rx)| Implement the receive component of a UART serial port. Attach it to the spare Arm UART to see it receive characters.
120+
[uart_tx](pio/uart_tx)| Implement the transmit component of a UART serial port, and print hello world.
121+
[ws2812](pio/ws2812)| Examples of driving WS2812 addressable RGB LEDs.
122+
[addition](pio/addition)| Add two integers together using PIO. Only around 8 billion times slower than Cortex-M0+.
123+
124+
### PWM
125+
126+
App|Description
127+
---|---
128+
[hello_pwm](pwm/hello_pwm) | Minimal example of driving PWM output on GPIOs.
129+
[led_fade](pwm/led_fade) | Fade an LED between low and high brightness. An interrupt handler updates the PWM slice's output level each time the counter wraps.
130+
[measure_duty_cycle](pwm/measure_duty_cycle) | Drives a PWM output at a range of duty cycles, and uses another PWM slice in input mode to measure the duty cycle.
131+
132+
### Reset
133+
134+
App|Description
135+
---|---
136+
[hello_reset](reset/hello_reset) | Perform a hard reset on some peripherals, then bring them back up.
137+
138+
### RTC
139+
140+
App|Description
141+
---|---
142+
[hello_rtc](rtc/hello_rtc) | Set a date/time on the RTC, then repeatedly print the current time, 10 times per second, to show it updating.
143+
[rtc_alarm](rtc/rtc_alarm) | Set an alarm on the RTC to trigger an interrupt at a date/time 5 seconds into the future.
144+
[rtc_alarm_repeat](rtc/rtc_alarm_repeat) | Trigger an RTC interrupt once per minute.
145+
146+
### SPI
147+
148+
App|Description
149+
---|---
150+
[bme280_spi](spi/bme280_spi) | Attach a BME280 temperature/humidity/pressure sensor via SPI.
151+
[mpu9250_spi](spi/mpu9250_spi) | Attach a MPU9250 accelerometer/gyoscope via SPI.
152+
[spi_dma](spi/spi_dma) | Use DMA to transfer data both to and from the SPI simultaneously. The SPI is configured for loopback.
153+
[spi_flash](spi/spi_flash) | Erase, program and read a serial flash device attached to one of the SPI controllers.
154+
155+
### System
156+
157+
App|Description
158+
---|---
159+
[hello_double_tap](system/hello_double_tap) | On dev boards with a reset button (but no BOOTSEL), a magic number in RAM can be used to enter the USB bootloader, when the reset button is pressed twice quickly.
160+
[narrow_io_write](system/narrow_io_write) | Demonstrate the effects of 8-bit and 16-bit writes on a 32-bit IO register.
161+
162+
### Timer
163+
164+
App|Description
165+
---|---
166+
[hello_timer](timer/hello_timer) | Set callbacks on the system timer, which repeat at regular intervals. Cancel the timer when we're done.
167+
[periodic_sampler](timer/periodic_sampler) | Sample GPIOs in a timer callback, and push the samples into a concurrency-safe queue. Pop data from the queue in code running in the foreground.
168+
[timer_lowlevel](timer/timer_lowlevel) | Example of direct access to the timer hardware. Not generally recommended, as the SDK may use the timer for IO timeouts.
169+
170+
### UART
171+
172+
App|Description
173+
---|---
174+
[hello_uart](uart/hello_uart) | Print some text from one of the UART serial ports, without going through `stdio`.
175+
[uart_advanced](uart/uart_advanced) | Use some other UART features like RX interrupts, hardware control flow, and data formats other than 8n1.
176+
177+
### USB Device
178+
179+
App|Description
180+
---|---
181+
[dev_audio_headset](usb/device/dev_audio_headset) | Audio headset example from TinyUSB
182+
[dev_hid_composite](usb/device/dev_hid_composite) | Composite HID (mouse + keyboard) example from TinyUSB
183+
[dev_hid_generic_inout](usb/device/dev_hid_generic_inout) | Generic HID device example from TinyUSB
184+
[dev_lowlevel](usb/device/dev_lowlevel) | A USB Bulk loopback implemented with direct access to the USB hardware (no TinyUSB)
185+
186+
### USB Host
187+
188+
App|Description
189+
---|---
190+
[host_hid](usb/host/host_hid) | Use USB in host mode to poll an attached HID keyboard (TinyUSB example)
191+
192+
### Watchdog
193+
194+
App|Description
195+
---|---
196+
[hello_watchdog](watchdog/hello_watchdog) | Set the watchdog timer, and let it expire. Detect the reboot, and halt.

Diff for: adc/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if (NOT PICO_NO_HARDWARE)
2+
add_subdirectory(adc_console)
3+
add_subdirectory(hello_adc)
4+
add_subdirectory(joystick_display)
5+
endif ()

Diff for: adc/adc_console/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_executable(adc_console
2+
adc_console.c
3+
)
4+
5+
target_link_libraries(adc_console pico_stdlib hardware_adc)
6+
7+
# create map/bin/hex file etc.
8+
pico_add_extra_outputs(adc_console)
9+
10+
# add url via pico_set_program_url
11+
example_auto_set_url(adc_console)
12+

Diff for: adc/adc_console/adc_console.c

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <stdio.h>
2+
#include "pico/stdlib.h"
3+
#include "hardware/gpio.h"
4+
#include "hardware/adc.h"
5+
6+
#define N_SAMPLES 1000
7+
uint16_t sample_buf[N_SAMPLES];
8+
9+
void printhelp() {
10+
puts("\nCommands:");
11+
puts("c0, ...\t: Select ADC channel n");
12+
puts("s\t: Sample once");
13+
puts("S\t: Sample many");
14+
puts("w\t: Wiggle pins");
15+
}
16+
17+
void __not_in_flash_func(adc_capture)(uint16_t *buf, size_t count) {
18+
adc_fifo_setup(true, false, 0, false, false);
19+
adc_run(true);
20+
for (int i = 0; i < count; i = i + 1)
21+
buf[i] = adc_fifo_get_blocking();
22+
adc_run(false);
23+
adc_fifo_drain();
24+
}
25+
26+
int main(void) {
27+
stdio_init_all();
28+
adc_init();
29+
adc_set_temp_sensor_enabled(true);
30+
31+
// Set all pins to input (as far as SIO is concerned)
32+
gpio_set_dir_all_bits(0);
33+
for (int i = 2; i < 30; ++i) {
34+
gpio_set_function(i, GPIO_FUNC_SIO);
35+
if (i >= 26) {
36+
gpio_disable_pulls(i);
37+
gpio_set_input_enabled(i, false);
38+
}
39+
}
40+
41+
printf("\n===========================\n");
42+
printf("RP2040 ADC and Test Console\n");
43+
printf("===========================\n");
44+
printhelp();
45+
46+
while (1) {
47+
char c = getchar();
48+
printf("%c", c);
49+
switch (c) {
50+
case 'c':
51+
c = getchar();
52+
printf("%c\n", c);
53+
if (c < '0' || c > '7') {
54+
printf("Unknown input channel\n");
55+
printhelp();
56+
} else {
57+
adc_select_input(c - '0');
58+
printf("Switched to channel %c\n", c);
59+
}
60+
break;
61+
case 's': {
62+
uint32_t result = adc_read();
63+
const float conversion_factor = 3.3f / (1 << 12);
64+
printf("\n0x%03x -> %f V\n", result, result * conversion_factor);
65+
break;
66+
}
67+
case 'S': {
68+
printf("\nStarting capture\n");
69+
adc_capture(sample_buf, N_SAMPLES);
70+
printf("Done\n");
71+
for (int i = 0; i < N_SAMPLES; i = i + 1)
72+
printf("%03x\n", sample_buf[i]);
73+
break;
74+
}
75+
case 'w':
76+
printf("\nPress any key to stop wiggling\n");
77+
int i = 1;
78+
gpio_set_dir_all_bits(-1);
79+
while (getchar_timeout_us(0) == PICO_ERROR_TIMEOUT) {
80+
// Pattern: Flash all pins for a cycle,
81+
// Then scan along pins for one cycle each
82+
i = i ? i << 1 : 1;
83+
gpio_put_all(i ? i : ~0);
84+
}
85+
gpio_set_dir_all_bits(0);
86+
printf("Wiggling halted.\n");
87+
break;
88+
case '\n':
89+
case '\r':
90+
break;
91+
case 'h':
92+
printhelp();
93+
break;
94+
default:
95+
printf("\nUnrecognised command: %c\n", c);
96+
printhelp();
97+
break;
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)