Broke everything

pull/2559/head
Mikkel Jeppesen 7 years ago
parent 548766d0a5
commit feeee4e40d

@ -6,7 +6,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "i2c.h" #include "i2c.h"
#ifdef USE_I2C #ifndef USE_I2C
// Limits the amount of we wait for any one i2c transaction. // Limits the amount of we wait for any one i2c transaction.
// Since were running SCL line 100kHz (=> 10μs/bit), and each transactions is // Since were running SCL line 100kHz (=> 10μs/bit), and each transactions is
@ -36,6 +36,9 @@ void i2c_delay(void) {
// Setup twi to run at 100kHz // Setup twi to run at 100kHz
void i2c_master_init(void) { void i2c_master_init(void) {
// Reset the registers set by slave mode
TWAR = 0xFE;
TWCR = 0;
// no prescaler // no prescaler
TWSR = 0; TWSR = 0;
// Set TWI clock frequency to SCL_CLOCK. Need TWBR>10. // Set TWI clock frequency to SCL_CLOCK. Need TWBR>10.

@ -40,6 +40,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
# include "serial.h" # include "serial.h"
#endif #endif
// Seconds to keep checking if USB has been enumearted, before settling for being slave half.
#define USBTIMEOUT 3
#ifndef DEBOUNCING_DELAY #ifndef DEBOUNCING_DELAY
# define DEBOUNCING_DELAY 5 # define DEBOUNCING_DELAY 5
#endif #endif
@ -128,7 +131,21 @@ uint8_t matrix_cols(void)
} }
bool has_usb(void) { bool has_usb(void) {
return UDADDR & _BV(ADDEN); // This will return true of a USB connection has been established return UDADDR & _BV(ADDEN); // This will return true if a USB connection has been established
}
void timer_reset(void) {
#ifndef __AVR_ATmega32A__
TCCR0A = 0;
TCCR0B = 0;
OCR0A = 0;
TIMSK0 = 0;
#else
// Timer0 CTC mode
TCCR0 = 0;
OCR0 = 0;
TIMSK = 0;
#endif
} }
void matrix_init(void) void matrix_init(void)
@ -156,28 +173,34 @@ void matrix_init(void)
matrix_debouncing[i] = 0; matrix_debouncing[i] = 0;
} }
_delay_ms(2000); // Give USB 2 seconds to establish a connection // initialize as slave
if (has_usb()) { // Set up as master
#ifdef USE_I2C
i2c_master_init();
#else
serial_master_init();
#endif
} else { // Set up as slave
timer_init(); timer_init();
#ifdef USE_I2C #ifdef USE_I2C
i2c_slave_init(SLAVE_I2C_ADDRESS); i2c_slave_init(SLAVE_I2C_ADDRESS);
#else #else
serial_slave_init(); serial_slave_init();
#endif #endif
}
sei(); sei();
matrix_init_quantum(); matrix_init_quantum();
if(!has_usb()){ /* Wait USBTIMEOUT for USB
uint32_t timeout_start = timer_count;
while(!has_usb() && USBTIMEOUT > timer_count - timeout_start) {
matrix_slave_scan();
}*/
// If we have USB, switch to master
if(has_usb()) {
//timer_reset();
#ifdef USE_I2C
i2c_master_init();
#else
serial_master_init();
#endif
}
else{
while (1) { while (1) {
matrix_slave_scan(); matrix_slave_scan();
} }

@ -38,6 +38,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define MATRIX_ROW_PINS { F5, F6, C7, F7 } #define MATRIX_ROW_PINS { F5, F6, C7, F7 }
#define MATRIX_COL_PINS { F1, F4, E2, B6, D7, D6} #define MATRIX_COL_PINS { F1, F4, E2, B6, D7, D6}
/* define if matrix uses jtag pins */
#define DISABLE_JTAG
/* define if matrix has ghost */ /* define if matrix has ghost */
//#define MATRIX_HAS_GHOST //#define MATRIX_HAS_GHOST

Loading…
Cancel
Save