RV32 SoC DS UG
High-Perf RV32 SoC DS UG
RV64 SoC DS UG API and Examples
Embedded IDE UG
Loading...
Searching...
No Matches
syscall.h
Go to the documentation of this file.
1#include <sys/stat.h>
2#include <sys/types.h>
3#include <errno.h>
4#include <sys/time.h>
5#include <unistd.h>
6#include <stdio.h>
7#include <stdint.h>
8#include <string.h>
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14// #if (ENABLE_SEMIHOSTING_PRINT == 1)
15// #include "semihosting.h"
16// #endif
17
18static void _putchar_nano(char character){
19 #if (ENABLE_SEMIHOSTING_PRINT == 1)
20 sh_writec(character);
21 #else
22 bsp_putChar(character);
23 #endif // (ENABLE_SEMIHOSTING_PRINT == 1)
24}
25 static char _getchar_nano(){
26 #if (ENABLE_SEMIHOSTING_PRINT == 1)
27 return sh_readc();
28 #else
29 return bsp_getChar();
30 #endif // (ENABLE_SEMIHOSTING_PRINT == 1)
31}
32
33/* Linker must provide these (define in linker.ld) */
34extern char _heap_start; /* start of heap area (set in linker script) */
35extern char _heap_end; /* optional end of heap area */
36
37static char *heap_ptr = &_heap_start;
38const char msg[] = "\033[31m\nERR: Heap Collision!\n\033[0m";
39
40/* _sbrk: provide heap for malloc */
41ssize_t _sbrk(int incr)
42{
43 char *prev_heap = heap_ptr;
44 if ((heap_ptr + incr) > &_heap_end) {
45 write(1,msg,sizeof(msg));
46 errno = ENOMEM;
47 // while(1); //Should we halt the program when hit this issue ?
48 return (ssize_t)-1;
49 }
50 heap_ptr += incr;
51 return (ssize_t)prev_heap;
52}
53
54
55/* _write: used by printf/vfprintf -> send to UART */
56ssize_t _write(int fd, const void *buf, size_t count)
57{
58 (void)fd; /* ignore fd; 1/2 -> stdout/stderr */
59 const char *p = buf;
60 for (size_t i = 0; i < count; ++i) {
61 char c = p[i];
62 if (c == '\n') {
63 _putchar_nano('\r');
64 //uart_write(&uart0, '\r');
65 }
66 _putchar_nano(c);
67 //uart_write(&uart0, c);
68 }
69 return (ssize_t)count;
70}
71
72ssize_t _read(int fd, char *buf, size_t count)
73{
74 (void)fd;
75 char *start = buf;
76 size_t i = 0;
77
78 for (; i < count; ) {
79
80 char c = _getchar_nano(); // Read raw data
81
82 // --- 1. Handle Enter (CR) ---
83 if (c == '\r' || c == '\n') { // Semihosting sometimes sends \n instead of \r!
84#if (ENABLE_SEMIHOSTING_PRINT == 0)
85 _putchar_nano('\r');
86 _putchar_nano('\n');
87#endif
88 *buf = '\n';
89 buf++;
90 return (ssize_t)(i + 1);
91 }
92
93 // --- 2. Handle Backspace (0x08 or 0x7F) ---
94 if (c == 0x08 || c == 0x7F) {
95 if (i > 0) {
96 buf--;
97 i--;
98
99#if (ENABLE_SEMIHOSTING_PRINT == 0)
100 // Only do the VT100 visual erase if on physical UART
101 _putchar_nano('\b');
102 _putchar_nano(' ');
103 _putchar_nano('\b');
104#endif
105 }
106 continue; // Skip the rest, get next char
107 }
108
109 // --- 3. Handle Normal Characters ---
110 if (c >= ' ' && c <= '~') {
111 *buf = c;
112#if (ENABLE_SEMIHOSTING_PRINT == 0)
113 _putchar_nano(c); // Only echo if on physical UART
114#endif
115 buf++;
116 i++;
117 }
118 }
119
120 return (ssize_t)i;
121}
122
123/* _close */
124int _close(int fd)
125{
126 (void)fd;
127 return -1;
128}
129
130/* _fstat: mark all fds as character special devices */
131int _fstat(int fd, struct stat *st)
132{
133 (void)fd;
134 st->st_mode = S_IFCHR;
135 return 0;
136}
137
138/* _isatty */
139int _isatty(int fd)
140{
141 (void)fd;
142 return 1; /* treat as TTY */
143}
144
145/* _lseek */
146off_t _lseek(int fd, off_t offset, int whence)
147{
148 (void)fd; (void)offset; (void)whence;
149 return (off_t)-1;
150}
151
152/* _exit - terminate program: loop forever */
153void _exit(int status)
154{
155 (void)status;
156 while (1) { __asm__ volatile ("wfi"); }
157}
158
159/* _kill - required by newlib sometimes */
160int _kill(int pid, int sig)
161{
162 (void)pid; (void)sig;
163 errno = EINVAL;
164 return -1;
165}
166
167/* _getpid */
168int _getpid(void)
169{
170 return 1;
171}
172
173#ifdef __cplusplus
174}
175#endif // C_plusplus
#define bsp_getChar()
Map standard character input to the physical UART.
Definition bsp.h:103
#define bsp_putChar(c)
Map standard character output to the physical UART.
Definition bsp.h:95
int _kill(int pid, int sig)
Definition syscall.h:160
int _isatty(int fd)
Definition syscall.h:139
const char msg[]
Definition syscall.h:38
int _close(int fd)
Definition syscall.h:124
int _fstat(int fd, struct stat *st)
Definition syscall.h:131
off_t _lseek(int fd, off_t offset, int whence)
Definition syscall.h:146
int _getpid(void)
Definition syscall.h:168
ssize_t _sbrk(int incr)
Definition syscall.h:41
char _heap_start
ssize_t _write(int fd, const void *buf, size_t count)
Definition syscall.h:56
ssize_t _read(int fd, char *buf, size_t count)
Definition syscall.h:72
void _exit(int status)
Definition syscall.h:153
char _heap_end