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

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.

https://budgetlightforum.com/t/-/44344/87

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?

Channels.h
ModeSets.h
NarsilM.atsln
NarsilM.c
NarsilM.componentinfo.xml
NarsilM.cproj
OpenIssues.txt
RampingTables.h
RegisterSettings.h
Rev History - Narsil pre M.txt
Rev History.txt
Setups.h
Setups-1Chan.h
Setups-31S.h
Setups-32S.h
Setups-Q8.h
TechNotes.txt
tk-calibMTN17DDm.h
tk-calibWight.h
tk-delay.h
tk-random.h

Channels.h
ModeSets.h
NarsilM.c

RampingTables.h
RegisterSettings.h

Setups-32S.h

tk-calibMTN17DDm.h
tk-calibWight.h
tk-delay.h
tk-random.h

Thanks

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?

Yeah, my first .hex compiled successfully.
7736 Bytes

But the file size is not the same
7028 Bytes

Keep trying……

That should work. Just flash the chip and test it out…

Hello, friends!
Please help me compile the firmware Anduril for BLF_LT1.
I keep getting this error:
Error 1 expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘battcheck_state’.
I am using AVR Studio 5.1.
Also used Amel Studio 7 and got a lot of errors there.
I am new to programming.
Perhaps I forgot to add a file or the required library is missing.
The firmware from the “bistro-HD” starter topic is compiled without errors.
I’m sure it’s very easy for most of you here.

Atmel Studio 7 gives these errors:

Error unknown type name ‘Event’
https://drive.google.com/file/d/1vmq305jUO-yFRdCcLP54kuT6RGFc32Z\_/view?usp=sharing

Did you bring in all the spaghetti monster files? Anduril and A2 are SM apps, using the SM engine. "Event" is defined in fsm-events.h. I just bring in all the fsm files, anduril2 files, and cfg's and hwdef's into one folder, one project. Many .c files are actually compiled in with include's, so you can include them in the solution, but then you have to specify the "Build Action" as "None". Works like a charm. I use the latest Microchip Studio for building A2 (Anduril 2) for all 3 MCU types with 3 different solutions - works great.

Anduril is D-E-D, dead, not touched in over a year. Anduril 2 is the active project.

Thank you friend for your help!
I did it!
I am very grateful to you!