some code formatting

This commit is contained in:
bigalex 2012-10-07 02:04:16 +02:00
parent 06dc89109f
commit 84bf510e36
13 changed files with 505 additions and 696 deletions

View File

@ -24,7 +24,6 @@
* *
******************************************************************************/ ******************************************************************************/
/* MCU frequency */ /* MCU frequency */
#ifndef F_CPU #ifndef F_CPU
#define F_CPU 8000000 #define F_CPU 8000000
@ -38,7 +37,6 @@
/* use second UART on mega128 / can128 / mega162 / mega324p / mega644p */ /* use second UART on mega128 / can128 / mega162 / mega324p / mega644p */
//#define UART_USE_SECOND //#define UART_USE_SECOND
/* Device-Type: /* Device-Type:
For AVRProg the BOOT-option is prefered For AVRProg the BOOT-option is prefered
which is the "correct" value for a bootloader. which is the "correct" value for a bootloader.
@ -61,7 +59,9 @@
*/ */
#define EXIT_WDT_TIME WDTO_250MS #define EXIT_WDT_TIME WDTO_250MS
void __vector_default(void) { ; } void __vector_default(void) {
;
}
/* /*
* define the following if the bootloader should not output * define the following if the bootloader should not output
@ -69,11 +69,9 @@ void __vector_default(void) { ; }
*/ */
#define READ_PROTECT_BOOTLOADER #define READ_PROTECT_BOOTLOADER
#define VERSION_HIGH '0' #define VERSION_HIGH '0'
#define VERSION_LOW '8' #define VERSION_LOW '8'
#ifdef UART_DOUBLESPEED #ifdef UART_DOUBLESPEED
// #define UART_CALC_BAUDRATE(baudRate) (((F_CPU*10UL) / ((baudRate) *8UL) +5)/10 -1) // #define UART_CALC_BAUDRATE(baudRate) (((F_CPU*10UL) / ((baudRate) *8UL) +5)/10 -1)
#define UART_CALC_BAUDRATE(baudRate) ((uint32_t)((F_CPU) + ((uint32_t)baudRate * 4UL)) / ((uint32_t)(baudRate) * 8UL) - 1) #define UART_CALC_BAUDRATE(baudRate) ((uint32_t)((F_CPU) + ((uint32_t)baudRate * 4UL)) / ((uint32_t)(baudRate) * 8UL) - 1)
@ -82,7 +80,6 @@ void __vector_default(void) { ; }
#define UART_CALC_BAUDRATE(baudRate) ((uint32_t)((F_CPU) + ((uint32_t)baudRate * 8UL)) / ((uint32_t)(baudRate) * 16UL) - 1) #define UART_CALC_BAUDRATE(baudRate) ((uint32_t)((F_CPU) + ((uint32_t)baudRate * 8UL)) / ((uint32_t)(baudRate) * 16UL) - 1)
#endif #endif
#include <stdint.h> #include <stdint.h>
#include <avr/io.h> #include <avr/io.h>
#include <avr/wdt.h> #include <avr/wdt.h>
@ -95,32 +92,31 @@ void __vector_default(void) { ; }
uint8_t gBuffer[SPM_PAGESIZE]; uint8_t gBuffer[SPM_PAGESIZE];
static void sendchar(uint8_t data) static void sendchar(uint8_t data) {
{ while (!(UART_STATUS & (1 << UART_TXREADY)))
while (!(UART_STATUS & (1<<UART_TXREADY))); ;
UART_DATA = data; UART_DATA = data;
} }
static uint8_t recvchar(void) static uint8_t recvchar(void) {
{ while (!(UART_STATUS & (1 << UART_RXREADY)))
while (!(UART_STATUS & (1<<UART_RXREADY))); ;
return UART_DATA; return UART_DATA;
} }
static inline void eraseFlash(void) static inline void eraseFlash(void) {
{
// erase only main section (bootloader protection) // erase only main section (bootloader protection)
uint32_t addr = 0; uint32_t addr = 0;
while (APP_END > addr) { while (APP_END > addr) {
boot_page_erase(addr); // Perform page erase boot_page_erase(addr);
// Perform page erase
boot_spm_busy_wait(); // Wait until the memory is erased. boot_spm_busy_wait(); // Wait until the memory is erased.
addr += SPM_PAGESIZE; addr += SPM_PAGESIZE;
} }
boot_rww_enable(); boot_rww_enable();
} }
static inline void recvBuffer(pagebuf_t size) static inline void recvBuffer(pagebuf_t size) {
{
pagebuf_t cnt; pagebuf_t cnt;
uint8_t *tmp = gBuffer; uint8_t *tmp = gBuffer;
@ -129,8 +125,7 @@ static inline void recvBuffer(pagebuf_t size)
} }
} }
static inline uint16_t writeFlashPage(uint16_t waddr, pagebuf_t size) static inline uint16_t writeFlashPage(uint16_t waddr, pagebuf_t size) {
{
uint32_t pagestart = (uint32_t) waddr << 1; uint32_t pagestart = (uint32_t) waddr << 1;
uint32_t baddr = pagestart; uint32_t baddr = pagestart;
uint16_t data; uint16_t data;
@ -140,9 +135,9 @@ static inline uint16_t writeFlashPage(uint16_t waddr, pagebuf_t size)
data = *tmp++; data = *tmp++;
data |= *tmp++ << 8; data |= *tmp++ << 8;
if ( baddr < APP_END ) if (baddr < APP_END) {
{ boot_page_fill(baddr, data);
boot_page_fill(baddr, data); // call asm routine. // call asm routine.
} }
baddr += 2; // Select next word in memory baddr += 2; // Select next word in memory
@ -156,9 +151,7 @@ static inline uint16_t writeFlashPage(uint16_t waddr, pagebuf_t size)
return baddr >> 1; return baddr >> 1;
} }
static inline uint16_t readFlashPage(uint16_t waddr, pagebuf_t size) {
static inline uint16_t readFlashPage(uint16_t waddr, pagebuf_t size)
{
uint32_t baddr = (uint32_t) waddr << 1; uint32_t baddr = (uint32_t) waddr << 1;
uint16_t data; uint16_t data;
@ -178,8 +171,7 @@ static inline uint16_t readFlashPage(uint16_t waddr, pagebuf_t size)
#else #else
data = pgm_read_word_near(baddr); data = pgm_read_word_near(baddr);
#endif #endif
} } else {
else {
data = 0xFFFF; // fake empty data = 0xFFFF; // fake empty
} }
#endif #endif
@ -192,10 +184,7 @@ static inline uint16_t readFlashPage(uint16_t waddr, pagebuf_t size)
return baddr >> 1; return baddr >> 1;
} }
static void send_boot(void) {
static void send_boot(void)
{
sendchar('A'); sendchar('A');
sendchar('V'); sendchar('V');
sendchar('R'); sendchar('R');
@ -207,8 +196,7 @@ static void send_boot(void)
static void (*jump_to_app)(void) = 0x0000; static void (*jump_to_app)(void) = 0x0000;
int main(void) int main(void) {
{
bootloader_wdt_off(); bootloader_wdt_off();
@ -218,7 +206,6 @@ int main(void)
BLDDR &= ~(1 << BLPNUM); // set as Input BLDDR &= ~(1 << BLPNUM); // set as Input
BLPORT |= (1 << BLPNUM); // Enable pullup BLPORT |= (1 << BLPNUM); // Enable pullup
UART_STATUS = (1 << UART_DOUBLE); UART_STATUS = (1 << UART_DOUBLE);
UART_BAUD_HIGH = 0; UART_BAUD_HIGH = 0;
UART_BAUD_LOW = 25; UART_BAUD_LOW = 25;
@ -231,7 +218,6 @@ int main(void)
jump_to_app(); // Jump to application sector jump_to_app(); // Jump to application sector
} }
for (;;) { for (;;) {
val = recvchar(); val = recvchar();
// Autoincrement? // Autoincrement?
@ -287,7 +273,8 @@ int main(void)
// Exit upgrade // Exit upgrade
} else if (val == 'E') { } else if (val == 'E') {
wdt_enable(EXIT_WDT_TIME); // Enable Watchdog Timer to give reset wdt_enable(EXIT_WDT_TIME);
// Enable Watchdog Timer to give reset
sendchar('\r'); sendchar('\r');
// Enter programming mode // Enter programming mode

View File

@ -22,9 +22,7 @@
#define UART_CTRL2_DATA (1<<UCSZ00)|(1<<UCSZ01)//((1<<URSEL0) | (1<<UCSZ10) | (1<<UCSZ00)) #define UART_CTRL2_DATA (1<<UCSZ00)|(1<<UCSZ01)//((1<<URSEL0) | (1<<UCSZ10) | (1<<UCSZ00))
#define UART_DATA UDR0 #define UART_DATA UDR0
static inline void bootloader_wdt_off(void) {
static inline void bootloader_wdt_off(void)
{
// cli(); // cli();
wdt_reset(); wdt_reset();
/* Clear WDRF in MCUSR */ /* Clear WDRF in MCUSR */
@ -36,9 +34,5 @@ static inline void bootloader_wdt_off(void)
WDTCSR = 0x00; WDTCSR = 0x00;
} }
#endif // #ifndef _MEGA88_H_ #endif // #ifndef _MEGA88_H_

View File

@ -1,7 +1,5 @@
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
const uint16_t freq_table[] PROGMEM = { const uint16_t freq_table[] PROGMEM = {
/*0x00*/39, /*0x00*/39,
/*0x01*/41, /*0x01*/41,

View File

@ -2,38 +2,25 @@
#include <avr/io.h> #include <avr/io.h>
#include "bughal.h" #include "bughal.h"
#include "util.h" //for timer #include "util.h" //for timer
/* Hardware abstraction layer for Pentabug hardware */ /* Hardware abstraction layer for Pentabug hardware */
/* /*
* initialize LEDs on C0-C3 * initialize LEDs on C0-C3
*/ */
static uint8_t oldinput; // button readings from last poll cycle static uint8_t oldinput; // button readings from last poll cycle
static uint8_t curinput; // button readings from current poll cycle static uint8_t curinput; // button readings from current poll cycle
//each switch has its own state machine //each switch has its own state machine
static uint8_t btnstates[BTN_BUTTONS]; //array for current button states static uint8_t btnstates[BTN_BUTTONS]; //array for current button states
static uint8_t btncounters[BTN_BUTTONS]; //individual counter for button state machine static uint8_t btncounters[BTN_BUTTONS]; //individual counter for button state machine
static timer_t btntimers[BTN_BUTTONS]; //individiual timer for for button state machine static timer_t btntimers[BTN_BUTTONS]; //individiual timer for for button state machine
void init_leds(void) { void init_leds(void) {
//enable LED channels as output //enable LED channels as output
DDRC |= (1 << PORTC0) | (1 << PORTC1) | (1 << PORTC2) | (1 << PORTC3); DDRC |= (1 << PORTC0) | (1 << PORTC1) | (1 << PORTC2) | (1 << PORTC3);
// both LEDs off // both LEDs off
PORTC &= ~((1 << PORTC0) | (1 << PORTC1) | (1 << PORTC2) | (1 << PORTC3)); PORTC &= ~((1 << PORTC0) | (1 << PORTC1) | (1 << PORTC2) | (1 << PORTC3));
//TCCR2A = (1 << WGM21);
//TCCR2B = (1 << CS20)|(1 << CS21);
//OCR2A = 255; /* TOP */
// TCNT2 = 0;
// /*enable interrupt*/
// TIMSK2 |= (1<<OCIE2A);
return; return;
} }
@ -45,10 +32,8 @@ inline void led_off(int leds){
PORTC &= ~leds; PORTC &= ~leds;
} }
void init_buzzr(void) { void init_buzzr(void) {
//its on B2 and C5, for reasons // its on B2 and C5
DDRC |= (1 << PORTC5); DDRC |= (1 << PORTC5);
DDRB |= (1 << PORTB2); DDRB |= (1 << PORTB2);
// switch it off // switch it off
@ -56,10 +41,9 @@ void init_buzzr(void){
return; return;
} }
void init_mic(void) { void init_mic(void) {
// buzzer is on B2 and C5, for reasons // buzzer is on B2 and C5, for reasons
// we use it as a mic // ... we use it as microphone
DDRC &= ~(1 << PORTC5); // switch C5 to input DDRC &= ~(1 << PORTC5); // switch C5 to input
DDRB |= (1 << PORTB2); // B2 as output DDRB |= (1 << PORTB2); // B2 as output
PORTB &= ~(1 << PORTB2); //and to GND PORTB &= ~(1 << PORTB2); //and to GND
@ -73,54 +57,62 @@ void init_mic(void){
return; return;
} }
void buzzr_up(void) { void buzzr_up(void) {
// one pin Vcc, other GND
PORTC &= ~(1 << PORTC5); PORTC &= ~(1 << PORTC5);
PORTB |= (1 << PORTB2); PORTB |= (1 << PORTB2);
return; return;
} }
;
void buzzr_down(void) { void buzzr_down(void) {
// one pin GND,other pin Vcc
PORTC |= (1 << PORTC5); PORTC |= (1 << PORTC5);
PORTB &= ~(1 << PORTB2); PORTB &= ~(1 << PORTB2);
} }
inline void buzzr_off(void) { inline void buzzr_off(void) {
// both pins to GND
PORTC &= ~(1 << PORTC5); PORTC &= ~(1 << PORTC5);
PORTB &= ~(1 << PORTB2); PORTB &= ~(1 << PORTB2);
} }
void buzzr_inv(void) { void buzzr_inv(void) {
// read and invert pin settings, make the piezo flip polarity
PORTC ^= (1 << PORTC5); PORTC ^= (1 << PORTC5);
PORTB ^= (1 << PORTB2); PORTB ^= (1 << PORTB2);
} }
void init_switch(void) { void init_switch(void) {
DDRD &= ~( (1 << PORTD1) | (1<<PORTD0)); // init switch 1 and switch 2
PORTD |= (1 << PORTD1) | (1<<PORTD0); //Pullups FTW DDRD &= ~((1 << PORTD1) | (1 << PORTD0)); // D0 and D1 as input
PORTD |= (1 << PORTD1) | (1 << PORTD0); // pull-ups FTW
// set predefined button states // set predefined button states
oldinput = 0; oldinput = 0;
curinput = 0; curinput = 0;
// initialize the state machine of each button
for (uint8_t i = 0; i < BTN_BUTTONS; i++) { for (uint8_t i = 0; i < BTN_BUTTONS; i++) {
btnstates[i] = BTNST_NTRL; //init button states btnstates[i] = BTNST_NTRL; //init button states
btncounters[i] = 0; //init button counters btncounters[i] = 0; //init button counters
timer_set(&btntimers[i], 0x05); //50ms - init button timers timer_set(&btntimers[i], 0x05); //50ms - init button timers
} };
return; return;
} }
bool switch_l(void) { bool switch_l(void) {
return !(PIND & 0b00000001); return !(PIND & 0b00000001);
}; }
bool switch_r(void) { bool switch_r(void) {
return !(PIND & 0b00000010); return !(PIND & 0b00000010);
}; }
void init_motor(void) void init_motor(void) {
{
/* vibration motor on B1, initially off: */ /* vibration motor on B1, initially off: */
DDRB |= (1 << PORTB1); DDRB |= (1 << PORTB1);
PORTB &= ~(1 << PORTB1); PORTB &= ~(1 << PORTB1);
@ -141,10 +133,8 @@ void button_clear(uint8_t button){
btnstates[button] = BTNST_NTRL; btnstates[button] = BTNST_NTRL;
} }
void stateswitch(uint8_t i) { void stateswitch(uint8_t i) {
switch(btnstates[i]) switch (btnstates[i]) {
{
case BTNST_NTRL: case BTNST_NTRL:
if (curinput & (1 << i)) { //button down if (curinput & (1 << i)) { //button down
btncounters[i] = 0; btncounters[i] = 0;
@ -209,12 +199,10 @@ void stateswitch(uint8_t i ){
// do nothing yet // do nothing yet
; ;
break; break;
} //end switch }; //end switch
timer_set(&btntimers[i], BTN_T_DEBOUNCE); timer_set(&btntimers[i], BTN_T_DEBOUNCE);
} }
void button_poll() { void button_poll() {
curinput = ~(PIND & 0b00000011); curinput = ~(PIND & 0b00000011);
for (uint8_t i = 0; i < BTN_BUTTONS; i++) { for (uint8_t i = 0; i < BTN_BUTTONS; i++) {
@ -230,5 +218,3 @@ bool btn_state(uint8_t btnstate, uint8_t btn){
return (btnstates[btn] == btnstate); return (btnstates[btn] == btnstate);
} }

View File

@ -7,7 +7,8 @@
enum { enum {
BUZZR_OUT, //initialize buzzer for OUTPUT mode (emmiting soundwaves) BUZZR_OUT, //initialize buzzer for OUTPUT mode (emmiting soundwaves)
BUZZR_IN //initialize buzzer for INPUT mode (registering soundwaves) BUZZR_IN
//initialize buzzer for INPUT mode (registering soundwaves)
}; };
#define LED_L (1 << PORTC0) #define LED_L (1 << PORTC0)

View File

@ -1,4 +1,3 @@
#include <stdint.h> #include <stdint.h>
#include <math.h> #include <math.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
@ -14,17 +13,13 @@ void music_init(void){
TCCR1A = 0; //TIMER1, normal, no PWM TCCR1A = 0; //TIMER1, normal, no PWM
TCCR1B = (1 << WGM12) | (1 << CS11); //CTC Mode, Clear Timer on Compare, Prescaler = 8 TCCR1B = (1 << WGM12) | (1 << CS11); //CTC Mode, Clear Timer on Compare, Prescaler = 8
// OCR1A = 500; //invert with 2 KHz --> 1 KHz sound --> 8000000/8/2000
// OCR1A = 10000;
// TIMSK1 |= (1 << OCIE1A); //enable Output Compare Interrupt
TIMSK1 &= ~(1 << OCIE1A); //disable Output Compare Interrupt TIMSK1 &= ~(1 << OCIE1A); //disable Output Compare Interrupt
return; return;
}; }
;
void music_setNote(uint16_t note, uint8_t octave) { void music_setNote(uint16_t note, uint8_t octave) {
cli(); cli();
if (note != NOTE_PAUSE) { if (note != NOTE_PAUSE) {
//Play a Note //Play a Note
@ -39,18 +34,10 @@ void music_setNote(uint16_t note, uint8_t octave){
return; return;
} }
/* timer interrupt function */ISR(TIMER1_COMPA_vect, ISR_NOBLOCK) {
/* timer interrupt function */
ISR(TIMER1_COMPA_vect, ISR_NOBLOCK) {
// invert buzzer polarity // invert buzzer polarity
buzzr_inv(); buzzr_inv();
} }

View File

@ -3,9 +3,7 @@
#ifndef _MUSIC_H #ifndef _MUSIC_H
#define _MUSIC_H #define _MUSIC_H
#define NOTE_PAUSE (65000) //Pause #define NOTE_PAUSE (65000) //Pause
//Values for Octave 0 //Values for Octave 0
#define NOTE_C (30577) // note C #define NOTE_C (30577) // note C
#define NOTE_Db (28862) // note C# / Db #define NOTE_Db (28862) // note C# / Db
@ -20,8 +18,6 @@
#define NOTE_Bb (17161) // note A# / Bb #define NOTE_Bb (17161) // note A# / Bb
#define NOTE_B (16198) // note B #define NOTE_B (16198) // note B
void music_init(void); void music_init(void);
void music_setNote(uint16_t note, uint8_t octave); void music_setNote(uint16_t note, uint8_t octave);
#endif #endif

View File

@ -14,171 +14,54 @@ enum {
pattern_length = 16 pattern_length = 16
}; };
static const synth_instrument_t instruments[] = { { 1 << 15, 100, 12, 0 }, { 0,
100, 12, 0 }, { 0, 200, 10, 0 }, { 1 << 13, 0, 0, 2 }, { 1 << 13, 0, 5,
2 }, };
static const uint8_t wave_table[][2] = { { 0, WAVE_PULSE }, { 3, WAVE_PULSE }, {
7, WAVE_PULSE }, { 12, WAVE_PULSE }, { 256 - 4, 0xff },
static const synth_instrument_t instruments[] = { { 0, WAVE_PULSE }, { 2, WAVE_PULSE }, { 7, WAVE_PULSE }, { 10, WAVE_PULSE }, {
{ 1<<15, 100, 12, 0 }, 256 - 4, 0xff },
{ 0, 100, 12, 0 },
{ 0, 200, 10, 0 },
{ 1<<13, 0, 0, 2 },
{ 1<<13, 0, 5, 2 },
};
static const uint8_t wave_table[][2] = { { 0, WAVE_NOISE }, { 0, WAVE_PULSE }, { 0xff, 0xff },
{ 0, WAVE_PULSE },
{ 3, WAVE_PULSE },
{ 7, WAVE_PULSE },
{ 12, WAVE_PULSE },
{ 256 - 4, 0xff },
{ 0, WAVE_PULSE }, { 0, WAVE_PULSE }, { 0xff, 0xff },
{ 2, WAVE_PULSE },
{ 7, WAVE_PULSE },
{ 10, WAVE_PULSE },
{ 256 - 4, 0xff },
{ 0, WAVE_NOISE },
{ 0, WAVE_PULSE },
{ 0xff, 0xff },
{ 0, WAVE_PULSE },
{ 0xff, 0xff },
}; };
static const uint8_t patterns[][pattern_length][2] PROGMEM = { { }, { { 33 - 12,
0 }, { 0, 0 }, { 0xff, 1 }, { 0, 0 }, { 33, 1 }, { 0xff, 1 }, { 33, 1 },
{ 0xff, 1 }, { 33, 1 }, { 0xff, 1 }, { 33 - 12, 1 }, { 0xff, 1 }, { 33
- 12, 1 }, { 0xff, 1 }, { 33, 1 }, { 0xff, 1 }, }, { { 28 - 12,
0 }, { 0, 0 }, { 0xff, 1 }, { 0, 0 }, { 28, 1 }, { 0xff, 1 }, { 28, 1 },
{ 0xff, 1 }, { 28, 1 }, { 0xff, 1 }, { 28 - 12, 1 }, { 0xff, 1 }, { 28
- 12, 1 }, { 0xff, 1 }, { 28, 1 }, { 0xff, 1 }, }, { { 0, 0 }, {
0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, {
57, 3 }, }, { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 57, 4 }, },
static const uint8_t patterns[][pattern_length][2] PROGMEM = { { { 60, 2 }, }, { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 57, 2 }, { 0, 0 }, {
{}, 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 55, 2 }, {
{ 0, 0 }, { 57, 2 }, { 0, 0 }, }, { { 55, 2 }, }, { { 0, 0 }, { 0, 0 }, {
{ 33 - 12, 0 }, 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0,
{ 0, 0 }, 0 }, { 0, 0 }, { 0, 0 }, { 57, 2 }, }, { { 55 - 3, 2 }, },
{ 0xff, 1 },
{ 0, 0 },
{ 33, 1 },
{ 0xff, 1 },
{ 33, 1 },
{ 0xff, 1 },
{ 33, 1 },
{ 0xff, 1 },
{ 33 - 12, 1 },
{ 0xff, 1 },
{ 33 - 12, 1 },
{ 0xff, 1 },
{ 33, 1 },
{ 0xff, 1 },
},
{
{ 28 - 12, 0 },
{ 0, 0 },
{ 0xff, 1 },
{ 0, 0 },
{ 28, 1 },
{ 0xff, 1 },
{ 28, 1 },
{ 0xff, 1 },
{ 28, 1 },
{ 0xff, 1 },
{ 28 - 12, 1 },
{ 0xff, 1 },
{ 28 - 12, 1 },
{ 0xff, 1 },
{ 28, 1 },
{ 0xff, 1 },
},
{
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 57, 3 },
},
{
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 57, 4 },
},
{
{ 60, 2 },
},
{
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 57, 2 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 55, 2 },
{ 0, 0 },
{ 57, 2 },
{ 0, 0 },
},
{
{ 55, 2 },
},
{
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 57, 2 },
},
{
{ 55-3, 2 },
},
}; };
static const uint8_t pattern_table[][channel_count] = { static const uint8_t pattern_table[][channel_count] = { { 1, 0, 5 },
{ 1, 0, 5 }, { 1, 3, 0 }, { 1, 0, 7 }, { 1, 3, 6 }, { 2, 0, 7 }, { 2, 4, 8 }, { 2, 0,
{ 1, 3, 0 }, 9 }, { 2, 4, 0 }, };
{ 1, 0, 7 },
{ 1, 3, 6 },
{ 2, 0, 7 },
{ 2, 4, 8 },
{ 2, 0, 9 },
{ 2, 4, 0 },
};
enum { enum {
pattern_table_length = sizeof(pattern_table) / sizeof(pattern_table[0]) pattern_table_length = sizeof(pattern_table) / sizeof(pattern_table[0])
}; };
static synth_channel_t channels[channel_count]; static synth_channel_t channels[channel_count];
static int8_t sample; static int8_t sample;
static int8_t tick; static int8_t tick;
static int8_t row; static int8_t row;
static int8_t seq; static int8_t seq;
/* PROTOTYPES */ /* PROTOTYPES */
uint8_t synth_mix(void); uint8_t synth_mix(void);
@ -189,12 +72,10 @@ static uint8_t timeslots_read; // current read head
/*register for atomic ++ and -- */ /*register for atomic ++ and -- */
register uint8_t timeslots_fill asm("r2"); register uint8_t timeslots_fill asm("r2");
static void enqueue_timeslot(uint8_t synthval); static void enqueue_timeslot(uint8_t synthval);
static uint8_t dequeue_timeslot(void); static uint8_t dequeue_timeslot(void);
void synth_init(void) void synth_init(void) {
{
sample = 0; sample = 0;
tick = 0; tick = 0;
row = 0; row = 0;
@ -202,22 +83,23 @@ void synth_init(void)
timeslots_fill = 0; timeslots_fill = 0;
} }
inline uint8_t synth_mix(void) inline uint8_t synth_mix(void) {
{
if (sample == 0) { // new tick if (sample == 0) { // new tick
for (int i = 1; i < channel_count; i++) { for (int i = 1; i < channel_count; i++) {
synth_channel_t* chan = &channels[i]; synth_channel_t* chan = &channels[i];
const synth_instrument_t* inst = &instruments[chan->inst_nr]; const synth_instrument_t* inst = &instruments[chan->inst_nr];
if(chan->level > inst->decay) chan->level -= inst->decay; if (chan->level > inst->decay)
else chan->level = 0; chan->level -= inst->decay;
else
chan->level = 0;
chan->pulse_width += inst->pulse_sweep; chan->pulse_width += inst->pulse_sweep;
chan->pos++; chan->pos++;
if(wave_table[chan->pos][1] == 0xff) chan->pos += wave_table[chan->pos][0]; if (wave_table[chan->pos][1] == 0xff)
chan->pos += wave_table[chan->pos][0];
// enter new rol // enter new rol
// w // w
@ -231,10 +113,12 @@ inline uint8_t synth_mix(void)
} else { } else {
chan->level = 80; // TODO: less? chan->level = 80; // TODO: less?
chan->note = note; chan->note = note;
chan->inst_nr = pgm_read_byte(&patterns[pattern_nr][row][1]); chan->inst_nr =
pgm_read_byte(&patterns[pattern_nr][row][1]);
inst = &instruments[chan->inst_nr]; inst = &instruments[chan->inst_nr];
chan->pos = inst->wave_table_pos; chan->pos = inst->wave_table_pos;
if(inst->pulse_width) chan->pulse_width = inst->pulse_width; if (inst->pulse_width)
chan->pulse_width = inst->pulse_width;
} }
} }
} }
@ -252,13 +136,13 @@ inline uint8_t synth_mix(void)
} }
} }
uint8_t output = 0; uint8_t output = 0;
for (int i = 0; i < channel_count; i++) { for (int i = 0; i < channel_count; i++) {
synth_channel_t* chan = &channels[i]; synth_channel_t* chan = &channels[i];
// const synth_instrument_t* inst = &instruments[chan->inst_nr]; // const synth_instrument_t* inst = &instruments[chan->inst_nr];
chan->phase += pgm_read_word(&freq_table[(uint8_t)(chan->note + wave_table[chan->pos][0])]); chan->phase +=
pgm_read_word(&freq_table[(uint8_t)(chan->note + wave_table[chan->pos][0])]);
uint8_t amp; uint8_t amp;
switch (wave_table[chan->pos][1]) { switch (wave_table[chan->pos][1]) {
@ -286,7 +170,6 @@ inline uint8_t synth_mix(void)
return output; return output;
} }
/* fill all the timeslots */ /* fill all the timeslots */
inline void synth_poll(void) { inline void synth_poll(void) {
/* refill timeslots queue */ /* refill timeslots queue */
@ -314,11 +197,8 @@ static inline uint8_t dequeue_timeslot() {
return t; return t;
} }
ISR(TIMER0_COMPA_vect) {
ISR(TIMER0_COMPA_vect)
{
/* calculate next analog sample value in synth mixer:*/ /* calculate next analog sample value in synth mixer:*/
OCR1B = dequeue_timeslot(); OCR1B = dequeue_timeslot();
} }

View File

@ -4,7 +4,9 @@
#define SYNTH_BUFSIZE (16) #define SYNTH_BUFSIZE (16)
#define SYNTH_BUFMASK (0b00001111) #define SYNTH_BUFMASK (0b00001111)
enum { WAVE_OFF, WAVE_PULSE, WAVE_SAW, WAVE_NOISE }; enum {
WAVE_OFF, WAVE_PULSE, WAVE_SAW, WAVE_NOISE
};
typedef struct { typedef struct {
uint8_t note; uint8_t note;
@ -18,7 +20,6 @@ typedef struct {
} synth_channel_t; } synth_channel_t;
typedef struct { typedef struct {
uint16_t pulse_width; uint16_t pulse_width;
uint8_t pulse_sweep; uint8_t pulse_sweep;
@ -27,7 +28,6 @@ typedef struct {
} synth_instrument_t; } synth_instrument_t;
void synth_init(void); void synth_init(void);
void synth_poll(void); void synth_poll(void);

View File

@ -23,18 +23,15 @@
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
//#include "main.h"
#include "usart.h" #include "usart.h"
#define UART_RXBUFSIZE 32 #define UART_RXBUFSIZE 32
static volatile uint8_t rxbuf0[UART_RXBUFSIZE]; static volatile uint8_t rxbuf0[UART_RXBUFSIZE];
static volatile uint8_t * volatile rxhead0, * volatile rxtail0; static volatile uint8_t * volatile rxhead0, * volatile rxtail0;
//volatile uint8_t xon = 0;
ISR (USART_RX_vect) ISR (USART_RX_vect) {
{
UCSR0B &= ~(1 << RXCIE0); UCSR0B &= ~(1 << RXCIE0);
asm volatile("sei"); asm volatile("sei");
@ -42,20 +39,19 @@ ISR (USART_RX_vect)
uint8_t c; uint8_t c;
c = UDR0; c = UDR0;
diff = rxhead0 - rxtail0; diff = rxhead0 - rxtail0;
if (diff < 0) diff += UART_RXBUFSIZE; if (diff < 0)
if (diff < UART_RXBUFSIZE -1) diff += UART_RXBUFSIZE;
{ if (diff < UART_RXBUFSIZE - 1) {
*rxhead0 = c; *rxhead0 = c;
++rxhead0; ++rxhead0;
if (rxhead0 == (rxbuf0 + UART_RXBUFSIZE)) rxhead0 = rxbuf0; if (rxhead0 == (rxbuf0 + UART_RXBUFSIZE))
rxhead0 = rxbuf0;
} }
UCSR0B |= (1 << RXCIE0); UCSR0B |= (1 << RXCIE0);
} }
void USART0_Init(void) {
void USART0_Init (void)
{
// set baudrate // set baudrate
#define BAUD_TOL 4 #define BAUD_TOL 4
#undef BAUD #undef BAUD
@ -70,9 +66,9 @@ void USART0_Init (void)
UCSR0A &= ~(1 << U2X0); // disable double speed operation UCSR0A &= ~(1 << U2X0); // disable double speed operation
#endif #endif
// flush receive buffer // flush receive buffer
while ( UCSR0A & (1 << RXC0) ) UDR0; while (UCSR0A & (1 << RXC0))
UDR0;
// set 8N1 // set 8N1
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
@ -86,27 +82,25 @@ void USART0_Init (void)
} }
void USART0_putc(char c) {
void USART0_putc (char c)
{
loop_until_bit_is_set(UCSR0A, UDRE0); loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c; UDR0 = c;
} }
uint8_t USART0_Getc_nb(uint8_t *c) {
uint8_t USART0_Getc_nb(uint8_t *c) if (rxhead0 == rxtail0)
{ return 0;
if (rxhead0==rxtail0) return 0;
*c = *rxtail0; *c = *rxtail0;
if (++rxtail0 == (rxbuf0 + UART_RXBUFSIZE)) rxtail0 = rxbuf0; if (++rxtail0 == (rxbuf0 + UART_RXBUFSIZE))
rxtail0 = rxbuf0;
return 1; return 1;
} }
void USART0_crlf(void) { void USART0_crlf(void) {
USART0_putc(0x0A); //newline USART0_putc(0x0A); //newline
USART0_putc(0x0D); //carriage return USART0_putc(0x0D); //carriage return
}; }
;
void USART0_put_uint8(uint8_t x) { void USART0_put_uint8(uint8_t x) {
uint8_t highchar = ((x & 0b11110000) >> 4) + 0x30; uint8_t highchar = ((x & 0b11110000) >> 4) + 0x30;

View File

@ -1,4 +1,3 @@
#include <stdint.h> #include <stdint.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <avr/common.h> #include <avr/common.h>
@ -7,8 +6,7 @@
static volatile uint8_t internal_counter; static volatile uint8_t internal_counter;
void timer_init(void) void timer_init(void) {
{
/* initialize timer2, CTC at 10ms, prescaler 1024 */ /* initialize timer2, CTC at 10ms, prescaler 1024 */
OCR2A = F_CPU / 1024 / 100; OCR2A = F_CPU / 1024 / 100;
TCCR2A = _BV(WGM21); TCCR2A = _BV(WGM21);
@ -16,23 +14,17 @@ void timer_init(void)
TIMSK2 = _BV(OCIE2A); TIMSK2 = _BV(OCIE2A);
} }
void timer_set(timer_t *t, uint8_t timeout) void timer_set(timer_t *t, uint8_t timeout) {
{
t->current = internal_counter; t->current = internal_counter;
t->timeout = timeout; t->timeout = timeout;
} }
void timerL_set(timerL_t *t, uint16_t timeout) void timerL_set(timerL_t *t, uint16_t timeout) {
{
t->current = internal_counter; t->current = internal_counter;
t->timeout = timeout; t->timeout = timeout;
} }
bool timer_expired(timer_t *t) {
bool timer_expired(timer_t *t)
{
if (t->timeout == 0) if (t->timeout == 0)
return true; return true;
@ -46,9 +38,7 @@ bool timer_expired(timer_t *t)
return false; return false;
} }
bool timerL_expired(timerL_t *t) {
bool timerL_expired(timerL_t *t)
{
if (t->timeout == 0) if (t->timeout == 0)
return true; return true;
@ -62,10 +52,7 @@ bool timerL_expired(timerL_t *t)
return false; return false;
} }
/* timer interrupt function */ISR(TIMER2_COMPA_vect, ISR_NOBLOCK) {
/* timer interrupt function */
ISR(TIMER2_COMPA_vect, ISR_NOBLOCK) {
internal_counter++; internal_counter++;
} }

View File

@ -17,7 +17,6 @@ typedef struct {
uint16_t timeout; uint16_t timeout;
} timerL_t; } timerL_t;
/* functions */ /* functions */
void timer_init(void); void timer_init(void);
void timer_set(timer_t *t, uint8_t timeout); void timer_set(timer_t *t, uint8_t timeout);