source: UsbWattMeter/trunk/lwip-1.4.1/ports/asp_dcre/sys_arch.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 6 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 10.4 KB
Line 
1/*
2 * ‘æ3‰ñ TOPPERSŠˆ—pƒAƒCƒfƒAEƒAƒvƒŠƒP[ƒVƒ‡ƒ“ŠJ”­ƒRƒ“ƒeƒXƒg
3 * ¼‰Y Œõ—m‚³‚ñ‚́ulwIP ‚̈ڐAv‚ðƒx[ƒX‚É‚µ‚Ü‚µ‚½B
4 * http://www.toppers.jp/contest.html
5 *
6 * @(#) $Id: sys_arch.c 164 2016-03-07 11:33:50Z coas-nagasima $
7 */
8#include <string.h>
9#include <kernel.h>
10#include <t_syslog.h>
11
12#include "arch/sys_arch.h"
13
14#include "lwip/opt.h"
15#include "lwip/debug.h"
16#include "lwip/def.h"
17#include "lwip/sys.h"
18#include "lwip/mem.h"
19#include "lwip/stats.h"
20#include "tlsf.h"
21
22static tlsf_t lwip_tlsf;
23static pool_t lwip_pool;
24extern void *lwip_heap;
25extern size_t lwip_heapsz;
26extern ID _echonet_ecn_mem_semid;
27
28void syslog_info(const char *format, ...)
29{
30 va_list ap;
31 va_start(ap, format);
32 vsyslog(LOG_NOTICE, format, ap);
33 va_end(ap);
34}
35
36void syslog_assert(const char *format, ...)
37{
38 va_list ap;
39 va_start(ap, format);
40 vsyslog(LOG_EMERG, format, ap);
41 va_end(ap);
42}
43
44void sys_init(void)
45{
46 lwip_tlsf = tlsf_create(lwip_heap);
47 if (lwip_tlsf == NULL)
48 return;
49
50 lwip_pool = tlsf_add_pool(lwip_tlsf, ((uint8_t *)lwip_heap) + tlsf_size(), lwip_heapsz - tlsf_size());
51}
52
53err_t sys_sem_new(sys_sem_t *sem, u8_t count)
54{
55 err_t ret = ERR_VAL;
56
57 syslog(LOG_DEBUG, "sys_sem_new in\n");
58 if (NULL != sem) {
59 T_CSEM csem = { TA_TPRI, (uint_t)count, (uint_t)1 };
60 ID id = acre_sem(&csem);
61 if (0 < id) {
62 *sem = id;
63 ret = ERR_OK;
64 syslog(LOG_DEBUG, "sys_sem_new out %d, %d\n", *sem, count);
65 }
66 else {
67 *sem = (sys_sem_t)NULL;
68 syslog(LOG_DEBUG, "sys_sem_new out ERROR!\n");
69 }
70 }
71 return ret;
72}
73
74void sys_sem_free(sys_sem_t *sem)
75{
76 syslog(LOG_DEBUG, "sys_sem_free in %d\n", *sem);
77 del_sem(*sem);
78 *sem = (sys_sem_t)NULL;
79 syslog(LOG_DEBUG, "sys_sem_free out\n");
80 return;
81}
82
83void sys_sem_signal(sys_sem_t *sem)
84{
85 syslog(LOG_DEBUG, "sys_sem_signal in %d\n", *sem);
86 sig_sem(*sem);
87 syslog(LOG_DEBUG, "sys_sem_signal out %d\n", *sem);
88 return;
89}
90
91u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
92{
93 SYSTIM systim0, systim1;
94 ER ercd;
95
96 get_tim(&systim0);
97 syslog(LOG_DEBUG, "sys_arch_sem_wait in %d\n", *sem);
98 if (timeout) {
99 ercd = twai_sem(*sem, (TMO)timeout);
100 }
101 else {
102 ercd = wai_sem(*sem);
103 }
104
105 if (E_OK == ercd) {
106 get_tim(&systim1);
107 syslog(LOG_DEBUG, "sys_arch_sem_wait out %d, E_OK\n", *sem);
108 return (u32_t)(systim1 - systim0);
109 }
110 syslog(LOG_DEBUG, "sys_arch_sem_wait out %d, SYS_ARCH_TIMEOUT\n", *sem);
111 return SYS_ARCH_TIMEOUT;
112}
113
114err_t sys_mbox_new(sys_mbox_t *mbox, int size)
115{
116 err_t ret = ERR_VAL;
117
118 syslog(LOG_DEBUG, "sys_mbox_new in\n");
119 if (NULL != mbox) {
120 T_CDTQ cdtq = { TA_NULL, (uint_t)size, (void *)NULL };
121 ID id = acre_dtq(&cdtq);
122 if (0 < id) {
123 *mbox = id;
124 ret = ERR_OK;
125 syslog(LOG_DEBUG, "sys_mbox_new out %d, %d\n", *mbox, size);
126 }
127 else {
128 *mbox = (sys_mbox_t)NULL;
129 syslog(LOG_DEBUG, "sys_mbox_new out ERROR!\n");
130 }
131 }
132 return ret;
133}
134
135void sys_mbox_free(sys_mbox_t *mbox)
136{
137 syslog(LOG_DEBUG, "sys_mbox_free in %d\n", *mbox);
138 del_dtq(*mbox);
139 *mbox = (sys_mbox_t)NULL;
140 syslog(LOG_DEBUG, "sys_mbox_free out\n");
141 return;
142}
143
144void sys_mbox_post(sys_mbox_t *mbox, void *msg)
145{
146 syslog(LOG_DEBUG, "sys_mbox_post in %d, 0x%08x\n", *mbox, msg);
147 snd_dtq(*mbox, (intptr_t)msg);
148 syslog(LOG_DEBUG, "sys_mbox_post out %d, 0x%08x\n", *mbox, msg);
149 return;
150}
151
152err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
153{
154 err_t ret = ERR_VAL;
155
156 syslog(LOG_DEBUG, "sys_mbox_trypost in %d, 0x%08x\n", *mbox, msg);
157 if (E_OK == psnd_dtq(*mbox, (intptr_t)msg)) {
158 ret = ERR_OK;
159 }
160 syslog(LOG_DEBUG, "sys_mbox_trypost out %d, 0x%08x %d\n", *mbox, msg, ret);
161 return ret;
162}
163
164u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
165{
166 SYSTIM systim0, systim1;
167 ER ercd;
168
169 get_tim(&systim0);
170 syslog(LOG_DEBUG, "sys_arch_mbox_fetch in %d\n", *mbox);
171 if (timeout) {
172 ercd = trcv_dtq(*mbox, (intptr_t *)msg, timeout);
173 }
174 else {
175 ercd = rcv_dtq(*mbox, (intptr_t*)msg);
176 }
177
178 if (E_OK == ercd) {
179 get_tim(&systim1);
180 syslog(LOG_DEBUG, "sys_arch_mbox_fetch out %d, E_OK\n", *mbox);
181 return (u32_t)(systim1 - systim0);
182 }
183 syslog(LOG_DEBUG, "sys_arch_mbox_fetch out %d, SYS_ARCH_TIMEOUT\n", *mbox);
184 return SYS_ARCH_TIMEOUT;
185}
186
187u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
188{
189 syslog(LOG_DEBUG, "sys_arch_mbox_tryfetch in %d\n", *mbox);
190 if (E_OK == prcv_dtq(*mbox, (intptr_t *)msg)) {
191 syslog(LOG_DEBUG, "sys_arch_mbox_tryfetch out %d, 0x%08x, ERR_OK\n", *mbox, *msg);
192 return (u32_t)ERR_OK;
193 }
194 syslog(LOG_DEBUG, "sys_arch_mbox_tryfetch out %d, SYS_MBOX_EMPTY\n", *mbox);
195 return SYS_MBOX_EMPTY;
196}
197
198sys_thread_t sys_thread_new(const char *name, void(*thread)(void *), void *arg, int stacksize, int prio)
199{
200 syslog(LOG_DEBUG, "sys_thread_new in\n");
201
202 T_CTSK ctsk = { TA_ACT, (intptr_t)arg, (TASK)thread, (PRI)prio, (SIZE)stacksize, (STK_T *)NULL };
203 ID id = acre_tsk(&ctsk);
204
205 syslog(LOG_DEBUG, "sys_thread_new %s, %d, %d, %d\n", name, stacksize, prio, id);
206 return (sys_thread_t)id;
207}
208
209sys_prot_t sys_arch_protect(void)
210{
211 bool_t state = sns_loc();
212 if (!state) {
213 loc_cpu();
214 }
215 return (sys_prot_t)(~state);
216}
217
218void sys_arch_unprotect(sys_prot_t pval)
219{
220 if (pval) {
221 unl_cpu();
222 }
223 return;
224}
225
226u32_t sys_now(void)
227{
228 SYSTIM systim;
229
230 get_tim(&systim);
231 return (u32_t)systim;
232}
233
234void *sys_malloc(size_t size)
235{
236 ER ret;
237 void *p;
238
239 ret = wai_sem(_echonet_ecn_mem_semid);
240
241 p = tlsf_malloc(lwip_tlsf, size);
242
243 if (ret == E_OK)
244 sig_sem(_echonet_ecn_mem_semid);
245
246 if (p == NULL) {
247 syslog(LOG_ERROR, "tlsf_malloc %d", size);
248 }
249
250 return p;
251}
252
253void *sys_calloc(size_t count, size_t size)
254{
255 ER ret;
256 void *p;
257
258 ret = wai_sem(_echonet_ecn_mem_semid);
259
260 p = tlsf_malloc(lwip_tlsf, count * size);
261
262 if (ret == E_OK)
263 sig_sem(_echonet_ecn_mem_semid);
264
265 if (p != NULL) {
266 memset(p, 0, count * size);
267 }
268 else {
269 syslog(LOG_ERROR, "tlsf_malloc %d", size);
270 }
271
272 return p;
273}
274
275void *sys_realloc(void *ptr, size_t size)
276{
277 ER ret;
278 void *p;
279
280 ret = wai_sem(_echonet_ecn_mem_semid);
281
282 p = tlsf_realloc(lwip_tlsf, ptr, size);
283
284 if (ret == E_OK)
285 sig_sem(_echonet_ecn_mem_semid);
286
287 if (p == NULL) {
288 syslog(LOG_ERROR, "tlsf_realloc %d", size);
289 }
290
291 return p;
292}
293
294void sys_free(void *mem)
295{
296 ER ret;
297
298 ret = wai_sem(_echonet_ecn_mem_semid);
299
300 tlsf_free(lwip_tlsf, mem);
301
302 if (ret == E_OK)
303 sig_sem(_echonet_ecn_mem_semid);
304}
305
306void *sys_trim(void *mem, size_t size)
307{
308 return mem;
309}
310
311void sys_sleep(uint32_t ms)
312{
313 dly_tsk(ms);
314}
315
316void sys_sprintf(void *dst, const char *fmt, ...)
317{
318 va_list arp;
319 unsigned int r, i, j, w, f;
320 unsigned long v;
321 char s[16], c, d, *p;
322 char *pos = (char *)dst;
323
324 va_start(arp, fmt);
325
326 for (;;) {
327 c = *fmt++; /* Get a char */
328 if (!c) break; /* End of format? */
329 if (c != '%') { /* Pass through it if not a % sequense */
330 *pos++ = c; continue;
331 }
332 f = 0;
333 c = *fmt++; /* Get first char of the sequense */
334 if (c == '0') { /* Flag: '0' padded */
335 f = 1; c = *fmt++;
336 }
337 else {
338 if (c == '-') { /* Flag: left justified */
339 f = 2; c = *fmt++;
340 }
341 }
342 for (w = 0; c >= '0' && c <= '9'; c = *fmt++) /* Minimum width */
343 w = w * 10 + c - '0';
344 if (c == 'l' || c == 'L') { /* Prefix: Size is long int */
345 f |= 4; c = *fmt++;
346 }
347 if (!c) break; /* End of format? */
348 d = c;
349 if (d >= 'a') d -= 0x20;
350 switch (d) { /* Type is... */
351 case 'S': /* String */
352 p = va_arg(arp, char*);
353 for (j = 0; p[j]; j++);
354 while (!(f & 2) && j++ < w) *pos++ = ' ';
355 while (*p != '\0') *pos++ = *p++;
356 while (j++ < w) *pos++ = ' ';
357 continue;
358 case 'C': /* Character */
359 *pos++ = (char)va_arg(arp, int); continue;
360 case 'B': /* Binary */
361 r = 2; break;
362 case 'O': /* Octal */
363 r = 8; break;
364 case 'D': /* Signed decimal */
365 case 'U': /* Unsigned decimal */
366 r = 10; break;
367 case 'X': /* Hexdecimal */
368 r = 16; break;
369 default: /* Unknown type (passthrough) */
370 *pos++ = c; continue;
371 }
372
373 /* Get an argument and put it in numeral */
374 v = (f & 4) ? va_arg(arp, long) : ((d == 'D') ? (long)va_arg(arp, int) : (long)va_arg(arp, unsigned int));
375 if (d == 'D' && (v & 0x80000000)) {
376 v = 0 - v;
377 f |= 8;
378 }
379 i = 0;
380 do {
381 d = (char)(v % r); v /= r;
382 if (d > 9) d += (c == 'x') ? 0x27 : 0x07;
383 s[i++] = d + '0';
384 } while (v && i < sizeof(s));
385 if (f & 8) s[i++] = '-';
386 j = i; d = (f & 1) ? '0' : ' ';
387 while (!(f & 2) && j++ < w) *pos++ = d;
388 do *pos++ = s[--i]; while (i);
389 while (j++ < w) *pos++ = ' ';
390 }
391
392 va_end(arp);
393}
394
395FIL fil[3];
396#define fil_count (sizeof(fil) / sizeof(fil[0]))
397
398FIL *sys_fopen(const char *file, const char *mode)
399{
400 FIL *result = NULL;
401 int i;
402
403 for (i = 0; i < fil_count; i++) {
404 if (fil[i].fs == NULL) {
405 result = &fil[i];
406 break;
407 }
408 }
409
410 if (result == NULL)
411 return NULL;
412
413 if (f_open(result, file, mode[0] == 'r' ? FA_READ : FA_WRITE) != FR_OK) {
414 return NULL;
415 }
416
417 return result;
418}
419
420size_t sys_fread(void *data, size_t len, size_t size, FIL *fp)
421{
422 UINT result;
423
424 switch ((intptr_t)fp) {
425 case 1:
426 case 2:
427 case 3:
428 return 0;
429 default:
430 if (f_read(fp, data, len * size, &result) != FR_OK) {
431 return 0;
432 }
433 break;
434 }
435
436 return result;
437}
438
439size_t sys_fwrite(const void *data, size_t len, size_t size, FIL *fp)
440{
441 UINT result;
442
443 switch ((intptr_t)fp) {
444 case 1:
445 syslog(LOG_ERROR, data);
446 return len * size;
447 case 2:
448 syslog(LOG_NOTICE, data);
449 return len * size;
450 case 3:
451 return 0;
452 default:
453 if (f_write(fp, data, len * size, &result) != FR_OK) {
454 return 0;
455 }
456 break;
457 }
458
459 return result;
460}
461
462long sys_fseek(FIL *fp, long ofs, long mode)
463{
464 FRESULT res;
465
466 switch (mode) {
467 case SEEK_SET:
468 res = f_lseek((fp), ofs);
469 break;
470 case SEEK_CUR:
471 res = f_lseek((fp), (fp)->fptr + ofs);
472 break;
473 case SEEK_END:
474 res = f_lseek((fp), (fp)->fsize - ofs);
475 break;
476 default:
477 return -1;
478 }
479
480 return (res == FR_OK) ? 0 : -1;
481}
482
483long sys_fclose(FIL *fp)
484{
485 if (f_close(fp) != FR_OK)
486 return EOF;
487 return 0;
488}
489
490char *sys_fgets(char *buf, long len, FIL *fp)
491{
492 return f_gets(buf, len, fp);
493}
494
495long sys_fputc(long data, FIL *fp)
496{
497 switch ((intptr_t)fp) {
498 case 1:
499 syslog(LOG_ERROR, "%c", data);
500 return 0;
501 case 2:
502 syslog(LOG_NOTICE, "%c", data);
503 return 0;
504 case 3:
505 return -1;
506 default:
507 return f_putc(data, fp);
508 }
509}
510
511long sys_fputs(const char *data, FIL *fp)
512{
513 switch ((intptr_t)fp) {
514 case 1:
515 syslog(LOG_ERROR, data);
516 return 0;
517 case 2:
518 syslog(LOG_NOTICE, data);
519 return 0;
520 case 3:
521 return -1;
522 default:
523 return f_puts(data, fp);
524 }
525}
526
527long sys_fflush(FIL *fp)
528{
529 return 0;
530}
531
532long sys_lseek(FIL *fp, long offset, int origin)
533{
534 if (f_lseek(fp, offset) != FR_OK) {
535 return -1;
536 }
537
538 return 0;
539}
540
541long sys_ftell(FIL *fp)
542{
543 return f_tell(fp);
544}
545
546long sys_ferror(FIL *fp)
547{
548 return fp->err;
549}
550
551long sys_rewind(FIL *fp)
552{
553 if (f_lseek(fp, 0) != FR_OK) {
554 return -1;
555 }
556
557 return 0;
558}
559
560void sys_exit(int error)
561{
562 abort();
563}
Note: See TracBrowser for help on using the repository browser.