-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy path0022-RP2040-add-SPI-support.patch
211 lines (203 loc) · 7.6 KB
/
0022-RP2040-add-SPI-support.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
From e4afab4cad35622e4640ed1058fc5e4ab2afbd9e Mon Sep 17 00:00:00 2001
From: Alexander Entinger <cto@lxrobotics.com>
Date: Mon, 15 Feb 2021 15:51:03 +0100
Subject: [PATCH 022/204] RP2040: add SPI support
---
.../TARGET_RP2040/PeripheralPins.c | 16 +--
.../hardware_spi/include/hardware/spi.h | 2 +-
.../pico-sdk/rp2_common/hardware_spi/spi.c | 2 +-
.../TARGET_RP2040/spi_api.c | 111 ++++++++++++++++++
targets/targets.json | 3 +-
5 files changed, 123 insertions(+), 11 deletions(-)
create mode 100644 targets/TARGET_RASPBERRYPI/TARGET_RP2040/spi_api.c
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/PeripheralPins.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/PeripheralPins.c
index 6364d42a5c..6ba3255565 100644
--- a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/PeripheralPins.c
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/PeripheralPins.c
@@ -123,24 +123,24 @@ const PinMap PinMap_PWM_OUT[] = {
const PinMap PinMap_SPI_MISO[] = {
{p0, SPI_0, (uint32_t) spi0},
{p4, SPI_0, (uint32_t) spi0},
- {p8, SPI_1, (uint32_t) spi0},
- {p12, SPI_1, (uint32_t) spi0},
+ {p8, SPI_1, (uint32_t) spi1},
+ {p12, SPI_1, (uint32_t) spi1},
{p16, SPI_0, (uint32_t) spi0},
{p20, SPI_0, (uint32_t) spi0},
- {p24, SPI_1, (uint32_t) spi0},
- {p28, SPI_1, (uint32_t) spi0},
+ {p24, SPI_1, (uint32_t) spi1},
+ {p28, SPI_1, (uint32_t) spi1},
{NC, NC, 0}
};
const PinMap PinMap_SPI_SSEL[] = {
{p1, SPI_0, (uint32_t) spi0},
{p5, SPI_0, (uint32_t) spi0},
- {p9, SPI_1, (uint32_t) spi0},
- {p13, SPI_1, (uint32_t) spi0},
+ {p9, SPI_1, (uint32_t) spi1},
+ {p13, SPI_1, (uint32_t) spi1},
{p17, SPI_0, (uint32_t) spi0},
{p21, SPI_0, (uint32_t) spi0},
- {p25, SPI_1, (uint32_t) spi0},
- {p29, SPI_1, (uint32_t) spi0},
+ {p25, SPI_1, (uint32_t) spi1},
+ {p29, SPI_1, (uint32_t) spi1},
{NC, NC, 0}
};
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_spi/include/hardware/spi.h b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_spi/include/hardware/spi.h
index 3ee8736e0c..789efc6f0d 100644
--- a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_spi/include/hardware/spi.h
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_spi/include/hardware/spi.h
@@ -86,7 +86,7 @@ typedef enum {
* and this function does not return any indication of this. You can use the \ref spi_set_baudrate function
* which will return the actual baudrate selected if this is important.
*/
-void spi_init(spi_inst_t *spi, uint baudrate);
+void _spi_init(spi_inst_t *spi, uint baudrate);
/*! \brief Deinitialise SPI instances
* \ingroup hardware_spi
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_spi/spi.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_spi/spi.c
index b0cad30755..1de19764e3 100644
--- a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_spi/spi.c
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_spi/spi.c
@@ -18,7 +18,7 @@ static inline void spi_unreset(spi_inst_t *spi) {
unreset_block_wait(spi == spi0 ? RESETS_RESET_SPI0_BITS : RESETS_RESET_SPI1_BITS);
}
-void spi_init(spi_inst_t *spi, uint baudrate) {
+void _spi_init(spi_inst_t *spi, uint baudrate) {
spi_reset(spi);
spi_unreset(spi);
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/spi_api.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/spi_api.c
new file mode 100644
index 0000000000..1cc6bfa0fb
--- /dev/null
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/spi_api.c
@@ -0,0 +1,111 @@
+/******************************************************************************
+ * INCLUDE
+ ******************************************************************************/
+
+#include "mbed_assert.h"
+#include "mbed_critical.h"
+#include "spi_api.h"
+#include "PeripheralPins.h"
+#include "PeripheralNames.h"
+
+/******************************************************************************
+ * CONSTANT
+ ******************************************************************************/
+
+static unsigned int const SPI_MASTER_DEFAULT_BITRATE = 1000 * 1000; /* 1 MHz */
+
+/******************************************************************************
+ * FUNCTION DEFINITION
+ ******************************************************************************/
+
+void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
+{
+ /* Check if all pins do in fact belong to the same SPI module. */
+ SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
+ SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
+ SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
+ SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
+
+ MBED_ASSERT(spi_mosi == spi_miso);
+ MBED_ASSERT(spi_miso == spi_sclk);
+ MBED_ASSERT(spi_sclk == spi_ssel);
+ MBED_ASSERT(spi_ssel != (SPIName)NC);
+
+ /* Obtain pointer to the SPI module. */
+ obj->dev = (spi_inst_t *)pinmap_function(mosi, PinMap_SPI_MOSI);
+
+ /* Configure GPIOs for SPI usage. */
+ gpio_set_function(mosi, GPIO_FUNC_SPI);
+ gpio_set_function(sclk, GPIO_FUNC_SPI);
+ gpio_set_function(miso, GPIO_FUNC_SPI);
+
+ /* Initialize SPI at 1 MHz bitrate */
+ _spi_init(obj->dev, SPI_MASTER_DEFAULT_BITRATE);
+}
+
+void spi_format(spi_t *obj, int bits, int mode, int slave)
+{
+ /* Doing some parameter sanity checks. */
+ MBED_ASSERT((bits >= 4) && (bits <= 16));
+ MBED_ASSERT((mode >= 0) && (mode <= 3));
+
+ /* Determine parameters for CPOL, CPHA */
+ typedef struct
+ {
+ spi_cpol_t cpol;
+ spi_cpha_t cpha;
+ } spi_mode_t;
+ spi_mode_t const SPI_MODE[4] =
+ {
+ {SPI_CPOL_0, SPI_CPHA_0}, /* MODE 0 */
+ {SPI_CPOL_0, SPI_CPHA_1}, /* MODE 1 */
+ {SPI_CPOL_1, SPI_CPHA_0}, /* MODE 2 */
+ {SPI_CPOL_1, SPI_CPHA_1} /* MODE 3 */
+ };
+
+ /* Configure the SPI. */
+ spi_set_format(obj->dev, bits, SPI_MODE[mode].cpol, SPI_MODE[mode].cpha, SPI_MSB_FIRST);
+ /* Set's the SPI up as slave if the value of slave is different from 0, e.g. a value of 1 or -1 set's this SPI up as a slave. */
+ spi_set_slave(obj->dev, slave != 0);
+}
+
+void spi_frequency(spi_t *obj, int hz)
+{
+ spi_set_baudrate(obj->dev, hz);
+}
+
+int spi_master_write(spi_t *obj, int value)
+{
+ uint8_t rx;
+ uint8_t const tx = (uint8_t)value;
+ spi_write_read_blocking(obj->dev, &tx, &rx, sizeof(rx));
+ return rx;
+}
+
+int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, char write_fill)
+{
+ /* The pico-sdk API does not support different length SPI buffers. */
+ MBED_ASSERT(tx_length == rx_length);
+ /* Perform the SPI transfer. */
+ return spi_write_read_blocking(obj->dev, (const uint8_t *)tx_buffer, (uint8_t *)rx_buffer, (size_t)tx_length);
+}
+
+const PinMap *spi_master_mosi_pinmap()
+{
+ return PinMap_SPI_MOSI;
+}
+
+const PinMap *spi_master_miso_pinmap()
+{
+ return PinMap_SPI_MISO;
+}
+
+const PinMap *spi_master_clk_pinmap()
+{
+ return PinMap_SPI_SCLK;
+}
+
+const PinMap *spi_master_cs_pinmap()
+{
+ return PinMap_SPI_SSEL;
+}
diff --git a/targets/targets.json b/targets/targets.json
index 6bf4c4af10..ec39112970 100644
--- a/targets/targets.json
+++ b/targets/targets.json
@@ -9422,7 +9422,8 @@
"ANALOGIN",
"PORT_IN",
"PORT_OUT",
- "SERIAL"
+ "SERIAL",
+ "SPI"
]
},
"NANO_RP2040_CONNECT": {
--
2.39.1