+/rtos

git clone https://git.julia.blue/+/rtos

Vivek MalneediMerge branch 'master' of github.com:EE445M-Sp20/lab-6-vm22967_jbr2558021d2d4

master
4.3 KiB186 linesraw
1#include "OS.h"
2#include "eDisk.h"
3#include "heap.h"
4#include "interpreter.h"
5#include "io.h"
6#include "lfs.h"
7#include "printf.h"
8
9#define BLOCK_SIZE 512
10
11const static uint8_t erase_buffer[BLOCK_SIZE] = {0};
12
13static int block_prog(const struct lfs_config* c, lfs_block_t block,
14                      lfs_off_t off, const void* buffer, lfs_size_t size) {
15    eDisk_Write(buffer, block, size / BLOCK_SIZE);
16    return 0;
17}
18
19static int block_read(const struct lfs_config* c, lfs_block_t block,
20                      lfs_off_t off, void* buffer, lfs_size_t size) {
21    eDisk_Read(buffer, block, size / BLOCK_SIZE);
22    return 0;
23}
24
25static int block_erase(const struct lfs_config* c, lfs_block_t block) {
26    eDisk_Write(erase_buffer, block, 1);
27    return 0;
28}
29
30static int sync(const struct lfs_config* c) {
31    return 0;
32}
33
34// these structs are used used by the filesystem
35static lfs_t* lfs;
36static lfs_file_t* file;
37
38static const struct lfs_config cfg = {
39    // block device operations
40    .read = block_read,
41    .prog = block_prog,
42    .erase = block_erase,
43    .sync = sync,
44
45    // block device configuration
46    .read_size = BLOCK_SIZE,
47    .prog_size = BLOCK_SIZE,
48    .block_size = BLOCK_SIZE,
49    .block_count = 1 << 21, // 1GiB
50    .cache_size = BLOCK_SIZE,
51    .lookahead_size = 32,
52    .block_cycles = 500,
53
54    .name_max = 63,
55};
56
57bool littlefs_init(void) {
58    if (!lfs) {
59        lfs = malloc(sizeof(lfs_t));
60    }
61    if (!file) {
62        file = malloc(sizeof(lfs_file_t));
63    }
64    return (!eDisk_Init()) && lfs && file;
65}
66
67bool littlefs_format(void) {
68    return lfs_format(lfs, &cfg) >= 0;
69}
70
71bool littlefs_mount(void) {
72    return lfs_mount(lfs, &cfg) >= 0;
73}
74
75bool littlefs_open_file(const char* name, bool create) {
76    return lfs_file_open(lfs, file, name,
77                         LFS_O_RDWR | (create ? LFS_O_CREAT : 0)) >= 0;
78}
79
80bool littlefs_open_file_append(const char* name, bool create) {
81    return lfs_file_open(lfs, file, name,
82                         LFS_O_RDWR | (create ? LFS_O_CREAT : 0) |
83                             LFS_O_APPEND) >= 0;
84}
85
86bool littlefs_read(uint8_t* output) {
87    return lfs_file_read(lfs, file, output, 1) == 1;
88}
89
90uint16_t littlefs_read_line(char* str, uint16_t len) {
91    int received = 0;
92    char current = '\0';
93    while (len--) {
94        if (!littlefs_read((uint8_t*)&current)) {
95            *str++ = '\n';
96            *str = '\0';
97            return 0;
98        }
99        switch (current) {
100        case '\n':
101        case '\r':
102            *str++ = '\n';
103            *str = '\0';
104            return ++received;
105        case 127:
106            if (received > 0) {
107                --str;
108                --received;
109            }
110            break;
111        default: *str++ = current; ++received;
112        }
113    }
114    *--str = '\0';
115    return received;
116}
117
118int32_t littlefs_read_buffer(void* buffer, uint32_t size) {
119    return lfs_file_read(lfs, file, buffer, size);
120}
121
122bool littlefs_seek(int32_t off) {
123    return lfs_file_seek(lfs, file, off, LFS_SEEK_SET) > -1;
124}
125
126int32_t littlefs_tell() {
127    return lfs_file_tell(lfs, file);
128}
129
130int32_t littlefs_write_buffer(void* buffer, uint32_t size) {
131    return lfs_file_write(lfs, file, buffer, size) >= 0;
132}
133
134bool littlefs_close_file(void) {
135    return lfs_file_close(lfs, file) >= 0;
136}
137
138bool littlefs_unmount(void) {
139    return lfs_unmount(lfs) >= 0;
140}
141
142bool littlefs_remove(const char* name) {
143    return lfs_remove(lfs, name) >= 0;
144}
145
146bool littlefs_move(const char* name, const char* new_name) {
147    return lfs_rename(lfs, name, new_name) >= 0;
148}
149
150bool littlefs_write(uint8_t c) {
151    return lfs_file_write(lfs, file, &c, 1) >= 0;
152}
153
154bool littlefs_ls(void) {
155    lfs_dir_t dir;
156    if (lfs_dir_open(lfs, &dir, "")) { // empty name for root dir
157        return false;
158    }
159    struct lfs_info info;
160    while (true) {
161        int result = lfs_dir_read(lfs, &dir, &info);
162        if (result < 0) {
163            return false;
164        } else if (!result) {
165            break;
166        } else if (info.type == LFS_TYPE_DIR) {
167            continue; // we are only concerned with the root
168        }
169        printf("%-32s %d bytes\n\r", info.name, info.size);
170    }
171    return !lfs_dir_close(lfs, &dir);
172}
173
174void littlefs_test(void) {
175    if (littlefs_init()) {
176        printf("error: edisk init\n\r");
177        OS_Kill();
178    }
179
180    // puts("formatting");
181    // littlefs_format();
182
183    // mount the filesystem
184    // littlefs_mount();
185    OS_Kill();
186}