/
spiflash.c
780 lines (642 loc) · 20.8 KB
/
spiflash.c
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/*
The MIT License (MIT)
Copyright (c) 2015 Peter Andersson (pelleplutt1976<at>gmail.com)
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.
*/
/**
* spiflash.c
*
* @author: petera
*/
#include "spiflash.h"
#define DECR_WAIT(_ms) ( (1 * (_ms) / 2) == 0 ? 1 : (1 * (_ms) / 2) )
#define BCW_IDLE 0
#define BCW_WAIT 1
#define BCW_READ_SR 2
#define BCW_CHECK 3
static const uint8_t MultiplyDeBruijnBitPosition[32] = {
32, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
static uint8_t _spiflash_clz(uint32_t v) {
// stolen from https://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup
return MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
}
static int _spiflash_is_hwbusy(spiflash_t *spi, uint8_t sr) {
return (sr & spi->cmd_tbl->sr_busy_bit);
}
static void _spiflash_compose_address(spiflash_t *spi, uint32_t addr, uint8_t *buf) {
uint8_t i;
for (i = 0; i < spi->cfg->addr_sz; i++) {
buf[i] = (spi->cfg->addr_endian ?
( addr >> (8*((spi->cfg->addr_sz - 1) - i)) ) :
( addr >> (8*i) )
) & 0xff;
}
}
static void _spiflash_finalize(spiflash_t *spi) {
spi->wait_period_ms = 0;
spi->busy_pre_check = 0;
spi->busy_check_wait = BCW_IDLE;
}
static uint16_t _spiflash_get_supported_block_mask(spiflash_t *spi) {
// bit 0:256 1:512 2:1K 3:2K 4:4K 5:8K 6:16K 7:32K 8:64K etc
uint16_t bm = 0 |
(spi->cmd_tbl->block_erase_4 ? (1<<4) : 0) |
(spi->cmd_tbl->block_erase_8 ? (1<<5) : 0) |
(spi->cmd_tbl->block_erase_16 ? (1<<6) : 0) |
(spi->cmd_tbl->block_erase_32 ? (1<<7) : 0) |
(spi->cmd_tbl->block_erase_64 ? (1<<8) : 0);
return bm;
}
static uint32_t _spiflash_get_largest_erase_area(spiflash_t *spi, uint32_t addr, uint32_t len) {
uint32_t res = 0;
uint16_t bm = _spiflash_get_supported_block_mask(spi);
uint8_t bm_lz = _spiflash_clz(bm);
uint8_t addr_lz = _spiflash_clz(addr);
bm >>= bm_lz;
bm_lz += 8; // block mask starts at 256 bytes
// check length against smallest erase block
if ((len & ((1 << bm_lz) - 1)) != 0) return 0;
while (bm != 0) {
if (addr_lz >= bm_lz && len >= (uint32_t)(1 << bm_lz)) {
res = 1 << bm_lz;
}
bm >>= 1;
bm_lz++;
uint8_t new_bm_lz = _spiflash_clz(bm);
if (new_bm_lz == 32) new_bm_lz =0 ;
bm >>= new_bm_lz;
bm_lz += new_bm_lz;
}
return res;
}
static uint8_t _spiflash_get_erase_cmd(spiflash_t *spi, uint32_t len) {
if (len == 4*1024 && spi->cmd_tbl->block_erase_4)
return spi->cmd_tbl->block_erase_4;
else if (len == 8*1024 && spi->cmd_tbl->block_erase_8)
return spi->cmd_tbl->block_erase_8;
else if (len == 16*1024 && spi->cmd_tbl->block_erase_16)
return spi->cmd_tbl->block_erase_16;
else if (len == 32*1024 && spi->cmd_tbl->block_erase_32)
return spi->cmd_tbl->block_erase_32;
else if (len == 64*1024 && spi->cmd_tbl->block_erase_64)
return spi->cmd_tbl->block_erase_64;
else
return 0;
}
static uint32_t _spiflash_get_erase_time(spiflash_t *spi, uint32_t len) {
if (len == 4*1024)
return spi->cfg->block_erase_4_ms;
else if (len == 8*1024)
return spi->cfg->block_erase_8_ms;
else if (len == 16*1024)
return spi->cfg->block_erase_16_ms;
else if (len == 32*1024)
return spi->cfg->block_erase_32_ms;
else if (len == 64*1024)
return spi->cfg->block_erase_64_ms;
else
return 0;
}
static int _spiflash_begin_async(spiflash_t *spi) {
int res = SPIFLASH_OK;
if (spi->op == SPIFLASH_OP_IDLE) {
return SPIFLASH_ERR_BAD_STATE;
}
if (spi->busy_pre_check) {
// busy check: issue read sr
SPIF_DBG("precheck...\n");
spi->hal->_spiflash_spi_cs(spi, 1);
res = spi->hal->_spiflash_spi_txrx(spi, &spi->cmd_tbl->read_sr, 1, &spi->sr_data, 1);
return res;
}
switch (spi->op) {
case SPIFLASH_OP_WRITE_sWREN: {
// write: issue write enable
SPIF_DBG("write - enable...\n");
spi->hal->_spiflash_spi_cs(spi, 1);
res = spi->hal->_spiflash_spi_txrx(spi, &spi->cmd_tbl->write_enable, 1, 0, 0);
return res;
}
case SPIFLASH_OP_WRITE_sADDR: {
// write: issue write address
SPIF_DBG("write - address...\n");
spi->hal->_spiflash_spi_cs(spi, 1);
spi->tx_internal_buf[0] = spi->cmd_tbl->page_program;
_spiflash_compose_address(spi, spi->addr, &spi->tx_internal_buf[1]);
res = spi->hal->_spiflash_spi_txrx(spi,
&spi->tx_internal_buf[0],
1 + spi->cfg->addr_sz + spi->cfg->addr_dummy_sz,
0, 0);
return res;
}
case SPIFLASH_OP_WRITE_sDATA: {
// write: data ordered in pages
uint32_t rem_pg_sz = spi->cfg->page_sz - (spi->addr & (spi->cfg->page_sz - 1));
uint32_t wr_sz = spi->wr_len < rem_pg_sz ? spi->wr_len : rem_pg_sz;
SPIF_DBG("write - data %i of %i wait...\n", wr_sz, spi->wr_len);
const uint8_t *wr_buf = spi->wr_buf;
spi->wr_buf += wr_sz;
spi->wr_len -= wr_sz;
spi->addr += wr_sz;
spi->wait_period_ms = spi->cfg->page_program_ms;
spi->busy_check_wait = BCW_WAIT;
res = spi->hal->_spiflash_spi_txrx(spi, wr_buf, wr_sz, 0, 0);
return res;
}
case SPIFLASH_OP_ERASE_BLOCK_sWREN: {
// erase: issue write enable
SPIF_DBG("erase - enable...\n");
spi->hal->_spiflash_spi_cs(spi, 1);
res = spi->hal->_spiflash_spi_txrx(spi, &spi->cmd_tbl->write_enable, 1, 0, 0);
return res;
}
case SPIFLASH_OP_ERASE_BLOCK_sERAS: {
// erase: issue write address
uint32_t era_sz = _spiflash_get_largest_erase_area(spi, spi->addr, spi->erase_len);
SPIF_DBG("erase - address %08x size %08x wait...\n", spi->addr, era_sz);
spi->hal->_spiflash_spi_cs(spi, 1);
uint8_t cmd = _spiflash_get_erase_cmd(spi, era_sz);
uint32_t era_time =_spiflash_get_erase_time(spi, era_sz);
if (cmd == 0x00) return SPIFLASH_ERR_BAD_CONFIG;
spi->tx_internal_buf[0] = cmd;
_spiflash_compose_address(spi, spi->addr, &spi->tx_internal_buf[1]);
spi->addr += era_sz;
spi->erase_len -= era_sz;
spi->wait_period_ms = era_time;
spi->busy_check_wait = BCW_WAIT;
res = spi->hal->_spiflash_spi_txrx(spi,
&spi->tx_internal_buf[0],
1 + spi->cfg->addr_sz + spi->cfg->addr_dummy_sz,
0, 0);
return res;
}
case SPIFLASH_OP_WRITE_SR_sWREN: {
// write_sr: issue write enable
SPIF_DBG("write_sr - enable...\n");
spi->hal->_spiflash_spi_cs(spi, 1);
res = spi->hal->_spiflash_spi_txrx(spi, &spi->cmd_tbl->write_enable, 1, 0, 0);
return res;
}
case SPIFLASH_OP_WRITE_SR_sDATA: {
// write_sr: data
SPIF_DBG("write_sr - data wait...\n");
spi->tx_internal_buf[1] = spi->sr_data;
spi->tx_internal_buf[0] = spi->cmd_tbl->write_sr;
spi->hal->_spiflash_spi_cs(spi, 1);
spi->wait_period_ms = spi->cfg->sr_write_ms;
spi->busy_check_wait = BCW_WAIT;
res = spi->hal->_spiflash_spi_txrx(spi, &spi->tx_internal_buf[0], 2, 0, 0);
return res;
}
case SPIFLASH_OP_ERASE_CHIP_sWREN: {
// erase chip: issue write enable
SPIF_DBG("erase chip - enable...\n");
spi->hal->_spiflash_spi_cs(spi, 1);
res = spi->hal->_spiflash_spi_txrx(spi, &spi->cmd_tbl->write_enable, 1, 0, 0);
return res;
}
case SPIFLASH_OP_ERASE_CHIP_sERAS: {
// erase chip: cmd
SPIF_DBG("erase chip - command wait...\n");
spi->hal->_spiflash_spi_cs(spi, 1);
spi->tx_internal_buf[0] = spi->cmd_tbl->chip_erase;
spi->wait_period_ms = spi->cfg->chip_erase_ms;
spi->busy_check_wait = BCW_WAIT;
res = spi->hal->_spiflash_spi_txrx(spi, &spi->tx_internal_buf[0], 1, 0, 0);
return res;
}
case SPIFLASH_OP_READ: {
// read: issue address and read
SPIF_DBG("read - address and data...\n");
spi->hal->_spiflash_spi_cs(spi, 1);
spi->tx_internal_buf[0] = spi->cmd_tbl->read_data;
_spiflash_compose_address(spi, spi->addr, &spi->tx_internal_buf[1]);
res = spi->hal->_spiflash_spi_txrx(spi,
&spi->tx_internal_buf[0],
1 + spi->cfg->addr_sz + spi->cfg->addr_dummy_sz,
spi->rd_buf, spi->rd_len);
return res;
}
case SPIFLASH_OP_FAST_READ: {
// fast read: issue address and read
SPIF_DBG("read fast - address and data...\n");
spi->hal->_spiflash_spi_cs(spi, 1);
spi->tx_internal_buf[0] = spi->cmd_tbl->read_data_fast;
_spiflash_compose_address(spi, spi->addr, &spi->tx_internal_buf[1]);
spi->tx_internal_buf[1 + spi->cfg->addr_sz + 1] = 0; // dummy for fast read
res = spi->hal->_spiflash_spi_txrx(spi,
&spi->tx_internal_buf[0],
1 + spi->cfg->addr_sz + 1 + spi->cfg->addr_dummy_sz,
spi->rd_buf, spi->rd_len);
return res;
}
case SPIFLASH_OP_READ_JEDEC: {
// read_jedec
SPIF_DBG("read_jedec...\n");
spi->hal->_spiflash_spi_cs(spi, 1);
res = spi->hal->_spiflash_spi_txrx(spi, &spi->cmd_tbl->jedec_id, 1, (uint8_t *)spi->id_dst, 3);
return res;
}
case SPIFLASH_OP_READ_PRODUCT: {
// read_jedec
SPIF_DBG("read_prod...\n");
spi->hal->_spiflash_spi_cs(spi, 1);
res = spi->hal->_spiflash_spi_txrx(spi, &spi->cmd_tbl->device_id, 1, (uint8_t *)spi->id_dst, 3);
return res;
}
case SPIFLASH_OP_READ_SR:
case SPIFLASH_OP_READ_SR_BUSY: {
// read_sr
SPIF_DBG("read_sr...\n");
spi->hal->_spiflash_spi_cs(spi, 1);
res = spi->hal->_spiflash_spi_txrx(spi, &spi->cmd_tbl->read_sr, 1, (uint8_t *)spi->sr_dst, 1);
return res;
}
case SPIFLASH_OP_READ_REG: {
// read_reg
SPIF_DBG("read_reg...\n");
spi->hal->_spiflash_spi_cs(spi, 1);
res = spi->hal->_spiflash_spi_txrx(spi, &spi->reg_nbr, 1, (uint8_t *)spi->reg_dst, 1);
return res;
}
case SPIFLASH_OP_WRITE_REG_sWREN: {
// write_reg: issue write enable
SPIF_DBG("write_reg - enable...\n");
spi->hal->_spiflash_spi_cs(spi, 1);
res = spi->hal->_spiflash_spi_txrx(spi, &spi->cmd_tbl->write_enable, 1, 0, 0);
return res;
}
case SPIFLASH_OP_WRITE_REG_DATA:
case SPIFLASH_OP_WRITE_REG_sDATAWAIT: {
// write_reg: data
SPIF_DBG("write_reg - data%s...\n", spi->op == SPIFLASH_OP_WRITE_REG_DATA ? "" : " wait");
spi->hal->_spiflash_spi_cs(spi, 1);
spi->busy_check_wait = spi->op == SPIFLASH_OP_WRITE_REG_DATA ? BCW_IDLE : BCW_WAIT;
res = spi->hal->_spiflash_spi_txrx(spi, &spi->tx_internal_buf[0], 2, 0, 0);
return res;
}
case SPIFLASH_OP_IDLE:
default:
res = SPIFLASH_ERR_INTERNAL;
break;
} // switch (spi->op)
return res;
}
static int _spiflash_end_async(spiflash_t *spi, int res) {
// handle early termination
if (res != SPIFLASH_OK) {
_spiflash_finalize(spi);
return res;
}
// handle busy pre check
if (spi->busy_pre_check) {
if (_spiflash_is_hwbusy(spi, spi->sr_data)) {
spi->hal->_spiflash_spi_cs(spi, 0);
SPIF_DBG("precheck busy\n");
return SPIFLASH_ERR_HW_BUSY;
} else {
SPIF_DBG("precheck ok\n");
spi->busy_pre_check = 0;
return _spiflash_begin_async(spi);
}
}
// handle busy-check-wait states
switch (spi->busy_check_wait) {
case BCW_WAIT:
SPIF_DBG("busy check WAIT %i...\n", spi->wait_period_ms);
spi->hal->_spiflash_spi_cs(spi, 0);
// if wait period is 0, call wait and then break free of the bsw loop
spi->busy_check_wait = spi->wait_period_ms == 0 ? BCW_IDLE : BCW_READ_SR;
spi->hal->_spiflash_wait(spi, spi->wait_period_ms);
return SPIFLASH_OK;
case BCW_READ_SR:
SPIF_DBG("busy CHECK wait...\n");
spi->busy_check_wait = BCW_CHECK;
spi->hal->_spiflash_spi_cs(spi, 1);
res = spi->hal->_spiflash_spi_txrx(spi, &spi->cmd_tbl->read_sr, 1, &spi->sr_data, 1);
return res;
case BCW_CHECK:
spi->hal->_spiflash_spi_cs(spi, 0);
if (_spiflash_is_hwbusy(spi, spi->sr_data)) {
spi->wait_period_ms = DECR_WAIT(spi->wait_period_ms);
SPIF_DBG("BUSY check WAIT %i...\n", spi->wait_period_ms);
spi->busy_check_wait = BCW_READ_SR;
spi->hal->_spiflash_wait(spi, spi->wait_period_ms);
return SPIFLASH_OK;
} else {
SPIF_DBG("busy check wait ok\n");
spi->busy_check_wait = BCW_IDLE;
break;
}
case BCW_IDLE:
SPIF_DBG("no BCW\n");
break;
} // switch (spi->busy_check_wait)
// handle results
switch (spi->op) {
case SPIFLASH_OP_WRITE_sWREN:
SPIF_DBG("write - enable ok\n");
spi->hal->_spiflash_spi_cs(spi, 0);
spi->op = SPIFLASH_OP_WRITE_sADDR;
break;
case SPIFLASH_OP_WRITE_sADDR:
SPIF_DBG("write - address ok\n");
spi->op = SPIFLASH_OP_WRITE_sDATA;
break;
case SPIFLASH_OP_WRITE_sDATA:
if (spi->wr_len == 0) {
SPIF_DBG("write - data ok, finish\n");
spi->op = SPIFLASH_OP_IDLE;
} else {
SPIF_DBG("write - data ok, new chunk\n");
spi->op = SPIFLASH_OP_WRITE_sWREN;
}
break;
case SPIFLASH_OP_ERASE_BLOCK_sWREN:
SPIF_DBG("erase - enable ok\n");
spi->hal->_spiflash_spi_cs(spi, 0);
spi->op = SPIFLASH_OP_ERASE_BLOCK_sERAS;
break;
case SPIFLASH_OP_ERASE_BLOCK_sERAS:
SPIF_DBG("erase - ok\n");
if (spi->erase_len == 0) {
SPIF_DBG("erase - ok, finish\n");
spi->op = SPIFLASH_OP_IDLE;
} else {
SPIF_DBG("erase - ok, new chunk\n");
spi->op = SPIFLASH_OP_ERASE_BLOCK_sWREN;
}
break;
case SPIFLASH_OP_WRITE_SR_sWREN:
SPIF_DBG("write_sr - enable ok\n");
spi->hal->_spiflash_spi_cs(spi, 0);
spi->op = SPIFLASH_OP_WRITE_SR_sDATA;
break;
case SPIFLASH_OP_WRITE_SR_sDATA:
SPIF_DBG("write_sr - ok\n");
spi->op = SPIFLASH_OP_IDLE;
break;
case SPIFLASH_OP_ERASE_CHIP_sWREN:
SPIF_DBG("erase chip - enable ok\n");
spi->hal->_spiflash_spi_cs(spi, 0);
spi->op = SPIFLASH_OP_ERASE_CHIP_sERAS;
break;
case SPIFLASH_OP_ERASE_CHIP_sERAS:
SPIF_DBG("erase chip - ok\n");
spi->op = SPIFLASH_OP_IDLE;
break;
case SPIFLASH_OP_READ:
SPIF_DBG("read - ok\n");
spi->op = SPIFLASH_OP_IDLE;
break;
case SPIFLASH_OP_FAST_READ:
SPIF_DBG("fast read - ok\n");
spi->op = SPIFLASH_OP_IDLE;
break;
case SPIFLASH_OP_READ_JEDEC:
SPIF_DBG("read jedec ok\n");
spi->op = SPIFLASH_OP_IDLE;
break;
case SPIFLASH_OP_READ_PRODUCT:
SPIF_DBG("read prod ok\n");
spi->op = SPIFLASH_OP_IDLE;
break;
case SPIFLASH_OP_READ_SR_BUSY:
case SPIFLASH_OP_READ_SR:
SPIF_DBG("read sr ok\n");
if (spi->op == SPIFLASH_OP_READ_SR_BUSY) {
*spi->sr_dst = ((*spi->sr_dst & spi->cmd_tbl->sr_busy_bit)) != 0;
}
spi->op = SPIFLASH_OP_IDLE;
break;
case SPIFLASH_OP_WRITE_REG_sWREN:
SPIF_DBG("write_reg - enable ok\n");
spi->hal->_spiflash_spi_cs(spi, 0);
spi->op = SPIFLASH_OP_WRITE_REG_sDATAWAIT;
break;
case SPIFLASH_OP_WRITE_REG_sDATAWAIT:
case SPIFLASH_OP_WRITE_REG_DATA:
SPIF_DBG("write_reg - ok\n");
spi->op = SPIFLASH_OP_IDLE;
break;
case SPIFLASH_OP_READ_REG:
SPIF_DBG("read reg ok\n");
spi->op = SPIFLASH_OP_IDLE;
break;
case SPIFLASH_OP_IDLE:
default:
res = SPIFLASH_ERR_INTERNAL;
break;
} // switch (spi->op)
if (res == SPIFLASH_OK && spi->op != SPIFLASH_OP_IDLE) {
// moar to do
res = _spiflash_begin_async(spi);
} else {
// finished or error
spi->hal->_spiflash_spi_cs(spi, 0);
_spiflash_finalize(spi);
}
return res;
}
static int _spiflash_exe(spiflash_t *spi) {
int res = SPIFLASH_OK;
if (spi->could_be_busy) {
spi->busy_pre_check = 1;
}
if (spi->async) {
// asynchronous
res = _spiflash_begin_async(spi);
} else {
// blocking
res = _spiflash_begin_async(spi);
while (res == SPIFLASH_OK && spi->op != SPIFLASH_OP_IDLE) {
res = SPIFLASH_async_trigger(spi, res);
}
_spiflash_finalize(spi);
}
return res;
}
void SPIFLASH_init(spiflash_t *spi,
const spiflash_config_t *cfg,
const spiflash_cmd_tbl_t *cmd,
const spiflash_hal_t *hal,
spiflash_cb_async_t async_cb,
uint8_t async,
void *user_data) {
memset(spi, 0, sizeof(spiflash_t));
spi->cfg = cfg;
spi->cmd_tbl = cmd;
spi->hal = hal;
spi->async_cb = async_cb;
spi->async = async;
spi->user_data = user_data;
spi->op = SPIFLASH_OP_IDLE;
}
int SPIFLASH_async_trigger(spiflash_t *spi, int err_code) {
int res = _spiflash_end_async(spi, err_code);
spiflash_op_t op = spi->op;
if (res != SPIFLASH_OK || op == SPIFLASH_OP_IDLE) {
if (res != SPIFLASH_OK) {
spi->op = SPIFLASH_OP_IDLE;
}
if (spi->async && spi->async_cb) {
spi->async_cb(spi, op, res);
}
}
return res;
}
int SPIFLASH_write(spiflash_t *spi, uint32_t addr, uint32_t len, const uint8_t *buf) {
int res;
if (spi->op != SPIFLASH_OP_IDLE) {
return SPIFLASH_ERR_BUSY;
}
spi->addr = addr;
spi->wr_buf = buf;
spi->wr_len = len;
spi->op = SPIFLASH_OP_WRITE_sWREN;
res = _spiflash_exe(spi);
return res;
}
int SPIFLASH_read(spiflash_t *spi, uint32_t addr, uint32_t len, uint8_t *buf) {
int res;
if (spi->op != SPIFLASH_OP_IDLE) {
return SPIFLASH_ERR_BUSY;
}
spi->addr = addr;
spi->rd_buf = buf;
spi->rd_len = len;
spi->op = SPIFLASH_OP_READ;
res = _spiflash_exe(spi);
return res;
}
int SPIFLASH_fast_read(spiflash_t *spi, uint32_t addr, uint32_t len,
uint8_t *buf) {
int res;
if (spi->op != SPIFLASH_OP_IDLE) {
return SPIFLASH_ERR_BUSY;
}
spi->addr = addr;
spi->rd_buf = buf;
spi->rd_len = len;
spi->op = spi->cmd_tbl->read_data_fast ? SPIFLASH_OP_FAST_READ : SPIFLASH_OP_READ;
res = _spiflash_exe(spi);
return res;
}
int SPIFLASH_read_jedec_id(spiflash_t *spi, uint32_t *jedec_id) {
int res;
if (spi->op != SPIFLASH_OP_IDLE) {
return SPIFLASH_ERR_BUSY;
}
spi->id_dst = jedec_id;
spi->op = SPIFLASH_OP_READ_JEDEC;
res = _spiflash_exe(spi);
return res;
}
int SPIFLASH_read_product_id(spiflash_t *spi, uint32_t *prod_id) {
int res;
if (spi->op != SPIFLASH_OP_IDLE) {
return SPIFLASH_ERR_BUSY;
}
spi->id_dst = prod_id;
spi->op = SPIFLASH_OP_READ_PRODUCT;
res = _spiflash_exe(spi);
return res;
}
int SPIFLASH_read_sr(spiflash_t *spi, uint8_t *sr) {
int res;
if (spi->op != SPIFLASH_OP_IDLE) {
return SPIFLASH_ERR_BUSY;
}
spi->sr_dst = sr;
spi->op = SPIFLASH_OP_READ_SR;
res = _spiflash_exe(spi);
return res;
}
int SPIFLASH_read_sr_busy(spiflash_t *spi, uint8_t *busy) {
int res;
if (spi->op != SPIFLASH_OP_IDLE) {
return SPIFLASH_ERR_BUSY;
}
spi->sr_dst = busy;
spi->op = SPIFLASH_OP_READ_SR_BUSY;
res = _spiflash_exe(spi);
return res;
}
int SPIFLASH_write_sr(spiflash_t *spi, uint8_t sr) {
int res;
if (spi->op != SPIFLASH_OP_IDLE) {
return SPIFLASH_ERR_BUSY;
}
spi->sr_data = sr;
spi->op = SPIFLASH_OP_WRITE_SR_sWREN;
res = _spiflash_exe(spi);
return res;
}
int SPIFLASH_read_reg(spiflash_t *spi, uint8_t reg, uint8_t *data) {
int res;
if (spi->op != SPIFLASH_OP_IDLE) {
return SPIFLASH_ERR_BUSY;
}
spi->reg_nbr = reg;
spi->reg_dst = data;
spi->op = SPIFLASH_OP_READ_REG;
res = _spiflash_exe(spi);
return res;
}
int SPIFLASH_write_reg(spiflash_t *spi, uint8_t reg, uint8_t data,
uint8_t write_en, uint32_t wait_ms) {
int res;
if (spi->op != SPIFLASH_OP_IDLE) {
return SPIFLASH_ERR_BUSY;
}
spi->tx_internal_buf[0] = reg;
spi->tx_internal_buf[1] = data;
spi->op = write_en ? SPIFLASH_OP_WRITE_REG_sWREN : SPIFLASH_OP_WRITE_REG_DATA;
if (write_en) {
spi->wait_period_ms = wait_ms;
}
res = _spiflash_exe(spi);
return res;
}
int SPIFLASH_erase(spiflash_t *spi, uint32_t addr, uint32_t len) {
int res;
if (spi->op != SPIFLASH_OP_IDLE) {
return SPIFLASH_ERR_BUSY;
}
uint32_t era_sz = _spiflash_get_largest_erase_area(spi, addr, len);
if (era_sz == 0) {
return SPIFLASH_ERR_ERASE_UNALIGNED;
}
spi->addr = addr;
spi->erase_len = len;
spi->op = SPIFLASH_OP_ERASE_BLOCK_sWREN;
res = _spiflash_exe(spi);
return res;
}
int SPIFLASH_chip_erase(spiflash_t *spi) {
int res;
if (spi->op != SPIFLASH_OP_IDLE) {
return SPIFLASH_ERR_BUSY;
}
spi->op = SPIFLASH_OP_ERASE_CHIP_sWREN;
res = _spiflash_exe(spi);
return res;
}
int SPIFLASH_is_busy(spiflash_t *spi) {
return spi->op == SPIFLASH_OP_IDLE ? SPIFLASH_OK : SPIFLASH_ERR_BUSY;
}