diff --git a/doc/references.md b/doc/references.md index 2ff1d7c..a2a34d6 100644 --- a/doc/references.md +++ b/doc/references.md @@ -40,6 +40,13 @@ ## C Stuff +* [comp.lang.c Frequently Asked Questions] + (http://c-faq.com/index.html) + +* [The C Preprocessor] + (http://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_1.html#SEC1) + (on ) + * [C Library Reference] (http://www.cplusplus.com/reference/) (on ) diff --git a/src/keyboard/ergodox/layout.h b/src/keyboard/ergodox/layout.h index ffc3445..861792c 100644 --- a/src/keyboard/ergodox/layout.h +++ b/src/keyboard/ergodox/layout.h @@ -14,27 +14,31 @@ #define KB_LAYERS 1 // anything >= 1, as long as there's memory + #if KB_ROWS != 12 || KB_COLUMNS != 7 + #error "Expecting different keyboard dimensions" + #endif + extern const uint8_t kb_layout[KB_LAYERS][KB_ROWS][KB_COLUMNS] = { // TODO (before release): put more effort into this // ------- layer 0: default ------- // --- right hand --- - /* 0 ---------- 1 --------------- 2 -------------- 3 ----------------- 4 --------------------- 5 -------------------------- 6 -------------------------- */ -/* 0 */ 0, KEY_6_Caret, KEY_7_Ampersand, KEY_8_Asterisk, KEY_9_LeftParenthesis, KEY_0_RightParenthesis, KEY_Dash_Underscore, /* 0 */ -/* 1 */ 0, KEY_y_Y, KEY_u_U, KEY_i_I, KEY_o_O, KEY_p_P, KEY_LeftBracket_LeftBrace, /* 1 */ -/* 2 */ 0,/*unused*/ KEY_h_H, KEY_j_J, KEY_k_K, KEY_l_L, KEY_Semicolon_Colon, KEY_SingleQuote_DoubleQuote, /* 2 */ -/* 3 */ 0, KEY_n_N, KEY_m_M, KEY_Comma_LessThan, KEY_Period_GreaterThan, KEY_Slash_Question, KEY_RightShift, /* 3 */ -/* 4 */ 0,/*unused*/ 0,/*unused*/ KEY_UpArrow, KEY_DownArrow, KEY_Backslash_Pipe, KEY_RightBracket_RightBrace, 0, /* 4 */ -/* 5 */ 0,/*unused*/ KEY_RightControl, KEY_RightAlt, KEY_PageUp, KEY_PageDown, KEY_ReturnEnter, KEY_Spacebar, /* 5 */ - /* 0 ---------- 1 --------------- 2 -------------- 3 ----------------- 4 --------------------- 5 -------------------------- 6 -------------------------- */ + /* 0 -------------------------- 1 -------------------------- 2 -------------------------- 3 -------------------------- 4 -------------------------- 5 -------------------------- 6 -------------------------- */ +/* 0 */ 0, KEY_6_Caret, KEY_7_Ampersand, KEY_8_Asterisk, KEY_9_LeftParenthesis, KEY_0_RightParenthesis, KEY_Dash_Underscore, /* 0 */ +/* 1 */ 0, KEY_y_Y, KEY_u_U, KEY_i_I, KEY_o_O, KEY_p_P, KEY_LeftBracket_LeftBrace, /* 1 */ +/* 2 */ 0,/*unused*/ KEY_h_H, KEY_j_J, KEY_k_K, KEY_l_L, KEY_Semicolon_Colon, KEY_SingleQuote_DoubleQuote, /* 2 */ +/* 3 */ 0, KEY_n_N, KEY_m_M, KEY_Comma_LessThan, KEY_Period_GreaterThan, KEY_Slash_Question, KEY_RightShift, /* 3 */ +/* 4 */ 0,/*unused*/ 0,/*unused*/ KEY_UpArrow, KEY_DownArrow, KEY_Backslash_Pipe, KEY_RightBracket_RightBrace, 0, /* 4 */ +/* 5 */ 0,/*unused*/ KEY_RightControl, KEY_RightAlt, KEY_PageUp, KEY_PageDown, KEY_ReturnEnter, KEY_Spacebar, /* 5 */ + /* 0 -------------------------- 1 -------------------------- 2 -------------------------- 3 -------------------------- 4 -------------------------- 5 -------------------------- 6 -------------------------- */ // --- left hand --- - /* 0 ------------------ 1 -------------------- 2 ----------------- 3 ------------ 4 -------------- 5 ------------ 6 ----------- */ -/* 6 */ KEY_Equal_Plus, KEY_1_Exclamation, KEY_2_At, KEY_3_Pound, KEY_4_Dollar, KEY_5_Percent, 0, /* 6 */ -/* 7 */ KEY_Tab, KEY_q_Q, KEY_w_W, KEY_e_E, KEY_r_R, KEY_t_T, 0, /* 7 */ -/* 8 */ KEY_CapsLock, KEY_a_A, KEY_s_S, KEY_d_D, KEY_f_F, KEY_g_G, 0,/*unused*/ /* 8 */ -/* 9 */ KEY_LeftShift, KEY_z_Z, KEY_x_X, KEY_c_C, KEY_v_V, KEY_b_B, 0, /* 9 */ -/* A */ 0, KEY_GraveAccent_Tilde, KEY_Backslash_Pipe, KEY_LeftArrow, KEY_RightArrow, 0,/*unused*/ 0,/*unused*/ /* A */ -/* B */ KEY_DeleteBackspace, KEY_DeleteForward, KEY_End, KEY_Home, KEY_LeftAlt, KEY_LeftControl, 0, 0 /*unused*/ /* B */ - /* 0 ------------------ 1 -------------------- 2 ----------------- 3 ------------ 4 -------------- 5 ------------ 6 ----------- */ + /* 0 -------------------------- 1 -------------------------- 2 -------------------------- 3 -------------------------- 4 -------------------------- 5 -------------------------- 6 -------------------------- */ +/* 6 */ KEY_Equal_Plus, KEY_1_Exclamation, KEY_2_At, KEY_3_Pound, KEY_4_Dollar, KEY_5_Percent, 0, /* 6 */ +/* 7 */ KEY_Tab, KEY_q_Q, KEY_w_W, KEY_e_E, KEY_r_R, KEY_t_T, 0, /* 7 */ +/* 8 */ KEY_CapsLock, KEY_a_A, KEY_s_S, KEY_d_D, KEY_f_F, KEY_g_G, 0,/*unused*/ /* 8 */ +/* 9 */ KEY_LeftShift, KEY_z_Z, KEY_x_X, KEY_c_C, KEY_v_V, KEY_b_B, 0, /* 9 */ +/* A */ 0, KEY_GraveAccent_Tilde, KEY_Backslash_Pipe, KEY_LeftArrow, KEY_RightArrow, 0,/*unused*/ 0,/*unused*/ /* A */ +/* B */ KEY_DeleteBackspace, KEY_DeleteForward, KEY_End, KEY_Home, KEY_LeftAlt, KEY_LeftControl, 0, 0 /*unused*/ /* B */ + /* 0 -------------------------- 1 -------------------------- 2 -------------------------- 3 -------------------------- 4 -------------------------- 5 -------------------------- 6 -------------------------- */ } #endif diff --git a/src/keyboard/ergodox/matrix.h b/src/keyboard/ergodox/matrix.h index f3c4fa5..89fc4ae 100644 --- a/src/keyboard/ergodox/matrix.h +++ b/src/keyboard/ergodox/matrix.h @@ -15,7 +15,7 @@ #define KB_ROWS 12 // must match real life #define KB_COLUMNS 7 // must match real life - extern bool kb_is_pressed[KB_ROWS][KB_COLUMNS] = {}; + extern bool kb_is_pressed[KB_ROWS][KB_COLUMNS] = {false}; #endif diff --git a/src/keyboard/ergodox/mcp23018.c b/src/keyboard/ergodox/mcp23018.c index 8d9b165..3733084 100644 --- a/src/keyboard/ergodox/mcp23018.c +++ b/src/keyboard/ergodox/mcp23018.c @@ -6,33 +6,16 @@ * Project located at * ------------------------------------------------------------------------- */ -// TODO: this is not working yet - - // see if the device is ready - // - success: set `mcp23018_ready = true` and continue initializing - // - failure: return `error`; we can try again later - - // set pin direction - // - unused : input : 1 - // - rows : output : 0 - // - columns : input : 1 - - // set pull-up - // - unused : on : 1 - // - rows : on : 1 - // - columns : on : 1 - - // set output pins high - // - rows : high : 1 - // - other : low : 0 (or ignored) - #include +#include "lib/data-types.h" +#define TWI_FREQ 400000 +#include "lib/teensy-2-0/twi.h" + +#include "matrix.h" #define MCP23018_h_INCLUDE_PRIVATE #include "mcp23018.h" -#include "teensy-2-0.h" -#include "lib/data-types.h" // register addresses (see "mcp23018.md") @@ -45,83 +28,106 @@ #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 ) -// ---------------------------------------------------------------------------- -// dbg -// ---------------------------------------------------------------------------- -#include -#include +// error check +#if KB_ROWS != 12 || KB_COLUMNS != 7 + #error "Expecting different keyboard dimensions" +#endif -#define blink_led(time1, time2) { \ - /* Teensy 2.0 onboard LED on PD6 - on high, off low */ \ - PORTD |= (1<<6); \ - _delay_ms(time1); \ - PORTD &= ~(1<<6); \ - _delay_ms(time2); \ -} - -void blink_hex(uint8_t num) { - // initial blink (get ready) - blink_led(700, 200); - // 1st hex number - for (uint8_t i=0; i<(num/0x10); i++) { - blink_led(200, 100); - } - _delay_ms(400); - // 2nd hex number - for (uint8_t i=0; i<(num%0x10); i++) { - blink_led(200, 100); - } -} - -// ---------------------------------------------------------------------------- // TWI -// ---------------------------------------------------------------------------- - -void twi_init(void) { - TWSR &= ~( (1< * @@ -76,6 +78,9 @@ * All addresses given for IOCON.BANK = 0, since that's the default value of the bit, and that's what we'll be using. * Outputs are open drain, so we want pull-up resistors set for everything. + * We want the row pins set as output high initially, and the column pins set + as input. We'll cycle through driving the row pins low and checking the + column pins in the update function. * abbreviations: * IODIR = I/O Direction Register diff --git a/src/keyboard/ergodox/teensy-2-0.c b/src/keyboard/ergodox/teensy-2-0.c index 34bf457..56fbb5b 100644 --- a/src/keyboard/ergodox/teensy-2-0.c +++ b/src/keyboard/ergodox/teensy-2-0.c @@ -26,6 +26,10 @@ #define CPU_62kHz 0x08 +/* returns: + * - success: 0 + * + will never return failure + */ uint8_t teensy_init(void) { CPU_PRESCALE(CPU_16MHz); // speed should match F_CPU in makefile diff --git a/src/keyboard/ergodox/teensy-2-0.md b/src/keyboard/ergodox/teensy-2-0.md index f4f3cbb..507d910 100644 --- a/src/keyboard/ergodox/teensy-2-0.md +++ b/src/keyboard/ergodox/teensy-2-0.md @@ -1,6 +1,5 @@ # Documentation : Teensy 2.0 - ## Pinouts and Pin assignments * `+` indicates pin @@ -68,7 +67,7 @@ LED, so there's no reason to set internal pull-up enabled on it. If we do, it will source current to the LED, which is fine, but unnecessary. * We want the row pins 'drive high' initially, and the column pins set as - input with internal pull-up. We'll cycle through driving the row pins low, + input with internal pull-up. We'll cycle through driving the row pins low and checking the column pins in the update function. ### PWM on ports OC1(A|B|C) (see datasheet section 14.10) @@ -103,30 +102,6 @@ * OCR = Output Compare Register * TCCR = Timer/Counter Control Register - -## I²C Status Codes (for Master modes) - -### Master Transmitter - -* `0x08` A START condition has been transmitted -* `0x10` A repeated START condition has been transmitted -* `0x18` SLA+W has been transmitted; ACK has been received -* `0x20` SLA+W has been transmitted; NOT ACK has been received -* `0x28` Data byte has been transmitted; ACK has been received -* `0x30` Data byte has been transmitted; NOT ACK has been received -* `0x38` Arbitration lost in SLA+W or data bytes - -### Master Receiver - -* `0x08` A START condition has been transmitted -* `0x10` A repeated START condition has been transmitted -* `0x38` Arbitration lost in SLA+R or NOT ACK bit -* `0x40` SLA+R has been transmitted; ACK has been received -* `0x48` SLA+R has been transmitted; NOT ACK has been received -* `0x50` Data byte has been received; ACK has been returned -* `0x58` Data byte has been received; NOT ACK has been returned - - ------------------------------------------------------------------------------- Copyright © 2012 Ben Blazak diff --git a/src/lib-other/readme.md b/src/lib-other/readme.md index 9cae856..69b2d11 100644 --- a/src/lib-other/readme.md +++ b/src/lib-other/readme.md @@ -1,3 +1,6 @@ + # src/lib-other Files taken from other projects diff --git a/src/lib/teensy-2-0/twi.c b/src/lib/teensy-2-0/twi.c new file mode 100644 index 0000000..4e77f38 --- /dev/null +++ b/src/lib/teensy-2-0/twi.c @@ -0,0 +1,79 @@ +/* ---------------------------------------------------------------------------- + * Very simple Teensy 2.0 TWI library : code + * + * - This is mostly straight from the datasheet, section 20.6.6, figure 20-11 + * (the code example in C), and section 20.8.1, figure 20-12 + * - Also see the documentation for `` at + * + * Some other (more complete) TWI libraries for the Teensy 2.0 (and other Amtel + * processors): + * - [i2cmaster] (http://homepage.hispeed.ch/peterfleury/i2cmaster.zip) + * - written by [peter-fleury] (http://homepage.hispeed.ch/peterfleury/) + * - [the arduino twi library] (https://github.com/arduino/Arduino/tree/master/libraries/Wire/utility) + * - look for an older version if you need one that doesn't depend on all the + * other Arduino stuff + * ---------------------------------------------------------------------------- + * Copyright (c) 2012 Ben Blazak + * Released under The MIT License (MIT) (see "license.md") + * Project located at + * ------------------------------------------------------------------------- */ + + +#include + +#include "twi.h" + + +void twi_init(void) { + // set the prescaler value to 0 + TWSR &= ~( (1< + * Released under The MIT License (MIT) (see "license.md") + * Project located at + * ------------------------------------------------------------------------- */ + + +#ifndef TWI_h + #define TWI_h + + #ifndef TWI_FREQ + #define TWI_FREQ 100000 + #endif + + + 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); + +#endif + diff --git a/src/lib/teensy-2-0/twi.md b/src/lib/teensy-2-0/twi.md new file mode 100644 index 0000000..49c2126 --- /dev/null +++ b/src/lib/teensy-2-0/twi.md @@ -0,0 +1,30 @@ +# Documentation : Teensy 2.0 I²C + +## I²C Status Codes (for Master modes) + +### Master Transmitter (datasheet section 20.8.1, table 20-3) + +* `0x08` A START condition has been transmitted +* `0x10` A repeated START condition has been transmitted +* `0x18` SLA+W has been transmitted; ACK has been received +* `0x20` SLA+W has been transmitted; NOT ACK has been received +* `0x28` Data byte has been transmitted; ACK has been received +* `0x30` Data byte has been transmitted; NOT ACK has been received +* `0x38` Arbitration lost in SLA+W or data bytes + +### Master Receiver (datasheet section 20.8.2, table 20-4) + +* `0x08` A START condition has been transmitted +* `0x10` A repeated START condition has been transmitted +* `0x38` Arbitration lost in SLA+R or NOT ACK bit +* `0x40` SLA+R has been transmitted; ACK has been received +* `0x48` SLA+R has been transmitted; NOT ACK has been received +* `0x50` Data byte has been received; ACK has been returned +* `0x58` Data byte has been received; NOT ACK has been returned + +------------------------------------------------------------------------------- + +Copyright © 2012 Ben Blazak +Released under The MIT License (MIT) (see "license.md") +Project located at +