From 7ab6ec98138c21e8b95375c148b8ed3a8c676860 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 23 Oct 2012 01:06:49 -0400 Subject: [PATCH] adding newlib0 example --- newlib0/Makefile | 45 +++++++++ newlib0/README | 73 +++++++++++++++ newlib0/memmap | 12 +++ newlib0/syscalls.c | 222 +++++++++++++++++++++++++++++++++++++++++++++ newlib0/uart02.c | 123 +++++++++++++++++++++++++ newlib0/vectors.s | 34 +++++++ 6 files changed, 509 insertions(+) create mode 100644 newlib0/Makefile create mode 100644 newlib0/README create mode 100644 newlib0/memmap create mode 100644 newlib0/syscalls.c create mode 100644 newlib0/uart02.c create mode 100644 newlib0/vectors.s diff --git a/newlib0/Makefile b/newlib0/Makefile new file mode 100644 index 0000000..652a6c2 --- /dev/null +++ b/newlib0/Makefile @@ -0,0 +1,45 @@ + +ARMGNU ?= arm-none-eabi + +COPS = -Wall -O2 -nostartfiles -ffreestanding + +LIB = -L /opt/gnuarm/arm-none-eabi/lib/ -L/opt/gnuarm/lib/gcc/arm-none-eabi/4.7.2 + +gcc : uart02.hex uart02.bin + +all : gcc + +clean : + rm -f *.o + rm -f *.bin + rm -f *.hex + rm -f *.elf + rm -f *.list + rm -f *.img + rm -f *.bc + rm -f *.clang.opt.s + +vectors.o : vectors.s + $(ARMGNU)-as vectors.s -o vectors.o + +uart02.o : uart02.c + $(ARMGNU)-gcc $(COPS) -c uart02.c -o uart02.o + +uart02.elf : memmap vectors.o uart02.o syscalls.o + $(ARMGNU)-ld vectors.o uart02.o syscalls.o -T memmap -o uart02.elf $(LIB) -lc -lgcc + $(ARMGNU)-objdump -D uart02.elf > uart02.list + +uart02.bin : uart02.elf + $(ARMGNU)-objcopy uart02.elf -O binary uart02.bin + +uart02.hex : uart02.elf + $(ARMGNU)-objcopy uart02.elf -O ihex uart02.hex + +syscalls.o : syscalls.c + $(ARMGNU)-gcc $(COPS) -c $(COPS) syscalls.c -o syscalls.o + + + + + + diff --git a/newlib0/README b/newlib0/README new file mode 100644 index 0000000..3614f41 --- /dev/null +++ b/newlib0/README @@ -0,0 +1,73 @@ + + +Somewhat threw this together. + +I have not tried newlib in a while, for a while there it was quite +difficult to get a combination of binutils, gcc and newlib that would +build crosscompiled. Somewhat trivial now. + +went here + +ftp://sources.redhat.com/pub/newlib/index.html + +downloaded newlib-1.20.0.tar.gz + +untarred it + +since I already had a gnu toolchain I built using the scripts in my +build_gcc that I built to /opt/gnuarm + +tar xzvf newlib-1.20.0.tar.gz +cd newlib-1.20.0 + +-- now here is my solution +cd newlib/libc/sys/arm/ +mv syscalls.c syscalls.corig +touch syscalls.c +mv libcfunc.c libcfunc.corig +touch libcfunc.c +mv crt0.S crt0.Sorig +touch crt0.S +cd ../../../../ +-- should be back in the newlib-1.20.0 directory + +./configure --target=arm-none-eabi --prefix=/opt/gnuarm +make +make install + +what I would then do is go into syscalls and start pruning, make all +the functions return whatever the proper flavor of success was. to +get printf to work you need isatty to return the right thing. note +I also have a functional malloc here by defining the beginning of my +heap and doing trivial memory management. + +The way I used to do all of this is based on something I learned from +others, before building gcc pull in the proper newlib directories +and then build gcc with the right headers and newlib stuff and it +would build it all into one nice package (when it worked) so you didnt +have to specify lib paths. Since I threw this together I had to specify +the lib paths to things you will need to fix those paths for yours. + +The reason why I neutered the three files in newlib is it is a quick and +easy way to allow the newlib makefile to continue without complaint but +also to allow the project to own those system calls. Since I run bare +metal each project may want to do something different. If this were an +operating system use of newlib then I would consider one solution for +everyone and have it built into the normal place. + +I had to fight the linker a little to get the right code in the right +place. Be careful to check your disassembly of your binary so that +you know that your start code is at the beginning of your image not +somewhere after crti or crtbegin or something like that. + +you should see + +... +12345678 +0000800C +Hello World! +0x0000800C +32780 + +If it all works. for now I will not leave binaries in the repo even +though I expect this is a challenging build for many. diff --git a/newlib0/memmap b/newlib0/memmap new file mode 100644 index 0000000..ceb0e66 --- /dev/null +++ b/newlib0/memmap @@ -0,0 +1,12 @@ + +MEMORY +{ + ram : ORIGIN = 0x8000, LENGTH = 0x20000 +} + +SECTIONS +{ + .text : { *(.text*) } > ram + .bss : { *(.bss*) } > ram +} + diff --git a/newlib0/syscalls.c b/newlib0/syscalls.c new file mode 100644 index 0000000..b252632 --- /dev/null +++ b/newlib0/syscalls.c @@ -0,0 +1,222 @@ +/* Support files for GNU libc. Files in the system namespace go here. + Files in the C namespace (ie those that do not start with an + underscore) go in .c. */ + +#include <_ansi.h> +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void uart_putc ( unsigned int c ); + +unsigned int heap_end=0x0020000; +unsigned int prev_heap_end; + +/* Forward prototypes. */ +int _system _PARAMS ((const char *)); +int _rename _PARAMS ((const char *, const char *)); +int isatty _PARAMS ((int)); +clock_t _times _PARAMS ((struct tms *)); +int _gettimeofday _PARAMS ((struct timeval *, struct timezone *)); +void _raise _PARAMS ((void)); +int _unlink _PARAMS ((void)); +int _link _PARAMS ((void)); +int _stat _PARAMS ((const char *, struct stat *)); +int _fstat _PARAMS ((int, struct stat *)); +caddr_t _sbrk _PARAMS ((int)); +int _getpid _PARAMS ((int)); +int _kill _PARAMS ((int, int)); +void _exit _PARAMS ((int)); +int _close _PARAMS ((int)); +int _open _PARAMS ((const char *, int, ...)); +int _write _PARAMS ((int, char *, int)); +int _lseek _PARAMS ((int, int, int)); +int _read _PARAMS ((int, char *, int)); +void initialise_monitor_handles _PARAMS ((void)); + +//static int +//remap_handle (int fh) +//{ + //return 0; +//} + +void +initialise_monitor_handles (void) +{ +} + +//static int +//get_errno (void) +//{ + //return(0); +//} + +//static int +//error (int result) +//{ + //errno = get_errno (); + //return result; +//} + + +int +_read (int file, + char * ptr, + int len) +{ + return len; +} + + +int +_lseek (int file, + int ptr, + int dir) +{ + return 0; +} + + +int +_write (int file, + char * ptr, + int len) +{ + int r; + for(r=0;rtv_sec = 10; + tp->tv_usec = 0; + } + if (tzp) + { + tzp->tz_minuteswest = 0; + tzp->tz_dsttime = 0; + } + return 0; +} + +clock_t +_times (struct tms * tp) +{ + clock_t timeval; + + timeval = 10; + if (tp) + { + tp->tms_utime = timeval; /* user time */ + tp->tms_stime = 0; /* system time */ + tp->tms_cutime = 0; /* user time, children */ + tp->tms_cstime = 0; /* system time, children */ + } + return timeval; +}; + + +int +_isatty (int fd) +{ + return 1; + fd = fd; +} + +int +_system (const char *s) +{ + if (s == NULL) + return 0; + errno = ENOSYS; + return -1; +} + +int +_rename (const char * oldpath, const char * newpath) +{ + errno = ENOSYS; + return -1; +} diff --git a/newlib0/uart02.c b/newlib0/uart02.c new file mode 100644 index 0000000..c4713b1 --- /dev/null +++ b/newlib0/uart02.c @@ -0,0 +1,123 @@ + +//------------------------------------------------------------------------- +//------------------------------------------------------------------------- + +#include + +extern void PUT32 ( unsigned int, unsigned int ); +extern unsigned int GET32 ( unsigned int ); +extern void dummy ( unsigned int ); + +#define GPFSEL1 0x20200004 +#define GPSET0 0x2020001C +#define GPCLR0 0x20200028 +#define GPPUD 0x20200094 +#define GPPUDCLK0 0x20200098 + +#define AUX_ENABLES 0x20215004 +#define AUX_MU_IO_REG 0x20215040 +#define AUX_MU_IER_REG 0x20215044 +#define AUX_MU_IIR_REG 0x20215048 +#define AUX_MU_LCR_REG 0x2021504C +#define AUX_MU_MCR_REG 0x20215050 +#define AUX_MU_LSR_REG 0x20215054 +#define AUX_MU_MSR_REG 0x20215058 +#define AUX_MU_SCRATCH 0x2021505C +#define AUX_MU_CNTL_REG 0x20215060 +#define AUX_MU_STAT_REG 0x20215064 +#define AUX_MU_BAUD_REG 0x20215068 + +//GPIO14 TXD0 and TXD1 +//GPIO15 RXD0 and RXD1 +//alt function 5 for uart1 +//alt function 0 for uart0 + +//((250,000,000/115200)/8)-1 = 270 +//------------------------------------------------------------------------ +void uart_putc ( unsigned int c ) +{ + if(c==0x0A) uart_putc(0x0D); + while(1) + { + if(GET32(AUX_MU_LSR_REG)&0x20) break; + } + PUT32(AUX_MU_IO_REG,c); +} +//------------------------------------------------------------------------ +void hexstrings ( unsigned int d ) +{ + //unsigned int ra; + unsigned int rb; + unsigned int rc; + + rb=32; + while(1) + { + rb-=4; + rc=(d>>rb)&0xF; + if(rc>9) rc+=0x37; else rc+=0x30; + uart_putc(rc); + if(rb==0) break; + } + uart_putc(0x20); +} +//------------------------------------------------------------------------ +void hexstring ( unsigned int d ) +{ + hexstrings(d); + uart_putc(0x0D); + uart_putc(0x0A); +} +//------------------------------------------------------------------------ +int notmain ( unsigned int earlypc ) +{ + unsigned int ra; + + PUT32(AUX_ENABLES,1); + PUT32(AUX_MU_IER_REG,0); + PUT32(AUX_MU_CNTL_REG,0); + PUT32(AUX_MU_LCR_REG,3); + PUT32(AUX_MU_MCR_REG,0); + PUT32(AUX_MU_IER_REG,0); + PUT32(AUX_MU_IIR_REG,0xC6); + PUT32(AUX_MU_BAUD_REG,270); + + ra=GET32(GPFSEL1); + ra&=~(7<<12); //gpio14 + ra|=2<<12; //alt5 + ra&=~(7<<15); //gpio15 + ra|=2<<15; //alt5 + PUT32(GPFSEL1,ra); + + PUT32(GPPUD,0); + for(ra=0;ra<150;ra++) dummy(ra); + PUT32(GPPUDCLK0,(1<<14)|(1<<15)); + for(ra=0;ra<150;ra++) dummy(ra); + PUT32(GPPUDCLK0,0); + + PUT32(AUX_MU_CNTL_REG,3); + + hexstring(0x12345678); + hexstring(earlypc); + + printf("Hello World!\n"); + printf("0x%08X\n",earlypc); + printf("%u\n",earlypc); + + return(0); +} +//------------------------------------------------------------------------- +//------------------------------------------------------------------------- + + +//------------------------------------------------------------------------- +// +// Copyright (c) 2012 David Welch dwelch@dwelch.com +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// +//------------------------------------------------------------------------- diff --git a/newlib0/vectors.s b/newlib0/vectors.s new file mode 100644 index 0000000..b88fad3 --- /dev/null +++ b/newlib0/vectors.s @@ -0,0 +1,34 @@ + +.globl _start +_start: + mov sp,#0x8000 + mov r0,pc + bl notmain +hang: b hang + +.globl PUT32 +PUT32: + str r1,[r0] + bx lr + +.globl GET32 +GET32: + ldr r0,[r0] + bx lr + +.globl dummy +dummy: + bx lr + + +;@------------------------------------------------------------------------- +;@ +;@ Copyright (c) 2012 David Welch dwelch@dwelch.com +;@ +;@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +;@ +;@ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +;@ +;@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +;@ +;@-------------------------------------------------------------------------