master
Stefan Dorn 2016-08-22 16:28:10 +01:00
parent 6e94b69be4
commit 24891b6108
3 changed files with 453 additions and 457 deletions

View File

@ -14,11 +14,9 @@
* - 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
}
@ -27,11 +25,9 @@ uint8_t kb_init(void) {
* - 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
}
@ -41,7 +37,7 @@ uint8_t kb_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
// check options
#if (MCP23018__DRIVE_ROWS && MCP23018__DRIVE_COLUMNS) \
|| !(MCP23018__DRIVE_ROWS || MCP23018__DRIVE_COLUMNS)
#error "See 'Pin drive direction' in 'options.h'"
#error "See 'Pin drive direction' in 'options.h'"
#endif
// ----------------------------------------------------------------------------
@ -70,8 +66,8 @@ 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
@ -81,13 +77,13 @@ uint8_t mcp23018_init(void) {
ret = twi_send(TWI_ADDR_WRITE);
if (ret) goto out; // make sure we got an ACK
twi_send(IODIRA);
#if MCP23018__DRIVE_ROWS
#if MCP23018__DRIVE_ROWS
twi_send(0b11111111); // IODIRA
twi_send(0b11000000); // IODIRB
#elif MCP23018__DRIVE_COLUMNS
#elif MCP23018__DRIVE_COLUMNS
twi_send(0b10000000); // IODIRA
twi_send(0b11111111); // IODIRB
#endif
#endif
twi_stop();
// set pull-up
@ -98,13 +94,13 @@ uint8_t mcp23018_init(void) {
ret = twi_send(TWI_ADDR_WRITE);
if (ret) goto out; // make sure we got an ACK
twi_send(GPPUA);
#if MCP23018__DRIVE_ROWS
#if MCP23018__DRIVE_ROWS
twi_send(0b11111111); // GPPUA
twi_send(0b11000000); // GPPUB
#elif MCP23018__DRIVE_COLUMNS
#elif MCP23018__DRIVE_COLUMNS
twi_send(0b10000000); // GPPUA
twi_send(0b11111111); // GPPUB
#endif
#endif
twi_stop();
// set logical value (doesn't matter on inputs)
@ -128,11 +124,11 @@ 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
@ -143,8 +139,8 @@ uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
// 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++)
for (u8 row=0; row<=5; row++)
for (u8 col=0; col<=6; col++)
matrix[row][col] = 0;
return ret;
@ -154,8 +150,8 @@ uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
// --------------------------------------------------------------------
// update our part of the matrix
#if MCP23018__DRIVE_ROWS
for (uint8_t row=0; row<=5; row++) {
#if MCP23018__DRIVE_ROWS
for (u8 row=0; row<=5; row++) {
// set active row low : 0
// set other rows hi-Z : 1
twi_start();
@ -174,7 +170,7 @@ uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
twi_stop();
// update matrix
for (uint8_t col=0; col<=6; col++) {
for (u8 col=0; col<=6; col++) {
matrix[row][col] = !( data & (1<<col) );
}
}
@ -186,8 +182,8 @@ uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
twi_send(0xFF);
twi_stop();
#elif MCP23018__DRIVE_COLUMNS
for (uint8_t col=0; col<=6; col++) {
#elif MCP23018__DRIVE_COLUMNS
for (u8 col=0; col<=6; col++) {
// set active column low : 0
// set other columns hi-Z : 1
twi_start();
@ -206,7 +202,7 @@ uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
twi_stop();
// update matrix
for (uint8_t row=0; row<=5; row++) {
for (u8 row=0; row<=5; row++) {
matrix[row][col] = !( data & (1<<(5-row)) );
}
}
@ -218,7 +214,7 @@ uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
twi_send(0xFF);
twi_stop();
#endif
#endif
// /update our part of the matrix
// --------------------------------------------------------------------
@ -231,7 +227,7 @@ uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
// check options
#if (TEENSY__DRIVE_ROWS && TEENSY__DRIVE_COLUMNS) \
|| !(TEENSY__DRIVE_ROWS || TEENSY__DRIVE_COLUMNS)
#error "See 'Pin drive direction' in 'options.h'"
#error "See 'Pin drive direction' in 'options.h'"
#endif
// ----------------------------------------------------------------------------
@ -368,7 +364,7 @@ uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
/* returns
* - success: 0
*/
uint8_t teensy_init(void) {
u8 teensy_init(void) {
// CPU speed : should match F_CPU in makefile
#if F_CPU != 16000000
#error "Expecting different CPU frequency"
@ -419,7 +415,7 @@ uint8_t teensy_init(void) {
#error "Expecting different keyboard dimensions"
#endif
uint8_t teensy_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
u8 teensy_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
#if TEENSY__DRIVE_ROWS
update_columns_for_row(matrix, 0);
update_columns_for_row(matrix, 1);
@ -451,7 +447,7 @@ void twi_init(void) {
TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
}
uint8_t twi_start(void) {
u8 twi_start(void) {
// send start
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTA);
// wait for transmission to complete
@ -471,7 +467,7 @@ void twi_stop(void) {
;
}
uint8_t twi_send(uint8_t data) {
u8 twi_send(u8 data) {
// load data into the data register
TWDR = data;
// send data
@ -486,7 +482,7 @@ uint8_t twi_send(uint8_t data) {
return 0; // success
}
uint8_t twi_read(uint8_t *data) {
u8 twi_read(u8 *data) {
// read 1 byte to TWDR, send ACK
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
// wait for transmission to complete
@ -563,7 +559,7 @@ uint8_t twi_read(uint8_t *data) {
#define DEBUG_TX_SIZE 32
#define DEBUG_TX_BUFFER EP_DOUBLE_BUFFER
static const uint8_t PROGMEM endpoint_config_table[] = {
static const u8 PROGMEM endpoint_config_table[] = {
// enable, UECFG0X(type, direction), UECFG1X(size, bank, allocation)
1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KEYBOARD_SIZE) | KEYBOARD_BUFFER, // 1
1, EP_TYPE_INTERRUPT_IN, EP_SIZE(EXTRA_SIZE) | EXTRA_BUFFER, // 2
@ -588,7 +584,7 @@ static const uint8_t PROGMEM endpoint_config_table[] = {
// spec and relevant portions of any USB class specifications!
static const uint8_t PROGMEM device_descriptor[] = {
static const u8 PROGMEM device_descriptor[] = {
18, // bLength
1, // bDescriptorType
0x00, 0x02, // bcdUSB
@ -606,7 +602,7 @@ static const uint8_t PROGMEM device_descriptor[] = {
};
// Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
static const uint8_t PROGMEM keyboard_hid_report_desc[] = {
static const u8 PROGMEM keyboard_hid_report_desc[] = {
0x05, 0x01, // Usage Page (Generic Desktop),
0x09, 0x06, // Usage (Keyboard),
0xA1, 0x01, // Collection (Application),
@ -643,7 +639,7 @@ static const uint8_t PROGMEM keyboard_hid_report_desc[] = {
// audio controls & system controls
// http://www.microsoft.com/whdc/archive/w2kbd.mspx
static const uint8_t PROGMEM extra_hid_report_desc[] = {
static const u8 PROGMEM extra_hid_report_desc[] = {
/* consumer */
0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
0x09, 0x01, // USAGE (Consumer Control)
@ -659,7 +655,7 @@ static const uint8_t PROGMEM extra_hid_report_desc[] = {
0xc0, // END_COLLECTION
};
static const uint8_t PROGMEM nkro_hid_report_desc[] = {
static const u8 PROGMEM nkro_hid_report_desc[] = {
0x05, 0x01, // Usage Page (Generic Desktop),
0x09, 0x06, // Usage (Keyboard),
0xA1, 0x01, // Collection (Application),
@ -696,7 +692,7 @@ static const uint8_t PROGMEM nkro_hid_report_desc[] = {
// debug messages
#ifdef KBD_DEBUG
static const uint8_t PROGMEM debug_hid_report_desc[] = {
static const u8 PROGMEM debug_hid_report_desc[] = {
0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined)
0x09, 0x74, // Usage 0x74
0xA1, 0x53, // Collection 0x53
@ -729,7 +725,7 @@ static const uint8_t PROGMEM debug_hid_report_desc[] = {
#define DEBUG_HID_DESC_OFFSET (9+(9+9+7)*DEBUG_HID_DESC_NUM+9)
#define CONFIG1_DESC_SIZE (9+(9+9+7)*NUM_INTERFACES)
static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
static const u8 PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
// configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
9, // bLength;
2, // bDescriptorType;
@ -856,9 +852,9 @@ static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
// can be completely removed if iManufacturer, iProduct, iSerialNumber
// in the device desciptor are changed to zeros.
struct usb_string_descriptor_struct {
uint8_t bLength;
uint8_t bDescriptorType;
int16_t wString[];
u8 bLength;
u8 bDescriptorType;
i16 wString[];
};
static const struct usb_string_descriptor_struct PROGMEM string0 = {
4, 3, {0x0409}};
@ -870,10 +866,10 @@ static const struct usb_string_descriptor_struct PROGMEM string2 = {
// This table defines which descriptor data is sent for each specific
// request from the host (in wValue and wIndex).
static struct descriptor_list_struct {
uint16_t wValue;
uint16_t wIndex;
const uint8_t *addr;
uint8_t length;
u16 wValue;
u16 wIndex;
const u8 *addr;
u8 length;
} const PROGMEM descriptor_list[] = {
// DEVICE descriptor
{0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
@ -894,9 +890,9 @@ static struct descriptor_list_struct {
{0x2200, DEBUG_INTERFACE, debug_hid_report_desc, sizeof(debug_hid_report_desc)},
#endif
// 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)}};
{0x0300, 0x0000, (const u8 *)&string0, 4},
{0x0301, 0x0409, (const u8 *)&string1, sizeof(STR_MANUFACTURER)},
{0x0302, 0x0409, (const u8 *)&string2, sizeof(STR_PRODUCT)}};
#define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
@ -907,33 +903,33 @@ static struct descriptor_list_struct {
**************************************************************************/
// zero when we are not configured, non-zero when enumerated
static volatile uint8_t usb_configuration=0;
static volatile u8 usb_configuration=0;
// which modifier keys are currently pressed
// 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
// 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
uint8_t keyboard_modifier_keys=0;
u8 keyboard_modifier_keys=0;
// which keys are currently pressed, up to 6 keys may be down at once
uint8_t keyboard_keys[KBD_KEYS]={0,0,0,0,0,0};
u8 keyboard_keys[KBD_KEYS]={0,0,0,0,0,0};
// protocol setting from the host. We use exactly the same report
// either way, so this variable only stores the setting since we
// are required to be able to report which setting is in use.
static uint8_t keyboard_protocol=1;
static u8 keyboard_protocol=1;
// the idle configuration, how often we send the report to the
// host (ms * 4) even when it hasn't changed
static uint8_t keyboard_idle_config=125;
static u8 keyboard_idle_config=125;
// count until idle timeout
static uint8_t keyboard_idle_count=0;
static u8 keyboard_idle_count=0;
// which consumer key is currently pressed
uint16_t consumer_key;
uint16_t last_consumer_key;
u16 consumer_key;
u16 last_consumer_key;
volatile uint8_t debug_flush_timer=0;
volatile u8 debug_flush_timer=0;
/**************************************************************************
*
@ -956,13 +952,13 @@ void usb_init(void) {
}
// return 0 if the USB is not configured, or the configuration
// number selected by the HOST
uint8_t usb_configured(void) {
u8 usb_configured(void) {
return usb_configuration;
}
// send the contents of keyboard_keys and keyboard_modifier_keys
int8_t usb_keyboard_send(void) {
uint8_t i, intr_state, timeout;
i8 usb_keyboard_send(void) {
u8 i, intr_state, timeout;
if (!usb_configuration)
return -1;
@ -1004,9 +1000,9 @@ int8_t usb_keyboard_send(void) {
// the transmit buffer flushing is triggered by the start of frame
//
ISR(USB_GEN_vect) {
uint8_t intbits, i; // used to declare a variable `t` as well, but it
u8 intbits, i; // used to declare a variable `t` as well, but it
// wasn't used ::Ben Blazak, 2012::
static uint8_t div4 = 0;
static u8 div4 = 0;
intbits = UDINT;
UDINT = 0;
@ -1055,18 +1051,18 @@ static inline void usb_ack_out(void) { UEINTX = ~(1 << RXOUTI); }
// functions, and the start-of-frame interrupt.
//
ISR(USB_COM_vect) {
uint8_t intbits;
const uint8_t *list;
const uint8_t *cfg;
uint8_t i, n, len, en;
uint8_t bmRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
uint16_t desc_val;
const uint8_t *desc_addr;
uint8_t desc_length;
u8 intbits;
const u8 *list;
const u8 *cfg;
u8 i, n, len, en;
u8 bmRequestType;
u8 bRequest;
u16 wValue;
u16 wIndex;
u16 wLength;
u16 desc_val;
const u8 *desc_addr;
u8 desc_length;
UENUM = 0;
intbits = UEINTX;
@ -1081,7 +1077,7 @@ ISR(USB_COM_vect) {
wLength |= (UEDATX << 8);
UEINTX = ~((1 << RXSTPI) | (1 << RXOUTI) | (1 << TXINI));
if (bRequest == GET_DESCRIPTOR) {
list = (const uint8_t *)descriptor_list;
list = (const u8 *)descriptor_list;
for (i = 0;; i++) {
if (i >= 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();

View File

@ -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);
u8 twi_start (void);
void twi_stop (void);
uint8_t twi_send (uint8_t data);
uint8_t twi_read (uint8_t * data);
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
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
@ -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

View File

@ -6,26 +6,16 @@
* Project located at <https://github.com/benblazak/ergodox-firmware>
* ------------------------------------------------------------------------- */
#include <stdbool.h>
#include <stdint.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"
// --------------------------------------------------------------------
// 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
// ----------------------------------------------------------------------------