Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Last active December 11, 2015 05:18
Show Gist options
  • Save jimweirich/4551220 to your computer and use it in GitHub Desktop.
Save jimweirich/4551220 to your computer and use it in GitHub Desktop.
I wanted to share this, but it's not quite ready to be a project yet. This is a Rake library to build Digispark applications. Currently this only supports DigiSpark arduinos, but when I find my Arduino starter kit, I want to make it work for that as well. Patches and suggestions are welcome.
# Put this file where the Rakefile can require it.
require 'rake/clean'
CLEAN.include("*.o", "*.d", "*.elf")
CLOBBER.include("*.hex", "*.eep", "lib", "libobj")
module Rake
class Arduino
include FileUtils
include Rake::DSL
def self.option(option_name, &block)
ivar = "@#{option_name}"
define_method(option_name) do
instance_variable_get(ivar) || instance_eval(&block)
end
define_method(option_name.to_s + "=") do |value|
instance_variable_set(ivar, value)
end
end
def initialize
yield self
define_support
end
option(:arduino_app) { "/Applications/ArduinoDigispark.app" }
option(:hardware) { "#{arduino_app}/Contents/Resources/Java/hardware" }
option(:avr) { "#{hardware}/tools/avr" }
option(:bin) { "#{avr}/bin" }
option(:compile_flags) {
[
'-c', '-g', '-Os', '-Wall',
'-mmcu=attiny85',
'-DF_CPU=16500000L',
'-MMD',
'-DUSB_VID=null',
'-DUSB_PID=null',
'-DARDUINO=103'
]
}
option(:gpp_compile_flags) { [ '-fno-exceptions', '-ffunction-sections', '-fdata-sections' ] }
option(:gcc_compile_flags) { [ '-ffunction-sections', '-fdata-sections' ] }
option(:gpp_flags) { (compile_flags + gpp_compile_flags).join(' ') }
option(:gcc_flags) { (compile_flags + gcc_compile_flags).join(' ') }
option(:inc_flags) { "-I#{hardware}/tiny-digispark/cores/tiny" }
option(:dude_config) { "#{hardware}/tools/avr/etc/avrdude.conf" }
option(:tiny_lib) { "#{hardware}/tiny-digispark/cores/tiny" }
def gpp(src, obj)
sh "#{bin}/avr-g++ #{gpp_flags} #{inc_flags} #{src} -o #{obj}"
end
def gcc(src, obj)
sh "#{bin}/avr-gcc #{gcc_flags} #{inc_flags} #{src} -o #{obj}"
end
def ar(lib, obj)
sh "#{bin}/avr-ar rcs #{lib} #{obj}"
end
def elf(elf, obj, lib)
sh "#{bin}/avr-gcc -Os -Wl,--gc-sections -mmcu=attiny85 -o #{elf} #{obj} #{lib} -L. -lm"
end
def eep(elf, eep)
sh "#{bin}/avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 #{elf} #{eep}"
end
def hex(elf, hex)
sh "#{bin}/avr-objcopy -O ihex -R .eeprom #{elf} #{hex}"
end
def dude(hex)
sh "#{bin}/avrdude -c#{dude_config} -v -v -v -v -pattiny85 -cdigispark -Pusb -Uflash:w:#{hex}:i"
end
def define_app(app_name)
cpp = "#{app_name}.cpp"
obj = "#{app_name}.cpp.o"
elf = "#{app_name}.cpp.elf"
eep = "#{app_name}.cpp.eep"
hex = "#{app_name}.cpp.hex"
desc "Compile #{app_name}"
file obj => cpp do
gpp(cpp, obj)
end
desc "Link #{app_name} to object file"
file elf => [obj, "lib/core.a"] do
elf(elf, obj, "lib/core.a")
end
desc "Build #{app_name} eep file"
file eep => [elf] do
eep(elf, eep)
end
desc "Build #{app_name} hex file"
file hex => [elf] do
hex(elf, hex)
end
desc "Build then #{app_name} application"
task app_name => [hex, eep]
task :all_apps => app_name
end
def lib_src
@lib_src ||= FileList["#{tiny_lib}/*.c", "#{tiny_lib}/*.cpp"]
end
def lib_obj
@lib_obj ||= lib_src.pathmap("libobj/%f.o")
end
def define_support
desc "Build all Arduino applications"
task :all_apps
directory "libobj"
lib_src.zip(lib_obj).each do |src, obj|
file obj => ["libobj", src] do
if src =~ /\.c$/
gcc(src, obj)
else
gpp(src, obj)
end
end
end
directory "lib"
file 'lib/core.a' => ["lib"] + lib_obj do
rm 'lib/core.a' rescue nil
lib_obj.each do |obj|
ar("lib/core.a", obj)
end
end
task :upload, [:app] do |t, args|
app_name = args.app
fail "Usage: rake upload[APPNAME]" unless app_name
app_task = Rake::Task["#{app_name}.cpp.hex"]
fail "Unknown App: '#{app_name}'" unless app_task
app_task.invoke
dude("#{app_name}.cpp.hex")
end
end
end
end
# Sample Rakefile for the Digispark/Arduino build script
require './arduino'
# Create an arduino builder
Avr = Rake::Arduino.new do |cfg|
# There are lots of configuration options, but I don't know
# yet what ones will survive. At the very least you must
# specify the location of the of the Arduino IDE application
# that comes with the special DigiSpark support.
cfg.arduino_app = "/Applications/ArduinoDigispark.app"
end
# Now define some applications
Avr.define_app("Blink")
Avr.define_app("HelloCharlie")
Avr.define_app("LedBlend")
# Some of the targets defined are listed below. See
# "rake -T" for a list of all the targets.
#
# rake Blink -- Compile the Blink application
# rake upload[Blink] -- Upload the Blink application
# -- If the app is not compile, compile it first
# rake lib/core.a -- Build the shared library
# rake all_apps -- Build all the apps defined in the Rakefile
desc "Build all the Arduino apps"
task :default => :all_apps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment