ECE 486 Support Libraries
syscalls.c
Go to the documentation of this file.
1 /*!
2  * @file
3  *
4  * @brief Support files for GNU libc.
5  *
6  * @author stolen from Sheaff.
7  *
8  * @date Jan 2016
9  *
10  * Some standard syscall functions are redefined here in order to
11  * make printf() calls direct output to the USART (to be accessed
12  * by the host when the development board is connected through the
13  * USB connector)
14  *
15  * note the changes to _write()
16  */
17 
18 
19 #include <_ansi.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/fcntl.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <time.h>
26 #include <sys/time.h>
27 #include <sys/times.h>
28 #include <errno.h>
29 #include <reent.h>
30 #include <unistd.h>
31 #include <sys/wait.h>
32 #include <stdint.h>
33 #include "usart.h"
34 
35 #define FreeRTOS
36 #define MAX_STACK_SIZE 0x2000
37 
38 extern int __io_putchar(int ch) __attribute__((weak));
39 extern int __io_getchar(void) __attribute__((weak));
40 
41 #ifndef FreeRTOS
42  register char * stack_ptr asm("sp");
43 #endif
44 
45 
46 
47 
48 caddr_t _sbrk(int incr)
49 {
50  extern char end asm("end");
51  static char *heap_end;
52  char *prev_heap_end,*min_stack_ptr;
53 
54  if (heap_end == 0)
55  heap_end = &end;
56 
57  prev_heap_end = heap_end;
58 
59 #ifdef FreeRTOS
60  /* Use the NVIC offset register to locate the main stack pointer. */
61  min_stack_ptr = (char*)(*(unsigned int *)*(unsigned int *)0xE000ED08);
62  /* Locate the STACK bottom address */
63  min_stack_ptr -= MAX_STACK_SIZE;
64 
65  if (heap_end + incr > min_stack_ptr)
66 #else
67  if (heap_end + incr > stack_ptr)
68 #endif
69  {
70 // write(1, "Heap and stack collision\n", 25);
71 // abort();
72  errno = ENOMEM;
73  return (caddr_t) -1;
74  }
75 
76  heap_end += incr;
77 
78  return (caddr_t) prev_heap_end;
79 }
80 
81 /*
82  * _gettimeofday primitive (Stub function)
83  * */
84 int _gettimeofday (struct timeval * tp, struct timezone * tzp)
85 {
86  /* Return fixed data for the timezone. */
87  if (tzp)
88  {
89  tzp->tz_minuteswest = 0;
90  tzp->tz_dsttime = 0;
91  }
92 
93  return 0;
94 }
96 {
97 }
98 
99 int _getpid(void)
100 {
101  return 1;
102 }
103 
104 int _kill(int pid, int sig)
105 {
106  errno = EINVAL;
107  return -1;
108 }
109 
110 void _exit (int status)
111 {
112  _kill(status, -1);
113  while (1) {}
114 }
115 
116 int _write(int file, char *ptr, int len)
117 {
118  // Transmit printf string to USART - blocking call
119  HAL_UART_Transmit(&UsartHandle,(uint8_t *)ptr,len,10000);
120  return len;
121 }
122 
123 int _close(int file)
124 {
125  return -1;
126 }
127 
128 int _fstat(int file, struct stat *st)
129 {
130  st->st_mode = S_IFCHR;
131  return 0;
132 }
133 
134 int _isatty(int file)
135 {
136  return 1;
137 }
138 
139 int _lseek(int file, int ptr, int dir)
140 {
141  return 0;
142 }
143 
144 int _read(int file, char *ptr, int len)
145 {
146  int DataIdx;
147 
148  for (DataIdx = 0; DataIdx < len; DataIdx++)
149  {
150  *ptr++ = __io_getchar();
151  }
152 
153  return len;
154 }
155 
156 int _open(char *path, int flags, ...)
157 {
158  /* Pretend like we always fail */
159  return -1;
160 }
161 
162 int _wait(int *status)
163 {
164  errno = ECHILD;
165  return -1;
166 }
167 
168 int _unlink(char *name)
169 {
170  errno = ENOENT;
171  return -1;
172 }
173 
174 int _times(struct tms *buf)
175 {
176  return -1;
177 }
178 
179 int _stat(char *file, struct stat *st)
180 {
181  st->st_mode = S_IFCHR;
182  return 0;
183 }
184 
185 int _link(char *old, char *new)
186 {
187  errno = EMLINK;
188  return -1;
189 }
190 
191 int _fork(void)
192 {
193  errno = EAGAIN;
194  return -1;
195 }
196 
197 int _execve(char *name, char **argv, char **env)
198 {
199  errno = ENOMEM;
200  return -1;
201 }
int _read(int file, char *ptr, int len)
Definition: syscalls.c:144
int _unlink(char *name)
Definition: syscalls.c:168
int _open(char *path, int flags,...)
Definition: syscalls.c:156
int __io_getchar(void)
Definition: syscalls.c:39
int __io_putchar(int ch) __attribute__((weak))
#define MAX_STACK_SIZE
Definition: syscalls.c:36
int _link(char *old, char *new)
Definition: syscalls.c:185
void _exit(int status)
Definition: syscalls.c:110
int _gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: syscalls.c:84
void initialise_monitor_handles()
Definition: syscalls.c:95
int _stat(char *file, struct stat *st)
Definition: syscalls.c:179
int _getpid(void)
Definition: syscalls.c:99
int _times(struct tms *buf)
Definition: syscalls.c:174
int _isatty(int file)
Definition: syscalls.c:134
USART Initialization to stream printf() output back to the ST-LINK.
int _kill(int pid, int sig)
Definition: syscalls.c:104
int _fstat(int file, struct stat *st)
Definition: syscalls.c:128
int _execve(char *name, char **argv, char **env)
Definition: syscalls.c:197
UART_HandleTypeDef UsartHandle
Definition: usart.c:17
int _fork(void)
Definition: syscalls.c:191
int _close(int file)
Definition: syscalls.c:123
int _lseek(int file, int ptr, int dir)
Definition: syscalls.c:139
int _wait(int *status)
Definition: syscalls.c:162
int _write(int file, char *ptr, int len)
Definition: syscalls.c:116