5 #include "sw/device/lib/testing/spi_device_testutils.h"
7 #include "dt/dt_spi_device.h"
9 #include "sw/device/lib/testing/test_framework/check.h"
11 #define MODULE_ID MAKE_MODULE_ID('s', 'd', 't')
17 static const dt_pad_t kSpiDeviceDirectPads[4] = {
24 status_t spi_device_testutils_configure_passthrough(
26 bool upload_write_commands) {
35 .
status = upload_write_commands,
50 .
opcode = kSpiDeviceFlashOpReadStatus1,
54 .payload_dir_to_host =
true,
58 .opcode = kSpiDeviceFlashOpReadStatus2,
62 .payload_dir_to_host =
true,
66 .opcode = kSpiDeviceFlashOpReadStatus3,
70 .payload_dir_to_host =
true,
74 .opcode = kSpiDeviceFlashOpReadJedec,
78 .payload_dir_to_host =
true,
82 .opcode = kSpiDeviceFlashOpReadSfdp,
86 .payload_dir_to_host =
true,
90 .opcode = kSpiDeviceFlashOpReadNormal,
92 .passthrough_swap_address =
true,
95 .payload_dir_to_host =
true,
99 .opcode = kSpiDeviceFlashOpReadFast,
101 .passthrough_swap_address =
true,
104 .payload_dir_to_host =
true,
108 .opcode = kSpiDeviceFlashOpReadDual,
110 .passthrough_swap_address =
true,
113 .payload_dir_to_host =
true,
117 .opcode = kSpiDeviceFlashOpReadQuad,
119 .passthrough_swap_address =
true,
122 .payload_dir_to_host =
true,
126 .opcode = kSpiDeviceFlashOpRead4b,
128 .passthrough_swap_address =
true,
131 .payload_dir_to_host =
true,
135 .opcode = kSpiDeviceFlashOpReadFast4b,
137 .passthrough_swap_address =
true,
140 .payload_dir_to_host =
true,
143 static_assert(
ARRAYSIZE(read_commands) <= UINT8_MAX,
144 "Length of read_commands must fit in uint8_t or we must change "
146 for (uint8_t i = 0; i <
ARRAYSIZE(read_commands); ++i) {
147 uint8_t slot = i + kSpiDeviceReadCommandSlotBase;
158 .
opcode = kSpiDeviceFlashOpWriteStatus1,
161 .payload_dir_to_host =
false,
162 .upload = upload_write_commands,
163 .set_busy_status = upload_write_commands,
167 .opcode = kSpiDeviceFlashOpWriteStatus2,
170 .payload_dir_to_host =
false,
171 .upload = upload_write_commands,
172 .set_busy_status = upload_write_commands,
176 .opcode = kSpiDeviceFlashOpWriteStatus3,
179 .payload_dir_to_host =
false,
180 .upload = upload_write_commands,
181 .set_busy_status = upload_write_commands,
185 .opcode = kSpiDeviceFlashOpChipErase,
188 .upload = upload_write_commands,
189 .set_busy_status = upload_write_commands,
193 .opcode = kSpiDeviceFlashOpSectorErase,
196 .upload = upload_write_commands,
197 .set_busy_status = upload_write_commands,
201 .opcode = kSpiDeviceFlashOpBlockErase32k,
204 .upload = upload_write_commands,
205 .set_busy_status = upload_write_commands,
209 .opcode = kSpiDeviceFlashOpBlockErase64k,
212 .upload = upload_write_commands,
213 .set_busy_status = upload_write_commands,
217 .opcode = kSpiDeviceFlashOpPageProgram,
220 .payload_dir_to_host =
false,
221 .upload = upload_write_commands,
222 .set_busy_status = upload_write_commands,
226 .opcode = kSpiDeviceFlashOpSectorErase4b,
229 .upload = upload_write_commands,
230 .set_busy_status = upload_write_commands,
234 .opcode = kSpiDeviceFlashOpBlockErase32k4b,
237 .upload = upload_write_commands,
238 .set_busy_status = upload_write_commands,
242 .opcode = kSpiDeviceFlashOpBlockErase64k4b,
245 .upload = upload_write_commands,
246 .set_busy_status = upload_write_commands,
250 .opcode = kSpiDeviceFlashOpPageProgram4b,
253 .payload_dir_to_host =
false,
254 .upload = upload_write_commands,
255 .set_busy_status = upload_write_commands,
259 .opcode = kSpiDeviceFlashOpReset,
262 .upload = upload_write_commands,
263 .set_busy_status = upload_write_commands,
267 static_assert(
ARRAYSIZE(write_commands) <= UINT8_MAX,
268 "Length of write_commands must fit into uint8_t");
269 for (uint8_t i = 0; i <
ARRAYSIZE(write_commands); ++i) {
270 uint8_t slot = i + (uint8_t)kSpiDeviceWriteCommandSlotBase;
288 kSpiDeviceFlashOpExit4bAddr));
290 if (upload_write_commands) {
305 status_t spi_device_testutils_configure_read_pipeline(
307 dif_spi_device_read_pipeline_mode_t dual_mode,
308 dif_spi_device_read_pipeline_mode_t quad_mode) {
309 if (spi_device == NULL || dual_mode >= kDifSpiDeviceReadPipelineModeCount ||
310 quad_mode >= kDifSpiDeviceReadPipelineModeCount) {
311 return INVALID_ARGUMENT();
316 .
opcode = kSpiDeviceFlashOpReadDual,
318 .passthrough_swap_address =
true,
321 .payload_dir_to_host =
true,
322 .read_pipeline_mode = dual_mode,
326 .
opcode = kSpiDeviceFlashOpReadQuad,
328 .passthrough_swap_address =
true,
331 .payload_dir_to_host =
true,
332 .read_pipeline_mode = quad_mode,
341 status_t spi_device_testutils_configure_pad_attrs(dif_pinmux_t *pinmux) {
345 for (uint32_t i = 0; i <
ARRAYSIZE(kSpiDeviceDirectPads); ++i) {
346 dt_pad_t pad = kSpiDeviceDirectPads[i];
354 "Specified slew rate not supported, trying supported slew rate");
359 "Specified drive strength not supported, trying supported drive "
372 upload_info_t *info) {
378 TRY(dif_spi_device_irq_is_pending(
379 &spid->
dev, kDifSpiDeviceIrqUploadCmdfifoNotEmpty, &upload_pending));
380 }
while (!upload_pending);
386 if (occupancy != 1) {
399 info->addr_4b = addr_4b;
401 info->has_address =
true;
408 if (info->data_len) {
409 if (info->data_len >
sizeof(info->data)) {
411 return INVALID_ARGUMENT();
418 TRY(dif_spi_device_irq_acknowledge(&spid->
dev,
419 kDifSpiDeviceIrqUploadCmdfifoNotEmpty));