-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
962 lines (704 loc) · 19.1 KB
/
main.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
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
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "main.h"
#include "driver.h"
#include "vfs.h"
#include "filesystem.h"
#include "sfs.h"
#include "rfs.h"
#include "w25.h"
#include "uartterm.h"
#include "calls.h"
#define PRESCALER 72
#define TIMPERIOD 0xffff
#define TICKSPERSEC (72000000 / PRESCALER)
#define ITDUR 10
#define OUTPUTPINSA (GPIO_PIN_4)
#define OUTPUTPINSB (GPIO_PIN_3)
SPI_HandleTypeDef hspi1;
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim2;
UART_HandleTypeDef huart1;
struct driver drivers[8];
struct bdevice dev[8];
struct filesystem fs[8];
struct bdevice *curdev;
// for heap test
void *_sbrk(ptrdiff_t incr);
void systemclock_config(void);
static void gpio_init(void);
static void spi1_init(void);
static void tim1_init(void);
static void tim2_init(void);
static void usart1_init(void);
static void flash_init(void);
static void rfs_init(void);
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
}
int printhelp()
{
ut_write("\r\ndriver commands:\n\r");
ut_write("\t%-23s%-32s\n\r",
"sd [dev]", "set current device to [dev]");
ut_write("\t%-23s%-32s\n\r",
"rd [addr]", "read data at address [addr]");
ut_write("\t%-23s%-32s\n\r",
"wd [addr] [str]","write string [str] into [addr]");
ut_write("\r\nfilesystem commands:\n\r");
ut_write("\t%-23s%-32s\n\r",
"f", "format choosen device");
ut_write("\t%-23s%-32s\n\r",
"i [struct] {[addr]}",
"dump filesystem [struct] (sb, in, bm) at [addr]");
ut_write("\t%-23s%-32s\n\r",
"c [sz]","create inode for data size of [size]");
ut_write("\t%-23s%-32s\n\r",
"d [addr]","delete inode with address [addr]");
ut_write("\t%-23s%-32s\n\r",
"s [addr] [data]",
"set data for inode with address [addr] to [data]");
ut_write("\t%-23s%-32s\n\r",
"g [addr]",
"get data from inode with address [addr]");
ut_write("\t%-23s%-32s\n\r",
"r [addr] [off] [sz]",
"read [sz] bytes from inode with address `[addr]` with offset of [off] bytes");
ut_write("\t%-23s%-32s\n\r",
"w [addr] [off] [data]",
"write data into inode with address `[addr]` with offset of [off] bytes");
ut_write("\r\nvirtual filesystem commands:\n\r");
ut_write("\t%-23s%-32s\n\r",
"mount [dev] [target]",
"mount [dev] to [target]");
ut_write("\t%-23s%-32s\n\r",
"format [target]",
"format device mounted at [target]");
ut_write("\t%-23s%-32s\n\r",
"umount [dev] [target]",
"unmount [target]");
ut_write("\t%-23s%-32s\n\r",
"mountlist",
"get list of mounted devices");
ut_write("\t%-23s%-32s\n\r",
"open [path] [flags]",
"open file with [path], if [flags] is 'c', create it");
ut_write("\t%-23s%-32s\n\r",
"read [fd] [sz]",
"read [sz] bytes from opened file with descriptor [fd]");
ut_write("\t%-23s%-32s\n\r",
"write [fd] [data]",
"write [data] into opened file with descriptor [fd]");
ut_write("\t%-23s%-32s\n\r",
"close [fd]",
"close opened file with descriptor [fd]");
ut_write("\t%-23s%-32s\n\r",
"mkdir [path]",
"create directory [path]");
ut_write("\t%-23s%-32s\n\r",
"rm [path]",
"delete file or directory [path]");
ut_write("\t%-23s%-32s\n\r",
"ls [path]",
"get list of file in directory [path]");
ut_write("\t%-23s%-32s\n\r",
"cd [path]",
"change current working directory to [path]");
ut_write("\n\r");
return 0;
}
int setdevice(const char **toks)
{
if (strcmp(toks[1], dev[0].name) == 0)
curdev = dev + 0;
else if (strcmp(toks[1], dev[1].name) == 0)
curdev = dev + 1;
else {
ut_write("unknown device %s\n\r", toks[1]);
return 0;
}
ut_write("device %s was set\n\r", toks[1]);
return 0;
}
int devformat(const char **toks)
{
size_t r;
if (fs_iserror(r = fs[0].format(curdev))) {
ut_write("error: %s\n\r",
vfs_strerror(fs_uint2interr(r)));
return 0;
}
return 0;
}
int readdata(const char **toks)
{
char rdata[256];
size_t addr, r;
sscanf(toks[1], "%x", &addr);
memset(rdata, 0, 256);
r = curdev->read(curdev->priv, addr, rdata, 256);
if (fs_iserror(r)) {
ut_write("error: %s\n\r",
vfs_strerror(fs_uint2interr(r)));
return 0;
}
ut_dumppage(rdata, 256);
return 0;
}
int writedata(const char **toks)
{
size_t addr, r;
sscanf(toks[1], "%x", &addr);
if (fs_iserror(r = curdev->erasesector(curdev->priv, addr))) {
ut_write("error: %s\n\r",
vfs_strerror(fs_uint2interr(r)));
return 0;
}
r = curdev->write(curdev->priv, addr, toks[2],
strlen(toks[2]) + 1);
if (fs_iserror(r)) {
ut_write("error: %s\n\r",
vfs_strerror(fs_uint2interr(r)));
return 0;
}
return 0;
}
int mntdevformat(const char **toks)
{
int r;
if ((r = format(toks[1])) < 0) {
ut_write("error: %s\n\r", vfs_strerror(r));
return 0;
}
return 0;
}
int createinode(const char **toks)
{
size_t addr, r;
sscanf(toks[1], "%d", &addr);
if (fs_iserror(r = fs[0].inodecreate(curdev, addr, FS_FILE))) {
ut_write("error: %s\n\r",
vfs_strerror(fs_uint2interr(r)));
return 0;
}
ut_write("new inode address: %x\n\r", r);
return 0;
}
int deleteinode(const char **toks)
{
size_t addr, r;
sscanf(toks[1], "%x", &addr);
if (fs_iserror(r = fs[0].inodedelete(curdev, addr))) {
ut_write("error: %s\n\r",
vfs_strerror(fs_uint2interr(r)));
return 0;
}
return 0;
}
int setinode(const char **toks)
{
size_t addr, r;
sscanf(toks[1], "%x", &addr);
r = fs[0].inodeset(curdev, addr, toks[2], strlen(toks[2]) + 1);
if (fs_iserror(r)) {
ut_write("error: %s\n\r",
vfs_strerror(fs_uint2interr(r)));
return 0;
}
return 0;
}
int getinode(const char **toks)
{
size_t addr;
uint8_t buf[256];
size_t r;
sscanf(toks[1], "%x", &addr);
if (fs_iserror(r = fs[0].inodeget(curdev, addr, buf, 256))) {
ut_write("error: %s\n\r",
vfs_strerror(fs_uint2interr(r)));
return 0;
}
buf[r] = '\0';
ut_write("%s\n\r", buf);
return 0;
}
int readinode(const char **toks)
{
size_t addr, offset, size;
uint8_t buf[256];
size_t r;
sscanf(toks[1], "%x", &addr);
sscanf(toks[2], "%d", &offset);
sscanf(toks[3], "%d", &size);
memset(buf, 0, 256);
r = fs[0].inoderead(curdev, addr, offset, buf, size);
if (fs_iserror(r)) {
ut_write("error: %s\n\r",
vfs_strerror(fs_uint2interr(r)));
return 0;
}
ut_write("%s\n\r", buf);
return 0;
}
int writeinode(const char **toks)
{
size_t addr, offset, r;
sscanf(toks[1], "%x", &addr);
sscanf(toks[2], "%d", &offset);
r = fs[0].inodewrite(curdev, addr, offset,
toks[3], strlen(toks[3]));
if (fs_iserror(r)) {
ut_write("error: %s\n\r",
vfs_strerror(fs_uint2interr(r)));
}
return 0;
}
int createbiginode(const char **toks)
{
size_t addr, insz, i;
char buf[5000];
insz = 5000;
ut_write("new inode address: %x\n\r",
(addr = fs[0].inodecreate(curdev, 32, FS_FILE)));
for (i = 0; i < insz; ++i)
buf[i] = (i % ('z' - 'a')) + 'a';
ut_write("set data: %d\n\r",
fs[0].inodeset(curdev, addr, buf, insz));
return 0;
}
int dumpsb()
{
struct sfs_superblock sb;
int i;
fs[0].dumpsuperblock(curdev, &sb);
ut_write("checksum: %lx\r\n", sb.checksum);
ut_write("inode count: %lx\r\n", sb.inodecnt);
ut_write("inode size: %lu\r\n", sb.inodesz);
ut_write("inodes start: %lx\r\n", sb.inodestart);
ut_write("free inode: %lx\r\n", sb.freeinodes);
ut_write("blocks start: %lx\r\n", sb.blockstart);
ut_write("free block: %lx\r\n", sb.freeblocks);
ut_write("inodes checksums: ");
for (i = 0; i < SFS_INODESECTORSCOUNT + 1; ++i) {
ut_write("%lx%s", sb.inodechecksum[i],
((i != SFS_INODESECTORSCOUNT) ? ", " : ""));
}
ut_write("\r\n");
return 0;
}
int dumpin(const char *arg)
{
size_t addr;
struct sfs_inode in;
sscanf(arg, "%x", &addr);
fs[0].dumpinode(curdev, addr, &in);
ut_write("checksum: %lx\r\n", in.checksum);
ut_write("next free: %lx\r\n", in.nextfree);
ut_write("size: %lu\r\n", in.size);
ut_write("allocsize: %lu\r\n", in.allocsize);
ut_write("type: %lx\r\n", in.type);
ut_write("block[0]: %x\r\n", in.blocks.block[0]);
ut_write("block[1]: %x\r\n", in.blocks.block[1]);
ut_write("indirect block: %x\r\n", in.blocks.blockindirect);
return 0;
}
int dumpb(const char *arg)
{
size_t addr;
struct sfs_blockmeta meta;
sscanf(arg, "%x", &addr);
fs[0].dumpblockmeta(curdev, addr, &meta);
ut_write("checksum: %lx\r\n", meta.checksum);
ut_write("next: %lx\r\n", meta.next);
ut_write("datasize: %lu\r\n", meta.datasize);
return 0;
}
int dump(const char **toks)
{
if (strcmp(toks[1], "sb") == 0)
return dumpsb();
if (strcmp(toks[1], "in") == 0)
return dumpin(toks[2]);
if (strcmp(toks[1], "bm") == 0)
return dumpb(toks[2]);
else {
ut_write("Unknown structure\n\r");
return 0;
}
return 0;
}
int mounthandler(const char **toks)
{
struct bdevice *d;
int r;
if (strcmp(toks[1], dev[0].name) == 0)
d = dev + 0;
else if (strcmp(toks[1], dev[1].name) == 0)
d = dev + 1;
else {
ut_write("unknown device %s\n\r", toks[1]);
return 0;
}
if ((r = mount(d, toks[2], fs + 0)) < 0) {
ut_write("mount: %s\n\r", vfs_strerror(r));
return 0;
}
return 0;
}
int umounthandler(const char **toks)
{
int r;
if ((r = umount(toks[1])) < 0) {
ut_write("error: %s\n\r", vfs_strerror(r));
return 0;
}
return 0;
}
int mountlisthandler(const char **toks)
{
char buf[256];
const char *list[MOUNTMAX];
const char **p;
int r;
if ((r = mountlist(list, buf, 2048)) < 0) {
ut_write("error: %s\n\r", vfs_strerror(r));
return 0;
}
for (p = list; *p != NULL; ++p)
ut_write("%s\n\r", *p);
return 0;
}
int openfile(const char **toks)
{
int fd;
fd = open(toks[1], (strcmp(toks[2], "c") == 0) ? O_CREAT : 0);
if (fd < 0) {
ut_write("error: %s\n\r", vfs_strerror(fd));
return 0;
}
ut_write("fd: %d\n\r", fd);
return 0;
}
int readfile(const char **toks)
{
uint8_t buf[256];
size_t sz;
int fd, r;
sscanf(toks[1], "%d", &fd);
sscanf(toks[2], "%d", &sz);
if ((r = read(fd, buf, sz)) < 0) {
ut_write("error: %s\n\r", vfs_strerror(r));
return 0;
}
buf[r] = '\0';
ut_write("%s\n\r", (char *) buf);
return 0;
}
int writefile(const char **toks)
{
int fd, r;
sscanf(toks[1], "%d", &fd);
if ((r = write(fd, toks[2], strlen(toks[2]))) < 0) {
ut_write("error: %s\n\r", vfs_strerror(r));
return 0;
}
return 0;
}
int closefile(const char **toks)
{
int fd, r;
sscanf(toks[1], "%d", &fd);
if ((r = close(fd)) < 0) {
ut_write("error %d: %s\n\r", fd, vfs_strerror(r));
return 0;
}
return 0;
}
int makedir(const char **toks)
{
int r;
if ((r = mkdir(toks[1])) < 0) {
ut_write("error: %s\n\r", vfs_strerror(r));
return 0;
}
return 0;
}
int makedev(const char **toks)
{
int r;
size_t driverid, deviceid;
sscanf(toks[2], "%d", &driverid);
sscanf(toks[3], "%d", &deviceid);
if ((r = mkdev(toks[1], driverid, deviceid)) < 0) {
ut_write("error: %s\n\r", vfs_strerror(r));
return 0;
}
return 0;
}
int unlinkfile(const char **toks)
{
int r;
if ((r = unlink(toks[1])) < 0) {
ut_write("unlink: %s\n\r", vfs_strerror(r));
return 0;
}
return 0;
}
int listvfsdir(const char **toks)
{
char buf[256];
char *list[16];
char **p;
int r;
if ((r = lsdir(toks[1], (const char **) list, buf, 256)) < 0) {
ut_write("error: %s\n\r", vfs_strerror(r));
return 0;
}
for (p = list; *p != NULL; ++p)
ut_write("%s\n\r", *p);
return 0;
}
int vfscd(const char **toks)
{
int r;
if ((r = cd(toks[1])) < 0) {
ut_write("error: %s\n\r", vfs_strerror(r));
return 0;
}
return 0;
}
int heaptest(const char **toks)
{
char *p[256];
int i;
ut_write("initial break: %p\n\r", _sbrk(0));
for (i = 0; i < 128; ++i)
p[i] = malloc(32);
for (i = 0; i < 4; ++i)
p[128 + i] = malloc(1024);
for (i = 0; i < 132; ++i) {
ut_write("%p ", p[i] - 12);
if (((i + 1) % 8) == 0)
ut_write("\n\r");
}
ut_write("\n\r");
ut_write("current break: %p\n\r", _sbrk(0));
for (i = 0; i < 132; ++i)
free(p[i]);
ut_write("current break (after free): %p\n\r", _sbrk(0));
for (i = 0; i < 128; ++i)
p[i] = malloc(32);
for (i = 0; i < 4; ++i)
p[128 + i] = malloc(1024);
for (i = 0; i < 132; ++i) {
ut_write("%p ", p[i] - 12);
if (((i + 1) % 8) == 0)
ut_write("\n\r");
}
ut_write("\n\r");
ut_write("current break: %p\n\r", _sbrk(0));
for (i = 0; i < 132; ++i)
free(p[i]);
ut_write("current break (after free): %p\n\r", _sbrk(0));
return 0;
}
int main(void)
{
HAL_Init();
systemclock_config();
gpio_init();
tim1_init();
tim2_init();
usart1_init();
spi1_init();
flash_init();
rfs_init();
HAL_TIM_Base_Start_IT(&htim1);
HAL_TIM_Base_Start_IT(&htim2);
__HAL_TIM_SET_COUNTER(&htim2, 0);
vfsinit();
ut_init(&huart1);
ut_addcommand("sd", setdevice);
ut_addcommand("rd", readdata);
ut_addcommand("wd", writedata);
ut_addcommand("f", devformat);
ut_addcommand("i", dump);
ut_addcommand("c", createinode);
ut_addcommand("d", deleteinode);
ut_addcommand("s", setinode);
ut_addcommand("g", getinode);
ut_addcommand("r", readinode);
ut_addcommand("w", writeinode);
ut_addcommand("b", createbiginode);
ut_addcommand("format", mntdevformat);
ut_addcommand("mount", mounthandler);
ut_addcommand("umount", umounthandler);
ut_addcommand("mountlist", mountlisthandler);
ut_addcommand("open", openfile);
ut_addcommand("read", readfile);
ut_addcommand("write", writefile);
ut_addcommand("close", closefile);
ut_addcommand("mkdir", makedir);
ut_addcommand("mkdev", makedev);
ut_addcommand("rm", unlinkfile);
ut_addcommand("ls", listvfsdir);
ut_addcommand("cd", vfscd);
ut_addcommand("heaptest", heaptest);
printhelp();
mount(dev + 0, "/", fs + 0);
mount(dev + 1, "/dev", fs + 0);
mount(NULL, "/tmp", fs + 1);
format("/tmp");
ut_promptcommand();
while (1) {
int c;
__HAL_TIM_SET_COUNTER(&htim2, 0);
if (ut_getcommand() == 0) {
ut_executecommand();
ut_promptcommand();
}
while ((c = __HAL_TIM_GET_COUNTER(&htim2)) < ITDUR);
}
}
void systemclock_config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
error_handler();
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK
| RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1
| RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct,
FLASH_LATENCY_0) != HAL_OK)
error_handler();
}
static void gpio_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOA, OUTPUTPINSA, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, OUTPUTPINSB, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = OUTPUTPINSA;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = OUTPUTPINSB;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
static void spi1_init(void)
{
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
error_handler();
}
static void tim1_init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
htim1.Instance = TIM1;
htim1.Init.Prescaler = 72 - 1;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 0xffff - 1;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
error_handler();
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
error_handler();
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
error_handler();
}
static void tim2_init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = PRESCALER - 1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = TIMPERIOD - 1;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
error_handler();
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
error_handler();
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
error_handler();
}
static void usart1_init()
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 921600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
error_handler();
}
static void flash_init()
{
struct w25_device d;
w25_getdriver(drivers + 0);
d.hspi = &hspi1;
d.gpio = GPIOA;
d.pin = GPIO_PIN_4;
drivers[0].initdevice(&d, dev + 0);
d.hspi = &hspi1;
d.gpio = GPIOB;
d.pin = GPIO_PIN_3;
drivers[0].initdevice(&d, dev + 1);
curdev = dev;
sfs_getfs(fs + 0);
}
static void rfs_init()
{
rfs_getfs(fs + 1);
}
void error_handler(void)
{
__disable_irq();
while (1) {}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif