some cleanup
parent
238a98d6b6
commit
f5641c807e
|
@ -288,16 +288,15 @@ uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
|
|||
|
||||
#define _teensypin_write(register, operation, pin_letter, pin_number) \
|
||||
do { \
|
||||
((register##pin_letter) operation (1<<(pin_number))); \
|
||||
((register##pin_letter)operation(1 << (pin_number))); \
|
||||
_delay_us(1); /* allow pins time to stabilize */ \
|
||||
} while(0)
|
||||
} while (0)
|
||||
#define teensypin_write(register, operation, pin) \
|
||||
_teensypin_write(register, operation, pin)
|
||||
|
||||
#define _teensypin_read(pin_letter, pin_number) \
|
||||
((PIN##pin_letter) & (1<<(pin_number)))
|
||||
#define teensypin_read(pin) \
|
||||
_teensypin_read(pin)
|
||||
((PIN##pin_letter) & (1 << (pin_number)))
|
||||
#define teensypin_read(pin) _teensypin_read(pin)
|
||||
|
||||
#define teensypin_write_all_unused(register, operation) \
|
||||
do { \
|
||||
|
@ -305,8 +304,8 @@ uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
|
|||
teensypin_write(register, operation, UNUSED_1); \
|
||||
teensypin_write(register, operation, UNUSED_2); \
|
||||
teensypin_write(register, operation, UNUSED_3); \
|
||||
teensypin_write(register, operation, UNUSED_4); } \
|
||||
while(0)
|
||||
teensypin_write(register, operation, UNUSED_4); \
|
||||
} while (0)
|
||||
|
||||
#define teensypin_write_all_row(register, operation) \
|
||||
do { \
|
||||
|
@ -315,8 +314,8 @@ uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
|
|||
teensypin_write(register, operation, ROW_2); \
|
||||
teensypin_write(register, operation, ROW_3); \
|
||||
teensypin_write(register, operation, ROW_4); \
|
||||
teensypin_write(register, operation, ROW_5); } \
|
||||
while(0)
|
||||
teensypin_write(register, operation, ROW_5); \
|
||||
} while (0)
|
||||
|
||||
#define teensypin_write_all_column(register, operation) \
|
||||
do { \
|
||||
|
@ -326,8 +325,8 @@ uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
|
|||
teensypin_write(register, operation, COLUMN_A); \
|
||||
teensypin_write(register, operation, COLUMN_B); \
|
||||
teensypin_write(register, operation, COLUMN_C); \
|
||||
teensypin_write(register, operation, COLUMN_D); } \
|
||||
while(0)
|
||||
teensypin_write(register, operation, COLUMN_D); \
|
||||
} while (0)
|
||||
|
||||
|
||||
/*
|
||||
|
@ -338,31 +337,31 @@ uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
|
|||
/* set column low (set as output) */ \
|
||||
teensypin_write(DDR, SET, COLUMN_##column); \
|
||||
/* read rows 0..5 and update matrix */ \
|
||||
matrix[0x0][0x##column] = ! teensypin_read(ROW_0); \
|
||||
matrix[0x1][0x##column] = ! teensypin_read(ROW_1); \
|
||||
matrix[0x2][0x##column] = ! teensypin_read(ROW_2); \
|
||||
matrix[0x3][0x##column] = ! teensypin_read(ROW_3); \
|
||||
matrix[0x4][0x##column] = ! teensypin_read(ROW_4); \
|
||||
matrix[0x5][0x##column] = ! teensypin_read(ROW_5); \
|
||||
matrix[0x0][0x##column] = !teensypin_read(ROW_0); \
|
||||
matrix[0x1][0x##column] = !teensypin_read(ROW_1); \
|
||||
matrix[0x2][0x##column] = !teensypin_read(ROW_2); \
|
||||
matrix[0x3][0x##column] = !teensypin_read(ROW_3); \
|
||||
matrix[0x4][0x##column] = !teensypin_read(ROW_4); \
|
||||
matrix[0x5][0x##column] = !teensypin_read(ROW_5); \
|
||||
/* set column hi-Z (set as input) */ \
|
||||
teensypin_write(DDR, CLEAR, COLUMN_##column); \
|
||||
} while(0)
|
||||
} while (0)
|
||||
|
||||
#define update_columns_for_row(matrix, row) \
|
||||
do { \
|
||||
/* set row low (set as output) */ \
|
||||
teensypin_write(DDR, SET, ROW_##row); \
|
||||
/* read columns 7..D and update matrix */ \
|
||||
matrix[0x##row][0x7] = ! teensypin_read(COLUMN_7); \
|
||||
matrix[0x##row][0x8] = ! teensypin_read(COLUMN_8); \
|
||||
matrix[0x##row][0x9] = ! teensypin_read(COLUMN_9); \
|
||||
matrix[0x##row][0xA] = ! teensypin_read(COLUMN_A); \
|
||||
matrix[0x##row][0xB] = ! teensypin_read(COLUMN_B); \
|
||||
matrix[0x##row][0xC] = ! teensypin_read(COLUMN_C); \
|
||||
matrix[0x##row][0xD] = ! teensypin_read(COLUMN_D); \
|
||||
matrix[0x##row][0x7] = !teensypin_read(COLUMN_7); \
|
||||
matrix[0x##row][0x8] = !teensypin_read(COLUMN_8); \
|
||||
matrix[0x##row][0x9] = !teensypin_read(COLUMN_9); \
|
||||
matrix[0x##row][0xA] = !teensypin_read(COLUMN_A); \
|
||||
matrix[0x##row][0xB] = !teensypin_read(COLUMN_B); \
|
||||
matrix[0x##row][0xC] = !teensypin_read(COLUMN_C); \
|
||||
matrix[0x##row][0xD] = !teensypin_read(COLUMN_D); \
|
||||
/* set row hi-Z (set as input) */ \
|
||||
teensypin_write(DDR, CLEAR, ROW_##row); \
|
||||
} while(0)
|
||||
} while (0)
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
|
@ -371,22 +370,21 @@ uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
|
|||
*/
|
||||
uint8_t teensy_init(void) {
|
||||
// CPU speed : should match F_CPU in makefile
|
||||
#if F_CPU != 16000000
|
||||
#error "Expecting different CPU frequency"
|
||||
#endif
|
||||
#if F_CPU != 16000000
|
||||
#error "Expecting different CPU frequency"
|
||||
#endif
|
||||
CPU_PRESCALE(CPU_16MHz);
|
||||
|
||||
// onboard LED
|
||||
// (tied to GND for hardware convenience)
|
||||
DDRD &= ~(1<<6); // set D(6) as input
|
||||
PORTD &= ~(1<<6); // set D(6) internal pull-up disabled
|
||||
DDRD &= ~(1 << 6); // set D(6) as input
|
||||
PORTD &= ~(1 << 6); // set D(6) internal pull-up disabled
|
||||
|
||||
// (tied to Vcc for hardware convenience)
|
||||
DDRB &= ~(1<<4); // set B(4) as input
|
||||
PORTB &= ~(1<<4); // set B(4) internal pull-up disabled
|
||||
DDRB &= ~(1 << 4); // set B(4) as input
|
||||
PORTB &= ~(1 << 4); // set B(4) internal pull-up disabled
|
||||
|
||||
// keyboard LEDs (see "PWM on ports OC1(A|B|C)" in "teensy-2-0.md")
|
||||
_kb_led_all_off(); // (just to put the pins in a known state)
|
||||
TCCR1A = 0b10101001; // set and configure fast PWM
|
||||
TCCR1B = 0b00001001; // set and configure fast PWM
|
||||
|
||||
|
@ -400,13 +398,13 @@ uint8_t teensy_init(void) {
|
|||
// rows and columns
|
||||
teensypin_write_all_row(DDR, CLEAR); // set as input (hi-Z)
|
||||
teensypin_write_all_column(DDR, CLEAR); // set as input (hi-Z)
|
||||
#if TEENSY__DRIVE_ROWS
|
||||
#if TEENSY__DRIVE_ROWS
|
||||
teensypin_write_all_row(PORT, CLEAR); // pull-up disabled
|
||||
teensypin_write_all_column(PORT, SET); // pull-up enabled
|
||||
#elif TEENSY__DRIVE_COLUMNS
|
||||
#elif TEENSY__DRIVE_COLUMNS
|
||||
teensypin_write_all_row(PORT, SET); // pull-up enabled
|
||||
teensypin_write_all_column(PORT, CLEAR); // pull-up disabled
|
||||
#endif
|
||||
#endif
|
||||
|
||||
return 0; // success
|
||||
}
|
||||
|
@ -415,18 +413,18 @@ uint8_t teensy_init(void) {
|
|||
* - success: 0
|
||||
*/
|
||||
#if KB_ROWS != 6 || KB_COLUMNS != 14
|
||||
#error "Expecting different keyboard dimensions"
|
||||
#error "Expecting different keyboard dimensions"
|
||||
#endif
|
||||
|
||||
uint8_t teensy_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
|
||||
#if TEENSY__DRIVE_ROWS
|
||||
#if TEENSY__DRIVE_ROWS
|
||||
update_columns_for_row(matrix, 0);
|
||||
update_columns_for_row(matrix, 1);
|
||||
update_columns_for_row(matrix, 2);
|
||||
update_columns_for_row(matrix, 3);
|
||||
update_columns_for_row(matrix, 4);
|
||||
update_columns_for_row(matrix, 5);
|
||||
#elif TEENSY__DRIVE_COLUMNS
|
||||
#elif TEENSY__DRIVE_COLUMNS
|
||||
update_rows_for_column(matrix, 7);
|
||||
update_rows_for_column(matrix, 8);
|
||||
update_rows_for_column(matrix, 9);
|
||||
|
@ -434,7 +432,7 @@ uint8_t teensy_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
|
|||
update_rows_for_column(matrix, B);
|
||||
update_rows_for_column(matrix, C);
|
||||
update_rows_for_column(matrix, D);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
return 0; // success
|
||||
}
|
||||
|
@ -443,7 +441,7 @@ uint8_t teensy_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
|
|||
|
||||
void twi_init(void) {
|
||||
// set the prescaler value to 0
|
||||
TWSR &= ~( (1<<TWPS1)|(1<<TWPS0) );
|
||||
TWSR &= ~((1 << TWPS1) | (1 << TWPS0));
|
||||
// set the bit rate
|
||||
// - TWBR should be 10 or higher (datasheet section 20.5.2)
|
||||
// - TWI_FREQ should be 400000 (400kHz) max (datasheet section 20.1)
|
||||
|
@ -452,43 +450,45 @@ void twi_init(void) {
|
|||
|
||||
uint8_t twi_start(void) {
|
||||
// send start
|
||||
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTA);
|
||||
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTA);
|
||||
// wait for transmission to complete
|
||||
while (!(TWCR & (1<<TWINT)));
|
||||
while (!(TWCR & (1 << TWINT)))
|
||||
;
|
||||
// if it didn't work, return the status code (else return 0)
|
||||
if ( (TW_STATUS != TW_START) &&
|
||||
(TW_STATUS != TW_REP_START) )
|
||||
if ((TW_STATUS != TW_START) && (TW_STATUS != TW_REP_START))
|
||||
return TW_STATUS; // error
|
||||
return 0; // success
|
||||
}
|
||||
|
||||
void twi_stop(void) {
|
||||
// send stop
|
||||
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
|
||||
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
|
||||
// wait for transmission to complete
|
||||
while (TWCR & (1<<TWSTO));
|
||||
while (TWCR & (1 << TWSTO))
|
||||
;
|
||||
}
|
||||
|
||||
uint8_t twi_send(uint8_t data) {
|
||||
// load data into the data register
|
||||
TWDR = data;
|
||||
// send data
|
||||
TWCR = (1<<TWINT)|(1<<TWEN);
|
||||
TWCR = (1 << TWINT) | (1 << TWEN);
|
||||
// wait for transmission to complete
|
||||
while (!(TWCR & (1<<TWINT)));
|
||||
while (!(TWCR & (1 << TWINT)))
|
||||
;
|
||||
// if it didn't work, return the status code (else return 0)
|
||||
if ( (TW_STATUS != TW_MT_SLA_ACK) &&
|
||||
(TW_STATUS != TW_MT_DATA_ACK) &&
|
||||
(TW_STATUS != TW_MR_SLA_ACK) )
|
||||
if ((TW_STATUS != TW_MT_SLA_ACK) && (TW_STATUS != TW_MT_DATA_ACK) &&
|
||||
(TW_STATUS != TW_MR_SLA_ACK))
|
||||
return TW_STATUS; // error
|
||||
return 0; // success
|
||||
}
|
||||
|
||||
uint8_t twi_read(uint8_t * data) {
|
||||
uint8_t twi_read(uint8_t *data) {
|
||||
// read 1 byte to TWDR, send ACK
|
||||
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
|
||||
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
|
||||
// wait for transmission to complete
|
||||
while (!(TWCR & (1<<TWINT)));
|
||||
while (!(TWCR & (1 << TWINT)))
|
||||
;
|
||||
// set data variable
|
||||
*data = TWDR;
|
||||
// if it didn't work, return the status code (else return 0)
|
||||
|
@ -598,19 +598,19 @@ static const uint8_t PROGMEM keyboard_hid_report_desc[] = {
|
|||
0x29, 0xE7, // Usage Maximum (231),
|
||||
0x15, 0x00, // Logical Minimum (0),
|
||||
0x25, 0x01, // Logical Maximum (1),
|
||||
0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
|
||||
0x81, 0x02, // Input (Data, Variable, Absolute), Modifier byte
|
||||
0x95, 0x01, // Report Count (1),
|
||||
0x75, 0x08, // Report Size (8),
|
||||
0x81, 0x03, // Input (Constant), ;Reserved byte
|
||||
0x81, 0x03, // Input (Constant), Reserved byte
|
||||
0x95, 0x05, // Report Count (5),
|
||||
0x75, 0x01, // Report Size (1),
|
||||
0x05, 0x08, // Usage Page (LEDs),
|
||||
0x19, 0x01, // Usage Minimum (1),
|
||||
0x29, 0x05, // Usage Maximum (5),
|
||||
0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
|
||||
0x91, 0x02, // Output (Data, Variable, Absolute), LED report
|
||||
0x95, 0x01, // Report Count (1),
|
||||
0x75, 0x03, // Report Size (3),
|
||||
0x91, 0x03, // Output (Constant), ;LED report padding
|
||||
0x91, 0x03, // Output (Constant), LED report padding
|
||||
0x95, 0x06, // Report Count (6),
|
||||
0x75, 0x08, // Report Size (8),
|
||||
0x15, 0x00, // Logical Minimum (0),
|
||||
|
@ -643,12 +643,11 @@ static const uint8_t PROGMEM extra_hid_report_desc[] = {
|
|||
#define KEYBOARD_HID_DESC_NUM 0
|
||||
#define KEYBOARD_HID_DESC_OFFSET (9+(9+9+7)*KEYBOARD_HID_DESC_NUM+9)
|
||||
|
||||
# define EXTRA_HID_DESC_NUM (KEYBOARD_HID_DESC_NUM + 1)
|
||||
# define EXTRA_HID_DESC_OFFSET (9+(9+9+7)*EXTRA_HID_DESC_NUM+9)
|
||||
#define EXTRA_HID_DESC_NUM (KEYBOARD_HID_DESC_NUM + 1)
|
||||
#define EXTRA_HID_DESC_OFFSET (9+(9+9+7)*EXTRA_HID_DESC_NUM+9)
|
||||
|
||||
#define NUM_INTERFACES (EXTRA_HID_DESC_NUM + 1)
|
||||
#define CONFIG1_DESC_SIZE (9+(9+9+7)*NUM_INTERFACES)
|
||||
//#define KEYBOARD_HID_DESC_OFFSET (9+9)
|
||||
static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
|
||||
// configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
|
||||
9, // bLength;
|
||||
|
@ -725,20 +724,11 @@ struct usb_string_descriptor_struct {
|
|||
int16_t wString[];
|
||||
};
|
||||
static const struct usb_string_descriptor_struct PROGMEM string0 = {
|
||||
4,
|
||||
3,
|
||||
{0x0409}
|
||||
};
|
||||
4, 3, {0x0409}};
|
||||
static const struct usb_string_descriptor_struct PROGMEM string1 = {
|
||||
sizeof(STR_MANUFACTURER),
|
||||
3,
|
||||
STR_MANUFACTURER
|
||||
};
|
||||
sizeof(STR_MANUFACTURER), 3, STR_MANUFACTURER};
|
||||
static const struct usb_string_descriptor_struct PROGMEM string2 = {
|
||||
sizeof(STR_PRODUCT),
|
||||
3,
|
||||
STR_PRODUCT
|
||||
};
|
||||
sizeof(STR_PRODUCT), 3, STR_PRODUCT};
|
||||
|
||||
// This table defines which descriptor data is sent for each specific
|
||||
// request from the host (in wValue and wIndex).
|
||||
|
@ -753,16 +743,18 @@ static struct descriptor_list_struct {
|
|||
// CONFIGURATION descriptor
|
||||
{0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
|
||||
// HID/REPORT descriptors
|
||||
{0x2100, KEYBOARD_INTERFACE, config1_descriptor+KEYBOARD_HID_DESC_OFFSET, 9},
|
||||
{0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
|
||||
{0x2100, KEYBOARD_INTERFACE, config1_descriptor + KEYBOARD_HID_DESC_OFFSET,
|
||||
9},
|
||||
{0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc,
|
||||
sizeof(keyboard_hid_report_desc)},
|
||||
// Extra HID Descriptor
|
||||
{0x2100, EXTRA_INTERFACE, config1_descriptor+EXTRA_HID_DESC_OFFSET, 9},
|
||||
{0x2200, EXTRA_INTERFACE, extra_hid_report_desc, sizeof(extra_hid_report_desc)},
|
||||
{0x2100, EXTRA_INTERFACE, config1_descriptor + EXTRA_HID_DESC_OFFSET, 9},
|
||||
{0x2200, EXTRA_INTERFACE, extra_hid_report_desc,
|
||||
sizeof(extra_hid_report_desc)},
|
||||
// STRING descriptors
|
||||
{0x0300, 0x0000, (const uint8_t *)&string0, 4},
|
||||
{0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
|
||||
{0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)}
|
||||
};
|
||||
{0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)}};
|
||||
#define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
|
||||
|
||||
|
||||
|
@ -811,59 +803,55 @@ uint16_t last_consumer_key;
|
|||
|
||||
|
||||
// initialize USB
|
||||
void usb_init(void)
|
||||
{
|
||||
void usb_init(void) {
|
||||
HW_CONFIG();
|
||||
USB_FREEZE(); // enable USB
|
||||
PLL_CONFIG(); // config PLL
|
||||
while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
|
||||
while (!(PLLCSR & (1 << PLOCK))) {} // wait for PLL lock
|
||||
USB_CONFIG(); // start USB clock
|
||||
UDCON = 0; // enable attach resistor
|
||||
usb_configuration = 0;
|
||||
UDIEN = (1<<EORSTE)|(1<<SOFE);
|
||||
UDIEN = (1 << EORSTE) | (1 << SOFE);
|
||||
sei();
|
||||
}
|
||||
|
||||
// return 0 if the USB is not configured, or the configuration
|
||||
// number selected by the HOST
|
||||
uint8_t usb_configured(void)
|
||||
{
|
||||
uint8_t usb_configured(void) {
|
||||
return usb_configuration;
|
||||
}
|
||||
|
||||
|
||||
// perform a single keystroke
|
||||
int8_t usb_keyboard_press(uint8_t key, uint8_t modifier)
|
||||
{
|
||||
int8_t usb_keyboard_press(uint8_t key, uint8_t modifier) {
|
||||
int8_t r;
|
||||
|
||||
keyboard_modifier_keys = modifier;
|
||||
keyboard_keys[0] = key;
|
||||
r = usb_keyboard_send();
|
||||
if (r) return r;
|
||||
if (r) { return r; }
|
||||
keyboard_modifier_keys = 0;
|
||||
keyboard_keys[0] = 0;
|
||||
return usb_keyboard_send();
|
||||
}
|
||||
|
||||
// send the contents of keyboard_keys and keyboard_modifier_keys
|
||||
int8_t usb_keyboard_send(void)
|
||||
{
|
||||
int8_t usb_keyboard_send(void) {
|
||||
uint8_t i, intr_state, timeout;
|
||||
|
||||
if (!usb_configuration) return -1;
|
||||
if (!usb_configuration)
|
||||
return -1;
|
||||
intr_state = SREG;
|
||||
cli();
|
||||
UENUM = KEYBOARD_ENDPOINT;
|
||||
timeout = UDFNUML + 50;
|
||||
while (1) {
|
||||
// are we ready to transmit?
|
||||
if (UEINTX & (1<<RWAL)) break;
|
||||
if (UEINTX & (1 << RWAL)) { break; }
|
||||
SREG = intr_state;
|
||||
// has the USB gone offline?
|
||||
if (!usb_configuration) return -1;
|
||||
if (!usb_configuration) { return -1; }
|
||||
// have we waited too long?
|
||||
if (UDFNUML == timeout) return -1;
|
||||
if (UDFNUML == timeout) { return -1; }
|
||||
// get ready to try checking again
|
||||
intr_state = SREG;
|
||||
cli();
|
||||
|
@ -871,7 +859,7 @@ int8_t usb_keyboard_send(void)
|
|||
}
|
||||
UEDATX = keyboard_modifier_keys;
|
||||
UEDATX = 0;
|
||||
for (i=0; i<6; i++) {
|
||||
for (i = 0; i < 6; i++) {
|
||||
UEDATX = keyboard_keys[i];
|
||||
}
|
||||
UEINTX = 0x3A;
|
||||
|
@ -886,37 +874,34 @@ int8_t usb_keyboard_send(void)
|
|||
*
|
||||
**************************************************************************/
|
||||
|
||||
|
||||
|
||||
// USB Device Interrupt - handle all device-level events
|
||||
// the transmit buffer flushing is triggered by the start of frame
|
||||
//
|
||||
ISR(USB_GEN_vect)
|
||||
{
|
||||
ISR(USB_GEN_vect) {
|
||||
uint8_t intbits, i; // used to declare a variable `t` as well, but it
|
||||
// wasn't used ::Ben Blazak, 2012::
|
||||
static uint8_t div4=0;
|
||||
static uint8_t div4 = 0;
|
||||
|
||||
intbits = UDINT;
|
||||
UDINT = 0;
|
||||
if (intbits & (1<<EORSTI)) {
|
||||
if (intbits & (1 << EORSTI)) {
|
||||
UENUM = 0;
|
||||
UECONX = 1;
|
||||
UECFG0X = EP_TYPE_CONTROL;
|
||||
UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
|
||||
UEIENX = (1<<RXSTPE);
|
||||
UEIENX = (1 << RXSTPE);
|
||||
usb_configuration = 0;
|
||||
}
|
||||
if ((intbits & (1<<SOFI)) && usb_configuration) {
|
||||
if ((intbits & (1 << SOFI)) && usb_configuration) {
|
||||
if (keyboard_idle_config && (++div4 & 3) == 0) {
|
||||
UENUM = KEYBOARD_ENDPOINT;
|
||||
if (UEINTX & (1<<RWAL)) {
|
||||
if (UEINTX & (1 << RWAL)) {
|
||||
keyboard_idle_count++;
|
||||
if (keyboard_idle_count == keyboard_idle_config) {
|
||||
keyboard_idle_count = 0;
|
||||
UEDATX = keyboard_modifier_keys;
|
||||
UEDATX = 0;
|
||||
for (i=0; i<6; i++) {
|
||||
for (i = 0; i < 6; i++) {
|
||||
UEDATX = keyboard_keys[i];
|
||||
}
|
||||
UEINTX = 0x3A;
|
||||
|
@ -927,24 +912,15 @@ ISR(USB_GEN_vect)
|
|||
}
|
||||
|
||||
|
||||
|
||||
// Misc functions to wait for ready and send/receive packets
|
||||
static inline void usb_wait_in_ready(void)
|
||||
{
|
||||
while (!(UEINTX & (1<<TXINI))) ;
|
||||
static inline void usb_wait_in_ready(void) {
|
||||
while (!(UEINTX & (1 << TXINI))) {}
|
||||
}
|
||||
static inline void usb_send_in(void)
|
||||
{
|
||||
UEINTX = ~(1<<TXINI);
|
||||
}
|
||||
static inline void usb_wait_receive_out(void)
|
||||
{
|
||||
while (!(UEINTX & (1<<RXOUTI))) ;
|
||||
}
|
||||
static inline void usb_ack_out(void)
|
||||
{
|
||||
UEINTX = ~(1<<RXOUTI);
|
||||
static inline void usb_send_in(void) { UEINTX = ~(1 << TXINI); }
|
||||
static inline void usb_wait_receive_out(void) {
|
||||
while (!(UEINTX & (1 << RXOUTI))) {}
|
||||
}
|
||||
static inline void usb_ack_out(void) { UEINTX = ~(1 << RXOUTI); }
|
||||
|
||||
|
||||
|
||||
|
@ -952,8 +928,7 @@ static inline void usb_ack_out(void)
|
|||
// other endpoints are manipulated by the user-callable
|
||||
// functions, and the start-of-frame interrupt.
|
||||
//
|
||||
ISR(USB_COM_vect)
|
||||
{
|
||||
ISR(USB_COM_vect) {
|
||||
uint8_t intbits;
|
||||
const uint8_t *list;
|
||||
const uint8_t *cfg;
|
||||
|
@ -969,7 +944,7 @@ ISR(USB_COM_vect)
|
|||
|
||||
UENUM = 0;
|
||||
intbits = UEINTX;
|
||||
if (intbits & (1<<RXSTPI)) {
|
||||
if (intbits & (1 << RXSTPI)) {
|
||||
bmRequestType = UEDATX;
|
||||
bRequest = UEDATX;
|
||||
wValue = UEDATX;
|
||||
|
@ -978,12 +953,12 @@ ISR(USB_COM_vect)
|
|||
wIndex |= (UEDATX << 8);
|
||||
wLength = UEDATX;
|
||||
wLength |= (UEDATX << 8);
|
||||
UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
|
||||
UEINTX = ~((1 << RXSTPI) | (1 << RXOUTI) | (1 << TXINI));
|
||||
if (bRequest == GET_DESCRIPTOR) {
|
||||
list = (const uint8_t *)descriptor_list;
|
||||
for (i=0; ; i++) {
|
||||
for (i = 0;; i++) {
|
||||
if (i >= NUM_DESC_LIST) {
|
||||
UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
|
||||
UECONX = (1 << STALLRQ) | (1 << EPEN); // stall
|
||||
return;
|
||||
}
|
||||
desc_val = pgm_read_word(list);
|
||||
|
@ -994,7 +969,7 @@ ISR(USB_COM_vect)
|
|||
list += 2;
|
||||
desc_val = pgm_read_word(list);
|
||||
if (desc_val != wIndex) {
|
||||
list += sizeof(struct descriptor_list_struct)-2;
|
||||
list += sizeof(struct descriptor_list_struct) - 2;
|
||||
continue;
|
||||
}
|
||||
list += 2;
|
||||
|
@ -1004,13 +979,15 @@ ISR(USB_COM_vect)
|
|||
break;
|
||||
}
|
||||
len = (wLength < 256) ? wLength : 255;
|
||||
if (len > desc_length) len = desc_length;
|
||||
if (len > desc_length)
|
||||
len = desc_length;
|
||||
do {
|
||||
// wait for host ready for IN packet
|
||||
do {
|
||||
i = UEINTX;
|
||||
} while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
|
||||
if (i & (1<<RXOUTI)) return; // abort
|
||||
} while (!(i & ((1 << TXINI) | (1 << RXOUTI))));
|
||||
if (i & (1 << RXOUTI))
|
||||
return; // abort
|
||||
// send IN packet
|
||||
n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
|
||||
for (i = n; i; i--) {
|
||||
|
@ -1024,14 +1001,14 @@ ISR(USB_COM_vect)
|
|||
if (bRequest == SET_ADDRESS) {
|
||||
usb_send_in();
|
||||
usb_wait_in_ready();
|
||||
UDADDR = wValue | (1<<ADDEN);
|
||||
UDADDR = wValue | (1 << ADDEN);
|
||||
return;
|
||||
}
|
||||
if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
|
||||
usb_configuration = wValue;
|
||||
usb_send_in();
|
||||
cfg = endpoint_config_table;
|
||||
for (i=1; i<5; i++) {
|
||||
for (i = 1; i < 5; i++) {
|
||||
UENUM = i;
|
||||
en = pgm_read_byte(cfg++);
|
||||
UECONX = en;
|
||||
|
@ -1054,43 +1031,44 @@ ISR(USB_COM_vect)
|
|||
if (bRequest == GET_STATUS) {
|
||||
usb_wait_in_ready();
|
||||
i = 0;
|
||||
#ifdef SUPPORT_ENDPOINT_HALT
|
||||
#ifdef SUPPORT_ENDPOINT_HALT
|
||||
if (bmRequestType == 0x82) {
|
||||
UENUM = wIndex;
|
||||
if (UECONX & (1<<STALLRQ)) i = 1;
|
||||
if (UECONX & (1 << STALLRQ))
|
||||
i = 1;
|
||||
UENUM = 0;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
UEDATX = i;
|
||||
UEDATX = 0;
|
||||
usb_send_in();
|
||||
return;
|
||||
}
|
||||
#ifdef SUPPORT_ENDPOINT_HALT
|
||||
if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
|
||||
&& bmRequestType == 0x02 && wValue == 0) {
|
||||
#ifdef SUPPORT_ENDPOINT_HALT
|
||||
if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE) &&
|
||||
bmRequestType == 0x02 && wValue == 0) {
|
||||
i = wIndex & 0x7F;
|
||||
if (i >= 1 && i <= MAX_ENDPOINT) {
|
||||
usb_send_in();
|
||||
UENUM = i;
|
||||
if (bRequest == SET_FEATURE) {
|
||||
UECONX = (1<<STALLRQ)|(1<<EPEN);
|
||||
UECONX = (1 << STALLRQ) | (1 << EPEN);
|
||||
} else {
|
||||
UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
|
||||
UECONX = (1 << STALLRQC) | (1 << RSTDT) | (1 << EPEN);
|
||||
UERST = (1 << i);
|
||||
UERST = 0;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
if (wIndex == KEYBOARD_INTERFACE) {
|
||||
if (bmRequestType == 0xA1) {
|
||||
if (bRequest == HID_GET_REPORT) {
|
||||
usb_wait_in_ready();
|
||||
UEDATX = keyboard_modifier_keys;
|
||||
UEDATX = 0;
|
||||
for (i=0; i<6; i++) {
|
||||
for (i = 0; i < 6; i++) {
|
||||
UEDATX = keyboard_keys[i];
|
||||
}
|
||||
usb_send_in();
|
||||
|
@ -1131,26 +1109,26 @@ ISR(USB_COM_vect)
|
|||
}
|
||||
}
|
||||
}
|
||||
UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
|
||||
UECONX = (1 << STALLRQ) | (1 << EPEN); // stall
|
||||
}
|
||||
|
||||
int8_t usb_extra_send(uint8_t report_id, uint16_t data)
|
||||
{
|
||||
int8_t usb_extra_send(uint8_t report_id, uint16_t data) {
|
||||
uint8_t intr_state, timeout;
|
||||
|
||||
if (!usb_configured()) return -1;
|
||||
if (!usb_configured())
|
||||
return -1;
|
||||
intr_state = SREG;
|
||||
cli();
|
||||
UENUM = EXTRA_ENDPOINT;
|
||||
timeout = UDFNUML + 50;
|
||||
while (1) {
|
||||
// are we ready to transmit?
|
||||
if (UEINTX & (1<<RWAL)) break;
|
||||
if (UEINTX & (1 << RWAL)) { break; }
|
||||
SREG = intr_state;
|
||||
// has the USB gone offline?
|
||||
if (!usb_configured()) return -1;
|
||||
if (!usb_configured()) { return -1; }
|
||||
// have we waited too long?
|
||||
if (UDFNUML == timeout) return -1;
|
||||
if (UDFNUML == timeout) { return -1; }
|
||||
// get ready to try checking again
|
||||
intr_state = SREG;
|
||||
cli();
|
||||
|
@ -1158,16 +1136,15 @@ int8_t usb_extra_send(uint8_t report_id, uint16_t data)
|
|||
}
|
||||
|
||||
UEDATX = report_id;
|
||||
UEDATX = data&0xFF;
|
||||
UEDATX = (data>>8)&0xFF;
|
||||
UEDATX = data & 0xFF;
|
||||
UEDATX = (data >> 8) & 0xFF;
|
||||
|
||||
UEINTX = 0x3A;
|
||||
SREG = intr_state;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int8_t usb_extra_consumer_send()
|
||||
{
|
||||
int8_t usb_extra_consumer_send() {
|
||||
int result = 0;
|
||||
// don't resend the same key repeatedly if held, only send it once.
|
||||
if (consumer_key != last_consumer_key) {
|
||||
|
|
|
@ -46,7 +46,6 @@
|
|||
#define KB_ROWS 6 // must match real life
|
||||
#define KB_COLUMNS 14 // must match real life
|
||||
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
uint8_t kb_init(void);
|
||||
|
@ -66,48 +65,6 @@ uint8_t teensy_update_matrix( bool matrix[KB_ROWS][KB_COLUMNS] );
|
|||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
#define _kb_led_1_on() (DDRB |= (1<<5))
|
||||
#define _kb_led_1_off() (DDRB &= ~(1<<5))
|
||||
#define _kb_led_1_set(n) (OCR1A = (uint8_t)(n))
|
||||
#define _kb_led_1_set_percent(n) (OCR1A = (uint8_t)((n) * 0xFF))
|
||||
|
||||
#define _kb_led_2_on() (DDRB |= (1<<6))
|
||||
#define _kb_led_2_off() (DDRB &= ~(1<<6))
|
||||
#define _kb_led_2_set(n) (OCR1B = (uint8_t)(n))
|
||||
#define _kb_led_2_set_percent(n) (OCR1B = (uint8_t)((n) * 0xFF))
|
||||
|
||||
#define _kb_led_3_on() (DDRB |= (1<<7))
|
||||
#define _kb_led_3_off() (DDRB &= ~(1<<7))
|
||||
#define _kb_led_3_set(n) (OCR1C = (uint8_t)(n))
|
||||
#define _kb_led_3_set_percent(n) (OCR1C = (uint8_t)((n) * 0xFF))
|
||||
|
||||
|
||||
#define _kb_led_all_on() do { \
|
||||
_kb_led_1_on(); \
|
||||
_kb_led_2_on(); \
|
||||
_kb_led_3_on(); \
|
||||
} while(0)
|
||||
|
||||
#define _kb_led_all_off() do { \
|
||||
_kb_led_1_off(); \
|
||||
_kb_led_2_off(); \
|
||||
_kb_led_3_off(); \
|
||||
} while(0)
|
||||
|
||||
#define _kb_led_all_set(n) do { \
|
||||
_kb_led_1_set(n); \
|
||||
_kb_led_2_set(n); \
|
||||
_kb_led_3_set(n); \
|
||||
} while(0)
|
||||
|
||||
#define _kb_led_all_set_percent(n) do { \
|
||||
_kb_led_1_set_percent(n); \
|
||||
_kb_led_2_set_percent(n); \
|
||||
_kb_led_3_set_percent(n); \
|
||||
} while(0)
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
#define TWI_FREQ 400000
|
||||
|
||||
void twi_init (void);
|
||||
|
|
Loading…
Reference in New Issue