1
WXK
2023-08-28 aa08ceb38550e9e6f1ee2d74e1d57339fc6888a6
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
/*******************************************************************************
 * File Name         : SPIFlash.c
 * Description       :
 * Created on        : 2019Äê3ÔÂ11ÈÕ
 * Author            : www.hido-studio.com
 *******************************************************************************/
 
/*******************************************************************************
 *                              Include Files                                  *
 *******************************************************************************/
#include "SPIFlash.h"
 
#include "stm32f1xx_hal.h"
#include "GPIO.h"
#include "HIDO_CRC32.h"
#include "string.h"
 
#ifdef _USE_OS_
#include "cmsis_os.h"
#endif
 
/*******************************************************************************
 *                                  Macro                                      *
 *******************************************************************************/
#define SPIFLASH_N25Q_SECTOR_COUNT                                   64
#define SPIFLASH_N25Q_SUBSECTOR_COUNT                                1024
#define SPIFLASH_N25Q_PAGE_COUNT                                     16384
 
#define SPIFLASH_N25Q_2MB_CAPACITY_VALUE                             0x15
#define SPIFLASH_N25Q_4MB_CAPACITY_VALUE                             0x16
#define SPIFLASH_N25Q_8MB_CAPACITY_VALUE                             0x17
#define SPIFLASH_N25Q_16MB_CAPACITY_VALUE                            0x18
 
#define SPIFLASH_N25Q_4MB_SIZE                                       0x400000
#define SPIFLASH_N25Q_8MB_SIZE                                       0x800000
#define SPIFLASH_N25Q_16MB_SIZE                                      0x1000000
 
#define SPIFLASH_N25Q_SECTOR_SIZE                                    0x10000
#define SPIFLASH_N25Q_HALF_SECTOR_SIZE                               0x8000
#define SPIFLASH_N25Q_SUBSECTOR_SIZE                                 0x1000
#define SPIFLASH_N25Q_PAGE_SIZE                                      0x100
#define SPIFLASH_N25Q_CHIP_LEFT_SIZE(id, addr)                       (l_astSPIFlashResourse[id].m_u32ChipSize - (addr & (l_astSPIFlashResourse[id].m_u32ChipSize - 1)))
#define SPIFLASH_N25Q_PAGE_LEFT_SIZE(addr)                           (SPIFLASH_N25Q_PAGE_SIZE - (addr & (SPIFLASH_N25Q_PAGE_SIZE - 1)))
#define SPIFLASH_N25Q_SUBSECTOR_LEFT_SIZE(addr)                      (SPIFLASH_N25Q_SUBSECTOR_SIZE - (addr & (SPIFLASH_N25Q_SUBSECTOR_SIZE - 1)))
#define SPIFLASH_N25Q_SECTOR_LEFT_SIZE(addr)                         (SPIFLASH_N25Q_SECTOR_SIZE - (addr & (SPIFLASH_N25Q_SECTOR_SIZE - 1)))
 
#define SPIFLASH_N25Q_SECTOR_ADDR(addr)                              ((addr) & (~(SPIFLASH_N25Q_SECTOR_SIZE - 1)))
#define SPIFLASH_N25Q_SUBSECTOR_ADDR(addr)                           ((addr) & (~(SPIFLASH_N25Q_SUBSECTOR_SIZE - 1)))
 
#define SPIFLASH_N25Q_CMD_READ_ID                                    0x9E
#define SPIFLASH_N25Q_CMD_READ                                       0x03
#define SPIFLASH_N25Q_CMD_FAST_READ                                  0x0B
#define SPIFLASH_N25Q_CMD_DUAL_OUTPUT_FAST_READ                      0x3B
 
#define SPIFLASH_N25Q_CMD_WRITE_ENABLE                               0x06
#define SPIFLASH_N25Q_CMD_WRITE_DISABLE                              0x04
#define SPIFLASH_N25Q_CMD_READ_STATUS_REGISTER                       0x05
#define SPIFLASH_N25Q_CMD_SUBSECTOR_ERASE                            0x20
#define SPIFLASH_N25Q_CMD_HALF_SECTOR_ERASE                          0x52
#define SPIFLASH_N25Q_CMD_SECTOR_ERASE                               0xD8
#define SPIFLASH_N25Q_CMD_BULK_ERASE                                 0xC7
#define SPIFLASH_N25Q_CMD_PAGE_PROGRAM                               0x02
#define SPIFLASH_N25Q_CMD_JEDEC_ID                                   0x9F
 
#define SPIFLASH_N25Q_CMD_POWER_DOWN                                 0xB9
#define SPIFLASH_N25Q_CMD_RELEASE_POWER_DOWN                         0xAB
 
#define SPIFLASH_N25Q_STATUS_REGISTER_IN_PROGRESS_BIT                0x01
#define SPIFLASH_N25Q_STATUS_REGISTER_IN_PROGRESS_READY              0
#define SPIFLASH_N25Q_STATUS_REGISTER_IN_PROGRESS_BUSY               1
 
#define SPIFLASH_DUMMY_BYTE                                          0xFF
 
/*******************************************************************************
 *                             Type Definition                                 *
 *******************************************************************************/
typedef struct
{
    E_SPI_ID m_eSPI_ID;
    ST_GPIO m_stPin[SPI_FLASH_PIN_LAST];
 
    HIDO_UINT32 m_u32ChipSize;
 
#ifdef _USE_OS_
    osMutexId m_mutexLock;
#endif
 
} ST_SPIFlashResourse;
 
/*******************************************************************************
 *                             Local Variable                                  *
 *******************************************************************************/
static ST_SPIFlashResourse l_astSPIFlashResourse[SPI_FLASH_ID_LAST];
 
 
/*******************************************************************************
 *                        Local Function Declaration                           *
 *******************************************************************************/
 
/*******************************************************************************
 *                             Local Function                                  *
 *******************************************************************************/
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
const static HIDO_UINT32 l_au32SPIFlashSectorEraseSize[SPI_FLASH_SECTOR_ERASE_MODE_LAST] =
{
    SPIFLASH_N25Q_SUBSECTOR_SIZE, SPIFLASH_N25Q_HALF_SECTOR_SIZE, SPIFLASH_N25Q_SECTOR_SIZE
};
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
const static HIDO_UINT8 l_au8SPIFlashSectorEraseCmd[SPI_FLASH_SECTOR_ERASE_MODE_LAST] =
{
    SPIFLASH_N25Q_CMD_SUBSECTOR_ERASE, SPIFLASH_N25Q_CMD_HALF_SECTOR_ERASE, SPIFLASH_N25Q_CMD_SECTOR_ERASE
};
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
static void SPIFlash_CSEnable(E_SPIFlashID _eID)
{
    HAL_GPIO_WritePin(l_astSPIFlashResourse[_eID].m_stPin[SPI_FLASH_PIN_CS].m_pstGPIOx, l_astSPIFlashResourse[_eID].m_stPin[SPI_FLASH_PIN_CS].m_u16GPIOPin, GPIO_PIN_RESET);
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
static void SPIFlash_CSDisable(E_SPIFlashID _eID)
{
    HAL_GPIO_WritePin(l_astSPIFlashResourse[_eID].m_stPin[SPI_FLASH_PIN_CS].m_pstGPIOx, l_astSPIFlashResourse[_eID].m_stPin[SPI_FLASH_PIN_CS].m_u16GPIOPin, GPIO_PIN_SET);
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
static HIDO_INT32 SPIFlash_SendByte(E_SPIFlashID _eID, HIDO_UINT8 _u8SendByte, HIDO_UINT8 *_pu8RecvByte)
{
    return SPI_ReadWrite(l_astSPIFlashResourse[_eID].m_eSPI_ID, _pu8RecvByte, &_u8SendByte, 1);
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
static HIDO_INT32 SPIFlash_WriteEnable(E_SPIFlashID _eID)
{
    HIDO_INT32 i32Result = HIDO_OK;
 
    SPIFlash_CSEnable(_eID);
    i32Result = SPIFlash_SendByte(_eID, SPIFLASH_N25Q_CMD_WRITE_ENABLE, HIDO_NULL);
    SPIFlash_CSDisable(_eID);
 
    return i32Result;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
static HIDO_INT32 SPIFlash_WriteDisable(E_SPIFlashID _eID)
{
    HIDO_INT32 i32Result = HIDO_OK;
 
    SPIFlash_CSEnable(_eID);
    i32Result = SPIFlash_SendByte(_eID, SPIFLASH_N25Q_CMD_WRITE_DISABLE, HIDO_NULL);
    SPIFlash_CSDisable(_eID);
 
    return i32Result;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
static HIDO_INT32 SPIFlash_ReadStatusRegister(E_SPIFlashID _eID, HIDO_UINT8 *_pu8Status)
{
    HIDO_INT32 i32Result = HIDO_OK;
 
    SPIFlash_CSEnable(_eID);
    i32Result = SPIFlash_SendByte(_eID, SPIFLASH_N25Q_CMD_READ_STATUS_REGISTER, HIDO_NULL);
    if(HIDO_OK == i32Result)
    {
        i32Result = SPIFlash_SendByte(_eID, SPIFLASH_DUMMY_BYTE, _pu8Status);
    }
    SPIFlash_CSDisable(_eID);
 
    return i32Result;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
static HIDO_INT32 SPIFlash_WaitBusy(E_SPIFlashID _eID)
{
    HIDO_INT32 i32Result = HIDO_OK;
    HIDO_UINT8 u8Status = 0;
 
    do
    {
        i32Result = SPIFlash_ReadStatusRegister(_eID, &u8Status);
        if (i32Result != HIDO_OK)
        {
            break;
        }
 
        //TODO MCU_IWDGFeed();
    }
    while ((u8Status & SPIFLASH_N25Q_STATUS_REGISTER_IN_PROGRESS_BIT) == SPIFLASH_N25Q_STATUS_REGISTER_IN_PROGRESS_BUSY);
 
    return i32Result;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
static HIDO_INT32 SPIFlash_PageProgram(E_SPIFlashID _eID, HIDO_UINT32 _u32DstAddr, HIDO_UINT8 *_pu8SrcBuf, HIDO_UINT32 _u32SrcLen, HIDO_UINT32 *_pu32ProgramLen)
{
    HIDO_UINT32 i = 0;
    HIDO_UINT32 u32ProgramLen = 0;
 
    u32ProgramLen = SPIFLASH_N25Q_PAGE_LEFT_SIZE(_u32DstAddr);
    if (u32ProgramLen > _u32SrcLen)
    {
        u32ProgramLen = _u32SrcLen;
    }
 
    if(SPIFlash_WaitBusy(_eID) != HIDO_OK)
    {
        goto err;
    }
 
    if(SPIFlash_WriteEnable(_eID) != HIDO_OK)
    {
        goto err;
    }
 
    SPIFlash_CSEnable(_eID);
 
    if (SPIFlash_SendByte(_eID, SPIFLASH_N25Q_CMD_PAGE_PROGRAM, HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    if (SPIFlash_SendByte(_eID, (HIDO_UINT8) ((_u32DstAddr) >> 16), HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    if (SPIFlash_SendByte(_eID, (HIDO_UINT8) ((_u32DstAddr) >> 8), HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    if (SPIFlash_SendByte(_eID, (HIDO_UINT8) _u32DstAddr, HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    for (i = 0; i < u32ProgramLen; i++)
    {
        if (SPIFlash_SendByte(_eID, _pu8SrcBuf[i], HIDO_NULL) != HIDO_OK)
        {
            goto err_cs_disable;
        }
    }
 
    SPIFlash_CSDisable(_eID);
 
    if(SPIFlash_WriteDisable(_eID) != HIDO_OK)
    {
        goto err;
    }
 
    if(SPIFlash_WaitBusy(_eID) != HIDO_OK)
    {
        goto err;
    }
 
    if(_pu32ProgramLen != HIDO_NULL)
    {
        *_pu32ProgramLen = u32ProgramLen;
    }
 
    return HIDO_OK;
 
err:
    return HIDO_ERR;
 
err_cs_disable:
    SPIFlash_CSDisable(_eID);
    return HIDO_ERR;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
static HIDO_INT32 SPIFlash_SectorErase(E_SPIFlashID _eID, HIDO_UINT32 _u32SectorAddr, E_SPIFlashSectorEraseMode _eEraseMode)
{
    _u32SectorAddr = _u32SectorAddr & (~(l_au32SPIFlashSectorEraseSize[_eEraseMode] - 1));
 
    if(SPIFlash_WriteEnable(_eID) != HIDO_OK)
    {
        goto err;
    }
 
    SPIFlash_CSEnable(_eID);
 
    if (SPIFlash_SendByte(_eID, l_au8SPIFlashSectorEraseCmd[_eEraseMode], HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    if (SPIFlash_SendByte(_eID, (HIDO_UINT8) ((_u32SectorAddr) >> 16), HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    if (SPIFlash_SendByte(_eID, (HIDO_UINT8) ((_u32SectorAddr) >> 8), HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    if (SPIFlash_SendByte(_eID, (HIDO_UINT8) _u32SectorAddr, HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    SPIFlash_CSDisable(_eID);
 
    if(SPIFlash_WriteDisable(_eID) != HIDO_OK)
    {
        goto err;
    }
 
    if(SPIFlash_WaitBusy(_eID) != HIDO_OK)
    {
        goto err;
    }
 
    return HIDO_OK;
 
err:
    return HIDO_ERR;
 
err_cs_disable:
    SPIFlash_CSDisable(_eID);
    return HIDO_ERR;
}
 
/*******************************************************************************
 *                             Global Function                                 *
 *******************************************************************************/
 
/*******************************************************************************
 * Function Name     : 
 * Description       : 
 * Input             : 
 * Output            : 
 * Return            : 
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
HIDO_INT32 SPIFlash_BulkErase(E_SPIFlashID _eID)
{
    if(SPIFlash_WriteEnable(_eID) != HIDO_OK)
    {
        goto err;
    }
 
    SPIFlash_CSEnable(_eID);
 
    if (SPIFlash_SendByte(_eID, SPIFLASH_N25Q_CMD_BULK_ERASE, HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    SPIFlash_CSDisable(_eID);
 
    if(SPIFlash_WriteDisable(_eID) != HIDO_OK)
    {
        goto err;
    }
 
    if(SPIFlash_WaitBusy(_eID) != HIDO_OK)
    {
        goto err;
    }
 
    return HIDO_OK;
 
err:
    return HIDO_ERR;
 
err_cs_disable:
    SPIFlash_CSDisable(_eID);
    return HIDO_ERR;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
HIDO_INT32 SPIFlash_Read(E_SPIFlashID _eID, HIDO_UINT8 *_pu8DstBuf, HIDO_UINT32 _u32SrcAddr, HIDO_UINT32 _u32SrcLen)
{
    HIDO_INT32 i = 0;
 
#ifdef _USE_OS_
    osMutexWait(l_astSPIFlashResourse[_eID].m_mutexLock, osWaitForever);
#endif
    SPIFlash_CSEnable(_eID);
 
    if (SPIFlash_SendByte(_eID, SPIFLASH_N25Q_CMD_READ, HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    if (SPIFlash_SendByte(_eID, (HIDO_UINT8) ((_u32SrcAddr) >> 16), HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    if (SPIFlash_SendByte(_eID, (HIDO_UINT8) ((_u32SrcAddr) >> 8), HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    if (SPIFlash_SendByte(_eID, (HIDO_UINT8) _u32SrcAddr, HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    for (i = 0; i < _u32SrcLen; i++)
    {
        if (SPIFlash_SendByte(_eID, SPIFLASH_DUMMY_BYTE, &_pu8DstBuf[i]) != HIDO_OK)
        {
            goto err_cs_disable;
        }
    }
 
    SPIFlash_CSDisable(_eID);
#ifdef _USE_OS_
    osMutexRelease(l_astSPIFlashResourse[_eID].m_mutexLock);
#endif
    return HIDO_OK;
 
err_cs_disable:
    SPIFlash_CSDisable(_eID);
#ifdef _USE_OS_
    osMutexRelease(l_astSPIFlashResourse[_eID].m_mutexLock);
#endif
    return HIDO_ERR;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
HIDO_INT32 SPIFlash_Write(E_SPIFlashID _eID, HIDO_UINT32 _u32DstAddr, HIDO_UINT8 *_pu8SrcBuf, HIDO_UINT32 _u32SrcLen)
{
    HIDO_UINT32 u32ProgramLen = 0;
    HIDO_UINT32 u32PageProgramLen = 0;
 
    if (_u32DstAddr >= l_astSPIFlashResourse[_eID].m_u32ChipSize || HIDO_NULL == _pu8SrcBuf || 0 == _u32SrcLen)
    {
        return HIDO_ERR;
    }
 
    if (_u32SrcLen > SPIFLASH_N25Q_CHIP_LEFT_SIZE(_eID, _u32DstAddr))
    {
        _u32SrcLen = SPIFLASH_N25Q_CHIP_LEFT_SIZE(_eID, _u32DstAddr);
    }
 
#ifdef _USE_OS_
    osMutexWait(l_astSPIFlashResourse[_eID].m_mutexLock, osWaitForever);
#endif
    while (_u32SrcLen != 0)
    {
        if (SPIFlash_PageProgram(_eID, _u32DstAddr, _pu8SrcBuf, _u32SrcLen, &u32PageProgramLen) != HIDO_OK)
        {
#ifdef _USE_OS_
            osMutexRelease(l_astSPIFlashResourse[_eID].m_mutexLock);
#endif
            return HIDO_ERR;
        }
 
        _u32DstAddr += u32PageProgramLen;
        _pu8SrcBuf += u32PageProgramLen;
        _u32SrcLen -= u32PageProgramLen;
        u32ProgramLen += u32PageProgramLen;
    }
 
#ifdef _USE_OS_
    osMutexRelease(l_astSPIFlashResourse[_eID].m_mutexLock);
#endif
    return HIDO_OK;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
HIDO_INT32 SPIFlash_Erase(E_SPIFlashID _eID, HIDO_UINT32 _u32EraseAddr, HIDO_UINT32 _u32EraseLen, E_SPIFlashSectorEraseMode _eEraseMode)
{
    HIDO_UINT32 u32TempLen = 0;
 
    if (_u32EraseAddr >= l_astSPIFlashResourse[_eID].m_u32ChipSize || 0 == _u32EraseLen || (_u32EraseAddr + _u32EraseLen) > l_astSPIFlashResourse[_eID].m_u32ChipSize
            || ((_u32EraseAddr & (l_au32SPIFlashSectorEraseSize[_eEraseMode] - 1)) != 0) || _eEraseMode >= SPI_FLASH_SECTOR_ERASE_MODE_LAST)
    {
        return HIDO_ERR;
    }
 
#ifdef _USE_OS_
    osMutexWait(l_astSPIFlashResourse[_eID].m_mutexLock, osWaitForever);
#endif
    if (0 == _u32EraseAddr && _u32EraseLen >= l_astSPIFlashResourse[_eID].m_u32ChipSize)
    {
        SPIFlash_BulkErase(_eID);
 
#ifdef _USE_OS_
        osMutexRelease(l_astSPIFlashResourse[_eID].m_mutexLock);
#endif
        return HIDO_OK;
    }
 
    while (u32TempLen < _u32EraseLen)
    {
        SPIFlash_SectorErase(_eID, _u32EraseAddr + u32TempLen, _eEraseMode);
        u32TempLen += l_au32SPIFlashSectorEraseSize[_eEraseMode];
    }
 
#ifdef _USE_OS_
    osMutexRelease(l_astSPIFlashResourse[_eID].m_mutexLock);
#endif
    return HIDO_OK;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
HIDO_INT32 SPIFlash_ReadJedecID(E_SPIFlashID _eID, HIDO_UINT8 *_pu8IDBuf, HIDO_UINT32 _u32IDBufSize)
{
    HIDO_INT32 i = 0;
 
#ifdef _USE_OS_
    osMutexWait(l_astSPIFlashResourse[_eID].m_mutexLock, osWaitForever);
#endif
    SPIFlash_CSEnable(_eID);
 
    if (SPIFlash_SendByte(_eID, SPIFLASH_N25Q_CMD_JEDEC_ID, HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    for (i = 0; i < _u32IDBufSize; i++)
    {
        if (SPIFlash_SendByte(_eID, SPIFLASH_DUMMY_BYTE, &_pu8IDBuf[i]) != HIDO_OK)
        {
            goto err_cs_disable;
        }
    }
 
    SPIFlash_CSDisable(_eID);
#ifdef _USE_OS_
    osMutexRelease(l_astSPIFlashResourse[_eID].m_mutexLock);
#endif
    return HIDO_OK;
 
err_cs_disable:
    SPIFlash_CSDisable(_eID);
#ifdef _USE_OS_
    osMutexRelease(l_astSPIFlashResourse[_eID].m_mutexLock);
#endif
    return HIDO_ERR;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
HIDO_INT32 SPIFlash_PowerDown(E_SPIFlashID _eID)
{
#ifdef _USE_OS_
    osMutexWait(l_astSPIFlashResourse[_eID].m_mutexLock, osWaitForever);
#endif
    SPIFlash_CSEnable(_eID);
 
    if (SPIFlash_SendByte(_eID, SPIFLASH_N25Q_CMD_POWER_DOWN, HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    SPIFlash_CSDisable(_eID);
    HAL_Delay(1);
#ifdef _USE_OS_
    osMutexRelease(l_astSPIFlashResourse[_eID].m_mutexLock);
#endif
    return HIDO_OK;
 
err_cs_disable:
    SPIFlash_CSDisable(_eID);
#ifdef _USE_OS_
    osMutexRelease(l_astSPIFlashResourse[_eID].m_mutexLock);
#endif
    return HIDO_ERR;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
HIDO_INT32 SPIFlash_ReleasePowerDown(E_SPIFlashID _eID)
{
#ifdef _USE_OS_
    osMutexWait(l_astSPIFlashResourse[_eID].m_mutexLock, osWaitForever);
#endif
    SPIFlash_CSEnable(_eID);
 
    if (SPIFlash_SendByte(_eID, SPIFLASH_N25Q_CMD_RELEASE_POWER_DOWN, HIDO_NULL) != HIDO_OK)
    {
        goto err_cs_disable;
    }
 
    SPIFlash_CSDisable(_eID);
    HAL_Delay(1);
#ifdef _USE_OS_
    osMutexRelease(l_astSPIFlashResourse[_eID].m_mutexLock);
#endif
    return HIDO_OK;
 
err_cs_disable:
    SPIFlash_CSDisable(_eID);
#ifdef _USE_OS_
    osMutexRelease(l_astSPIFlashResourse[_eID].m_mutexLock);
#endif
    return HIDO_ERR;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
HIDO_INT32 SPIFlash_CalcCRC32(E_SPIFlashID _eID, HIDO_UINT32 _u32StartAddr, HIDO_UINT32 _u32CalcLen, HIDO_UINT32 *_pu32CRC32)
{
    HIDO_UINT8 au8Buf[512];
    HIDO_UINT32 u32CRC32 = 0xFFFFFFFF;
    HIDO_UINT32 u32ReadLen = 0;
 
    while (_u32CalcLen != 0)
    {
        if (_u32CalcLen > sizeof(au8Buf))
        {
            u32ReadLen = sizeof(au8Buf);
        }
        else
        {
            u32ReadLen = _u32CalcLen;
        }
 
        if (SPIFlash_Read(_eID, au8Buf, _u32StartAddr, u32ReadLen) != HIDO_OK)
        {
            return HIDO_ERR;
        }
 
        u32CRC32 = HIDO_CRC32SubCalc(u32CRC32, au8Buf, u32ReadLen);
        _u32StartAddr += u32ReadLen;
        _u32CalcLen -= u32ReadLen;
    }
 
    if (_pu32CRC32 != HIDO_NULL)
    {
        *_pu32CRC32 = u32CRC32 ^ 0xFFFFFFFF;
    }
 
    return HIDO_OK;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
HIDO_VOID SPIFlash_SPIRegister(E_SPIFlashID _eID, E_SPI_ID _eSPI_ID)
{
    l_astSPIFlashResourse[_eID].m_eSPI_ID = _eSPI_ID;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
HIDO_VOID SPIFlash_PinRegister(E_SPIFlashID _eID, E_SPIFlashPin _ePin, HIDO_VOID *_pGPIO, HIDO_UINT16 _u16Pin)
{
    l_astSPIFlashResourse[_eID].m_stPin[_ePin].m_pstGPIOx = (GPIO_TypeDef *)_pGPIO;
    l_astSPIFlashResourse[_eID].m_stPin[_ePin].m_u16GPIOPin = _u16Pin;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
HIDO_INT32 SPIFlash_UnInit(E_SPIFlashID _eID)
{
    HAL_GPIO_DeInit(l_astSPIFlashResourse[_eID].m_stPin[SPI_FLASH_PIN_CS].m_pstGPIOx, l_astSPIFlashResourse[_eID].m_stPin[SPI_FLASH_PIN_CS].m_u16GPIOPin);
    HAL_GPIO_DeInit(l_astSPIFlashResourse[_eID].m_stPin[SPI_FLASH_PIN_MISO].m_pstGPIOx, l_astSPIFlashResourse[_eID].m_stPin[SPI_FLASH_PIN_MISO].m_u16GPIOPin);
    HAL_GPIO_DeInit(l_astSPIFlashResourse[_eID].m_stPin[SPI_FLASH_PIN_MOSI].m_pstGPIOx, l_astSPIFlashResourse[_eID].m_stPin[SPI_FLASH_PIN_MOSI].m_u16GPIOPin);
    HAL_GPIO_DeInit(l_astSPIFlashResourse[_eID].m_stPin[SPI_FLASH_PIN_SCK].m_pstGPIOx, l_astSPIFlashResourse[_eID].m_stPin[SPI_FLASH_PIN_SCK].m_u16GPIOPin);
 
    return HIDO_OK;
}
 
/*******************************************************************************
 * Function Name     :
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê3ÔÂ11ÈÕ
 *******************************************************************************/
HIDO_INT32 SPIFlash_Init(E_SPIFlashID _eID)
{
    HIDO_UINT8 au8JedecID[3];
 
#ifdef _USE_OS_
    l_astSPIFlashResourse[_eID].m_mutexLock = osMutexCreate(HIDO_NULL);
#endif
 
    if (SPIFlash_ReadJedecID(_eID, au8JedecID, 3) != HIDO_OK)
    {
        return HIDO_ERR;
    }
 
    if (0xFF == au8JedecID[0] && 0xFF == au8JedecID[1] && 0xFF == au8JedecID[2])
    {
        if (SPIFlash_ReadJedecID(_eID, au8JedecID, 3) != HIDO_OK)
        {
            return HIDO_ERR;
        }
    }
 
    switch (au8JedecID[2])
    {
        case SPIFLASH_N25Q_2MB_CAPACITY_VALUE:
        {
            l_astSPIFlashResourse[_eID].m_u32ChipSize = SPIFLASH_N25Q_4MB_SIZE;
            break;
        }
        case SPIFLASH_N25Q_4MB_CAPACITY_VALUE:
        {
            l_astSPIFlashResourse[_eID].m_u32ChipSize = SPIFLASH_N25Q_4MB_SIZE;
            break;
        }
        case SPIFLASH_N25Q_8MB_CAPACITY_VALUE:
        {
            l_astSPIFlashResourse[_eID].m_u32ChipSize = SPIFLASH_N25Q_8MB_SIZE;
            break;
        }
        case SPIFLASH_N25Q_16MB_CAPACITY_VALUE:
        {
            l_astSPIFlashResourse[_eID].m_u32ChipSize = SPIFLASH_N25Q_16MB_SIZE;
            break;
        }
        default:
        {
            l_astSPIFlashResourse[_eID].m_u32ChipSize = SPIFLASH_N25Q_4MB_SIZE;
            break;
        }
    }
 
    return HIDO_OK;
}