Remove keyboard scanning from USB interrupt. Fix initial blink of LEDs when initializing. Coding style fixes.
This commit is contained in:
@@ -38,18 +38,26 @@
|
||||
|
||||
static inline __attribute__((always_inline)) uint8_t getKeycode(KEYBOARD_LAYOUT(layout), uint8_t keyId, uint8_t modifierState)
|
||||
{
|
||||
if (keyId<LAYOUT_KEY_COUNT)
|
||||
return layout[keyId][modifierState];
|
||||
else
|
||||
if (keyId<LAYOUT_KEY_COUNT) {
|
||||
uint8_t k = layout[keyId][modifierState];
|
||||
if (k==0) {
|
||||
k = layout[keyId][0];
|
||||
}
|
||||
|
||||
return k;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) uint8_t getModifierState(const uint8_t *leftKeyStates, const uint8_t *rightKeyStates){
|
||||
uint8_t mod=0;
|
||||
if (leftKeyStates[KEYID_LEFT_MOD])
|
||||
uint8_t mod = 0;
|
||||
if (leftKeyStates[KEYID_LEFT_MOD]) {
|
||||
mod |= MODIFIER_MOD_PRESSED;
|
||||
if (leftKeyStates[KEYID_LEFT_FN] | rightKeyStates[KEYID_RIGHT_FN])
|
||||
}
|
||||
if (leftKeyStates[KEYID_LEFT_FN] | rightKeyStates[KEYID_RIGHT_FN]) {
|
||||
mod |= MODIFIER_FN_PRESSED;
|
||||
}
|
||||
|
||||
return mod;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ void LedDriver_WriteRegister(uint8_t i2cAddress, uint8_t reg, uint8_t val)
|
||||
LedDriver_WriteBuffer(i2cAddress, buffer, sizeof(buffer));
|
||||
}
|
||||
|
||||
void LedDriver_EnableAllLeds()
|
||||
void LedDriver_InitAllLeds(char isEnabled)
|
||||
{
|
||||
CLOCK_EnableClock(LED_DRIVER_SDB_CLOCK);
|
||||
PORT_SetPinMux(LED_DRIVER_SDB_PORT, LED_DRIVER_SDB_PIN, kPORT_MuxAsGpio);
|
||||
@@ -35,14 +35,14 @@ void LedDriver_EnableAllLeds()
|
||||
LedDriver_WriteRegister(address, LED_DRIVER_REGISTER_FRAME, LED_DRIVER_FRAME_1);
|
||||
|
||||
uint8_t i;
|
||||
for (i=FRAME_REGISTER_PWM_FIRST; i<=FRAME_REGISTER_PWM_LAST; i++) {
|
||||
LedDriver_WriteRegister(address, i, isEnabled ? 0xFF : 0x00);
|
||||
}
|
||||
for (i=FRAME_REGISTER_LED_CONTROL_FIRST; i<=FRAME_REGISTER_LED_CONTROL_LAST; i++) {
|
||||
LedDriver_WriteRegister(address, i, 0xff);
|
||||
}
|
||||
for (i=FRAME_REGISTER_BLINK_CONTROL_FIRST; i<=FRAME_REGISTER_BLINK_CONTROL_LAST; i++) {
|
||||
LedDriver_WriteRegister(address, i, 0x00);
|
||||
}
|
||||
for (i=FRAME_REGISTER_PWM_FIRST; i<=FRAME_REGISTER_PWM_LAST; i++) {
|
||||
LedDriver_WriteRegister(address, i, 0x00);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +43,6 @@
|
||||
|
||||
extern void LedDriver_WriteBuffer(uint8_t i2cAddress, uint8_t buffer[], uint8_t size);
|
||||
extern void LedDriver_WriteRegister(uint8_t i2cAddress, uint8_t reg, uint8_t val);
|
||||
extern void LedDriver_EnableAllLeds();
|
||||
extern void LedDriver_InitAllLeds(char isEnabled);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -6,10 +6,12 @@
|
||||
void main() {
|
||||
InitPeripherials();
|
||||
InitClock();
|
||||
LedDriver_EnableAllLeds();
|
||||
LedDriver_InitAllLeds(0);
|
||||
usbKeyboadTask();
|
||||
InitUsb();
|
||||
|
||||
while (1){
|
||||
usbKeyboadTask();
|
||||
asm("wfi");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,8 @@ usb_device_class_struct_t UsbKeyboardClass = {
|
||||
USB_DEVICE_CONFIGURATION_COUNT,
|
||||
};
|
||||
|
||||
static usb_keyboard_report_t UsbKeyboardReport;
|
||||
volatile static int activeLayout=0;
|
||||
volatile static usb_keyboard_report_t UsbKeyboardReport[2];
|
||||
|
||||
#define KEYBOARD_MATRIX_COLS_NUM 7
|
||||
#define KEYBOARD_MATRIX_ROWS_NUM 5
|
||||
@@ -66,54 +67,79 @@ key_matrix_t keyMatrix = {
|
||||
}
|
||||
};
|
||||
|
||||
void readLeftKeys(uint8_t *stateVector){
|
||||
uint8_t data[] = {0};
|
||||
uint8_t success=0;
|
||||
|
||||
if (I2cWrite(I2C_MAIN_BUS_BASEADDR, I2C_ADDRESS_LEFT_KEYBOARD_HALF, data, sizeof(data)) == kStatus_Success) {
|
||||
if (I2cRead(I2C_MAIN_BUS_BASEADDR, I2C_ADDRESS_LEFT_KEYBOARD_HALF, stateVector, KEY_STATE_COUNT) == kStatus_Success) {
|
||||
success = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
bzero(stateVector, KEY_STATE_COUNT);
|
||||
}
|
||||
}
|
||||
|
||||
void usbKeyboadTask(){
|
||||
//Producer task for USB packets. When the USB interrupt is called,
|
||||
//the newest packet is sent out immediately, thus not doing long task
|
||||
//in the interrupt handler.
|
||||
int newLayout = 1-activeLayout;
|
||||
|
||||
static uint8_t leftKeyStates[KEY_STATE_COUNT];
|
||||
|
||||
UsbKeyboardReport[newLayout].modifiers = 0;
|
||||
UsbKeyboardReport[newLayout].reserved = 0;
|
||||
|
||||
KeyMatrix_Init(&keyMatrix);
|
||||
KeyMatrix_Scan(&keyMatrix);
|
||||
|
||||
bzero(&UsbKeyboardReport[newLayout].scancodes, USB_KEYBOARD_MAX_KEYS*sizeof(UsbKeyboardReport[newLayout].scancodes[0]));
|
||||
|
||||
readLeftKeys(leftKeyStates);
|
||||
|
||||
uint8_t modifierState = getModifierState(leftKeyStates, keyMatrix.keyStates);
|
||||
|
||||
int scancodeIdx = 0;
|
||||
for (uint8_t keyId=0; keyId<KEYBOARD_MATRIX_COLS_NUM*KEYBOARD_MATRIX_ROWS_NUM; keyId++) {
|
||||
if (scancodeIdx>=USB_KEYBOARD_MAX_KEYS) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (keyMatrix.keyStates[keyId]) {
|
||||
uint8_t code=getKeycode(defaultKeyboardLayout, keyId, modifierState);
|
||||
if (code) {
|
||||
UsbKeyboardReport[newLayout].scancodes[scancodeIdx++] = code;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (uint8_t keyId=0; keyId<KEY_STATE_COUNT; keyId++) {
|
||||
if (scancodeIdx>=USB_KEYBOARD_MAX_KEYS) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (leftKeyStates[keyId]) {
|
||||
uint8_t code=getKeycode(defaultKeyboardLayout, LAYOUT_LEFT_OFFSET+keyId, modifierState);
|
||||
if (code) {
|
||||
UsbKeyboardReport[newLayout].scancodes[scancodeIdx++] = code;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Change to the new layout in atomic operation (int copy). Even if
|
||||
//the copy is not atomic itself, only single bit changes. So it can
|
||||
//never be a problem
|
||||
activeLayout = newLayout;
|
||||
}
|
||||
|
||||
|
||||
static usb_status_t UsbKeyboardAction(void)
|
||||
{
|
||||
uint8_t leftKeyStates[KEY_STATE_COUNT] = {0};
|
||||
|
||||
UsbKeyboardReport.modifiers = 0;
|
||||
UsbKeyboardReport.reserved = 0;
|
||||
|
||||
KeyMatrix_Init(&keyMatrix);
|
||||
KeyMatrix_Scan(&keyMatrix);
|
||||
|
||||
uint8_t scancodeIdx;
|
||||
for (uint8_t scancodeIdx=0; scancodeIdx<USB_KEYBOARD_MAX_KEYS; scancodeIdx++) {
|
||||
UsbKeyboardReport.scancodes[scancodeIdx] = 0;
|
||||
}
|
||||
|
||||
uint8_t data[] = {0};
|
||||
I2cWrite(I2C_MAIN_BUS_BASEADDR, I2C_ADDRESS_LEFT_KEYBOARD_HALF, data, sizeof(data));
|
||||
I2cRead(I2C_MAIN_BUS_BASEADDR, I2C_ADDRESS_LEFT_KEYBOARD_HALF, leftKeyStates, KEY_STATE_COUNT);
|
||||
|
||||
uint8_t modifierState = getModifierState(leftKeyStates, keyMatrix.keyStates);
|
||||
|
||||
scancodeIdx = 0;
|
||||
for (uint8_t keyId=0; keyId<KEYBOARD_MATRIX_COLS_NUM*KEYBOARD_MATRIX_ROWS_NUM; keyId++) {
|
||||
if (scancodeIdx>=USB_KEYBOARD_MAX_KEYS)
|
||||
break;
|
||||
|
||||
if (keyMatrix.keyStates[keyId]) {
|
||||
uint8_t code=getKeycode(defaultKeyboardLayout, keyId, modifierState);
|
||||
if (code)
|
||||
UsbKeyboardReport.scancodes[scancodeIdx++] = code;
|
||||
}
|
||||
}
|
||||
|
||||
for (uint8_t keyId=0; keyId<KEY_STATE_COUNT; keyId++) {
|
||||
if (scancodeIdx>=USB_KEYBOARD_MAX_KEYS)
|
||||
break;
|
||||
|
||||
if (leftKeyStates[keyId]) {
|
||||
uint8_t code=getKeycode(defaultKeyboardLayout, LAYOUT_LEFT_OFFSET+keyId, modifierState);
|
||||
if (code)
|
||||
UsbKeyboardReport.scancodes[scancodeIdx++] = code;
|
||||
}
|
||||
}
|
||||
|
||||
return USB_DeviceHidSend(UsbCompositeDevice.keyboardHandle, USB_KEYBOARD_ENDPOINT_INDEX,
|
||||
(uint8_t*)&UsbKeyboardReport, USB_KEYBOARD_REPORT_LENGTH);
|
||||
(uint8_t*)&UsbKeyboardReport[activeLayout], USB_KEYBOARD_REPORT_LENGTH);
|
||||
}
|
||||
|
||||
usb_status_t UsbKeyboardCallback(class_handle_t handle, uint32_t event, void *param)
|
||||
|
||||
@@ -36,4 +36,5 @@
|
||||
extern usb_status_t UsbKeyboardSetConfiguration(class_handle_t handle, uint8_t configuration);
|
||||
extern usb_status_t UsbKeyboardSetInterface(class_handle_t handle, uint8_t interface, uint8_t alternateSetting);
|
||||
|
||||
extern void usbKeyboadTask();
|
||||
#endif
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "fsl_i2c.h"
|
||||
#include "i2c_addresses.h"
|
||||
|
||||
void I2cRead(I2C_Type *baseAddress, uint8_t i2cAddress, uint8_t *data, uint8_t size)
|
||||
status_t I2cRead(I2C_Type *baseAddress, uint8_t i2cAddress, uint8_t *data, uint8_t size)
|
||||
{
|
||||
i2c_master_transfer_t masterXfer;
|
||||
masterXfer.slaveAddress = i2cAddress;
|
||||
@@ -11,10 +11,10 @@ void I2cRead(I2C_Type *baseAddress, uint8_t i2cAddress, uint8_t *data, uint8_t s
|
||||
masterXfer.data = data;
|
||||
masterXfer.dataSize = size;
|
||||
masterXfer.flags = kI2C_TransferDefaultFlag;
|
||||
I2C_MasterTransferBlocking(baseAddress, &masterXfer);
|
||||
return I2C_MasterTransferBlocking(baseAddress, &masterXfer);
|
||||
}
|
||||
|
||||
void I2cWrite(I2C_Type *baseAddress, uint8_t i2cAddress, uint8_t *data, uint8_t size)
|
||||
status_t I2cWrite(I2C_Type *baseAddress, uint8_t i2cAddress, uint8_t *data, uint8_t size)
|
||||
{
|
||||
i2c_master_transfer_t masterXfer;
|
||||
masterXfer.slaveAddress = i2cAddress;
|
||||
@@ -24,5 +24,5 @@ void I2cWrite(I2C_Type *baseAddress, uint8_t i2cAddress, uint8_t *data, uint8_t
|
||||
masterXfer.data = data;
|
||||
masterXfer.dataSize = size;
|
||||
masterXfer.flags = kI2C_TransferDefaultFlag;
|
||||
I2C_MasterTransferBlocking(baseAddress, &masterXfer);
|
||||
return I2C_MasterTransferBlocking(baseAddress, &masterXfer);
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
|
||||
// Functions:
|
||||
|
||||
void I2cRead(I2C_Type *baseAddress, uint8_t i2cAddress, uint8_t *data, uint8_t size);
|
||||
void I2cWrite(I2C_Type *baseAddress, uint8_t i2cAddress, uint8_t *data, uint8_t size);
|
||||
status_t I2cRead(I2C_Type *baseAddress, uint8_t i2cAddress, uint8_t *data, uint8_t size);
|
||||
status_t I2cWrite(I2C_Type *baseAddress, uint8_t i2cAddress, uint8_t *data, uint8_t size);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user