1 #include <stm32f10x_map.h>
8 #include <interfaces/sd.h>
12 process_event_t sdcard_inserted_event;
14 process_event_t sdcard_removed_event;
16 static struct process *event_process =
NULL;
22 #define DBG(x) printf x
28 SPI1->CR1 &= ~SPI_CR1_SPE;
29 RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
30 GPIO_CONF_INPUT_PORT(A,0,FLOATING);
31 GPIO_CONF_INPUT_PORT(A,1,FLOATING);
32 GPIO_CONF_OUTPUT_PORT(A,4,PUSH_PULL,50);
33 GPIOA->BSRR = GPIO_BSRR_BS4;
34 GPIO_CONF_OUTPUT_PORT(A,5,ALT_PUSH_PULL,50);
35 GPIO_CONF_INPUT_PORT(A,6,FLOATING);
36 GPIO_CONF_OUTPUT_PORT(A,7,ALT_PUSH_PULL,50);
37 RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;
38 SPI1->CR2 = SPI_CR2_SSOE;
39 SPI1->CR1 = (SPI_CR1_SPE
42 | SPI_CR1_CPOL | SPI_CR1_CPHA
43 | SPI_CR1_SSM | SPI_CR1_SSI);
48 if_spiInit(hwInterface *iface)
51 GPIOA->BSRR = GPIO_BSRR_BS4;
53 if_spiSend(iface, 0xff);
55 GPIOA->BSRR = GPIO_BSRR_BR4;
60 if_initInterface(hwInterface* file, eint8* opts)
65 DBG((TXT(
"Card failed to init, breaking up...\n")));
70 DBG((TXT(
"Card didn't return the ready state, breaking up...\n")
77 sd_getDriveSize(file, &sc);
78 file->sectorCount = sc/512;
79 DBG((TXT(
"Card Capacity is %lu Bytes (%lu Sectors)\n"), sc, file->sectorCount));
88 if_readBuf(hwInterface* file,euint32 address,euint8* buf)
90 return(sd_readSector(file,address,buf,512));
94 if_writeBuf(hwInterface* file,euint32 address,euint8* buf)
96 return(sd_writeSector(file,address, buf));
100 if_setPos(hwInterface* file,euint32 address)
107 if_spiSend(hwInterface *iface, euint8 outgoing)
111 while(!(
SPI1->SR & SPI_SR_RXNE));
119 static EmbeddedFileSystem sdcard_efs;
120 static File file_descriptors[MAX_FDS];
126 for (fd = 0; fd < MAX_FDS; fd++) {
127 if (!file_getAttr(&file_descriptors[fd], FILE_STATUS_OPEN)) {
137 if (sdcard_efs.myCard.sectorCount == 0)
return NULL;
138 if (fd >= MAX_FDS || fd < 0)
return NULL;
139 if (!file_getAttr(&file_descriptors[fd], FILE_STATUS_OPEN))
return NULL;
140 return &file_descriptors[fd];
148 if (sdcard_efs.myCard.sectorCount == 0)
return -1;
150 if (fd < 0)
return -1;
156 if (file_fopen(&file_descriptors[fd], &sdcard_efs.myFs,
157 (
char*)name, mode) < 0) {
166 File *file = get_file(fd);
169 fs_flushFs(&sdcard_efs.myFs);
173 cfs_read (
int fd,
void *buf,
unsigned int len)
175 File *file = get_file(fd);
177 return file_read(file, len, (euint8*)buf);
181 cfs_write (
int fd,
const void *buf,
unsigned int len)
183 File *file = get_file(fd);
185 return file_write(file, len, (euint8*)buf);
191 File *file = get_file(fd);
194 if (file_setpos(file, offset) != 0)
return -1;
195 return file->FilePtr;
201 return (rmfile(&sdcard_efs.myFs,(euint8*)name) == 0) ? 0 : -1;
206 #define COMPILE_TIME_CHECK(expr) \
207 (void) (__builtin_choose_expr ((expr), 0, ((void)0))+3)
209 #define COMPILE_TIME_CHECK(expr)
212 #define MAX_DIR_LISTS 4
213 DirList dir_lists[MAX_DIR_LISTS];
219 for(l = 0; l < MAX_DIR_LISTS; l++) {
220 if (dir_lists[l].fs ==
NULL) {
221 return &dir_lists[l];
231 COMPILE_TIME_CHECK(
sizeof(DirList*) <=
sizeof(
struct cfs_dir));
232 if (sdcard_efs.myCard.sectorCount == 0)
return -1;
233 dirs = find_free_dir_list();
234 if (!dirs)
return -1;
235 if (ls_openDir(dirs, &sdcard_efs.myFs, (eint8*)name) != 0) {
239 *(DirList**)dirp = dirs;
248 char *to = dirent->name;
249 DirList *dirs = *(DirList**)dirp;
250 if (sdcard_efs.myCard.sectorCount == 0)
return 1;
251 if (ls_getNext(dirs) != 0)
return 1;
252 start = dirs->currentEntry.FileName;
264 start = dirs->currentEntry.FileName + 8;
269 while(start < end && *start >
' ') {
274 if (dirs->currentEntry.Attribute & ATTR_DIRECTORY) {
277 dirent->size = dirs->currentEntry.FileSize;
285 (*(DirList**)dirp)->fs =
NULL;
289 PROCESS(sdcard_process,
"SD card process");
297 for (fd = 0; fd < MAX_FDS; fd++) {
298 file_setAttr(&file_descriptors[fd], FILE_STATUS_OPEN,0);
301 sdcard_efs.myCard.sectorCount = 0;
307 ev== PROCESS_EVENT_TIMER || ev == PROCESS_EVENT_POLL);
308 if (ev == PROCESS_EVENT_EXIT)
break;
309 if (ev == PROCESS_EVENT_TIMER) {
310 if (!(GPIOA->IDR & (1<<0))) {
311 if (sdcard_efs.myCard.sectorCount == 0) {
314 if (efs_init(&sdcard_efs,0) == 0) {
318 printf(
"SD card inserted\n");
320 printf(
"SD card insertion failed\n");
324 if (sdcard_efs.myCard.sectorCount != 0) {
326 fs_umount(&sdcard_efs.myFs);
327 sdcard_efs.myCard.sectorCount = 0;
331 printf(
"SD card removed\n");
352 return sdcard_efs.myCard.sectorCount > 0;
356 sdcard_event_process(
struct process *p)
int cfs_open(const char *name, int flags)
Open a file.
cfs_offset_t cfs_seek(int fd, cfs_offset_t offset, int whence)
Seek to a specified position in an open file.
#define PROCESS_BEGIN()
Define the beginning of a process.
#define NULL
The null pointer.
process_event_t process_alloc_event(void)
Allocate a global event number.
#define CFS_READ
Specify that cfs_open() should open a file for reading.
int cfs_readdir(struct cfs_dir *dir, struct cfs_dirent *record)
Read a directory entry.
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
#define PROCESS_END()
Define the end of a process.
int cfs_remove(const char *name)
Remove a file.
void cfs_closedir(struct cfs_dir *dir)
Close a directory opened with cfs_opendir().
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
#define PROCESS(name, strname)
Declare a process.
int cfs_opendir(struct cfs_dir *dir, const char *name)
Open a directory for reading directory entries.
void process_start(struct process *p, process_data_t data)
Start a process.
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Header file for the Contiki process interface.
#define SPI1
Peripheral SPI1 base pointer.
void cfs_close(int fd)
Close an open file.
#define CLOCK_SECOND
A second, measured in system clock time.