diff --git a/src/keyboard/controller.c b/src/keyboard/controller.c index b750907..6d4442b 100644 --- a/src/keyboard/controller.c +++ b/src/keyboard/controller.c @@ -14,51 +14,47 @@ * - success: 0 * - error: number of the function that failed */ -uint8_t kb_init(void) { - if (teensy_init()) // must be first - return 1; - if (mcp23018_init()) // must be second - return 2; +u8 kb_init(void) { + if (teensy_init()) { return 1; } + if (mcp23018_init()) { return 2; } - return 0; // success + return 0; // success } /* returns * - success: 0 * - error: number of the function that failed */ -uint8_t kb_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) { - if (teensy_update_matrix(matrix)) - return 1; - if (mcp23018_update_matrix(matrix)) - return 2; +u8 kb_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) { + if (teensy_update_matrix(matrix)) { return 1; } + if (mcp23018_update_matrix(matrix)) { return 2; } - return 0; // success + return 0; // success } // ---------------------------------------------------------------------------- // check options #if (MCP23018__DRIVE_ROWS && MCP23018__DRIVE_COLUMNS) \ - || !(MCP23018__DRIVE_ROWS || MCP23018__DRIVE_COLUMNS) - #error "See 'Pin drive direction' in 'options.h'" + || !(MCP23018__DRIVE_ROWS || MCP23018__DRIVE_COLUMNS) +#error "See 'Pin drive direction' in 'options.h'" #endif // ---------------------------------------------------------------------------- // register addresses (see "mcp23018.md") -#define IODIRA 0x00 // i/o direction register -#define IODIRB 0x01 -#define GPPUA 0x0C // GPIO pull-up resistor register -#define GPPUB 0x0D -#define GPIOA 0x12 // general purpose i/o port register (write modifies OLAT) -#define GPIOB 0x13 -#define OLATA 0x14 // output latch register -#define OLATB 0x15 +#define IODIRA 0x00 // i/o direction register +#define IODIRB 0x01 +#define GPPUA 0x0C // GPIO pull-up resistor register +#define GPPUB 0x0D +#define GPIOA 0x12 // general purpose i/o port register (write modifies OLAT) +#define GPIOB 0x13 +#define OLATA 0x14 // output latch register +#define OLATB 0x15 // TWI aliases -#define TWI_ADDR_WRITE ( (MCP23018_TWI_ADDRESS<<1) | TW_WRITE ) -#define TWI_ADDR_READ ( (MCP23018_TWI_ADDRESS<<1) | TW_READ ) +#define TWI_ADDR_WRITE ( (MCP23018_TWI_ADDRESS<<1) | TW_WRITE ) +#define TWI_ADDR_READ ( (MCP23018_TWI_ADDRESS<<1) | TW_READ ) // ---------------------------------------------------------------------------- @@ -70,57 +66,57 @@ uint8_t kb_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) { * - `twi_stop()` must be called *exactly once* for each twi block, the way * things are currently set up. this may change in the future. */ -uint8_t mcp23018_init(void) { - uint8_t ret; +u8 mcp23018_init(void) { + u8 ret; - // set pin direction - // - unused : input : 1 - // - input : input : 1 - // - driving : output : 0 - twi_start(); - ret = twi_send(TWI_ADDR_WRITE); - if (ret) goto out; // make sure we got an ACK - twi_send(IODIRA); - #if MCP23018__DRIVE_ROWS - twi_send(0b11111111); // IODIRA - twi_send(0b11000000); // IODIRB - #elif MCP23018__DRIVE_COLUMNS - twi_send(0b10000000); // IODIRA - twi_send(0b11111111); // IODIRB - #endif - twi_stop(); + // set pin direction + // - unused : input : 1 + // - input : input : 1 + // - driving : output : 0 + twi_start(); + ret = twi_send(TWI_ADDR_WRITE); + if (ret) goto out; // make sure we got an ACK + twi_send(IODIRA); +#if MCP23018__DRIVE_ROWS + twi_send(0b11111111); // IODIRA + twi_send(0b11000000); // IODIRB +#elif MCP23018__DRIVE_COLUMNS + twi_send(0b10000000); // IODIRA + twi_send(0b11111111); // IODIRB +#endif + twi_stop(); - // set pull-up - // - unused : on : 1 - // - input : on : 1 - // - driving : off : 0 - twi_start(); - ret = twi_send(TWI_ADDR_WRITE); - if (ret) goto out; // make sure we got an ACK - twi_send(GPPUA); - #if MCP23018__DRIVE_ROWS - twi_send(0b11111111); // GPPUA - twi_send(0b11000000); // GPPUB - #elif MCP23018__DRIVE_COLUMNS - twi_send(0b10000000); // GPPUA - twi_send(0b11111111); // GPPUB - #endif - twi_stop(); + // set pull-up + // - unused : on : 1 + // - input : on : 1 + // - driving : off : 0 + twi_start(); + ret = twi_send(TWI_ADDR_WRITE); + if (ret) goto out; // make sure we got an ACK + twi_send(GPPUA); +#if MCP23018__DRIVE_ROWS + twi_send(0b11111111); // GPPUA + twi_send(0b11000000); // GPPUB +#elif MCP23018__DRIVE_COLUMNS + twi_send(0b10000000); // GPPUA + twi_send(0b11111111); // GPPUB +#endif + twi_stop(); - // set logical value (doesn't matter on inputs) - // - unused : hi-Z : 1 - // - input : hi-Z : 1 - // - driving : hi-Z : 1 - twi_start(); - ret = twi_send(TWI_ADDR_WRITE); - if (ret) goto out; // make sure we got an ACK - twi_send(OLATA); - twi_send(0b11111111); //OLATA - twi_send(0b11111111); //OLATB + // set logical value (doesn't matter on inputs) + // - unused : hi-Z : 1 + // - input : hi-Z : 1 + // - driving : hi-Z : 1 + twi_start(); + ret = twi_send(TWI_ADDR_WRITE); + if (ret) goto out; // make sure we got an ACK + twi_send(OLATA); + twi_send(0b11111111); //OLATA + twi_send(0b11111111); //OLATB out: - twi_stop(); - return ret; + twi_stop(); + return ret; } /* returns: @@ -128,110 +124,110 @@ out: * - failure: twi status code */ #if KB_ROWS != 6 || KB_COLUMNS != 14 - #error "Expecting different keyboard dimensions" +#error "Expecting different keyboard dimensions" #endif -uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) { - uint8_t ret, data; +u8 mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) { + u8 ret, data; - // initialize things, just to make sure - // - it's not appreciably faster to skip this, and it takes care of the - // case when the i/o expander isn't plugged in during the first - // init() - ret = mcp23018_init(); + // initialize things, just to make sure + // - it's not appreciably faster to skip this, and it takes care of the + // case when the i/o expander isn't plugged in during the first + // init() + ret = mcp23018_init(); - // if there was an error - if (ret) { - // clear our part of the matrix - for (uint8_t row=0; row<=5; row++) - for (uint8_t col=0; col<=6; col++) - matrix[row][col] = 0; + // if there was an error + if (ret) { + // clear our part of the matrix + for (u8 row=0; row<=5; row++) + for (u8 col=0; col<=6; col++) + matrix[row][col] = 0; - return ret; - } + return ret; + } - // -------------------------------------------------------------------- - // update our part of the matrix + // -------------------------------------------------------------------- + // update our part of the matrix - #if MCP23018__DRIVE_ROWS - for (uint8_t row=0; row<=5; row++) { - // set active row low : 0 - // set other rows hi-Z : 1 - twi_start(); - twi_send(TWI_ADDR_WRITE); - twi_send(GPIOB); - twi_send( 0xFF & ~(1<<(5-row)) ); - twi_stop(); +#if MCP23018__DRIVE_ROWS + for (u8 row=0; row<=5; row++) { + // set active row low : 0 + // set other rows hi-Z : 1 + twi_start(); + twi_send(TWI_ADDR_WRITE); + twi_send(GPIOB); + twi_send( 0xFF & ~(1<<(5-row)) ); + twi_stop(); - // read column data - twi_start(); - twi_send(TWI_ADDR_WRITE); - twi_send(GPIOA); - twi_start(); - twi_send(TWI_ADDR_READ); - twi_read(&data); - twi_stop(); + // read column data + twi_start(); + twi_send(TWI_ADDR_WRITE); + twi_send(GPIOA); + twi_start(); + twi_send(TWI_ADDR_READ); + twi_read(&data); + twi_stop(); - // update matrix - for (uint8_t col=0; col<=6; col++) { - matrix[row][col] = !( data & (1<= NUM_DESC_LIST) { UECONX = (1 << STALLRQ) | (1 << EPEN); // stall @@ -1099,7 +1095,7 @@ ISR(USB_COM_vect) { continue; } list += 2; - desc_addr = (const uint8_t *)pgm_read_word(list); + desc_addr = (const u8 *)pgm_read_word(list); list += 2; desc_length = pgm_read_byte(list); break; @@ -1237,8 +1233,8 @@ ISR(USB_COM_vect) { UECONX = (1 << STALLRQ) | (1 << EPEN); // stall } -int8_t usb_extra_send(uint8_t report_id, uint16_t data) { - uint8_t intr_state, timeout; +i8 usb_extra_send(u8 report_id, u16 data) { + u8 intr_state, timeout; if (!usb_configured()) return -1; @@ -1269,7 +1265,7 @@ int8_t usb_extra_send(uint8_t report_id, uint16_t data) { return 0; } -int8_t usb_extra_consumer_send() { +i8 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) { @@ -1287,9 +1283,9 @@ int8_t usb_extra_consumer_send() { #ifdef KBD_DEBUG // transmit a character. 0 returned on success, -1 on error -int8_t usb_debug_putchar(char c) { - static uint8_t previous_timeout = 0; - uint8_t timeout, intr_state; +i8 usb_debug_putchar(char c) { + static u8 previous_timeout = 0; + u8 timeout, intr_state; // if we're not online (enumerated and configured), error if (!usb_configuration) @@ -1343,7 +1339,7 @@ int8_t usb_debug_putchar(char c) { // immediately transmit any buffered output. void usb_debug_flush_output(void) { - uint8_t intr_state; + u8 intr_state; intr_state = SREG; cli(); @@ -1393,7 +1389,7 @@ void usb_debug_free_memory() { // timer // -------------------------------------------------------- -volatile uint32_t timer0_ms = 0; +volatile u32 timer0_ms = 0; void timer0_init(void){ cli(); diff --git a/src/keyboard/controller.h b/src/keyboard/controller.h index 9816155..c4d8006 100644 --- a/src/keyboard/controller.h +++ b/src/keyboard/controller.h @@ -52,41 +52,41 @@ // -------------------------------------------------------------------- -uint8_t kb_init(void); -uint8_t kb_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]); +u8 kb_init(void); +u8 kb_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]); // -------------------------------------------------------------------- #define MCP23018_TWI_ADDRESS 0b0100000 -uint8_t mcp23018_init(void); -uint8_t mcp23018_update_matrix( bool matrix[KB_ROWS][KB_COLUMNS] ); +u8 mcp23018_init(void); +u8 mcp23018_update_matrix( bool matrix[KB_ROWS][KB_COLUMNS] ); // -------------------------------------------------------------------- -uint8_t teensy_init(void); -uint8_t teensy_update_matrix( bool matrix[KB_ROWS][KB_COLUMNS] ); +u8 teensy_init(void); +u8 teensy_update_matrix( bool matrix[KB_ROWS][KB_COLUMNS] ); // -------------------------------------------------------------------- #define TWI_FREQ 400000 -void twi_init (void); -uint8_t twi_start (void); -void twi_stop (void); -uint8_t twi_send (uint8_t data); -uint8_t twi_read (uint8_t * data); +void twi_init (void); +u8 twi_start (void); +void twi_stop (void); +u8 twi_send (u8 data); +u8 twi_read (u8 * data); // -------------------------------------------------------------------- -void usb_init(void); // initialize everything -uint8_t usb_configured(void); // is the USB port configured +void usb_init(void); // initialize everything +u8 usb_configured(void); // is the USB port configured -int8_t usb_keyboard_send(void); -extern uint8_t keyboard_modifier_keys; -extern uint8_t keyboard_keys[6]; +i8 usb_keyboard_send(void); +extern u8 keyboard_modifier_keys; +extern u8 keyboard_keys[6]; -extern uint16_t consumer_key; +extern u16 consumer_key; // Everything below this point is only intended for usb_serial.c @@ -101,10 +101,10 @@ extern uint16_t consumer_key; #define EP_SINGLE_BUFFER 0x02 #define EP_DOUBLE_BUFFER 0x06 -#define EP_SIZE(s) ((s) == 64 ? 0x30 : \ - ((s) == 32 ? 0x20 : \ - ((s) == 16 ? 0x10 : \ - 0x00))) +#define EP_SIZE(s) ((s) == 64 ? 0x30 : \ + ((s) == 32 ? 0x20 : \ + ((s) == 16 ? 0x10 : \ + 0x00))) #define MAX_ENDPOINT 4 @@ -142,12 +142,12 @@ extern uint16_t consumer_key; // debug // ----------------------------------------------------------------- -extern volatile uint8_t debug_flush_timer; +extern volatile u8 debug_flush_timer; void usb_debug_free_memory(void); void usb_debug_print(const char *s); void usb_debug_printf(const char *fmt, ...); -int8_t usb_debug_putchar(char c); +i8 usb_debug_putchar(char c); void usb_debug_flush_output(void); #ifdef KBD_DEBUG diff --git a/src/main.c b/src/main.c index 75f65de..c9c667b 100644 --- a/src/main.c +++ b/src/main.c @@ -6,26 +6,16 @@ * Project located at * ------------------------------------------------------------------------- */ - #include #include -// -------------------------------------------------------------------- -// hardware -// -------------------------------------------------------------------- - -// comment out this define to disable the debug interface completely -// however, just not using the functions gets rid of most of the firmware bloat already -#define KBD_DEBUG -#include "./keyboard/controller.c" -#include "./keyboard/keyboard.h" - // -------------------------------------------------------------------- // types and forward declarations // -------------------------------------------------------------------- typedef int8_t i8; typedef uint8_t u8; +typedef int16_t i16; typedef uint16_t u16; typedef uint32_t u32; @@ -38,6 +28,16 @@ typedef void (*keyfunc)(keycode, bool); #include "./main.h" +// -------------------------------------------------------------------- +// hardware +// -------------------------------------------------------------------- + +// comment out this define to disable the debug interface completely +// however, just not using the functions gets rid of most of the firmware bloat already +#define KBD_DEBUG +#include "./keyboard/controller.c" +#include "./keyboard/keyboard.h" + // ---------------------------------------------------------------------------- // layout data // ----------------------------------------------------------------------------