• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ******************************************************************************
3 File:     syscalls.c
4 Info:     Generated by Atollic TrueSTUDIO(R) 8.0.0   2017-10-17
5 
6 The MIT License (MIT)
7 Copyright (c) 2009-2017 Atollic AB
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in all
17 copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 SOFTWARE.
26 
27 ******************************************************************************
28 */
29 
30 /* Includes */
31 #include <stdint.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <signal.h>
38 #include <time.h>
39 #include <sys/time.h>
40 #include <sys/times.h>
41 #include "stm32l4xx_hal.h"
42 #include "StmUtil.h"
43 
44 extern RTC_HandleTypeDef hrtc;
45 extern UART_HandleTypeDef huart2;
46 
47 /* Variables */
48 #undef errno
49 extern int32_t errno;
50 
51 uint8_t *__env[1] = { 0 };
52 uint8_t **environ = __env;
53 
54 /* Functions */
initialise_monitor_handles()55 void initialise_monitor_handles()
56 {
57 }
58 
_getpid(void)59 int _getpid(void)
60 {
61 	errno = ENOSYS;
62 	return -1;
63 }
64 
_gettimeofday(struct timeval * ptimeval,void * ptimezone)65 int _gettimeofday(struct timeval  *ptimeval, void *ptimezone)
66 {
67     if(ptimezone)
68     {
69         struct timezone* tz = ptimezone;
70         tz->tz_minuteswest = _timezone / 60;
71         tz->tz_dsttime = _daylight;
72     }
73 
74     if(ptimeval)
75     {
76         RTC_TimeTypeDef time = {0};
77         RTC_DateTypeDef date = {0};
78 
79         if((HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN) != HAL_OK) ||
80            (HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN) != HAL_OK))
81         {
82             errno = ENOSYS;
83             return -1;
84         }
85 
86         struct tm local = {0};
87         local.tm_year = date.Year + 100;
88         local.tm_mon = date.Month - 1;
89         local.tm_mday = date.Date;
90         local.tm_wday = date.WeekDay - 1;
91         local.tm_hour = time.Hours;
92         local.tm_min = time.Minutes;
93         local.tm_sec = time.Seconds;
94         ptimeval->tv_sec = mktime(&local);
95         ptimeval->tv_usec = (time.SecondFraction * 1000 * 1000 / time.SubSeconds);
96     }
97 
98   return 0;
99 }
100 
_kill(int32_t pid,int32_t sig)101 int _kill(int32_t pid, int32_t sig)
102 {
103 	errno = ENOSYS;
104 	return -1;
105 }
106 
_exit(int32_t status)107 void _exit(int32_t status)
108 {
109 	while (1) {}		/* Make sure we hang here */
110 }
111 
_write(int32_t file,uint8_t * ptr,int32_t len)112 int _write(int32_t file, uint8_t *ptr, int32_t len)
113 {
114 #ifndef NDEBUG
115     if (file == 1) //STDOUT
116     {
117         HAL_UART_Transmit(&huart2, ptr, len, HAL_MAX_DELAY);
118     }
119     else if (file == 2) //STDERR
120     {
121         for(uint32_t n = 0; n < len; n++)
122         {
123             ITM_SendChar(ptr[n]);
124         }
125         return len;
126     }
127     else if ((file >= ITMFILENO) && (file < ITMFILENO + ITMCHANNELNO))
128     {
129         for(uint32_t n = 0; n < len; n++)
130         {
131             ITM_Out(file - ITMFILENO, ptr[n]);
132         }
133         return len;
134     }
135 #endif
136     errno = ENOSYS;
137     return -1;
138 }
139 
_sbrk(int32_t incr)140 void * _sbrk(int32_t incr)
141 {
142 	extern char   end; /* Set by linker.  */
143 	static char * heap_end;
144 	char *        prev_heap_end;
145 
146 	if (heap_end == 0) {
147 		heap_end = & end;
148 	}
149 
150 	prev_heap_end = heap_end;
151 	heap_end += incr;
152 
153 	return (void *) prev_heap_end;
154 }
155 
_close(int32_t file)156 int _close(int32_t file)
157 {
158     if ((file >= ITMFILENO) && (file < ITMFILENO + ITMCHANNELNO))
159     {
160         return 0;
161     }
162 	errno = ENOSYS;
163 	return -1;
164 }
165 
166 
_fstat(int32_t file,struct stat * st)167 int _fstat(int32_t file, struct stat *st)
168 {
169     if ((file >= ITMFILENO) && (file < ITMFILENO + ITMCHANNELNO))
170     {
171         st->st_mode = S_IFCHR;
172         st->st_size = 0;
173         return 0;
174     }
175 	errno = ENOSYS;
176 	return -1;
177 }
178 
_isatty(int32_t file)179 int _isatty(int32_t file)
180 {
181     if ((file >= ITMFILENO) && (file < ITMFILENO + ITMCHANNELNO))
182     {
183         return 1;
184     }
185 	errno = ENOSYS;
186 	return 0;
187 }
188 
_lseek(int32_t file,int32_t ptr,int32_t dir)189 int _lseek(int32_t file, int32_t ptr, int32_t dir)
190 {
191     if ((file >= ITMFILENO) && (file < ITMFILENO + ITMCHANNELNO))
192     {
193         return 0;
194     }
195 	errno = ENOSYS;
196 	return -1;
197 }
198 
_read(int32_t file,uint8_t * ptr,int32_t len)199 int _read(int32_t file, uint8_t *ptr, int32_t len)
200 {
201     errno = ENOSYS;
202     return -1;
203 }
204 
_readlink(const char * path,char * buf,size_t bufsize)205 int _readlink(const char *path, char *buf, size_t bufsize)
206 {
207   errno = ENOSYS;
208   return -1;
209 }
210 
_open(const uint8_t * path,int32_t flags,int32_t mode)211 int _open(const uint8_t *path, int32_t flags, int32_t mode)
212 {
213     unsigned int channel = 0;
214     if((strlen((char*)path) == 7 ) &&
215        !strncmp((char*)path, "ITM[", 4) &&
216        !strcmp((char*)&path[6], "]") &&
217        (sscanf((char*)&path[4],"%02u", &channel) == 1) &&
218        (channel < ITMCHANNELNO) &&
219        ((flags == 0x601) || (flags == 0x10601)))
220     {
221         return ITMFILENO + channel;
222     }
223 	errno = ENOSYS;
224 	return -1;
225 }
226 
_wait(int32_t * status)227 int _wait(int32_t *status)
228 {
229 	errno = ENOSYS;
230 	return -1;
231 }
232 
_unlink(const uint8_t * name)233 int _unlink(const uint8_t *name)
234 {
235 	errno = ENOSYS;
236 	return -1;
237 }
238 
_times(struct tms * buf)239 int _times(struct tms *buf)
240 {
241 	RTC_TimeTypeDef time = {0};
242 	RTC_DateTypeDef date = {0};
243 
244 	if((HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN) != HAL_OK) ||
245 	   (HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN) != HAL_OK))
246 	{
247 		errno = ENOSYS;
248 		return -1;
249 	}
250 
251 	struct tm local = {0};
252 	local.tm_year = date.Year + 100;
253 	local.tm_mon = date.Month - 1;
254 	local.tm_mday = date.Date;
255 	local.tm_wday = date.WeekDay - 1;
256 	local.tm_hour = time.Hours;
257 	local.tm_min = time.Minutes;
258 	local.tm_sec = time.Seconds;
259 
260 	buf->tms_utime = mktime(&local);        /* user time */
261 	buf->tms_stime = 0;	   	                /* system time */
262 	buf->tms_cutime = 0;	                /* user time, children */
263 	buf->tms_cstime = 0;		            /* system time, children */
264 	return 0;
265 }
266 
_stat(const uint8_t * file,struct stat * st)267 int _stat(const uint8_t *file, struct stat *st)
268 {
269 	errno = ENOSYS;
270 	return -1;
271 }
272 
_symlink(const char * path1,const char * path2)273 int _symlink(const char *path1, const char *path2)
274 {
275   errno = ENOSYS;
276   return -1;
277 }
278 
_link(const uint8_t * old,const uint8_t * new)279 int _link(const uint8_t *old, const uint8_t *new)
280 {
281 	errno = ENOSYS;
282 	return -1;
283 }
284 
_fork(void)285 int _fork(void)
286 {
287 	errno = ENOSYS;
288 	return -1;
289 }
290 
_execve(const uint8_t * name,uint8_t * const * argv,uint8_t * const * env)291 int _execve(const uint8_t *name, uint8_t * const *argv, uint8_t * const *env)
292 {
293 	errno = ENOSYS;
294 	return -1;
295 }
296 
297