def __get_boards(dict):
boards = []
- keys = dict.keys()
+ keys = list(dict.keys())
for key in keys:
idx = key.find('.name')
if idx > 0:
def __get_cpu(dict, board):
cpus = []
- keys = dict.keys()
+ keys = list(dict.keys())
for key in keys:
idx = key.find(board + '.menu.cpu.')
start = len(board + '.menu.cpu.')
matches = []
for root, dirnames, filenames in os.walk(path):
- matches.extend(Glob(root + '/' + pattern, ondisk, source, strings))
-
+ # This is a helper function to build Arduino libraries. Scripts are using this function
+ # to add all the files in a folder as compilation targets rather than specifying each
+ # file to compile from a Arduino library folder.
+
+ # Since the function is recursive, it adds even "/library/<library-name>/examples" to the
+ # compilation list. This is an extra overhead as stack is never going to use ".o" generated
+ # for these examples.
+ if 'examples' not in root:
+ matches.extend(Glob(os.path.join(root, pattern), ondisk, source, strings))
return matches
# To make sure the src is built in 'BUILD_DIR' (by default it will be built at
if env.get('CPU'):
prefix += env.get('CPU') + '_'
- build_dir = env.get('BUILD_DIR') + '/arduino/'
+ build_dir = os.path.join(env.get('BUILD_DIR'), 'arduino')
for src in srcs:
- obj = src.path.replace(arduino_home, build_dir)
+ if (os.path.isabs(src.path)):
+ obj = src.path.replace(arduino_home, build_dir)
+ else:
+ obj = os.path.join(build_dir, src.path)
i = obj.rfind('.')
obj = obj[0:i]
if env.get('OBJSUFFIX'):
return objs
def __import_lib(env, lib):
- lib_path = arduino_home + '/libraries/' + lib
+ lib_path = os.path.join(arduino_home, 'libraries', lib)
if not os.path.exists(lib_path):
if target_arch == 'avr':
- lib_path = arduino_home + '/hardware/arduino/avr/libraries/' + lib
+ lib_path = os.path.join(arduino_home, 'hardware', 'arduino', 'avr', 'libraries', lib)
else:
- lib_path = arduino_home + '/hardware/arduino/sam/libraries/' + lib
+ lib_path = os.path.join(arduino_home, 'hardware', 'arduino', 'sam', 'libraries', lib)
- if os.path.exists(lib_path + '/src'):
- lib_path = lib_path + '/src'
+ if os.path.exists(os.path.join(lib_path, 'src')):
+ lib_path = os.path.join(lib_path, 'src')
env.AppendUnique(CPPPATH = [lib_path])
- if os.path.exists(lib_path + '/utility'):
- env.AppendUnique(CPPPATH = [lib_path + '/utility'])
+ if os.path.exists(os.path.join(lib_path, 'utility')):
+ env.AppendUnique(CPPPATH = [os.path.join(lib_path, 'utility')])
lib_src = []
lib_src.extend(__search_files(lib_path, '*.S'))
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, 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:
- lib_a = env.StaticLibrary(lib, __src_to_obj(env, lib_src))
- env.AppendUnique(LIBS = [File(lib_a[0])])
+ 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')
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 = os.path.join(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:
else:
hex = env.Command(name + '.hex', source, 'arm-none-eabi-objcopy -O binary $SOURCE $TARGET')
-#Currently supporting only megaADK build
+#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')
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
+ print("Upload command: %s" %upload_cmd)
+ install_cmd = env.Command('install_cmd', None, upload_cmd)
+ env.Default('install_cmd')
+ elif target_arch == 'arm':
+ port = 'ttyACM0'
+ upload_cmd = 'stty -F /dev/' + port + ' speed 1200 cs8 -cstopb -parenb \n' + arduino_home + '/hardware/tools/bossac -i --port=' + port + ' -U false -e -w -b ' + binary + ' -R'
+ print("Upload command: %s" %upload_cmd)
install_cmd = env.Command('install_cmd', None, upload_cmd)
env.Default('install_cmd')
# 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')
+SConscript(os.path.join(env.get('SRC_DIR'), 'extlibs', 'arduino', 'SConscript'))
arduino_home = env.get('ARDUINO_HOME')
-print 'ARDUINO_HOME = ' + env.get('ARDUINO_HOME')
+print('ARDUINO_HOME = ' + env.get('ARDUINO_HOME'))
# Overwrite suffixes and prefixes
if env['HOST_OS'] == 'win32':
else:
env.AppendUnique(CCFLAGS = ['-g'])
+# Force header presence defines
+env.AppendUnique(CPPDEFINES = ['HAVE_ARDUINO_TIME_H'])
+
# BOARD / CPU option
# Get IDE version
-if os.path.exists(arduino_home + '/lib/version.txt'):
- vf = open(arduino_home + '/lib/version.txt', 'r')
- version = vf.readline().replace('.', '')
+if os.path.exists(os.path.join(arduino_home, 'lib', 'version.txt')):
+ vf = open(os.path.join(arduino_home, 'lib', 'version.txt'), 'r')
+ version = vf.readline().replace('.', '').strip()
else:
- print '''
+ print('''
************************************* Error ***********************************
* Can't find version file (lib/version.txt), please check if (%s)
* is arduino root directory. *
*******************************************************************************
-''' % arduino_home
+''' % arduino_home)
Exit(1)
if version[0:2] == '10':
is_1_0_x = True
- boards_info = __parse_config(arduino_home + '/hardware/arduino/boards.txt')
- env.PrependENVPath('PATH', arduino_home + '/hardware/tools/avr/bin/')
+ boards_info = __parse_config(os.path.join(arduino_home, 'hardware', 'arduino', 'boards.txt'))
+ env.PrependENVPath('PATH', os.path.join(arduino_home, 'hardware', 'tools', 'avr', 'bin'))
env.Replace(CC = 'avr-gcc')
env.Replace(CXX = 'avr-g++')
env.Replace(AR = 'avr-ar')
env.Replace(LINK = 'avr-gcc')
env.Replace(RANLIB = 'avr-ranlib')
if target_arch != 'avr':
- print '''
+ print('''
************************************* Error ***********************************
* Arduino 1.0.x IDE only support 'avr', to support other arch at least 1.5.x *
* is required.
*******************************************************************************
-'''
+''')
Exit(1)
else:
is_1_0_x = False
if target_arch == 'avr':
- boards_info = __parse_config(arduino_home + '/hardware/arduino/avr/boards.txt')
- platform_info = __parse_config(arduino_home + '/hardware/arduino/avr/platform.txt')
+ boards_info = __parse_config(os.path.join(arduino_home, 'hardware', 'arduino', 'avr', 'boards.txt'))
+ platform_info = __parse_config(os.path.join(arduino_home, 'hardware', 'arduino', 'avr', 'platform.txt'))
elif target_arch == 'arm':
- boards_info = __parse_config(arduino_home + '/hardware/arduino/sam/boards.txt')
- platform_info = __parse_config(arduino_home + '/hardware/arduino/sam/platform.txt')
+ boards_info = __parse_config(os.path.join(arduino_home, 'hardware', 'arduino', 'sam', 'boards.txt'))
+ platform_info = __parse_config(os.path.join(arduino_home, 'hardware', 'arduino', 'sam', 'platform.txt'))
else:
- print '''
+ print('''
************************************* Error ***********************************
* CPU arch %s isn't supported currently.
*******************************************************************************
-''' % target_arch
+''' % target_arch)
#Board option, let user to select the board
boards = __get_boards(boards_info)
usb_pid = __get_board_info(board, '.pid.0')
if is_1_0_x:
- core_base = arduino_home + '/hardware/arduino/'
+ core_base = os.path.join(arduino_home, 'hardware', 'arduino')
else:
if target_arch == 'avr':
- core_base = arduino_home + '/hardware/arduino/avr/'
+ core_base = os.path.join(arduino_home, 'hardware', 'arduino', 'avr')
else:
- core_base = arduino_home + '/hardware/arduino/sam/'
+ core_base = os.path.join(arduino_home, 'hardware', 'arduino', 'sam')
-variant_folder = core_base + 'variants/' + variant
+variant_folder = os.path.join(core_base, 'variants', variant)
env.AppendUnique(CPPPATH = [variant_folder])
core = __get_board_info(board, '.build.core')
-core_folder = core_base + 'cores/' + core + '/'
+core_folder = os.path.join(core_base, 'cores', core)
env.AppendUnique(CPPPATH = [core_folder])
if is_1_0_x:
else:
cpu_flag = '-mcpu=' + mcu
- comm_flag = [cpu_flag, '-DF_CPU=' + f_cpu, '-DARDUINO=' + version, '-DARDUINO_' + __get_board_info(board, '.build.board'), '-std=c99']
+ 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)
env.Replace(CXX = platform_info.get('compiler.cpp.cmd'))
env.Replace(AR = platform_info.get('compiler.ar.cmd'))
if target_arch == 'arm':
- env.AppendUnique(CPPPATH = [arduino_home + '/hardware/arduino/sam/system/libsam',
- arduino_home + '/hardware/arduino/sam/system/CMSIS/CMSIS/Include/',
- arduino_home + '/hardware/arduino/sam/system//CMSIS/Device/ATMEL'])
+ env.AppendUnique(CPPPATH = [os.path.join(arduino_home, 'hardware', 'arduino', 'sam', 'system', 'libsam'),
+ os.path.join(arduino_home, 'hardware', 'arduino', 'sam', 'system', 'CMSIS', 'CMSIS', 'Include'),
+ os.path.join(arduino_home, 'hardware', 'arduino', 'sam', 'system', '', 'CMSIS', 'Device', 'ATMEL')])
env.AppendUnique(ASFLAGS = ['-x', 'assembler-with-cpp'])
env.AppendUnique(ASFLAGS = comm_flag)
env.AppendUnique(CFLAGS = Split(platform_info.get('compiler.c.flags')))
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' + os.path.join(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:
if variant_folder.find(' ') >= 0:
variant_folder = '"' + variant_folder + '"'
env.Replace(LINKCOM = '$LINK -o $TARGET $_LIBDIRFLAGS $LINKFLAGS $SOURCES $_LIBFLAGS '
- + variant_folder + '/' + variant_system_lib + ' -Wl,--end-group')
+ + os.path.join(variant_folder, variant_system_lib) + ' -Wl,--end-group')
else:
env.Replace(LINKCOM = '$LINK -o $TARGET $_LIBDIRFLAGS $LINKFLAGS $SOURCES $_LIBFLAGS -Wl,--end-group')
else:
env.AppendUnique(LIBS = 'm')
env.Replace(ARCOM = '$AR ' + platform_info.get('compiler.ar.flags') + ' $TARGET $SOURCES')
+# Make sure the .d files are removed when clean the build
+if env.GetOption('clean'):
+ dfs = __search_files(env.get('BUILD_DIR'), '*.d')
+ for df in dfs:
+ Execute(Delete(df))
__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