Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / build_common / arduino / SConscript
index 4c3a01f..65c0a3b 100644 (file)
@@ -113,12 +113,28 @@ def __import_lib(env, lib):
        lib_src.extend(__search_files(lib_path, '*.c'))
        lib_src.extend(__search_files(lib_path, '*.cpp'))
 
+       lib_obj = __src_to_obj(env, lib_src)
        build_dir = env.get('BUILD_DIR')
        if build_dir:
-               lib_a = env.StaticLibrary(build_dir + lib, __src_to_obj(env, lib_src))
+               lib_a = env.StaticLibrary(build_dir + lib, lib_obj)
        else:
-               lib_a = env.StaticLibrary(lib, __src_to_obj(env, lib_src))
-       env.AppendUnique(LIBS = [File(lib_a[0])])
+               lib_a = env.StaticLibrary(lib, lib_obj)
+
+       # If we link libSPI.a, the final binary is not getting launched
+       # on the board.  Which is not the case if we directly use SPI.o.
+
+       if env.get('TARGET_ARCH') == 'arm':
+               if lib == 'SPI':
+                       for obj in lib_obj:
+                               if obj.name.endswith('SPI.o'):
+                                       env.PrependUnique(LIBS = [File(obj)])
+               else:
+
+                       env.AppendUnique(LIBS = [File(lib_a[0])])
+       else:
+               env.PrependUnique(LIBS = [File(lib_a[0])])
+
+       #env.AppendUnique(LIBS = [File(lib_a[0])])
 
 def __build_core(env):
        core_src = __search_files(core_folder, '*.S')
@@ -130,11 +146,14 @@ def __build_core(env):
        core_src.extend(__search_files(variant_folder, '*.cpp'))
 
        core_obj = __src_to_obj(env, core_src)
-       build_dir = env.get('BUILD_DIR')
-       if build_dir:
-               s_core = env.StaticLibrary(build_dir + 'core', core_obj)
-       else:
-               s_core = env.StaticLibrary('core', core_obj)
+
+       prefix = env.get('BOARD') + '_'
+       if env.get('CPU'):
+               prefix += env.get('CPU') + '_'
+
+       core = env.get('BUILD_DIR', '.') + '/arduino/' + prefix + 'core'
+       s_core = env.StaticLibrary(core, core_obj)
+
        env.AppendUnique(LIBS = [File(s_core[0])])
 
        # To avoid compiler issue. Otherewise there may be warnings:
@@ -153,6 +172,25 @@ def __create_bin(env, source):
        else:
                hex = env.Command(name + '.hex', source, 'arm-none-eabi-objcopy -O binary $SOURCE $TARGET')
 
+#Currently supporting only mega (ie. Arduino ATMega2560) and arduino_due_x/arduino_due_x_dbg (i.e. Arduino Due) builds
+def __upload(env, binary):
+        if target_arch == 'avr':
+                protocol = __get_board_info(board, '.upload.protocol')
+                speed = __get_board_info(board, '.upload.speed')
+                port = '/dev/ttyACM0'
+                upload_cmd = arduino_home + '/hardware/tools/avr/bin/avrdude -C' + arduino_home +'/hardware/tools/avr/etc/avrdude.conf -p' \
+                + mcu + ' -c' + protocol + ' -P' + port + ' -b' + speed + ' -D -Uflash:w:' + binary + ':i'
+                print "Upload command: %s" %upload_cmd
+                install_cmd = env.Command('install_cmd', None, upload_cmd)
+                env.Default('install_cmd')
+        elif target_arch == 'arm':
+                protocol = __get_board_info(board, '.upload.protocol')
+                speed = __get_board_info(board, '.upload.speed')
+                port = 'ttyACM0'
+                uu = __get_board_info(board, '.upload.native_usb')
+                os.system('stty -F /dev/' + port + ' speed 1200 cs8 -cstopb -parenb')
+                os.system(arduino_home + '/hardware/tools/bossac -i --port=' + port + ' -U false -e -w -b ' + binary + ' -R')
+
 # Print the command line that to upload binary to the board
 def __upload_help(env):
        if target_arch == 'avr':
@@ -182,16 +220,12 @@ help_vars.Update(env)
 Help(help_vars.GenerateHelpText(env))
 
 target_arch = env.get('TARGET_ARCH')
+
+# Verify that the arduino, time, red bear, and nordic libraries are
+# installed.  If not, get them and install them.
+SConscript(env.get('SRC_DIR') + '/extlibs/arduino/SConscript')
 arduino_home = env.get('ARDUINO_HOME')
-if not arduino_home:
-       print '''
-************************************* Error ***********************************
-*   Arduino root directory isn't set, you can set enviornment variable        *
-* ARDUINO_HOME or add it in command line as:                                  *
-*      # scons ARDUINO_HOME=<path to arduino root directory> ...              *
-*******************************************************************************
-'''
-       Exit(1)
+print 'ARDUINO_HOME = ' + env.get('ARDUINO_HOME')
 
 # Overwrite suffixes and prefixes
 if env['HOST_OS'] == 'win32':
@@ -221,7 +255,7 @@ else:
 # Get IDE version
 if os.path.exists(arduino_home + '/lib/version.txt'):
        vf = open(arduino_home + '/lib/version.txt', 'r')
-       version = vf.readline().replace('.', '')
+       version = vf.readline().replace('.', '').strip()
 else:
        print '''
 ************************************* Error ***********************************
@@ -310,7 +344,7 @@ core_folder = core_base + 'cores/' + core + '/'
 env.AppendUnique(CPPPATH = [core_folder])
 
 if is_1_0_x:
-       comm_flags = []
+       comm_flags = ['-std=c99']
        if mcu:
                comm_flags.extend(['-mmcu=' + mcu])
        if f_cpu:
@@ -344,9 +378,11 @@ else:
 
        comm_flag = [cpu_flag, '-DF_CPU=' + f_cpu, '-DARDUINO=' + version, '-DARDUINO_' + __get_board_info(board, '.build.board')]
        if target_arch == 'arm':
-               comm_flag.extend(['-DARDUINO_ARCH_SAM'])
+               # As of 1.5.8 the arduino headers for arm had _asm_ bugs with ARM and
+               # require gnu99 to be used
+               comm_flag.extend(['-std=gnu99', '-DARDUINO_ARCH_SAM'])
        else:
-               comm_flag.extend(['-DARDUINO_ARCH_AVR'])
+               comm_flag.extend(['-std=c99', '-DARDUINO_ARCH_AVR'])
 
        compiler_path = platform_info.get('compiler.path')
        compiler_path = compiler_path.replace('{runtime.ide.path}', arduino_home)
@@ -374,9 +410,8 @@ else:
 
        if target_arch == 'arm':
                env.AppendUnique(LINKFLAGS = ['-Os', '-Wl,--gc-sections', cpu_flag,
-                                       '-T' + variant_folder + '/' + __get_board_info(board, '.build.ldscript'),
-                                       '-Wl,-Map,' + env.get('BUILD_DIR') + 'arduino_prj.map'])
-               env.AppendUnique(LINKFLAGS = Split('-lm -lgcc -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 -Wl,--start-group'))
+                                       '-T' + variant_folder + '/' + __get_board_info(board, '.build.ldscript')])
+               env.AppendUnique(LINKFLAGS = Split('-lm -lgcc -mthumb -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 -Wl,--start-group'))
 
                variant_system_lib = __get_board_info(board, '.build.variant_system_lib')
                if variant_system_lib:
@@ -392,9 +427,11 @@ else:
                env.AppendUnique(LIBS = 'm')
        env.Replace(ARCOM = '$AR ' + platform_info.get('compiler.ar.flags') + ' $TARGET $SOURCES')
 
+
 __build_core(env)
 
 env.AddMethod(__import_lib, "ImportLib") #import arduino library
-#env.AddMethod(__build_core, "BuildCore") #build arduino core
+env.AddMethod(__build_core, "BuildCore") #build arduino core
 env.AddMethod(__create_bin, "CreateBin") #create binary files(.eep and .hex)
+env.AddMethod(__upload, "Upload") #Upload binary to board
 env.AddMethod(__upload_help, "UploadHelp") #print the command line that to upload binary to the boardf