2 # This script includes arduino specific config
12 if not os.path.isfile(f):
16 strs = file.readlines()
19 if len(str) > 0 and str[0] == '#':
24 dict.setdefault(str[0:idx], str[idx + 1:])
28 def __get_boards(dict):
32 idx = key.find('.name')
34 if key.endswith('.name'):
35 boards.append(key[0:idx])
38 def __get_cpu(dict, board):
42 idx = key.find(board + '.menu.cpu.')
43 start = len(board + '.menu.cpu.')
45 end = key.find('.', start)
58 def __get_board_info(board, key):
60 v = boards_info.get(board + '.menu.cpu.' + cpu + key)
62 v = boards_info.get(board + key)
64 v = boards_info.get(board + key)
67 def __search_files(path, pattern, ondisk=True, source=True, strings=False, recursive=True):
69 return Glob(pattern, ondisk, source, strings)
72 for root, dirnames, filenames in os.walk(path):
73 matches.extend(Glob(root + '/' + pattern, ondisk, source, strings))
77 # To make sure the src is built in 'BUILD_DIR' (by default it will be built at
78 # the same directory as the .c .cpp .S)
79 def __src_to_obj(env, srcs):
81 prefix = env.get('BOARD') + '_'
83 prefix += env.get('CPU') + '_'
85 build_dir = env.get('BUILD_DIR') + '/arduino/'
87 obj = src.path.replace(arduino_home, build_dir)
90 if env.get('OBJSUFFIX'):
91 obj += env.get('OBJSUFFIX')
92 objs.extend(env.Object(obj, src, OBJPREFIX=prefix))
95 def __import_lib(env, lib):
96 lib_path = arduino_home + '/libraries/' + lib
97 if not os.path.exists(lib_path):
98 if target_arch == 'avr':
99 lib_path = arduino_home + '/hardware/arduino/avr/libraries/' + lib
101 lib_path = arduino_home + '/hardware/arduino/sam/libraries/' + lib
103 if os.path.exists(lib_path + '/src'):
104 lib_path = lib_path + '/src'
106 env.AppendUnique(CPPPATH = [lib_path])
108 if os.path.exists(lib_path + '/utility'):
109 env.AppendUnique(CPPPATH = [lib_path + '/utility'])
112 lib_src.extend(__search_files(lib_path, '*.S'))
113 lib_src.extend(__search_files(lib_path, '*.c'))
114 lib_src.extend(__search_files(lib_path, '*.cpp'))
116 build_dir = env.get('BUILD_DIR')
118 lib_a = env.StaticLibrary(build_dir + lib, __src_to_obj(env, lib_src))
120 lib_a = env.StaticLibrary(lib, __src_to_obj(env, lib_src))
121 env.AppendUnique(LIBS = [File(lib_a[0])])
123 def __build_core(env):
124 core_src = __search_files(core_folder, '*.S')
125 core_src.extend(__search_files(core_folder, '*.c'))
126 core_src.extend(__search_files(core_folder, '*.cpp'))
128 core_src.extend(__search_files(variant_folder, '*.S'))
129 core_src.extend(__search_files(variant_folder, '*.c'))
130 core_src.extend(__search_files(variant_folder, '*.cpp'))
132 core_obj = __src_to_obj(env, core_src)
133 build_dir = env.get('BUILD_DIR')
135 s_core = env.StaticLibrary(build_dir + 'core', core_obj)
137 s_core = env.StaticLibrary('core', core_obj)
138 env.AppendUnique(LIBS = [File(s_core[0])])
140 # To avoid compiler issue. Otherewise there may be warnings:
141 # undefined reference to '_exit' '_close', '_getpid' ...
142 # Above functions are used in libc.a and implemented in syscalls_sam3.c
143 if env.get('TARGET_ARCH') == 'arm':
145 if obj.name.endswith('syscalls_sam3.o'):
146 env.AppendUnique(LIBS = [File(obj)])
148 def __create_bin(env, source):
150 if env.get('TARGET_ARCH') == 'avr':
151 eep = env.Command(name + '.eep', source, 'avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 $SOURCE $TARGET')
152 hex = env.Command(name + '.hex', source, 'avr-objcopy -O ihex -R .eeprom $SOURCE $TARGET')
154 hex = env.Command(name + '.hex', source, 'arm-none-eabi-objcopy -O binary $SOURCE $TARGET')
156 # Print the command line that to upload binary to the board
157 def __upload_help(env):
158 if target_arch == 'avr':
159 protocol = __get_board_info(board, '.upload.protocol')
160 speed = __get_board_info(board, '.upload.speed')
162 upload_cmd = arduino_home + '/hardware/tools/avr/bin/avrdude -C' + arduino_home +'/hardware/tools/avr/etc/avrdude.conf -v -v -v -v -p' \
163 + mcu + ' -c' + protocol + ' -P<serial_port>' + ' -b' + speed + ' -D -Uflash:w:<hex_file>:i'
165 uu = __get_board_info(board, '.upload.native_usb')
166 upload_cmd = arduino_home + '/hardware/tools/bossac -i -d --port=<serial_port> -U ' + uu + ' -e -w -v -b <bin file> -R'
169 ===============================================================================
170 You can upload the bin file with following command line:
172 Help('\n $ ' + upload_cmd)
174 \nPlease replace <xxx> according to the actual situation.
175 ===============================================================================
178 # ARDUINO_HOME build option
179 help_vars = Variables()
180 help_vars.Add(PathVariable('ARDUINO_HOME', 'ARDUINO root directory', os.environ.get('ARDUINO_HOME')))
181 help_vars.Update(env)
182 Help(help_vars.GenerateHelpText(env))
184 target_arch = env.get('TARGET_ARCH')
185 arduino_home = env.get('ARDUINO_HOME')
188 ************************************* Error ***********************************
189 * Arduino root directory isn't set, you can set enviornment variable *
190 * ARDUINO_HOME or add it in command line as: *
191 * # scons ARDUINO_HOME=<path to arduino root directory> ... *
192 *******************************************************************************
196 # Overwrite suffixes and prefixes
197 if env['HOST_OS'] == 'win32':
198 env['OBJSUFFIX'] = '.o'
199 env['SHOBJSUFFIX'] = '.os'
200 env['LIBPREFIX'] = 'lib'
201 env['LIBSUFFIX'] = '.a'
202 env['SHLIBPREFIX'] = 'lib'
203 env['SHLIBSUFFIX'] = '.so'
204 env['LIBPREFIXES'] = ['lib']
205 env['LIBSUFFIXES'] = ['.a', '.so']
206 env['PROGSUFFIX'] = ''
207 elif platform.system().lower() == 'darwin':
208 env['SHLIBSUFFIX'] = '.so'
209 env['LIBSUFFIXES'] = ['.a', '.so']
210 env['PROGSUFFIX'] = ''
212 # Debug/release relative flags
213 if env.get('RELEASE'):
214 env.AppendUnique(CCFLAGS = ['-Os'])
215 env.AppendUnique(CPPDEFINES = ['NDEBUG'])
217 env.AppendUnique(CCFLAGS = ['-g'])
222 if os.path.exists(arduino_home + '/lib/version.txt'):
223 vf = open(arduino_home + '/lib/version.txt', 'r')
224 version = vf.readline().replace('.', '')
227 ************************************* Error ***********************************
228 * Can't find version file (lib/version.txt), please check if (%s)
229 * is arduino root directory. *
230 *******************************************************************************
234 if version[0:2] == '10':
236 boards_info = __parse_config(arduino_home + '/hardware/arduino/boards.txt')
237 env.PrependENVPath('PATH', arduino_home + '/hardware/tools/avr/bin/')
238 env.Replace(CC = 'avr-gcc')
239 env.Replace(CXX = 'avr-g++')
240 env.Replace(AR = 'avr-ar')
241 env.Replace(AS = 'avr-as')
242 env.Replace(LINK = 'avr-gcc')
243 env.Replace(RANLIB = 'avr-ranlib')
244 if target_arch != 'avr':
246 ************************************* Error ***********************************
247 * Arduino 1.0.x IDE only support 'avr', to support other arch at least 1.5.x *
249 *******************************************************************************
254 if target_arch == 'avr':
255 boards_info = __parse_config(arduino_home + '/hardware/arduino/avr/boards.txt')
256 platform_info = __parse_config(arduino_home + '/hardware/arduino/avr/platform.txt')
257 elif target_arch == 'arm':
258 boards_info = __parse_config(arduino_home + '/hardware/arduino/sam/boards.txt')
259 platform_info = __parse_config(arduino_home + '/hardware/arduino/sam/platform.txt')
262 ************************************* Error ***********************************
263 * CPU arch %s isn't supported currently.
264 *******************************************************************************
267 #Board option, let user to select the board
268 boards = __get_boards(boards_info)
269 help_vars = Variables()
270 help_vars.Add(EnumVariable('BOARD', 'arduino board', boards[0], boards))
271 help_vars.Update(env)
272 Help(help_vars.GenerateHelpText(env))
275 board = env.get('BOARD')
276 cpus = __get_cpu(boards_info, board)
278 help_vars = Variables()
279 help_vars.Add(EnumVariable('CPU', 'arduino board cpu', cpus[0], cpus))
280 help_vars.Update(env)
281 Help(help_vars.GenerateHelpText(env))
283 # Arduino commom flags
285 board = env.get('BOARD')
286 mcu = __get_board_info(board, '.build.mcu')
287 f_cpu = __get_board_info(board, '.build.f_cpu')
288 usb_vid = __get_board_info(board, '.build.vid')
289 usb_pid = __get_board_info(board, '.build.pid')
290 variant = __get_board_info(board, '.build.variant')
293 usb_vid = __get_board_info(board, '.vid.0')
295 usb_pid = __get_board_info(board, '.pid.0')
298 core_base = arduino_home + '/hardware/arduino/'
300 if target_arch == 'avr':
301 core_base = arduino_home + '/hardware/arduino/avr/'
303 core_base = arduino_home + '/hardware/arduino/sam/'
305 variant_folder = core_base + 'variants/' + variant
306 env.AppendUnique(CPPPATH = [variant_folder])
308 core = __get_board_info(board, '.build.core')
309 core_folder = core_base + 'cores/' + core + '/'
310 env.AppendUnique(CPPPATH = [core_folder])
315 comm_flags.extend(['-mmcu=' + mcu])
317 comm_flags.extend(['-DF_CPU=' + f_cpu])
318 comm_flags.extend(['-DARDUINO=' + version])
320 comm_flags.extend(['-DUSB_VID=' + usb_vid])
322 comm_flags.extend(['-DUSB_PID=' + usb_pid])
324 env.AppendUnique(ASFLAGS = ['-x', 'assembler-with-cpp'])
325 env.AppendUnique(ASFLAGS = comm_flags)
327 env.AppendUnique(CFLAGS = ['-Os', '-ffunction-sections', '-fdata-sections', '-MMD'])
328 env.AppendUnique(CFLAGS = comm_flags)
330 env.AppendUnique(CXXFLAGS = ['-Os', '-fno-exceptions', '-ffunction-sections', '-fdata-sections','-MMD'])
331 env.AppendUnique(CXXFLAGS = comm_flags)
333 env.AppendUnique(LINKFLAGS = ['-Os'])
334 if mcu == 'atmega2560':
335 env.AppendUnique(LINKFLAGS = ['-Wl,--gc-sections,--relax'])
337 env.AppendUnique(LINKFLAGS = ['-Wl,--gc-sections'])
338 env.AppendUnique(LINKFLAGS = ['-mmcu=' + mcu])
340 if target_arch == 'avr':
341 cpu_flag = '-mmcu=' + mcu
343 cpu_flag = '-mcpu=' + mcu
345 comm_flag = [cpu_flag, '-DF_CPU=' + f_cpu, '-DARDUINO=' + version, '-DARDUINO_' + __get_board_info(board, '.build.board')]
346 if target_arch == 'arm':
347 comm_flag.extend(['-DARDUINO_ARCH_SAM'])
349 comm_flag.extend(['-DARDUINO_ARCH_AVR'])
351 compiler_path = platform_info.get('compiler.path')
352 compiler_path = compiler_path.replace('{runtime.ide.path}', arduino_home)
353 env.PrependENVPath('PATH', compiler_path)
354 env.Replace(CC = platform_info.get('compiler.c.cmd'))
355 env.Replace(CXX = platform_info.get('compiler.cpp.cmd'))
356 env.Replace(AR = platform_info.get('compiler.ar.cmd'))
357 if target_arch == 'arm':
358 env.AppendUnique(CPPPATH = [arduino_home + '/hardware/arduino/sam/system/libsam',
359 arduino_home + '/hardware/arduino/sam/system/CMSIS/CMSIS/Include/',
360 arduino_home + '/hardware/arduino/sam/system//CMSIS/Device/ATMEL'])
361 env.AppendUnique(ASFLAGS = ['-x', 'assembler-with-cpp'])
362 env.AppendUnique(ASFLAGS = comm_flag)
363 env.AppendUnique(CFLAGS = Split(platform_info.get('compiler.c.flags')))
364 env.AppendUnique(CXXFLAGS = Split(platform_info.get('compiler.cpp.flags')))
365 env.AppendUnique(ARFLAGS = Split(platform_info.get('compiler.ar.flags')))
366 env.AppendUnique(CCFLAGS = comm_flag)
368 extra_flags = __get_board_info(board, '.build.extra_flags')
370 extra_flags = extra_flags.replace('{build.usb_flags}', '')
371 env.AppendUnique(CCFLAGS = Split(extra_flags))
372 usb_flags = ['-DUSB_VID=' + usb_vid, '-DUSB_PID=' + usb_pid, '-DUSBCON', '-DUSB_MANUFACTURER="Unknown"']
373 env.AppendUnique(CCFLAGS = usb_flags)
375 if target_arch == 'arm':
376 env.AppendUnique(LINKFLAGS = ['-Os', '-Wl,--gc-sections', cpu_flag,
377 '-T' + variant_folder + '/' + __get_board_info(board, '.build.ldscript'),
378 '-Wl,-Map,' + env.get('BUILD_DIR') + 'arduino_prj.map'])
379 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'))
381 variant_system_lib = __get_board_info(board, '.build.variant_system_lib')
382 if variant_system_lib:
383 if variant_folder.find(' ') >= 0:
384 variant_folder = '"' + variant_folder + '"'
385 env.Replace(LINKCOM = '$LINK -o $TARGET $_LIBDIRFLAGS $LINKFLAGS $SOURCES $_LIBFLAGS '
386 + variant_folder + '/' + variant_system_lib + ' -Wl,--end-group')
388 env.Replace(LINKCOM = '$LINK -o $TARGET $_LIBDIRFLAGS $LINKFLAGS $SOURCES $_LIBFLAGS -Wl,--end-group')
390 env.AppendUnique(LINKFLAGS = Split(platform_info.get('compiler.c.elf.flags')))
391 env.AppendUnique(LINKFLAGS = [cpu_flag])
392 env.AppendUnique(LIBS = 'm')
393 env.Replace(ARCOM = '$AR ' + platform_info.get('compiler.ar.flags') + ' $TARGET $SOURCES')
397 env.AddMethod(__import_lib, "ImportLib") #import arduino library
398 #env.AddMethod(__build_core, "BuildCore") #build arduino core
399 env.AddMethod(__create_bin, "CreateBin") #create binary files(.eep and .hex)
400 env.AddMethod(__upload_help, "UploadHelp") #print the command line that to upload binary to the boardf