Added keyboard driver from Manuel.

This commit is contained in:
Dual Tachyon 2023-08-10 21:34:28 +01:00
parent 4276ef807c
commit acbf814fb4
7 changed files with 252 additions and 2 deletions

View File

@ -24,6 +24,7 @@ OBJS += driver/eeprom.o
OBJS += driver/flash.o
OBJS += driver/gpio.o
OBJS += driver/i2c.o
OBJS += driver/keyboard.o
OBJS += driver/spi.o
OBJS += driver/st7565.o
OBJS += driver/system.o

View File

@ -20,6 +20,15 @@
#include <stdint.h>
enum GPIOA_PINS {
GPIOA_PIN_KEYBOARD_0 = 3,
GPIOA_PIN_KEYBOARD_1 = 4,
GPIOA_PIN_KEYBOARD_2 = 5,
GPIOA_PIN_KEYBOARD_3 = 6,
GPIOA_PIN_KEYBOARD_4 = 10,
GPIOA_PIN_KEYBOARD_5 = 11,
GPIOA_PIN_KEYBOARD_6 = 12,
GPIOA_PIN_KEYBOARD_7 = 13,
GPIOA_PIN_I2C_SCL = 10,
GPIOA_PIN_I2C_SDA = 11,
};

131
driver/keyboard.c Normal file
View File

@ -0,0 +1,131 @@
/* Copyright 2023 Manuel Jinger
* Copyright 2023 Dual Tachyon
* https://github.com/DualTachyon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bsp/dp32g030/gpio.h"
#include "driver/gpio.h"
#include "driver/keyboard.h"
#include "driver/systick.h"
KEY_Code_t KEYBOARD_Poll(void)
{
GPIO_SetBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_4);
GPIO_SetBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_5);
GPIO_SetBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_6);
GPIO_SetBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_7);
SYSTICK_DelayUs(1);
// Keys connected to gnd
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_0)) {
return KEY_SIDE1;
}
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_1)) {
return KEY_SIDE2;
}
// Original doesn't do PTT
// First row
GPIO_ClearBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_4);
SYSTICK_DelayUs(1);
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_0)) {
return KEY_MENU;
}
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_1)) {
return KEY_1;
}
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_2)) {
return KEY_4;
}
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_3)) {
return KEY_7;
}
// Second row
GPIO_ClearBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_5);
SYSTICK_DelayUs(1);
GPIO_SetBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_4);
SYSTICK_DelayUs(1);
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_0)) {
return KEY_UP;
}
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_1)) {
return KEY_2;
}
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_2)) {
return KEY_5;
}
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_3)) {
return KEY_8;
}
// Third row
GPIO_ClearBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_4);
SYSTICK_DelayUs(1);
GPIO_ClearBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_5);
SYSTICK_DelayUs(1);
GPIO_SetBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_4);
SYSTICK_DelayUs(1);
GPIO_ClearBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_6);
SYSTICK_DelayUs(1);
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_0)) {
return KEY_DOWN;
}
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_1)) {
return KEY_3;
}
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_2)) {
return KEY_6;
}
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_3)) {
return KEY_9;
}
// Fourth row
GPIO_ClearBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_7);
SYSTICK_DelayUs(1);
GPIO_SetBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_6);
SYSTICK_DelayUs(1);
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_0)) {
return KEY_EXIT;
}
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_1)) {
return KEY_STAR;
}
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_2)) {
return KEY_0;
}
if (!GPIO_CheckBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_3)) {
return KEY_F;
}
GPIO_SetBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_4);
GPIO_SetBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_5);
GPIO_ClearBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_6);
GPIO_SetBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_7);
return KEY_INVALID;
}

49
driver/keyboard.h Normal file
View File

@ -0,0 +1,49 @@
/* Copyright 2023 Manuel Jinger
* Copyright 2023 Dual Tachyon
* https://github.com/DualTachyon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DRIVER_KEYBOARD_H
#define DRIVER_KEYBOARD_H
enum KEY_Code_t {
KEY_0 = 0,
KEY_1 = 1,
KEY_2 = 2,
KEY_3 = 3,
KEY_4 = 4,
KEY_5 = 5,
KEY_6 = 6,
KEY_7 = 7,
KEY_8 = 8,
KEY_9 = 9,
KEY_MENU = 10,
KEY_UP = 11,
KEY_DOWN = 12,
KEY_EXIT = 13,
KEY_STAR = 14,
KEY_F = 15,
KEY_PTT = 21,
KEY_SIDE2 = 22,
KEY_SIDE1 = 23,
KEY_INVALID = 255,
};
typedef enum KEY_Code_t KEY_Code_t;
KEY_Code_t KEYBOARD_Poll(void);
#endif

View File

@ -94,3 +94,13 @@ void UART_Send(const void *pBuffer, uint32_t Size)
}
}
// TODO: Not part of the original FW, but used for easier testing
void UART_Print(const char *pString)
{
while (*pString) {
UART1->TDR = (uint8_t)*pString++;
while ((UART1->IF & UART_IF_TXFIFO_FULL_MASK) != UART_IF_TXFIFO_FULL_BITS_NOT_SET) {
}
}
}

View File

@ -24,6 +24,7 @@ extern uint8_t UART_DMA_Buffer[256];
void UART_Init(void);
void UART_Send(const void *pBuffer, uint32_t Size);
void UART_Print(const char *pString);
#endif

53
main.c
View File

@ -26,6 +26,7 @@
#include "driver/eeprom.h"
#include "driver/flash.h"
#include "driver/gpio.h"
#include "driver/keyboard.h"
#include "driver/st7565.h"
#include "driver/system.h"
#include "driver/systick.h"
@ -61,6 +62,49 @@ static void BACKLIGHT_TurnOn(void)
GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_BACKLIGHT);
}
#if 0
static void ProcessKey(void)
{
KEY_Code_t Key;
Key = KEYBOARD_Poll();
switch (Key) {
case KEY_0: UART_Print("ZERO\r\n"); break;
case KEY_1: UART_Print("ONE\r\n"); break;
case KEY_2: UART_Print("TWO\r\n"); break;
case KEY_3: UART_Print("THREE\r\n"); break;
case KEY_4: UART_Print("FOUR\r\n"); break;
case KEY_5: UART_Print("FIVE\r\n"); break;
case KEY_6: UART_Print("SIX\r\n"); break;
case KEY_7: UART_Print("SEVEN\r\n"); break;
case KEY_8: UART_Print("EIGHT\r\n"); break;
case KEY_9: UART_Print("NINE\r\n"); break;
case KEY_MENU: UART_Print("MENU\r\n"); break;
case KEY_UP: UART_Print("UP\r\n"); break;
case KEY_DOWN: UART_Print("DOWN\r\n"); break;
case KEY_EXIT: UART_Print("EXIT\r\n"); break;
case KEY_STAR: UART_Print("STAR\r\n"); break;
case KEY_F: UART_Print("F\r\n"); break;
case KEY_PTT: UART_Print("PTT\r\n"); break;
case KEY_SIDE2: UART_Print("SIDE2\r\n"); break;
case KEY_SIDE1: UART_Print("SIDE1\r\n"); break;
case KEY_INVALID: break;
}
}
#endif
static void Console(void)
{
KEY_Code_t Key;
Key = KEYBOARD_Poll();
if (Key != KEY_INVALID) {
Key += 0x40;
UART_Send(&Key, 1);
}
}
void Main(void)
{
// Enable clock gating of blocks we need.
@ -106,9 +150,14 @@ void Main(void)
EEPROM_ReadBuffer(0x0EB0, Test, 8);
while (1) {
SYSTEM_DelayMs(500);
Console();
SYSTEM_DelayMs(200);
FLASHLIGHT_TurnOff();
SYSTEM_DelayMs(500);
Console();
SYSTEM_DelayMs(200);
FLASHLIGHT_TurnOn();
}
}