initial AVR code

This commit is contained in:
Joachim Fenkes 2012-07-31 21:42:53 +02:00
commit b1adb512d6
6 changed files with 504 additions and 0 deletions

77
avr/.cproject Normal file
View file

@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?>
<cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="de.innot.avreclipse.configuration.app.release.1203339825">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="de.innot.avreclipse.configuration.app.release.1203339825" moduleId="org.eclipse.cdt.core.settings" name="Release">
<externalSettings/>
<extensions>
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.MakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactName="${ProjName}" buildArtefactType="de.innot.avreclipse.buildArtefactType.app" buildProperties="org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release,org.eclipse.cdt.build.core.buildArtefactType=de.innot.avreclipse.buildArtefactType.app" description="" id="de.innot.avreclipse.configuration.app.release.1203339825" name="Release" parent="de.innot.avreclipse.configuration.app.release">
<folderInfo id="de.innot.avreclipse.configuration.app.release.1203339825." name="/" resourcePath="">
<toolChain id="de.innot.avreclipse.toolchain.winavr.app.release.1515955421" name="AVR-GCC Toolchain" superClass="de.innot.avreclipse.toolchain.winavr.app.release">
<option id="de.innot.avreclipse.toolchain.options.toolchain.objcopy.flash.app.release.1829874228" name="Generate HEX file for Flash memory" superClass="de.innot.avreclipse.toolchain.options.toolchain.objcopy.flash.app.release"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.objcopy.eeprom.app.release.314636699" name="Generate HEX file for EEPROM" superClass="de.innot.avreclipse.toolchain.options.toolchain.objcopy.eeprom.app.release"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.objdump.app.release.8212953" name="Generate Extended Listing (Source + generated Assembler)" superClass="de.innot.avreclipse.toolchain.options.toolchain.objdump.app.release"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.size.app.release.506266488" name="Print Size" superClass="de.innot.avreclipse.toolchain.options.toolchain.size.app.release"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.avrdude.app.release.297259257" name="AVRDude" superClass="de.innot.avreclipse.toolchain.options.toolchain.avrdude.app.release"/>
<targetPlatform id="de.innot.avreclipse.targetplatform.winavr.app.release.882697694" name="AVR Cross-Target" superClass="de.innot.avreclipse.targetplatform.winavr.app.release"/>
<builder buildPath="${workspace_loc:/mucke/Release}" id="de.innot.avreclipse.target.builder.winavr.app.release.482856708" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="AVR GNU Make Builder" superClass="de.innot.avreclipse.target.builder.winavr.app.release"/>
<tool id="de.innot.avreclipse.tool.assembler.winavr.app.release.376075989" name="AVR Assembler" superClass="de.innot.avreclipse.tool.assembler.winavr.app.release">
<option id="de.innot.avreclipse.assembler.option.debug.level.2049456890" name="Generate Debugging Info" superClass="de.innot.avreclipse.assembler.option.debug.level" value="de.innot.avreclipse.assembler.option.debug.level.none" valueType="enumerated"/>
<inputType id="de.innot.avreclipse.tool.assembler.input.249577734" superClass="de.innot.avreclipse.tool.assembler.input"/>
</tool>
<tool id="de.innot.avreclipse.tool.compiler.winavr.app.release.648288506" name="AVR Compiler" superClass="de.innot.avreclipse.tool.compiler.winavr.app.release">
<option id="de.innot.avreclipse.compiler.option.debug.level.1457477342" name="Generate Debugging Info" superClass="de.innot.avreclipse.compiler.option.debug.level" value="de.innot.avreclipse.compiler.option.debug.level.none" valueType="enumerated"/>
<option id="de.innot.avreclipse.compiler.option.optimize.249607838" name="Optimization Level" superClass="de.innot.avreclipse.compiler.option.optimize" value="de.innot.avreclipse.compiler.optimize.size" valueType="enumerated"/>
<inputType id="de.innot.avreclipse.compiler.winavr.input.923435432" name="C Source Files" superClass="de.innot.avreclipse.compiler.winavr.input"/>
</tool>
<tool id="de.innot.avreclipse.tool.cppcompiler.app.release.1293045382" name="AVR C++ Compiler" superClass="de.innot.avreclipse.tool.cppcompiler.app.release">
<option id="de.innot.avreclipse.cppcompiler.option.debug.level.1245168054" name="Generate Debugging Info" superClass="de.innot.avreclipse.cppcompiler.option.debug.level" value="de.innot.avreclipse.cppcompiler.option.debug.level.none" valueType="enumerated"/>
<option id="de.innot.avreclipse.cppcompiler.option.optimize.214573146" name="Optimization Level" superClass="de.innot.avreclipse.cppcompiler.option.optimize" value="de.innot.avreclipse.cppcompiler.optimize.size" valueType="enumerated"/>
</tool>
<tool id="de.innot.avreclipse.tool.linker.winavr.app.release.267049539" name="AVR C Linker" superClass="de.innot.avreclipse.tool.linker.winavr.app.release">
<inputType id="de.innot.avreclipse.tool.linker.input.269832048" name="OBJ Files" superClass="de.innot.avreclipse.tool.linker.input">
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
</inputType>
</tool>
<tool id="de.innot.avreclipse.tool.cpplinker.app.release.448691111" name="AVR C++ Linker" superClass="de.innot.avreclipse.tool.cpplinker.app.release"/>
<tool id="de.innot.avreclipse.tool.archiver.winavr.base.1728063550" name="AVR Archiver" superClass="de.innot.avreclipse.tool.archiver.winavr.base"/>
<tool id="de.innot.avreclipse.tool.objdump.winavr.app.release.1001059543" name="AVR Create Extended Listing" superClass="de.innot.avreclipse.tool.objdump.winavr.app.release"/>
<tool id="de.innot.avreclipse.tool.objcopy.flash.winavr.app.release.284390286" name="AVR Create Flash image" superClass="de.innot.avreclipse.tool.objcopy.flash.winavr.app.release"/>
<tool id="de.innot.avreclipse.tool.objcopy.eeprom.winavr.app.release.1237762833" name="AVR Create EEPROM image" superClass="de.innot.avreclipse.tool.objcopy.eeprom.winavr.app.release"/>
<tool id="de.innot.avreclipse.tool.size.winavr.app.release.1656883247" name="Print Size" superClass="de.innot.avreclipse.tool.size.winavr.app.release"/>
<tool id="de.innot.avreclipse.tool.avrdude.app.release.841157722" name="AVRDude" superClass="de.innot.avreclipse.tool.avrdude.app.release"/>
</toolChain>
</folderInfo>
<sourceEntries>
<entry excluding="sound3.c|sound1.c|sound.c" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
</sourceEntries>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<project id="mucke.de.innot.avreclipse.project.winavr.elf_2.1.0.2045951346" name="AVR Cross Target Application" projectType="de.innot.avreclipse.project.winavr.elf_2.1.0"/>
</storageModule>
<storageModule moduleId="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
<scannerConfigBuildInfo instanceId="de.innot.avreclipse.configuration.app.release.1203339825;de.innot.avreclipse.configuration.app.release.1203339825.;de.innot.avreclipse.tool.compiler.winavr.app.release.648288506;de.innot.avreclipse.compiler.winavr.input.923435432">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="de.innot.avreclipse.core.AVRGCCManagedMakePerProjectProfileC"/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="de.innot.avreclipse.configuration.app.release.1203339825;de.innot.avreclipse.configuration.app.release.1203339825.">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="de.innot.avreclipse.core.AVRGCCManagedMakePerProjectProfileC"/>
</scannerConfigBuildInfo>
</storageModule>
<storageModule moduleId="refreshScope"/>
</cproject>

83
avr/.project Normal file
View file

@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>noiseplug</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
<dictionary>
<key>?name?</key>
<value></value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.append_environment</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.autoBuildTarget</key>
<value>all</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildArguments</key>
<value></value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildCommand</key>
<value>make</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildLocation</key>
<value>${workspace_loc:/mucke/Release}</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.cleanBuildTarget</key>
<value>clean</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.contents</key>
<value>org.eclipse.cdt.make.core.activeConfigSettings</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableAutoBuild</key>
<value>false</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableCleanBuild</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableFullBuild</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.fullBuildTarget</key>
<value>all</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.stopOnError</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>
<value>true</value>
</dictionary>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
<nature>de.innot.avreclipse.core.avrnature</nature>
</natures>
</projectDescription>

111
avr/noiseplug.c Normal file
View file

@ -0,0 +1,111 @@
/*
* main.c
*
* Created on: 30.10.2011
* Author: dojoe
*/
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include <avr/pgmspace.h>
//register uint8_t int_ctr asm("r18");
uint8_t int_ctr;
#if defined(__AVR_ATmega32U4__)
ISR(TIMER0_OVF_vect)
#elif defined(__AVR_ATtiny9__)
ISR(TIM0_OVF_vect)
#else
#error "warghs"
#endif
{
int_ctr = (int_ctr + 1) & 3;
}
#define NOISEPLUG_BIATCH
#include "sound1.c"
#if 0
static inline uint8_t next_sample(const uint32_t t)
{
// return t*(42&t>>10);
// return t*((42&t>>10)%14);
// return (t*5&t>>7)|(t*3&t>>10);
// return t*9&t>>4|t*5&t>>7|t*3&t/1024;
// return (t*9&t>>4|t*5&t>>7|t*3&t/1024)-1;
// return t>>4|t&((t>>5)/(t>>7-(t>>15)&-t>>7-(t>>15)));
// return (int)(t/1e7*t*t+t)%127|t>>4|t>>5|t%127+(t>>16)|t;
uint8_t a = t>>6&1?t>>5:-t>>4;
uint8_t b = t>>6^t>>8|t>>12|t&63;
return random();
// return ((t>>6^t>>8|t>>12|t&63)&127)+((t>>6&1?t>>6:-t>>5)&63);
// return t << 3;
}
#endif
int main()
{
#if defined(__AVR_ATmega32U4__)
#define SAMPLE_L OCR0A
clock_prescale_set(clock_div_2);
DDRB = 1 << PB7 | 1 << PB2; // PB7 == OC0A
TCNT0 = 0;
OCR0A = 0;
TCCR0A = 3 << WGM00 | 2 << COM0A0; // Fast PWM mode, positive polarity
TCCR0B = 1 << CS00; // Select main clock, no prescaling
TIMSK0 = 1 << TOIE0; // enable overflow interrupt
TIFR0 = 1 << TOV0; // make sure it happens
set_sleep_mode(SLEEP_MODE_IDLE);
sei();
#elif defined(__AVR_ATtiny9__)
#define SAMPLE_L OCR0AL
CCP = 0xD8;
CLKMSR = 0;
CCP = 0xD8;
CLKPSR = 0;
PUEB = 0;
DDRB = (1 << PB0) | (1 << PB2);
TCNT0 = 0;
OCR0A = 0;
TCCR0A = (1 << WGM00) | (2 << COM0A0);
TCCR0B = (1 << WGM02) | (1 << CS00);
TCCR0C = 0;
TIMSK0 = 1 << TOIE0;
set_sleep_mode(SLEEP_MODE_IDLE);
sei();
TIFR0 = 1 << TOV0;
#else
#error "not supported"
#endif
uint8_t sample = 0;
#ifdef __AVR_ATmega32U4__
DDRE = 1 << PE6;
uint16_t t = 0;
#endif
while (1) {
sleep_mode();
if (int_ctr == 0) {
//PORTB = 1 << PB2;
SAMPLE_L = sample;
sample = next_sample();
//PORTB = 0;
#ifdef __AVR_ATmega32U4__
PORTE = (t & 4096) ? 1 << PE6 : 0;
t++;
#endif
}
}
}

18
avr/sound.c Normal file
View file

@ -0,0 +1,18 @@
int main(r)
{
unsigned short rnd = 13373;
while(1)
{
unsigned char f1 = (rnd&(3<<13))>>13;
unsigned char f2 = (rnd&(3<<10))>>10;
rnd <<=1;
rnd |= f1^f2;
putchar(rnd);
}
}

180
avr/sound1.c Normal file
View file

@ -0,0 +1,180 @@
#include <inttypes.h>
#ifndef PROGMEM
#define PROGMEM /* nix */
#endif
#define arpeggio_DELAY 128
#define BIT(x,n) (((x)&(1<<(n)))>>(n))
#define SO(x) (sizeof((x))/sizeof(*(x)))
#define A1 0
#define As1 1
#define B1 2
#define C1 3
#define Cs1 4
#define D1 5
#define Ds1 6
#define E1 7
#define F1 8
#define Fs1 9
#define G1 10
#define Gs1 11
#define A2 12
#define Aa2 13
#define B2 14
#define C2 15
#define Cs2 16
#define D2 17
#define Ds2 18
#define E2 19
#define F2 20
#define Fs2 21
#define G2 22
#define Gs2 23
#define HOLD 24
static const PROGMEM uint8_t sin[] = {0, 49, 97, 141, 180, 212, 235, 250, 254, 250, 235, 212, 180, 141, 97, 49 };
static const PROGMEM uint8_t octave_delay[] = { 36, 34, 32, 31, 29, 27, 26, 24, 23, 22, 20, 19, 18, 17, 16, 15, 14, 14, 13, 12, 11, 11, 10, 10 };
static const PROGMEM struct { uint8_t a; uint8_t b; } synth[] = { { 7, 6}, {7, 5}, {7,5}, {6,5} };
static const PROGMEM uint8_t arpeggiobase[] = { 3, 4, 4, 5 };
static const PROGMEM uint8_t bassdrum[] = { 1, 1, 1, 1 };
static const PROGMEM uint8_t snare[] = { 1, 1, 1, 1 };
static const PROGMEM uint8_t melody[] =
{
D2, 0, D2, 0, 0, 0, 0, 0, D2, 0,
A1, 0, B1, 0, D2, 0, D2, 0, D2, 0
};
static inline uint8_t next_note()
{
static uint8_t idx=0;
const uint8_t v = melody[idx++];
if(idx == SO(melody))
idx = 0;
return v;
}
static inline uint8_t next_rnd()
{
static unsigned short rnd = 13373;
const uint8_t f1 = (rnd&(3<<13))>>13;
const uint8_t f2 = (rnd&(3<<10))>>10;
rnd <<=1;
rnd |= f1^f2;
return sin[((uint8_t)rnd)%SO(sin)];
}
static inline uint8_t next_sin(const uint8_t step)
{
static uint8_t sinoff=SO(sin)-1;
sinoff += step;
sinoff &= SO(sin) - 1;
return sin[sinoff];
}
static inline uint8_t next_sample()
{
static uint16_t t=0;
static uint8_t t8=0;
static uint8_t barevent=0;
static uint8_t bars=0;
static uint8_t pc = 0;
static uint8_t arpeggiocnt = 1;
static uint8_t next_sin_time = 0;
static uint8_t current_tone = 0;
static uint8_t current_tone_base = 12;
static unsigned short snaredelay;
static unsigned short bassdelay;
static uint8_t synth1;
static uint8_t synth2;
if(t%1024 == 0)
{
// implicit rollover of t roughly every 2 seconds
if(t==0) t8 = 0;
else ++t8;
// determine which note we're playing
barevent |= 8;
if(t8%2 == 0) barevent |= 4;
if(t8%4 == 0) barevent |= 2;
if(t8%8 == 0) barevent |= 1;
}
else barevent = 0;
if(barevent & 8)
{
current_tone = current_tone_base = next_note();
}
// increment bar counter
if(barevent & 1) ++bars;
// increment pattern counter
if(bars % 8 == 0)
{
++pc;
pc %= SO(arpeggiobase);
}
// increment arpeggio
if(t % arpeggio_DELAY == 0)
{
// arpeggio
++arpeggiocnt;
arpeggiocnt &= 3;
current_tone = current_tone_base + 4 * arpeggiocnt;
}
// render synth
synth1 = ((t&(t>>(synth[pc].a))) | (t&(t>>(synth[pc].b))) << 1);
if(t == next_sin_time)
{
next_sin_time += octave_delay[current_tone];
synth2 = next_sin(5);
}
// mix two synth lines
unsigned int mix = (synth1>>1) | (synth2>>1);
// load or decrement snare delay
if(barevent & 8 && snare[pc]) snaredelay = 800;
else if(snaredelay > 0) --snaredelay;
// add snare drum
if(snaredelay>0) mix = (next_rnd() & 7) << 3;
// load or decrement bass delay
if(barevent & 4 && bassdrum[pc]) bassdelay = 800;
else if(bassdelay > 0) --bassdelay;
// add bass drum
if(bassdelay>0) mix = next_sin(1);
++t;
// here comes the noize!
return mix;
}
#ifndef NOISEPLUG_BIATCH
int main()
{
while(1) putchar(next_sample());
}
#endif

35
avr/sound3.c Normal file
View file

@ -0,0 +1,35 @@
#define SO(x) (sizeof((x))/sizeof(*(x)))
static unsigned char sin[] = {0, 49, 97, 141, 180, 212, 235, 250, 254, 250, 235, 212, 180, 141, 97, 49 };
unsigned char getRand()
{
static unsigned short rnd = 13373;
unsigned char f1 = (rnd&(3<<13))>>13;
unsigned char f2 = (rnd&(3<<10))>>10;
rnd <<=1;
rnd |= f1^f2;
return (unsigned char)(rnd&0x0f);
}
static inline unsigned char rnd_adv()
{
return sin[getRand()%SO(sin)];
}
static inline unsigned char sin_adv()
{
static unsigned char sinoff=SO(sin)-1;
++sinoff;
sinoff %= SO(sin);
return sin[sinoff];
}
int main()
{
while(1)
{
putchar(sin_adv());
}
}