NovicesFirmwareGuide: learn to use existing code for your custom builds (WIP) <!--break-->

Building flashlights is a fun and dynamic hobby that's rewarding and addictive. When I first started I learned about all the different BUDGET hosts available and then the materials and processes needed to start building. I built myself a few lights and then had to build some for my family and friends and co-workers, and then their family and friends and co-workers. I've progressed from upgrading an emmiter to complete overhauls and building (host) lights from scratch with readily available parts from various sources. Its been a challenge and its been fun but for the last year of so I've been digging deeper and deeper into the electronics side of it all. This has been my biggest challenge so far with this hobby and I feel like I've probably learned about as much as I'm going to without enrolling in the local college.

So, for a while now I've been wanting to start a thread where the PROS can school the weekend warriors on, not writing code but just the knowledge necessary to navigate and alter existing code for a particular build (maybe do a few examples using the new bistro-HD , like for a 1s and 2s+ clicky, 1s and 2s+ E-switch, combo E-switch + clicky 1s and 2s+, and with or without LDO) and using Atmel studio 7 to make it happen.

The new Bisto-HD firmware has sent me over the edge and I just gotta learn how to use this stuff and I'm sure others would love to learn also.

I have spent many hours scouring BLF for all the info regarding this subject and what info is available is pretty scattered and burried under tons of other info. The best place I've found to get started with the basics is Hoop's guide on how to flash attiny 13 . Within the OP of that thread is also a link to comfychairs how to:use atmel avr studio 5 to make a hex file and Warhawk-AVG's howto: .

These are all awesome resources and are what got me where I am now (I can flash a precompiled H file as long as I can figure out the proper command prompt) but I'm still pretty much lost when someone says "comment out" READ_VOLTAGE_FROM_DIVIDER. I don't know how to "comment out", "enable", "disable", "define" or properly compile a HEX using an existing C source.

Also, these resources are becoming a bit dated. We've progressed from hacking najang's with attiny 13's to now having multiple custom boards and firmwares using attiny 25's, 45's, & 85"s, and the tools for using the firmware (Atmel studio) have advanced.

Those Howto's refer to atmel studio 5 and I haven't been able to get 5 to work on my computer, only 7. I"m unable to properly compile from the C source using Atmel studio 7. I always have a bunch of errors that I have no clue on how to fix.

So I guess the goal here is to hopefully get "the pros" to chime in and help us "weekend warriors" out when they can.

1) how to: flash Hoop's guide , WarHawk-AVG's guide

2) how to: compile a hex file using Atmel studio 7?????

3) how to: "comment out", "enable", "disable" and "define" portions of code and when and why its needed

4) flashlight setup examples with triple and quadruple channel boards,2cell+, momentary, LDO, Narsil and Bistro-HD

5) how to: what, when, where, why and how: modegroups.hex, delay.hex, calibration.hex, tk-attiny.hex, tk-voltage.hex, tk-random.hex, make files, D files, EEP files, ELF file, MAP file, SREC file




I'll try to compress what information is gathered over time to keep it all in easy reach.

I realize this could be alot of work but hopefully it'll be easy with as many gurus as there are here.

To compile a hex file in Amtel Studio 7 is the same as in 5

The how to do in Amtsl Studio 5 can lead to errors if the file also needs to load information from the config files, you see more files in the Bistro HD folder

You can simply load the bistroHD.c file
And then start from build in the how to create hex in studio 5
The studio will have also the other files

Rightclick on solution explorer on the right side
Click on add new project,
Remove the main.c
Add existing Item BistroHD.c and the other files
from there you can use the how to in the blf topic from comfychair

Comment out or disable just add // in front
Enable remove //

For different pwm channels you need to enable or disable the channels in top section of the file, read the comments on the code lines they tell you what to do

There is only one hex file!
The Amtel studio loads from all those config files what it needs and build a flash code in the hex file

If you got the hex file you need also use the right fuses to flash the driver, most will have add a .bat file to do so

new project

select your Chip

if you use a different than the 25 you need to change that in the BistroHD.c file as well

delete the main.c

getting the files loaded

load all the files for the solution

building the hex file

here you find the hex files

the text file is from texas Bistro including the whole command prompt to flash

here is the whole project for Amtel Studio 7
including the Attiny25 hex file

Okay, wow that was an eye opener for sure and those screenshots are great! I was able to figure out where I was going wrong and also I understand the enable and disable deal now.

I'm starting to understand more of what I'm reading in the files and at the same time I realize there's more that I need to nail down with the different driver configurations so I know what to turn off/on.

So, I was able to correctly load all the files for the first time but that's as far as I went because I started really reading the code trying to decipher how it's setup at default.

Thanks Lexel for taking the time to post.

I'm having trouble with the command for NarsilTriple v1-3.

When I enter -p attiny85 -c usbasp -u -Uflash:w:\Tiny254585Projects\NarsilTriple\NarsilTriple\Release\NarsilTriple.hex:a -p t85 -c usbasp -Ulfuse:w:0xe2:m -Uhfuse:w:0xde:m -Uefuse:w:0xff:m,

I get '-p' is not recognized as an internal or external command, operable program or batch file.

I just want to say “Thanks” to RotorHead64 for starting this thread. I’m not there yet to need this, but I feel it won’t be long, as I’m getting tired of the UI options that can be bought pre-loaded. Also thanks to Lexel for the excellent post above, and to others who happen to add to this thread in the future. I’m also hoping to hear from guys like Texas_Ace (and lots of others), who don’t write code but have figured out how to tweak stuff to their liking.

Thanks and +1!

I figure anyone climbing the ladder of flashlight modifications will eventually find themselves here trying to figure this stuff out.

open the command prompt in the folder your hex files is located

then prompt
avrdude -p attiny85 -c usbasp -u -Uflash:w:NarsilTriple.hex:a -U lfuse:w:0xe2:m -U hfuse:w:0xde:m -U efuse:w:0xff:m

Thanks Lexel. That worked :beer:

"There can never be an instruction manual for everything anyone might want to do to a code. At some point you have to have look through the actual program lines and understand what they do. If you're not sure exactly what wake_time_med does, you press cntrl-f, type wake_time_med, and find every place it's used and see." (Flintrock)

Just putting this here so I'll remember how to do this if I ever need it. Good stuff

haha, cntrl-f is pretty universal, added tip you select to search in whole solution, to find where it's used in headers. But I thought that was just me defending my ugly programming, lol. Really though this embedded programming doesn't lend itself well to pretty code. You need a bunch of preprocessor stuff where normally you'd probably just make it a run-time choice. It's hard to organize code well into files in normal ways because you need to be able to switch between in-lining functions or not in-lining them (you can't inline a function from a separate c file), and you just generally do things to save space that aren't always pretty. At least those are my excuses.

I'm thinking of a "How to compile a hex WITHOUT atmel studio" guide (on windows).

Although with people talking about the latest chips I'd have to figure out where to find the headers for them. Not even sure AS7 ships with them by default, but at least there should be add-in packages available.

To add to lexel's post for HD, you can alternatively select to use the included Makefile in the Project->Properties->build. But it will make a BUCNCH of hex's unless you un-comment a ONE_BUILD line in it. Also you can't use the debugger in that case. It will keep trying to recompile because it doesn't see the output it's expecting.

However, you may get better results with the Makefile. I think I've chosen pretty good compile flags. I'm getting smaller builds from the Makefile, although I'm sure it's because I forgot some option in studio. It should be possible to make them the same. Just take note that the Makefile places hex's in hex/ and AS will place them somwhere like Release/bin or something like that. So don't flash the wrong one.

Flintrock, could you, or someone else, explain what a makefile is, when and why it’s needed, and how to use it? An in depth description of its philosophy is not needed, but a brief summary of the essential information. Anything would help really…

A little short on time to edit this well. Bascially it has instructions for how to create one or several "targets", and what each target depends on. In turn there may be instructions where that dependency is the "target" telling how to construct the dependency. Most targets and dependencies are files, and the ultimate goal is usually to create one or a few executable, or something like one (in this case hex files). Each instruction set is just a set of standard windows command-line commands, but the most featured command is usually the compiler command, or sometimes the linker (used to combine several compiled files into an exectuable).

There is much nice variable manipulation, and this makefile has lists of all firmware compatible with each attiny and builds hexes for each firmware for each attiny equal or greater to that. It also builds battcheck variations of each build.

The Makefile is capable of sending #define configs to the compiler and they override the configs in the source code. This is how the battcheck builds are made. It overrides the VOLTAGE_CAL (I think that's it) option and otherwise remakes the same configuration.

It also makes the preprocessor files, which shows what the C code looks like after all the configurations are applied and the #if's are worked out, a simplified version of the final code for each configuration (but may be hard for novices to understand some of the expanded macros in that format).

It also finally checks the sizes of all the files built and creates a summary.

It's last trick is it knows how to pack the results up into a zip for me to post, but you have to run it with a different command "make packit".

Generally you can tell a makefile what target you want it to make. The default is "all" and all is defined as some list of other targets.

In Atmel studio under build you just tell it use the makefile and uses that instead of its own rules for compiling. Oh, since the Makefile has pretty good compiler options in it I think, it will likely beat your Atmel defaults by a little.

Oh yes, the very last trick is this Makefile should work without Atmel Studio (I'm pretty sure), if you install WinAVR, just by double clicking the included bat file. Actually you can use the double click method with AS instaleed and without WinAVR installed, but you need to modify the bat file and remove the WinAVR related options. Hmm, I should probably include two bat files.

So the makefile is a way to easily edit options without having to find all references in the .c file? And without needed to adjust local compiler build settings? And this is only required for those that want to more deeply customize the origional code?

First don't worry about too much. It's just an alternative tool to compile the code for most causal modders, and maybe your questions point that out.

I'd say the biggest use if any for most people other than developers is yeah, getting optimal build options without figuring them out in AS, including even the device choice, and you get the battcheck build automatically too. Most people would want to edit the Makefile and un-comment a #ONE_BUILD option but the biggest use for me (probably not for you) is being able to compile all the different configurations at once, because I make sure they all still compile.

I wouldn't put easily editing options as high on list, except for a few key options, specifically which config files to include, and some standard variations on the builds for automation, or maybe just to avoid duplicating configurations that are almost the same, but one option differs. I wouldn't try to replace the config files with the Makefile though. I might use it to override TA's new reduced turbo mode without needing to edit or duplicate his config file. That's more useful for me though because I need to keep my config files in stock distributable form. Others can just change it.

Another potential use is not needing to import a project into AS at all. I find that process a little annoying for people who just want to edit a few lines. You can actually double click a .c or .h file to just use AS's editor without going through all the mess of creating a new project. Of course you can use a bunch of other code-highlighting text editors too or just notepad even. Then just double click a .bat file to compile. You still need gcc-avr compiler installed, either from winavr or AS (or avrtools really), and for the AS route I should make a new bat file. The most traditional way to compile programs is just edit the code text in any text editor you want, and then type "make" on a command prompt, which uses your Makefile to build it. You don't actually need a development environment like AS to just modify code a little and compile it.

Update with one-click compiling script that uses the Atmel Studio toolchain.

No need to import the project into Atmel Studio to compile. You can still edit files with Atmel Studio or any other editor. The one downside is build errors are not presently parsed. You'll be able to see them in the output window though, possibly requiring scrolling up.

The following are all files in the NarsilM V1.0 root directory.
If I want to compile a NarsilM-32S.hex
When I execute “Add-Existing item …” (load all the files for the solution)
Which of the following files should I add?

Rev History - Narsil pre M.txt
Rev History.txt






NarsilM.c file
Setups * .h file (* what i need)
and all other .h files

Do i need change Setups-32S.h file name to Setups.h?

There are some failed messages.

Read thru NarsilM.c as a guide and define the things you need and uncomment the things you don’t.
Tell me more about your hardware setup. Light, driver, switches?