Skip to content

Commit

Permalink
rc0 ready for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxcodejohn committed Apr 15, 2011
1 parent 15f53fc commit acc8c71
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 67 deletions.
12 changes: 3 additions & 9 deletions firmware_buzzIface/README
Expand Up @@ -12,18 +12,12 @@ BuzIface: Interface Platine (USB client)


Befehle vom BuzIface zum Server Befehle vom BuzIface zum Server



opcode: "W"
args : keymask (ein byte, bits 1-4 bedeuten die keys A-D)
funktion: wait_keys_blocking(keymask) hier timeout adden ???
antwort : <KEYID> | "X" (timeout)


opcode "L" opcode "L"
args : <LEDID> <BRIGHT> args : <LEDID> <BRIGHT>
funktion: set_led_state funktion: set_led_state


opcode "R"
funktion: reset BuzIface





238 changes: 180 additions & 58 deletions firmware_buzzIface/buzzIface.c
Expand Up @@ -29,27 +29,107 @@


/* functions: */ /* functions: */


uint8_t wait_keys_blocking(uint8_t keymask /* TODO timeout?,*/ ); //uint8_t wait_keys_blocking(uint8_t keymask /* TODO timeout?,*/ );

uint8_t send_uart_key(uint8_t key); uint8_t send_uart_key(uint8_t key);
static void PCI_init(void );
static void phasentimer_init(void);
static uint8_t do_command (uint8_t * commline);

/* GLOBALZ */


volatile uint8_t key_buf;




/* GLOBALZ */


volatile uint8_t keys_armed;
volatile uint8_t key_input;
volatile uint8_t bufreg_b; #define MASKI_B (0x03)
volatile uint8_t bufreg_d; #define MASKI_D (0xd0)
#define SENDPLACESIZE (8)
#undef KEYS_HAVE_EXTERN_PULLUP

volatile uint8_t phasemask_b;
volatile uint8_t phasemask_d;
volatile uint8_t sendline[SENDPLACESIZE] ;
volatile uint8_t sendplace ;

struct channel cz[5]; struct channel cz[5];






//TODO:
//reduce PCIRQ indem die semantik von phasemask und PCMSK getauscht wird.


inline void hardware_init(void) ISR(PCINT0_vect)
{
uint8_t act ;
act = PINB & (MASKI_B & phasemask_b);
if (act){
switch (act){
case (1 << 0): //Chan A key is B0 --> PCINT0
case (1 << 1): //Chan B key is B1 --> PCINT1
phasemask_b &= ~(act);
sendline[sendplace] = act;
break;
default :
sendline[sendplace] = 0xff;

}
sendplace = (sendplace +1) % SENDPLACESIZE;
}
}

ISR(PCINT2_vect) {
uint8_t act,c ;
act = PIND & MASKI_D & phasemask_d;
if (act){
switch (act){
case (1 << 6): //Chan C key is D6 --> PCINT22
c=CHAN_C;
phasemask_d &= ~(act);
PCMSK2 &= ~(act);
break;
case (1 << 7): //Chan D key is D7 --> PCINT23
c=CHAN_D;
phasemask_d &= ~(act);
PCMSK2 &= ~(act);
break;
case (1<<4): //Chan E key is D4 --> PCINT20
c=CHAN_E;
phasemask_d &= ~(act);
PCMSK2 &= ~(act);
break;
default :
c= 0xff;
}
sendline[sendplace] = c;
sendplace = (sendplace +1) % SENDPLACESIZE;
}
}

ISR(TIMER0_COMPA_vect) {

uint8_t j;

//bis die letzte phase ausgeschaltet worden:
j = MASKI_B & ( PCMSK0 ^ MASKI_B) ;
PCMSK0 = MASKI_B;
//in der phasenmaske nur die setzen die zu j nicht gehören
phasemask_b |= MASKI_B & (~ j) ;

//same for PCIE2
j = MASKI_D * (PCMSK2 ^ MASKI_D) ;
PCMSK2 = MASKI_D ;
phasemask_d |= MASKI_D & ( ~ j) ;

}



static inline void hardware_init(void)
{ {
uint8_t i ; uint8_t i ;
keys_armed = 0;
key_input = 0;
cz[0].led1port = &PORTC ; cz[0].led1port = &PORTC ;
cz[0].led1pin = PIN0; cz[0].led1pin = PIN0;
cz[0].led2port = &PORTC; cz[0].led2port = &PORTC;
Expand Down Expand Up @@ -85,16 +165,6 @@ inline void hardware_init(void)
cz[4].keyport = &PIND; cz[4].keyport = &PIND;
cz[4].keypin = PIN4; cz[4].keypin = PIN4;



/*{ PORTC, PORTC, PORTC, PORTD, PORTD };*/
/*{ PIN0, PIN2, PIN4, PIN5, PIN2 };*/
/*{ PORTC, PORTC, PORTC, PORTB, PORTD };*/
/*{ PIN1, PIN3, PIN5, PIN2, PIN3 };*/
/*{ PINB, PINB, PIND, PIND, PIND };*/
/*{ PIN0, PIN1, PIN6, PIN7, PIN4 };*/





for(i=0; i< CHAN_NUM ; i++){ for(i=0; i< CHAN_NUM ; i++){
/*leds get outputs */ /*leds get outputs */
Expand All @@ -112,33 +182,50 @@ inline void hardware_init(void)
#endif #endif
} }


sendplace = 0 ;
USART_Init(); USART_Init();
PCI_init();
phasentimer_init();
}



static inline void phasentimer_init()
{
//20MHz / 1024 = 19 KHz
//prescaler
//(1.0/19) * 18 = 0.9473684 -> phasenwechsel alle ~ millisekunde
TCCR0A = 0 | (1 << WGM01); // CTC Modus
TCCR0B = 0 | (1<< CS02) | (1<< CS00); //Prescale 1024
OCR0A = 0x09 ; //9 ist 18/2 s.o.
TIMSK0 = 0 | (1<<OCIE0A); //IRQ CTM erlauben
return;
}

static inline void PCI_init()
{


PCIFR = 0; //sanity
PCMSK0 = MASKI_B;
PCMSK2 = MASKI_D;
PCICR = (1<<PCIE0) | (1<<PCIE2); //dont have keys at port c ...
return;
} }





uint8_t set_led_state(ledid_t led_id, uint8_t brightness) uint8_t set_led_state(ledid_t led_id, uint8_t brightness)
{ {
uint8_t ch; uint8_t ch=led_id.channel;
switch (led_id){ switch (led_id.led){
case (LED1_BASE + CHAN_A) : case (0) :
case (LED1_BASE + CHAN_B) :
case (LED1_BASE + CHAN_C) :
case (LED1_BASE + CHAN_D) :
case (LED1_BASE + CHAN_E) :
ch = led_id - LED1_BASE;
if (brightness) { if (brightness) {
*(cz[ch].led1port) |= cz[ch].led1pin ; *(cz[ch].led1port) |= cz[ch].led1pin ;
} else { } else {
*(cz[ch].led2port) &= ~(cz[ch].led1pin) ; *(cz[ch].led2port) &= ~(cz[ch].led1pin) ;
} }
break ; break ;
case (LED2_BASE + CHAN_A) : case (1) :
case (LED2_BASE + CHAN_B) :
case (LED2_BASE + CHAN_C) :
case (LED2_BASE + CHAN_D) :
case (LED2_BASE + CHAN_E) :
ch = led_id - LED2_BASE;
if (brightness) { if (brightness) {
*(cz[ch].led2port) |= cz[ch].led2pin ; *(cz[ch].led2port) |= cz[ch].led2pin ;
} else { } else {
Expand Down Expand Up @@ -179,55 +266,90 @@ uint8_t send_uart_key(uint8_t key){
return 0; return 0;
} }


static inline uint8_t do_setled_command(uint8_t * commline){
ledid_t lid;
switch (commline[0]){
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
lid.channel = commline[0] - 'A';

default:
return ERROR_COMCHAN ;
}
switch (commline[1]){
case '1':
case '2':
lid.led = commline[1] - '1';


default:
return ERROR_COMLED ;
}
return set_led_state (lid, commline[2]) ;
}


inline void arm_buttons(uint8_t buttonbits){


keys_armed = 1 ;
key_input = 0;


return ;

static uint8_t do_command (uint8_t * commline){


switch (commline[0]){
case 'L':
return do_setled_command(commline+1);
default:
return ERROR_COMAND ;
}
return 0; //useless
} }






int main(void) int main(void)
{ {
uint8_t commandbuf[8];
uint8_t commidx=0 , i=0 ,k;
hardware_init(); hardware_init();
sei(); sei();
//------------------------------------ //------------------------------------


uint8_t c ,i ; // USART_puts("Test");
char buf[16]; // USART_puts("\n\r");


USART_puts("Test");
USART_puts("\n\r");




for(;;){ //ever for(;;){ //ever

/* PART 1 :check if host tells us to do something:*/
i = uart_getc_nb(&c); i = uart_getc_nb(&commandbuf[commidx]);
if (i) { if(i){
USART_putc(c); if (commandbuf[commidx] == '\n'){
switch (c){ //befehlszeile zu ende :-)
case 'r' : commandbuf[commidx] = 0 ;
PORTC = 0 | (1<<DDC3); do_command(commandbuf);
break; commidx = 0;
case 'g' : commandbuf[0] = 0 ;
PORTC = 0 | (1<<DDC2); }else{
break; commidx ++ ;
default : if(commidx >= 8 ){
PORTC = 0 | (1<<DDC3)|(1<<DDC2); commidx = 0;
commandbuf[0] = 0 ;
}
} }
} }


/*PART 2 : send stuff to send, if any... */
i = 0;
cli(); //prevent sendplace from bein altered //TODO: better only disable all PC IRQS
while (i < sendplace ) {
send_uart_key(sendline[sendplace]);
i++;
}
sendplace = 0;
sei();
} }


/* never gets here*/ /* never gets here*/
return 0; return 0;
} }



uint8_t wait_keys_blocking(uint8_t keymask /* TODO timeout?,*/ );

0 comments on commit acc8c71

Please sign in to comment.