-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.cpp
71 lines (62 loc) · 2 KB
/
main.cpp
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
#include <stdio.h>
#include "sd_card.h"
#include "hw_config.h"
#include "stdio_cli.h"
#include "ff_utils.h"
//
#include "FreeRTOS.h"
#include "task.h"
#include "FreeRTOS_time.h"
//
#include "pico/stdlib.h"
//
#include "crash.h"
#ifndef USE_PRINTF
#error This program is useless without standard input and output.
#endif
// If the card is physically removed, unmount the filesystem:
static void card_detect_callback(uint gpio, uint32_t events) {
static bool busy;
if (busy) return; // Avoid switch bounce
busy = true;
for (size_t i = 0; i < sd_get_num(); ++i) {
sd_card_t *sd_card_p = sd_get_by_num(i);
if (sd_card_p->card_detect_gpio == gpio) {
if (sd_card_p->ff_disk.xStatus.bIsMounted) {
DBG_PRINTF("(Card Detect Interrupt: unmounting %s)\n", sd_card_p->device_name);
unmount(sd_card_p->device_name);
}
sd_card_p->m_Status |= STA_NOINIT; // in case medium is removed
sd_card_detect(sd_card_p);
}
}
busy = false;
}
int main() {
crash_handler_init();
stdio_init_all();
FreeRTOS_time_init();
setvbuf(stdout, NULL, _IONBF, 1); // specify that the stream should be unbuffered
printf("\033[2J\033[H"); // Clear Screen
stdio_flush();
// Implicitly called by disk_initialize,
// but called here to set up the GPIOs
// before enabling the card detect interrupt:
sd_init_driver();
for (size_t i = 0; i < sd_get_num(); ++i) {
sd_card_t *sd_card_p = sd_get_by_num(i);
if (sd_card_p->use_card_detect) {
// Set up an interrupt on Card Detect to detect removal of the card
// when it happens:
gpio_set_irq_enabled_with_callback(
sd_card_p->card_detect_gpio, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL,
true, &card_detect_callback);
}
}
CLI_Start();
/* Start the tasks and timer running. */
vTaskStartScheduler();
/* should never reach here */
abort();
return 0;
}