tones: keep frequencies in tone tables in 8.8 fixed point format

The phase calculation is already done using fixed point math, so we can
trivially feed it tone frequencies in the same format.  This results in the
tones being closer to the desired frequencies.  E.g., a E6 will sound as
1319 Hz (as it should) instead of 1344 Hz as before.

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
main: do phase math using a uint16_t fixed point

Using integer math for the phase calculation limits the tone frequencies to
rather large discrete steps.

When we represent a frequency as how many wave array steps we make per
sample relative to 1 Hz, we get about 64 possible frequencies (assuming
16384 Hz sample rate and 256 entry wave array):

> c(0:64)*(16384/256)
 [1]    0   64  128  192  256  320  384  448  512  576  640  704  768  832  896
[16]  960 1024 1088 1152 1216 1280 1344 1408 1472 1536 1600 1664 1728 1792 1856
[31] 1920 1984 2048 2112 2176 2240 2304 2368 2432 2496 2560 2624 2688 2752 2816
[46] 2880 2944 3008 3072 3136 3200 3264 3328 3392 3456 3520 3584 3648 3712 3776
[61] 3840 3904 3968 4032 4096

That is, we get frequencies that are multiples of 64 Hz. This are pretty
substantial steps and makes many musical patterns sound out of tune.

Instead of using a uint8_t to describe the frequency and the current phase,
we could use more bits with an implied decimal point some number of bits in.
This would allow us to calculate more accurately, and then simply round the
index or interpolate between two values when doing the wave array lookup.

This commit switches the phase calculation to uint16_t used as a 8.8 fixed
point number.  That is, given a phase x, the real world phase is x / 256.

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Use ARRAY_LEN instead of sizeof to detect end of fixed tone pattern

The two are equivalent for uint8_t, but break if the type changes to
something larger.

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
tones: add a helper macro to convert from Hz to steps

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Use symmetry to store only 1/4 of a sine wave

This shrinks the sine_wave array by 190 bytes, but requires a bit more
complex code to calculate any value which takes up 42 extra bytes of flash.
So, overall we save 148 bytes of flash.

   text	   data	    bss	    dec	    hex	filename
   1530	      2	     33	   1565	    61d	fmfox-atmega48p.elf (before)
   1382	      2	     33	   1417	    589	fmfox-atmega48p.elf (after)

Additionally, this fixes slight offset in the negative half-cycle.
Previously, the mean of the sine wave was 127.5 instead of 128 as one would
expect from a sine wave.

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
gen-eeprom: fix typo in usage message

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Don't open-code mcu_set_clock_prescalar

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Remove redundant code

The most recent merge with avr-common brings us these macros via arch.h.
Therefore the ones in main.c are redundant.

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Merge avr-common

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
arch.h: add ARRAY_LEN

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
arch: add MCU_RAW_FREQ for ATmega48P

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
arch: move most of generic code into arch/generic_*.h

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
arch: add mcu_set_clock_prescalar helper

A more advanced version would disable interrupts if they are enabled to
avoid an interrupt mid-function breaking the back-to-back instruction
requirement.

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
arch: add __set_pin static inline

This is a low-overhead function to set/clear a GPIO pin.

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
arch: add PIN_{INPUT,OUTPUT}_* macros

These can be used to construct the PORT and DDR register values in more
semantically readable way.

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
manual: proofreading pass by Holly

At this point, the manual should be done.

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
manual: add a check for current draw to the step-by-step instructions

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
manual: include the BOM in an appendix

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
manual: minor edit to the construction chapter

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
manual: address remaining datasheet todo items

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Next