-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsd.c
153 lines (129 loc) · 3.52 KB
/
sd.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
/*
Copyright (C) 2013-2015 Andrea Zoppi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "ch.h"
#include "hal.h"
#include "hal_sdc.h"
#include "chevents.h"
#include "string.h"
#include "hal_fsmc_sdram.h"
#include "ff.h"
#define SD_POLLING_INTERVAL 10
#define SD_POLLING_DELAY 10
#define SD_TIMEOUT 500
//20 because this is greater than all FRESULT integers
#define SD_PARTIAL_WRITE 20
static FATFS SDC_FS;
static bool sd_fs_ready = FALSE;
static int cnt = 0;
static virtual_timer_t tmr;
static void sd_insert_handler(eventid_t id) {
FRESULT err;
(void) id;
#if HAL_USE_SDC
if (sdcConnect(&SDCD1))
#else
if (mmcConnect(&MMCD1))
#endif
return;
err = f_mount(&SDC_FS, "/", 1);
if (err != FR_OK) {
#if HAL_USE_SDC
sdcDisconnect(&SDCD1);
#else
mmcDisconnect(&MMCD1);
#endif
return;
}
sd_fs_ready = TRUE;
}
static void sd_remove_handler(eventid_t id) {
(void) id;
#if HAL_USE_SDC
sdcDisconnect(&SDCD1);
#else
mmcDisconnect(&MMCD1);
#endif
sd_fs_ready = FALSE;
}
static event_source_t sd_inserted_event, sd_removed_event;
static void tmrfunc(void *p) {
BaseBlockDevice *bbdp = p;
chSysLockFromISR();
if (cnt > 0) {
if (blkIsInserted(bbdp)) {
if (--cnt == 0) {
chEvtBroadcastI(&sd_inserted_event);
}
}
else cnt = SD_POLLING_INTERVAL;
}
else if (!blkIsInserted(bbdp)) {
cnt = SD_POLLING_INTERVAL;
chEvtBroadcastI(&sd_removed_event);
}
chVTSetI(&tmr, TIME_MS2I(SD_POLLING_DELAY), tmrfunc, bbdp);
chSysUnlockFromISR();
}
static void sd_tmr_init(void *p) {
chEvtObjectInit(&sd_inserted_event);
chEvtObjectInit(&sd_removed_event);
chSysLock();
cnt = SD_POLLING_INTERVAL;
chVTSetI(&tmr, TIME_MS2I(SD_POLLING_DELAY), tmrfunc, p);
chSysUnlock();
}
uint8_t sd_init(void) {
/*
* On insertion SDC initialization and FS mount.
*/
static const evhandler_t sd_evhndl[] = {sd_insert_handler, sd_remove_handler};
event_listener_t sd_inserted_listener, sd_removed_listener;
sdcStart(&SDCD1, NULL);
// Activate card insertion monitor
sd_tmr_init(&SDCD1);
chEvtRegister(&sd_inserted_event, &sd_inserted_listener, 0);
chEvtRegister(&sd_removed_event, &sd_removed_listener, 0);
chEvtDispatch(sd_evhndl, chEvtWaitOneTimeout(ALL_EVENTS, TIME_MS2I(SD_TIMEOUT)));
return sd_fs_ready ? 0 : 1;
}
uint8_t sd_write(FIL* fil, char* string) {
unsigned int len = strlen(string);
unsigned int written;
FRESULT failure = f_write(fil, string, len, &written);
if(failure) {
//TODO handle error
return failure;
}
if(len < written) {
//TODO handle error
return SD_PARTIAL_WRITE;
}
return FR_OK;
}
uint8_t sd_test(void) {
FIL f;
FRESULT open_err = f_open(&f, "testing123.txt", FA_CREATE_NEW);
f_close(&f);
open_err = f_open(&f, "testing123.txt", FA_OPEN_APPEND);
if(open_err != FR_OK) {
f_close(&f);
return open_err;
}
FRESULT write_err = sd_write(&f, "testing\n");
if(write_err) {
f_close(&f);
return write_err;
}
f_close(&f);
return FR_OK;
}