[270] | 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 <errno.h>
|
---|
| 59 | #include <sys/types.h>
|
---|
| 60 | #include <string.h>
|
---|
| 61 | #include "t_stdlib.h"
|
---|
| 62 | #include "syssvc/syslog.h"
|
---|
| 63 | #include "kernel_cfg.h"
|
---|
| 64 | #include "diskio.h"
|
---|
| 65 | #include "sdfs.h"
|
---|
| 66 | #include "ff.h"
|
---|
| 67 |
|
---|
| 68 | gpio_t ins;
|
---|
| 69 | sdfs_t sdfs;
|
---|
| 70 | #define WP() false
|
---|
| 71 |
|
---|
| 72 | extern FATFS RomDisk;
|
---|
| 73 | extern unsigned char RamDisk[SZ_RAMDISK * 1024];
|
---|
| 74 |
|
---|
| 75 | bool_t romdisk_init();
|
---|
| 76 |
|
---|
| 77 | int mruby_arduino_init()
|
---|
| 78 | {
|
---|
| 79 | int result = -1;
|
---|
| 80 |
|
---|
| 81 | /* SD_CD */
|
---|
| 82 | gpio_init_in(&ins, P7_8);
|
---|
| 83 |
|
---|
| 84 | /* SDã«ã¼ããåæå */
|
---|
| 85 | sdfs_init(&sdfs, P8_5, P8_6, P8_3, P8_4, "sd");
|
---|
| 86 |
|
---|
| 87 | SD_begin();
|
---|
| 88 |
|
---|
| 89 | FIL fd;
|
---|
| 90 | UINT rlen = 0;
|
---|
| 91 | memset(&fd, 0, sizeof(fd));
|
---|
| 92 | if (f_open(&fd, "1:/httpd-fs.bin", FA_OPEN_EXISTING | FA_READ) == FR_OK) {
|
---|
| 93 | f_read(&fd, RamDisk, SZ_RAMDISK * 1024, &rlen);
|
---|
| 94 | f_close(&fd);
|
---|
| 95 | if (romdisk_init())
|
---|
| 96 | result = 0;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | if (result == 0) {
|
---|
| 100 | syslog(LOG_NOTICE, "ramdisk ok!");
|
---|
| 101 | }
|
---|
| 102 | else {
|
---|
| 103 | syslog(LOG_NOTICE, "ramdisk ng!");
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | arduino_init();
|
---|
| 107 |
|
---|
| 108 | return result;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | bool_t romdisk_init()
|
---|
| 112 | {
|
---|
| 113 | DSTATUS dst;
|
---|
| 114 | FRESULT res;
|
---|
| 115 |
|
---|
| 116 | if (RomDisk.fs_type != 0)
|
---|
| 117 | return true;
|
---|
| 118 |
|
---|
| 119 | if ((dst = ramdisk_initialize()) != RES_OK) {
|
---|
| 120 | return false;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | if ((res = f_mount(&RomDisk, "0:", 1)) != FR_OK) {
|
---|
| 124 | return false;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | return true;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | DSTATUS disk_initialize(BYTE pdrv)
|
---|
| 131 | {
|
---|
| 132 | switch (pdrv) {
|
---|
| 133 | case 0:
|
---|
| 134 | return ramdisk_initialize();
|
---|
| 135 | case 1:
|
---|
| 136 | return sdfs_initialize(&sdfs);
|
---|
| 137 | }
|
---|
| 138 | return STA_NOINIT;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | DSTATUS disk_status(BYTE pdrv)
|
---|
| 142 | {
|
---|
| 143 | switch (pdrv) {
|
---|
| 144 | case 0:
|
---|
| 145 | return ramdisk_get_status();
|
---|
| 146 | case 1:
|
---|
| 147 | return sdfs_status(&sdfs);
|
---|
| 148 | }
|
---|
| 149 | return STA_NOINIT;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | DRESULT disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
|
---|
| 153 | {
|
---|
| 154 | switch (pdrv) {
|
---|
| 155 | case 0:
|
---|
| 156 | return ramdisk_read(buff, sector, count);
|
---|
| 157 | case 1:
|
---|
| 158 | return sdfs_read(&sdfs, buff, sector, count);
|
---|
| 159 | }
|
---|
| 160 | return RES_PARERR;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | DRESULT disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
|
---|
| 164 | {
|
---|
| 165 | switch (pdrv) {
|
---|
| 166 | case 0:
|
---|
| 167 | return ramdisk_write(buff, sector, count);
|
---|
| 168 | case 1:
|
---|
| 169 | return sdfs_write(&sdfs, buff, sector, count);
|
---|
| 170 | }
|
---|
| 171 | return RES_PARERR;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
|
---|
| 175 | {
|
---|
| 176 | switch (pdrv) {
|
---|
| 177 | case 0:
|
---|
| 178 | return RES_PARERR; /* ramdisk_ioctl(cmd, buff); */
|
---|
| 179 | case 1:
|
---|
| 180 | switch(cmd) {
|
---|
| 181 | case CTRL_SYNC:
|
---|
| 182 | return sdfs_sync(&sdfs);
|
---|
| 183 | case GET_SECTOR_COUNT:
|
---|
| 184 | return sdfs_sectors(&sdfs);
|
---|
| 185 | case GET_BLOCK_SIZE:
|
---|
| 186 | *((DWORD*)buff) = 1; // default when not known
|
---|
| 187 | return RES_OK;
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | return RES_PARERR;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | void sdfs_cychdr(intptr_t exinf)
|
---|
| 194 | {
|
---|
| 195 | BYTE s;
|
---|
| 196 |
|
---|
| 197 | s = sdfs._is_initialized;
|
---|
| 198 | if (WP()) /* Write protected */
|
---|
| 199 | s |= STA_PROTECT;
|
---|
| 200 | else /* Write enabled */
|
---|
| 201 | s &= ~STA_PROTECT;
|
---|
| 202 | if (gpio_read(&ins)) /* Card is in socket */
|
---|
| 203 | s &= ~STA_NODISK;
|
---|
| 204 | else /* Socket empty */
|
---|
| 205 | s |= (STA_NODISK | STA_NOINIT);
|
---|
| 206 | sdfs._is_initialized = s;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | static int lock[TNUM_TSKID];
|
---|
| 210 | static int stack_pos[TNUM_TSKID];
|
---|
| 211 |
|
---|
| 212 | void __malloc_lock(struct _reent *a)
|
---|
| 213 | {
|
---|
| 214 | ID tskid;
|
---|
| 215 | ER ret;
|
---|
| 216 | int count, sp;
|
---|
| 217 |
|
---|
| 218 | ret = get_tid(&tskid);
|
---|
| 219 | if (ret != E_OK) {
|
---|
| 220 | /*syslog(LOG_DEBUG, "get_tid %s", itron_strerror(ret));*/
|
---|
| 221 | Asm("bkpt #0");
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | Asm("mov %0, sp" : "=r"(sp) :);
|
---|
| 225 | if ((stack_pos[tskid - 1] == 0) || (stack_pos[tskid - 1] < sp))
|
---|
| 226 | stack_pos[tskid - 1] = sp;
|
---|
| 227 |
|
---|
| 228 | count = ++lock[tskid - 1];
|
---|
| 229 | if (count != 1)
|
---|
| 230 | return;
|
---|
| 231 |
|
---|
| 232 | ret = wai_sem(SEM_MALLOC);
|
---|
| 233 | if (ret != E_OK) {
|
---|
| 234 | /*syslog(LOG_DEBUG, "wai_sem %s", itron_strerror(ret));*/
|
---|
| 235 | Asm("bkpt #0");
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | void __malloc_unlock(struct _reent *a)
|
---|
| 240 | {
|
---|
| 241 | ID tskid;
|
---|
| 242 | ER ret;
|
---|
| 243 | int count;
|
---|
| 244 |
|
---|
| 245 | ret = get_tid(&tskid);
|
---|
| 246 | if (ret != E_OK) {
|
---|
| 247 | /*syslog(LOG_DEBUG, "get_tid %s", itron_strerror(ret));*/
|
---|
| 248 | Asm("bkpt #0");
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | count = --lock[tskid - 1];
|
---|
| 252 | if(count != 0)
|
---|
| 253 | return;
|
---|
| 254 |
|
---|
| 255 | ret = sig_sem(SEM_MALLOC);
|
---|
| 256 | if (ret != E_OK) {
|
---|
| 257 | /*syslog(LOG_DEBUG, "sig_sem %s", itron_strerror(ret));*/
|
---|
| 258 | Asm("bkpt #0");
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | // Provide implementation of _sbrk (low-level dynamic memory allocation
|
---|
| 263 | // routine) for GCC_ARM which compares new heap pointer with MSP instead of
|
---|
| 264 | // SP. This make it compatible with RTX RTOS thread stacks.
|
---|
| 265 |
|
---|
| 266 | // Linker defined symbol used by _sbrk to indicate where heap should start.
|
---|
| 267 | int __end__;
|
---|
| 268 | uint32_t __HeapLimit;
|
---|
| 269 |
|
---|
| 270 | // Turn off the errno macro and use actual global variable instead.
|
---|
| 271 | #undef errno
|
---|
| 272 | int errno;
|
---|
| 273 |
|
---|
| 274 | static unsigned char* heap = (unsigned char*)&__end__;
|
---|
| 275 |
|
---|
| 276 | // Dynamic memory allocation related syscall.
|
---|
| 277 | caddr_t _sbrk(int incr) {
|
---|
| 278 | unsigned char* prev_heap = heap;
|
---|
| 279 | unsigned char* new_heap = heap + incr;
|
---|
| 280 |
|
---|
| 281 | if (new_heap >= (unsigned char*)&__HeapLimit) { /* __HeapLimit is end of heap section */
|
---|
| 282 | errno = ENOMEM;
|
---|
| 283 | return (caddr_t)-1;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | heap = new_heap;
|
---|
| 287 | return (caddr_t) prev_heap;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | void reset_heap()
|
---|
| 291 | {
|
---|
| 292 | // .data
|
---|
| 293 | extern uint8_t __malloc_av_[0x408];
|
---|
| 294 | extern uint8_t __malloc_sbrk_base[0x4];
|
---|
| 295 | extern uint8_t __malloc_trim_threshold[0x4];
|
---|
| 296 | // .bss
|
---|
| 297 | extern uint8_t __malloc_top_pad[0x4];
|
---|
| 298 | extern uint8_t __malloc_current_mallinfo[0x28];
|
---|
| 299 | extern uint8_t __malloc_max_sbrked_mem[0x4];
|
---|
| 300 | extern uint8_t __malloc_max_total_mem[0x4];
|
---|
| 301 | //
|
---|
| 302 | extern void *__etext;
|
---|
| 303 | extern void *__data_start__;
|
---|
| 304 |
|
---|
| 305 | int offset = (int)&__etext - (int)&__data_start__;
|
---|
| 306 |
|
---|
| 307 | __malloc_lock(_REENT);
|
---|
| 308 |
|
---|
| 309 | memcpy(__malloc_av_, &__malloc_av_[offset], sizeof(__malloc_av_));
|
---|
| 310 | memcpy(__malloc_sbrk_base, &__malloc_sbrk_base[offset], sizeof(__malloc_sbrk_base));
|
---|
| 311 | memcpy(__malloc_trim_threshold, &__malloc_trim_threshold[offset], sizeof(__malloc_trim_threshold));
|
---|
| 312 |
|
---|
| 313 | memset(__malloc_top_pad, 0, sizeof(__malloc_top_pad));
|
---|
| 314 | memset(__malloc_current_mallinfo, 0, sizeof(__malloc_current_mallinfo));
|
---|
| 315 | memset(__malloc_max_sbrked_mem, 0, sizeof(__malloc_max_sbrked_mem));
|
---|
| 316 | memset(__malloc_max_total_mem, 0, sizeof(__malloc_max_total_mem));
|
---|
| 317 |
|
---|
| 318 | heap = (unsigned char*)&__end__;
|
---|
| 319 |
|
---|
| 320 | __malloc_unlock(_REENT);
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | void exit(int return_code) {
|
---|
| 324 | Asm("bkpt #0");
|
---|
| 325 | reset_heap();
|
---|
| 326 | ext_tsk();
|
---|
| 327 | for(;;);
|
---|
| 328 | }
|
---|