1 | /*
|
---|
2 | * TOPPERS ECHONET Lite Communication Middleware
|
---|
3 | *
|
---|
4 | * Copyright (C) 2014-2017 Cores Co., Ltd. Japan
|
---|
5 | *
|
---|
6 | * ä¸è¨èä½æ¨©è
|
---|
7 | ã¯ï¼ä»¥ä¸ã®(1)ï½(4)ã®æ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§
|
---|
8 | * ã¢ï¼æ¬ã½ããã¦ã§ã¢ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹
|
---|
9 | * å¤ã»åé
|
---|
10 | å¸ï¼ä»¥ä¸ï¼å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
11 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
12 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
13 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
14 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
15 | * ç¨ã§ããå½¢ã§åé
|
---|
16 | å¸ããå ´åã«ã¯ï¼åé
|
---|
17 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
18 | * è
|
---|
19 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®èä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
20 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
21 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
22 | * ç¨ã§ããªãå½¢ã§åé
|
---|
23 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
24 | * ã¨ï¼
|
---|
25 | * (a) åé
|
---|
26 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
27 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
28 | * ä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
29 | * (b) åé
|
---|
30 | å¸ã®å½¢æ
|
---|
31 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
32 | * å ±åãããã¨ï¼
|
---|
33 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
34 | * 害ãããï¼ä¸è¨èä½æ¨©è
|
---|
35 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
36 | 責ãããã¨ï¼
|
---|
37 | * ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®ã¦ã¼ã¶ã¾ãã¯ã¨ã³ãã¦ã¼ã¶ããã®ãããªãç
|
---|
38 | * ç±ã«åºã¥ãè«æ±ãããï¼ä¸è¨èä½æ¨©è
|
---|
39 | ããã³TOPPERSããã¸ã§ã¯ãã
|
---|
40 | * å
|
---|
41 | 責ãããã¨ï¼
|
---|
42 | *
|
---|
43 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨èä½æ¨©è
|
---|
44 | ã
|
---|
45 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ç¹å®ã®ä½¿ç¨ç®ç
|
---|
46 | * ã«å¯¾ããé©åæ§ãå«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§
|
---|
47 | * ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ害ã«é¢ãã¦ãï¼ã
|
---|
48 | * ã®è²¬ä»»ãè² ããªãï¼
|
---|
49 | *
|
---|
50 | */
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * ãµã³ãã«ããã°ã©ã (1)ã®æ¬ä½
|
---|
54 | */
|
---|
55 |
|
---|
56 | #include <kernel.h>
|
---|
57 | #include <stdlib.h>
|
---|
58 | #include <string.h>
|
---|
59 | #include "t_stdlib.h"
|
---|
60 | #include "syssvc/serial.h"
|
---|
61 | #include "syssvc/syslog.h"
|
---|
62 | #include "kernel_cfg.h"
|
---|
63 | #include "diskio.h"
|
---|
64 | #include "sdfs.h"
|
---|
65 | #include "ff.h"
|
---|
66 | #include "tlsf.h"
|
---|
67 | #include "ntstdio.h"
|
---|
68 |
|
---|
69 | #define SIO_PORTID 1
|
---|
70 | extern ntstdio_t ntstdio;
|
---|
71 |
|
---|
72 | static tlsf_t sys_tlsf;
|
---|
73 | static pool_t sys_pool;
|
---|
74 |
|
---|
75 | uint32_t __HeapBase;
|
---|
76 | uint32_t __HeapLimit;
|
---|
77 | FATFS RomDisk;
|
---|
78 |
|
---|
79 | gpio_t ins;
|
---|
80 | sdfs_t sdfs;
|
---|
81 | #define WP() false
|
---|
82 |
|
---|
83 | typedef struct SD {
|
---|
84 | DSTATUS dst;
|
---|
85 | BYTE type;
|
---|
86 | FATFS FatFs;
|
---|
87 | } SD;
|
---|
88 | SD Sd;
|
---|
89 |
|
---|
90 | void sys_init(void);
|
---|
91 | bool_t romdisk_init();
|
---|
92 | bool_t SD_begin();
|
---|
93 |
|
---|
94 | int mruby_arduino_init()
|
---|
95 | {
|
---|
96 | int result = -1;
|
---|
97 |
|
---|
98 | sys_init();
|
---|
99 |
|
---|
100 | /* SD_CD */
|
---|
101 | gpio_init_in(&ins, P7_8);
|
---|
102 |
|
---|
103 | /* SDã«ã¼ããåæå */
|
---|
104 | sdfs_init(&sdfs, P8_5, P8_6, P8_3, P8_4, "sd");
|
---|
105 |
|
---|
106 | result = SD_begin() ? 0 : -1;
|
---|
107 | if (result == 0) {
|
---|
108 | ntstdio_printf(&ntstdio, "SD card (1:) OK!\n");
|
---|
109 | }
|
---|
110 | else {
|
---|
111 | ntstdio_printf(&ntstdio, "SD card (1:) NG!\n");
|
---|
112 | }
|
---|
113 | sta_cyc(SDFS_CYC);
|
---|
114 |
|
---|
115 | if (romdisk_init())
|
---|
116 | result = 0;
|
---|
117 |
|
---|
118 | if (result == 0) {
|
---|
119 | ntstdio_printf(&ntstdio, "ROM disk (0:) OK!\n");
|
---|
120 | }
|
---|
121 | else {
|
---|
122 | ntstdio_printf(&ntstdio, "ROM disk (0:) NG!\n");
|
---|
123 | }
|
---|
124 |
|
---|
125 | /* uploadãã£ã¬ã¯ããªãä½æãã¦ãã */
|
---|
126 | f_mkdir("1:/upload");
|
---|
127 |
|
---|
128 | serial_ctl_por(SIO_PORTID, IOCTL_FCSND | IOCTL_FCRCV);
|
---|
129 |
|
---|
130 | return result;
|
---|
131 | }
|
---|
132 |
|
---|
133 | void sys_init(void)
|
---|
134 | {
|
---|
135 | sys_tlsf = tlsf_create(&__HeapBase);
|
---|
136 | if (sys_tlsf == NULL)
|
---|
137 | return;
|
---|
138 |
|
---|
139 | sys_pool = tlsf_add_pool(sys_tlsf, ((uint8_t *)&__HeapBase) + tlsf_size(), ((size_t)&__HeapLimit - (size_t)&__HeapBase) - tlsf_size());
|
---|
140 | }
|
---|
141 |
|
---|
142 | void sys_fini(void)
|
---|
143 | {
|
---|
144 | tlsf_destroy(sys_tlsf);
|
---|
145 | }
|
---|
146 |
|
---|
147 | void *sys_malloc(size_t size)
|
---|
148 | {
|
---|
149 | return tlsf_malloc(sys_tlsf, size);
|
---|
150 | }
|
---|
151 |
|
---|
152 | void *sys_calloc(size_t size, size_t count)
|
---|
153 | {
|
---|
154 | void *result = tlsf_malloc(sys_tlsf, count * size);
|
---|
155 | memset(result, 0, count * size);
|
---|
156 | return result;
|
---|
157 | }
|
---|
158 |
|
---|
159 | void *sys_realloc(void *ptr, size_t size)
|
---|
160 | {
|
---|
161 | return tlsf_realloc(sys_tlsf, ptr, size);
|
---|
162 | }
|
---|
163 |
|
---|
164 | void sys_free(void *ptr)
|
---|
165 | {
|
---|
166 | tlsf_free(sys_tlsf, ptr);
|
---|
167 | }
|
---|
168 |
|
---|
169 | bool_t romdisk_init()
|
---|
170 | {
|
---|
171 | DSTATUS dst;
|
---|
172 | FRESULT res;
|
---|
173 |
|
---|
174 | if (RomDisk.fs_type != 0)
|
---|
175 | return true;
|
---|
176 |
|
---|
177 | if ((dst = ramdisk_initialize()) != RES_OK) {
|
---|
178 | return false;
|
---|
179 | }
|
---|
180 |
|
---|
181 | if ((res = f_mount(&RomDisk, "0:", 1)) != FR_OK) {
|
---|
182 | return false;
|
---|
183 | }
|
---|
184 |
|
---|
185 | return true;
|
---|
186 | }
|
---|
187 |
|
---|
188 | bool_t SD_begin()
|
---|
189 | {
|
---|
190 | DSTATUS dst;
|
---|
191 | FRESULT res;
|
---|
192 | BYTE pdrv = 1, type;
|
---|
193 |
|
---|
194 | if (Sd.FatFs.fs_type != 0)
|
---|
195 | return true;
|
---|
196 |
|
---|
197 | if ((dst = disk_initialize(pdrv)) != RES_OK) {
|
---|
198 | return false;
|
---|
199 | }
|
---|
200 |
|
---|
201 | if ((dst = disk_ioctl(pdrv, MMC_GET_TYPE, &type)) != RES_OK) {
|
---|
202 | Sd.dst = dst;
|
---|
203 | Sd.type = 0;
|
---|
204 | }
|
---|
205 | else {
|
---|
206 | Sd.dst = RES_OK;
|
---|
207 | Sd.type = type;
|
---|
208 | }
|
---|
209 |
|
---|
210 | if ((res = f_mount(&Sd.FatFs, "1:", 1)) != FR_OK) {
|
---|
211 | return false;
|
---|
212 | }
|
---|
213 |
|
---|
214 | return true;
|
---|
215 | }
|
---|
216 |
|
---|
217 | DSTATUS disk_initialize(BYTE pdrv)
|
---|
218 | {
|
---|
219 | switch (pdrv) {
|
---|
220 | case 0:
|
---|
221 | return ramdisk_initialize();
|
---|
222 | case 1:
|
---|
223 | return sdfs_initialize(&sdfs);
|
---|
224 | }
|
---|
225 | return STA_NOINIT;
|
---|
226 | }
|
---|
227 |
|
---|
228 | DSTATUS disk_status(BYTE pdrv)
|
---|
229 | {
|
---|
230 | switch (pdrv) {
|
---|
231 | case 0:
|
---|
232 | return ramdisk_get_status();
|
---|
233 | case 1:
|
---|
234 | return sdfs_status(&sdfs);
|
---|
235 | }
|
---|
236 | return STA_NOINIT;
|
---|
237 | }
|
---|
238 |
|
---|
239 | DRESULT disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
|
---|
240 | {
|
---|
241 | switch (pdrv) {
|
---|
242 | case 0:
|
---|
243 | return ramdisk_read(buff, sector, count);
|
---|
244 | case 1:
|
---|
245 | return sdfs_read(&sdfs, buff, sector, count);
|
---|
246 | }
|
---|
247 | return RES_PARERR;
|
---|
248 | }
|
---|
249 |
|
---|
250 | DRESULT disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
|
---|
251 | {
|
---|
252 | switch (pdrv) {
|
---|
253 | case 0:
|
---|
254 | return ramdisk_write(buff, sector, count);
|
---|
255 | case 1:
|
---|
256 | return sdfs_write(&sdfs, buff, sector, count);
|
---|
257 | }
|
---|
258 | return RES_PARERR;
|
---|
259 | }
|
---|
260 |
|
---|
261 | DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
|
---|
262 | {
|
---|
263 | switch (pdrv) {
|
---|
264 | case 0:
|
---|
265 | return RES_PARERR; /* ramdisk_ioctl(cmd, buff); */
|
---|
266 | case 1:
|
---|
267 | switch(cmd) {
|
---|
268 | case CTRL_SYNC:
|
---|
269 | return sdfs_sync(&sdfs);
|
---|
270 | case GET_SECTOR_COUNT:
|
---|
271 | return sdfs_sectors(&sdfs);
|
---|
272 | case GET_BLOCK_SIZE:
|
---|
273 | *((DWORD*)buff) = 1; // default when not known
|
---|
274 | return RES_OK;
|
---|
275 | }
|
---|
276 | }
|
---|
277 | return RES_PARERR;
|
---|
278 | }
|
---|
279 |
|
---|
280 | void sdfs_cychdr(intptr_t exinf)
|
---|
281 | {
|
---|
282 | BYTE s;
|
---|
283 |
|
---|
284 | s = sdfs._is_initialized;
|
---|
285 | if (WP()) /* Write protected */
|
---|
286 | s |= STA_PROTECT;
|
---|
287 | else /* Write enabled */
|
---|
288 | s &= ~STA_PROTECT;
|
---|
289 | if (gpio_read(&ins)) /* Card is in socket */
|
---|
290 | s &= ~STA_NODISK;
|
---|
291 | else /* Socket empty */
|
---|
292 | s |= (STA_NODISK | STA_NOINIT);
|
---|
293 | sdfs._is_initialized = s;
|
---|
294 | }
|
---|
295 |
|
---|
296 | int ff_cre_syncobj(BYTE vol, _SYNC_t* sobj)
|
---|
297 | {
|
---|
298 | return 1;
|
---|
299 | }
|
---|
300 |
|
---|
301 | int ff_req_grant(_SYNC_t sobj)
|
---|
302 | {
|
---|
303 | ER ret;
|
---|
304 | ret = wai_sem(SEM_FILESYSTEM);
|
---|
305 | return ret == E_OK;
|
---|
306 | }
|
---|
307 |
|
---|
308 | void ff_rel_grant(_SYNC_t sobj)
|
---|
309 | {
|
---|
310 | sig_sem(SEM_FILESYSTEM);
|
---|
311 | }
|
---|
312 |
|
---|
313 | int ff_del_syncobj(_SYNC_t sobj)
|
---|
314 | {
|
---|
315 | return 1;
|
---|
316 | }
|
---|
317 |
|
---|
318 | void* ff_memalloc (UINT msize)
|
---|
319 | {
|
---|
320 | return sys_malloc(msize);
|
---|
321 | }
|
---|
322 |
|
---|
323 | void ff_memfree (void* mblock)
|
---|
324 | {
|
---|
325 | sys_free(mblock);
|
---|
326 | }
|
---|