From 316ff19bc7ece01a51a385b2a63def478d9b1a28 Mon Sep 17 00:00:00 2001 From: Andrew Karpow Date: Wed, 21 Aug 2013 16:15:14 +0200 Subject: [PATCH 1/4] Added gcc cross-compiler support + Makefile --- .../{startup_LPC17xx.s => startup_LPC17xx.S} | 0 Makefile | 85 +++++++ jtag_1149.c | 1 - lpc17xx.ld | 216 ++++++++++++++++++ syscalls.c | 167 ++++++++++++++ uart.c | 1 - uart.h | 4 +- 7 files changed, 471 insertions(+), 3 deletions(-) rename Base/Core/CM3/DeviceSupport/NXP/LPC17xx/startup/gcc/{startup_LPC17xx.s => startup_LPC17xx.S} (100%) create mode 100644 Makefile create mode 100644 lpc17xx.ld create mode 100644 syscalls.c diff --git a/Base/Core/CM3/DeviceSupport/NXP/LPC17xx/startup/gcc/startup_LPC17xx.s b/Base/Core/CM3/DeviceSupport/NXP/LPC17xx/startup/gcc/startup_LPC17xx.S similarity index 100% rename from Base/Core/CM3/DeviceSupport/NXP/LPC17xx/startup/gcc/startup_LPC17xx.s rename to Base/Core/CM3/DeviceSupport/NXP/LPC17xx/startup/gcc/startup_LPC17xx.S diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a8de46c --- /dev/null +++ b/Makefile @@ -0,0 +1,85 @@ +TARGET = ddk-arm + +#### Setup #### +TOOLCHAIN = arm-none-eabi + +CC = $(TOOLCHAIN)-gcc +CP = $(TOOLCHAIN)-objcopy +SIZE = $(TOOLCHAIN)-size + +CFLAGS += -Os -ffunction-sections -fdata-sections +CFLAGS += -mthumb -mcpu=cortex-m3 -std=c99 -I./ +CFLAGS += -IBase/Core/CM3/DeviceSupport/NXP/LPC17xx +CFLAGS += -IBase/Core/CM3/CoreSupport +CFLAGS += -IBase/FreeRTOSV7.3.0/FreeRTOS/Source/include +CFLAGS += -IBase/FreeRTOSV7.3.0/FreeRTOS/Source/portable/GCC/ARM_CM3 +CFLAGS += -IBase/CDL/Drivers/include +CFLAGS += -IBase/directc +CFLAGS += -D RAM_MODE=0 +LFLAGS = -Wl,--gc-sections -Tlpc17xx.ld +CPFLAGS = -Obinary +CRT0 = ./Base/Core/CM3/DeviceSupport/NXP/LPC17xx/startup/gcc/startup_LPC17xx.S + +SRC = \ +buffer.c \ +helper.c \ +io.c \ +led.c \ +main.c \ +printf-stdarg.c \ +stats.c \ +uart.c \ +tests.c \ +jtag_1149.c \ +cli.c \ +syscalls.c \ +Base/Core/CM3/DeviceSupport/NXP/LPC17xx/system_LPC17xx.c \ +Base/CDL/Drivers/source/lpc17xx_clkpwr.c \ +Base/CDL/Drivers/source/lpc17xx_exti.c \ +Base/CDL/Drivers/source/lpc17xx_gpio.c \ +Base/CDL/Drivers/source/lpc17xx_libcfg_default.c \ +Base/CDL/Drivers/source/lpc17xx_nvic.c \ +Base/CDL/Drivers/source/lpc17xx_pinsel.c \ +Base/CDL/Drivers/source/lpc17xx_systick.c \ +Base/CDL/Drivers/source/lpc17xx_timer.c \ +Base/CDL/Drivers/source/lpc17xx_uart.c \ +Base/FreeRTOSV7.3.0/FreeRTOS/Source/croutine.c \ +Base/FreeRTOSV7.3.0/FreeRTOS/Source/list.c \ +Base/FreeRTOSV7.3.0/FreeRTOS/Source/queue.c \ +Base/FreeRTOSV7.3.0/FreeRTOS/Source/tasks.c \ +Base/FreeRTOSV7.3.0/FreeRTOS/Source/timers.c \ +Base/FreeRTOSV7.3.0/FreeRTOS/Source/portable/MemMang/heap_4.c \ +Base/FreeRTOSV7.3.0/FreeRTOS/Source/portable/GCC/ARM_CM3/port.c \ +Base/directc/dpalg.c \ +Base/directc/dpchain.c \ +Base/directc/dpcom.c \ +Base/directc/dpcore.c \ +Base/directc/dpfrom.c \ +Base/directc/dpjtag.c \ +Base/directc/dpnvm.c \ +Base/directc/dpsecurity.c \ +Base/directc/dpuser.c \ +Base/directc/dputil.c \ +bitstream/bitstream.c + +OBJ = $(SRC:.c=.o) + + +.PHONY: clean install + +#### Rules #### +all: $(TARGET).bin + +%.o: %.c + $(CC) -c $(CFLAGS) $< -o $@ + +$(TARGET).elf: $(OBJ) + $(CC) $(LFLAGS) $(CFLAGS) -o $(TARGET).elf $(OBJ) $(CRT0) + +$(TARGET).bin: $(TARGET).elf + $(CP) $(CPFLAGS) $(TARGET).elf $(TARGET).bin + $(SIZE) $(TARGET).elf + +clean: + rm -f $(OBJ) $(TARGET).elf $(TARGET).bin + diff --git a/jtag_1149.c b/jtag_1149.c index 5e7c9ad..29ee357 100644 --- a/jtag_1149.c +++ b/jtag_1149.c @@ -32,7 +32,6 @@ #include #include -#include #include #include "jtag_1149.h" diff --git a/lpc17xx.ld b/lpc17xx.ld new file mode 100644 index 0000000..a7a91ad --- /dev/null +++ b/lpc17xx.ld @@ -0,0 +1,216 @@ +/* Linker script for Cortex-M3 + * + * Version:CodeSourcery Sourcery G++ Lite 2007q3-53 + * BugURL:https://support.codesourcery.com/GNUToolchain/ + * + * Copyright 2007 CodeSourcery. + * + * The authors hereby grant permission to use, copy, modify, distribute, + * and license this software and its documentation for any purpose, provided + * that existing copyright notices are retained in all copies and that this + * notice is included verbatim in any distributions. No written agreement, + * license, or royalty fee is required for any of the authorized uses. + * Modifications to this software may be copyrighted by their authors + * and need not follow the licensing terms described here, provided that + * the new terms are clearly indicated on the first page of each file where + * they apply. */ + +OUTPUT_FORMAT ("elf32-littlearm", "elf32-bigarm", "elf32-littlearm") +/*ENTRY(_start)*/ +SEARCH_DIR(.) + +MEMORY +{ + rom (rx) : ORIGIN = 0x00000000, LENGTH = 256K + ram (rwx) : ORIGIN = 0x10000000, LENGTH = 32K + + ram1(rwx) : ORIGIN = 0x2007C000, LENGTH = 16k + ram2(rwx) : ORIGIN = 0x20080000, LENGTH = 16k +} + +/* These force the linker to search for particular symbols from + * the start of the link process and thus ensure the user's + * overrides are picked up + */ + +EXTERN(__cs3_reset_cortex_m) +EXTERN(__cs3_interrupt_vector_cortex_m) +EXTERN(__cs3_start_c main __cs3_stack __cs3_stack_size __cs3_heap_end) + +PROVIDE(__cs3_stack = __cs3_region_start_ram + __cs3_region_size_ram); +PROVIDE(__cs3_stack_size = __cs3_region_start_ram + __cs3_region_size_ram - _end); +PROVIDE(__cs3_heap_start = _end); +PROVIDE(__cs3_heap_end = __cs3_region_start_ram + __cs3_region_size_ram); + +SECTIONS +{ + .text : + { + CREATE_OBJECT_SYMBOLS + __cs3_region_start_rom = .; + *(.cs3.region-head.rom) + __cs3_interrupt_vector = __cs3_interrupt_vector_cortex_m; + *(.cs3.interrupt_vector) + /* Make sure we pulled in an interrupt vector. */ + ASSERT (. != __cs3_interrupt_vector_cortex_m, "No interrupt vector"); + *(.rom) + *(.rom.b) + + __cs3_reset = __cs3_reset_cortex_m; + *(.cs3.reset) + /* Make sure we pulled in some reset code. */ + ASSERT (. != __cs3_reset, "No reset code"); + + /* CRP section */ + . = 0x000002fc; + KEEP(*(.crp)) + + . = ALIGN(4); + *(.text .text.* .gnu.linkonce.t.*) + *(.plt) + *(.gnu.warning) + *(.glue_7t) *(.glue_7) *(.vfp11_veneer) + + *(.rodata .rodata.* .gnu.linkonce.r.*) + + *(.ARM.extab* .gnu.linkonce.armextab.*) + *(.gcc_except_table) + *(.eh_frame_hdr) + *(.eh_frame) + + . = ALIGN(4); + KEEP(*(.init)) + + . = ALIGN(4); + __preinit_array_start = .; + KEEP (*(.preinit_array)) + __preinit_array_end = .; + + . = ALIGN(4); + __init_array_start = .; + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array)) + __init_array_end = .; + + . = ALIGN(0x4); + KEEP (*crtbegin.o(.ctors)) + KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors)) + KEEP (*(SORT(.ctors.*))) + KEEP (*crtend.o(.ctors)) + + . = ALIGN(4); + KEEP(*(.fini)) + + . = ALIGN(4); + __fini_array_start = .; + KEEP (*(.fini_array)) + KEEP (*(SORT(.fini_array.*))) + __fini_array_end = .; + + KEEP (*crtbegin.o(.dtors)) + KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors)) + KEEP (*(SORT(.dtors.*))) + KEEP (*crtend.o(.dtors)) + + . = ALIGN(4); + __cs3_regions = .; + LONG (0) + LONG (__cs3_region_init_ram) + LONG (__cs3_region_start_ram) + LONG (__cs3_region_init_size_ram) + LONG (__cs3_region_zero_size_ram) + } + + /* .ARM.exidx is sorted, so has to go in its own output section. */ + __exidx_start = .; + .ARM.exidx : + { + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + } >rom + __exidx_end = .; + .text.align : + { + . = ALIGN(8); + _etext = .; + } >rom + __cs3_region_size_rom = LENGTH(rom); + __cs3_region_num = 1; + + .data : + { + __cs3_region_start_ram = .; + *(.cs3.region-head.ram) + KEEP(*(.jcr)) + *(.got.plt) *(.got) + *(.shdata) + *(.data .data.* .gnu.linkonce.d.*) + *(.ram) + . = ALIGN (8); + _edata = .; + } >ram AT>rom + .bss : + { + __bss_start__ = .; + *(.shbss) + *(.bss .bss.* .gnu.linkonce.b.*) + *(COMMON) + __bss_end__ = .; + *(.ram.b) + . = ALIGN (8); + _end = .; + __end = .; + } >ram AT>rom + /* This used for USB RAM section */ + .usb_ram (NOLOAD): + { + *.o (USB_RAM) + } > ram2 + .heap (NOLOAD) : + { + *(.heap) + } >ram + .stack (__cs3_stack - __cs3_stack_size) (NOLOAD): + { + *(.stack) + _estack = .; + PROVIDE(estack = .); + } >ram + + __cs3_region_init_ram = LOADADDR (.data); + __cs3_region_init_size_ram = _edata - __cs3_region_start_ram; + __cs3_region_zero_size_ram = _end - _edata; + __cs3_region_size_ram = LENGTH(ram); + __cs3_region_num = 1; + + .stab 0 (NOLOAD) : { *(.stab) } + .stabstr 0 (NOLOAD) : { *(.stabstr) } + /* DWARF debug sections. + * Symbols in the DWARF debugging sections are relative to the beginning + * of the section so we begin them at 0. */ + /* DWARF 1 */ + .debug 0 : { *(.debug) } + .line 0 : { *(.line) } + /* GNU DWARF 1 extensions */ + .debug_srcinfo 0 : { *(.debug_srcinfo) } + .debug_sfnames 0 : { *(.debug_sfnames) } + /* DWARF 1.1 and DWARF 2 */ + .debug_aranges 0 : { *(.debug_aranges) } + .debug_pubnames 0 : { *(.debug_pubnames) } + /* DWARF 2 */ + .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_line 0 : { *(.debug_line) } + .debug_frame 0 : { *(.debug_frame) } + .debug_str 0 : { *(.debug_str) } + .debug_loc 0 : { *(.debug_loc) } + .debug_macinfo 0 : { *(.debug_macinfo) } + /* SGI/MIPS DWARF 2 extensions */ + .debug_weaknames 0 : { *(.debug_weaknames) } + .debug_funcnames 0 : { *(.debug_funcnames) } + .debug_typenames 0 : { *(.debug_typenames) } + .debug_varnames 0 : { *(.debug_varnames) } + + .note.gnu.arm.ident 0 : { KEEP (*(.note.gnu.arm.ident)) } + .ARM.attributes 0 : { KEEP (*(.ARM.attributes)) } + /DISCARD/ : { *(.note.GNU-stack) } +} diff --git a/syscalls.c b/syscalls.c new file mode 100644 index 0000000..a6bd705 --- /dev/null +++ b/syscalls.c @@ -0,0 +1,167 @@ +/***********************************************************************/ +/* */ +/* SYSCALLS.C: System Calls Remapping */ +/* most of this is from newlib-lpc and a Keil-demo */ +/* */ +/* these are "reentrant functions" as needed by */ +/* the WinARM-newlib-config, see newlib-manual */ +/* collected and modified by Martin Thomas */ +/* TODO: some more work has to be done on this */ +/***********************************************************************/ + +#include +#include +#include + +#include "uart.h" + +// new code for _read_r provided by Alexey Shusharin - Thanks +_ssize_t _read_r(struct _reent *r, int file, void *ptr, size_t len) +{ + char c; + int i; + unsigned char *p; + + p = (unsigned char*)ptr; + + for (i = 0; i < len; i++) + { + c = __getchar(); + + *p++ = c; + __putchar(c); + + if (c == 0x0D && i <= (len - 2)) + { + *p = 0x0A; + __putchar(0x0A); + return i + 2; + } + } + return i; +} + + +#if 0 +// old version - no longer in use +_ssize_t _read_r( + struct _reent *r, + int file, + void *ptr, + size_t len) +{ + char c; + int i; + unsigned char *p; + + p = (unsigned char*)ptr; + + for (i = 0; i < len; i++) { + // c = uart0Getch(); + c = uart0GetchW(); + if (c == 0x0D) { + *p='\0'; + break; + } + *p++ = c; + uart0Putch(c); + } + return len - i; +} +#endif + +_ssize_t _write_r ( + struct _reent *r, + int file, + const void *ptr, + size_t len) +{ + int i; + const uint8_t *p = + (const uint8_t *) ptr; + + for (i = 0; i < len; i++) { + if (*p == '\n' ) { + __putchar('\r'); + } + __putchar(*p++); + } + + return len; +} + +int _close_r( + struct _reent *r, + int file) +{ + return 0; +} + +_off_t _lseek_r( + struct _reent *r, + int file, + _off_t ptr, + int dir) +{ + return (_off_t)0; /* Always indicate we are at file beginning. */ +} + + +int _fstat_r( + struct _reent *r, + int file, + struct stat *st) +{ + /* Always set as character device. */ + st->st_mode = S_IFCHR; + /* assigned to strong type with implicit */ + /* signed/unsigned conversion. Required by */ + /* newlib. */ + + return 0; +} + +int _isatty(int file) +{ + return 1; +} + +void _exit (int n) { +label: goto label; /* endless loop */ +} + +/* "malloc clue function" */ + +/**** Locally used variables. ****/ +extern char __cs3_heap_end[]; /* end is set in the linker command */ +/* file and is the end of statically */ +/* allocated data (thus start of heap). */ + +static char *heap_ptr; /* Points to current end of the heap. */ + +/************************** _sbrk_r *************************************/ +/* Support function. Adjusts end of heap to provide more memory to */ +/* memory allocator. Simple and dumb with no sanity checks. */ +/* struct _reent *r -- re-entrancy structure, used by newlib to */ +/* support multiple threads of operation. */ +/* ptrdiff_t nbytes -- number of bytes to add. */ +/* Returns pointer to start of new heap area. */ +/* Note: This implementation is not thread safe (despite taking a */ +/* _reent structure as a parameter). */ +/* Since _s_r is not used in the current implementation, the following */ +/* messages must be suppressed. */ + +void * _sbrk_r( + struct _reent *_s_r, + ptrdiff_t nbytes) +{ + char *base; /* errno should be set to ENOMEM on error */ + + if (!heap_ptr) { /* Initialize if first time through. */ + heap_ptr = __cs3_heap_end; + } + base = heap_ptr; /* Point to end of heap. */ + heap_ptr += nbytes; /* Increase heap. */ + + return base; /* Return pointer to start of new heap area. */ +} diff --git a/uart.c b/uart.c index a53ffc9..4c61bb8 100644 --- a/uart.c +++ b/uart.c @@ -37,7 +37,6 @@ #include #include #include -#include #include "lpc17xx_uart.h" #include "lpc17xx_pinsel.h" diff --git a/uart.h b/uart.h index 529bd3a..dc0123e 100644 --- a/uart.h +++ b/uart.h @@ -51,8 +51,10 @@ int getchar_nb(void); // for stdio (printf, scanf, etc) - blocking! int __getchar(void); void __putchar(int); +#ifndef __GNUC__ void putchar(int); int getchar(void); +#endif void uart1_init(const uint32_t , const bool); void UART1_IRQHandler(void); @@ -73,4 +75,4 @@ int getchar3(void); void putchar3(int); -#endif \ No newline at end of file +#endif From 56bedabf4b6ae2782580272caf3d686c8a8277df Mon Sep 17 00:00:00 2001 From: Andrew Karpow Date: Tue, 17 Sep 2013 17:48:27 +0200 Subject: [PATCH 2/4] added data-initalisation, increased stack + heap --- .../NXP/LPC17xx/startup/gcc/startup_LPC17xx.S | 22 ++++++++++++++----- Makefile | 2 +- lpc17xx.ld | 10 ++++----- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Base/Core/CM3/DeviceSupport/NXP/LPC17xx/startup/gcc/startup_LPC17xx.S b/Base/Core/CM3/DeviceSupport/NXP/LPC17xx/startup/gcc/startup_LPC17xx.S index 129c532..d04504a 100755 --- a/Base/Core/CM3/DeviceSupport/NXP/LPC17xx/startup/gcc/startup_LPC17xx.S +++ b/Base/Core/CM3/DeviceSupport/NXP/LPC17xx/startup/gcc/startup_LPC17xx.S @@ -16,7 +16,7 @@ // */ - .equ Stack_Size, 0x00000100 + .equ Stack_Size, 0x00002000 .section ".stack", "w" .align 3 .globl __cs3_stack_mem @@ -26,8 +26,7 @@ __cs3_stack_mem: .space Stack_Size .endif .size __cs3_stack_mem, . - __cs3_stack_mem - .set __cs3_stack_size, . - __cs3_stack_mem - + .set __cs3_stack_size, . - __cs3_stack_mem /* // Heap Configuration @@ -35,7 +34,7 @@ __cs3_stack_mem: // */ - .equ Heap_Size, 0x00001000 + .equ Heap_Size, 0x00002000 .section ".heap", "w" .align 3 @@ -66,10 +65,10 @@ __cs3_interrupt_vector_cortex_m: .long 0 /* Reserved */ .long 0 /* Reserved */ .long 0 /* Reserved */ - .long SVC_Handler /* SVCall Handler */ + .long vPortSVCHandler /* SVC_Handler */ /* SVCall Handler */ .long DebugMon_Handler /* Debug Monitor Handler */ .long 0 /* Reserved */ - .long PendSV_Handler /* PendSV Handler */ + .long xPortPendSVHandler /* PendSV_Handler */ /* PendSV Handler */ .long SysTick_Handler /* SysTick Handler */ /* External Interrupts */ @@ -124,6 +123,17 @@ __cs3_interrupt_vector_cortex_m: __cs3_reset_cortex_m: .fnstart .if (RAM_MODE) +/* init data section in ram */ + LDR R0, =__cs3_region_init_ram + LDR R1, =__cs3_region_start_ram + LDR R2, =_edata +copy_loop: + LDR R3, [R0] + STR R3, [R1] + ADD R0, #4 + ADD R1, #4 + CMP R1, R2 + BLT copy_loop /* Clear .bss section (Zero init) */ MOV R0, #0 LDR R1, =__bss_start__ diff --git a/Makefile b/Makefile index a8de46c..59a747c 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ CFLAGS += -IBase/FreeRTOSV7.3.0/FreeRTOS/Source/include CFLAGS += -IBase/FreeRTOSV7.3.0/FreeRTOS/Source/portable/GCC/ARM_CM3 CFLAGS += -IBase/CDL/Drivers/include CFLAGS += -IBase/directc -CFLAGS += -D RAM_MODE=0 +CFLAGS += -D RAM_MODE=1 LFLAGS = -Wl,--gc-sections -Tlpc17xx.ld CPFLAGS = -Obinary CRT0 = ./Base/Core/CM3/DeviceSupport/NXP/LPC17xx/startup/gcc/startup_LPC17xx.S diff --git a/lpc17xx.ld b/lpc17xx.ld index a7a91ad..fec7c7a 100644 --- a/lpc17xx.ld +++ b/lpc17xx.ld @@ -24,8 +24,8 @@ MEMORY rom (rx) : ORIGIN = 0x00000000, LENGTH = 256K ram (rwx) : ORIGIN = 0x10000000, LENGTH = 32K - ram1(rwx) : ORIGIN = 0x2007C000, LENGTH = 16k - ram2(rwx) : ORIGIN = 0x20080000, LENGTH = 16k + sram1(rwx) : ORIGIN = 0x2007C000, LENGTH = 16k + sram2(rwx) : ORIGIN = 0x20004000, LENGTH = 16k } /* These force the linker to search for particular symbols from @@ -164,17 +164,17 @@ SECTIONS .usb_ram (NOLOAD): { *.o (USB_RAM) - } > ram2 + } > sram2 .heap (NOLOAD) : { *(.heap) - } >ram + } >sram1 .stack (__cs3_stack - __cs3_stack_size) (NOLOAD): { *(.stack) _estack = .; PROVIDE(estack = .); - } >ram + } >sram1 __cs3_region_init_ram = LOADADDR (.data); __cs3_region_init_size_ram = _edata - __cs3_region_start_ram; From f95fdfb110262611485dcd5a0cff7f284fd6d8dd Mon Sep 17 00:00:00 2001 From: Andrew Karpow Date: Fri, 4 Oct 2013 16:08:47 +0200 Subject: [PATCH 3/4] fixed read/write stubs --- Makefile | 1 - syscalls.c | 200 +++++++++++++++++++---------------------------------- 2 files changed, 71 insertions(+), 130 deletions(-) diff --git a/Makefile b/Makefile index 59a747c..8cee5cf 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,6 @@ helper.c \ io.c \ led.c \ main.c \ -printf-stdarg.c \ stats.c \ uart.c \ tests.c \ diff --git a/syscalls.c b/syscalls.c index a6bd705..83d3cfc 100644 --- a/syscalls.c +++ b/syscalls.c @@ -1,167 +1,109 @@ -/***********************************************************************/ -/* */ -/* SYSCALLS.C: System Calls Remapping */ -/* most of this is from newlib-lpc and a Keil-demo */ -/* */ -/* these are "reentrant functions" as needed by */ -/* the WinARM-newlib-config, see newlib-manual */ -/* collected and modified by Martin Thomas */ -/* TODO: some more work has to be done on this */ -/***********************************************************************/ - -#include -#include +/* + * Copyright (c) 2013, The DDK Project + * Dmitry Nedospasov + * Thorsten Schroeder + * Andrew Karpow + * + * All rights reserved. + * + * This file is part of Die Datenkrake (DDK). + * + * "THE BEER-WARE LICENSE" (Revision 42): + * , and + * wrote this file. As long as you retain this notice you can do whatever + * you want with this stuff. If we meet some day, and you think this stuff is + * worth it, you can buy us a beer in return. Die Datenkrake Project. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE DDK PROJECT BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * File: syscalls.c + * Description: Newlib syscall wrapper for libc compatibility + * + */ + +#include +#include #include #include "uart.h" -// new code for _read_r provided by Alexey Shusharin - Thanks -_ssize_t _read_r(struct _reent *r, int file, void *ptr, size_t len) +#undef errno +extern int errno; + +int _read(int file, char *ptr, int len) { char c; int i; - unsigned char *p; - - p = (unsigned char*)ptr; for (i = 0; i < len; i++) { - c = __getchar(); - - *p++ = c; - __putchar(c); - - if (c == 0x0D && i <= (len - 2)) - { - *p = 0x0A; - __putchar(0x0A); - return i + 2; - } + c = __getchar(); + + *ptr++ = c; + __putchar(c); + + if(c == '\r' && i < len - 1) + { + *ptr = '\n'; + __putchar(*ptr); + i++; + break; + } } return i; } - -#if 0 -// old version - no longer in use -_ssize_t _read_r( - struct _reent *r, - int file, - void *ptr, - size_t len) -{ - char c; - int i; - unsigned char *p; - - p = (unsigned char*)ptr; - - for (i = 0; i < len; i++) { - // c = uart0Getch(); - c = uart0GetchW(); - if (c == 0x0D) { - *p='\0'; - break; - } - *p++ = c; - uart0Putch(c); - } - return len - i; -} -#endif - -_ssize_t _write_r ( - struct _reent *r, - int file, - const void *ptr, - size_t len) +int _write(int file, char *ptr, int len) { int i; - const uint8_t *p = - (const uint8_t *) ptr; - - for (i = 0; i < len; i++) { - if (*p == '\n' ) { - __putchar('\r'); - } - __putchar(*p++); - } + + for (i = 0; i < len; i++) + __putchar(*ptr++); return len; } -int _close_r( - struct _reent *r, - int file) -{ +int _close(int file) { return 0; } -_off_t _lseek_r( - struct _reent *r, - int file, - _off_t ptr, - int dir) -{ - return (_off_t)0; /* Always indicate we are at file beginning. */ -} - -int _fstat_r( - struct _reent *r, - int file, - struct stat *st) +int _lseek(int file, int ptr, int dir) { - /* Always set as character device. */ - st->st_mode = S_IFCHR; - /* assigned to strong type with implicit */ - /* signed/unsigned conversion. Required by */ - /* newlib. */ - return 0; } -int _isatty(int file) -{ +int _isatty(int file) { return 1; } -void _exit (int n) { -label: goto label; /* endless loop */ +int _fstat(int file, struct stat *st) +{ + st->st_mode = S_IFCHR; + return 0; } -/* "malloc clue function" */ - -/**** Locally used variables. ****/ -extern char __cs3_heap_end[]; /* end is set in the linker command */ -/* file and is the end of statically */ -/* allocated data (thus start of heap). */ - -static char *heap_ptr; /* Points to current end of the heap. */ - -/************************** _sbrk_r *************************************/ -/* Support function. Adjusts end of heap to provide more memory to */ -/* memory allocator. Simple and dumb with no sanity checks. */ -/* struct _reent *r -- re-entrancy structure, used by newlib to */ -/* support multiple threads of operation. */ -/* ptrdiff_t nbytes -- number of bytes to add. */ -/* Returns pointer to start of new heap area. */ -/* Note: This implementation is not thread safe (despite taking a */ -/* _reent structure as a parameter). */ -/* Since _s_r is not used in the current implementation, the following */ -/* messages must be suppressed. */ - -void * _sbrk_r( - struct _reent *_s_r, - ptrdiff_t nbytes) +extern char __cs3_heap_end[]; /* end is set in the linker command */ +static char *heap_ptr; /* Points to current end of the heap. */ + +void *_sbrk(ptrdiff_t nbytes) { - char *base; /* errno should be set to ENOMEM on error */ + char *base; /* errno should be set to ENOMEM on error */ - if (!heap_ptr) { /* Initialize if first time through. */ - heap_ptr = __cs3_heap_end; + if (!heap_ptr) { /* Initialize if first time through. */ + heap_ptr = __cs3_heap_end; } - base = heap_ptr; /* Point to end of heap. */ - heap_ptr += nbytes; /* Increase heap. */ + base = heap_ptr; /* Point to end of heap. */ + heap_ptr += nbytes; /* Increase heap. */ - return base; /* Return pointer to start of new heap area. */ + return base; /* Return pointer to start of new heap area. */ } From 9df3826bcf328c130a1386fdfcbe6016df9a7ee7 Mon Sep 17 00:00:00 2001 From: Andrew Karpow Date: Fri, 4 Oct 2013 14:07:54 +0200 Subject: [PATCH 4/4] added lpc17xx checksum calculation program --- Makefile | 17 ++++++++++----- checksum.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 checksum.c diff --git a/Makefile b/Makefile index 8cee5cf..2fb5f62 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ TOOLCHAIN = arm-none-eabi CC = $(TOOLCHAIN)-gcc CP = $(TOOLCHAIN)-objcopy SIZE = $(TOOLCHAIN)-size +CHECKSUM= ./checksum CFLAGS += -Os -ffunction-sections -fdata-sections CFLAGS += -mthumb -mcpu=cortex-m3 -std=c99 -I./ @@ -72,13 +73,17 @@ all: $(TARGET).bin %.o: %.c $(CC) -c $(CFLAGS) $< -o $@ -$(TARGET).elf: $(OBJ) - $(CC) $(LFLAGS) $(CFLAGS) -o $(TARGET).elf $(OBJ) $(CRT0) +$(TARGET).elf: $(OBJ) $(CRT0) + $(CC) $(LFLAGS) $(CFLAGS) $^ -o $@ -$(TARGET).bin: $(TARGET).elf - $(CP) $(CPFLAGS) $(TARGET).elf $(TARGET).bin - $(SIZE) $(TARGET).elf +$(TARGET).bin: $(TARGET).elf $(CHECKSUM) + $(CP) $(CPFLAGS) $< $@ + $(CHECKSUM) $@ + $(SIZE) $< + +$(CHECKSUM): checksum.c + gcc -o $@ $< clean: - rm -f $(OBJ) $(TARGET).elf $(TARGET).bin + rm -f $(OBJ) $(CHECKSUM) $(TARGET).elf $(TARGET).bin diff --git a/checksum.c b/checksum.c new file mode 100644 index 0000000..7be4e1f --- /dev/null +++ b/checksum.c @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2013, The DDK Project + * Dmitry Nedospasov + * Thorsten Schroeder + * Andrew Karpow + * + * All rights reserved. + * + * This file is part of Die Datenkrake (DDK). + * + * "THE BEER-WARE LICENSE" (Revision 42): + * wrote this file. As long as you retain this notice + * you can do whatever you want with this stuff. + * If we meet some day, and you think this stuff is worth it, you can buy us a + * beer in return. Die Datenkrake Project. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE DDK PROJECT BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * File: checksum.c + * Description: LPC 17xx checksum insertion program + */ + + +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + int32_t fd, i, *addr; + + if(argc < 2) + { + fprintf(stderr, "Usage: %s file\n", argv[0]); + exit(EXIT_FAILURE); + } + + if((fd = open(argv[1], O_RDWR)) == -1 || + (addr = mmap(NULL, 8 * sizeof(uint32_t), PROT_READ | + PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) + { + fprintf(stderr, "Error opening '%s'\n", argv[1]); + exit(EXIT_FAILURE); + } + + addr[7] = 0; + for(i = 0; i < 7; i++) + addr[7] += addr[i]; + + addr[7] = -addr[7]; + + return EXIT_SUCCESS; +}