aboutsummaryrefslogtreecommitdiff
path: root/sdram-fs.c
blob: c0fcc0938053d122a5e3f1ea5d095d901199aa4e (plain) (blame)
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
#include "sdram-malloc.h"
#include "sdram-fs.h"

/* maximum number of files */
#ifndef SDFS_NFILE
#define SDFS_NFILE 4
#endif

__attribute__((section(".sdram1"))) filetab_t filetab[SDFS_NFILE];
static filetab_t *curfile;
static int nfile;
static int initialized = 0;

/* Initialize the SDRAM file system
 */
int sdfs_init(void)
{
    if (initialized && nfile)
        sdram_free(filetab[0].loc);
    memset((void *)filetab, 0, sizeof(filetab));
    curfile = NULL;
    nfile = 0;
    initialized = 1;
}

/* Return the number of entries in the file table
 */
int sdfs_count(void)
{
    return nfile;
}

/* Return the first entry in the file table
 */
filetab_t *sdfs_first(void)
{
    curfile = filetab;
    return nfile ? curfile : NULL;
}

/* Return the next entry in the file table
 */
filetab_t *sdfs_next(void)
{
    return (++curfile < &filetab[nfile]) ? curfile : NULL;
}

/* Open (create) a new file
 */
int sdfs_open(unsigned char *name)
{
    if (nfile == SDFS_NFILE) {
        curfile = NULL;
        return -1;
    }
    curfile = &filetab[nfile];
    strncpy(curfile->name, (char *)name, sizeof(curfile->name));
    curfile->loc = sdram_malloc(0);
    if (curfile->loc == NULL) {
        curfile = NULL;
        return -1;
    }
    curfile->len = 0;
    ++nfile;
    return 0;
}

/* Write data to the open file
 */
int sdfs_write(unsigned char *buf, int len)
{
    if (curfile == NULL)
        return -1;
    uint8_t *writeptr = sdram_malloc(len);
    if (writeptr == NULL)
        return -1;
    memcpy(writeptr, buf, len);
    curfile->len += len;
    return 0;
}

/* Close the open file
 */
int sdfs_close(void)
{
    if (curfile == NULL)
        return -1;
    int pad = ALIGN - (curfile->len & (ALIGN - 1));
    if (sdram_malloc(pad) == NULL)
        return -1;
    curfile = NULL;
    return 0;
}

/* Close and remove the open file
 */
int sdfs_cancel(void)
{
    if (curfile == NULL)
        return -1;
    sdram_free(curfile->loc);
    memset(curfile, 0, sizeof(*curfile));
    --nfile;
    return 0;
}