| 
									
										
										
										
											2014-05-18 23:38:59 +03:00
										 |  |  | # Copyright (C) Ivan Kravets <me@ikravets.com> | 
					
						
							|  |  |  | # See LICENSE for details. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  |     Builder for Atmel AVR series of microcontrollers | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from os.path import join | 
					
						
							| 
									
										
										
										
											2014-08-01 16:19:54 +03:00
										 |  |  | from time import sleep | 
					
						
							| 
									
										
										
										
											2014-05-18 23:38:59 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-18 20:11:52 +02:00
										 |  |  | from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Default, | 
					
						
							| 
									
										
										
										
											2015-02-23 19:01:03 +02:00
										 |  |  |                           DefaultEnvironment, SConscript) | 
					
						
							| 
									
										
										
										
											2014-07-27 22:42:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-27 23:59:20 +02:00
										 |  |  | from platformio.util import get_serialports | 
					
						
							| 
									
										
										
										
											2014-07-27 22:42:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-01 16:19:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-23 19:01:03 +02:00
										 |  |  | def BeforeUpload(target, source, env):  # pylint: disable=W0613,W0621 | 
					
						
							| 
									
										
										
										
											2015-02-16 23:16:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _rpi_sysgpio(path, value): | 
					
						
							| 
									
										
										
										
											2014-08-01 16:19:54 +03:00
										 |  |  |         with open(path, "w") as f: | 
					
						
							|  |  |  |             f.write(str(value)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-29 20:29:24 +03:00
										 |  |  |     if "micronucleus" in env['UPLOADER']: | 
					
						
							|  |  |  |         print "Please unplug/plug device ..." | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-04 13:58:20 +02:00
										 |  |  |     upload_options = env.get("BOARD_OPTIONS", {}).get("upload", {}) | 
					
						
							| 
									
										
										
										
											2015-02-16 23:16:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-29 20:29:24 +03:00
										 |  |  |     if "usb" in env.subst("$UPLOAD_PROTOCOL"): | 
					
						
							|  |  |  |         upload_options['require_upload_port'] = False | 
					
						
							|  |  |  |         env.Replace(UPLOAD_SPEED=None) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-04 13:58:20 +02:00
										 |  |  |     if env.subst("$UPLOAD_SPEED"): | 
					
						
							| 
									
										
										
										
											2015-06-17 13:35:10 +03:00
										 |  |  |         env.Append(UPLOADERFLAGS=[ | 
					
						
							|  |  |  |             "-b", "$UPLOAD_SPEED", | 
					
						
							|  |  |  |             "-D" | 
					
						
							|  |  |  |         ]) | 
					
						
							| 
									
										
										
										
											2015-03-04 13:58:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if not upload_options.get("require_upload_port", False): | 
					
						
							|  |  |  |         return | 
					
						
							| 
									
										
										
										
											2015-03-04 21:06:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-04 13:58:20 +02:00
										 |  |  |     env.AutodetectUploadPort() | 
					
						
							|  |  |  |     env.Append(UPLOADERFLAGS=["-P", "$UPLOAD_PORT"]) | 
					
						
							| 
									
										
										
										
											2015-03-04 21:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-01 16:19:54 +03:00
										 |  |  |     if env.subst("$BOARD") == "raspduino": | 
					
						
							| 
									
										
										
										
											2015-02-16 23:16:48 +02:00
										 |  |  |         _rpi_sysgpio("/sys/class/gpio/export", 18) | 
					
						
							|  |  |  |         _rpi_sysgpio("/sys/class/gpio/gpio18/direction", "out") | 
					
						
							|  |  |  |         _rpi_sysgpio("/sys/class/gpio/gpio18/value", 1) | 
					
						
							| 
									
										
										
										
											2014-08-01 16:19:54 +03:00
										 |  |  |         sleep(0.1) | 
					
						
							| 
									
										
										
										
											2015-02-16 23:16:48 +02:00
										 |  |  |         _rpi_sysgpio("/sys/class/gpio/gpio18/value", 0) | 
					
						
							|  |  |  |         _rpi_sysgpio("/sys/class/gpio/unexport", 18) | 
					
						
							| 
									
										
										
										
											2015-03-04 13:58:20 +02:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2014-12-28 20:41:21 +02:00
										 |  |  |         if not upload_options.get("disable_flushing", False): | 
					
						
							|  |  |  |             env.FlushSerialBuffer("$UPLOAD_PORT") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         before_ports = [i['port'] for i in get_serialports()] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-23 01:06:50 +02:00
										 |  |  |         if upload_options.get("use_1200bps_touch", False): | 
					
						
							| 
									
										
										
										
											2014-12-28 20:41:21 +02:00
										 |  |  |             env.TouchSerialPort("$UPLOAD_PORT", 1200) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if upload_options.get("wait_for_upload_port", False): | 
					
						
							|  |  |  |             env.Replace(UPLOAD_PORT=env.WaitForNewSerialPort(before_ports)) | 
					
						
							| 
									
										
										
										
											2014-08-01 16:19:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-18 20:11:52 +02:00
										 |  |  | env = DefaultEnvironment() | 
					
						
							| 
									
										
										
										
											2015-02-23 21:02:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | SConscript(env.subst(join("$PIOBUILDER_DIR", "scripts", "baseavr.py"))) | 
					
						
							| 
									
										
										
										
											2015-02-18 20:11:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 18:53:20 +02:00
										 |  |  | if "digispark" in env.get( | 
					
						
							|  |  |  |         "BOARD_OPTIONS", {}).get("build", {}).get("core", ""): | 
					
						
							| 
									
										
										
										
											2015-02-23 22:36:21 +02:00
										 |  |  |     env.Replace( | 
					
						
							|  |  |  |         UPLOADER=join("$PIOPACKAGES_DIR", "tool-micronucleus", "micronucleus"), | 
					
						
							|  |  |  |         UPLOADERFLAGS=[ | 
					
						
							|  |  |  |             "-c", "$UPLOAD_PROTOCOL", | 
					
						
							|  |  |  |             "--timeout", "60" | 
					
						
							|  |  |  |         ], | 
					
						
							| 
									
										
										
										
											2015-03-04 13:58:20 +02:00
										 |  |  |         UPLOADHEXCMD='"$UPLOADER" $UPLOADERFLAGS $SOURCES' | 
					
						
							| 
									
										
										
										
											2015-02-23 22:36:21 +02:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | else: | 
					
						
							|  |  |  |     env.Replace( | 
					
						
							|  |  |  |         UPLOADER=join("$PIOPACKAGES_DIR", "tool-avrdude", "avrdude"), | 
					
						
							|  |  |  |         UPLOADERFLAGS=[ | 
					
						
							|  |  |  |             "-v", | 
					
						
							|  |  |  |             "-p", "$BOARD_MCU", | 
					
						
							|  |  |  |             "-C", | 
					
						
							|  |  |  |             '"%s"' % join("$PIOPACKAGES_DIR", "tool-avrdude", "avrdude.conf"), | 
					
						
							|  |  |  |             "-c", "$UPLOAD_PROTOCOL" | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         UPLOADHEXCMD='"$UPLOADER" $UPLOADERFLAGS -U flash:w:$SOURCES:i', | 
					
						
							|  |  |  |         UPLOADEEPCMD='"$UPLOADER" $UPLOADERFLAGS -U eeprom:w:$SOURCES:i' | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-02-18 20:11:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 23:38:59 +03:00
										 |  |  | # | 
					
						
							|  |  |  | # Target: Build executable and linkable firmware | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-09 12:22:24 +02:00
										 |  |  | target_elf = env.BuildFirmware() | 
					
						
							| 
									
										
										
										
											2014-05-18 23:38:59 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Target: Extract EEPROM data (from EEMEM directive) to .eep file | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-27 22:42:04 +03:00
										 |  |  | target_eep = env.Alias("eep", env.ElfToEep(join("$BUILD_DIR", "firmware"), | 
					
						
							|  |  |  |                                            target_elf)) | 
					
						
							| 
									
										
										
										
											2014-05-18 23:38:59 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Target: Build the .hex file | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-27 22:42:04 +03:00
										 |  |  | if "uploadlazy" in COMMAND_LINE_TARGETS: | 
					
						
							| 
									
										
										
										
											2015-02-18 20:11:52 +02:00
										 |  |  |     target_firm = join("$BUILD_DIR", "firmware.hex") | 
					
						
							| 
									
										
										
										
											2014-07-27 22:42:04 +03:00
										 |  |  | else: | 
					
						
							| 
									
										
										
										
											2015-02-18 20:11:52 +02:00
										 |  |  |     target_firm = env.ElfToHex(join("$BUILD_DIR", "firmware"), target_elf) | 
					
						
							| 
									
										
										
										
											2014-05-18 23:38:59 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-09 19:57:39 +02:00
										 |  |  | # | 
					
						
							|  |  |  | # Target: Print binary size | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | target_size = env.Alias("size", target_elf, "$SIZEPRINTCMD") | 
					
						
							|  |  |  | AlwaysBuild(target_size) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 23:38:59 +03:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2014-07-27 22:42:04 +03:00
										 |  |  | # Target: Upload by default .hex file | 
					
						
							| 
									
										
										
										
											2014-05-18 23:38:59 +03:00
										 |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-23 19:01:03 +02:00
										 |  |  | upload = env.Alias(["upload", "uploadlazy"], target_firm, | 
					
						
							|  |  |  |                    [BeforeUpload, "$UPLOADHEXCMD"]) | 
					
						
							| 
									
										
										
										
											2014-07-27 22:42:04 +03:00
										 |  |  | AlwaysBuild(upload) | 
					
						
							| 
									
										
										
										
											2014-05-18 23:38:59 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2014-07-27 22:42:04 +03:00
										 |  |  | # Target: Upload .eep file | 
					
						
							| 
									
										
										
										
											2014-05-18 23:38:59 +03:00
										 |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-27 22:42:04 +03:00
										 |  |  | uploadeep = env.Alias("uploadeep", target_eep, [ | 
					
						
							| 
									
										
										
										
											2015-02-23 19:01:03 +02:00
										 |  |  |     BeforeUpload, "$UPLOADEEPCMD"]) | 
					
						
							| 
									
										
										
										
											2014-07-27 22:42:04 +03:00
										 |  |  | AlwaysBuild(uploadeep) | 
					
						
							| 
									
										
										
										
											2014-05-18 23:38:59 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-27 22:42:04 +03:00
										 |  |  | # | 
					
						
							|  |  |  | # Setup default targets | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-18 20:11:52 +02:00
										 |  |  | Default([target_firm, target_size]) |