From ec55df1e9889a310b159709e06919b138d50a93c Mon Sep 17 00:00:00 2001
From: Thibaut VIARD <thibaut.viard@atmel.com>
Date: Wed, 7 May 2014 09:59:31 +0200
Subject: [PATCH]  compilation checkpoint

---
 cores/arduino/Arduino.h                    | 116 ---------------------
 cores/arduino/main.cpp                     |   6 +-
 cores/arduino/startup.c                    |  51 +++++++--
 cores/arduino/syscalls.c                   |  65 ++++++------
 cores/arduino/syscalls.h                   |  10 +-
 cores/arduino/validation/build_gcc/gcc.mk  |   5 +-
 cores/arduino/validation/build_gcc/test.mk |   9 +-
 cores/arduino/wiring.c                     |  17 ++-
 8 files changed, 103 insertions(+), 176 deletions(-)

diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h
index 01dba458..0eb5eeb1 100644
--- a/cores/arduino/Arduino.h
+++ b/cores/arduino/Arduino.h
@@ -72,122 +72,6 @@ typedef void (*voidFuncPtr)( void ) ;
 
 #include "WVariant.h"
 
-///* Definitions and types for pins */
-//typedef enum _EAnalogChannel
-//{
-//  NO_ADC=-1,
-//  ADC0=0,
-//  ADC1=1,
-//  ADC2=2,
-//  ADC3=3,
-//  ADC4=4,
-//  ADC5=5,
-//  ADC6=6,
-//  ADC7=7,
-//  ADC10=10,
-//  ADC16=16,
-//  ADC17=17,
-//  ADC18=18,
-//  ADC19=19,
-//  DAC0,
-//} EAnalogChannel ;
-//
-//#define ADC_CHANNEL_NUMBER_NONE 0xffffffff
-//
-//// Definitions for TC channels
-//typedef enum _ETCChannel
-//{
-//  NOT_ON_TIMER=-1,
-//	TC3_CH0,
-//	TC3_CH1,
-//	TCC0_CH0,
-//	TCC0_CH1,
-//	TCC0_CH4,
-//	TCC0_CH5,
-//	TCC0_CH6,
-//	TCC0_CH7,
-//	TCC1_CH0,
-//	TCC1_CH1,
-//	TCC2_CH0,
-//	TCC2_CH1
-//} ETCChannel ;
-//
-//// Definitions for PWM channels
-//typedef enum _EPWMChannel
-//{
-//  NOT_ON_PWM=-1,
-//	PWM3_CH0=TC3_CH0,
-//	PWM3_CH1=TC3_CH1,
-//	PWM0_CH0=TCC0_CH0,
-//	PWM0_CH1=TCC0_CH1,
-//	PWM0_CH4=TCC0_CH4,
-//	PWM0_CH5=TCC0_CH5,
-//	PWM0_CH6=TCC0_CH6,
-//	PWM0_CH7=TCC0_CH7,
-//	PWM1_CH0=TCC1_CH0,
-//	PWM1_CH1=TCC1_CH1,
-//	PWM2_CH0=TCC2_CH0,
-//	PWM2_CH1=TCC2_CH1
-//} EPWMChannel ;
-//
-//typedef enum _EPortType
-//{
-//	NOT_A_PORT=-1,
-//	PORTA=0,
-//	PORTB=1,
-//	PORTC=2,
-//} EPortType ;
-//
-//
-////A    B                 C       D          E      F   G   H
-////EIC REF ADC AC PTC DAC SERCOM SERCOM_ALT TC/TCC TCC COM AC/GCLK
-//
-//typedef enum _EPioType
-//{
-//  PIO_NOT_A_PIN=-1,  /* Not under control of a peripheral. */
-//  PIO_EXTINT,     /* The pin is controlled by the associated signal of peripheral A. */
-//  PIO_ANALOG,     /* The pin is controlled by the associated signal of peripheral B. */
-//  PIO_SERCOM,     /* The pin is controlled by the associated signal of peripheral C. */
-//  PIO_SERCOM_ALT, /* The pin is controlled by the associated signal of peripheral D. */
-//  PIO_TIMER,      /* The pin is controlled by the associated signal of peripheral E. */
-//  PIO_TIMER_ALT,  /* The pin is controlled by the associated signal of peripheral F. */
-//  PIO_COM,        /* The pin is controlled by the associated signal of peripheral G. */
-//  PIO_AC_CLK,     /* The pin is controlled by the associated signal of peripheral H. */
-//  PIO_PWM=PIO_TIMER,
-//  PIO_PWM_ALT=PIO_TIMER_ALT,
-//
-//  PIO_DIGITAL,    /* The pin is controlled by PORT. */
-//  PIO_INPUT,        /* The pin is controlled by PORT and is an input. */
-//  PIO_INPUT_PULLUP, /* The pin is controlled by PORT and is an input with internal pull-up resistor enabled. */
-//  PIO_OUTPUT,       /* The pin is controlled by PORT and is an output. */
-//} EPioType ;
-//
-///**
-// * Pin Attributes to be OR-ed
-// */
-//#define PIN_ATTR_COMBO         (1UL<<0)
-//#define PIN_ATTR_ANALOG        (1UL<<1)
-//#define PIN_ATTR_DIGITAL       (1UL<<2)
-//#define PIN_ATTR_PWM           (1UL<<3)
-//#define PIN_ATTR_TIMER         (1UL<<4)
-//
-///* Types used for the table below */
-//typedef struct _PinDescription
-//{
-//  uint32_t ulPort ;
-//  uint32_t ulPin ;
-//  EPioType ulPinType ;
-//  uint32_t ulPinConfiguration ;
-//  uint32_t ulPinAttribute ;
-//  EAnalogChannel ulAnalogChannel ; /* Analog pin in the Arduino context (label on the board) */
-//  EAnalogChannel ulADCChannelNumber ; /* ADC Channel number in the SAM device */
-//  EPWMChannel ulPWMChannel ;
-//  ETCChannel ulTCChannel ;
-//} PinDescription ;
-//
-///* Pins table to be instanciated into variant.cpp */
-//extern const PinDescription g_APinDescription[] ;
-
 #ifdef __cplusplus
 } // extern "C"
 
diff --git a/cores/arduino/main.cpp b/cores/arduino/main.cpp
index 7584860a..0efdb70d 100644
--- a/cores/arduino/main.cpp
+++ b/cores/arduino/main.cpp
@@ -40,9 +40,9 @@ int main( void )
 
 	delay(1);
 
-#if defined(USBCON)
-	USBDevice.attach();
-#endif
+//#if defined(USBCON)
+//	USBDevice.attach();
+//#endif
 
 	setup();
 
diff --git a/cores/arduino/startup.c b/cores/arduino/startup.c
index 58bd2b27..811e975a 100644
--- a/cores/arduino/startup.c
+++ b/cores/arduino/startup.c
@@ -29,18 +29,49 @@ extern uint32_t __bss_end__ ;
 extern uint32_t __StackTop;
 
 extern int main( void ) ;
-
-//void __libc_init_array(void);
+extern void __libc_init_array(void);
 
 /* Default empty handler */
 void Dummy_Handler(void);
 
 /* Cortex-M0+ core handlers */
+#if defined DEBUG
+void NMI_Handler( void )
+{
+  while ( 1 )
+  {
+  }
+}
+
+void HardFault_Handler( void )
+{
+  while ( 1 )
+  {
+  }
+}
+
+void SVC_Handler( void )
+{
+  while ( 1 )
+  {
+  }
+}
+
+void PendSV_Handler( void )
+{
+  while ( 1 )
+  {
+  }
+}
+
+void SysTick_Handler         ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
+#else
 void NMI_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
 void HardFault_Handler       ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
 void SVC_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
 void PendSV_Handler          ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
 void SysTick_Handler         ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
+#endif //DEBUG
 
 /* Peripherals handlers */
 void PM_Handler              ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
@@ -73,7 +104,7 @@ void PTC_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler
 void I2S_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
 
 /* Exception Table */
-__attribute__ ((section(".vectors")))
+__attribute__ ((section(".isr_vector")))
 const DeviceVectors exception_table=
 {
   /* Configure Initial Stack Pointer, using linker-generated symbols */
@@ -161,7 +192,7 @@ void SystemInit( void )
    */
   SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_STARTUP( 0x6u ) | /* cf table 15.10 of product datasheet in chapter 15.8.6 */
                          SYSCTRL_XOSC32K_XTALEN | SYSCTRL_XOSC32K_EN32K ;
-  SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_ENABLE ; /* separate call, as described in chapter 15.6.3 */
+  SYSCTRL->XOSC32K.bit.ENABLE = 1 ; /* separate call, as described in chapter 15.6.3 */
 
   while ( (SYSCTRL->PCLKSR.reg & SYSCTRL_PCLKSR_XOSC32KRDY) == 0 )
   {
@@ -268,6 +299,7 @@ void SystemInit( void )
     /* Wait for synchronization */
   }
 
+#if 0
   /*
    * 6) Put OSC8M as source for Generic Clock Generator 3
    */
@@ -287,15 +319,16 @@ void SystemInit( void )
   {
     /* Wait for synchronization */
   }
+#endif
 
   /*
    * Now that all system clocks are configured, we can set CPU and APBx BUS clocks.
    * There values are normally the one present after Reset.
    */
-  PM->CPUSEL.reg  = PM_CPUSEL_CPUDIV_DIV1 ;
-  PM->APBASEL.reg = PM_APBASEL_APBADIV_DIV1_Val ;
-  PM->APBBSEL.reg = PM_APBBSEL_APBBDIV_DIV1_Val ;
-  PM->APBCSEL.reg = PM_APBCSEL_APBCDIV_DIV1_Val ;
+  //PM->CPUSEL.reg  = PM_CPUSEL_CPUDIV_DIV1 ;
+  //PM->APBASEL.reg = PM_APBASEL_APBADIV_DIV1_Val ;
+  //PM->APBBSEL.reg = PM_APBBSEL_APBBDIV_DIV1_Val ;
+  //PM->APBCSEL.reg = PM_APBCSEL_APBCDIV_DIV1_Val ;
 
   SystemCoreClock=VARIANT_MCK ;
 }
@@ -330,7 +363,7 @@ void Reset_Handler( void )
   }
 
   /* Initialize the C library */
-//	__libc_init_array();
+  __libc_init_array();
 
   SystemInit() ;
 
diff --git a/cores/arduino/syscalls.c b/cores/arduino/syscalls.c
index 251e53d4..6f648792 100644
--- a/cores/arduino/syscalls.c
+++ b/cores/arduino/syscalls.c
@@ -21,15 +21,10 @@
   *
   */
 
-#include "syscalls.h"
+#include <errno.h>
 
-#include <stdio.h>
-#include <stdarg.h>
+#include "syscalls.h"
 #include "sam.h"
-#if defined (  __GNUC__  ) /* GCC CS3 */
-  #include <sys/types.h>
-  #include <sys/stat.h>
-#endif
 
 #undef errno
 extern int errno ;
@@ -38,24 +33,24 @@ extern int __ram_end__ ;
 
 extern caddr_t _sbrk( int incr )
 {
-	static unsigned char *heap = NULL ;
-	unsigned char *prev_heap ;
-	int ramend = (int)&__ram_end__ ;
+  static unsigned char *heap = NULL ;
+  unsigned char *prev_heap ;
+  int ramend = (int)&__ram_end__ ;
 
-	if ( heap == NULL )
-	{
-		heap = (unsigned char *)&__end__ ;
-	}
-	prev_heap = heap ;
+  if ( heap == NULL )
+  {
+    heap = (unsigned char *)&__end__ ;
+  }
+  prev_heap = heap ;
 
-	if ( ((int)prev_heap + incr) > ramend )
-	{
-		return (caddr_t) -1 ;
-	}
+  if ( ((int)prev_heap + incr) > ramend )
+  {
+    return (caddr_t) -1 ;
+  }
 
-	heap += incr ;
+  heap += incr ;
 
-	return (caddr_t) prev_heap ;
+  return (caddr_t) prev_heap ;
 }
 
 extern int link( char *cOld, char *cNew )
@@ -95,15 +90,25 @@ extern int _write( int file, char *ptr, int len )
 {
   int iIndex=0 ;
 
-	for ( ; len >= 0 ; len--, ptr++, iIndex++ )
-	{
-		// Check if the transmitter is ready
-//    while ((UART->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY)
-    {
-    }
-
-    // Send character
-//    UART->UART_THR = *ptr;
+  switch ( file )
+  {
+    case 1 /*STDOUT_FILENO*/:
+      for ( ; len >= 0 ; len--, ptr++, iIndex++ )
+      {
+        // Check if the transmitter is ready
+//        while ((UART->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY)
+        {
+        }
+
+        // Send character
+//        UART->UART_THR = *ptr;
+      }
+    break;
+
+    default:
+      errno = EBADF ;
+      iIndex = -1 ;
+    break;
   }
 
   return iIndex ;
diff --git a/cores/arduino/syscalls.h b/cores/arduino/syscalls.h
index 2d0c6c2b..8f215424 100644
--- a/cores/arduino/syscalls.h
+++ b/cores/arduino/syscalls.h
@@ -30,6 +30,7 @@
 #include <stdarg.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+//#include <unistd.h>
 
 /*----------------------------------------------------------------------------
  *        Exported functions
@@ -37,11 +38,14 @@
 #ifdef __cplusplus
 extern "C" {
 #endif
-
-extern caddr_t _sbrk( int incr ) ;
+/*
+*/
+extern void _exit( int status ) ;
 
 extern int link( char *cOld, char *cNew ) ;
 
+extern caddr_t _sbrk( int incr ) ;
+
 extern int _close( int file ) ;
 
 extern int _fstat( int file, struct stat *st ) ;
@@ -54,8 +58,6 @@ extern int _read(int file, char *ptr, int len) ;
 
 extern int _write( int file, char *ptr, int len ) ;
 
-extern void _exit( int status ) ;
-
 extern void _kill( int pid, int sig ) ;
 
 extern int _getpid ( void ) ;
diff --git a/cores/arduino/validation/build_gcc/gcc.mk b/cores/arduino/validation/build_gcc/gcc.mk
index a9e7572e..14c4f814 100644
--- a/cores/arduino/validation/build_gcc/gcc.mk
+++ b/cores/arduino/validation/build_gcc/gcc.mk
@@ -46,11 +46,13 @@ CFLAGS += -Wpacked -Wredundant-decls -Wnested-externs -Winline -Wlong-long
 CFLAGS += -Wunreachable-code
 CFLAGS += -Wcast-align
 
+# -flto
 CFLAGS += --param max-inline-insns-single=500 -mcpu=cortex-m0plus -mthumb -mlong-calls -ffunction-sections -nostdlib -std=c99
 CFLAGS += $(OPTIMIZATION) $(INCLUDES) -D$(DEVICE) -D$(VARIANT)
 
 # To reduce application size use only integer printf function.
 CFLAGS += -Dprintf=iprintf
+# -u _scanf_float -u _printf_float
 
 
 
@@ -66,7 +68,7 @@ CPPFLAGS += -Wsign-compare -Waggregate-return -Wmissing-declarations
 CPPFLAGS += -Wformat -Wmissing-format-attribute -Wno-deprecated-declarations
 CPPFLAGS += -Wpacked -Wredundant-decls -Winline -Wlong-long
 
-#-fno-rtti -fno-exceptions
+#-fno-rtti -fno-exceptions -flto
 CPPFLAGS += --param max-inline-insns-single=500 -mcpu=cortex-m0plus -mthumb -mlong-calls -ffunction-sections -fdata-sections -std=c++98
 CPPFLAGS += $(OPTIMIZATION) $(INCLUDES) -D$(DEVICE)
 
@@ -85,5 +87,6 @@ ASFLAGS = -mcpu=cortex-m0plus -mthumb -Wall -g $(OPTIMIZATION) $(INCLUDES)
 # ---------------------------------------------------------------------------------------
 # LD Flags
 
+#--nostartfiles  -flto
 LDFLAGS= -mcpu=cortex-m0plus -mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--entry=Reset_Handler -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -Wl,--warn-unresolved-symbols
 
diff --git a/cores/arduino/validation/build_gcc/test.mk b/cores/arduino/validation/build_gcc/test.mk
index 51b6c858..1340aa69 100644
--- a/cores/arduino/validation/build_gcc/test.mk
+++ b/cores/arduino/validation/build_gcc/test.mk
@@ -43,6 +43,9 @@ HARDWARE_PATH=../../../../../..
 ARCH_PATH=$(HARDWARE_PATH)/arduino/samd
 ARDUINO_CORE_PATH=$(ARCH_PATH)/cores/arduino
 ARDUINO_USB_PATH=$(ARDUINO_CORE_PATH)/USB
+LIBRARIES_PATH = $(ARCH_PATH)/libraries
+SPI_PATH = $(LIBRARIES_PATH)/SPI
+WIRE_PATH = $(LIBRARIES_PATH)/Wire
 VARIANT_PATH = $(ARCH_PATH)/variants/$(VARIANT)
 TOOLS_PATH = $(HARDWARE_PATH)/tools
 CMSIS_ROOT_PATH=$(TOOLS_PATH)/CMSIS
@@ -62,7 +65,7 @@ OUTPUT_PATH = debug_$(VARIANT)
 # Files
 #-------------------------------------------------------------------------------
 
-vpath %.cpp $(PROJECT_BASE_PATH) $(ARDUINO_CORE_PATH) $(VARIANT_PATH)
+vpath %.cpp $(PROJECT_BASE_PATH) $(ARDUINO_CORE_PATH) $(VARIANT_PATH) $(SPI_PATH) $(WIRE_PATH)
 vpath %.c $(ARDUINO_CORE_PATH) $(VARIANT_PATH)
 
 #VPATH+=$(PROJECT_BASE_PATH)
@@ -72,7 +75,8 @@ INCLUDES += -I$(ARDUINO_USB_PATH)
 INCLUDES += -I$(VARIANT_PATH)
 INCLUDES += -I$(CMSIS_ARM_PATH)
 INCLUDES += -I$(CMSIS_ATMEL_PATH)
-#INCLUDES += -I$(CMSIS_DEVICE_PATH)
+INCLUDES += -I$(SPI_PATH)
+INCLUDES += -I$(WIRE_PATH)
 
 #-------------------------------------------------------------------------------
 ifdef DEBUG
@@ -101,6 +105,7 @@ endif
 
 OUTPUT_BIN=test_$(TOOLCHAIN)_$(LIBS_POSTFIX)
 LIBS=-Wl,--start-group -lgcc -lc -lstdc++ -Wl,--end-group
+#LIBS=--specs=nano.specs -Wl,--start-group -lc lnosys -Wl,--end-group
 
 LIB_PATH =-L$(PROJECT_BASE_PATH)/..
 LIB_PATH+=-L=/lib/thumb2
diff --git a/cores/arduino/wiring.c b/cores/arduino/wiring.c
index f183dd4c..b13bf15b 100644
--- a/cores/arduino/wiring.c
+++ b/cores/arduino/wiring.c
@@ -32,8 +32,6 @@ extern "C" {
  */
 uint32_t SystemCoreClock=1000000ul ;
 
-//void __libc_init_array(void);
-
 /*
  * Arduino Zero board initialization
  *
@@ -47,14 +45,11 @@ void init( void )
   uint32_t ul ;
 
   // Set Systick to 1ms interval, common to all Cortex-M variants
-  if ( SysTick_Config( SystemCoreClock / 1000 ) )
-  {
-    // Capture error
-    while ( 1 ) ;
-  }
-
-  // Initialize C library
-//  __libc_init_array();
+ // if ( SysTick_Config( SystemCoreClock / 1000 ) )
+ // {
+ //   // Capture error
+ //   while ( 1 ) ;
+ // }
 
   // Setup PORT for Digital I/O
 	PM->APBBMASK.reg |= PM_APBBMASK_PORT ;
@@ -62,7 +57,7 @@ void init( void )
 	// Setup all pins (digital and analog) in INPUT mode (default is nothing)
 	for ( ul = 0 ; ul < NUM_DIGITAL_PINS ; ul++ )
 	{
-	  pinMode( ul, INPUT ) ;
+//	  pinMode( ul, INPUT ) ;
 	}
 
   // Initialize Serial port U(S)ART pins
-- 
GitLab