mirror of
https://github.com/qmk/qmk_firmware
synced 2024-12-22 08:26:21 +00:00
I2C driver cleanup (#21273)
* remove i2c_start and i2c_stop from i2c drivers * remove static i2c_address variable from chibios i2c driver
This commit is contained in:
parent
2b0965944d
commit
e9bd7d7ad3
@ -138,23 +138,6 @@ void i2c_init(void) {
|
||||
|
||||
---
|
||||
|
||||
### `i2c_status_t i2c_start(uint8_t address, uint16_t timeout)` :id=api-i2c-start
|
||||
|
||||
Start an I2C transaction.
|
||||
|
||||
#### Arguments :id=api-i2c-start-arguments
|
||||
|
||||
- `uint8_t address`
|
||||
The 7-bit I2C address of the device (ie. without the read/write bit - this will be set automatically).
|
||||
- `uint16_t timeout`
|
||||
The time in milliseconds to wait for a response from the target device.
|
||||
|
||||
#### Return Value :id=api-i2c-start-return
|
||||
|
||||
`I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
|
||||
|
||||
---
|
||||
|
||||
### `i2c_status_t i2c_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout)` :id=api-i2c-transmit
|
||||
|
||||
Send multiple bytes to the selected I2C device.
|
||||
@ -285,6 +268,21 @@ Reads from a register with a 16-bit address (big endian) on the I2C device.
|
||||
|
||||
---
|
||||
|
||||
### `i2c_status_t i2c_stop(void)` :id=api-i2c-stop
|
||||
### `i2c_status_t i2c_ping_address(uint8_t address, uint16_t timeout)` :id=api-i2c-ping-address
|
||||
|
||||
Stop the current I2C transaction.
|
||||
Pings the I2C bus for a specific address.
|
||||
|
||||
On ChibiOS a "best effort" attempt is made by reading a single byte from register 0 at the requested address. This should generally work except for I2C devices that do not not respond to a register 0 read request, which will result in a false negative result (unsucessful response to ping attempt).
|
||||
|
||||
This function is weakly defined, meaning it can be overridden if necessary for your particular use case:
|
||||
|
||||
#### Arguments
|
||||
|
||||
- `uint8_t address`
|
||||
The 7-bit I2C address of the device (ie. without the read/write bit - this will be set automatically).
|
||||
- `uint16_t timeout`
|
||||
The time in milliseconds to wait for a response from the target device.
|
||||
|
||||
#### Return Value
|
||||
|
||||
`I2C_STATUS_TIMEOUT` if the timeout period elapses, `I2C_STATUS_ERROR` if some other error occurs, otherwise `I2C_STATUS_SUCCESS`.
|
||||
|
@ -23,12 +23,11 @@ I2C アドレスと他の技術詳細について、さらなる情報を得る
|
||||
| 関数 | 説明 |
|
||||
|-------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `void i2c_init(void);` | I2C ドライバを初期化します。他のあらゆるトランザクションを開始する前に、この関数を一度だけ呼ぶ必要があります。 |
|
||||
| `i2c_status_t i2c_start(uint8_t address, uint16_t timeout);` | I2C トランザクションを開始します。アドレスは方向ビットのない7ビットスレーブアドレスです。 |
|
||||
| `i2c_status_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout);` | I2C 経由でデータを送信します。アドレスは方向ビットのない7ビットスレーブアドレスです。トランザクションのステータスを返します。 |
|
||||
| `i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout);` | I2C 経由でデータを受信します。アドレスは方向ビットのない7ビットスレーブアドレスです。 `length` で指定した長さのバイト列を `data` に保存し、トランザクションのステータスを返します。 |
|
||||
| `i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout);` | `i2c_transmit` と同様ですが、 `regaddr` でスレーブのデータ書き込み先のレジスタを指定します。 |
|
||||
| `i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout);` | `i2c_receive` と同様ですが、 `regaddr` でスレーブのデータ読み込み先のレジスタを指定します。 |
|
||||
| `i2c_status_t i2c_stop(void);` | I2C トランザクションを終了します。 |
|
||||
| `i2c_status_t i2c_ping_address(uint8_t address, uint16_t timeout);` | I2C アドレスをテストします。アドレスは方向ビットのない7ビットスレーブアドレスです。 |
|
||||
|
||||
### 関数の戻り値 :id=function-return
|
||||
|
||||
|
@ -41,8 +41,6 @@ void pca9505_init(uint8_t slave_addr) {
|
||||
}
|
||||
|
||||
// TODO: could check device connected
|
||||
// i2c_start(SLAVE_TO_ADDR(slave) | I2C_WRITE);
|
||||
// i2c_stop();
|
||||
}
|
||||
|
||||
bool pca9505_set_config(uint8_t slave_addr, pca9505_port_t port, uint8_t conf) {
|
||||
|
@ -29,8 +29,6 @@ void pca9555_init(uint8_t slave_addr) {
|
||||
}
|
||||
|
||||
// TODO: could check device connected
|
||||
// i2c_start(SLAVE_TO_ADDR(slave) | I2C_WRITE);
|
||||
// i2c_stop();
|
||||
}
|
||||
|
||||
bool pca9555_set_config(uint8_t slave_addr, pca9555_port_t port, uint8_t conf) {
|
||||
|
@ -223,13 +223,8 @@ __attribute__((weak)) bool oled_send_cmd_P(const uint8_t *data, uint16_t size) {
|
||||
spi_stop();
|
||||
return (status >= 0);
|
||||
# elif defined(OLED_TRANSPORT_I2C)
|
||||
i2c_status_t status = i2c_start((OLED_DISPLAY_ADDRESS << 1) | I2C_WRITE, OLED_I2C_TIMEOUT);
|
||||
|
||||
for (uint16_t i = 0; i < size && status >= 0; i++) {
|
||||
status = i2c_write(pgm_read_byte((const char *)data++), OLED_I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
i2c_stop();
|
||||
i2c_status_t status = i2c_transmit_P((OLED_DISPLAY_ADDRESS << 1), data, size, OLED_I2C_TIMEOUT);
|
||||
|
||||
return (status == I2C_STATUS_SUCCESS);
|
||||
# endif
|
||||
|
@ -28,18 +28,14 @@ bool qp_comms_i2c_init(painter_device_t device) {
|
||||
}
|
||||
|
||||
bool qp_comms_i2c_start(painter_device_t device) {
|
||||
painter_driver_t * driver = (painter_driver_t *)device;
|
||||
qp_comms_i2c_config_t *comms_config = (qp_comms_i2c_config_t *)driver->comms_config;
|
||||
return i2c_start(comms_config->chip_address << 1) == I2C_STATUS_SUCCESS;
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t qp_comms_i2c_send_data(painter_device_t device, const void *data, uint32_t byte_count) {
|
||||
return qp_comms_i2c_send_raw(device, data, byte_count);
|
||||
}
|
||||
|
||||
void qp_comms_i2c_stop(painter_device_t device) {
|
||||
i2c_stop();
|
||||
}
|
||||
void qp_comms_i2c_stop(painter_device_t device) {}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Command+Data I2C support
|
||||
|
@ -19,7 +19,6 @@ void RAP_ReadBytes(uint8_t address, uint8_t* data, uint8_t count) {
|
||||
pd_dprintf("error cirque_pinnacle i2c_read_register\n");
|
||||
touchpad_init = false;
|
||||
}
|
||||
i2c_stop();
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,6 +31,5 @@ void RAP_Write(uint8_t address, uint8_t data) {
|
||||
pd_dprintf("error cirque_pinnacle i2c_write_register\n");
|
||||
touchpad_init = false;
|
||||
}
|
||||
i2c_stop();
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ static uint16_t i2c_timeout_timer;
|
||||
|
||||
void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
|
||||
uint8_t data[] = {0x00, red, green, blue, white};
|
||||
i2c_transmit(TRACKBALL_WRITE, data, sizeof(data), I2C_TIMEOUT);
|
||||
i2c_transmit(TRACKBALL_ADDRESS, data, sizeof(data), I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
int16_t mouse_offset(uint8_t positive, uint8_t negative, int16_t scale) {
|
||||
@ -120,7 +120,7 @@ bool pointing_device_task(void) {
|
||||
static uint16_t debounce_timer;
|
||||
uint8_t state[5] = {};
|
||||
if (timer_elapsed(i2c_timeout_timer) > I2C_WAITCHECK) {
|
||||
if (i2c_read_register(TRACKBALL_WRITE, 0x04, state, 5, I2C_TIMEOUT) == I2C_STATUS_SUCCESS) {
|
||||
if (i2c_read_register(TRACKBALL_ADDRESS, 0x04, state, 5, I2C_TIMEOUT) == I2C_STATUS_SUCCESS) {
|
||||
if (!state[4] && !debounce) {
|
||||
if (scrolling) {
|
||||
#ifdef PIMORONI_TRACKBALL_INVERT_X
|
||||
|
@ -20,10 +20,8 @@
|
||||
#include "pointing_device.h"
|
||||
|
||||
#ifndef TRACKBALL_ADDRESS
|
||||
# define TRACKBALL_ADDRESS 0x0A
|
||||
# define TRACKBALL_ADDRESS (0x0A << 1)
|
||||
#endif
|
||||
#define TRACKBALL_WRITE ((TRACKBALL_ADDRESS << 1) | I2C_WRITE)
|
||||
#define TRACKBALL_READ ((TRACKBALL_ADDRESS << 1) | I2C_READ)
|
||||
|
||||
void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white);
|
||||
void trackball_check_click(bool pressed, report_mouse_t *mouse);
|
||||
|
@ -137,23 +137,16 @@ uint8_t init_mcp23018(void) {
|
||||
// - unused : input : 1
|
||||
// - input : input : 1
|
||||
// - driving : output : 0
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, BAJJAK_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(IODIRA, BAJJAK_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b00000000, BAJJAK_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b00111111, BAJJAK_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
i2c_stop();
|
||||
uint8_t data[] = {0b00000000, 0b00111111};
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, IODIRA, data, 2, BAJJAK_EZ_I2C_TIMEOUT);
|
||||
|
||||
// set pull-up
|
||||
// - unused : on : 1
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, BAJJAK_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPPUA, BAJJAK_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b00000000, BAJJAK_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b01111111, BAJJAK_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
|
||||
out:
|
||||
i2c_stop();
|
||||
if (!mcp23018_status) {
|
||||
// set pull-up
|
||||
// - unused : on : 1
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, IODIRA, data, 2, BAJJAK_EZ_I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
#ifdef LEFT_LEDS
|
||||
if (!mcp23018_status) mcp23018_status = bajjak_left_leds_update();
|
||||
@ -176,21 +169,11 @@ uint8_t bajjak_left_leds_update(void) {
|
||||
// - unused : hi-Z : 1
|
||||
// - input : hi-Z : 1
|
||||
// - driving : hi-Z : 1
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, BAJJAK_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(OLATA, BAJJAK_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b11111111
|
||||
& ~(bajjak_left_led_1<<LEFT_LED_1_SHIFT),
|
||||
BAJJAK_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b11111111
|
||||
& ~(bajjak_left_led_2<<LEFT_LED_2_SHIFT),
|
||||
BAJJAK_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
uint8_t data[2];
|
||||
data[0] = 0b11111111 & ~(bajjak_left_led_1<<LEFT_LED_1_SHIFT);
|
||||
data[1] = 0b11111111 & ~(bajjak_left_led_2<<LEFT_LED_2_SHIFT);
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, OLATA, data, 2, BAJJAK_EZ_I2C_TIMEOUT);
|
||||
|
||||
out:
|
||||
i2c_stop();
|
||||
return mcp23018_status;
|
||||
}
|
||||
#endif
|
||||
|
@ -27,9 +27,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#include "i2c_master.h"
|
||||
|
||||
// I2C aliases and register addresses (see "mcp23018.md")
|
||||
#define I2C_ADDR 0b0100000
|
||||
#define I2C_ADDR_WRITE ( (I2C_ADDR<<1) | I2C_WRITE )
|
||||
#define I2C_ADDR_READ ( (I2C_ADDR<<1) | I2C_READ )
|
||||
#define I2C_ADDR (0b0100000<<1)
|
||||
#define IODIRA 0x00 // i/o direction register
|
||||
#define IODIRB 0x01
|
||||
#define GPPUA 0x0C // GPIO pull-up resistor register
|
||||
|
@ -145,12 +145,7 @@ static matrix_row_t read_cols(uint8_t row) {
|
||||
uint8_t data = 0;
|
||||
// reading GPIOB (column port) since in mcp23018's sequential mode
|
||||
// it is addressed directly after writing to GPIOA in select_row()
|
||||
mcp23018_status = i2c_start(I2C_ADDR_READ, BAJJAK_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_read_nack(BAJJAK_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
|
||||
data = ~((uint8_t)mcp23018_status);
|
||||
mcp23018_status = I2C_STATUS_SUCCESS;
|
||||
out:
|
||||
i2c_stop();
|
||||
mcp23018_status = i2c_receive(I2C_ADDR, &data, 1, BAJJAK_EZ_I2C_TIMEOUT);
|
||||
return data;
|
||||
}
|
||||
} else {
|
||||
@ -195,11 +190,10 @@ static void select_row(uint8_t row) {
|
||||
if (!mcp23018_status) {
|
||||
// set active row low : 0
|
||||
// set other rows hi-Z : 1
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, BAJJAK_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPIOA, BAJJAK_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0xFF & ~(1 << row), BAJJAK_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
out:
|
||||
i2c_stop();
|
||||
uint8_t data;
|
||||
data = 0xFF & ~(1 << row);
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, GPIOA, &data, 1, BAJJAK_EZ_I2C_TIMEOUT);
|
||||
|
||||
}
|
||||
} else {
|
||||
// select on teensy
|
||||
|
@ -387,37 +387,13 @@ static void unselect_cols(void)
|
||||
|
||||
// Complete rows from other modules over i2c
|
||||
i2c_status_t i2c_transaction(uint8_t address, uint32_t mask, uint8_t col_offset) {
|
||||
i2c_status_t status = i2c_start(address, 5);
|
||||
if (status < 0) {
|
||||
goto error;
|
||||
uint8_t data[MATRIX_ROWS + 1];
|
||||
i2c_status_t status = i2c_readReg(address, 0x01, data, (MATRIX_ROWS + 1), 5);
|
||||
|
||||
for (uint8_t i = 0; i < (MATRIX_ROWS) && status >= 0; i++) { //assemble slave matrix in main matrix
|
||||
matrix[i] &= mask; //mask bits to keep
|
||||
matrix[i] |= ((uint32_t)data[i+1] << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end
|
||||
}
|
||||
|
||||
status = i2c_write(0x01, 50);
|
||||
if (status < 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
status = i2c_start(address | I2C_READ, 50);
|
||||
|
||||
status = i2c_read_ack(50);
|
||||
if (status != 0x55) { //synchronization byte
|
||||
goto error;
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; i < MATRIX_ROWS-1 && status >= 0; i++) { //assemble slave matrix in main matrix
|
||||
matrix[i] &= mask; //mask bits to keep
|
||||
status = i2c_read_ack(50);
|
||||
matrix[i] |= ((uint32_t)status << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end
|
||||
}
|
||||
//last read request must be followed by a NACK
|
||||
if (status >= 0) {
|
||||
matrix[MATRIX_ROWS - 1] &= mask; //mask bits to keep
|
||||
status = i2c_read_nack(50);
|
||||
matrix[MATRIX_ROWS - 1] |= ((uint32_t)status << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end
|
||||
}
|
||||
|
||||
error:
|
||||
i2c_stop();
|
||||
|
||||
return (status < 0) ? status : I2C_STATUS_SUCCESS;
|
||||
return status;
|
||||
}
|
||||
|
@ -155,31 +155,16 @@ uint8_t init_mcp23018(void) {
|
||||
// - unused : input : 1
|
||||
// - input : input : 1
|
||||
// - driving : output : 0
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(IODIRA, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b00000000, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b00111111, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
i2c_stop();
|
||||
uint8_t data[] = {0b00000000, 0b00111111};
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, IODIRA, data, 2, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
|
||||
// set pull-up
|
||||
// - unused : on : 1
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPPUA, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b00000000, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b00111111, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
|
||||
out:
|
||||
i2c_stop();
|
||||
if (!mcp23018_status) {
|
||||
// set pull-up
|
||||
// - unused : on : 1
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, GPPUA, data, 2, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
#ifdef LEFT_LEDS
|
||||
if (!mcp23018_status) mcp23018_status = ergodox_left_leds_update();
|
||||
@ -203,17 +188,11 @@ uint8_t ergodox_left_leds_update(void) {
|
||||
// - unused : hi-Z : 1
|
||||
// - input : hi-Z : 1
|
||||
// - driving : hi-Z : 1
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(OLATA, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b11111111 & ~(ergodox_left_led_3 << LEFT_LED_3_SHIFT), ERGODOX_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b11111111 & ~(ergodox_left_led_2 << LEFT_LED_2_SHIFT) & ~(ergodox_left_led_1 << LEFT_LED_1_SHIFT), ERGODOX_EZ_I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
uint8_t data[2];
|
||||
data[0] = 0b11111111 & ~(ergodox_left_led_3 << LEFT_LED_3_SHIFT);
|
||||
data[1] = 0b11111111 & ~(ergodox_left_led_2 << LEFT_LED_2_SHIFT) & ~(ergodox_left_led_1 << LEFT_LED_1_SHIFT);
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, OLATA, data, 2, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
|
||||
out:
|
||||
i2c_stop();
|
||||
return mcp23018_status;
|
||||
}
|
||||
#endif
|
||||
|
@ -26,9 +26,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#include "i2c_master.h"
|
||||
|
||||
// I2C aliases and register addresses (see "mcp23018.md")
|
||||
#define I2C_ADDR 0b0100000
|
||||
#define I2C_ADDR_WRITE ( (I2C_ADDR<<1) | I2C_WRITE )
|
||||
#define I2C_ADDR_READ ( (I2C_ADDR<<1) | I2C_READ )
|
||||
#define I2C_ADDR (0b0100000<<1)
|
||||
#define IODIRA 0x00 // i/o direction register
|
||||
#define IODIRB 0x01
|
||||
#define GPPUA 0x0C // GPIO pull-up resistor register
|
||||
|
@ -24,30 +24,41 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
# include "ws2812.h"
|
||||
|
||||
void setleds_custom(rgb_led_t *led, uint16_t led_num) {
|
||||
i2c_init();
|
||||
i2c_start(0x84, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
uint16_t length = 0;
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
# ifdef RGBW
|
||||
int bytes_per_led = 4;
|
||||
# else
|
||||
int bytes_per_led = 3;
|
||||
# endif
|
||||
# if defined(ERGODOX_LED_30)
|
||||
// prevent right-half code from trying to bitbang all 30
|
||||
// so with 30 LEDs, we count from 29 to 15 here, and the
|
||||
// other half does 0 to 14.
|
||||
uint8_t half_led_num = RGBLIGHT_LED_COUNT / 2;
|
||||
length = half_led_num * bytes_per_led;
|
||||
uint8_t data[length];
|
||||
for (i = half_led_num + half_led_num - 1; i >= half_led_num; --i)
|
||||
# elif defined(ERGODOX_LED_15_MIRROR)
|
||||
length = led_num * bytes_per_led;
|
||||
uint8_t data[length];
|
||||
for (i = 0; i < led_num; ++i)
|
||||
# else // ERGDOX_LED_15 non-mirrored
|
||||
length = led_num * bytes_per_led;
|
||||
uint8_t data[length];
|
||||
for (i = led_num - 1; i >= 0; --i)
|
||||
# endif
|
||||
{
|
||||
uint8_t *data = (uint8_t *)(led + i);
|
||||
i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
uint8_t *data_byte = (uint8_t *)(led + i);
|
||||
data[j++] = data_byte[0];
|
||||
data[j++] = data_byte[1];
|
||||
data[j++] = data_byte[2];
|
||||
#ifdef RGBW
|
||||
i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
data[j++] = data_byte[3];
|
||||
#endif
|
||||
}
|
||||
i2c_stop();
|
||||
i2c_transmit(0x84, data, sizeof(data), ERGODOX_EZ_I2C_TIMEOUT);
|
||||
|
||||
ws2812_setleds(led, led_num);
|
||||
}
|
||||
|
@ -146,13 +146,8 @@ static matrix_row_t read_cols(uint8_t row) {
|
||||
uint8_t data = 0;
|
||||
// reading GPIOB (column port) since in mcp23018's sequential mode
|
||||
// it is addressed directly after writing to GPIOA in select_row()
|
||||
mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
|
||||
data = ~((uint8_t)mcp23018_status);
|
||||
mcp23018_status = I2C_STATUS_SUCCESS;
|
||||
out:
|
||||
i2c_stop();
|
||||
return data;
|
||||
mcp23018_status = i2c_receive(I2C_ADDR, &data, 1, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
return ~data;
|
||||
}
|
||||
} else {
|
||||
/* read from teensy
|
||||
@ -196,11 +191,9 @@ static void select_row(uint8_t row) {
|
||||
if (!mcp23018_status) {
|
||||
// set active row low : 0
|
||||
// set other rows hi-Z : 1
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0xFF & ~(1 << row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
out:
|
||||
i2c_stop();
|
||||
uint8_t data;
|
||||
data = 0xFF & ~(1 << row);
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, GPIOA, &data, 1, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
}
|
||||
} else {
|
||||
// select on teensy
|
||||
|
@ -42,9 +42,7 @@ extern i2c_status_t mcp23017_status;
|
||||
// All address pins of the mcp23017 are connected to the ground on the ferris
|
||||
// | 0 | 1 | 0 | 0 | A2 | A1 | A0 |
|
||||
// | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
|
||||
#define I2C_ADDR 0b0100000
|
||||
#define I2C_ADDR_WRITE ((I2C_ADDR << 1) | I2C_WRITE)
|
||||
#define I2C_ADDR_READ ((I2C_ADDR << 1) | I2C_READ)
|
||||
#define I2C_ADDR (0b0100000 << 1)
|
||||
|
||||
// Register addresses
|
||||
// See https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library/blob/master/Adafruit_MCP23017.h
|
||||
@ -77,9 +75,9 @@ uint8_t init_mcp23017(void) {
|
||||
// - driving : output : 0
|
||||
// This means: we will read all the bits on GPIOA
|
||||
// This means: we will write to the pins 0-4 on GPIOB (in select_rows)
|
||||
uint8_t buf[] = {IODIRA, 0b11111111, 0b11110000};
|
||||
uint8_t buf[] = {0b11111111, 0b11110000};
|
||||
print("before transmit\n");
|
||||
mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, buf, sizeof(buf), MCP23017_I2C_TIMEOUT);
|
||||
mcp23017_status = i2c_writeReg(I2C_ADDR, IODIRA, buf, sizeof(buf), MCP23017_I2C_TIMEOUT)
|
||||
uprintf("after transmit %i\n", mcp23017_status);
|
||||
if (!mcp23017_status) {
|
||||
// set pull-up
|
||||
@ -88,8 +86,7 @@ uint8_t init_mcp23017(void) {
|
||||
// - driving : off : 0
|
||||
// This means: we will read all the bits on GPIOA
|
||||
// This means: we will write to the pins 0-4 on GPIOB (in select_rows)
|
||||
uint8_t pullup_buf[] = {GPPUA, 0b11111111, 0b11110000};
|
||||
mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, pullup_buf, sizeof(pullup_buf), MCP23017_I2C_TIMEOUT);
|
||||
mcp23017_status = i2c_writeReg(I2C_ADDR, GPPUA, buf, sizeof(buf), MCP23017_I2C_TIMEOUT)
|
||||
uprintf("after transmit2 %i\n", mcp23017_status);
|
||||
}
|
||||
return mcp23017_status;
|
||||
@ -189,18 +186,13 @@ static matrix_row_t read_cols(uint8_t row) {
|
||||
if (mcp23017_status) { // if there was an error
|
||||
return 0;
|
||||
} else {
|
||||
uint8_t buf[] = {MCP23017_GPIOA};
|
||||
mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, buf, sizeof(buf), MCP23017_I2C_TIMEOUT);
|
||||
// We read all the pins on GPIOA.
|
||||
// The initial state was all ones and any depressed key at a given column for the currently selected row will have its bit flipped to zero.
|
||||
// The return value is a row as represented in the generic matrix code were the rightmost bits represent the lower columns and zeroes represent non-depressed keys while ones represent depressed keys.
|
||||
// Since the pins connected to eact columns are sequential, and counting from zero up (col 5 -> GPIOA0, col 6 -> GPIOA1 and so on), the only transformation needed is a bitwise not to swap all zeroes and ones.
|
||||
uint8_t data[] = {0};
|
||||
if (!mcp23017_status) {
|
||||
mcp23017_status = i2c_receive(I2C_ADDR_READ, data, sizeof(data), MCP23017_I2C_TIMEOUT);
|
||||
data[0] = ~(data[0]);
|
||||
}
|
||||
return data[0];
|
||||
mcp23017_status = i2c_readReg(I2C_ADDR, MCP23017_GPIOA, data, sizeof(data), MCP23017_I2C_TIMEOUT);
|
||||
return ~data[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -244,8 +236,8 @@ static void select_row(uint8_t row) {
|
||||
} else {
|
||||
// Select the desired row by writing a byte for the entire GPIOB bus where only the bit representing the row we want to select is a zero (write instruction) and every other bit is a one.
|
||||
// Note that the row - MATRIX_ROWS_PER_SIDE reflects the fact that being on the right hand, the columns are numbered from MATRIX_ROWS_PER_SIDE to MATRIX_ROWS, but the pins we want to write to are indexed from zero up on the GPIOB bus.
|
||||
uint8_t buf[] = {MCP23017_GPIOB, 0xFF & ~(1 << (row - MATRIX_ROWS_PER_SIDE))};
|
||||
mcp23017_status = i2c_transmit(I2C_ADDR_WRITE, buf, sizeof(buf), I2C_TIMEOUT);
|
||||
uint8_t buf[] = {0xFF & ~(1 << (row - MATRIX_ROWS_PER_SIDE))};
|
||||
mcp23017_status = i2c_writeReg(I2C_ADDR, MCP23017_GPIOB, buf, sizeof(buf), MCP23017_I2C_TIMEOUT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -49,23 +49,17 @@ uint8_t init_mcp23018(void) {
|
||||
// - unused : input : 1
|
||||
// - input : input : 1
|
||||
// - driving : output : 0
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(IODIRA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b00000000, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b11111111, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
i2c_stop();
|
||||
uint8_t data[] = {0b00000000, 0b00111111};
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, IODIRA, data, sizeof(data), ERGODOX_EZ_I2C_TIMEOUT);
|
||||
|
||||
// set pull-up
|
||||
// - unused : on : 1
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPPUA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b00000000, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b11111111, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
if (!mcp23018_status) {
|
||||
// set pull-up
|
||||
// - unused : on : 1
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, GPPUA, data, sizeof(data), ERGODOX_EZ_I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
out:
|
||||
i2c_stop();
|
||||
// SREG=sreg_prev;
|
||||
//uprintf("Init %x\n", mcp23018_status);
|
||||
return mcp23018_status;
|
||||
|
@ -11,10 +11,7 @@ extern i2c_status_t mcp23018_status;
|
||||
#define ERGODOX_EZ_I2C_TIMEOUT 1000
|
||||
|
||||
// I2C aliases and register addresses (see "mcp23018.md")
|
||||
//#define I2C_ADDR 0b0100000
|
||||
#define I2C_ADDR 0x20
|
||||
#define I2C_ADDR_WRITE ( (I2C_ADDR<<1) | I2C_WRITE )
|
||||
#define I2C_ADDR_READ ( (I2C_ADDR<<1) | I2C_READ )
|
||||
#define I2C_ADDR (0x20<<1)
|
||||
#define IODIRA 0x00 // i/o direction register
|
||||
#define IODIRB 0x01
|
||||
#define GPPUA 0x0C // GPIO pull-up resistor register
|
||||
|
@ -234,15 +234,8 @@ static matrix_row_t read_cols(uint8_t row)
|
||||
return 0;
|
||||
} else {
|
||||
uint8_t data = 0;
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
|
||||
data = (~((uint8_t)mcp23018_status) >> 2) & 0x01 ;
|
||||
mcp23018_status = I2C_STATUS_SUCCESS;
|
||||
out:
|
||||
i2c_stop();
|
||||
|
||||
mcp23018_status = i2c_readReg(I2C_ADDR, GPIOB, &data, 1, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
data = (~((uint8_t)data) >> 2) & 0x01 ;
|
||||
#ifdef DEBUG_MATRIX
|
||||
if (data != 0x00) xprintf("I2C: %d\n", data);
|
||||
#endif
|
||||
@ -274,11 +267,8 @@ static void select_row(uint8_t row)
|
||||
if (mcp23018_status) { // do nothing on error
|
||||
// Read using bitmask
|
||||
} else { // set active row low : 0 // set other rows hi-Z : 1
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
out:
|
||||
i2c_stop();
|
||||
uint8_t data = ~(1<<row);
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, GPIOA, &data, 1, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
}
|
||||
} else {
|
||||
// Output low(DDR:1, PORT:0) to select
|
||||
|
@ -45,23 +45,17 @@ uint8_t init_mcp23018(void) {
|
||||
// - unused : input : 1
|
||||
// - input : input : 1
|
||||
// - driving : output : 0
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(IODIRA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b10000000, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b11111111, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
i2c_stop();
|
||||
uint8_t data[] = {0b10000000, 0b11111111};
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, IODIRA, data, 2, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
|
||||
// set pull-up
|
||||
// - unused : on : 1
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPPUA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b10000000, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b11111111, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
if (!mcp23018_status) {
|
||||
// set pull-up
|
||||
// - unused : on : 1
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, GPPUA, data, 2, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
out:
|
||||
i2c_stop();
|
||||
// SREG=sreg_prev;
|
||||
//uprintf("Init %x\n", mcp23018_status);
|
||||
return mcp23018_status;
|
||||
|
@ -11,10 +11,7 @@ extern i2c_status_t mcp23018_status;
|
||||
#define ERGODOX_EZ_I2C_TIMEOUT 1000
|
||||
|
||||
// I2C aliases and register addresses (see "mcp23018.md")
|
||||
//#define I2C_ADDR 0b0100000
|
||||
#define I2C_ADDR 0x20
|
||||
#define I2C_ADDR_WRITE ( (I2C_ADDR<<1) | I2C_WRITE )
|
||||
#define I2C_ADDR_READ ( (I2C_ADDR<<1) | I2C_READ )
|
||||
#define I2C_ADDR (0x20<<1)
|
||||
#define IODIRA 0x00 // i/o direction register
|
||||
#define IODIRB 0x01
|
||||
#define GPPUA 0x0C // GPIO pull-up resistor register
|
||||
|
@ -254,14 +254,7 @@ static matrix_row_t read_cols(uint8_t row)
|
||||
return 0;
|
||||
} else {
|
||||
uint8_t data = 0;
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
|
||||
data = ~((uint8_t)mcp23018_status);
|
||||
mcp23018_status = I2C_STATUS_SUCCESS;
|
||||
out:
|
||||
i2c_stop();
|
||||
mcp23018_status = i2c_readReg(I2C_ADDR, GPIOB, &data, 1, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
|
||||
#ifdef DEBUG_MATRIX
|
||||
if (data != 0x00) xprintf("I2C: %d\n", data);
|
||||
@ -304,11 +297,9 @@ static void select_row(uint8_t row)
|
||||
// select on mcp23018
|
||||
if (mcp23018_status) { // do nothing on error
|
||||
} else { // set active row low : 0 // set other rows hi-Z : 1
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0xFF & ~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
out:
|
||||
i2c_stop();
|
||||
uint8_t data = 0xFF & ~(1<<row);
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, GPIOA, &data, 1, ERGODOX_EZ_I2C_TIMEOUT);
|
||||
|
||||
}
|
||||
} else {
|
||||
// Output low(DDR:1, PORT:0) to select
|
||||
|
@ -43,23 +43,17 @@ uint8_t init_mcp23018(void) {
|
||||
// - unused : input : 1
|
||||
// - input : input : 1
|
||||
// - driving : output : 0
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(IODIRA, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b10000000, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b11111111, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
i2c_stop();
|
||||
uint8_t data[] = {0b10000000, 0b11111111};
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, IODIRA, data, sizeof(data), I2C_TIMEOUT);
|
||||
|
||||
// set pull-up
|
||||
// - unused : on : 1
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPPUA, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b10000000, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b11111111, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
if (!mcp23018_status) {
|
||||
// set pull-up
|
||||
// - unused : on : 1
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, GPPUA, data, sizeof(data), I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
out:
|
||||
i2c_stop();
|
||||
// SREG=sreg_prev;
|
||||
//uprintf("Init %x\n", mcp23018_status);
|
||||
return mcp23018_status;
|
||||
|
@ -11,10 +11,7 @@ extern i2c_status_t mcp23018_status;
|
||||
#define I2C_TIMEOUT 1000
|
||||
|
||||
// I2C aliases and register addresses (see "mcp23018.md")
|
||||
//#define I2C_ADDR 0b0100000
|
||||
#define I2C_ADDR 0x20
|
||||
#define I2C_ADDR_WRITE ( (I2C_ADDR<<1) | I2C_WRITE )
|
||||
#define I2C_ADDR_READ ( (I2C_ADDR<<1) | I2C_READ )
|
||||
#define I2C_ADDR (0x20<<1)
|
||||
#define IODIRA 0x00 // i/o direction register
|
||||
#define IODIRB 0x01
|
||||
#define GPPUA 0x0C // GPIO pull-up resistor register
|
||||
|
@ -297,19 +297,12 @@ static matrix_row_t read_cols(uint8_t row) {
|
||||
return 0;
|
||||
} else {
|
||||
uint8_t data = 0;
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPIOB, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_read_nack(I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
|
||||
data = ~((uint8_t)mcp23018_status);
|
||||
mcp23018_status = I2C_STATUS_SUCCESS;
|
||||
out:
|
||||
i2c_stop();
|
||||
mcp23018_status = i2c_readReg(I2C_ADDR, GPIOB, &data, 1, I2C_TIMEOUT);
|
||||
|
||||
#ifdef DEBUG_MATRIX
|
||||
if (data != 0x00) xprintf("I2C: %d\n", data);
|
||||
if (~data != 0x00) xprintf("I2C: %d\n", ~data);
|
||||
#endif
|
||||
return data;
|
||||
return ~data;
|
||||
}
|
||||
} else {
|
||||
/* read from teensy
|
||||
@ -350,11 +343,8 @@ static void select_row(uint8_t row)
|
||||
// select on mcp23018
|
||||
if (mcp23018_status) { // do nothing on error
|
||||
} else { // set active row low : 0 // set other rows hi-Z : 1
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPIOA, I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0xFF & ~(1<<row), I2C_TIMEOUT); if (mcp23018_status) goto out;
|
||||
out:
|
||||
i2c_stop();
|
||||
uint8_t data = 0xFF & ~(1<<row);
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT);
|
||||
}
|
||||
} else {
|
||||
// Output low(DDR:1, PORT:0) to select
|
||||
|
@ -39,30 +39,16 @@ uint8_t init_mcp23018(void) {
|
||||
// - unused : input : 1
|
||||
// - input : input : 1
|
||||
// - driving : output : 0
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(IODIRA, I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b11000001, I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b11111111, I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
i2c_stop();
|
||||
uint8_t data[] = {0b11000001, 0b11111111};
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, IODIRA, data, sizeof(data), I2C_TIMEOUT);
|
||||
|
||||
// set pull-up
|
||||
// - unused : on : 1
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPPUA, I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b11000001, I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0b11111111, I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
if (!mcp23018_status) {
|
||||
// set pull-up
|
||||
// - unused : on : 1
|
||||
// - input : on : 1
|
||||
// - driving : off : 0
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, GPPUA, data, sizeof(data), I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
out:
|
||||
i2c_stop();
|
||||
return mcp23018_status;
|
||||
}
|
||||
|
@ -25,9 +25,7 @@ extern i2c_status_t mcp23018_status;
|
||||
#define XXX KC_NO
|
||||
|
||||
// I2C aliases and register addresses (see "mcp23018.md")
|
||||
#define I2C_ADDR 0x20 // 0b0100000
|
||||
#define I2C_ADDR_WRITE ((I2C_ADDR << 1) | I2C_WRITE)
|
||||
#define I2C_ADDR_READ ((I2C_ADDR << 1) | I2C_READ)
|
||||
#define I2C_ADDR (0x20 << 1) // 0b0100000
|
||||
#define IODIRA 0x00 // i/o direction register
|
||||
#define IODIRB 0x01
|
||||
#define GPPUA 0x0C // GPIO pull-up resistor register
|
||||
|
@ -177,20 +177,12 @@ static matrix_row_t read_cols(uint8_t row) {
|
||||
if (mcp23018_status) { // if there was an error
|
||||
return 0;
|
||||
} else {
|
||||
uint8_t data = 0;
|
||||
mcp23018_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_read_nack(I2C_TIMEOUT);
|
||||
if (mcp23018_status < 0) goto out;
|
||||
data = ~((uint8_t)mcp23018_status);
|
||||
mcp23018_status = I2C_STATUS_SUCCESS;
|
||||
out:
|
||||
i2c_stop();
|
||||
|
||||
uint8_t data = 0;
|
||||
mcp23018_status = i2c_receive(I2C_ADDR, &data, 1, I2C_TIMEOUT);
|
||||
#ifdef DEBUG_MATRIX
|
||||
if (data != 0x00) xprintf("I2C: %d\n", data);
|
||||
if (~data != 0x00) xprintf("I2C: %d\n", ~data);
|
||||
#endif
|
||||
return data;
|
||||
return ~data;
|
||||
}
|
||||
} else {
|
||||
return ~((((PINF & COL4) >> 1) | ((PINF & (COL1 | COL2 | COL3)) >> 3)) & 0xF);
|
||||
@ -213,14 +205,10 @@ static void select_row(uint8_t row) {
|
||||
// select on mcp23018
|
||||
if (mcp23018_status) { // do nothing on error
|
||||
} else { // set active row low : 0 // set other rows hi-Z : 1
|
||||
mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(GPIOA, I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
mcp23018_status = i2c_write(0xFF & ~(1 << (row + 1)), I2C_TIMEOUT);
|
||||
if (mcp23018_status) goto out;
|
||||
out:
|
||||
i2c_stop();
|
||||
uint8_t data;
|
||||
data = 0xFF & ~(1 << (row + 1));
|
||||
mcp23018_status = i2c_writeReg(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT);
|
||||
|
||||
}
|
||||
} else {
|
||||
setPinOutput(row_pins[row - MATRIX_ROWS_PER_SIDE]);
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user