Skip to content

Commit dbe9a66

Browse files
Wren6991kilograham
authored andcommitted
Add pio_clocked_input example
1 parent a62287a commit dbe9a66

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

pio/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
if (NOT PICO_NO_HARDWARE)
22
add_subdirectory(addition)
33
add_subdirectory(apa102)
4+
add_subdirectory(clocked_input)
45
add_subdirectory(differential_manchester)
56
add_subdirectory(hello_pio)
67
add_subdirectory(hub75)

pio/clocked_input/CMakeLists.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
add_executable(pio_clocked_input)
2+
3+
pico_generate_pio_header(pio_clocked_input ${CMAKE_CURRENT_LIST_DIR}/clocked_input.pio)
4+
5+
target_sources(pio_clocked_input PRIVATE clocked_input.c)
6+
7+
target_link_libraries(pio_clocked_input PRIVATE
8+
pico_stdlib
9+
hardware_pio
10+
hardware_spi
11+
)
12+
13+
pico_add_extra_outputs(pio_clocked_input)
14+
15+
# add url via pico_set_program_url
16+
example_auto_set_url(pio_clocked_input)

pio/clocked_input/clocked_input.c

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
10+
#include "pico/stdlib.h"
11+
#include "hardware/clocks.h"
12+
#include "hardware/pio.h"
13+
#include "hardware/spi.h"
14+
#include "clocked_input.pio.h"
15+
16+
// Set up a PIO state machine to shift in serial data, sampling with an
17+
// external clock, and push the data to the RX FIFO, 8 bits at a time.
18+
//
19+
// Use one of the hard SPI peripherals to drive data into the SM through a
20+
// pair of external jumper wires, then read back and print out the data from
21+
// the SM to confirm everything worked ok.
22+
//
23+
// On your Pico you need to connect jumper wires to these pins:
24+
// - GPIO 2 -> GPIO 5 (clock output to clock input)
25+
// - GPIO 3 -> GPIO 4 (data output to data input)
26+
27+
#define SPI_SCK_PIN 2
28+
#define SPI_TX_PIN 3
29+
// GPIO 4 for PIO data input, GPIO 5 for clock input:
30+
#define PIO_INPUT_PIN_BASE 4
31+
32+
#define BUF_SIZE 8
33+
34+
int main() {
35+
stdio_init_all();
36+
37+
// Configure the SPI before PIO to avoid driving any glitches into the
38+
// state machine.
39+
spi_init(spi0, 1000 * 1000);
40+
uint actual_freq_hz = spi_set_baudrate(spi0, clock_get_hz(clk_sys) / 6);
41+
printf("SPI running at %u Hz\n", actual_freq_hz);
42+
gpio_set_function(SPI_TX_PIN, GPIO_FUNC_SPI);
43+
gpio_set_function(SPI_SCK_PIN, GPIO_FUNC_SPI);
44+
45+
// Load the clocked_input program, and configure a free state machine
46+
// to run the program.
47+
PIO pio = pio0;
48+
uint offset = pio_add_program(pio, &clocked_input_program);
49+
uint sm = pio_claim_unused_sm(pio, true);
50+
clocked_input_program_init(pio, sm, offset, PIO_INPUT_PIN_BASE);
51+
52+
// Make up some random data to send.
53+
static uint8_t txbuf[BUF_SIZE];
54+
puts("Data to transmit:");
55+
for (int i = 0; i < BUF_SIZE; ++i) {
56+
txbuf[i] = rand() >> 16;
57+
printf("%02x\n", txbuf[i]);
58+
}
59+
60+
// The "blocking" write function will send all the data in one go, and not
61+
// return until the full transmission is finished.
62+
spi_write_blocking(spi0, (const uint8_t*)txbuf, BUF_SIZE);
63+
64+
// The data we just sent should now be present in the state machine's FIFO.
65+
puts("Reading back from RX FIFO:");
66+
for (int i = 0; i < BUF_SIZE; ++i) {
67+
uint8_t rxdata = pio_sm_get_blocking(pio, sm);
68+
printf("%02x %s\n", rxdata, rxdata == txbuf[i] ? "OK" : "FAIL");
69+
}
70+
puts("Done.");
71+
}

pio/clocked_input/clocked_input.pio

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
;
2+
; Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
3+
;
4+
; SPDX-License-Identifier: BSD-3-Clause
5+
;
6+
7+
.program clocked_input
8+
9+
; Sample bits using an external clock, and push groups of bits into the RX FIFO.
10+
; - IN pin 0 is the data pin
11+
; - IN pin 1 is the clock pin
12+
; - Autopush is enabled, threshold 8
13+
;
14+
; This program samples data with each rising clock edge (like mode 0 or mode 3
15+
; SPI). The data is actually sampled one system clock cycle after the rising
16+
; edge is observed, so a clock ratio of at least input_clk < clk_sys / 6 is
17+
; recommended for good sampling alignment.
18+
19+
wait 0 pin 1
20+
wait 1 pin 1
21+
in pins, 1
22+
23+
% c-sdk {
24+
static inline void clocked_input_program_init(PIO pio, uint sm, uint offset, uint pin) {
25+
pio_sm_config c = clocked_input_program_get_default_config(offset);
26+
27+
// Set the IN base pin to the provided `pin` parameter. This is the data
28+
// pin, and the next-numbered GPIO is used as the clock pin.
29+
sm_config_set_in_pins(&c, pin);
30+
// Set the pin directions to input at the PIO
31+
pio_sm_set_consecutive_pindirs(pio, sm, pin, 2, false);
32+
// Connect these GPIOs to this PIO block
33+
pio_gpio_init(pio, pin);
34+
pio_gpio_init(pio, pin + 1);
35+
36+
// Shifting to left matches the customary MSB-first ordering of SPI.
37+
sm_config_set_in_shift(
38+
&c,
39+
false, // Shift-to-right = false (i.e. shift to left)
40+
true, // Autopush enabled
41+
8 // Autopush threshold = 8
42+
);
43+
44+
// We only receive, so disable the TX FIFO to make the RX FIFO deeper.
45+
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_RX);
46+
47+
// Load our configuration, and start the program from the beginning
48+
pio_sm_init(pio, sm, offset, &c);
49+
pio_sm_set_enabled(pio, sm, true);
50+
}
51+
%}

0 commit comments

Comments
 (0)