Fix double where.
Remove suprious HTML from end of file

avra didn't mind, but avrasm2.exe does not like it
Add a simple BSDmakefile for testing

This "BSDmakefile" works for both gmake
and BSD make (also probably other makes);
so once programming commands are implemented
here it can replace original Makefile.

It requires avra git version containing
a6e8b2957953810dae6467eeb4905bfc5ea6c33e
(Aug 8 2012 or later), so that -o, -e and -d
switches work.
Add .obj files to .gitignore
Assembler can be overriden on the commandline

Now you can do

make clean afro2.hex ASM=avrasm2.exe

or

gmake -f BSDmakefile clean afro2.hex ASM=avrasm2.exe

if you want to use some other assembler instead
of the default avra.
Replace .ifdefs with #ifdefs

avra currently does not care, but avrasm2
treats defined() in the assembler macros (.if)
and defined() in preprocess macros (#if) differently.

Assembler definition is:

http://support.atmel.no/knowledgebase/avrstudiohelp/mergedProjects/AVRASM/Html/expressions.html#DEFINED(symbol

DEFINED(symbol) Returns true if symbol is previously defined using
.equ/.set/.def directives. Normally used in conjunction with .if
directives (.if defined(foo)), but may be used in any context. It
differs from other functions in that parentheses around its argument are
not required, and that it only makes sense to use a single symbol as
argument.

But by the time we reach the assembler, our "-D afro2_esc"
symbol gets expanded by the preprocessor to integer 1.

This causes the following error with avrasm2:

tgy.asm(53): Including file 'm8def.inc'
tgy.asm(85): error: syntax error, unexpected INTEGER, expecting SYMBOL

This can be avoided by using -D afro2_esc=SOMETHING but
this in turn is not (yet) supported by avra, which has no
real C preprocessor yet.

AVRASM2 documentation explains further:

http://support.atmel.no/knowledgebase/avrstudiohelp/mergedProjects/AVRASM/Html/AVR_Assembler_2_-_Known_Issues.htm#defined

* The preprocessor 'defined' keyword only relates to symbols defined
with #define, and correctly does this only in preprocessor conditionals
(#if/#elif).

* In all remaining code, the assembler's notion of defined is used,
the correct behaviour would be to only recognize it in assembler
conditionals (.if/.elif).

So the solution is to change assembler conditionals (.if/.endif)
with C preprocessor conditionals (#if/#endif) which will work
with both assemblers correctly.
Merge remote-tracking branch 'saper/master'

Some fixups for building on BSD and with avrasm2.
BSDmakefile will need a yet-released avra for now,
but I will soon change it to drop all of the includes
in tgy.asm and use "-i" options instead.
Port missing Makefile targets to BSDmakefile.

It still doesn't depend on tgy.asm, so it doesn't quite work yet.
Add explicit dependency on tgy.asm

For BSD make it would be enough to have

 .inc.hex: tgy.asm

but GNU make does not recognize it, therefore
an explicit dependency is added.

In theory explicit dependency is better, since
we could have some .hex files that are built
independently.
Comment and typo fixes.
Simplify and optimize rcp_int by using OCR1B instead of tracking 24 bits.

MAX_RC_PULS * CPU_MHZ easily fits in two bytes anyway, so this just
cleans things up by changing the timeout case to an interrupt and the
common case to not need any range checking, with the same end result.
Stop using 4 registers just to avoid 4 extra cycles in PWM interrupt,
and implement optional complementary PWM.

pwm_off and pwm_on were as optimized as they could possibly get, save one
cycle possible from using "out" instead of "st" on boards that have all
N-channel FETs on the same port. This was just silly compared for how
many registers were needed just to do this. Also, it's not possible to do
complementary this way without stealing yet another 4 registers. Using
bit flags for FETs instead also and makes the commutation routines much
smaller.

Note: No adjustable dead time for complementary PWM yet. Most boards with
all-N-channel FETs but no dedicated gate driver ICs seem to be worst at
the turn-on time for the high side (2-5 microseconds), but everything
else is reasonable.
Use TIFR.OCF1B instead of OCT1B_PENDING. No OCF1B interrupt required.
Support wider calibration range all the way from 0 to MAX_RC_PULS.
Use shorter RC timeout while armed (HK i86 workaround).

The HK i86 flight controller has broken software, as noted here:

http://www.rcgroups.com/forums/showthread.php?t=1669859

This does not fix the problem, but we should time out now before too much
damage is done.
BSDmakefile should work now. Waiting on avra -i option so we can drop the
symlink hack.

Merge remote-tracking branch 'saper/master'
Build up start power to PWR_MAX_START more quickly.
Sync loss avoidance, comp PWM, ZC DC bias cancellation, adjustable timing
advance, and startup and timeout improvements.

Warning: This is a significant commit. I even broke a few FETs because of
some earlier bugs. Test carefully.

Complementary PWM is now supported, ala wii-esc. This has an interesting
effect of having the motor speed more closely and more quickly track the
duty cycle even without any active braking or closed-loop controlling.
Warning: It may break P/N (or other) boards.

Jumping from low to high _speed_ should no longer lose track of timing
because of excessive averaging. This caused problems in some cases even
without ZC tracking issues, such as with the NTM 28-26 1200KV.

Jumping from low to high duty cycle that results in a demagnetization
period that exceeds the blanking window will now be tracked and if
excessive (approaching 30 degrees), PWM will be stopped 30 -
MOTOR_ADVANCE degrees in order to avoid blinding the ZC in the next
commutation. This should (in theory) be smoother than the wii-esc
approach of jumping forward, as the power will only be cut for about 15
degrees. This should solve problems with the expensive and/or pancake
motors that have higher inductance and lower winding resistance such as
the RCTigerMotor MT-3506. Thank you to ziss_dm for the ideas, discussion,
and awesome LTSpice simulation!

Jumping from run to brake to run again (or run to RC timeout to run)
while the motor is spinning quickly will no longer cause sync loss due
to excessive ZC filter length and/or blanking periods. Instead, we look
briefly without power to see if the motor is spinning and align to it
before inducing any demagnetization current or PWM noise.

Starting NTM 28-30 750KV motors should no longer squeal in some cases.
This was a bug where zc_filter_time was not being set during starting.
The long blanking period is instead taken care of by the ZC filtering.

DC bias is now cancelled from the commutation timing. This might help
at very low speeds, but it's probably not that useful.

Timers are now set to absolute values precalculated by update_timing.
This avoids skew from processing overhead. There is still a little bit of
skew from the ZC filtering, but I don't see any way around this. Also,
boards with larger sense filtering capacitors such as the F-30A line will
have slightly droopy timing as RPM increases, but this should not be a
big deal. MOTOR_ADVANCE has changed from 15 to 18 degrees, which looks
almost perfect on the 'scope for everything I have tried with.
Halve ZC filter time when running at 8MHz.
Add tp_8khz target for "type 1" clones that blow up at >8kHz PWM.