2012-03-19 08:01:41 +01:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* ergoDOX controller: MCP23018 specific code
|
|
|
|
* ----------------------------------------------------------------------------
|
2012-03-27 12:06:52 +02:00
|
|
|
* Copyright (c) 2012 Ben Blazak <benblazak.dev@gmail.com>
|
|
|
|
* Released under The MIT License (MIT) (see "license.md")
|
|
|
|
* Project located at <https://github.com/benblazak/ergodox-firmware>
|
2012-03-19 08:01:41 +01:00
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
2012-04-10 10:44:27 +02:00
|
|
|
// TODO: still working on all this
|
|
|
|
// - need to separate the 'check if everything's all right' function, i think
|
|
|
|
// - do more error checking in update_matrix() ?
|
|
|
|
// - does update_matrix() really need a helper function? can it be conditional?
|
|
|
|
// - [and lots of stuff, just need to clean it up] :)
|
2012-03-27 12:06:52 +02:00
|
|
|
|
|
|
|
#include <util/twi.h>
|
|
|
|
|
2012-04-02 10:59:00 +02:00
|
|
|
#include "lib/data-types.h"
|
2012-04-10 10:44:27 +02:00
|
|
|
#include "lib/teensy-2-0/twi.h" // `TWI_FREQ` defined in "teensy-2-0.c"
|
2012-04-02 10:59:00 +02:00
|
|
|
|
2012-04-10 10:44:27 +02:00
|
|
|
#define KEYBOARD_INCLUDE_PRIVATE
|
2012-04-02 10:59:00 +02:00
|
|
|
#include "matrix.h"
|
2012-03-27 12:06:52 +02:00
|
|
|
#include "mcp23018.h"
|
2012-03-19 08:01:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
// 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
|
2012-04-10 10:44:27 +02:00
|
|
|
#define GPIOA 0x12 // general purpose i/o port register (write modifies OLAT)
|
2012-03-19 08:01:41 +01:00
|
|
|
#define GPIOB 0x13
|
|
|
|
#define OLATA 0x14 // output latch register
|
|
|
|
#define OLATB 0x15
|
|
|
|
|
2012-04-02 10:59:00 +02:00
|
|
|
// TWI aliases
|
|
|
|
#define TWI_ADDR_WRITE ( (MCP23018_TWI_ADDRESS<<1) | TW_WRITE )
|
|
|
|
#define TWI_ADDR_READ ( (MCP23018_TWI_ADDRESS<<1) | TW_READ )
|
2012-04-01 02:02:55 +02:00
|
|
|
|
|
|
|
|
2012-04-10 10:44:27 +02:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/* returns:
|
2012-04-02 10:59:00 +02:00
|
|
|
* - success: 0
|
|
|
|
* - failure: twi status code
|
2012-04-10 10:44:27 +02:00
|
|
|
*
|
|
|
|
* notes:
|
|
|
|
* - this checks whether the device is initialized by reading from it. not the
|
|
|
|
* most efficient method, but easy and fairly reliable.
|
2012-04-02 10:59:00 +02:00
|
|
|
*/
|
2012-04-10 10:44:27 +02:00
|
|
|
static uint8_t _init(bool release_twi_bus) {
|
|
|
|
static bool initialized;
|
|
|
|
uint8_t ret, data;
|
2012-04-01 02:02:55 +02:00
|
|
|
|
2012-04-10 10:44:27 +02:00
|
|
|
// check for errors and previous initialization
|
2012-04-01 02:02:55 +02:00
|
|
|
|
2012-04-02 10:59:00 +02:00
|
|
|
twi_start();
|
|
|
|
ret = twi_send(TWI_ADDR_WRITE);
|
2012-04-10 10:44:27 +02:00
|
|
|
|
|
|
|
if (ret) goto out; // address write failed (no ACK)
|
|
|
|
|
|
|
|
twi_send(OLATA);
|
|
|
|
twi_start();
|
|
|
|
twi_send(TWI_ADDR_READ);
|
|
|
|
ret = twi_read(&data);
|
|
|
|
|
|
|
|
if (ret) goto out; // read failed
|
|
|
|
if (data == 0xFF) goto out; // already initialized (OLATA == 0xFF)
|
|
|
|
|
|
|
|
// initialize things, if we need to and we can
|
2012-04-01 02:02:55 +02:00
|
|
|
|
2012-04-02 10:59:00 +02:00
|
|
|
// set pin direction
|
|
|
|
// - unused : input : 1
|
|
|
|
// - rows : output : 0
|
|
|
|
// - columns : input : 1
|
|
|
|
twi_start();
|
|
|
|
twi_send(TWI_ADDR_WRITE);
|
|
|
|
twi_send(IODIRA);
|
|
|
|
twi_send(0b11000000); // IODIRA
|
|
|
|
twi_send(0b11111111); // IODIRB
|
2012-03-19 08:01:41 +01:00
|
|
|
|
2012-04-02 10:59:00 +02:00
|
|
|
// set pull-up
|
2012-04-05 12:08:20 +02:00
|
|
|
// - unused : on : 1
|
|
|
|
// - rows : off : 0
|
|
|
|
// - columns : on : 1
|
2012-04-02 10:59:00 +02:00
|
|
|
twi_start();
|
|
|
|
twi_send(TWI_ADDR_WRITE);
|
|
|
|
twi_send(GPPUA);
|
2012-04-05 12:08:20 +02:00
|
|
|
twi_send(0b11000000); // GPPUA
|
2012-04-02 10:59:00 +02:00
|
|
|
twi_send(0b11111111); // GPPUB
|
2012-03-19 08:01:41 +01:00
|
|
|
|
2012-04-05 12:08:20 +02:00
|
|
|
// set logical value (doesn't matter on inputs)
|
|
|
|
// - unused : high (hi-Z) : 1
|
|
|
|
// - rows : high (hi-Z) : 1
|
|
|
|
// - columns : high (hi-Z) : 1
|
2012-04-02 10:59:00 +02:00
|
|
|
twi_start();
|
|
|
|
twi_send(TWI_ADDR_WRITE);
|
2012-04-10 10:44:27 +02:00
|
|
|
twi_send(OLATA);
|
|
|
|
twi_send(0b11111111); //OLATA
|
|
|
|
twi_send(0b11111111); //OLATB
|
2012-04-05 12:08:20 +02:00
|
|
|
|
2012-04-10 10:44:27 +02:00
|
|
|
out:
|
|
|
|
if (release_twi_bus)
|
|
|
|
twi_stop();
|
|
|
|
|
|
|
|
if (ret) initialized = false;
|
|
|
|
else initialized = true;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/* returns:
|
|
|
|
* - success: 0
|
|
|
|
* - failure: twi status code
|
|
|
|
*/
|
|
|
|
uint8_t mcp23018_init(void) {
|
|
|
|
return _init(true);
|
2012-03-27 12:06:52 +02:00
|
|
|
}
|
2012-03-19 08:01:41 +01:00
|
|
|
|
2012-04-05 12:08:20 +02:00
|
|
|
/* returns:
|
2012-04-02 10:59:00 +02:00
|
|
|
* - success: 0
|
|
|
|
* - failure: twi status code
|
|
|
|
*/
|
2012-04-05 12:08:20 +02:00
|
|
|
#if KB_ROWS != 12 || KB_COLUMNS != 7
|
|
|
|
#error "Expecting different keyboard dimensions"
|
|
|
|
#endif
|
2012-04-10 10:44:27 +02:00
|
|
|
uint8_t mcp23018_update_matrix(bool matrix[KB_ROWS][KB_COLUMNS]) {
|
2012-04-02 10:59:00 +02:00
|
|
|
uint8_t ret, data;
|
|
|
|
|
2012-04-10 10:44:27 +02:00
|
|
|
// initialize things if necessary
|
|
|
|
ret = _init(false);
|
|
|
|
|
|
|
|
// if there was an error
|
2012-04-02 10:59:00 +02:00
|
|
|
if (ret) {
|
2012-04-10 10:44:27 +02:00
|
|
|
// clear the matrix
|
|
|
|
for (uint8_t row=0x6; row<=0xB; row++)
|
|
|
|
for (uint8_t col=0; col<=6; col++)
|
|
|
|
matrix[row][col] = 0;
|
|
|
|
|
|
|
|
// release the twi bus and return
|
2012-04-02 10:59:00 +02:00
|
|
|
twi_stop();
|
|
|
|
return ret;
|
|
|
|
}
|
2012-04-01 02:02:55 +02:00
|
|
|
|
2012-04-10 10:44:27 +02:00
|
|
|
// update our part of the matrix
|
2012-04-02 10:59:00 +02:00
|
|
|
for (uint8_t row=0x6; row<=0xB; row++) {
|
2012-04-10 10:44:27 +02:00
|
|
|
// set active row low : 0
|
|
|
|
// set other rows high (hi-Z) : 1
|
2012-04-02 10:59:00 +02:00
|
|
|
twi_start();
|
|
|
|
twi_send(TWI_ADDR_WRITE);
|
2012-04-10 10:44:27 +02:00
|
|
|
twi_send(OLATA);
|
|
|
|
twi_send( 0b11111111 & ~(1<<(row-6)) ); // OLATA
|
2012-04-02 10:59:00 +02:00
|
|
|
|
|
|
|
// get column data
|
|
|
|
twi_start();
|
2012-04-10 10:44:27 +02:00
|
|
|
twi_send(TWI_ADDR_WRITE);
|
|
|
|
twi_send(GPIOB);
|
2012-04-02 10:59:00 +02:00
|
|
|
twi_send(TWI_ADDR_READ);
|
2012-04-10 10:44:27 +02:00
|
|
|
twi_read(&data); // GPIOB
|
2012-04-02 10:59:00 +02:00
|
|
|
|
|
|
|
// update matrix
|
|
|
|
for (uint8_t col=0; col<=6; col++)
|
|
|
|
matrix[row][col] = !( data & (1<<col) );
|
|
|
|
}
|
2012-04-01 02:02:55 +02:00
|
|
|
|
2012-04-10 10:44:27 +02:00
|
|
|
// set all rows high (hi-Z) : 1
|
2012-04-06 09:19:57 +02:00
|
|
|
twi_start();
|
|
|
|
twi_send(TWI_ADDR_WRITE);
|
|
|
|
twi_send(GPIOA);
|
|
|
|
twi_send(0b11111111);
|
|
|
|
|
2012-04-10 10:44:27 +02:00
|
|
|
// release the twi bus and return
|
2012-04-02 10:59:00 +02:00
|
|
|
twi_stop();
|
2012-04-05 12:08:20 +02:00
|
|
|
return 0; // success
|
2012-03-27 12:06:52 +02:00
|
|
|
}
|
2012-04-02 10:59:00 +02:00
|
|
|
|