Files
refleks-lite/arcadestick_software/Core/Src/main.c

531 lines
17 KiB
C

/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2026 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usb_device.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usbd_core.h"
#ifdef REFLEKS_XINPUT
#include "xinput_app.h"
#else
#include "dinput_app.h"
#endif
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define MODE_SWITCH_DEBOUNCE_MS 50U
#define USB_DISCONNECT_DELAY_MS 100U
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
static uint32_t modeSwitchChangedAt;
static uint8_t modeSwitchChangePending;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
static void ModeSwitch_Process(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
extern USBD_HandleTypeDef hUsbDeviceFS;
#if 0 /* Replaced by dinput_app.c; kept inside CubeMX user section only. */
typedef struct
{
uint8_t dpad; // D-pad (4 bit) [Up, Down, Left, Right]
uint8_t buttonLSB; // Buton 1-8 (1 bit)
uint8_t buttonMSB; // Buton 9-10 (1 bit)
} __attribute__((packed)) gamePAD;
gamePAD gamepad = {0};
#endif
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
#ifdef APP_VECTOR_ADDRESS
/* Relocated applications must own all interrupts before HAL starts SysTick. */
SCB->VTOR = APP_VECTOR_ADDRESS;
__DSB();
__ISB();
#endif
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
ModeSwitch_Process();
#ifdef REFLEKS_XINPUT
XInput_AppProcess();
#else
DInput_AppProcess();
#if 0 /* Legacy 3-byte HID loop. */
if(HAL_GPIO_ReadPin(UP_GPIO_Port, UP_Pin) == GPIO_PIN_SET &&
HAL_GPIO_ReadPin(RIGHT_GPIO_Port, RIGHT_Pin) == GPIO_PIN_SET &&
HAL_GPIO_ReadPin(DOWN_GPIO_Port, DOWN_Pin) == GPIO_PIN_SET &&
HAL_GPIO_ReadPin(LEFT_GPIO_Port, LEFT_Pin) == GPIO_PIN_SET)
{
gamepad.dpad = 8;
//centre
}
else if(HAL_GPIO_ReadPin(UP_GPIO_Port, UP_Pin) == GPIO_PIN_RESET &&
HAL_GPIO_ReadPin(RIGHT_GPIO_Port, RIGHT_Pin) == GPIO_PIN_RESET)
{
HAL_Delay(5);
if(HAL_GPIO_ReadPin(UP_GPIO_Port, UP_Pin) == GPIO_PIN_RESET &&
HAL_GPIO_ReadPin(RIGHT_GPIO_Port, RIGHT_Pin) == GPIO_PIN_RESET)
{
gamepad.dpad = 1;
//up-right
}
}
else if(HAL_GPIO_ReadPin(DOWN_GPIO_Port, DOWN_Pin) == GPIO_PIN_RESET &&
HAL_GPIO_ReadPin(RIGHT_GPIO_Port, RIGHT_Pin) == GPIO_PIN_RESET)
{
HAL_Delay(5);
if(HAL_GPIO_ReadPin(DOWN_GPIO_Port, DOWN_Pin) == GPIO_PIN_RESET &&
HAL_GPIO_ReadPin(RIGHT_GPIO_Port, RIGHT_Pin) == GPIO_PIN_RESET)
{
gamepad.dpad = 3;
//down-right
}
}
else if(HAL_GPIO_ReadPin(DOWN_GPIO_Port, DOWN_Pin) == GPIO_PIN_RESET &&
HAL_GPIO_ReadPin(LEFT_GPIO_Port, LEFT_Pin) == GPIO_PIN_RESET)
{
HAL_Delay(5);
if(HAL_GPIO_ReadPin(DOWN_GPIO_Port, DOWN_Pin) == GPIO_PIN_RESET &&
HAL_GPIO_ReadPin(LEFT_GPIO_Port, LEFT_Pin) == GPIO_PIN_RESET)
{
gamepad.dpad = 5;
//down-left
}
}
else if(HAL_GPIO_ReadPin(UP_GPIO_Port, UP_Pin) == GPIO_PIN_RESET &&
HAL_GPIO_ReadPin(LEFT_GPIO_Port, LEFT_Pin) == GPIO_PIN_RESET)
{
HAL_Delay(5);
if(HAL_GPIO_ReadPin(UP_GPIO_Port, UP_Pin) == GPIO_PIN_RESET &&
HAL_GPIO_ReadPin(LEFT_GPIO_Port, LEFT_Pin) == GPIO_PIN_RESET)
{
gamepad.dpad = 7;
//up-left
}
}
else if(HAL_GPIO_ReadPin(UP_GPIO_Port, UP_Pin) == GPIO_PIN_RESET)
{
HAL_Delay(5);
if(HAL_GPIO_ReadPin(UP_GPIO_Port, UP_Pin) == GPIO_PIN_RESET)
{
gamepad.dpad = 0;
//up
}
}
else if(HAL_GPIO_ReadPin(DOWN_GPIO_Port, DOWN_Pin) == GPIO_PIN_RESET)
{
HAL_Delay(5);
if(HAL_GPIO_ReadPin(DOWN_GPIO_Port, DOWN_Pin) == GPIO_PIN_RESET)
{
gamepad.dpad = 4;
//down
}
}
else if(HAL_GPIO_ReadPin(LEFT_GPIO_Port, LEFT_Pin) == GPIO_PIN_RESET)
{
HAL_Delay(5);
if(HAL_GPIO_ReadPin(LEFT_GPIO_Port, LEFT_Pin) == GPIO_PIN_RESET)
{
gamepad.dpad = 6;
//left
}
}
else if(HAL_GPIO_ReadPin(RIGHT_GPIO_Port, RIGHT_Pin) == GPIO_PIN_RESET)
{
HAL_Delay(5);
if(HAL_GPIO_ReadPin(RIGHT_GPIO_Port, RIGHT_Pin) == GPIO_PIN_RESET)
{
gamepad.dpad = 2;
//right
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(X_GPIO_Port, X_Pin)== GPIO_PIN_RESET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(X_GPIO_Port, X_Pin)== GPIO_PIN_RESET) {
gamepad.buttonLSB |=1 << 0;
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(Y_GPIO_Port, Y_Pin)== GPIO_PIN_RESET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(Y_GPIO_Port, Y_Pin)== GPIO_PIN_RESET) {
gamepad.buttonLSB |= 1 << 1;
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(A_GPIO_Port, A_Pin)== GPIO_PIN_RESET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(A_GPIO_Port, A_Pin)== GPIO_PIN_RESET) {
gamepad.buttonLSB |= 1 << 2;
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(B_GPIO_Port, B_Pin)== GPIO_PIN_RESET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(B_GPIO_Port, B_Pin)== GPIO_PIN_RESET) {
gamepad.buttonLSB |= 1 << 3;
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(RB_GPIO_Port, RB_Pin)== GPIO_PIN_RESET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(RB_GPIO_Port, RB_Pin)== GPIO_PIN_RESET) {
gamepad.buttonLSB |= 1 << 4;
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(LB_GPIO_Port, LB_Pin)== GPIO_PIN_RESET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(LB_GPIO_Port, LB_Pin)== GPIO_PIN_RESET) {
gamepad.buttonLSB |= 1 << 5;
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(RT_GPIO_Port, RT_Pin)== GPIO_PIN_RESET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(RT_GPIO_Port, RT_Pin)== GPIO_PIN_RESET) {
gamepad.buttonLSB |= 1 << 6;
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(LT_GPIO_Port, LT_Pin)== GPIO_PIN_RESET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(LT_GPIO_Port, LT_Pin)== GPIO_PIN_RESET) {
gamepad.buttonLSB |= 1 << 7;
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(Start_GPIO_Port, Start_Pin)== GPIO_PIN_RESET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(Start_GPIO_Port, Start_Pin)== GPIO_PIN_RESET) {
gamepad.buttonMSB |= 1 << 0;
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(Select_GPIO_Port, Select_Pin)== GPIO_PIN_RESET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(Select_GPIO_Port, Select_Pin)== GPIO_PIN_RESET) {
gamepad.buttonMSB |= 1 << 1;
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(X_GPIO_Port, X_Pin)== GPIO_PIN_SET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(X_GPIO_Port, X_Pin)== GPIO_PIN_SET) {
gamepad.buttonLSB &= ~(1 << 0);
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(Y_GPIO_Port, Y_Pin)== GPIO_PIN_SET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(Y_GPIO_Port, Y_Pin)== GPIO_PIN_SET) {
gamepad.buttonLSB &= ~ (1 << 1);
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(A_GPIO_Port, A_Pin)== GPIO_PIN_SET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(A_GPIO_Port, A_Pin)== GPIO_PIN_SET) {
gamepad.buttonLSB &= ~(1 << 2);
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(B_GPIO_Port, B_Pin)== GPIO_PIN_SET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(B_GPIO_Port, B_Pin)== GPIO_PIN_SET) {
gamepad.buttonLSB &= ~(1 << 3);
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(RB_GPIO_Port, RB_Pin)== GPIO_PIN_SET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(RB_GPIO_Port, RB_Pin)== GPIO_PIN_SET) {
gamepad.buttonLSB &= ~(1 << 4);
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(LB_GPIO_Port, LB_Pin)== GPIO_PIN_SET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(LB_GPIO_Port, LB_Pin)== GPIO_PIN_SET) {
gamepad.buttonLSB &= ~(1 << 5);
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(RT_GPIO_Port, RT_Pin)== GPIO_PIN_SET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(RT_GPIO_Port, RT_Pin)== GPIO_PIN_SET) {
gamepad.buttonLSB &= ~ (1 << 6);
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(LT_GPIO_Port, LT_Pin)== GPIO_PIN_SET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(LT_GPIO_Port, LT_Pin)== GPIO_PIN_SET) {
gamepad.buttonLSB &= ~(1 << 7);
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(Start_GPIO_Port, Start_Pin)== GPIO_PIN_SET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(Start_GPIO_Port, Start_Pin)== GPIO_PIN_SET) {
gamepad.buttonMSB &= ~(1 << 0);
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
if (HAL_GPIO_ReadPin(Select_GPIO_Port, Select_Pin)== GPIO_PIN_SET) {
HAL_Delay(5);
if (HAL_GPIO_ReadPin(Select_GPIO_Port, Select_Pin)== GPIO_PIN_SET) {
gamepad.buttonMSB &= ~(1 << 1);
}
}
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&gamepad, sizeof(gamepad));
#endif
#endif
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pins : Start_Pin UP_Pin DOWN_Pin RIGHT_Pin
LEFT_Pin A_Pin B_Pin Y_Pin
X_Pin RB_Pin RT_Pin LB_Pin
LT_Pin */
GPIO_InitStruct.Pin = Start_Pin|UP_Pin|DOWN_Pin|RIGHT_Pin
|LEFT_Pin|A_Pin|B_Pin|Y_Pin
|X_Pin|RB_Pin|RT_Pin|LB_Pin
|LT_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pins : Select_Pin DINPUT_Pin XINPUT_Pin */
GPIO_InitStruct.Pin = Select_Pin|DINPUT_Pin|XINPUT_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/**
* @brief Restarts the MCU when the slide switch requests the other USB mode.
* The bootloader will read the same switch and start the selected app.
*/
static void ModeSwitch_Process(void)
{
GPIO_PinState dinputState = HAL_GPIO_ReadPin(DINPUT_GPIO_Port, DINPUT_Pin);
GPIO_PinState xinputState = HAL_GPIO_ReadPin(XINPUT_GPIO_Port, XINPUT_Pin);
/* The switch is valid only when exactly one active-low input is selected. */
#ifdef REFLEKS_XINPUT
if ((dinputState == GPIO_PIN_SET) && (xinputState == GPIO_PIN_RESET))
#else
if ((dinputState == GPIO_PIN_RESET) && (xinputState == GPIO_PIN_SET))
#endif
{
if (modeSwitchChangePending == 0U)
{
modeSwitchChangedAt = HAL_GetTick();
modeSwitchChangePending = 1U;
}
else if ((HAL_GetTick() - modeSwitchChangedAt) >= MODE_SWITCH_DEBOUNCE_MS)
{
/* Force a clean USB disconnect before the bootloader changes USB mode. */
(void)USBD_Stop(&hUsbDeviceFS);
HAL_Delay(USB_DISCONNECT_DELAY_MS);
NVIC_SystemReset();
}
}
else
{
/* Current mode selected, both open, or switch contact transition/bounce. */
modeSwitchChangePending = 0U;
}
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */