From 80ddf397fd692699b8384608dba556d959f84d11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Wed, 12 Oct 2016 03:03:59 +0200 Subject: [PATCH] Make the left keyboard half send the state of the keys to the right half via I2C and make the right half send the relevant scancodes to the host via USB. --- left/build/kds/.project | 10 ++++ left/src/i2c.h | 19 +++++++ left/src/init_peripherials.c | 36 ++++++++++++ left/src/init_peripherials.h | 8 +++ left/src/main.c | 89 +++++++++++++++--------------- right/src/usb_interface_keyboard.c | 22 ++++++++ 6 files changed, 139 insertions(+), 45 deletions(-) create mode 100644 left/src/i2c.h create mode 100644 left/src/init_peripherials.c create mode 100644 left/src/init_peripherials.h diff --git a/left/build/kds/.project b/left/build/kds/.project index 7129608..9b30aed 100644 --- a/left/build/kds/.project +++ b/left/build/kds/.project @@ -71,6 +71,16 @@ 1 PARENT-3-PROJECT_LOC/lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_gpio.h + + drivers/fsl_i2c.c + 1 + PARENT-3-PROJECT_LOC/lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_i2c.c + + + drivers/fsl_i2c.h + 1 + PARENT-3-PROJECT_LOC/lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_i2c.h + drivers/fsl_port.h 1 diff --git a/left/src/i2c.h b/left/src/i2c.h new file mode 100644 index 0000000..4d34f7c --- /dev/null +++ b/left/src/i2c.h @@ -0,0 +1,19 @@ +#ifndef __I2C_H__ +#define __I2C_H__ + +// Macros: + + #define I2C_BUS_BASEADDR I2C0 + #define I2C_BUS_CLK_SRC I2C0_CLK_SRC + #define I2C_BUS_BAUD_RATE 100000 // 100 kHz works even with a 20 meter long bridge cable. + #define I2C_BUS_MUX kPORT_MuxAlt2 + + #define I2C_BUS_SDA_PORT PORTB + #define I2C_BUS_SDA_CLOCK kCLOCK_PortB + #define I2C_BUS_SDA_PIN 4 + + #define I2C_BUS_SCL_PORT PORTB + #define I2C_BUS_SCL_CLOCK kCLOCK_PortB + #define I2C_BUS_SCL_PIN 3 + +#endif diff --git a/left/src/init_peripherials.c b/left/src/init_peripherials.c new file mode 100644 index 0000000..bcfaa6e --- /dev/null +++ b/left/src/init_peripherials.c @@ -0,0 +1,36 @@ +#include "fsl_common.h" +#include "fsl_port.h" +#include "test_led.h" +#include "i2c_addresses.h" +#include "fsl_i2c.h" +#include "fsl_clock.h" +#include "i2c.h" + +void InitI2c() { + port_pin_config_t pinConfig = { + .pullSelect = kPORT_PullUp, + }; + + i2c_master_config_t masterConfig; + I2C_MasterGetDefaultConfig(&masterConfig); + uint32_t sourceClock; + + // Initialize main bus + + CLOCK_EnableClock(I2C_BUS_SDA_CLOCK); + CLOCK_EnableClock(I2C_BUS_SCL_CLOCK); + + pinConfig.mux = I2C_BUS_MUX; + PORT_SetPinConfig(I2C_BUS_SDA_PORT, I2C_BUS_SDA_PIN, &pinConfig); + PORT_SetPinConfig(I2C_BUS_SCL_PORT, I2C_BUS_SCL_PIN, &pinConfig); + + masterConfig.baudRate_Bps = I2C_BUS_BAUD_RATE; + sourceClock = CLOCK_GetFreq(I2C_BUS_CLK_SRC); + I2C_MasterInit(I2C_BUS_BASEADDR, &masterConfig, sourceClock); +} + +void InitPeripherials(void) +{ + InitTestLed(); + InitI2c(); +} diff --git a/left/src/init_peripherials.h b/left/src/init_peripherials.h new file mode 100644 index 0000000..bb75a64 --- /dev/null +++ b/left/src/init_peripherials.h @@ -0,0 +1,8 @@ +#ifndef __INIT_H__ +#define __INIT_H__ + +// Functions: + + void InitPeripherials(); + +#endif diff --git a/left/src/main.c b/left/src/main.c index 6d31a7a..a6f489d 100644 --- a/left/src/main.c +++ b/left/src/main.c @@ -1,8 +1,12 @@ #include "fsl_gpio.h" #include "init_clock.h" #include "fsl_port.h" +#include "fsl_i2c.h" #include "key_matrix.h" #include "test_led.h" +#include "i2c_addresses.h" +#include "i2c.h" +#include "init_peripherials.h" #define KEYBOARD_MATRIX_COLS_NUM 7 #define KEYBOARD_MATRIX_ROWS_NUM 5 @@ -28,13 +32,51 @@ key_matrix_t keyMatrix = { } }; +i2c_slave_config_t slaveConfig; +i2c_slave_handle_t slaveHandle; +volatile bool g_SlaveCompletionFlag = false; + +static void i2c_slave_callback(I2C_Type *base, i2c_slave_transfer_t *xfer, void *userData) +{ + switch (xfer->event) + { + case kI2C_SlaveTransmitEvent: + xfer->data = keyMatrix.keyStates; + xfer->dataSize = KEYBOARD_MATRIX_COLS_NUM*KEYBOARD_MATRIX_ROWS_NUM; + break; + case kI2C_SlaveReceiveEvent: + break; + case kI2C_SlaveCompletionEvent: + g_SlaveCompletionFlag = true; + break; + case kI2C_SlaveTransmitAckEvent: + break; + default: + g_SlaveCompletionFlag = true; + break; + } +} + int main(void) { - InitTestLed(); InitClock(); + InitPeripherials(); + I2C_SlaveGetDefaultConfig(&slaveConfig); + slaveConfig.slaveAddress = I2C_ADDRESS_LEFT_KEYBOARD_HALF; + slaveConfig.addressingMode = kI2C_Address7bit/kI2C_RangeMatch; + I2C_SlaveInit(I2C_BUS_BASEADDR, &slaveConfig); + I2C_SlaveTransferCreateHandle(I2C_BUS_BASEADDR, &slaveHandle, i2c_slave_callback, NULL); + I2C_SlaveTransferNonBlocking(I2C_BUS_BASEADDR, &slaveHandle, kI2C_SlaveCompletionEvent); + +// while (!g_SlaveCompletionFlag) {} +// g_SlaveCompletionFlag = false; + + TEST_LED_OFF(); KeyMatrix_Init(&keyMatrix); - KeyMatrix_Scan(&keyMatrix); + while (1) { + KeyMatrix_Scan(&keyMatrix); + } while (1) { @@ -48,48 +90,6 @@ int main(void) } /* -#define I2C_DATA_LENGTH 2 - -#include "board.h" -#include "fsl_clock_manager.h" -#include "fsl_i2c_slave_driver.h" -#include "fsl_i2c_shared_function.h" -#include "i2c_addresses.h" -#include "main.h" - -uint8_t isSw2Pressed; -uint8_t isSw3Pressed; -uint8_t buffer[I2C_DATA_LENGTH]; - -void I2C0_IRQHandler(void) -{ - I2C_DRV_IRQHandler(I2C0_IDX); -} - -static void i2c_slave_callback(uint8_t instance, i2c_slave_event_t i2cEvent, void *param) -{ - i2c_slave_state_t *slaveState = I2C_DRV_SlaveGetHandler(instance); - - switch (i2cEvent) { - case kI2CSlaveTxReq: - slaveState->txSize = I2C_DATA_LENGTH; - slaveState ->txBuff = buffer; - slaveState ->isTxBusy = true; - buffer[0] = isSw2Pressed; - buffer[1] = isSw3Pressed; - break; - - case kI2CSlaveTxEmpty: - slaveState->isTxBusy = false; - break; - - default: - break; - } -} - -int main(void) -{ // Initialize GPIO. gpio_input_pin_user_config_t inputPin[] = @@ -157,5 +157,4 @@ int main(void) GPIO_DRV_WritePinOutput(kGpioLED1, isSw2Pressed); GPIO_DRV_WritePinOutput(kGpioLED3, isSw3Pressed); } -} */ diff --git a/right/src/usb_interface_keyboard.c b/right/src/usb_interface_keyboard.c index 13db19c..799379d 100644 --- a/right/src/usb_interface_keyboard.c +++ b/right/src/usb_interface_keyboard.c @@ -3,6 +3,9 @@ #include "usb_composite_device.h" #include "test_led.h" #include "key_matrix.h" +#include "fsl_i2c.h" +#include "i2c.h" +#include "i2c_addresses.h" static usb_device_endpoint_struct_t UsbKeyboardEndpoints[USB_KEYBOARD_ENDPOINT_COUNT] = {{ USB_KEYBOARD_ENDPOINT_INDEX | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT), @@ -62,6 +65,9 @@ key_matrix_t keyMatrix = { } }; +#define KEY_STATE_COUNT (5*7) +uint8_t leftKeyStates[KEY_STATE_COUNT] = {0}; + static usb_status_t UsbKeyboardAction(void) { UsbKeyboardReport.modifiers = 0; @@ -75,6 +81,16 @@ static usb_status_t UsbKeyboardAction(void) UsbKeyboardReport.scancodes[scancodeIdx] = 0; } + i2c_master_transfer_t masterXfer; + masterXfer.slaveAddress = I2C_ADDRESS_LEFT_KEYBOARD_HALF; + masterXfer.direction = kI2C_Read; + masterXfer.subaddress = 0; + masterXfer.subaddressSize = 0; + masterXfer.data = leftKeyStates; + masterXfer.dataSize = KEY_STATE_COUNT; + masterXfer.flags = kI2C_TransferDefaultFlag; + I2C_MasterTransferBlocking(I2C_MAIN_BUS_BASEADDR, &masterXfer); + scancodeIdx = 0; for (uint8_t keyId=0; keyId