forked from Nitrokey/nitrokey-pro-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from cyril-L/dev/keybi
Started the keyboard matrix driver and a keymap, tested on prototype PCB
- Loading branch information
Showing
22 changed files
with
1,500 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "keybi/drivers/delay.h" | ||
#include "stm32f10x_systick.h" | ||
|
||
void Keybi_DelayUs(uint32_t duration_us) { | ||
uint32_t remaining_ticks = duration_us * 72; | ||
uint32_t prev = SysTick_GetCounter(); | ||
|
||
while (1) { | ||
uint32_t curr = SysTick_GetCounter(); | ||
uint32_t elapsed; | ||
if (curr > prev) { | ||
elapsed = 719999 + prev - curr; | ||
} else { | ||
elapsed = prev - curr; | ||
} | ||
if (elapsed >= remaining_ticks) { | ||
break; | ||
} | ||
remaining_ticks -= elapsed; | ||
prev = curr; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include "stm32f10x.h" | ||
|
||
void Keybi_DelayUs(uint32_t duration_us); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include "keybi/drivers/matrix.h" | ||
#include "keybi/drivers/delay.h" | ||
|
||
typedef struct gpio_t { | ||
GPIO_TypeDef * port; | ||
uint16_t pin; | ||
} gpio_t; | ||
|
||
static gpio_t rows[KEYBI_MATRIX_ROWS] = { | ||
{GPIOC, GPIO_Pin_3}, // R01 | ||
{GPIOA, GPIO_Pin_0}, // R02 | ||
{GPIOA, GPIO_Pin_1}, // R03 | ||
{GPIOC, GPIO_Pin_4}, // R04 | ||
{GPIOC, GPIO_Pin_5}}; // R05 | ||
|
||
static gpio_t cols[KEYBI_MATRIX_COLS] = { | ||
{GPIOB, GPIO_Pin_7}, // C01 | ||
{GPIOB, GPIO_Pin_8}, // C02 | ||
{GPIOB, GPIO_Pin_9}, // C03 | ||
{GPIOC, GPIO_Pin_0}, // C04 | ||
{GPIOC, GPIO_Pin_1}, // C05 | ||
{GPIOC, GPIO_Pin_2}, // C06 | ||
{GPIOA, GPIO_Pin_15}, // C07 | ||
{GPIOC, GPIO_Pin_10}, // C08 | ||
{GPIOC, GPIO_Pin_11}, // C09 | ||
{GPIOC, GPIO_Pin_12}, // C10 | ||
{GPIOA, GPIO_Pin_10}, // C11 | ||
{GPIOA, GPIO_Pin_9}, // C12 | ||
{GPIOC, GPIO_Pin_9}, // C13 | ||
{GPIOB, GPIO_Pin_1}}; // C14 | ||
|
||
static uint8_t key_states[KEYBI_MATRIX_COLS][KEYBI_MATRIX_ROWS] = {0}; | ||
|
||
void Keybi_Matrix_Init(void) { | ||
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); | ||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); | ||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); | ||
|
||
for (int r = 0; r < KEYBI_MATRIX_ROWS; ++r) { | ||
GPIO_InitTypeDef GPIO_InitStructure; | ||
GPIO_InitStructure.GPIO_Pin = rows[r].pin; | ||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; | ||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | ||
GPIO_Init(rows[r].port, &GPIO_InitStructure); | ||
} | ||
|
||
for (int c = 0; c < KEYBI_MATRIX_COLS; ++c) { | ||
GPIO_InitTypeDef GPIO_InitStructure; | ||
GPIO_InitStructure.GPIO_Pin = cols[c].pin; | ||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; | ||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | ||
GPIO_Init(cols[c].port, &GPIO_InitStructure); | ||
} | ||
} | ||
|
||
void Keybi_Matrix_Scan(int (*callback)(keybi_keyboard_matrix_event_t)) { | ||
for (uint8_t c = 0; c < KEYBI_MATRIX_COLS; ++c) { | ||
GPIO_SetBits(cols[c].port, cols[c].pin); | ||
for (uint8_t r = 0; r < KEYBI_MATRIX_ROWS; ++r) { | ||
uint8_t state = GPIO_ReadInputDataBit(rows[r].port, rows[r].pin); | ||
if (state != key_states[c][r]) { | ||
keybi_keyboard_matrix_event_t event = { | ||
.col = c, | ||
.row = r, | ||
.pressed = state, | ||
.time = 0 | ||
}; | ||
if (callback(event) == 0) { | ||
// Save state if callback has been able to handle it | ||
key_states[c][r] = state; | ||
} | ||
} | ||
} | ||
GPIO_ResetBits(cols[c].port, cols[c].pin); | ||
// Adjacent columns of the first rows registered on a single key press | ||
// TODO investigate why and find the appropriate delay | ||
Keybi_DelayUs(100); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include "stm32f10x.h" | ||
|
||
#define KEYBI_MATRIX_ROWS (5) | ||
#define KEYBI_MATRIX_COLS (14) | ||
|
||
typedef struct { | ||
uint8_t col; | ||
uint8_t row; | ||
uint8_t pressed; | ||
uint16_t time; | ||
} keybi_keyboard_matrix_event_t; | ||
|
||
void Keybi_Matrix_Init(void); | ||
void Keybi_Matrix_Scan(int (*callback)(keybi_keyboard_matrix_event_t)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.