2015-06-15 06:51:06 +00:00
|
|
|
/* newlib_syscalls.c - newlib syscalls for ESP8266
|
|
|
|
*
|
|
|
|
* Part of esp-open-rtos
|
|
|
|
* Copyright (C) 2105 Superhouse Automation Pty Ltd
|
|
|
|
* BSD Licensed as described in the file LICENSE
|
|
|
|
*/
|
|
|
|
#include <sys/reent.h>
|
|
|
|
#include <sys/types.h>
|
2015-09-03 01:14:44 +00:00
|
|
|
#include <sys/errno.h>
|
2015-06-15 06:51:06 +00:00
|
|
|
#include <espressif/sdk_private.h>
|
2015-07-29 00:34:09 +00:00
|
|
|
#include <common_macros.h>
|
2015-10-06 04:51:57 +00:00
|
|
|
#include <esp/uart.h>
|
2015-06-15 06:51:06 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2016-02-16 11:00:29 +00:00
|
|
|
extern void *xPortSupervisorStackPointer;
|
|
|
|
|
2015-07-29 00:34:09 +00:00
|
|
|
IRAM caddr_t _sbrk_r (struct _reent *r, int incr)
|
2015-06-15 06:51:06 +00:00
|
|
|
{
|
|
|
|
extern char _heap_start; /* linker script defined */
|
|
|
|
static char * heap_end;
|
|
|
|
char * prev_heap_end;
|
|
|
|
|
|
|
|
if (heap_end == NULL)
|
|
|
|
heap_end = &_heap_start;
|
|
|
|
prev_heap_end = heap_end;
|
2016-02-16 11:00:29 +00:00
|
|
|
|
|
|
|
intptr_t sp = (intptr_t)xPortSupervisorStackPointer;
|
|
|
|
if(sp == 0) /* scheduler not started */
|
|
|
|
__asm__ __volatile__ ("mov %0, a1\n" : "=a"(sp));
|
|
|
|
|
|
|
|
if ((intptr_t)heap_end + incr >= sp)
|
|
|
|
{
|
|
|
|
r->_errno = ENOMEM;
|
|
|
|
return (caddr_t)-1;
|
|
|
|
}
|
2015-06-15 06:51:06 +00:00
|
|
|
heap_end += incr;
|
|
|
|
|
|
|
|
return (caddr_t) prev_heap_end;
|
|
|
|
}
|
|
|
|
|
2015-10-06 04:51:57 +00:00
|
|
|
/* syscall implementation for stdio write to UART */
|
2015-06-15 06:51:06 +00:00
|
|
|
long _write_r(struct _reent *r, int fd, const char *ptr, int len )
|
|
|
|
{
|
2015-09-03 01:14:44 +00:00
|
|
|
if(fd != r->_stdout->_file) {
|
|
|
|
r->_errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2015-10-06 04:51:57 +00:00
|
|
|
for(int i = 0; i < len; i++) {
|
2015-10-06 12:24:40 +00:00
|
|
|
/* Auto convert CR to CRLF, ignore other LFs (compatible with Espressif SDK behaviour) */
|
|
|
|
if(ptr[i] == '\r')
|
|
|
|
continue;
|
|
|
|
if(ptr[i] == '\n')
|
|
|
|
uart_putc(0, '\r');
|
2015-10-06 04:51:57 +00:00
|
|
|
uart_putc(0, ptr[i]);
|
|
|
|
}
|
2015-06-15 06:51:06 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2015-10-06 04:51:57 +00:00
|
|
|
/* syscall implementation for stdio read from UART */
|
2015-08-07 20:41:13 +00:00
|
|
|
__attribute__((weak)) long _read_r( struct _reent *r, int fd, char *ptr, int len )
|
2015-06-15 06:51:06 +00:00
|
|
|
{
|
2015-10-06 04:51:57 +00:00
|
|
|
int ch, i;
|
|
|
|
|
2015-09-03 01:14:44 +00:00
|
|
|
if(fd != r->_stdin->_file) {
|
|
|
|
r->_errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2015-10-06 04:51:57 +00:00
|
|
|
uart_rxfifo_wait(0, 1);
|
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
ch = uart_getc_nowait(0);
|
|
|
|
if (ch < 0) break;
|
2015-09-03 01:14:44 +00:00
|
|
|
ptr[i] = ch;
|
|
|
|
}
|
2015-10-06 04:51:57 +00:00
|
|
|
return i;
|
2015-06-15 06:51:06 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 01:15:05 +00:00
|
|
|
/* Stub syscall implementations follow, to allow compiling newlib functions that
|
|
|
|
pull these in via various codepaths
|
|
|
|
*/
|
|
|
|
__attribute__((alias("syscall_returns_enosys"))) int _open_r(struct _reent *r, const char *pathname, int flags, int mode);
|
|
|
|
__attribute__((alias("syscall_returns_enosys"))) int _fstat_r(struct _reent *r, int fd, void *buf);
|
|
|
|
__attribute__((alias("syscall_returns_enosys"))) int _close_r(struct _reent *r, int fd);
|
|
|
|
__attribute__((alias("syscall_returns_enosys"))) off_t _lseek_r(struct _reent *r, int fd, off_t offset, int whence);
|
2015-06-15 06:51:06 +00:00
|
|
|
|
2015-09-03 01:15:05 +00:00
|
|
|
/* Generic stub for any newlib syscall that fails with errno ENOSYS
|
|
|
|
("Function not implemented") and a return value equivalent to
|
|
|
|
(int)-1. */
|
|
|
|
static int syscall_returns_enosys(struct _reent *r)
|
2015-06-15 06:51:06 +00:00
|
|
|
{
|
2015-09-03 01:15:05 +00:00
|
|
|
r->_errno=ENOSYS;
|
2015-06-15 06:51:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|