void processTWI( void ) { uint8_t b,p1,p2; b = usiTwiReceiveByte(); switch (b) { case 0x81: // set slave address p1 = usiTwiReceiveByte(); if(p1 < 128) // Address is 7 bit { eeprom_update_byte(&b_slave_address, p1); usiTwiSlaveInit(eeprom_read_byte(&b_slave_address)); } break; case 0x82: // clear led led_clear(); break; case 0x83: // set led brightness, p1=led, p2=brightness p1 = usiTwiReceiveByte(); p2 = usiTwiReceiveByte(); set_led_pulse(p1,0); // Turn off pulsing set_brightness(p1,p2); break; case 0x84: // Set to pulse led, p1=led, p2 = (0 = OFF, 1 = ON) set_led_pulse(usiTwiReceiveByte(),usiTwiReceiveByte()); break; case 0x85: // Dim up/down led to specific value, p1=led, p2=value to dim to dim_led(usiTwiReceiveByte(),usiTwiReceiveByte()); break; case 0x86: // get firmware revision usiTwiTransmitByte(FIRMWARE_REVISION); break; case 0x90: // get keyUp Event usiTwiTransmitByte(getKeyUp()); break; case 0x91: // get KeyDown Event usiTwiTransmitByte(getKeyDown()); break; case 0x92: // set keyrepeat set_keyrepeat(usiTwiReceiveByte(),usiTwiReceiveByte()); break; case 0xFE: // reset to known state led_clear(); button_init(); flushTwiBuffers(); break; case 0xFF: // flush the bus break; default: break; } }
void usiTwiSlaveInit( uint8_t ownAddress ) { flushTwiBuffers( ); slaveAddress = ownAddress; // In Two Wire mode (USIWM1, USIWM0 = 1X), the slave USI will pull SCL // low when a start condition is detected or a counter overflow (only // for USIWM1, USIWM0 = 11). This inserts a wait state. SCL is released // by the ISRs (USI_START_vect and USI_OVERFLOW_vect). // Set SCL and SDA as output // DDR_USI |= ( 1 << PORT_USI_SCL ) | ( 1 << PORT_USI_SDA ); // set SCL high PORT_USI |= ( 1 << PORT_USI_SCL ); // set SDA high PORT_USI |= ( 1 << PORT_USI_SDA ); // set SCL as output DDR_USI |= ( 1 << PORT_USI_SCL ); // Set SDA as input DDR_USI &= ~( 1 << PORT_USI_SDA ); USICR = // enable Start Condition Interrupt ( 1 << USISIE ) | // disable Overflow Interrupt ( 0 << USIOIE ) | // set USI in Two-wire mode, no USI Counter overflow hold ( 1 << USIWM1 ) | ( 0 << USIWM0 ) | // Shift Register Clock Source = external, positive edge // 4-Bit Counter Source = external, both edges ( 1 << USICS1 ) | ( 0 << USICS0 ) | ( 0 << USICLK ) | // no toggle clock-port pin ( 0 << USITC ); // clear all interrupt flags and reset overflow counter USISR = ( 1 << USI_START_COND_INT ) | ( 1 << USIOIF ) | ( 1 << USIPF ) | ( 1 << USIDC ); } // end usiTwiSlaveInit