From e88b4f9040b63d70563bee8553596f3c317022a5 Mon Sep 17 00:00:00 2001 From: tcsullivan Date: Sat, 17 Nov 2018 14:49:42 -0500 Subject: [PATCH] fixed pdclib for arm, can make programs for stmos --- Makefile | 6 +- initrd/Makefile | 4 + initrd/test | Bin 0 -> 8060 bytes initrd/test.c | 8 ++ src/kernel/clock.c | 2 + src/kernel/svc.c | 2 + src/kernel/vfs.c | 29 +++- src/kernel/vfs.h | 1 + src/pdclib/Makefile | 6 +- .../platform/stmos/functions/os/syscalls.c} | 85 +++++++----- .../platform/stmos/functions/stdio/remove.c | 79 +++++------ .../platform/stmos/functions/stdio/tmpfile.c | 124 +++++++++--------- .../platform/stmos/functions/time/clock.c | 15 ++- .../platform/stmos/functions/time/time.c | 16 +-- .../stmos/functions/time/timespec_get.c | 15 +-- src/pdclib/platform/stmos/include/syscalls.h | 53 ++++++++ src/user/user.c | 26 ++-- 17 files changed, 289 insertions(+), 182 deletions(-) create mode 100644 initrd/Makefile create mode 100755 initrd/test create mode 100644 initrd/test.c rename src/{user/syscalls.h => pdclib/platform/stmos/functions/os/syscalls.c} (63%) create mode 100644 src/pdclib/platform/stmos/include/syscalls.h diff --git a/Makefile b/Makefile index 0d4d85c..d497420 100644 --- a/Makefile +++ b/Makefile @@ -42,9 +42,11 @@ all: @$(MAKE) -C src/user @echo " INITRD" @tools/rba initrd.img $$(find initrd/*) - @$(CROSS)$(OBJCOPY) -B arm -I binary -O elf32-littlearm initrd.img initrd.img.o + @$(CROSS)$(OBJCOPY) -B arm -I binary -O elf32-littlearm initrd.img \ + initrd.img.o @echo " LINK " $(OUT) - @$(CROSS)$(CC) $(CFLAGS) $(LFLAGS) -o $(OUT) $$(find src/ -name "*.o") initrd.img.o + @$(CROSS)$(CC) $(CFLAGS) $(LFLAGS) -o $(OUT) \ + $$(find src/fs src/kernel src/user -name "*.o") initrd.img.o clean: diff --git a/initrd/Makefile b/initrd/Makefile new file mode 100644 index 0000000..b2ca46f --- /dev/null +++ b/initrd/Makefile @@ -0,0 +1,4 @@ +all: + @arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -fsigned-char -Os -nostdinc \ + -I../src/pdclib/include -I../src/pdclib/platform/stmos/include \ + test.c ../src/pdclib/pdclib.a -s -o test -pie diff --git a/initrd/test b/initrd/test new file mode 100755 index 0000000000000000000000000000000000000000..8b9d8a40dbfee1286e3021a2df2403bf06aab0d2 GIT binary patch literal 8060 zcmd5>dwf*Ywf-h^5{5h^B!Gt`>Oc|<210l!k3l6NOfm@pYT^|EWhTkMnGljVNi<}z zri#i`+orxs0WB(8+FAuARt;&W(iSb%4A_cjHEnH0u{9`4Ak^G%&Dp~v+FS4McmKHi z_xsMbA8W6*_TFpnIcLu;^X5A(77HTDiY!R!vlw6+`Mp#=q7Xd|cEqsle2kLz`t3-VnoH8gtt%UAe4tG&y8wJYkd+*Mw>Jkn5JI=7~-)?4nWsPUqqZbjow9>2)>8kZY& zb*oI_GkVSuH$48T!Q#ss8a4K# zc%Nb$16%aOD^65ALb1fJwC`5DlW`*DWg|>pr+BmCJ&L;(zpYpsY04)m))l8Iwkyt1 zTr}F0pRah4;&R1{6)#ilR$QgnP+X(9Uhz7`_bJ|>c(dXyinl3#Lh*LRI~6~tc$ebc ziuWkqt9YN{ZpE)FKB)L@#XXAOQ+!zQM~ZtDA5+|?_>^L#nDeD6wkeKRoTzw&VqI~X zV!PrD#aW6cD9%+pP4NuHvlY)#T%fo}@qERL6qhSr$NdpSY!l{zwNID_*kNJDcmelG z6!$=pFfm`4hu${E-zdsGAxycxc69aavh8}vSzn0ucl&HTUld&5XB((Wuwc&T?q$0_ zvBaM$c)Jh1=$i9+`8j1K0k2$(12EwyhAG*INmv6Nl&j&?2PDnj_1CuT`Ni5 zp}C~JeW3HOZO&`&i;ZQpCGE*2$hLJ2FL}`U=x437Fy=1jFz4{1q~fF$^k()Yuoc}K z3gZ>?2p*#$0dXA5lN{%(r|cMyf#Yoh=w078_!mI@xNn>V4|4yv?!0q5p6YWy?{0D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`lGEvs;H&JG zDmfW%24w_yi2;=&{$0&7tLtwiZ|B|<$oOQgBt_)h%u9FCC->ljNE@q8=c}1NkC6E? zzl+Yau_`Pl{ca?N5h5q=*So($pU8<{oQklV`Jb%$e{96Iu=slyA^wRzdEeg5KpVk) zB#E4$jflv}|Ek+5C;xLqlIW8303mtNC-?G;Qy8abO?1TmCbo;d$eM~Ae=tucX%lQE fsH;BG5R(dA5-E^HMv}1DjHF!3?Ti%25xM^c`h^~` literal 0 HcmV?d00001 diff --git a/initrd/test.c b/initrd/test.c new file mode 100644 index 0000000..f673ce0 --- /dev/null +++ b/initrd/test.c @@ -0,0 +1,8 @@ +#include + +int main(void) +{ + printf("Hello, world!\n"); + return 0; +} + diff --git a/src/kernel/clock.c b/src/kernel/clock.c index 5b80203..950f4ca 100644 --- a/src/kernel/clock.c +++ b/src/kernel/clock.c @@ -35,6 +35,8 @@ void clock_svc(uint32_t *args) task_sleep(args[1]); else if (args[0] == 1) udelay(args[1]); + else if (args[0] == 2) + *((unsigned int *)args[1]) = ticks; } void clock_init(void) diff --git a/src/kernel/svc.c b/src/kernel/svc.c index 7d171c3..cf1fbe2 100644 --- a/src/kernel/svc.c +++ b/src/kernel/svc.c @@ -65,6 +65,7 @@ void SVC_Handler(void) { case 2: /* Clock-related calls * 0 - delay * 1 - udelay + * 2 - ticks */ clock_svc(args); break; @@ -74,6 +75,7 @@ void SVC_Handler(void) { * 1 - open * 2 - close * 3 - read + * 4 - write */ vfs_svc(args); break; diff --git a/src/kernel/vfs.c b/src/kernel/vfs.c index d6f9e14..da1d641 100644 --- a/src/kernel/vfs.c +++ b/src/kernel/vfs.c @@ -26,6 +26,9 @@ void vfs_svc(uint32_t *args) case 3: *((int *)args[4]) = vfs_read(args[1], args[2], (uint8_t *)args[3]); break; + case 4: + *((int *)args[4]) = vfs_write(args[1], args[2], (const uint8_t *)args[3]); + break; default: break; } @@ -141,8 +144,30 @@ uint32_t vfs_read(int fd, uint32_t count, uint8_t *buffer) VFS_EOF)) return 0; - uint32_t ret = vfs_volumes[vfs_files[fd].vol].funcs->read(vfs_files[fd].fsinfo, - count, buffer); + uint32_t ret = vfs_volumes[vfs_files[fd].vol].funcs->read( + vfs_files[fd].fsinfo, count, buffer); + + if (ret < count) + vfs_files[fd].flags |= VFS_EOF; + + return ret; +} + +uint32_t vfs_write(int fd, uint32_t count, const uint8_t *buffer) +{ + if (fd < 0 || fd > VFS_MAX_FILES || count == 0 || buffer == 0) + return 0; + if (vfs_volumes[vfs_files[fd].vol].funcs->write == 0) + return -1; + if (vfs_files[fd].pid != task_getpid()) + return 0; + // TODO append? + if ((!(vfs_files[fd].flags & VFS_FILE_WRITE)) || (vfs_files[fd].flags & + VFS_EOF)) + return 0; + + uint32_t ret = vfs_volumes[vfs_files[fd].vol].funcs->write( + vfs_files[fd].fsinfo, count, buffer); if (ret < count) vfs_files[fd].flags |= VFS_EOF; diff --git a/src/kernel/vfs.h b/src/kernel/vfs.h index c9c9179..29763a5 100644 --- a/src/kernel/vfs.h +++ b/src/kernel/vfs.h @@ -47,5 +47,6 @@ int vfs_mount(vfs_volume_funcs *funcs, uint32_t flags); int vfs_open(const char *path, uint32_t flags); int vfs_close(int fd); uint32_t vfs_read(int fd, uint32_t count, uint8_t *buffer); +uint32_t vfs_write(int fd, uint32_t count, const uint8_t *buffer); #endif // VFS_H_ diff --git a/src/pdclib/Makefile b/src/pdclib/Makefile index ebb0ad4..bef1e19 100644 --- a/src/pdclib/Makefile +++ b/src/pdclib/Makefile @@ -24,8 +24,10 @@ REGDEPFILES := $(patsubst %,$(BUILDDIR)/%.d,$(REGFILES)) # All files belonging to the source distribution ALLFILES := $(SRCFILES) $(HDRFILES) $(AUXFILES) +CC = arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -fsigned-char --specs=nosys.specs + WARNINGS := -Wall -Wextra -pedantic -Wno-unused-parameter -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -Wno-long-long -Wuninitialized -Wstrict-prototypes -Wdeclaration-after-statement -CFLAGS := -fno-builtin -g -std=c99 -I./testing -I../user -I./platform/stmos/include $(WARNINGS) $(USERFLAGS) +CFLAGS := -fno-builtin -g -std=c99 -I./testing -I./platform/stmos/include $(WARNINGS) $(USERFLAGS) .PHONY: all clean srcdist tests testdrivers regtests regtestdrivers todos fixmes help @@ -58,7 +60,7 @@ all: $(BUILDDIR)/pdclib.a #testdrivers regtestdrivers $(BUILDDIR)/pdclib.a: $(OBJFILES) @echo " AR $@" - @ar rc $(BUILDDIR)/pdclib.a $? + @arm-none-eabi-ar rc $(BUILDDIR)/pdclib.a $? @echo tests: testdrivers diff --git a/src/user/syscalls.h b/src/pdclib/platform/stmos/functions/os/syscalls.c similarity index 63% rename from src/user/syscalls.h rename to src/pdclib/platform/stmos/functions/os/syscalls.c index c6a0c61..a58dc50 100644 --- a/src/user/syscalls.h +++ b/src/pdclib/platform/stmos/functions/os/syscalls.c @@ -1,12 +1,9 @@ -#ifndef SYSCALLS_H_ -#define SYSCALLS_H_ - -#include +#include "syscalls.h" // // Task-related calls -inline void _exit(int code) +void _exit(int code) { register uint32_t r1 __asm("r1") = code; __asm("\ @@ -16,7 +13,7 @@ inline void _exit(int code) " :: "r" (r1)); } -inline int fork(void) +int fork(void) { int ret = 0; register uint32_t r1 __asm("r1") = (uint32_t)&ret; @@ -28,7 +25,7 @@ inline int fork(void) return ret; } -inline int getpid(void) +int getpid(void) { int ret = 0; register uint32_t r1 __asm("r1") = (uint32_t)&ret; @@ -40,7 +37,7 @@ inline int getpid(void) return ret; } -inline void *sbrk(unsigned int bytes) +void *sbrk(unsigned int bytes) { void *ret = 0; register uint32_t r1 __asm("r1") = bytes; @@ -55,32 +52,34 @@ inline void *sbrk(unsigned int bytes) } // -// File-related calls - -// Indicates mounted volume -#define VFS_MOUNTED (1 << 0) -// Set if filesystem is read-only -#define VFS_READONLY (1 << 1) +// Clock-related calls -// Indicates an opened file -#define VFS_FILE_OPEN (1 << 0) -// Indicates read permission on file -#define VFS_FILE_READ (1 << 1) -// Indicates write permission on file -#define VFS_FILE_WRITE (1 << 2) -// Set if EOF has been reached -#define VFS_EOF (1 << 3) - -#define EOF (-1) +void delay(unsigned int ms) +{ + register uint32_t r1 __asm("r1") = ms; + __asm("\ + mov r0, 0; \ + mov r1, %0; \ + svc 2; \ + " :: "r" (r1)); +} -struct dirent { - char name[32]; -}; +unsigned int ticks(void) +{ + unsigned int ret = 0; + register uint32_t r1 __asm("r1") = (uint32_t)&ret; + __asm("\ + mov r0, 2; \ + mov r1, %0; \ + svc 2; \ + " :: "r" (r1)); + return ret; +} -struct vfs_volume_funcs_t; -typedef struct vfs_volume_funcs_t vfs_volume_funcs; +// +// File-related calls -inline int mount(vfs_volume_funcs *funcs, uint32_t flags) +int mount(vfs_volume_funcs *funcs, uint32_t flags) { int ret = 0; register uint32_t r1 __asm("r1") = (uint32_t)funcs; @@ -96,7 +95,7 @@ inline int mount(vfs_volume_funcs *funcs, uint32_t flags) return ret; } -inline int open(const char *path, uint32_t flags) +int open(const char *path, uint32_t flags) { int ret = 0; register uint32_t r1 __asm("r1") = (uint32_t)path; @@ -112,7 +111,7 @@ inline int open(const char *path, uint32_t flags) return ret; } -inline int close(int fd) +int close(int fd) { int ret = 0; register uint32_t r1 __asm("r1") = fd; @@ -123,9 +122,10 @@ inline int close(int fd) mov r2, %1; \ svc 3; \ " :: "r" (r1), "r" (r2)); + return ret; } -inline int read(int fd, uint32_t count, uint8_t *buffer) +int read(int fd, uint32_t count, uint8_t *buffer) { int ret = 0; register uint32_t r1 __asm("r1") = fd; @@ -143,4 +143,21 @@ inline int read(int fd, uint32_t count, uint8_t *buffer) return ret; } -#endif // SYSCALLS_H_ +int write(int fd, uint32_t count, const uint8_t *buffer) +{ + int ret = 0; + register uint32_t r1 __asm("r1") = fd; + register uint32_t r2 __asm("r2") = count; + register uint32_t r3 __asm("r3") = (uint32_t)buffer; + register uint32_t r4 __asm("r4") = (uint32_t)&ret; + __asm("\ + mov r0, 4; \ + mov r1, %0; \ + mov r2, %1; \ + mov r3, %2; \ + mov r4, %3; \ + svc 3; \ + " :: "r" (r1), "r" (r2), "r" (r3), "r" (r4)); + return ret; +} + diff --git a/src/pdclib/platform/stmos/functions/stdio/remove.c b/src/pdclib/platform/stmos/functions/stdio/remove.c index aca3eaf..c1ed6a0 100644 --- a/src/pdclib/platform/stmos/functions/stdio/remove.c +++ b/src/pdclib/platform/stmos/functions/stdio/remove.c @@ -7,57 +7,58 @@ /* This is an example implementation of remove() fit for use with POSIX kernels. */ -#include +#include #ifndef REGTEST -#include +//#include -#include "/usr/include/errno.h" +//#include "/usr/include/errno.h" extern struct _PDCLIB_file_t * _PDCLIB_filelist; -extern int unlink( const char * pathname ); +//extern int unlink( const char * pathname ); int remove( const char * pathname ) { - int rc; - struct _PDCLIB_file_t * current = _PDCLIB_filelist; - while ( current != NULL ) - { - if ( ( current->filename != NULL ) && ( strcmp( current->filename, pathname ) == 0 ) ) - { - return EOF; - } - current = current->next; - } - if ( ( rc = unlink( pathname ) ) == -1 ) - { - switch ( errno ) - { - /* See the comments on implementation-defined errno values in - <_PDCLIB_config.h>. - */ - case EACCES: - case EFAULT: - case EIO: - case EISDIR: - case ELOOP: - case ENAMETOOLONG: - case ENOENT: - case ENOMEM: - case ENOTDIR: - case EPERM: - case EROFS: - _PDCLIB_errno = _PDCLIB_ERROR; - break; - default: +// int rc; +// struct _PDCLIB_file_t * current = _PDCLIB_filelist; +// while ( current != NULL ) +// { +// if ( ( current->filename != NULL ) && ( strcmp( current->filename, pathname ) == 0 ) ) +// { +// return EOF; +// } +// current = current->next; +// } +// if ( ( rc = unlink( pathname ) ) == -1 ) +// { +// switch ( errno ) +// { +// /* See the comments on implementation-defined errno values in +// <_PDCLIB_config.h>. +// */ +// case EACCES: +// case EFAULT: +// case EIO: +// case EISDIR: +// case ELOOP: +// case ENAMETOOLONG: +// case ENOENT: +// case ENOMEM: +// case ENOTDIR: +// case EPERM: +// case EROFS: +// _PDCLIB_errno = _PDCLIB_ERROR; +// break; +// default: /* This should be something like EUNKNOWN. */ _PDCLIB_errno = _PDCLIB_ERROR; - break; - } - } - return rc; +// break; +// } +// } +// return rc; + return EOF; } #endif diff --git a/src/pdclib/platform/stmos/functions/stdio/tmpfile.c b/src/pdclib/platform/stmos/functions/stdio/tmpfile.c index 585a61d..f8a697b 100644 --- a/src/pdclib/platform/stmos/functions/stdio/tmpfile.c +++ b/src/pdclib/platform/stmos/functions/stdio/tmpfile.c @@ -4,20 +4,20 @@ Permission is granted to use, modify, and / or redistribute at will. */ -#include +//#include #ifndef REGTEST #include "pdclib/_PDCLIB_glue.h" -#include +//#include #include -#include - -#include -#include -#include -#include +//#include +// +//#include +//#include +//#include +//#include extern struct _PDCLIB_file_t * _PDCLIB_filelist; @@ -26,61 +26,61 @@ extern struct _PDCLIB_file_t * _PDCLIB_filelist; */ struct _PDCLIB_file_t * tmpfile( void ) { - FILE * rc; - /* This is the chosen way to get high-quality randomness. Replace as - appropriate. - */ - FILE * randomsource = fopen( "/proc/sys/kernel/random/uuid", "rb" ); - char filename[ L_tmpnam ]; - _PDCLIB_fd_t fd; - if ( randomsource == NULL ) - { - return NULL; - } - for ( ;; ) - { - /* Get a filename candidate. What constitutes a valid filename and - where temporary files are usually located is platform-dependent, - which is one reason why this function is located in the platform - overlay. The other reason is that a *good* implementation should - use high-quality randomness instead of a pseudo-random sequence to - generate the filename candidate, which is *also* platform-dependent. - */ - unsigned int random; - fscanf( randomsource, "%u", &random ); - sprintf( filename, "/tmp/%u.tmp", random ); - /* Check if file of this name exists. Note that fopen() is a very weak - check, which does not take e.g. access permissions into account - (file might exist but not readable). Replace with something more - appropriate. - */ - fd = open( filename, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR ); - if ( fd != -1 ) - { - break; - } - close( fd ); - } - fclose( randomsource ); - /* See fopen(). */ - if ( ( rc = calloc( 1, sizeof( struct _PDCLIB_file_t ) + _PDCLIB_UNGETCBUFSIZE + L_tmpnam + BUFSIZ ) ) == NULL ) - { - /* No memory to set up FILE structure */ - close( fd ); +// FILE * rc; +// /* This is the chosen way to get high-quality randomness. Replace as +// appropriate. +// */ +// FILE * randomsource = fopen( "/proc/sys/kernel/random/uuid", "rb" ); +// char filename[ L_tmpnam ]; +// _PDCLIB_fd_t fd; +// if ( randomsource == NULL ) +// { return NULL; - } - rc->status = _PDCLIB_filemode( "wb+" ) | _IOLBF | _PDCLIB_DELONCLOSE; - rc->handle = fd; - rc->ungetbuf = (unsigned char *)rc + sizeof( struct _PDCLIB_file_t ); - rc->filename = (char *)rc->ungetbuf + _PDCLIB_UNGETCBUFSIZE; - rc->buffer = rc->filename + L_tmpnam; - strcpy( rc->filename, filename ); - rc->bufsize = BUFSIZ; - rc->bufidx = 0; - rc->ungetidx = 0; - rc->next = _PDCLIB_filelist; - _PDCLIB_filelist = rc; - return rc; +// } +// for ( ;; ) +// { +// /* Get a filename candidate. What constitutes a valid filename and +// where temporary files are usually located is platform-dependent, +// which is one reason why this function is located in the platform +// overlay. The other reason is that a *good* implementation should +// use high-quality randomness instead of a pseudo-random sequence to +// generate the filename candidate, which is *also* platform-dependent. +// */ +// unsigned int random; +// fscanf( randomsource, "%u", &random ); +// sprintf( filename, "/tmp/%u.tmp", random ); +// /* Check if file of this name exists. Note that fopen() is a very weak +// check, which does not take e.g. access permissions into account +// (file might exist but not readable). Replace with something more +// appropriate. +// */ +// fd = open( filename, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR ); +// if ( fd != -1 ) +// { +// break; +// } +// close( fd ); +// } +// fclose( randomsource ); +// /* See fopen(). */ +// if ( ( rc = calloc( 1, sizeof( struct _PDCLIB_file_t ) + _PDCLIB_UNGETCBUFSIZE + L_tmpnam + BUFSIZ ) ) == NULL ) +// { +// /* No memory to set up FILE structure */ +// close( fd ); +// return NULL; +// } +// rc->status = _PDCLIB_filemode( "wb+" ) | _IOLBF | _PDCLIB_DELONCLOSE; +// rc->handle = fd; +// rc->ungetbuf = (unsigned char *)rc + sizeof( struct _PDCLIB_file_t ); +// rc->filename = (char *)rc->ungetbuf + _PDCLIB_UNGETCBUFSIZE; +// rc->buffer = rc->filename + L_tmpnam; +// strcpy( rc->filename, filename ); +// rc->bufsize = BUFSIZ; +// rc->bufidx = 0; +// rc->ungetidx = 0; +// rc->next = _PDCLIB_filelist; +// _PDCLIB_filelist = rc; +// return rc; } #endif diff --git a/src/pdclib/platform/stmos/functions/time/clock.c b/src/pdclib/platform/stmos/functions/time/clock.c index 825e040..b7880af 100644 --- a/src/pdclib/platform/stmos/functions/time/clock.c +++ b/src/pdclib/platform/stmos/functions/time/clock.c @@ -8,16 +8,17 @@ #ifndef REGTEST -#include +#include clock_t clock( void ) { - struct tms buf; - if ( times( &buf ) != (clock_t)-1 ) - { - return buf.tms_utime + buf.tms_stime; - } - return -1; + return ticks(); +// struct tms buf; +// if ( times( &buf ) != (clock_t)-1 ) +// { +// return buf.tms_utime + buf.tms_stime; +// } +// return -1; } #endif diff --git a/src/pdclib/platform/stmos/functions/time/time.c b/src/pdclib/platform/stmos/functions/time/time.c index cbb29e1..814c925 100644 --- a/src/pdclib/platform/stmos/functions/time/time.c +++ b/src/pdclib/platform/stmos/functions/time/time.c @@ -8,22 +8,16 @@ #ifndef REGTEST -#include +#include /* See comments in time.h on the semantics of time_t. */ time_t time( time_t * timer ) { - struct timeval tv; - if ( gettimeofday( &tv, NULL ) == 0 ) - { - if ( timer != NULL ) - { - *timer = tv.tv_sec; - } - return tv.tv_sec; - } - return -1; + if ( timer != NULL ) + *timer = ticks(); + + return ticks(); } #endif diff --git a/src/pdclib/platform/stmos/functions/time/timespec_get.c b/src/pdclib/platform/stmos/functions/time/timespec_get.c index d8cbab7..df17f1a 100644 --- a/src/pdclib/platform/stmos/functions/time/timespec_get.c +++ b/src/pdclib/platform/stmos/functions/time/timespec_get.c @@ -8,21 +8,18 @@ #ifndef REGTEST -#include +#include int timespec_get( struct timespec * ts, int base ) { if ( base == TIME_UTC ) { - /* We can make do with a really thin wrapper here. */ - struct timeval tv; - if ( gettimeofday( &tv, NULL ) == 0 ) - { - ts->tv_sec = tv.tv_sec; - ts->tv_nsec = tv.tv_usec * 1000; - return base; - } + unsigned int t = ticks(); + ts->tv_sec = t / 1000; + ts->tv_nsec = t * 1000000; + return base; } + /* Not supporting any other time base than TIME_UTC for now. */ return 0; } diff --git a/src/pdclib/platform/stmos/include/syscalls.h b/src/pdclib/platform/stmos/include/syscalls.h new file mode 100644 index 0000000..2acb59c --- /dev/null +++ b/src/pdclib/platform/stmos/include/syscalls.h @@ -0,0 +1,53 @@ +#ifndef SYSCALLS_H_ +#define SYSCALLS_H_ + +#include + +// +// Task-related calls + +void _exit(int code); + +int fork(void); +int getpid(void); +void *sbrk(unsigned int bytes); + +// +// Clock-related calls + +void delay(unsigned int ms); +unsigned int ticks(void); + +// +// File-related calls + +// Indicates mounted volume +#define VFS_MOUNTED (1 << 0) +// Set if filesystem is read-only +#define VFS_READONLY (1 << 1) + +// Indicates an opened file +#define VFS_FILE_OPEN (1 << 0) +// Indicates read permission on file +#define VFS_FILE_READ (1 << 1) +// Indicates write permission on file +#define VFS_FILE_WRITE (1 << 2) +// Set if EOF has been reached +#define VFS_EOF (1 << 3) + +#define EOF (-1) + +struct dirent { + char name[32]; +}; + +struct vfs_volume_funcs_t; +typedef struct vfs_volume_funcs_t vfs_volume_funcs; + +int mount(vfs_volume_funcs *funcs, uint32_t flags); +int open(const char *path, uint32_t flags); +int close(int fd); +int read(int fd, uint32_t count, uint8_t *buffer); +int write(int fd, uint32_t count, const uint8_t *buffer); + +#endif // SYSCALLS_H_ diff --git a/src/user/user.c b/src/user/user.c index 218ac97..dd1aa54 100644 --- a/src/user/user.c +++ b/src/user/user.c @@ -4,8 +4,6 @@ #include #include -#include "syscalls.h" - void user_delay(uint32_t ms) { register uint32_t r1 asm("r1") = ms; @@ -26,17 +24,17 @@ void user_main(void) int count = vfs_read(test, 20, (uint8_t *)buf); (void)count; - if (fork() == 0) { - while (1) { - gpio(GPIO_OUT, 5, 1); - user_delay(2000); - } - } else { - while (1) { - user_delay(1000); - gpio(GPIO_OUT, 5, 0); - user_delay(1000); - } - } +// if (fork() == 0) { +// while (1) { +// gpio(GPIO_OUT, 5, 1); +// user_delay(2000); +// } +// } else { +// while (1) { +// user_delay(1000); +// gpio(GPIO_OUT, 5, 0); +// user_delay(1000); +// } +// } }