This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

USB MSC Corruption

Hello,

I have been experiencing corruption using a similar implementation to the USBD_MSC example. Even after the various SDK fixes suggested to the same problem outlined in the forum.

I have edited the example to use a repeating timer with the scheduler to write an incrementing index, and two random numbers to a log_data.txt file.

i.e. sprintf(log_record, "%d,%d,%d\r\n", record_number,rand(),rand());

1,3542,16169

2,16521,11936

3,6713,17489

etc...

However, randomly in the log, symbols such as ' ' will replace anywhere between 8 and 18ish lines of data with symbols.

Using FATFS, the first few lines of a log was randomly being deleted/replaced with these non-characters, and symbols.

Upon switching to exFAT, as suggested by another user, the corruption still occurs, but somewhere in the middle of the log.

I have included the application for the nRF52840 DK with my function 'log_data_write_handler'.

Running this on the PCA10056 will create a log every 500ms. It is very reproducable that I have corruption happen anywhere between 200 and 1000 logs in. The corruption is shown in corrupt_log.txt by line 854.

1,32225,17543
2,25089,21183
3,25137,25566
4,26966,4978
5,20495,10311
6,11367,30054
7,17031,13145
8,19882,25736
9,30524,28505
10,28394,22102
11,24851,19067
12,12754,11653
13,6561,27096
14,13628,15188
15,32085,4143
16,6967,31406
17,24165,13403
18,25562,24834
19,31353,920
20,10444,24803
21,7962,19318
22,1422,31327
23,10457,1945
24,14479,29983
25,18751,3894
26,18670,8259
27,16248,7757
28,15629,13306
29,28606,13990
30,11738,12516
31,1414,5262
32,17116,22825
33,3181,13134
34,25343,8022
35,11233,7536
36,9760,9979
37,29071,1201
38,21336,13061
39,22160,24005
40,30729,7644
41,27475,31693
42,25514,14139
43,22088,26521
44,5202,9171
45,4434,28317
46,24582,6815
47,4586,9653
48,26306,7174
49,18451,23448
50,6473,32434
51,8193,14110
52,24748,28210
53,29320,32049
54,12956,14162
55,4166,14997
56,7793,32310
57,21391,19799
58,7926,14905
59,25885,2582
60,15610,5000
61,8052,30965
62,20120,32380
63,15639,26204
64,24385,12475
65,15725,17265
66,3214,19471
67,11376,4697
68,25543,23297
69,14619,23087
70,3123,31549
71,18065,24256
72,18973,20901
73,25613,6157
74,9899,9267
75,22413,9598
76,18526,13711
77,10046,14566
78,18536,15988
79,19878,13626
80,4273,8387
81,1171,32017
82,3752,12388
83,21191,11483
84,18122,11744
85,18528,15585
86,5363,20159
87,5641,18176
88,9575,28578
89,27363,27685
90,29344,19489
91,17713,5511
92,21461,22626
93,8645,3496
94,26703,6270
95,13870,11529
96,27499,4500
97,8607,5808
98,15725,12457
99,16542,16474
100,11531,17222
101,3952,17024
102,19894,24015
103,18247,11276
104,26278,19365
105,8746,21976
106,18092,25851
107,29088,29163
108,2231,26233
109,29732,21106
110,5411,9874
111,5448,9344
112,27589,17574
113,1191,6789
114,695,11735
115,20364,17040
116,17892,5035
117,26979,1092
118,850,12390
119,20195,668
120,20531,29989
121,12281,23902
122,12970,32186
123,19571,3680
124,7261,26187
125,28529,24190
126,446,24233
127,13708,11863
128,6681,6001
129,2499,3786
130,5214,25829
131,1322,25907
132,19628,13192
133,7505,5990
134,20129,10875
135,19829,31591
136,12388,6042
137,9833,1775
138,4719,342
139,28994,31392
140,20509,31313
141,20677,1282
142,11511,4795
143,3474,6469
144,2750,879
145,30989,30134
146,29752,28364
147,4880,5629
148,2235,21332
149,24145,3356
150,5243,3079
151,3988,807
152,24979,31357
153,914,21187
154,3540,14022
155,10149,609
156,29009,24833
157,16696,5432
158,24999,28863
159,16369,28676
160,24077,7701
161,1691,19840
162,28703,16515
163,22229,32420
164,19817,16264
165,19324,29343
166,1462,28929
167,3546,17043
168,18967,325
169,12683,15634
170,6322,16642
171,25395,11612
172,22864,29910
173,21985,23126
174,13988,685
175,6978,31050
176,16476,6365
177,21126,4193
178,8681,11011
179,23058,1249
180,31247,24731
181,28650,20774
182,7980,20833
183,27868,7778
184,23624,11115
185,1645,15892
186,29408,7939
187,17285,16202
188,28018,11334
189,4058,7062
190,3784,11901
191,6684,14289
192,27141,16702
193,26853,13458
194,28528,23363
195,21087,19052
196,31235,15109
197,17075,11755
198,10675,288
199,32053,14157
200,5758,5222
201,17488,18945
202,10294,11200
203,5171,14305
204,7951,6601
205,23608,7214
206,6377,13865
207,25369,27215
208,8030,177
209,16849,11337
210,2699,23099
211,8531,11517
212,17567,28479
213,9966,2597
214,14885,12341
215,15227,27149
216,785,29615
217,6476,20753
218,4236,7730
219,19668,21210
220,27519,27608
221,5142,6999
222,20449,14246
223,18638,2941
224,12481,23726
225,16738,26047
226,28947,3300
227,21639,17996
228,23866,14785
229,27571,25356
230,12633,27289
231,20551,20312
232,14426,7357
233,8056,16252
234,20410,2384
235,4353,2029
236,23579,27882
237,31882,21577
238,31368,11502
239,18902,21012
240,31365,30379
241,14256,19244
242,27870,13365
243,9619,27665
244,21293,29441
245,19484,12759
246,15400,6211
247,4144,15335
248,22704,32520
249,23789,32121
250,21913,23571
251,12369,2770
252,1594,887
253,18093,32317
254,11188,27640
255,4295,12490
256,25859,3106
257,24786,17097
258,5062,27943
259,31247,12292
260,16846,3962
261,28479,27584
262,21352,32044
263,31695,16498
264,4714,9737
265,17346,28482
266,24064,24946
267,9822,19265
268,2868,12792
269,3990,19561
270,16959,9917
271,29754,22104
272,25745,30347
273,25825,11980
274,18338,12465
275,25490,24541
276,20599,8097
277,26677,510
278,15347,14667
279,16316,10673
280,8163,14647
281,12817,29965
282,28871,16485
283,6846,29937
284,6241,5861
285,24670,28166
286,26879,27445
287,30407,11638
288,31113,11101
289,22516,28014
290,13898,21452
291,8117,1364
292,12926,20465
293,13845,25032
294,1696,18585
295,13455,92
296,18288,31755
297,29167,27412
298,25030,31717
299,24842,28572
300,26393,27828
301,11805,3660
302,5838,9046
303,18182,23772
304,12354,15377
305,14885,22759
306,22934,1903
307,6159,26145
308,10045,26847
309,17401,29783
310,4167,781
311,24287,2347
312,23402,19964
313,14711,10637
314,20693,29417
315,30937,24313
316,15541,2528
317,23019,21618
318,3716,5402
319,16270,18242
320,8468,22045
321,30602,18286
322,30559,22674
323,25146,1019
324,31243,20197
325,26870,7908
326,448,7327
327,28485,17622
328,14890,10883
329,10943,1201
330,10701,24682
331,3524,8895
332,27276,24798
333,12511,25522
334,24311,25209
335,26359,2912
336,28759,17148
337,26683,2877
338,8571,12334
339,16331,7324
340,577,635
341,26663,17061
342,20451,9544
343,16984,28693
344,19975,10519
345,21472,19641
346,9349,23391
347,30563,22184
348,19958,41
349,27666,23363
350,25344,2410
351,28635,1006
352,20068,26660
353,20562,29323
354,13764,21307
355,7773,10683
356,9013,8216
357,28834,15571
358,15241,2303
359,24754,928
360,3417,32048
361,12018,19931
362,17621,22209
363,14669,21098
364,20555,4662
365,12444,22109
366,16760,25574
367,14544,18339
368,30610,18189
369,24549,18320
370,24081,25264
371,6022,24847
372,19521,24628
373,7552,26278
374,20873,4029
375,32489,13359
376,31352,30279
377,8717,20560
378,23189,8583
379,795,10687
380,26339,23935
381,3988,27703
382,27958,10212
383,19567,10553
384,13368,32047
385,13068,20611
386,4154,23429
387,28895,13393
388,30908,5960
389,30169,5462
390,27323,23929
391,32149,29305
392,9149,32724
393,32584,6224
394,413,26281
395,12389,11357
396,4887,17532
397,1555,12299
398,9490,26460
399,24911,14183
400,22704,17412
401,20961,20382
402,3608,5811
403,7168,20794
404,12286,13771
405,27844,6684
406,11255,29227
407,26960,27448
408,9086,8526
409,1211,26131
410,8645,11552
411,18627,26110
412,25149,17828
413,32048,12559
414,29668,4167
415,23050,15588
416,17746,28929
417,12410,791
418,25985,18737
419,13952,8576
420,1375,18485
421,24922,16941
422,1557,10700
423,31408,1826
424,8213,32301
425,23680,30161
426,28390,22500
427,32716,7768
428,28079,31599
429,18949,31357
430,12421,27804
431,17717,16490
432,26228,30112
433,12016,9512
434,28751,1015
435,2553,19164
436,6200,30464
437,7859,22212
438,13806,15444
439,5710,27377
440,19417,21994
441,7341,32706
442,26839,9707
443,13593,22820
444,23236,7254
445,11688,5003
446,32203,8788
447,23915,1199
448,13424,24408
449,6490,27655
450,21593,13310
451,3072,12038
452,5087,1697
453,23526,7448
454,17497,15802
455,19649,30555
456,25891,1020
457,18267,14366
458,23408,12848
459,29762,22042
460,23252,31440
461,21810,32626
462,19088,13158
463,2370,2156
464,13213,22434
465,10193,2541
466,887,8766
467,24112,27574
468,12206,22418
469,15373,22113
470,1582,7670
471,23202,18714
472,13897,32731
473,19105,20253
474,24201,29608
475,29918,4338
476,11064,1366
477,22970,15723
478,20907,32202
479,23379,1626
480,29779,9206
481,32365,11539
482,15233,29614
483,19999,23205
484,12028,26186
485,20029,16343
486,7237,10497
487,10377,14564
488,5540,23808
489,152,28918
490,22011,15693
491,6790,16740
492,5446,18015
493,16473,18604
494,6645,15225
495,4662,29999
496,4842,9674
497,11590,32687
498,1616,10566
499,21862,4489
500,24865,19266
501,3473,4785
502,29558,1746
503,19213,23410
504,25997,19171
505,12120,17891
506,29084,15895
507,29905,23336
508,28248,25699
509,31271,6254
510,30276,619
511,6274,1956
512,14266,23192
513,12403,10234
514,14779,8191
515,6556,25114
516,8566,15090
517,24863,1065
518,17561,15202
519,712,380
520,5211,5371
521,2297,29210
522,11590,253
523,16217,3574
524,6085,8667
525,25660,8169
526,1393,17558
527,1232,11377
528,5916,23512
529,28685,18476
530,30617,4304
531,27738,23058
532,22353,1488
533,7168,17781
534,5255,9896
535,15439,14266
536,7270,8896
537,11154,5589
538,20688,27895
539,25012,17798
540,32485,23871
541,18351,20053
542,12885,20469
543,6584,3662
544,29030,24321
545,16428,32381
546,11716,4274
547,19256,17192
548,30220,6230
549,32075,975
550,13335,1180
551,4157,18148
552,1031,30635
553,8508,19786
554,28690,17662
555,28539,10640
556,7441,4358
557,22937,3818
558,13255,20093
559,17873,5107
560,4850,13708
561,24807,25893
562,5652,4252
563,24526,8980
564,31998,31484
565,12313,26733
566,16417,27959
567,17192,13234
568,23956,13107
569,24591,12467
570,27364,10250
571,10309,7627
572,30623,21416
573,15121,18656
574,3231,17961
575,9140,24600
576,21527,19695
577,32343,4700
578,13919,23942
579,32691,15245
580,30591,21049
581,19330,6281
582,8573,21617
583,17833,16092
584,18279,4304
585,18978,21574
586,27935,4369
587,30635,16881
588,8169,22430
589,31024,24431
590,2998,6382
591,31225,20853
592,6446,11941
593,12436,6233
594,24703,8297
595,11392,2637
596,31976,24197
597,21150,13657
598,3332,2624
599,13400,25882
600,27607,26619
601,11662,18492
602,28314,22285
603,30021,29113
604,13126,27487
605,3342,12750
606,19426,1222
607,15160,706
608,21133,36
609,31669,1366
610,12875,24125
611,5069,18699
612,12431,31833
613,13444,23574
614,908,15007
615,22476,24356
616,316,11308
617,17515,6350
618,13101,11510
619,25770,17626
620,23310,31076
621,23491,6967
622,13309,9129
623,1544,19127
624,31886,740
625,20435,25482
626,5531,30713
627,3889,28030
628,16846,9261
629,19533,9720
630,20975,28035
631,9374,8625
632,18926,27866
633,13520,20018
634,19120,8643
635,13683,3853
636,19610,2084
637,13926,29409
638,30430,27534
639,9474,15371
640,8072,5213
641,2310,14636
642,16455,10901
643,9285,10591
644,27644,27255
645,24849,10295
646,4356,1766
647,15206,23931
648,5190,21885
649,4310,27809
650,4347,8301
651,824,24587
652,18752,14871
653,23056,3075
654,4957,11885
655,3773,9592
656,20435,11785
657,28517,2166
658,13351,3850
659,1185,27239
660,30321,32690
661,9703,29707
662,9635,18112
663,26811,32569
664,15002,11662
665,22165,29779
666,11750,28651
667,5521,906
668,7770,19894
669,28633,13783
670,21586,6719
671,26066,4532
672,11719,25949
673,14089,20382
674,16148,6735
675,3548,23116
676,13445,12371
677,14824,5804
678,16549,18696
679,9526,12194
680,9028,22660
681,28708,24960
682,5450,13109
683,24597,323
684,9535,7544
685,3288,15251
686,20630,14587
687,30937,18297
688,26044,27603
689,10,2270
690,31461,7516
691,18831,24263
692,1425,25812
693,10284,6482
694,24288,1463
695,5487,21360
696,16539,21976
697,13213,11360
698,1790,26948
699,605,7407
700,17223,8150
701,12838,25713
702,27391,28826
703,17002,18813
704,12299,3810
705,22143,10860
706,19825,17706
707,27986,6544
708,21739,8621
709,26570,309
710,18734,20675
711,2557,2905
712,4343,16641
713,25365,18730
714,3802,27919
715,18433,22084
716,27082,26439
717,19675,29607
718,21095,787
719,26908,22138
720,20747,14339
721,12930,9857
722,26771,6833
723,23228,26720
724,6637,1877
725,23515,20365
726,5222,23079
727,32633,13334
728,7857,20856
729,11431,10264
730,1206,30095
731,10392,26875
732,5664,8772
733,13838,10350
734,421,3678
735,16264,26918
736,6420,27822
737,30760,27478
738,18976,744
739,8038,25837
740,22510,21060
741,21367,402
742,8544,11736
743,7803,26208
744,31,23220
745,10857,13665
746,3179,5563
747,18363,19660
748,20898,6980
749,13017,15870
750,16273,3188
751,17478,14394
752,25726,20057
753,30604,25377
754,15345,16071
755,29928,25840
756,7750,23283
757,2486,4219
758,7412,13265
759,15259,30445
760,22427,19501
761,25204,22845
762,5583,15499
763,29953,13679
764,12711,14528
765,29777,27535
766,7940,21325
767,8685,13934
768,7073,6782
769,27844,29723
770,21471,27463
771,16602,31264
772,2126,5593
773,9647,29058
774,32763,12295
775,22384,30326
776,21372,12636
777,18144,30691
778,23740,13560
779,11268,4764
780,22407,32047
781,6033,25690
782,32468,5344
783,12054,4730
784,13014,10901
785,32745,124
786,29633,351
787,5331,29239
788,15708,4975
789,14971,5597
790,3915,17012
791,7827,12723
792,11802,12728
793,13764,29069
794,26889,9722
795,5210,4106
796,28826,1801
797,9647,22421
798,2523,24357
799,28249,14103
800,10868,29717
801,17683,26236
802,18799,22024
803,11884,22252
804,28874,44
805,18225,27334
806,23486,26384
807,27035,12636
808,11725,4282
809,31033,8817
810,3726,4744
811,412,5491
812,13880,4295
813,5060,28792
814,14064,7189
815,3661,19195
816,3794,2167
817,15449,83
818,20161,6713
819,30524,28151
820,25071,9353
821,14058,22899
822,16939,30162
823,15649,14889
824,9454,11737
825,18262,25288
826,27940,22938
827,29537,18062
828,28090,28896
829,17128,22077
830,18667,4519
831,27019,12510
832,30795,5425
833,20948,9273
834,18832,23274
835,1245,14607
836,23587,25853
837,24766,18206
838,27499,8882
839,18877,19666
840,29140,1166
841,16949,1739
842,28833,13865
843,5442,787
844,26743,6605
845,50,11291
846,20133,25043
847,1707,1914
848,2868,25534
849,23964,9318
850,19378,276
851,6372,30190
852,14015,16896
853,1989,5373
854,19540,3216     ! #$$(!"(  & 0 20040808,007    	
 ! "$$ ("!,!"!0 0004802(8948        "!  (  ((!&  00241802(0105        ""$! ("), "#00 0044820,966       !    &'(	
($!,"0021$14000
854,   
	 !    $'  (	(&' 10104$0010	868       
   # $  (! #$!
000 01610(28056
           $$!( "( $$&0005$30100,169   886,1157,18874
887,16387,19236
888,16020,22748
889,26692,22275
890,763,32367
891,25467,15948
892,19840,26168
893,25577,29306
894,7862,10664
895,16196,26318
896,23559,23803
897,2991,18630
898,9347,21013
899,8026,17501
900,9836,11543
901,24313,20488
902,16765,9924
903,1766,15469
904,510,6295
905,23317,993
906,16521,11936
907,27067,5544
908,29337,23330
909,19646,6380
910,30680,26606
911,5595,25465
912,28709,16766
913,20889,8254
914,26215,22480
915,19697,24964
916,23572,12552
917,2490,6891
918,384,2501
919,3542,16169
920,9958,7999
921,31007,32130
922,12856,32549
923,3599,23303
924,9638,31032
925,6449,9102
926,752,3751
927,25419,28277
928,5996,31529
929,6728,13078
930,31958,13277
931,23784,10504
932,23259,30689
933,21798,28699
934,13668,20148
935,3436,15378
936,21411,4172
937,27769,30
938,3038,21238
939,1038,22047
940,32766,23282
941,7772,7578
942,5846,26570
943,13869,3704
944,15925,31606
945,17876,15236
946,16810,30513
947,6358,16546
948,16922,10777
949,3157,6352
950,6659,11657
951,27200,22494
952,14989,11063
953,19260,17389
954,19797,26892
955,11089,2729
956,9977,14022
957,7509,3654
958,22115,6480
959,18713,1596
960,23766,20444
961,8277,28609
962,23226,3782
963,30291,2571
964,15655,3113
965,26206,23106
966,14387,14909
967,13546,29511
968,6652,19319
969,6016,32039
970,17012,23646
971,3952,14429
972,19439,24367
973,17204,30923
974,12398,9520
975,678,21623
976,30633,8660
977,25058,518
978,14814,17300
979,5881,8953
980,860,32392
981,1626,30121
982,25806,25799
983,10753,11011
984,15432,26756
985,16221,23556
986,787,9189
987,26626,23283
988,11321,17058
989,10515,23650
990,24527,26978
991,30061,13282
992,7941,14038
993,11924,8329
994,7407,14993
995,18526,25893
996,22544,32174
997,31585,10301
998,16555,31008
999,12829,22475
1000,30666,5591
1001,7402,3260
1002,29451,4760
1003,6304,10787
1004,4397,27545
1005,21512,13377
1006,27486,8159
1007,24581,6966
1008,28482,28760
1009,5762,30083
1010,3523,29880
1011,1051,26183
1012,2715,8724
1013,16523,11061
1014,14499,7999
1015,25048,3672
1016,11993,744
1017,30272,14214
1018,16946,22384
1019,12513,6565
1020,20517,140
1021,13612,30625
1022,9716,24223
1023,11528,15657
1024,4280,19412
1025,5573,10028
1026,25140,20223
1027,28615,30743
1028,30294,8241
1029,15599,13259
1030,1420,23325
1031,31176,8032
1032,20429,31534
1033,32118,156
1034,27746,32100
1035,27743,22833
1036,19064,17394
1037,10646,6586
1038,11879,6041
1039,29452,2164
1040,14272,25282
1041,5237,22460
1042,15385,606
1043,23803,10316
1044,663,18558
1045,17318,29493
1046,11328,3249
1047,26246,6044
1048,21758,26145
1049,20645,2100
1050,14706,27500
1051,12976,21631
1052,28030,5187
1053,31327,2500
1054,28561,6341
1055,29866,10191
1056,9393,27289
1057,26282,9579
1058,2378,9165
1059,18767,16544
1060,8888,1906
1061,5063,5805
1062,32150,28660
1063,16553,16324
1064,17604,18234
1065,31205,27272
1066,15674,25729
1067,5997,13127
1068,12944,27640
1069,23711,13047
1070,8265,3100
1071,8313,498
1072,9187,13522
1073,19579,10865
1074,927,9286
1075,24155,18122
1076,22032,25989
1077,22635,18281
1078,5734,7388
1079,19659,7312
1080,12664,15856
1081,28494,16334
1082,22419,1946
1083,23085,22848
1084,7940,24968
1085,29038,31883
1086,17255,30613
1087,4370,14741
1088,3502,12003
1089,29185,32006
1090,12528,20670
1091,29110,31874
1092,10231,1840
1093,10410,10915
1094,24456,1892
1095,31618,28344
1096,14704,1467
1097,4855,7231
1098,13395,20400
1099,26249,26148
1100,17460,10092
1101,17891,18872
1102,10179,15656
1103,3341,11632
1104,18026,25159
1105,28500,12130
1106,25366,21039
1107,1273,24447
1108,12230,11499
1109,1948,24977
1110,3540,32642
1111,26982,13044
1112,1798,30228
1113,13049,4116
1114,19284,5050
1115,21529,13738
1116,16247,3099
1117,21271,31157
1118,26678,6862
1119,1793,30778
1120,15984,29812
1121,18571,20412
1122,14312,11664
1123,17852,10619
1124,4307,13101
1125,25688,2412
1126,8739,12590
1127,24337,8699
1128,20625,627
1129,2412,10115
1130,24685,1713
1131,26228,24457
1132,14885,31246
1133,32290,26556
1134,27543,10879
1135,7559,28845
1136,29206,9954
1137,28095,26702
1138,6462,17370
1139,3478,20524
1140,31352,4718
usbd_msc.zip

Any help would be appreciated as the same problem is occuring on an audit trail for a production unit.

Thanks,

Jeff

  • Edvin,

    Unfortunately having a problem in the file system seems to be more of a problem to my product then originally suggested!

    Is it possible this is the same issue as outlined in this post?
    https://devzone.nordicsemi.com/f/nordic-q-a/41792/fatfs-qspi-usbmsc-bug

    Would you expect Nordic to have a fix for this in a timely fashion? Or should I abandon the file system?

    Appreciate the help,
    Jeff

  • In that thread rc766437 suggested:
    "I think it is the same problem, just modify the QSPI block driver code<nrf_block_dev_qspi.c>, let the write/read buf pointer address aligned 4 bytes."

    I have come across a few examples of people running into this issue, but haven't been able to find the correct spot to 4 byte align the buffer.
    https://devzone.nordicsemi.com/f/nordic-q-a/38156/qspi-read-fail/

    Any insight on this?

  • Edvin,

    Nothing fruitful on my end from this, as the serial flash should be able to deal with single byte accesses. 

    Any update from Nordic? Surely I am not the only one running into issues with file system implementations.

    Thanks,
    Jeff

  • Hello Jeff,

    I got an answer from one of our SDK engineers. He wrote the following:

    ----------------------

    I've tested it using attached code, but I did not manage to get it working. In provided code f_printf is called from irq context, while internally it is waiting for interrupt from qspi and without proper irq priorities it hangs.

    I modified example to write to file from main loop, and it seems to be working on nRF52840 DK 1.0.0, I don't get any corrupted data tested up to 4500 records. Please check example with attached code (nonirq_main.c) if data still gets corrupted.

    ----------------------

    Attached is the noirq_main.c file that he used for testing. Can you please try it and see if it works better?

    /**
     * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
     *
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without modification,
     * are permitted provided that the following conditions are met:
     *
     * 1. Redistributions of source code must retain the above copyright notice, this
     *    list of conditions and the following disclaimer.
     *
     * 2. Redistributions in binary form, except as embedded into a Nordic
     *    Semiconductor ASA integrated circuit in a product or a software update for
     *    such product, must reproduce the above copyright notice, this list of
     *    conditions and the following disclaimer in the documentation and/or other
     *    materials provided with the distribution.
     *
     * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
     *    contributors may be used to endorse or promote products derived from this
     *    software without specific prior written permission.
     *
     * 4. This software, with or without modification, must only be used with a
     *    Nordic Semiconductor ASA integrated circuit.
     *
     * 5. Any software provided in binary form under this license must not be reverse
     *    engineered, decompiled, modified and/or disassembled.
     *
     * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
     * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
     * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     */
    #include <stdint.h>
    #include <stdbool.h>
    #include <stddef.h>
    #include <inttypes.h>
    #include <stdlib.h>
    
    #include "nrf.h"
    #include "nrf_block_dev.h"
    #include "nrf_block_dev_ram.h"
    #include "nrf_block_dev_empty.h"
    #include "nrf_block_dev_qspi.h"
    #include "nrf_block_dev_sdc.h"
    #include "nrf_drv_usbd.h"
    #include "nrf_drv_clock.h"
    #include "nrf_gpio.h"
    #include "nrf_atomic.h"
    #include "nrf_drv_power.h"
    
    #include "ff.h"
    #include "diskio_blkdev.h"
    
    #include "app_scheduler.h"
    #include "app_usbd.h"
    #include "app_usbd_core.h"
    #include "app_usbd_string_desc.h"
    #include "app_usbd_msc.h"
    #include "app_error.h"
    #include "app_timer.h"
    
    #include "bsp.h"
    
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    /**@file
     * @defgroup usbd_msc_example main.c
     * @{
     * @ingroup usbd_msc_example
     * @brief USBD MSC example
     *
     */
    APP_TIMER_DEF(msg_timing_id);
    
    #define LED_USB_RESUME   (BSP_BOARD_LED_0)
    #define LED_USB_START    (BSP_BOARD_LED_1)
    
    #define BTN_RANDOM_FILE  0
    #define BTN_LIST_DIR     1
    #define BTN_MKFS         2
    
    #define KEY_EV_RANDOM_FILE_MSK (1U << BTN_RANDOM_FILE)
    #define KEY_EV_LIST_DIR_MSK    (1U << BTN_LIST_DIR   )
    #define KEY_EV_MKFS_MSK        (1U << BTN_MKFS       )
    
    /**
     * @brief Enable power USB detection
     *
     * Configure if example supports USB port connection
     */
    #ifndef USBD_POWER_DETECTION
    #define USBD_POWER_DETECTION true
    #endif
    
    /**
     * @brief SD card enable/disable
     */
    #define USE_SD_CARD       0
    
    /**
     * @brief FatFS for QPSI enable/disable
     */
    #define USE_FATFS_QSPI    1
    
    /**
     * @brief Mass storage class user event handler
     */
    static void msc_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                                    app_usbd_msc_user_event_t     event);
    
    
    /**
     * @brief Ram block device size
     *
     * @note Windows fails to format volumes smaller than 190KB
     */
    #define RAM_BLOCK_DEVICE_SIZE (380 * 512)
    
    /**
     * @brief  RAM block device work buffer
     */
    static uint8_t m_block_dev_ram_buff[RAM_BLOCK_DEVICE_SIZE];
    
    /**
     * @brief  RAM block device definition
     */
    NRF_BLOCK_DEV_RAM_DEFINE(
        m_block_dev_ram,
        NRF_BLOCK_DEV_RAM_CONFIG(512, m_block_dev_ram_buff, sizeof(m_block_dev_ram_buff)),
        NFR_BLOCK_DEV_INFO_CONFIG("Nordic", "RAM", "1.00")
    );
    
    
    /**
     * @brief Empty block device definition
     */
    NRF_BLOCK_DEV_EMPTY_DEFINE(
        m_block_dev_empty,
        NRF_BLOCK_DEV_EMPTY_CONFIG(512, 1024 * 1024),
        NFR_BLOCK_DEV_INFO_CONFIG("Nordic", "EMPTY", "1.00")
    );
    
    
    /**
     * @brief  QSPI block device definition
     */
    NRF_BLOCK_DEV_QSPI_DEFINE(
        m_block_dev_qspi,
        NRF_BLOCK_DEV_QSPI_CONFIG(
            512,
            NRF_BLOCK_DEV_QSPI_FLAG_CACHE_WRITEBACK,
            NRF_DRV_QSPI_DEFAULT_CONFIG
         ),
         NFR_BLOCK_DEV_INFO_CONFIG("Nordic", "QSPI", "1.00")
    );
    
    #if USE_SD_CARD
    
    #define SDC_SCK_PIN     (27)        ///< SDC serial clock (SCK) pin.
    #define SDC_MOSI_PIN    (26)        ///< SDC serial data in (DI) pin.
    #define SDC_MISO_PIN    (2)         ///< SDC serial data out (DO) pin.
    #define SDC_CS_PIN      (32 + 15)   ///< SDC chip select (CS) pin.
    
    /**
     * @brief  SDC block device definition
     */
    NRF_BLOCK_DEV_SDC_DEFINE(
        m_block_dev_sdc,
        NRF_BLOCK_DEV_SDC_CONFIG(
            SDC_SECTOR_SIZE,
            APP_SDCARD_CONFIG(SDC_MOSI_PIN, SDC_MISO_PIN, SDC_SCK_PIN, SDC_CS_PIN)
         ),
         NFR_BLOCK_DEV_INFO_CONFIG("Nordic", "SDC", "1.00")
    );
    
    
    /**
     * @brief Block devices list passed to @ref APP_USBD_MSC_GLOBAL_DEF
     */
    #define BLOCKDEV_LIST() (                                   \
        NRF_BLOCKDEV_BASE_ADDR(m_block_dev_ram, block_dev),     \
        NRF_BLOCKDEV_BASE_ADDR(m_block_dev_empty, block_dev),   \
        NRF_BLOCKDEV_BASE_ADDR(m_block_dev_qspi, block_dev),    \
        NRF_BLOCKDEV_BASE_ADDR(m_block_dev_sdc, block_dev)      \
    )
    
    #else
    //#define BLOCKDEV_LIST() (                                   \
    //    NRF_BLOCKDEV_BASE_ADDR(m_block_dev_ram, block_dev),     \
    //    NRF_BLOCKDEV_BASE_ADDR(m_block_dev_empty, block_dev),   \
    //    NRF_BLOCKDEV_BASE_ADDR(m_block_dev_qspi, block_dev)     \
    //)
    #define BLOCKDEV_LIST() ( NRF_BLOCKDEV_BASE_ADDR(m_block_dev_qspi, block_dev) )
    #endif
    
    /**
     * @brief Endpoint list passed to @ref APP_USBD_MSC_GLOBAL_DEF
     */
    #define ENDPOINT_LIST() APP_USBD_MSC_ENDPOINT_LIST(1, 1)
    
    /**
     * @brief Mass storage class work buffer size
     */
    #define MSC_WORKBUFFER_SIZE (1024)
    
    /*lint -save -e26 -e64 -e123 -e505 -e651*/
    /**
     * @brief Mass storage class instance
     */
    APP_USBD_MSC_GLOBAL_DEF(m_app_msc,
                            0,
                            msc_user_ev_handler,
                            ENDPOINT_LIST(),
                            BLOCKDEV_LIST(),
                            MSC_WORKBUFFER_SIZE);
    
    /*lint -restore*/
    
    /**
     * @brief Events from keys
     */
    static nrf_atomic_u32_t m_key_events;
    
    /**
     * @brief  USB connection status
     */
    static bool m_usb_connected = false;
    
    
    #if USE_FATFS_QSPI
    
    static FATFS m_filesystem;
    
    static bool fatfs_init(void)
    {
        FRESULT ff_result;
        DSTATUS disk_state = STA_NOINIT;
    
        memset(&m_filesystem, 0, sizeof(FATFS));
    
        // Initialize FATFS disk I/O interface by providing the block device.
        static diskio_blkdev_t drives[] =
        {
            DISKIO_BLOCKDEV_CONFIG(NRF_BLOCKDEV_BASE_ADDR(m_block_dev_qspi, block_dev), NULL)
        };
    
        diskio_blockdev_register(drives, ARRAY_SIZE(drives));
    
        NRF_LOG_INFO("Initializing disk 0 (QSPI)...");
        disk_state = disk_initialize(0);
        if (disk_state)
        {
            NRF_LOG_ERROR("Disk initialization failed.");
            return false;
        }
    
        NRF_LOG_INFO("Mounting volume...");
        ff_result = f_mount(&m_filesystem, "", 1);
        if (ff_result != FR_OK)
        {
            if (ff_result == FR_NO_FILESYSTEM)
            {
                NRF_LOG_ERROR("Mount failed. Filesystem not found. Please format device.");
            }
            else
            {
                NRF_LOG_ERROR("Mount failed: %u", ff_result);
            }
            return false;
        }
    
        return true;
    }
    
    static void fatfs_mkfs(void)
    {
        FRESULT ff_result;
    
        if (m_usb_connected)
        {
            NRF_LOG_ERROR("Unable to operate on filesystem while USB is connected");
            return;
        }
    
        NRF_LOG_INFO("\r\nCreating filesystem...");
        static uint8_t buf[512];
        ff_result = f_mkfs("", FM_EXFAT, 1024, buf, sizeof(buf));
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("Mkfs failed.");
            return;
        }
    
        NRF_LOG_INFO("Mounting volume...");
        ff_result = f_mount(&m_filesystem, "", 1);
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("Mount failed.");
            return;
        }
    
        NRF_LOG_INFO("Done");
    }
    
    static void fatfs_ls(void)
    {
        DIR dir;
        FRESULT ff_result;
        FILINFO fno;
    
        if (m_usb_connected)
        {
            NRF_LOG_ERROR("Unable to operate on filesystem while USB is connected");
            return;
        }
    
        NRF_LOG_INFO("\r\nListing directory: /");
        ff_result = f_opendir(&dir, "/");
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("Directory listing failed: %u", ff_result);
            return;
        }
    
        uint32_t entries_count = 0;
        do
        {
            ff_result = f_readdir(&dir, &fno);
            if (ff_result != FR_OK)
            {
                NRF_LOG_ERROR("Directory read failed: %u", ff_result);
                return;
            }
    
            if (fno.fname[0])
            {
                if (fno.fattrib & AM_DIR)
                {
                    NRF_LOG_RAW_INFO("   <DIR>   %s\r\n",(uint32_t)fno.fname);
                }
                else
                {
                    NRF_LOG_RAW_INFO("%9lu  %s\r\n", fno.fsize, (uint32_t)fno.fname);
                }
            }
    
            ++entries_count;
            NRF_LOG_FLUSH();
        } while (fno.fname[0]);
    
    
        NRF_LOG_RAW_INFO("Entries count: %u\r\n", entries_count);
    }
    
    static void fatfs_create_test_file(void)
    {
        FRESULT ff_result;
        FIL file;
        uint32_t file_size = 0;
    
        ff_result = f_open(&file, "log_data.txt", FA_READ);
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("log_data.txt Does not exist\r\n");
            NRF_LOG_INFO("Creating log_data.txt");
            ff_result = f_open(&file, "log_data.txt", FA_CREATE_ALWAYS | FA_WRITE);
            if (ff_result != FR_OK)
            {
                if(!m_usb_connected)
                    NRF_LOG_ERROR("Unable to open or create file: log_data.txt");
                NRF_LOG_FLUSH();
                return;
            }
        }
    
        ff_result = f_close(&file);
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("Unable to close file: %u \r\n", ff_result);
            NRF_LOG_FLUSH();
            return;
        }
    
        file_size = f_size(&file);
        NRF_LOG_INFO("log_data.txt create SUCCESSFUL; file size = %d\r\n", file_size);
    }
    
    static uint32_t record_number = 0;
    
    static volatile bool write_file = false;
    static void log_data_write_handler(void * p_context)
    {
        write_file = true;
    }
    
    static void write()
    {
        int i = 0;
        char log_record[128] = {0};
        char log_record_r[128] = {0};
    
        FRESULT ff_result;
        FIL file;
        uint32_t fileSize;
    
        ff_result = f_open(&file, "log_data.txt", FA_OPEN_APPEND | FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
        if (ff_result != FR_OK)
        {
            if(!m_usb_connected)
                NRF_LOG_INFO("Unable to open or create log_data.txt: %u", ff_result);
            NRF_LOG_FLUSH();
            return;
        }
    
        ++record_number;
        sprintf(log_record, "%d,%d,%d\r\n", record_number,rand(),rand());
    //    sprintf(log_record, "1234567890123456789012345678901234567890%d\r\n", record_number+10000000);
        //if (record_number>276 && record_number<290)
    
        FSIZE_t ptr = f_tell(&file);
    //    NRF_LOG_INFO("Pointer: %lu", ptr);
        i = f_printf(&file, log_record);
    //    if (i != 50)
    //    {
    //      NRF_LOG_INFO("f_printf != EOF, %d, %d, %d", i, record_number, sizeof(log_record));
    //    }
    //    NRF_LOG_INFO("AW: Pointer: %lu",f_tell(&file));
    //    f_lseek(&file, ptr);
    //    NRF_LOG_INFO("LS: Pointer: %lu",f_tell(&file));
    
    //    if (record_number>2)
    //    {
    //        f_gets(log_record_r, 50, &file);
    //        if (strcmp(log_record, log_record_r) != 0) {
    //
    //            NRF_LOG_INFO("Records are corrupted");
    //        }
    //
    //    }
    
        i = f_close(&file);
        if (i != 0)
        {
          NRF_LOG_INFO("f_close != 0, %d, %d", i, record_number);
        }
    
        NRF_LOG_INFO("Wrote Data Record: %d", record_number);
    }
    
    static void fatfs_file_create(void)
    {
        FRESULT ff_result;
        FIL file;
        char filename[16];
    
        if (m_usb_connected)
        {
            NRF_LOG_ERROR("Unable to operate on filesystem while USB is connected");
            return;
        }
    
        (void)snprintf(filename, sizeof(filename), "%08x.txt", rand());
    
        NRF_LOG_RAW_INFO("Creating random file: %s ...", (uint32_t)filename);
        NRF_LOG_FLUSH();
    
        ff_result = f_open(&file, filename, FA_CREATE_ALWAYS | FA_WRITE);
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("\r\nUnable to open or create file: %u", ff_result);
            NRF_LOG_FLUSH();
            return;
        }
    
        ff_result = f_close(&file);
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("\r\nUnable to close file: %u", ff_result);
            NRF_LOG_FLUSH();
            return;
        }
        NRF_LOG_RAW_INFO("done\r\n");
    }
    
    static void fatfs_uninit(void)
    {
        NRF_LOG_INFO("Un-initializing disk 0 (QSPI)...");
        UNUSED_RETURN_VALUE(disk_uninitialize(0));
    }
    #else //USE_FATFS_QSPI
    #define fatfs_init()        false
    #define fatfs_mkfs()        do { } while (0)
    #define fatfs_ls()          do { } while (0)
    #define fatfs_file_create() do { } while (0)
    #define fatfs_uninit()      do { } while (0)
    #endif
    
    /**
     * @brief Class specific event handler.
     *
     * @param p_inst    Class instance.
     * @param event     Class specific event.
     */
    static void msc_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                                    app_usbd_msc_user_event_t     event)
    {
        UNUSED_PARAMETER(p_inst);
        UNUSED_PARAMETER(event);
    }
    
    /**
     * @brief USBD library specific event handler.
     *
     * @param event     USBD library event.
     */
    static void usbd_user_ev_handler(app_usbd_event_type_t event)
    {
        switch (event)
        {
            case APP_USBD_EVT_DRV_SUSPEND:
                bsp_board_led_off(LED_USB_RESUME);
                break;
            case APP_USBD_EVT_DRV_RESUME:
                bsp_board_led_on(LED_USB_RESUME);
                break;
            case APP_USBD_EVT_STARTED:
                bsp_board_led_on(LED_USB_START);
                break;
            case APP_USBD_EVT_STOPPED:
                UNUSED_RETURN_VALUE(fatfs_init());
                app_usbd_disable();
                bsp_board_leds_off();
                break;
            case APP_USBD_EVT_POWER_DETECTED:
                NRF_LOG_INFO("USB power detected");
    
                if (!nrf_drv_usbd_is_enabled())
                {
                    fatfs_uninit();
                    app_usbd_enable();
                }
                break;
            case APP_USBD_EVT_POWER_REMOVED:
                NRF_LOG_INFO("USB power removed");
                app_usbd_stop();
                m_usb_connected = false;
                break;
            case APP_USBD_EVT_POWER_READY:
                NRF_LOG_INFO("USB ready");
                app_usbd_start();
                m_usb_connected = true;
                break;
            default:
                break;
        }
    }
    
    static void bsp_event_callback(bsp_event_t ev)
    {
        switch (ev)
        {
            /* Just set a flag to be processed in the main loop */
            case CONCAT_2(BSP_EVENT_KEY_, BTN_RANDOM_FILE):
                UNUSED_RETURN_VALUE(nrf_atomic_u32_or(&m_key_events, KEY_EV_RANDOM_FILE_MSK));
                break;
    
            case CONCAT_2(BSP_EVENT_KEY_, BTN_LIST_DIR):
                UNUSED_RETURN_VALUE(nrf_atomic_u32_or(&m_key_events, KEY_EV_LIST_DIR_MSK));
                break;
    
            case CONCAT_2(BSP_EVENT_KEY_, BTN_MKFS):
                UNUSED_RETURN_VALUE(nrf_atomic_u32_or(&m_key_events, KEY_EV_MKFS_MSK));
                break;
    
            default:
                return; // no implementation needed
        }
    }
    
    int main(void)
    {
        ret_code_t ret;
    
        APP_SCHED_INIT(32, 20);
    
        static const app_usbd_config_t usbd_config = {
            .ev_state_proc = usbd_user_ev_handler
        };
    
        ret = NRF_LOG_INIT(app_usbd_sof_timestamp_get);
        APP_ERROR_CHECK(ret);
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        ret = nrf_drv_clock_init();
        APP_ERROR_CHECK(ret);
    
        /* Fill whole RAM block device buffer */
        for (size_t i = 0; i < sizeof(m_block_dev_ram_buff); ++i)
        {
            m_block_dev_ram_buff[i] = i;
        }
    
        /* Configure LEDs and buttons */
        nrf_drv_clock_lfclk_request(NULL);
        ret = app_timer_init();
    
        ret = app_timer_create(&msg_timing_id,
                                APP_TIMER_MODE_REPEATED,
                                log_data_write_handler);
        APP_ERROR_CHECK(ret);
    
    
    
        APP_ERROR_CHECK(ret);
        ret = bsp_init(BSP_INIT_BUTTONS, bsp_event_callback);
        APP_ERROR_CHECK(ret);
        bsp_board_init(BSP_INIT_LEDS);
    
        if (fatfs_init())
        {
            fatfs_ls();
    //        fatfs_file_create();
            fatfs_create_test_file();
        }
    
        ret = app_usbd_init(&usbd_config);
        APP_ERROR_CHECK(ret);
    
        app_usbd_class_inst_t const * class_inst_msc = app_usbd_msc_class_inst_get(&m_app_msc);
        ret = app_usbd_class_append(class_inst_msc);
        APP_ERROR_CHECK(ret);
    
        NRF_LOG_INFO("USBD MSC example started.");
    
        if (USBD_POWER_DETECTION)
        {
            ret = app_usbd_power_events_enable();
            APP_ERROR_CHECK(ret);
        }
        else
        {
            NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now");
    
            app_usbd_enable();
            app_usbd_start();
            m_usb_connected = true;
        }
    
        ret = app_timer_start(msg_timing_id, APP_TIMER_TICKS(500), NULL);
        APP_ERROR_CHECK(ret);
    
        while (true)
        {
            if(!app_usbd_event_queue_process())
            {
                app_sched_execute();
            }
            if (write_file) {
                write_file = false;
                write();
            }
    
            /* Process BSP key events flags.*/
            uint32_t events = nrf_atomic_u32_fetch_store(&m_key_events, 0);
    //        if (events & KEY_EV_RANDOM_FILE_MSK)
    //        {
    //            fatfs_file_create();
    //        }
    //
    //        if (events & KEY_EV_LIST_DIR_MSK)
    //        {
    //            fatfs_ls();
    //        }
    //
            if (events & KEY_EV_MKFS_MSK)
            {
                fatfs_mkfs();
                record_number = 0;
            }
    //
            NRF_LOG_FLUSH();
            /* Sleep CPU only if there was no interrupt since last loop processing */
            __WFE();
        }
    }
    
    /** @} */

    Best regards,

    Edvin

  • Edvin,

    I'm still get corruption running the SDK engineers code. Near the 1905th byte this time (~132nd record). Ran the test twice from my end with the same corrupted results. 

    Regards,
    Jeff

Related