-
Notifications
You must be signed in to change notification settings - Fork 13
/
psram_spi.h
615 lines (555 loc) · 20.9 KB
/
psram_spi.h
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/******************************************************************************
rp2040-psram
Copyright © 2023 Ian Scott
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
/**
* @file psram_spi.h
*
* \mainpage
*
* The interface to this file is defined in psram_spi.h. Please see the
* documentation for this file.
*
* The following defines _MUST_ be defined:
*
* - @c PSRAM_PIN_CS - GPIO number of the chip select pin
* - @c PSRAM_PIN_SCK - GPIO number of the clock pin
* - @c PSRAM_PIN_MOSI - GPIO number of the MOSI pin
* - @c PSRAM_PIN_MISO - GPIO number of the MISO pin
*
* Optional define:
* - @c PSRAM_MUTEX - Define this to put PSRAM access behind a mutex. This must
* be used if the PSRAM is to be used by multiple cores.
*
* Project homepage: https://github.com/polpo/rp2040-psram
*/
#pragma once
#include "hardware/pio.h"
#include "hardware/gpio.h"
#include "hardware/timer.h"
#include "hardware/dma.h"
#if defined(PSRAM_MUTEX)
#include "pico/mutex.h"
#elif defined(PSRAM_SPINLOCK)
#include "hardware/sync.h"
#endif
#include <string.h>
#include "psram_spi.pio.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief A struct that holds the configuration for the PSRAM interface.
*
* This struct is generated by psram_spi_init() and must be passed to all calls to
* the psram access functions.
*/
typedef struct psram_spi_inst {
PIO pio;
int sm;
uint offset;
#if defined(PSRAM_MUTEX)
mutex_t mtx;
#elif defined(PSRAM_SPINLOCK)
spin_lock_t* spinlock;
uint32_t spin_irq_state;
#endif
int write_dma_chan;
dma_channel_config write_dma_chan_config;
int read_dma_chan;
dma_channel_config read_dma_chan_config;
#if defined(PSRAM_ASYNC)
int async_dma_chan;
dma_channel_config async_dma_chan_config;
#endif
} psram_spi_inst_t;
#if defined(PSRAM_ASYNC)
extern psram_spi_inst_t* async_spi_inst;
#endif
/**
* @brief Write and read raw data to the PSRAM SPI PIO, driven by the CPU
* without DMA. This can be used if DMA has not yet been initialized.
*
* Used to send raw commands and receive data from the PSRAM. Usually the @c
* psram_write*() and @c psram_read*() commands should be used instead.
*
* @param spi The PSRAM configuration instance returned from psram_spi_init().
* @param src Pointer to the source data to write.
* @param src_len Length of the source data in bytes.
* @param dst Pointer to the destination for read data, if any. Set to 0 or NULL
* if no data is to be read.
* @param dst_len Length of the destination data in bytes. Set to 0 if no data
* is to be read.
*/
__force_inline static void __time_critical_func(pio_spi_write_read_blocking)(
psram_spi_inst_t* spi,
const uint8_t* src, const size_t src_len,
uint8_t* dst, const size_t dst_len
) {
size_t tx_remain = src_len, rx_remain = dst_len;
#if defined(PSRAM_MUTEX)
mutex_enter_blocking(&spi->mtx);
#elif defined(PSRAM_SPINLOCK)
spi->spin_irq_state = spin_lock_blocking(spi->spinlock);
#endif
io_rw_8 *txfifo = (io_rw_8 *) &spi->pio->txf[spi->sm];
while (tx_remain) {
if (!pio_sm_is_tx_fifo_full(spi->pio, spi->sm)) {
*txfifo = *src++;
--tx_remain;
}
}
io_rw_8 *rxfifo = (io_rw_8 *) &spi->pio->rxf[spi->sm];
while (rx_remain) {
if (!pio_sm_is_rx_fifo_empty(spi->pio, spi->sm)) {
*dst++ = *rxfifo;
--rx_remain;
}
}
#if defined(PSRAM_MUTEX)
mutex_exit(&spi->mtx);
#elif defined(PSRAM_SPINLOCK)
spin_unlock(spi->spinlock, spi->spin_irq_state);
#endif
}
/**
* @brief Write raw data to the PSRAM SPI PIO, driven by DMA without CPU
* involvement.
*
* It's recommended to use DMA when possible as it's higher speed. Used to send
* raw commands to the PSRAM. This function is faster than
* pio_spi_write_read_dma_blocking() if no data is to be read.
*
* @param spi The PSRAM configuration instance returned from psram_spi_init().
* @param src Pointer to the source data to write.
* @param src_len Length of the source data in bytes.
*/
__force_inline static void __time_critical_func(pio_spi_write_dma_blocking)(
psram_spi_inst_t* spi,
const uint8_t* src, const size_t src_len
) {
#ifdef PSRAM_MUTEX
mutex_enter_blocking(&spi->mtx);
#elif defined(PSRAM_SPINLOCK)
spi->spin_irq_state = spin_lock_blocking(spi->spinlock);
#endif // PSRAM_SPINLOCK
#if defined(PSRAM_WAITDMA)
#if defined(PSRAM_ASYNC)
dma_channel_wait_for_finish_blocking(spi->async_dma_chan);
#endif // PSRAM_ASYNC
dma_channel_wait_for_finish_blocking(spi->write_dma_chan);
dma_channel_wait_for_finish_blocking(spi->read_dma_chan);
#endif // PSRAM_WAITDMA
dma_channel_transfer_from_buffer_now(spi->write_dma_chan, src, src_len);
dma_channel_wait_for_finish_blocking(spi->write_dma_chan);
#ifdef PSRAM_MUTEX
mutex_exit(&spi->mtx);
#elif defined(PSRAM_SPINLOCK)
spin_unlock(spi->spinlock, spi->spin_irq_state);
#endif // PSRAM_SPINLOCK
}
/**
* @brief Write and read raw data to the PSRAM SPI PIO, driven by DMA without CPU
* involvement.
*
* It's recommended to use DMA when possible as it's higher speed. Used to send
* raw commands and receive data from the PSRAM. Usually the @c psram_write* and
* @c psram_read* commands should be used instead.
*
* @param spi The PSRAM configuration instance returned from psram_spi_init().
* @param src Pointer to the source data to write.
* @param src_len Length of the source data in bytes.
* @param dst Pointer to the destination for read data, if any. Set to 0 or NULL
* if no data is to be read.
* @param dst_len Length of the destination data in bytes. Set to 0 if no data
* is to be read.
*/
__force_inline static void __time_critical_func(pio_spi_write_read_dma_blocking)(
psram_spi_inst_t* spi,
const uint8_t* src, const size_t src_len,
uint8_t* dst, const size_t dst_len
) {
#ifdef PSRAM_MUTEX
mutex_enter_blocking(&spi->mtx);
#elif defined(PSRAM_SPINLOCK)
spi->spin_irq_state = spin_lock_blocking(spi->spinlock);
#endif // PSRAM_SPINLOCK
#if defined(PSRAM_WAITDMA)
#if defined(PSRAM_ASYNC)
dma_channel_wait_for_finish_blocking(spi->async_dma_chan);
#endif // PSRAM_ASYNC
dma_channel_wait_for_finish_blocking(spi->write_dma_chan);
dma_channel_wait_for_finish_blocking(spi->read_dma_chan);
#endif // PSRAM_WAITDMA
dma_channel_transfer_from_buffer_now(spi->write_dma_chan, src, src_len);
dma_channel_transfer_to_buffer_now(spi->read_dma_chan, dst, dst_len);
dma_channel_wait_for_finish_blocking(spi->write_dma_chan);
dma_channel_wait_for_finish_blocking(spi->read_dma_chan);
#ifdef PSRAM_MUTEX
mutex_exit(&spi->mtx);
#elif defined(PSRAM_SPINLOCK)
spin_unlock(spi->spinlock, spi->spin_irq_state);
#endif // PSRAM_SPINLOCK
}
/**
* @brief Write raw data asynchronously to the PSRAM SPI PIO, driven by DMA without CPU
* involvement.
*
* Used to send raw commands to the PSRAM. Usually the @c psram_write*_async()
* command should be used instead.
*
* @param spi The PSRAM configuration instance returned from psram_spi_init().
* @param src Pointer to the source data to write.
* @param src_len Length of the source data in bytes.
*/
#if defined(PSRAM_ASYNC)
__force_inline static void __time_critical_func(pio_spi_write_async)(
psram_spi_inst_t* spi,
const uint8_t* src, const size_t src_len
) {
#if defined(PSRAM_ASYNC_SYNCHRONIZE)
#ifdef PSRAM_MUTEX
mutex_enter_blocking(&spi->mtx);
#elif defined(PSRAM_SPINLOCK)
spi->spin_irq_state = spin_lock_blocking(spi->spinlock);
#endif // PSRAM_SPINLOCK
#endif // defined(PSRAM_ASYNC_SYNCHRONIZE)
// Wait for all DMA to PSRAM to complete
dma_channel_wait_for_finish_blocking(spi->write_dma_chan);
dma_channel_wait_for_finish_blocking(spi->read_dma_chan);
dma_channel_wait_for_finish_blocking(spi->async_dma_chan);
async_spi_inst = spi;
dma_channel_transfer_from_buffer_now(spi->async_dma_chan, src, src_len);
}
#endif
/**
* @brief Initialize the PSRAM over SPI. This function must be called before
* accessing PSRAM.
*
* @param pio The PIO instance to use (PIO0 or PIO1).
* @param sm The state machine number in the PIO module to use. If -1 is given,
* will use the first available state machine.
* @param clkdiv Clock divisor for the state machine. At RP2040 speeds greater
* than 280MHz, a clkdiv >1.0 is needed. For example, at 400MHz, a clkdiv of
* 1.6 is recommended.
* @param fudge Whether to insert an extra "fudge factor" of one clock cycle
* before reading from the PSRAM. Depending on your PCB layout or PSRAM type,
* you may need to do this.
*
* @return The PSRAM configuration instance. This instance should be passed to
* all PSRAM access functions.
*/
psram_spi_inst_t psram_spi_init_clkdiv(PIO pio, int sm, float clkdiv, bool fudge);
/**
* @brief Initialize the PSRAM over SPI. This function must be called before
* accessing PSRAM.
*
* Defaults to a clkdiv of 1.0. This function is provided for backwards
* compatibility. Use psram_spi_init_clkdiv instead if you want a clkdiv other
* than 1.0.
*
* @param pio The PIO instance to use (PIO0 or PIO1).
* @param sm The state machine number in the PIO module to use. If -1 is given,
* will use the first available state machine.
*
* @return The PSRAM configuration instance. This instance should be passed to
* all PSRAM access functions.
*/
psram_spi_inst_t psram_spi_init(PIO pio, int sm);
int test_psram(psram_spi_inst_t* psram_spi, int increment);
void psram_spi_uninit(psram_spi_inst_t spi, bool fudge);
static uint8_t write8_command[] = {
40, // 40 bits write
0, // 0 bits read
0x02u, // Write command
0, 0, 0, // Address
0 // 8 bits data
};
/**
* @brief Write 8 bits of data to a given address asynchronously to the PSRAM SPI PIO,
* driven by DMA without CPU involvement.
*
* This function is optimized to write 8 bits as quickly as possible to the
* PSRAM as opposed to the more general-purpose psram_write() function.
*
* @param spi The PSRAM configuration instance returned from psram_spi_init().
* @param addr Address to write to.
* @param val Value to write.
*/
#if defined(PSRAM_ASYNC)
__force_inline static void psram_write8_async(psram_spi_inst_t* spi, uint32_t addr, uint8_t val) {
write8_command[3] = addr >> 16;
write8_command[4] = addr >> 8;
write8_command[5] = addr;
write8_command[6] = val;
pio_spi_write_async(spi, write8_command, sizeof(write8_command));
};
#endif
/**
* @brief Write 8 bits of data to a given address to the PSRAM SPI PIO,
* driven by DMA without CPU involvement, blocking until the write is
* complete.
*
* This function is optimized to write 8 bits as quickly as possible to the
* PSRAM as opposed to the more general-purpose psram_write() function.
*
* @param spi The PSRAM configuration instance returned from psram_spi_init().
* @param addr Address to write to.
* @param val Value to write.
*/
__force_inline static void psram_write8(psram_spi_inst_t* spi, uint32_t addr, uint8_t val) {
write8_command[3] = addr >> 16;
write8_command[4] = addr >> 8;
write8_command[5] = addr;
write8_command[6] = val;
pio_spi_write_dma_blocking(spi, write8_command, sizeof(write8_command));
};
static uint8_t read8_command[] = {
40, // 40 bits write
8, // 8 bits read
0x0bu, // Fast read command
0, 0, 0, // Address
0 // 8 delay cycles
};
/**
* @brief Read 8 bits of data from a given address to the PSRAM SPI PIO,
* driven by DMA without CPU involvement, blocking until the read is
* complete.
*
* This function is optimized to read 8 bits as quickly as possible from the
* PSRAM as opposed to the more general-purpose psram_read() function.
*
* @param spi The PSRAM configuration instance returned from psram_spi_init().
* @param addr Address to read from.
* @return The data at the specified address.
*/
__force_inline static uint8_t psram_read8(psram_spi_inst_t* spi, uint32_t addr) {
read8_command[3] = addr >> 16;
read8_command[4] = addr >> 8;
read8_command[5] = addr;
uint8_t val;
pio_spi_write_read_dma_blocking(spi, read8_command, sizeof(read8_command), &val, 1);
return val;
};
static uint8_t write16_command[] = {
48, // 48 bits write
0, // 0 bits read
0x02u, // Write command
0, 0, 0, // Address
0, 0 // 16 bits data
};
/**
* @brief Write 16 bits of data to a given address to the PSRAM SPI PIO,
* driven by DMA without CPU involvement, blocking until the write is
* complete.
*
* This function is optimized to write 16 bits as quickly as possible to the
* PSRAM as opposed to the more general-purpose psram_write() function.
*
* @param spi The PSRAM configuration instance returned from psram_spi_init().
* @param addr Address to write to.
* @param val Value to write.
*/
__force_inline static void psram_write16(psram_spi_inst_t* spi, uint32_t addr, uint16_t val) {
write16_command[3] = addr >> 16;
write16_command[4] = addr >> 8;
write16_command[5] = addr;
write16_command[6] = val;
write16_command[7] = val >> 8;
pio_spi_write_dma_blocking(spi, write16_command, sizeof(write16_command));
};
static uint8_t read16_command[] = {
40, // 40 bits write
16, // 16 bits read
0x0bu, // Fast read command
0, 0, 0, // Address
0 // 8 delay cycles
};
/**
* @brief Read 16 bits of data from a given address to the PSRAM SPI PIO,
* driven by DMA without CPU involvement, blocking until the read is
* complete.
*
* This function is optimized to read 16 bits as quickly as possible from the
* PSRAM as opposed to the more general-purpose psram_read() function.
*
* @param spi The PSRAM configuration instance returned from psram_spi_init().
* @param addr Address to read from.
* @return The data at the specified address.
*/
__force_inline static uint16_t psram_read16(psram_spi_inst_t* spi, uint32_t addr) {
read16_command[3] = addr >> 16;
read16_command[4] = addr >> 8;
read16_command[5] = addr;
uint16_t val;
pio_spi_write_read_dma_blocking(spi, read16_command, sizeof(read16_command), (unsigned char*)&val, 2);
return val;
};
static uint8_t write32_command[] = {
64, // 64 bits write
0, // 0 bits read
0x02u, // Write command
0, 0, 0, // Address
0, 0, 0, 0 // 32 bits data
};
/**
* @brief Write 32 bits of data to a given address to the PSRAM SPI PIO,
* driven by DMA without CPU involvement, blocking until the write is
* complete.
*
* This function is optimized to write 32 bits as quickly as possible to the
* PSRAM as opposed to the more general-purpose psram_write() function.
*
* @param spi The PSRAM configuration instance returned from psram_spi_init().
* @param addr Address to write to.
* @param val Value to write.
*/
__force_inline static void psram_write32(psram_spi_inst_t* spi, uint32_t addr, uint32_t val) {
// Break the address into three bytes and send read command
write32_command[3] = addr >> 16;
write32_command[4] = addr >> 8;
write32_command[5] = addr;
write32_command[6] = val;
write32_command[7] = val >> 8;
write32_command[8] = val >> 16;
write32_command[9] = val >> 24;
pio_spi_write_dma_blocking(spi, write32_command, sizeof(write32_command));
};
/**
* @brief Write 32 bits of data to a given address asynchronously to the PSRAM
* SPI PIO, driven by DMA without CPU involvement.
*
* This function is optimized to write 32 bits as quickly as possible to the
* PSRAM as opposed to the more general-purpose psram_write() function.
*
* @param spi The PSRAM configuration instance returned from psram_spi_init().
* @param addr Address to write to.
* @param val Value to write.
*/
__force_inline static void psram_write32_async(psram_spi_inst_t* spi, uint32_t addr, uint32_t val) {
// Break the address into three bytes and send read command
write32_command[3] = addr >> 16;
write32_command[4] = addr >> 8;
write32_command[5] = addr;
write32_command[6] = val;
write32_command[7] = val >> 8;
write32_command[8] = val >> 16;
write32_command[9] = val >> 24;
pio_spi_write_async(spi, write32_command, sizeof(write32_command));
};
static uint8_t read32_command[] = {
40, // 40 bits write
32, // 32 bits read
0x0bu, // Fast read command
0, 0, 0, // Address
0 // 8 delay cycles
};
/**
* @brief Read 32 bits of data from a given address to the PSRAM SPI PIO,
* driven by DMA without CPU involvement, blocking until the read is
* complete.
*
* This function is optimized to read 32 bits as quickly as possible from the
* PSRAM as opposed to the more general-purpose psram_read() function.
*
* @param spi The PSRAM configuration instance returned from psram_spi_init().
* @param addr Address to read from.
* @return The data at the specified address.
*/
__force_inline static uint32_t psram_read32(psram_spi_inst_t* spi, uint32_t addr) {
read32_command[3] = addr >> 16;
read32_command[4] = addr >> 8;
read32_command[5] = addr;
uint32_t val;
pio_spi_write_read_dma_blocking(spi, read32_command, sizeof(read32_command), (unsigned char*)&val, 4);
return val;
};
static uint8_t write_command[] = {
0, // n bits write
0, // 0 bits read
0x02u, // Fast write command
0, 0, 0 // Address
};
/**
* @brief Write @c count bytes of data to a given address to the PSRAM SPI PIO,
* driven by DMA without CPU involvement, blocking until the write is
* complete.
*
* @param spi The PSRAM configuration instance returned from psram_spi_init().
* @param addr Address to write to.
* @param src Pointer to the source data to write.
* @param count Number of bytes to write.
*/
__force_inline static void psram_write(psram_spi_inst_t* spi, const uint32_t addr, const uint8_t* src, const size_t count) {
// Break the address into three bytes and send read command
write_command[0] = (4 + count) * 8;
write_command[3] = addr >> 16;
write_command[4] = addr >> 8;
write_command[5] = addr;
pio_spi_write_dma_blocking(spi, write_command, sizeof(write_command));
pio_spi_write_dma_blocking(spi, src, count);
};
static uint8_t read_command[] = {
40, // 40 bits write
0, // n bits read
0x0bu, // Fast read command
0, 0, 0, // Address
0 // 8 delay cycles
};
/**
* @brief Read @c count bits of data from a given address to the PSRAM SPI PIO,
* driven by DMA without CPU involvement, blocking until the read is
* complete.
*
* @param spi The PSRAM configuration instance returned from psram_spi_init().
* @param addr Address to read from.
* @param dst Pointer to the destination for the read data.
* @param count Number of bytes to read.
*/
__force_inline static void psram_read(psram_spi_inst_t* spi, const uint32_t addr, uint8_t* dst, const size_t count) {
read_command[1] = count * 8;
read_command[3] = addr >> 16;
read_command[4] = addr >> 8;
read_command[5] = addr;
pio_spi_write_read_dma_blocking(spi, read_command, sizeof(read_command), dst, count);
};
static uint8_t write_async_fast_command[134] = {
0, // n bits write
0, // 0 bits read
0x02u // Fast write command
};
/**
* @brief Write @c count bytes of data to a given address asynchronously to the
* PSRAM SPI PIO, driven by DMA without CPU involvement.
*
* @param spi The PSRAM configuration instance returned from psram_spi_init().
* @param addr Address to write to.
* @param src Pointer to the source data to write.
* @param count Number of bytes to write.
*/
__force_inline static void psram_write_async_fast(psram_spi_inst_t* spi, uint32_t addr, uint8_t* val, const size_t count) {
write_async_fast_command[0] = (4 + count) * 8;
write_async_fast_command[3] = addr >> 16;
write_async_fast_command[4] = addr >> 8;
write_async_fast_command[5] = addr;
memcpy(write_async_fast_command + 6, val, count);
pio_spi_write_async(spi, write_async_fast_command, 6 + count);
};
#ifdef __cplusplus
}
#endif