jdi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. * Copyright (c) 2018, Chips&Media
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #if defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WIN32) || defined(__MINGW32__)
  26. #elif defined(linux) || defined(__linux) || defined(ANDROID)
  27. #else
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "../jdi.h"
  31. #include "jpulog.h"
  32. #define JPU_BIT_REG_SIZE 0x300
  33. #define JPU_BIT_REG_BASE (0x10000000 + 0x3000)
  34. #define JDI_DRAM_PHYSICAL_BASE 0x00
  35. #define JDI_DRAM_PHYSICAL_SIZE (128*1024*1024)
  36. #define JDI_SYSTEM_ENDIAN JDI_LITTLE_ENDIAN
  37. typedef struct jpu_buffer_t jpudrv_buffer_t;
  38. typedef struct jpu_buffer_pool_t
  39. {
  40. jpudrv_buffer_t jdb;
  41. int inuse;
  42. } jpu_buffer_pool_t;
  43. static int s_jpu_fd;
  44. static jpu_instance_pool_t *s_pjip;
  45. static jpu_instance_pool_t s_jip;
  46. static int s_task_num;
  47. static int s_clock_state;
  48. static jpudrv_buffer_t s_jdb_video_memory;
  49. static jpudrv_buffer_t s_jdb_register;
  50. static jpu_buffer_pool_t s_jpu_buffer_pool[MAX_JPU_BUFFER_POOL];
  51. static int s_jpu_buffer_pool_count;
  52. static Int32 swap_endian(BYTE* data, Uint32 len, Uint32 endian);
  53. int jdi_probe()
  54. {
  55. int ret;
  56. ret = jdi_init();
  57. jdi_release();
  58. return ret;
  59. }
  60. /* @return number of task
  61. */
  62. int jdi_get_task_num()
  63. {
  64. if (s_jpu_fd == -1 || s_jpu_fd == 0x00) {
  65. return 0;
  66. }
  67. return s_task_num;
  68. }
  69. int jdi_init()
  70. {
  71. int ret;
  72. if (s_jpu_fd != -1 && s_jpu_fd != 0x00)
  73. {
  74. s_task_num++;
  75. return 0;
  76. }
  77. s_jpu_fd = 1;
  78. memset((void *)&s_jpu_buffer_pool, 0x00, sizeof(jpu_buffer_pool_t)*MAX_JPU_BUFFER_POOL);
  79. s_jpu_buffer_pool_count = 0;
  80. if (!(s_pjip = jdi_get_instance_pool()))
  81. {
  82. JLOG(ERR, "[jdi] fail to create instance pool for saving context \n");
  83. goto ERR_JDI_INIT;
  84. }
  85. s_jdb_video_memory.phys_addr = JDI_DRAM_PHYSICAL_BASE;
  86. s_jdb_video_memory.size = JDI_DRAM_PHYSICAL_SIZE;
  87. if (!s_pjip->instance_pool_inited)
  88. {
  89. memset(&s_pjip->vmem, 0x00, sizeof(jpeg_mm_t));
  90. ret = jmem_init(&s_pjip->vmem, (unsigned long)s_jdb_video_memory.phys_addr, s_jdb_video_memory.size);
  91. if (ret < 0)
  92. {
  93. JLOG(ERR, "[JDI] fail to init jpu memory management logic\n");
  94. goto ERR_JDI_INIT;
  95. }
  96. }
  97. s_jdb_register.phys_addr = JPU_BIT_REG_BASE;
  98. s_jdb_register.virt_addr = JPU_BIT_REG_BASE;
  99. s_jdb_register.size = JPU_BIT_REG_SIZE;
  100. jdi_set_clock_gate(1);
  101. s_task_num++;
  102. JLOG(INFO, "[jdi] success to init driver \n");
  103. return s_jpu_fd;
  104. ERR_JDI_INIT:
  105. jdi_release();
  106. return -1;
  107. }
  108. int jdi_release()
  109. {
  110. if (s_jpu_fd == -1 || s_jpu_fd == 0x00)
  111. return 0;
  112. if (jdi_lock() < 0)
  113. {
  114. JLOG(ERR, "[jdi] fail to handle lock function\n");
  115. return -1;
  116. }
  117. if (s_task_num > 1) // means that the opened instance remains
  118. {
  119. s_task_num--;
  120. jdi_unlock();
  121. return 0;
  122. }
  123. s_task_num--;
  124. memset(&s_jdb_register, 0x00, sizeof(jpudrv_buffer_t));
  125. if (s_jpu_fd != -1 && s_jpu_fd != 0x00)
  126. s_jpu_fd = -1;
  127. s_pjip = NULL;
  128. jdi_unlock();
  129. return 0;
  130. }
  131. jpu_instance_pool_t *jdi_get_instance_pool()
  132. {
  133. if (!s_pjip)
  134. {
  135. s_pjip = &s_jip;
  136. memset(s_pjip, 0x00, sizeof(jpu_instance_pool_t));
  137. }
  138. return (jpu_instance_pool_t *)s_pjip;
  139. }
  140. int jdi_open_instance(unsigned long instIdx)
  141. {
  142. if(!s_pjip || s_jpu_fd == -1 || s_jpu_fd == 0x00)
  143. return -1;
  144. s_pjip->jpu_instance_num++;
  145. return 0;
  146. }
  147. int jdi_close_instance(unsigned long instIdx)
  148. {
  149. if(!s_pjip || s_jpu_fd == -1 || s_jpu_fd == 0x00)
  150. return -1;
  151. s_pjip->jpu_instance_num--;
  152. return 0;
  153. }
  154. int jdi_get_instance_num()
  155. {
  156. if(!s_pjip || s_jpu_fd == -1 || s_jpu_fd == 0x00)
  157. return -1;
  158. return s_pjip->jpu_instance_num;
  159. }
  160. int jdi_hw_reset()
  161. {
  162. if(!s_pjip || s_jpu_fd == -1 || s_jpu_fd == 0x00)
  163. return -1;
  164. // to do any action for hw reset
  165. return 0;
  166. }
  167. int jdi_lock()
  168. {
  169. return 0;
  170. }
  171. void jdi_unlock()
  172. {
  173. }
  174. void jdi_write_register(unsigned long addr, unsigned int data)
  175. {
  176. unsigned long *reg_addr;
  177. if(!s_pjip || s_jpu_fd == -1 || s_jpu_fd == 0x00)
  178. return;
  179. reg_addr = (unsigned long *)(addr + (unsigned long)s_jdb_register.virt_addr);
  180. //*(volatile unsigned long *)reg_addr = data;
  181. *(volatile unsigned int *)reg_addr = data;
  182. }
  183. unsigned long jdi_read_register(unsigned long addr)
  184. {
  185. unsigned long *reg_addr;
  186. reg_addr = (unsigned long *)(addr + (unsigned long)s_jdb_register.virt_addr);
  187. //return *(volatile unsigned long *)reg_addr;
  188. return *(volatile unsigned int *)reg_addr;
  189. }
  190. int jdi_write_memory(unsigned long addr, unsigned char *data, int len, int endian)
  191. {
  192. jpudrv_buffer_t jdb = {0, };
  193. unsigned long offset;
  194. int i;
  195. if(!s_pjip || s_jpu_fd == -1 || s_jpu_fd == 0x00)
  196. return -1;
  197. for (i=0; i<MAX_JPU_BUFFER_POOL; i++)
  198. {
  199. if (s_jpu_buffer_pool[i].inuse == 1)
  200. {
  201. jdb = s_jpu_buffer_pool[i].jdb;
  202. if (addr >= jdb.phys_addr && addr < (jdb.phys_addr + jdb.size))
  203. break;
  204. }
  205. }
  206. if (!jdb.size) {
  207. JLOG(ERR, "address 0x%08x is not mapped address!!!\n", addr);
  208. return -1;
  209. }
  210. offset = addr - (unsigned long)jdb.phys_addr;
  211. swap_endian(data, len, endian);
  212. memcpy((void *)((unsigned long)jdb.virt_addr+offset), data, len);
  213. return len;
  214. }
  215. int jdi_read_memory(unsigned long addr, unsigned char *data, int len, int endian)
  216. {
  217. jpudrv_buffer_t jdb = {0};
  218. unsigned long offset;
  219. int i;
  220. if(!s_pjip || s_jpu_fd == -1 || s_jpu_fd == 0x00)
  221. return -1;
  222. for (i=0; i<MAX_JPU_BUFFER_POOL; i++)
  223. {
  224. if (s_jpu_buffer_pool[i].inuse == 1)
  225. {
  226. jdb = s_jpu_buffer_pool[i].jdb;
  227. if (addr >= jdb.phys_addr && addr < (jdb.phys_addr + jdb.size))
  228. break;
  229. }
  230. }
  231. if (!jdb.size)
  232. return -1;
  233. offset = addr - (unsigned long)jdb.phys_addr;
  234. memcpy(data, (const void *)((unsigned long)jdb.virt_addr+offset), len);
  235. swap_endian(data, (Uint32)len, (Uint32)endian);
  236. return len;
  237. }
  238. int jdi_allocate_dma_memory(jpu_buffer_t *vb)
  239. {
  240. int i;
  241. unsigned long offset;
  242. jpudrv_buffer_t jdb = {0, };
  243. if(!s_pjip || s_jpu_fd == -1 || s_jpu_fd == 0x00)
  244. return -1;
  245. jdb.size = vb->size;
  246. jdb.phys_addr = (unsigned long)jmem_alloc(&s_pjip->vmem, jdb.size, 0);
  247. if (jdb.phys_addr == (unsigned long)-1)
  248. return -1; // not enough memory
  249. offset = (unsigned long)(jdb.phys_addr - s_jdb_video_memory.phys_addr);
  250. jdb.base = (unsigned long )s_jdb_video_memory.base + offset;
  251. jdb.virt_addr = jdb.phys_addr;
  252. vb->phys_addr = (unsigned long)jdb.phys_addr;
  253. vb->base = (unsigned long)jdb.base;
  254. vb->virt_addr = (unsigned long)vb->phys_addr;
  255. for (i=0; i<MAX_JPU_BUFFER_POOL; i++)
  256. {
  257. if (s_jpu_buffer_pool[i].inuse == 0)
  258. {
  259. s_jpu_buffer_pool[i].jdb = jdb;
  260. s_jpu_buffer_pool_count++;
  261. s_jpu_buffer_pool[i].inuse = 1;
  262. break;
  263. }
  264. }
  265. return 0;
  266. }
  267. void jdi_free_dma_memory(jpu_buffer_t *vb)
  268. {
  269. int i;
  270. jpudrv_buffer_t jdb = {0, };
  271. if(!s_pjip || s_jpu_fd == -1 || s_jpu_fd == 0x00)
  272. return ;
  273. if (vb->size == 0)
  274. return ;
  275. for (i=0; i<MAX_JPU_BUFFER_POOL; i++)
  276. {
  277. if (s_jpu_buffer_pool[i].jdb.phys_addr == vb->phys_addr)
  278. {
  279. s_jpu_buffer_pool[i].inuse = 0;
  280. s_jpu_buffer_pool_count--;
  281. jdb = s_jpu_buffer_pool[i].jdb;
  282. break;
  283. }
  284. }
  285. if (!jdb.size)
  286. {
  287. JLOG(ERR, "[JDI] invalid buffer to free address = 0x%x\n", (int)jdb.virt_addr);
  288. return ;
  289. }
  290. jmem_free(&s_pjip->vmem, (unsigned long)jdb.phys_addr, 0);
  291. memset(vb, 0, sizeof(jpu_buffer_t));
  292. }
  293. int jdi_set_clock_gate(int enable)
  294. {
  295. s_clock_state = enable;
  296. return 0;
  297. }
  298. int jdi_get_clock_gate()
  299. {
  300. return s_clock_state;
  301. }
  302. int jdi_wait_inst_ctrl_busy(int timeout, unsigned int addr_flag_reg, unsigned int flag)
  303. {
  304. unsigned int data_flag_reg;
  305. JLOG(ERR, "<%s:%d> Need to implement counter\n", __FUNCTION__, __LINE__);
  306. while(1)
  307. {
  308. data_flag_reg = jdi_read_register(addr_flag_reg);
  309. if (((data_flag_reg >> 4)&0xf) == flag) {
  310. break;
  311. }
  312. //if (timeout > 0 && (cur - elapse) > timeout) {
  313. // JLOG(ERR, "[jDI] jdi_wait_inst_ctrl_busy timeout, 0x%x=0x%lx\n", addr_flag_reg, jdi_read_register(addr_flag_reg));
  314. // return -1;
  315. //}
  316. }
  317. return 0;
  318. }
  319. int jdi_wait_interrupt(int timeout, unsigned int addr_int_reason, unsigned long instIdx)
  320. {
  321. //int count = 0;
  322. while(1)
  323. {
  324. if (jdi_read_register(addr_int_reason))
  325. break;
  326. //Sleep(1); // 1ms sec
  327. //if (count++ > timeout)
  328. // return -1;
  329. }
  330. return 0;
  331. }
  332. void jdi_log(int cmd, int step, int inst)
  333. {
  334. int i;
  335. switch(cmd)
  336. {
  337. case JDI_LOG_CMD_PICRUN:
  338. if (step == 1) //
  339. JLOG(INFO, "\n**PIC_RUN start\n");
  340. else
  341. JLOG(INFO, "\n**PIC_RUN end \n");
  342. break;
  343. }
  344. for (i=0; i<=0x238; i=i+16)
  345. {
  346. JLOG(INFO, "0x%04xh: 0x%08x 0x%08x 0x%08x 0x%08x\n", i,
  347. jdi_read_register(i), jdi_read_register(i+4),
  348. jdi_read_register(i+8), jdi_read_register(i+0xc));
  349. }
  350. }
  351. static void SwapByte(Uint8* data, Uint32 len)
  352. {
  353. Uint8 temp;
  354. Uint32 i;
  355. for (i=0; i<len; i+=2) {
  356. temp = data[i];
  357. data[i] = data[i+1];
  358. data[i+1] = temp;
  359. }
  360. }
  361. static void SwapWord(Uint8* data, Uint32 len)
  362. {
  363. Uint16 temp;
  364. Uint16* ptr = (Uint16*)data;
  365. Int32 i, size = len/sizeof(Uint16);
  366. for (i=0; i<size; i+=2) {
  367. temp = ptr[i];
  368. ptr[i] = ptr[i+1];
  369. ptr[i+1] = temp;
  370. }
  371. }
  372. static void SwapDword(Uint8* data, Uint32 len)
  373. {
  374. Uint32 temp;
  375. Uint32* ptr = (Uint32*)data;
  376. Int32 i, size = len/sizeof(Uint32);
  377. for (i=0; i<size; i+=2) {
  378. temp = ptr[i];
  379. ptr[i] = ptr[i+1];
  380. ptr[i+1] = temp;
  381. }
  382. }
  383. static Int32 swap_endian(BYTE* data, Uint32 len, Uint32 endian)
  384. {
  385. Uint8 endianMask[8] = { // endianMask : [2] - 4byte unit swap
  386. 0x00, 0x07, 0x04, 0x03, // [1] - 2byte unit swap
  387. 0x06, 0x05, 0x02, 0x01 // [0] - 1byte unit swap
  388. };
  389. Uint8 targetEndian;
  390. Uint8 systemEndian;
  391. Uint8 changes;
  392. BOOL byteSwap=FALSE, wordSwap=FALSE, dwordSwap=FALSE;
  393. if (endian > 7) {
  394. JLOG(ERR, "Invalid endian mode: %d, expected value: 0~7\n", endian);
  395. return -1;
  396. }
  397. targetEndian = endianMask[endian];
  398. systemEndian = endianMask[JDI_SYSTEM_ENDIAN];
  399. changes = targetEndian ^ systemEndian;
  400. byteSwap = changes & 0x01 ? TRUE : FALSE;
  401. wordSwap = changes & 0x02 ? TRUE : FALSE;
  402. dwordSwap = changes & 0x04 ? TRUE : FALSE;
  403. if (byteSwap == TRUE) SwapByte(data, len);
  404. if (wordSwap == TRUE) SwapWord(data, len);
  405. if (dwordSwap == TRUE) SwapDword(data, len);
  406. return changes == 0 ? 0 : 1;
  407. }
  408. int kbhit(void)
  409. {
  410. return 0;
  411. }
  412. int getch(void)
  413. {
  414. return -1;
  415. }
  416. /*
  417. * newlib_stubs.c
  418. * the bellow code is just to build ref-code. customers will removed the bellow code because they need a library which is related to the system library such as newlibc
  419. */
  420. #include <errno.h>
  421. #include <sys/stat.h>
  422. #include <sys/times.h>
  423. #include <sys/unistd.h>
  424. #ifndef STDOUT_USART
  425. #define STDOUT_USART 0
  426. #endif
  427. #ifndef STDERR_USART
  428. #define STDERR_USART 0
  429. #endif
  430. #ifndef STDIN_USART
  431. #define STDIN_USART 0
  432. #endif
  433. #undef errno
  434. extern int errno;
  435. /*
  436. environ
  437. A pointer to a list of environment variables and their values.
  438. For a minimal environment, this empty list is adequate:
  439. */
  440. char *__env[1] = { 0 };
  441. char **environ = __env;
  442. int _write(int file, char *ptr, int len);
  443. void _exit(int status) {
  444. _write(1, "exit", 4);
  445. while (1) {
  446. ;
  447. }
  448. }
  449. int _open(int file) {
  450. return -1;
  451. }
  452. int _close(int file) {
  453. return -1;
  454. }
  455. /*
  456. execve
  457. Transfer control to a new process. Minimal implementation (for a system without processes):
  458. */
  459. int _execve(char *name, char **argv, char **env) {
  460. errno = ENOMEM;
  461. return -1;
  462. }
  463. /*
  464. fork
  465. Create a new process. Minimal implementation (for a system without processes):
  466. */
  467. int _fork() {
  468. errno = EAGAIN;
  469. return -1;
  470. }
  471. /*
  472. fstat
  473. Status of an open file. For consistency with other minimal implementations in these examples,
  474. all files are regarded as character special devices.
  475. The `sys/stat.h' header file required is distributed in the `include' subdirectory for this C library.
  476. */
  477. int _fstat(int file, struct stat *st) {
  478. st->st_mode = S_IFCHR;
  479. return 0;
  480. }
  481. /*
  482. getpid
  483. Process-ID; this is sometimes used to generate strings unlikely to conflict with other processes. Minimal implementation, for a system without processes:
  484. */
  485. int _getpid() {
  486. return 1;
  487. }
  488. /*
  489. isatty
  490. Query whether output stream is a terminal. For consistency with the other minimal implementations,
  491. */
  492. int _isatty(int file) {
  493. switch (file){
  494. case STDOUT_FILENO:
  495. case STDERR_FILENO:
  496. case STDIN_FILENO:
  497. return 1;
  498. default:
  499. //errno = ENOTTY;
  500. errno = EBADF;
  501. return 0;
  502. }
  503. }
  504. /*
  505. kill
  506. Send a signal. Minimal implementation:
  507. */
  508. int _kill(int pid, int sig) {
  509. errno = EINVAL;
  510. return (-1);
  511. }
  512. /*
  513. link
  514. Establish a new name for an existing file. Minimal implementation:
  515. */
  516. int _link(char *old, char *new) {
  517. errno = EMLINK;
  518. return -1;
  519. }
  520. /*
  521. lseek
  522. Set position in a file. Minimal implementation:
  523. */
  524. int _lseek(int file, int ptr, int dir) {
  525. return 0;
  526. }
  527. static char* __get_MSP(void)
  528. {
  529. JLOG(ERR, "<%s:%d> Need to implement it\n", __FUNCTION__, __LINE__);
  530. return NULL;
  531. }
  532. /*
  533. sbrk
  534. Increase program data space.
  535. Malloc and related functions depend on this
  536. */
  537. caddr_t _sbrk(int incr) {
  538. // extern char _ebss; // Defined by the linker
  539. char _ebss;
  540. static char *heap_end;
  541. char *prev_heap_end;
  542. JLOG(ERR, "<%s:%d> check _ebss to implement it\n", __FUNCTION__, __LINE__);
  543. if (heap_end == 0) {
  544. heap_end = &_ebss;
  545. }
  546. prev_heap_end = heap_end;
  547. char * stack = (char*) __get_MSP();
  548. if (heap_end + incr > stack)
  549. {
  550. _write (STDERR_FILENO, "Heap and stack collision\n", 25);
  551. errno = ENOMEM;
  552. return (caddr_t) -1;
  553. //abort ();
  554. }
  555. heap_end += incr;
  556. return (caddr_t) prev_heap_end;
  557. }
  558. /*
  559. read
  560. Read a character to a file. `libc' subroutines will use this system routine for input from all files, including stdin
  561. Returns -1 on error or blocks until the number of characters have been read.
  562. */
  563. int _read(int file, char *ptr, int len) {
  564. int n;
  565. int num = 0;
  566. switch (file) {
  567. case STDIN_FILENO:
  568. for (n = 0; n < len; n++) {
  569. char c=0;
  570. #if STDIN_USART == 1
  571. while ((USART1->SR & USART_FLAG_RXNE) == (uint16_t)RESET) {}
  572. c = (char)(USART1->DR & (uint16_t)0x01FF);
  573. #elif STDIN_USART == 2
  574. while ((USART2->SR & USART_FLAG_RXNE) == (uint16_t) RESET) {}
  575. c = (char) (USART2->DR & (uint16_t) 0x01FF);
  576. #elif STDIN_USART == 3
  577. while ((USART3->SR & USART_FLAG_RXNE) == (uint16_t)RESET) {}
  578. c = (char)(USART3->DR & (uint16_t)0x01FF);
  579. #endif
  580. *ptr++ = c;
  581. num++;
  582. }
  583. break;
  584. default:
  585. errno = EBADF;
  586. return -1;
  587. }
  588. return num;
  589. }
  590. /*
  591. stat
  592. Status of a file (by name). Minimal implementation:
  593. int _EXFUN(stat,( const char *__path, struct stat *__sbuf ));
  594. */
  595. int _stat(const char *filepath, struct stat *st) {
  596. st->st_mode = S_IFCHR;
  597. return 0;
  598. }
  599. /*
  600. times
  601. Timing information for current process. Minimal implementation:
  602. */
  603. clock_t _times(struct tms *buf) {
  604. return -1;
  605. }
  606. /*
  607. unlink
  608. Remove a file's directory entry. Minimal implementation:
  609. */
  610. int _unlink(char *name) {
  611. errno = ENOENT;
  612. return -1;
  613. }
  614. /*
  615. wait
  616. Wait for a child process. Minimal implementation:
  617. */
  618. int _wait(int *status) {
  619. errno = ECHILD;
  620. return -1;
  621. }
  622. /*
  623. write
  624. Write a character to a file. `libc' subroutines will use this system routine for output to all files, including stdout
  625. Returns -1 on error or number of bytes sent
  626. */
  627. int _write(int file, char *ptr, int len) {
  628. int n;
  629. switch (file) {
  630. case STDOUT_FILENO: /*stdout*/
  631. for (n = 0; n < len; n++) {
  632. #if STDOUT_USART == 1
  633. while ((USART1->SR & USART_FLAG_TC) == (uint16_t)RESET) {}
  634. USART1->DR = (*ptr++ & (uint16_t)0x01FF);
  635. #elif STDOUT_USART == 2
  636. while ((USART2->SR & USART_FLAG_TC) == (uint16_t) RESET) {
  637. }
  638. USART2->DR = (*ptr++ & (uint16_t) 0x01FF);
  639. #elif STDOUT_USART == 3
  640. while ((USART3->SR & USART_FLAG_TC) == (uint16_t)RESET) {}
  641. USART3->DR = (*ptr++ & (uint16_t)0x01FF);
  642. #endif
  643. }
  644. break;
  645. case STDERR_FILENO: /* stderr */
  646. for (n = 0; n < len; n++) {
  647. #if STDERR_USART == 1
  648. while ((USART1->SR & USART_FLAG_TC) == (uint16_t)RESET) {}
  649. USART1->DR = (*ptr++ & (uint16_t)0x01FF);
  650. #elif STDERR_USART == 2
  651. while ((USART2->SR & USART_FLAG_TC) == (uint16_t) RESET) {
  652. }
  653. USART2->DR = (*ptr++ & (uint16_t) 0x01FF);
  654. #elif STDERR_USART == 3
  655. while ((USART3->SR & USART_FLAG_TC) == (uint16_t)RESET) {}
  656. USART3->DR = (*ptr++ & (uint16_t)0x01FF);
  657. #endif
  658. }
  659. break;
  660. default:
  661. errno = EBADF;
  662. return -1;
  663. }
  664. return len;
  665. }
  666. int _gettimeofday( struct timeval *tv, void *tzvp )
  667. {
  668. Uint64 t = 0;//__your_system_time_function_here__(); // get uptime in nanoseconds
  669. tv->tv_sec = t / 1000000000; // convert to seconds
  670. tv->tv_usec = ( t % 1000000000 ) / 1000; // get remaining microseconds
  671. JLOG(WARN, "<%s:%d> NEED TO IMPLEMENT %s\n", __FUNCTION__, __LINE__, __FUNCTION__);
  672. return 0;
  673. }
  674. #endif