Skip to content

Commit ca3f6d6

Browse files
Michael StoopsWren6991
authored andcommitted
Minor changes to spi_master and spi_slave. Adjusted documentation.
1 parent dea81ae commit ca3f6d6

File tree

9 files changed

+202
-127
lines changed

9 files changed

+202
-127
lines changed

spi/spi_master_slave/README.adoc

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
= Communicating as master and slave via SPI
2+
3+
This example code shows how to interface two RP2040 microcontrollers to each other using SPI.
4+
5+
== Wiring information
6+
7+
[frame="topbot",options="header"]
8+
|===
9+
| Function | Master (RP2040) | Slave (RP2040) | Master (Pico) | Slave (Pico)
10+
| MOSI | DO0 | DI0 | 25 | 21
11+
| SCLK | SCK0 | SCK0 | 24 | 24
12+
| GND | GND | GND | 23 | 23
13+
| CS | CS0 | CS0 | 22 | 22
14+
| MISO | DI0 | DO0 | 21 | 25
15+
|===
16+
17+
[[spi_master_slave_wiring]]
18+
[pdfwidth=75%]
19+
.Wiring Diagram for SPI Master and Slave.
20+
image::spi_master_slave_bb.png[]
21+
22+
At least one of the boards should be powered, and will share power to the other.
23+
24+
If the master is not connected properly to a slave, the master will report reading all zeroes.
25+
26+
If the slave is not connected properly to a master, it will initialize but never transmit nor receive, because it's waiting for clock signal from the master.
27+
28+
== Outputs
29+
30+
Both master and slave boards will give output to their UART or USB CDC serial port.
31+
32+
With master and slave properly connected, the master should output something like this:
33+
34+
....
35+
SPI master example
36+
SPI master says: The following buffer will be written to MOSI endlessly:
37+
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
38+
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
39+
20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
40+
30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
41+
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
42+
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
43+
60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
44+
70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f
45+
80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f
46+
90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f
47+
a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af
48+
b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf
49+
c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf
50+
d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df
51+
e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef
52+
f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff
53+
SPI master says: read page 0 from the MISO line:
54+
ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0
55+
ef ee ed ec eb ea e9 e8 e7 e6 e5 e4 e3 e2 e1 e0
56+
df de dd dc db da d9 d8 d7 d6 d5 d4 d3 d2 d1 d0
57+
cf ce cd cc cb ca c9 c8 c7 c6 c5 c4 c3 c2 c1 c0
58+
bf be bd bc bb ba b9 b8 b7 b6 b5 b4 b3 b2 b1 b0
59+
af ae ad ac ab aa a9 a8 a7 a6 a5 a4 a3 a2 a1 a0
60+
9f 9e 9d 9c 9b 9a 99 98 97 96 95 94 93 92 91 90
61+
8f 8e 8d 8c 8b 8a 89 88 87 86 85 84 83 82 81 80
62+
7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70
63+
6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60
64+
5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50
65+
4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40
66+
3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
67+
2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
68+
1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10
69+
0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
70+
....
71+
72+
The slave should output something like this:
73+
74+
....
75+
SPI slave example
76+
SPI slave says: When reading from MOSI, the following buffer will be written to MISO:
77+
ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0
78+
ef ee ed ec eb ea e9 e8 e7 e6 e5 e4 e3 e2 e1 e0
79+
df de dd dc db da d9 d8 d7 d6 d5 d4 d3 d2 d1 d0
80+
cf ce cd cc cb ca c9 c8 c7 c6 c5 c4 c3 c2 c1 c0
81+
bf be bd bc bb ba b9 b8 b7 b6 b5 b4 b3 b2 b1 b0
82+
af ae ad ac ab aa a9 a8 a7 a6 a5 a4 a3 a2 a1 a0
83+
9f 9e 9d 9c 9b 9a 99 98 97 96 95 94 93 92 91 90
84+
8f 8e 8d 8c 8b 8a 89 88 87 86 85 84 83 82 81 80
85+
7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70
86+
6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60
87+
5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50
88+
4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40
89+
3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
90+
2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
91+
1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10
92+
0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
93+
SPI slave says: read page 0 from the MOSI line:
94+
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
95+
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
96+
20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
97+
30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
98+
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
99+
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
100+
60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
101+
70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f
102+
80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f
103+
90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f
104+
a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af
105+
b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf
106+
c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf
107+
d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df
108+
e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef
109+
f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff
110+
....
111+
112+
If you look at the communication with a logic analyzer, you should see this:
113+
114+
[[spi_master_slave_wiring]]
115+
[pdfwidth=75%]
116+
.Data capture as seen in Saleae Logic.
117+
image::spi_master_slave_logic.png[]
118+
119+
== List of Files
120+
121+
CMakeLists.txt:: CMake file to incorporate the example in to the examples build tree.
122+
spi_master/spi_master.c:: The example code for SPI master.
123+
spi_slave/spi_slave.c:: The example code for SPI slave.
124+
spi_master_slave_logic.png:: Communication as seen by logic analyzer (screenshot).
125+
spi_master_slave.logicdata:: Communication as seen by logic analyzer (original file, free viewer at https://www.saleae.com/downloads/).
126+
spi_master_slave.fzz:: Fritzing file.
127+
spi_master_slave_bb.png:: Breadboard wiring diagram.
128+
129+
== Bill of Materials
130+
131+
.A list of materials required for the example
132+
[[spi-master-slave-bom-table]]
133+
[cols=3]
134+
|===
135+
| *Item* | *Quantity* | Details
136+
| Breadboard | 1 | generic part
137+
| Raspberry Pi Pico | 2 | http://raspberrypi.org/
138+
| M/M Jumper wires | 8 | generic part
139+
|===
140+

spi/spi_master_slave/README.md

Lines changed: 0 additions & 98 deletions
This file was deleted.

spi/spi_master_slave/spi_master/spi_master.c

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

1026
#include <stdio.h>
@@ -39,34 +55,35 @@ int main() {
3955

4056
printf("SPI master example\n");
4157

42-
// Enable SPI 0 at 1 KHz and connect to GPIOs
43-
spi_init(spi_default, 1000);
58+
// Enable SPI 0 at 1 MHz and connect to GPIOs
59+
spi_init(spi_default, 1000 * 1000);
4460
gpio_set_function(PICO_DEFAULT_SPI_RX_PIN, GPIO_FUNC_SPI);
4561
gpio_set_function(PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI);
4662
gpio_set_function(PICO_DEFAULT_SPI_TX_PIN, GPIO_FUNC_SPI);
4763
gpio_set_function(PICO_DEFAULT_SPI_CSN_PIN, GPIO_FUNC_SPI);
4864
// Make the SPI pins available to picotool
4965
bi_decl(bi_4pins_with_func(PICO_DEFAULT_SPI_RX_PIN, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_SCK_PIN, PICO_DEFAULT_SPI_CSN_PIN, GPIO_FUNC_SPI));
5066

51-
printf("SPI master initialised.\n");
52-
5367
uint8_t out_buf[BUF_LEN], in_buf[BUF_LEN];
5468

5569
// Initialize output buffer
5670
for (size_t i = 0; i < BUF_LEN; ++i) {
5771
out_buf[i] = i;
5872
}
59-
60-
printf("The following buffer will be written to MOSI endlessly.\n");
73+
74+
printf("SPI master says: The following buffer will be written to MOSI endlessly:\n");
6175
printbuf(out_buf, BUF_LEN);
6276

63-
for (size_t i = 0; true; ++i) {
77+
for (size_t i = 0; ; ++i) {
6478
// Write the output buffer to MOSI, and at the same time read from MISO.
6579
spi_write_read_blocking(spi_default, out_buf, in_buf, BUF_LEN);
6680

6781
// Write to stdio whatever came in on the MISO line.
68-
printf("Read the following from MISO, page %d:\n", i);
82+
printf("SPI master says: read page %d from the MISO line:\n", i);
6983
printbuf(in_buf, BUF_LEN);
84+
85+
// Sleep for ten seconds so you get a chance to read the output.
86+
sleep_ms(10 * 1000);
7087
}
7188
#endif
7289
}
32 KB
Binary file not shown.
-145 KB
Binary file not shown.
-115 KB
Binary file not shown.
248 KB
Loading
Loading

spi/spi_master_slave/spi_slave/spi_slave.c

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

1026
#include <stdio.h>
@@ -41,8 +57,8 @@ int main() {
4157

4258
printf("SPI slave example\n");
4359

44-
// Enable SPI 0 at 1 KHz and connect to GPIOs
45-
spi_init(spi_default, 1000);
60+
// Enable SPI 0 at 1 MHz and connect to GPIOs
61+
spi_init(spi_default, 1000 * 1000);
4662
spi_set_slave(spi_default, true);
4763
gpio_set_function(PICO_DEFAULT_SPI_RX_PIN, GPIO_FUNC_SPI);
4864
gpio_set_function(PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI);
@@ -51,8 +67,6 @@ int main() {
5167
// Make the SPI pins available to picotool
5268
bi_decl(bi_4pins_with_func(PICO_DEFAULT_SPI_RX_PIN, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_SCK_PIN, PICO_DEFAULT_SPI_CSN_PIN, GPIO_FUNC_SPI));
5369

54-
printf("SPI slave initialised.\n");
55-
5670
uint8_t out_buf[BUF_LEN], in_buf[BUF_LEN];
5771

5872
// Initialize output buffer
@@ -61,14 +75,16 @@ int main() {
6175
out_buf[i] = ~i;
6276
}
6377

64-
printf("When reading from MOSI, the following buffer will be written to MISO:\n");
78+
printf("SPI slave says: When reading from MOSI, the following buffer will be written to MISO:\n");
6579
printbuf(out_buf, BUF_LEN);
6680

67-
for (size_t i = 0; true; ++i) {
81+
for (size_t i = 0; ; ++i) {
6882
// Write the output buffer to MISO, and at the same time read from MOSI.
6983
spi_write_read_blocking(spi_default, out_buf, in_buf, BUF_LEN);
7084

71-
// The slave doesn't print the buffer. It can't afford to ignore the SPI bus while writing to stdio.
85+
// Write to stdio whatever came in on the MOSI line.
86+
printf("SPI slave says: read page %d from the MOSI line:\n", i);
87+
printbuf(in_buf, BUF_LEN);
7288
}
7389
#endif
7490
}

0 commit comments

Comments
 (0)