jdi.c 17 KB

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