Started following some tutorial on att85 usi. Downloaded example code from make avr book.

This commit is contained in:
Dan
2022-09-20 01:08:01 -04:00
parent d0cbc0000e
commit 361a828c46
295 changed files with 68746 additions and 0 deletions

View File

@@ -0,0 +1,196 @@
##########------------------------------------------------------##########
########## Project-specific Details ##########
########## Check these every time you start a new project ##########
##########------------------------------------------------------##########
MCU = atmega168p
F_CPU = 8000000UL
BAUD = 9600UL
## Also try BAUD = 19200 or 38400 if you're feeling lucky.
## A directory for common include files and the simple USART library.
## If you move either the current folder or the Library folder, you'll
## need to change this path to match.
LIBDIR = ../../AVR-Programming-Library
##########------------------------------------------------------##########
########## Programmer Defaults ##########
########## Set up once, then forget about it ##########
########## (Can override. See bottom of file.) ##########
##########------------------------------------------------------##########
PROGRAMMER_TYPE = usbtiny
# extra arguments to avrdude: baud rate, chip type, -F flag, etc.
PROGRAMMER_ARGS =
##########------------------------------------------------------##########
########## Program Locations ##########
########## Won't need to change if they're in your PATH ##########
##########------------------------------------------------------##########
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
AVRSIZE = avr-size
AVRDUDE = avrdude
##########------------------------------------------------------##########
########## Makefile Magic! ##########
########## Summary: ##########
########## We want a .hex file ##########
########## Compile source files into .elf ##########
########## Convert .elf file into .hex ##########
########## You shouldn't need to edit below. ##########
##########------------------------------------------------------##########
## The name of your project (without the .c)
# TARGET = blinkLED
## Or name it automatically after the enclosing directory
TARGET = $(lastword $(subst /, ,$(CURDIR)))
# Object files: will find all .c/.h files in current directory
# and in LIBDIR. If you have any other (sub-)directories with code,
# you can add them in to SOURCES below in the wildcard statement.
SOURCES=$(wildcard *.c $(LIBDIR)/*.c)
OBJECTS=$(SOURCES:.c=.o)
HEADERS=$(SOURCES:.c=.h)
## Compilation options, type man avr-gcc if you're curious.
CPPFLAGS = -DF_CPU=$(F_CPU) -DBAUD=$(BAUD) -I. -I$(LIBDIR)
CFLAGS = -Os -g -std=gnu99 -Wall
## Use short (8-bit) data types
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
## Splits up object files per function
CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS = -Wl,-Map,$(TARGET).map
## Optional, but often ends up with smaller code
LDFLAGS += -Wl,--gc-sections
## Relax shrinks code even more, but makes disassembly messy
## LDFLAGS += -Wl,--relax
## LDFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## for floating-point printf
## LDFLAGS += -Wl,-u,vfprintf -lprintf_min ## for smaller printf
TARGET_ARCH = -mmcu=$(MCU)
## Explicit pattern rules:
## To make .o files from .c files
%.o: %.c $(HEADERS) Makefile
$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<;
$(TARGET).elf: $(OBJECTS)
$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@
%.hex: %.elf
$(OBJCOPY) -j .text -j .data -O ihex $< $@
%.eeprom: %.elf
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@
%.lst: %.elf
$(OBJDUMP) -S $< > $@
## These targets don't have files named after them
.PHONY: all disassemble disasm eeprom size clean squeaky_clean flash fuses
all: $(TARGET).hex
debug:
@echo
@echo "Source files:" $(SOURCES)
@echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD)
@echo
# Optionally create listing file from .elf
# This creates approximate assembly-language equivalent of your code.
# Useful for debugging time-sensitive bits,
# or making sure the compiler does what you want.
disassemble: $(TARGET).lst
disasm: disassemble
# Optionally show how big the resulting program is
size: $(TARGET).elf
$(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf
clean:
rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \
$(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \
$(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \
$(TARGET).eeprom
squeaky_clean:
rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ *.eeprom
##########------------------------------------------------------##########
########## Programmer-specific details ##########
########## Flashing code to AVR using avrdude ##########
##########------------------------------------------------------##########
flash: $(TARGET).hex
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$<
## An alias
program: flash
flash_eeprom: $(TARGET).eeprom
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$<
avrdude_terminal:
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt
## If you've got multiple programmers that you use,
## you can define them here so that it's easy to switch.
## To invoke, use something like `make flash_arduinoISP`
flash_usbtiny: PROGRAMMER_TYPE = usbtiny
flash_usbtiny: PROGRAMMER_ARGS = # USBTiny works with no further arguments
flash_usbtiny: flash
flash_usbasp: PROGRAMMER_TYPE = usbasp
flash_usbasp: PROGRAMMER_ARGS = # USBasp works with no further arguments
flash_usbasp: flash
flash_arduinoISP: PROGRAMMER_TYPE = avrisp
flash_arduinoISP: PROGRAMMER_ARGS = -b 19200 -P /dev/ttyACM0
## (for windows) flash_arduinoISP: PROGRAMMER_ARGS = -b 19200 -P com5
flash_arduinoISP: flash
flash_109: PROGRAMMER_TYPE = avr109
flash_109: PROGRAMMER_ARGS = -b 9600 -P /dev/ttyUSB0
flash_109: flash
##########------------------------------------------------------##########
########## Fuse settings and suitable defaults ##########
##########------------------------------------------------------##########
## Mega 48, 88, 168, 328 default values
LFUSE = 0x62
HFUSE = 0xdf
EFUSE = 0x00
## Generic
FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m
fuses:
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) \
$(PROGRAMMER_ARGS) $(FUSE_STRING)
show_fuses:
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nv
## Called with no extra definitions, sets to defaults
set_default_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m
set_default_fuses: fuses
## Set the fuse byte for full-speed mode
## Note: can also be set in firmware for modern chips
set_fast_fuse: LFUSE = 0xE2
set_fast_fuse: FUSE_STRING = -U lfuse:w:$(LFUSE):m
set_fast_fuse: fuses
## Set the EESAVE fuse byte to preserve EEPROM across flashes
set_eeprom_save_fuse: HFUSE = 0xD7
set_eeprom_save_fuse: FUSE_STRING = -U hfuse:w:$(HFUSE):m
set_eeprom_save_fuse: fuses
## Clear the EESAVE fuse byte
clear_eeprom_save_fuse: FUSE_STRING = -U hfuse:w:$(HFUSE):m
clear_eeprom_save_fuse: fuses

View File

@@ -0,0 +1,85 @@
/* Direct-digital synthesis */
// ------- Preamble -------- //
#include <avr/io.h> /* Defines pins, ports, etc */
#include <util/delay.h> /* Functions to waste time */
#include <avr/interrupt.h>
#include <avr/power.h>
#include "pinDefines.h"
#include "macros.h"
#include "fullSine.h"
static inline void initTimer0(void){
set_bit(TCCR0A, COM0A1); /* PWM output on OCR0A */
set_bit(SPEAKER_DDR, SPEAKER); /* enable output on pin */
set_bit(TCCR0A, WGM00); /* Fast PWM mode */
set_bit(TCCR0A, WGM01); /* Fast PWM mode, pt.2 */
set_bit(TCCR0B, CS00); /* Clock with /1 prescaler */
}
static inline void pollButton(void){
if (bit_is_clear(BUTTON_PIN, BUTTON)) {
set_bit(SPEAKER_DDR, SPEAKER); /* enable output on pin */
clear_bit(LED_PORT, LED0);
}
else {
clear_bit(SPEAKER_DDR, SPEAKER); /* disable output on pin */
set_bit(LED_PORT, LED0);
}
}
static inline void initLED(void){
set_bit(LED_DDR, LED0); /* LED on for diagnostics */
set_bit(LED_PORT, LED0);
_delay_ms(100);
clear_bit(LED_PORT, LED0);
}
int main(void){
volatile uint16_t accumulator0;
volatile uint16_t accumulator1;
volatile uint16_t tuningWord0;
volatile uint16_t tuningWord1;
volatile uint16_t mixer;
// -------- Inits --------- //
clock_prescale_set(clock_div_1); /* CPU clock 8 MHz */
initLED();
initTimer0();
set_bit(BUTTON_PORT, BUTTON); /* pullup on button */
set_bit(SPEAKER_DDR, SPEAKER); /* speaker output */
tuningWord0 = 440*2; /* Dial tone frequencies */
tuningWord1 = 350*2;
// ------ Event loop ------ //
while(1){
loop_until_bit_is_set(TIFR0, TOV0); /* wait until overflow bit set */
set_bit(TIFR0, TOV0); /* writing set should reset... */
// Note that this bit has to be pretty fast... we have only 256 cycles to
// set the next value in OCR0A, or we may hear a glitch
accumulator0 += tuningWord0; /* take tuningWord steps forward */
accumulator1 += tuningWord1; /* take tuningWord steps forward */
mixer = fullSine[(uint8_t) (accumulator0 >> 8)]; /* add together */
mixer += fullSine[(uint8_t) (accumulator1 >> 8)];
OCR0A = 128 + (mixer >> 1); /* divide by 2, the fast way */
pollButton();
} /* End event loop */
return 0; /* This line is never reached */
}

View File

@@ -0,0 +1,34 @@
int8_t fullSine[256] = {
0, 3, 6, 9, 12, 15, 18, 21,
24, 27, 30, 34, 37, 39, 42, 45,
48, 51, 54, 57, 60, 62, 65, 68,
70, 73, 75, 78, 80, 83, 85, 87,
90, 92, 94, 96, 98, 100, 102, 104,
106, 107, 109, 110, 112, 113, 115, 116,
117, 118, 120, 121, 122, 122, 123, 124,
125, 125, 126, 126, 126, 127, 127, 127,
127, 127, 127, 127, 126, 126, 126, 125,
125, 124, 123, 122, 122, 121, 120, 118,
117, 116, 115, 113, 112, 110, 109, 107,
106, 104, 102, 100, 98, 96, 94, 92,
90, 87, 85, 83, 80, 78, 75, 73,
70, 68, 65, 62, 60, 57, 54, 51,
48, 45, 42, 39, 37, 34, 30, 27,
24, 21, 18, 15, 12, 9, 6, 3,
0, -4, -7, -10, -13, -16, -19, -22,
-25, -28, -31, -35, -38, -40, -43, -46,
-49, -52, -55, -58, -61, -63, -66, -69,
-71, -74, -76, -79, -81, -84, -86, -88,
-91, -93, -95, -97, -99, -101, -103, -105,
-107, -108, -110, -111, -113, -114, -116, -117,
-118, -119, -121, -122, -123, -123, -124, -125,
-126, -126, -127, -127, -127, -128, -128, -128,
-128, -128, -128, -128, -127, -127, -127, -126,
-126, -125, -124, -123, -123, -122, -121, -119,
-118, -117, -116, -114, -113, -111, -110, -108,
-107, -105, -103, -101, -99, -97, -95, -93,
-91, -88, -86, -84, -81, -79, -76, -74,
-71, -69, -66, -63, -61, -58, -55, -52,
-49, -46, -43, -40, -38, -35, -31, -28,
-25, -22, -19, -16, -13, -10, -7, -4
};

View File

@@ -0,0 +1,101 @@
## This file generates headers with lookup tables for various waveforms
## Add your own.
import math
def phaseSteps(maxPhase, length=256):
steps = range(0, length)
steps = [1.0*x/length * 2.0*math.pi * (maxPhase/360.0) for x in steps]
return(steps)
def scaleAndRound(data, scale=255, signedInt=True):
data = [0.0+x-min(data) for x in data]
data = [1.0*x/max(data)*scale for x in data]
data = [int(round(x)) for x in data]
if signedInt:
data = [x-(scale+1)/2 for x in data]
return(data)
def makeSin(maxPhase, length=256):
sinus = [math.sin(x) for x in phaseSteps(maxPhase, length)]
return(sinus)
def prettyPrint(data, perLine = 8):
outString = ""
for i in range(len(data) / perLine):
strings = [str(x) for x in data[perLine*i:(perLine*i+perLine)]]
outString += "\t" + ", ".join(strings) + ",\n"
outString = outString[:-2] + "\n" # drop the final comma
return(outString)
def writeHeader(fileName, dataName, data, signedInt=True):
outfile = open(fileName, "w")
if signedInt:
outfile.write("int8_t {}[{:d}] = {{ \n".format(dataName, len(data)))
else:
outfile.write("uint8_t {}[{:d}] = {{ \n".format(dataName, len(data)))
outfile.write(prettyPrint(data))
outfile.write("};\n")
outfile.close()
def bandlimitedSawtooth(maxPhase, numberPartials, length=256):
wave = [0]*length
sign = 1.0
for k in range(1, numberPartials+1):
phases = phaseSteps(maxPhase*k, length)
for i in range(length):
wave[i] += sign * math.sin(phases[i]) / k
sign = sign * -1
return(wave)
def bandlimitedSquare(maxPhase, numberPartials, length=256):
wave = [0]*length
for k in range(1, numberPartials*2, 2):
phases = phaseSteps(maxPhase*k, length)
for i in range(length):
wave[i] += math.sin(phases[i]) / k
return(wave)
def bandlimitedTriangle(maxPhase, numberPartials, length=256):
wave = [0]*length
sign = 1.0
for k in range(1, numberPartials*2, 2):
phases = phaseSteps(maxPhase*k, length)
for i in range(length):
wave[i] += sign * math.sin(phases[i]) / k**2
sign = sign * -1
return(wave)
if __name__ == "__main__":
## Full-waves, full 256 bytes, 0-255 range
writeHeader("fullSine.h", 'fullSine', scaleAndRound(makeSin(360)))
triangleWave = range(0,64)
triangleWave.extend(range(64, -64, -1))
triangleWave.extend(range(-64, 0, 1))
triangleWave = scaleAndRound(triangleWave)
writeHeader("fullTriangle.h", 'fullTriangle', triangleWave)
for numberFrequencies in [3,7,15]:
saw = scaleAndRound(bandlimitedSawtooth(360, numberFrequencies))
writeHeader("fullSaw{}.h".format(numberFrequencies),
'fullSaw{}'.format(numberFrequencies), saw)
tri = scaleAndRound(bandlimitedTriangle(360, numberFrequencies))
writeHeader("fullTri{}.h".format(numberFrequencies),
'fullTri{}'.format(numberFrequencies), tri)
square = scaleAndRound(bandlimitedSquare(360, numberFrequencies))
writeHeader("fullSquare{}.h".format(numberFrequencies),
'fullSquare{}'.format(numberFrequencies), square)
## Note that if you define / use too many different waveforms,
## and you don't store them in PROGMEM in your AVR C routines,
## you might run the chip out of RAM, which causes strange and
## nearly impossible-to-diagnose glitches.
## So here we're breaking each waveform up into its own include file.
## There are ways of storing them all in program memory, and we'll
## see examples of that in later chapters.