some cleanup

master
Stefan Dorn 2016-06-12 03:08:50 +01:00
parent 238a98d6b6
commit f5641c807e
2 changed files with 673 additions and 739 deletions

View File

@ -296,8 +296,7 @@ uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
#define _teensypin_read(pin_letter, pin_number) \
((PIN##pin_letter) & (1 << (pin_number)))
#define teensypin_read(pin) \
_teensypin_read(pin)
#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)
/*
@ -386,7 +385,6 @@ uint8_t teensy_init(void) {
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
@ -454,10 +452,10 @@ uint8_t twi_start(void) {
// send start
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
}
@ -466,7 +464,8 @@ void twi_stop(void) {
// send stop
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) {
@ -475,10 +474,10 @@ uint8_t twi_send(uint8_t data) {
// send data
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) &&
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
@ -488,7 +487,8 @@ uint8_t twi_read(uint8_t * data) {
// read 1 byte to TWDR, send ACK
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),
@ -648,7 +648,6 @@ static const uint8_t PROGMEM extra_hid_report_desc[] = {
#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)},
{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);
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();
@ -886,13 +874,10 @@ 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;
@ -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;
@ -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
if (i & (1 << RXOUTI))
return; // abort
// send IN packet
n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
for (i = n; i; i--) {
@ -1057,7 +1034,8 @@ ISR(USB_COM_vect)
#ifdef SUPPORT_ENDPOINT_HALT
if (bmRequestType == 0x82) {
UENUM = wIndex;
if (UECONX & (1<<STALLRQ)) i = 1;
if (UECONX & (1 << STALLRQ))
i = 1;
UENUM = 0;
}
#endif
@ -1067,8 +1045,8 @@ ISR(USB_COM_vect)
return;
}
#ifdef SUPPORT_ENDPOINT_HALT
if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
&& bmRequestType == 0x02 && wValue == 0) {
if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE) &&
bmRequestType == 0x02 && wValue == 0) {
i = wIndex & 0x7F;
if (i >= 1 && i <= MAX_ENDPOINT) {
usb_send_in();
@ -1134,23 +1112,23 @@ ISR(USB_COM_vect)
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();
@ -1166,8 +1144,7 @@ int8_t usb_extra_send(uint8_t report_id, uint16_t data)
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) {

View File

@ -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);