adding newlib0 example

This commit is contained in:
root
2012-10-23 01:06:49 -04:00
parent 4daeded498
commit 7ab6ec9813
6 changed files with 509 additions and 0 deletions

45
newlib0/Makefile Normal file
View File

@@ -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

73
newlib0/README Normal file
View File

@@ -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.

12
newlib0/memmap Normal file
View File

@@ -0,0 +1,12 @@
MEMORY
{
ram : ORIGIN = 0x8000, LENGTH = 0x20000
}
SECTIONS
{
.text : { *(.text*) } > ram
.bss : { *(.bss*) } > ram
}

222
newlib0/syscalls.c Normal file
View File

@@ -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 <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
#include <errno.h>
#include <reent.h>
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;r<len;r++) uart_putc(ptr[r]);
return len;
}
int
_open (const char * path,
int flags,
...)
{
return 0;
}
int
_close (int file)
{
return 0;
}
void
_exit (int n)
{
while(1);
}
int
_kill (int n, int m)
{
return(0);
}
int
_getpid (int n)
{
return 1;
n = n;
}
caddr_t
_sbrk (int incr)
{
prev_heap_end = heap_end;
heap_end += incr;
return (caddr_t) prev_heap_end;
}
int
_fstat (int file, struct stat * st)
{
return 0;
}
int _stat (const char *fname, struct stat *st)
{
return 0;
}
int
_link (void)
{
return -1;
}
int
_unlink (void)
{
return -1;
}
void
_raise (void)
{
return;
}
int
_gettimeofday (struct timeval * tp, struct timezone * tzp)
{
if(tp)
{
tp->tv_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;
}

123
newlib0/uart02.c Normal file
View File

@@ -0,0 +1,123 @@
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
#include <stdio.h>
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.
//
//-------------------------------------------------------------------------

34
newlib0/vectors.s Normal file
View File

@@ -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.
;@
;@-------------------------------------------------------------------------