Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 101 additions & 7 deletions source/iar/retarget_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@
* limitations under the License.
*/

#include <LowLevelIOInterface.h>
#include <stdio.h>

#include "RTE_Components.h"

#define RTE_CMSIS_Compiler_File_Interface
#ifdef RTE_CMSIS_Compiler_File_Interface
#include "retarget_fs.h"
#endif

#ifdef RTE_CMSIS_Compiler_STDERR
#include "retarget_stderr.h"
#endif
Expand All @@ -32,12 +38,12 @@
#include "retarget_stdout.h"
#endif

#if defined(RTE_CMSIS_Compiler_STDIN)
#if defined(RTE_CMSIS_Compiler_STDIN) || defined(RTE_CMSIS_Compiler_File_Interface)

size_t __read(int handle, unsigned char *buf, size_t bufSize)
{
#if defined(RTE_CMSIS_Compiler_STDIN)
if (handle == 0)
if (handle == _LLIO_STDIN)
{
size_t nChars = 0;
for (size_t i = bufSize; i > 0; --i)
Expand All @@ -53,12 +59,21 @@ size_t __read(int handle, unsigned char *buf, size_t bufSize)
}
#endif

return -1;
#if defined(RTE_CMSIS_Compiler_File_Interface)
int32_t sz = rt_fs_read(handle, buf, bufSize);
if (sz < 0) {
sz = -1;
}
return (sz);
#else
/* Not implemented */
return _LLIO_ERROR;
#endif
}

#endif

#if defined(RTE_CMSIS_Compiler_STDOUT) || defined(RTE_CMSIS_Compiler_STDERR)
#if defined(RTE_CMSIS_Compiler_STDOUT) || defined(RTE_CMSIS_Compiler_STDERR) || defined(RTE_CMSIS_Compiler_File_Interface)

size_t __write(int handle, const unsigned char *buf, size_t bufSize)
{
Expand All @@ -69,7 +84,7 @@ size_t __write(int handle, const unsigned char *buf, size_t bufSize)
}

#if defined(RTE_CMSIS_Compiler_STDOUT)
if (handle == 1 /* stdout */) {
if (handle == _LLIO_STDOUT) {
for (size_t i = bufSize; i > 0; --i)
{
stdout_putchar(*buf++);
Expand All @@ -79,7 +94,7 @@ size_t __write(int handle, const unsigned char *buf, size_t bufSize)
#endif

#if defined(RTE_CMSIS_Compiler_STDERR)
if (handle == 2 /* stderr */) {
if (handle == _LLIO_STDERR) {
for (size_t i = bufSize; i > 0; --i)
{
stderr_putchar(*buf);
Expand All @@ -88,7 +103,86 @@ size_t __write(int handle, const unsigned char *buf, size_t bufSize)
return bufSize;
}
#endif
return -1;


#if defined(RTE_CMSIS_Compiler_File_Interface)
int32_t sz = rt_fs_write(handle, buf, bufSize);
if (sz < 0) {
sz = -1;
}
return (sz);
#else
/* Not implemented */
return _LLIO_ERROR;
#endif
}

#endif


int __close(int fildes)
{
switch (fildes) {
case _LLIO_STDIN:
case _LLIO_STDOUT:
case _LLIO_STDERR:
return (0);
}

#if defined(RTE_CMSIS_Compiler_File_Interface)
int32_t rval;
rval = rt_fs_close(fildes);
if (rval < 0) {
rval = _LLIO_ERROR;
}
return (rval);
#else
/* Not implemented */
return _LLIO_ERROR;
#endif
}


int __open(const char *path, int oflag)
{
#if defined(RTE_CMSIS_Compiler_File_Interface)
int32_t rval;
int32_t mode;
mode = oflag & (_LLIO_RDONLY | _LLIO_WRONLY | _LLIO_RDWR | _LLIO_APPEND);
mode |= (oflag & (_LLIO_CREAT | _LLIO_TRUNC)) >> 1;
rval = rt_fs_open(path, mode);
if (rval < 0) {
rval = _LLIO_ERROR;
}
return (rval);
#else
/* Not implemented */
(void)path;
(void)oflag;
return _LLIO_ERROR;
#endif
}

long __lseek(int fildes, long offset, int whence)
{
#if defined(RTE_CMSIS_Compiler_File_Interface)
int64_t rval;
rval = rt_fs_seek(fildes, offset, whence);
if (rval < 0) {
rval = _LLIO_ERROR;
}
else {
if ((sizeof(long) != sizeof(int64_t)) && ((rval >> 32) != 0)) {
/* Returned file offset does not fit into off_t */
rval = _LLIO_ERROR;
}
}
return ((long)rval);
#else
/* Not implemented */
(void)offset;
(void)whence;
return _LLIO_ERROR;
#endif
}