-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtest.c
60 lines (44 loc) · 1.29 KB
/
test.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
#include <stdio.h>
#include <stdlib.h>
#include "pico/stdio.h"
#include "pico/stdlib.h"
#include <lfs_rp2040.h>
// variables used by the filesystem
lfs_t lfs;
lfs_file_t file;
struct lfs_config cfg;
// entry point
int main(void) {
stdio_init_all();
while (getchar_timeout_us(0) != 'X') {
sleep_ms(10);
}
printf("Hello from Pi Pico!\n");
lfs_rp2040_init(&cfg);
// mount the filesystem
int err = lfs_mount(&lfs, &cfg);
printf("%d\n\n", err);
// reformat if we can't mount the filesystem
// this should only happen on the first boot
if (err) {
lfs_format(&lfs, &cfg);
lfs_mount(&lfs, &cfg);
}
// read current count
uint32_t boot_count = 0;
lfs_file_open(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));
// update boot count
boot_count += 1;
lfs_file_rewind(&lfs, &file);
lfs_file_write(&lfs, &file, &boot_count, sizeof(boot_count));
// remember the storage is not updated until the file is closed successfully
lfs_file_close(&lfs, &file);
// release any resources we were using
lfs_unmount(&lfs);
// print the boot count
printf("boot_count: %d\n", boot_count);
while(true) {
sleep_ms(1000);
}
}