Initial Refleks Lite hardware and firmware release

This commit is contained in:
2026-09-03 16:38:04 +03:00
commit 6e552c2ef1
146 changed files with 92135 additions and 0 deletions

View File

@@ -0,0 +1,155 @@
#include "main.h"
#include "usb_device.h"
#include "usbd_dfu_bootloader.h"
typedef void (*ApplicationEntry)(void);
static void Bootloader_SystemClockConfig(void);
static void Bootloader_GPIOInit(void);
static uint8_t Bootloader_IsApplicationValid(uint32_t address);
static void Bootloader_JumpToApplication(uint32_t address);
int main(void)
{
uint32_t selectedAddress = 0U;
HAL_Init();
Bootloader_GPIOInit();
HAL_Delay(20U);
/* Holding START during reset always forces USB DFU mode. */
if (HAL_GPIO_ReadPin(Start_GPIO_Port, Start_Pin) != GPIO_PIN_RESET)
{
GPIO_PinState pa4 = HAL_GPIO_ReadPin(DINPUT_GPIO_Port, DINPUT_Pin);
GPIO_PinState pa5 = HAL_GPIO_ReadPin(XINPUT_GPIO_Port, XINPUT_Pin);
/* Actual board wiring: PA4 LOW selects XInput, PA5 LOW selects DInput. */
if ((pa4 == GPIO_PIN_RESET) && (pa5 == GPIO_PIN_SET))
{
selectedAddress = DFU_APP1_ADDRESS;
}
else if ((pa4 == GPIO_PIN_SET) && (pa5 == GPIO_PIN_RESET))
{
selectedAddress = DFU_APP2_ADDRESS;
}
}
if ((selectedAddress != 0U) && Bootloader_IsApplicationValid(selectedAddress))
{
Bootloader_JumpToApplication(selectedAddress);
}
Bootloader_SystemClockConfig();
MX_USB_DEVICE_Init();
while (1)
{
}
}
static void Bootloader_GPIOInit(void)
{
GPIO_InitTypeDef gpio = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
gpio.Pin = DINPUT_Pin | XINPUT_Pin;
gpio.Mode = GPIO_MODE_INPUT;
gpio.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &gpio);
gpio.Pin = Start_Pin;
HAL_GPIO_Init(GPIOC, &gpio);
}
static uint8_t Bootloader_IsApplicationValid(uint32_t address)
{
uint32_t stackPointer = *(volatile uint32_t *)address;
uint32_t resetHandler = *(volatile uint32_t *)(address + 4U);
uint32_t slotEnd = address + DFU_APP_SLOT_SIZE;
return ((stackPointer >= SRAM_BASE) &&
(stackPointer <= (SRAM_BASE + (20U * 1024U))) &&
((resetHandler & 1U) != 0U) &&
((resetHandler & ~1U) >= address) &&
((resetHandler & ~1U) < slotEnd)) ? 1U : 0U;
}
static void Bootloader_JumpToApplication(uint32_t address)
{
uint32_t stackPointer = *(volatile uint32_t *)address;
uint32_t resetHandler = *(volatile uint32_t *)(address + 4U);
__disable_irq();
SysTick->CTRL = 0U;
SysTick->LOAD = 0U;
SysTick->VAL = 0U;
HAL_DeInit();
NVIC->ICER[0] = 0xFFFFFFFFU;
NVIC->ICER[1] = 0xFFFFFFFFU;
NVIC->ICPR[0] = 0xFFFFFFFFU;
NVIC->ICPR[1] = 0xFFFFFFFFU;
SCB->VTOR = address;
__DSB();
__ISB();
__enable_irq();
__asm volatile (
"msr msp, %0\n"
"bx %1\n"
:
: "r" (stackPointer), "r" (resetHandler)
: "memory");
while (1)
{
}
}
static void Bootloader_SystemClockConfig(void)
{
RCC_OscInitTypeDef oscillator = {0};
RCC_ClkInitTypeDef clocks = {0};
RCC_PeriphCLKInitTypeDef peripheral = {0};
oscillator.OscillatorType = RCC_OSCILLATORTYPE_HSE;
oscillator.HSEState = RCC_HSE_ON;
oscillator.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
oscillator.HSIState = RCC_HSI_ON;
oscillator.PLL.PLLState = RCC_PLL_ON;
oscillator.PLL.PLLSource = RCC_PLLSOURCE_HSE;
oscillator.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&oscillator) != HAL_OK)
{
Error_Handler();
}
clocks.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
clocks.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
clocks.AHBCLKDivider = RCC_SYSCLK_DIV1;
clocks.APB1CLKDivider = RCC_HCLK_DIV2;
clocks.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&clocks, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
peripheral.PeriphClockSelection = RCC_PERIPHCLK_USB;
peripheral.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
if (HAL_RCCEx_PeriphCLKConfig(&peripheral) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}

View File

@@ -0,0 +1,66 @@
#include "dinput_app.h"
#include "main.h"
#include "usb_device.h"
#include "usbd_dinput.h"
typedef struct __attribute__((packed))
{
uint16_t buttons;
uint8_t hat;
uint8_t axes[6];
} DInputReport;
_Static_assert(sizeof(DInputReport) == DINPUT_REPORT_SIZE,
"DInput input report must be exactly 9 bytes");
extern USBD_HandleTypeDef hUsbDeviceFS;
static uint8_t IsPressed(GPIO_TypeDef *port, uint16_t pin)
{
return (HAL_GPIO_ReadPin(port, pin) == GPIO_PIN_RESET) ? 1U : 0U;
}
static uint8_t ReadHat(void)
{
uint8_t up = IsPressed(UP_GPIO_Port, UP_Pin);
uint8_t down = IsPressed(DOWN_GPIO_Port, DOWN_Pin);
uint8_t left = IsPressed(LEFT_GPIO_Port, LEFT_Pin);
uint8_t right = IsPressed(RIGHT_GPIO_Port, RIGHT_Pin);
if (up && right && !down && !left) return 1U;
if (down && right && !up && !left) return 3U;
if (down && left && !up && !right) return 5U;
if (up && left && !down && !right) return 7U;
if (up && !down && !left && !right) return 0U;
if (right && !up && !down && !left) return 2U;
if (down && !up && !left && !right) return 4U;
if (left && !up && !down && !right) return 6U;
return 8U;
}
void DInput_AppProcess(void)
{
DInputReport report =
{
.buttons = 0U,
.hat = 8U,
.axes = {0x80U, 0x80U, 0x80U, 0x80U, 0x80U, 0x80U}
};
report.hat = ReadHat();
if (IsPressed(X_GPIO_Port, X_Pin)) report.buttons |= (1U << 0);
if (IsPressed(Y_GPIO_Port, Y_Pin)) report.buttons |= (1U << 1);
if (IsPressed(A_GPIO_Port, A_Pin)) report.buttons |= (1U << 2);
if (IsPressed(B_GPIO_Port, B_Pin)) report.buttons |= (1U << 3);
if (IsPressed(RB_GPIO_Port, RB_Pin)) report.buttons |= (1U << 4);
if (IsPressed(LB_GPIO_Port, LB_Pin)) report.buttons |= (1U << 5);
if (IsPressed(RT_GPIO_Port, RT_Pin)) report.buttons |= (1U << 6);
if (IsPressed(LT_GPIO_Port, LT_Pin)) report.buttons |= (1U << 7);
if (IsPressed(Start_GPIO_Port, Start_Pin)) report.buttons |= (1U << 8);
if (IsPressed(Select_GPIO_Port, Select_Pin)) report.buttons |= (1U << 9);
(void)USBD_DINPUT_SendReport(&hUsbDeviceFS,
(uint8_t *)&report,
sizeof(report));
}

View File

@@ -0,0 +1,530 @@
/* 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 */

View File

@@ -0,0 +1,85 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32f1xx_hal_msp.c
* @brief This file provides code for the MSP Initialization
* and de-Initialization codes.
******************************************************************************
* @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"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
/* USER CODE END TD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN Define */
/* USER CODE END Define */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN Macro */
/* USER CODE END Macro */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* External functions --------------------------------------------------------*/
/* USER CODE BEGIN ExternalFunctions */
/* USER CODE END ExternalFunctions */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* Initializes the Global MSP.
*/
void HAL_MspInit(void)
{
/* USER CODE BEGIN MspInit 0 */
/* USER CODE END MspInit 0 */
__HAL_RCC_AFIO_CLK_ENABLE();
__HAL_RCC_PWR_CLK_ENABLE();
/* System interrupt init*/
/** NOJTAG: JTAG-DP Disabled and SW-DP Enabled
*/
__HAL_AFIO_REMAP_SWJ_NOJTAG();
/* USER CODE BEGIN MspInit 1 */
/* USER CODE END MspInit 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

View File

@@ -0,0 +1,217 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32f1xx_it.c
* @brief Interrupt Service Routines.
******************************************************************************
* @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 "stm32f1xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
/* USER CODE END TD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/* External variables --------------------------------------------------------*/
extern PCD_HandleTypeDef hpcd_USB_FS;
/* USER CODE BEGIN EV */
/* USER CODE END EV */
/******************************************************************************/
/* Cortex-M3 Processor Interruption and Exception Handlers */
/******************************************************************************/
/**
* @brief This function handles Non maskable interrupt.
*/
void NMI_Handler(void)
{
/* USER CODE BEGIN NonMaskableInt_IRQn 0 */
/* USER CODE END NonMaskableInt_IRQn 0 */
/* USER CODE BEGIN NonMaskableInt_IRQn 1 */
while (1)
{
}
/* USER CODE END NonMaskableInt_IRQn 1 */
}
/**
* @brief This function handles Hard fault interrupt.
*/
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */
/* USER CODE END HardFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_HardFault_IRQn 0 */
/* USER CODE END W1_HardFault_IRQn 0 */
}
}
/**
* @brief This function handles Memory management fault.
*/
void MemManage_Handler(void)
{
/* USER CODE BEGIN MemoryManagement_IRQn 0 */
/* USER CODE END MemoryManagement_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
/* USER CODE END W1_MemoryManagement_IRQn 0 */
}
}
/**
* @brief This function handles Prefetch fault, memory access fault.
*/
void BusFault_Handler(void)
{
/* USER CODE BEGIN BusFault_IRQn 0 */
/* USER CODE END BusFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_BusFault_IRQn 0 */
/* USER CODE END W1_BusFault_IRQn 0 */
}
}
/**
* @brief This function handles Undefined instruction or illegal state.
*/
void UsageFault_Handler(void)
{
/* USER CODE BEGIN UsageFault_IRQn 0 */
/* USER CODE END UsageFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_UsageFault_IRQn 0 */
/* USER CODE END W1_UsageFault_IRQn 0 */
}
}
/**
* @brief This function handles System service call via SWI instruction.
*/
void SVC_Handler(void)
{
/* USER CODE BEGIN SVCall_IRQn 0 */
/* USER CODE END SVCall_IRQn 0 */
/* USER CODE BEGIN SVCall_IRQn 1 */
/* USER CODE END SVCall_IRQn 1 */
}
/**
* @brief This function handles Debug monitor.
*/
void DebugMon_Handler(void)
{
/* USER CODE BEGIN DebugMonitor_IRQn 0 */
/* USER CODE END DebugMonitor_IRQn 0 */
/* USER CODE BEGIN DebugMonitor_IRQn 1 */
/* USER CODE END DebugMonitor_IRQn 1 */
}
/**
* @brief This function handles Pendable request for system service.
*/
void PendSV_Handler(void)
{
/* USER CODE BEGIN PendSV_IRQn 0 */
/* USER CODE END PendSV_IRQn 0 */
/* USER CODE BEGIN PendSV_IRQn 1 */
/* USER CODE END PendSV_IRQn 1 */
}
/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
/* USER CODE BEGIN SysTick_IRQn 0 */
/* USER CODE END SysTick_IRQn 0 */
HAL_IncTick();
/* USER CODE BEGIN SysTick_IRQn 1 */
/* USER CODE END SysTick_IRQn 1 */
}
/******************************************************************************/
/* STM32F1xx Peripheral Interrupt Handlers */
/* Add here the Interrupt Handlers for the used peripherals. */
/* For the available peripheral interrupt handler names, */
/* please refer to the startup file (startup_stm32f1xx.s). */
/******************************************************************************/
/**
* @brief This function handles USB low priority or CAN RX0 interrupts.
*/
void USB_LP_CAN1_RX0_IRQHandler(void)
{
/* USER CODE BEGIN USB_LP_CAN1_RX0_IRQn 0 */
/* USER CODE END USB_LP_CAN1_RX0_IRQn 0 */
HAL_PCD_IRQHandler(&hpcd_USB_FS);
/* USER CODE BEGIN USB_LP_CAN1_RX0_IRQn 1 */
/* USER CODE END USB_LP_CAN1_RX0_IRQn 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

View File

@@ -0,0 +1,176 @@
/**
******************************************************************************
* @file syscalls.c
* @author Auto-generated by STM32CubeIDE
* @brief STM32CubeIDE Minimal System calls file
*
* For more information about which c-functions
* need which of these lowlevel functions
* please consult the Newlib libc-manual
******************************************************************************
* @attention
*
* Copyright (c) 2020-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.
*
******************************************************************************
*/
/* Includes */
#include <sys/stat.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
/* Variables */
extern int __io_putchar(int ch) __attribute__((weak));
extern int __io_getchar(void) __attribute__((weak));
char *__env[1] = { 0 };
char **environ = __env;
/* Functions */
void initialise_monitor_handles()
{
}
int _getpid(void)
{
return 1;
}
int _kill(int pid, int sig)
{
(void)pid;
(void)sig;
errno = EINVAL;
return -1;
}
void _exit (int status)
{
_kill(status, -1);
while (1) {} /* Make sure we hang here */
}
__attribute__((weak)) int _read(int file, char *ptr, int len)
{
(void)file;
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
*ptr++ = __io_getchar();
}
return len;
}
__attribute__((weak)) int _write(int file, char *ptr, int len)
{
(void)file;
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
__io_putchar(*ptr++);
}
return len;
}
int _close(int file)
{
(void)file;
return -1;
}
int _fstat(int file, struct stat *st)
{
(void)file;
st->st_mode = S_IFCHR;
return 0;
}
int _isatty(int file)
{
(void)file;
return 1;
}
int _lseek(int file, int ptr, int dir)
{
(void)file;
(void)ptr;
(void)dir;
return 0;
}
int _open(char *path, int flags, ...)
{
(void)path;
(void)flags;
/* Pretend like we always fail */
return -1;
}
int _wait(int *status)
{
(void)status;
errno = ECHILD;
return -1;
}
int _unlink(char *name)
{
(void)name;
errno = ENOENT;
return -1;
}
int _times(struct tms *buf)
{
(void)buf;
return -1;
}
int _stat(char *file, struct stat *st)
{
(void)file;
st->st_mode = S_IFCHR;
return 0;
}
int _link(char *old, char *new)
{
(void)old;
(void)new;
errno = EMLINK;
return -1;
}
int _fork(void)
{
errno = EAGAIN;
return -1;
}
int _execve(char *name, char **argv, char **env)
{
(void)name;
(void)argv;
(void)env;
errno = ENOMEM;
return -1;
}

View File

@@ -0,0 +1,79 @@
/**
******************************************************************************
* @file sysmem.c
* @author Generated by STM32CubeIDE
* @brief STM32CubeIDE System Memory calls file
*
* For more information about which C functions
* need which of these lowlevel functions
* please consult the newlib libc manual
******************************************************************************
* @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.
*
******************************************************************************
*/
/* Includes */
#include <errno.h>
#include <stdint.h>
/**
* Pointer to the current high watermark of the heap usage
*/
static uint8_t *__sbrk_heap_end = NULL;
/**
* @brief _sbrk() allocates memory to the newlib heap and is used by malloc
* and others from the C library
*
* @verbatim
* ############################################################################
* # .data # .bss # newlib heap # MSP stack #
* # # # # Reserved by _Min_Stack_Size #
* ############################################################################
* ^-- RAM start ^-- _end _estack, RAM end --^
* @endverbatim
*
* This implementation starts allocating at the '_end' linker symbol
* The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack
* The implementation considers '_estack' linker symbol to be RAM end
* NOTE: If the MSP stack, at any point during execution, grows larger than the
* reserved size, please increase the '_Min_Stack_Size'.
*
* @param incr Memory size
* @return Pointer to allocated memory
*/
void *_sbrk(ptrdiff_t incr)
{
extern uint8_t _end; /* Symbol defined in the linker script */
extern uint8_t _estack; /* Symbol defined in the linker script */
extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */
const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
const uint8_t *max_heap = (uint8_t *)stack_limit;
uint8_t *prev_heap_end;
/* Initialize heap end at first call */
if (NULL == __sbrk_heap_end)
{
__sbrk_heap_end = &_end;
}
/* Protect heap from growing into the reserved MSP stack */
if (__sbrk_heap_end + incr > max_heap)
{
errno = ENOMEM;
return (void *)-1;
}
prev_heap_end = __sbrk_heap_end;
__sbrk_heap_end += incr;
return (void *)prev_heap_end;
}

View File

@@ -0,0 +1,406 @@
/**
******************************************************************************
* @file system_stm32f1xx.c
* @author MCD Application Team
* @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
*
* 1. This file provides two functions and one global variable to be called from
* user application:
* - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
* factors, AHB/APBx prescalers and Flash settings).
* This function is called at startup just after reset and
* before branch to main program. This call is made inside
* the "startup_stm32f1xx_xx.s" file.
*
* - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
* by the user application to setup the SysTick
* timer or configure other parameters.
*
* - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
* be called whenever the core clock is changed
* during program execution.
*
* 2. After each device reset the HSI (8 MHz) is used as system clock source.
* Then SystemInit() function is called, in "startup_stm32f1xx_xx.s" file, to
* configure the system clock before to branch to main program.
*
* 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depending on
* the product used), refer to "HSE_VALUE".
* When HSE is used as system clock source, directly or through PLL, and you
* are using different crystal you have to adapt the HSE value to your own
* configuration.
*
******************************************************************************
* @attention
*
* Copyright (c) 2017-2021 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.
*
******************************************************************************
*/
/** @addtogroup CMSIS
* @{
*/
/** @addtogroup stm32f1xx_system
* @{
*/
/** @addtogroup STM32F1xx_System_Private_Includes
* @{
*/
#include "stm32f1xx.h"
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_Defines
* @{
*/
#if !defined (HSE_VALUE)
#define HSE_VALUE 8000000U /*!< Default value of the External oscillator in Hz.
This value can be provided and adapted by the user application. */
#endif /* HSE_VALUE */
#if !defined (HSI_VALUE)
#define HSI_VALUE 8000000U /*!< Default value of the Internal oscillator in Hz.
This value can be provided and adapted by the user application. */
#endif /* HSI_VALUE */
/*!< Uncomment the following line if you need to use external SRAM */
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
/* #define DATA_IN_ExtSRAM */
#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
/* Note: Following vector table addresses must be defined in line with linker
configuration. */
/*!< Uncomment the following line if you need to relocate the vector table
anywhere in Flash or Sram, else the vector table is kept at the automatic
remap of boot address selected */
/* #define USER_VECT_TAB_ADDRESS */
#if defined(USER_VECT_TAB_ADDRESS)
/*!< Uncomment the following line if you need to relocate your vector Table
in Sram else user remap will be done in Flash. */
/* #define VECT_TAB_SRAM */
#if defined(VECT_TAB_SRAM)
#define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field.
This value must be a multiple of 0x200. */
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
This value must be a multiple of 0x200. */
#else
#define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field.
This value must be a multiple of 0x200. */
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
This value must be a multiple of 0x200. */
#endif /* VECT_TAB_SRAM */
#endif /* USER_VECT_TAB_ADDRESS */
/******************************************************************************/
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_Macros
* @{
*/
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_Variables
* @{
*/
/* This variable is updated in three ways:
1) by calling CMSIS function SystemCoreClockUpdate()
2) by calling HAL API function HAL_RCC_GetHCLKFreq()
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
Note: If you use this function to configure the system clock; then there
is no need to call the 2 first functions listed above, since SystemCoreClock
variable is updated automatically.
*/
uint32_t SystemCoreClock = 8000000;
const uint8_t AHBPrescTable[16U] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
const uint8_t APBPrescTable[8U] = {0, 0, 0, 0, 1, 2, 3, 4};
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_FunctionPrototypes
* @{
*/
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
#ifdef DATA_IN_ExtSRAM
static void SystemInit_ExtMemCtl(void);
#endif /* DATA_IN_ExtSRAM */
#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
/**
* @}
*/
/** @addtogroup STM32F1xx_System_Private_Functions
* @{
*/
/**
* @brief Setup the microcontroller system
* Initialize the Embedded Flash Interface, the PLL and update the
* SystemCoreClock variable.
* @note This function should be used only after reset.
* @param None
* @retval None
*/
void SystemInit (void)
{
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
#ifdef DATA_IN_ExtSRAM
SystemInit_ExtMemCtl();
#endif /* DATA_IN_ExtSRAM */
#endif
/* Configure the Vector Table location -------------------------------------*/
#if defined(USER_VECT_TAB_ADDRESS)
SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
#endif /* USER_VECT_TAB_ADDRESS */
}
/**
* @brief Update SystemCoreClock variable according to Clock Register Values.
* The SystemCoreClock variable contains the core clock (HCLK), it can
* be used by the user application to setup the SysTick timer or configure
* other parameters.
*
* @note Each time the core clock (HCLK) changes, this function must be called
* to update SystemCoreClock variable value. Otherwise, any configuration
* based on this variable will be incorrect.
*
* @note - The system frequency computed by this function is not the real
* frequency in the chip. It is calculated based on the predefined
* constant and the selected clock source:
*
* - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
*
* - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
*
* - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
* or HSI_VALUE(*) multiplied by the PLL factors.
*
* (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value
* 8 MHz) but the real value may vary depending on the variations
* in voltage and temperature.
*
* (**) HSE_VALUE is a constant defined in stm32f1xx.h file (default value
* 8 MHz or 25 MHz, depending on the product used), user has to ensure
* that HSE_VALUE is same as the real frequency of the crystal used.
* Otherwise, this function may have wrong result.
*
* - The result of this function could be not correct when using fractional
* value for HSE crystal.
* @param None
* @retval None
*/
void SystemCoreClockUpdate (void)
{
uint32_t tmp = 0U, pllmull = 0U, pllsource = 0U;
#if defined(STM32F105xC) || defined(STM32F107xC)
uint32_t prediv1source = 0U, prediv1factor = 0U, prediv2factor = 0U, pll2mull = 0U;
#endif /* STM32F105xC */
#if defined(STM32F100xB) || defined(STM32F100xE)
uint32_t prediv1factor = 0U;
#endif /* STM32F100xB or STM32F100xE */
/* Get SYSCLK source -------------------------------------------------------*/
tmp = RCC->CFGR & RCC_CFGR_SWS;
switch (tmp)
{
case 0x00U: /* HSI used as system clock */
SystemCoreClock = HSI_VALUE;
break;
case 0x04U: /* HSE used as system clock */
SystemCoreClock = HSE_VALUE;
break;
case 0x08U: /* PLL used as system clock */
/* Get PLL clock source and multiplication factor ----------------------*/
pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
#if !defined(STM32F105xC) && !defined(STM32F107xC)
pllmull = ( pllmull >> 18U) + 2U;
if (pllsource == 0x00U)
{
/* HSI oscillator clock divided by 2 selected as PLL clock entry */
SystemCoreClock = (HSI_VALUE >> 1U) * pllmull;
}
else
{
#if defined(STM32F100xB) || defined(STM32F100xE)
prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U;
/* HSE oscillator clock selected as PREDIV1 clock entry */
SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
#else
/* HSE selected as PLL clock entry */
if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
{/* HSE oscillator clock divided by 2 */
SystemCoreClock = (HSE_VALUE >> 1U) * pllmull;
}
else
{
SystemCoreClock = HSE_VALUE * pllmull;
}
#endif
}
#else
pllmull = pllmull >> 18U;
if (pllmull != 0x0DU)
{
pllmull += 2U;
}
else
{ /* PLL multiplication factor = PLL input clock * 6.5 */
pllmull = 13U / 2U;
}
if (pllsource == 0x00U)
{
/* HSI oscillator clock divided by 2 selected as PLL clock entry */
SystemCoreClock = (HSI_VALUE >> 1U) * pllmull;
}
else
{/* PREDIV1 selected as PLL clock entry */
/* Get PREDIV1 clock source and division factor */
prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U;
if (prediv1source == 0U)
{
/* HSE oscillator clock selected as PREDIV1 clock entry */
SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
}
else
{/* PLL2 clock selected as PREDIV1 clock entry */
/* Get PREDIV2 division factor and PLL2 multiplication factor */
prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4U) + 1U;
pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8U) + 2U;
SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
}
}
#endif /* STM32F105xC */
break;
default:
SystemCoreClock = HSI_VALUE;
break;
}
/* Compute HCLK clock frequency ----------------*/
/* Get HCLK prescaler */
tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4U)];
/* HCLK clock frequency */
SystemCoreClock >>= tmp;
}
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
/**
* @brief Setup the external memory controller. Called in startup_stm32f1xx.s
* before jump to __main
* @param None
* @retval None
*/
#ifdef DATA_IN_ExtSRAM
/**
* @brief Setup the external memory controller.
* Called in startup_stm32f1xx_xx.s/.c before jump to main.
* This function configures the external SRAM mounted on STM3210E-EVAL
* board (STM32 High density devices). This SRAM will be used as program
* data memory (including heap and stack).
* @param None
* @retval None
*/
void SystemInit_ExtMemCtl(void)
{
__IO uint32_t tmpreg;
/*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is
required, then adjust the Register Addresses */
/* Enable FSMC clock */
RCC->AHBENR = 0x00000114U;
/* Delay after an RCC peripheral clock enabling */
tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_FSMCEN);
/* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
RCC->APB2ENR = 0x000001E0U;
/* Delay after an RCC peripheral clock enabling */
tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPDEN);
(void)(tmpreg);
/* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/
/*---------------- SRAM Address lines configuration -------------------------*/
/*---------------- NOE and NWE configuration --------------------------------*/
/*---------------- NE3 configuration ----------------------------------------*/
/*---------------- NBL0, NBL1 configuration ---------------------------------*/
GPIOD->CRL = 0x44BB44BBU;
GPIOD->CRH = 0xBBBBBBBBU;
GPIOE->CRL = 0xB44444BBU;
GPIOE->CRH = 0xBBBBBBBBU;
GPIOF->CRL = 0x44BBBBBBU;
GPIOF->CRH = 0xBBBB4444U;
GPIOG->CRL = 0x44BBBBBBU;
GPIOG->CRH = 0x444B4B44U;
/*---------------- FSMC Configuration ---------------------------------------*/
/*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/
FSMC_Bank1->BTCR[4U] = 0x00001091U;
FSMC_Bank1->BTCR[5U] = 0x00110212U;
}
#endif /* DATA_IN_ExtSRAM */
#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,95 @@
#include "xinput_app.h"
#include "main.h"
#include "usb_device.h"
#include "usbd_xinput.h"
typedef struct __attribute__((packed))
{
uint8_t reportId;
uint8_t reportSize;
uint8_t buttons1;
uint8_t buttons2;
uint8_t leftTrigger;
uint8_t rightTrigger;
int16_t leftX;
int16_t leftY;
int16_t rightX;
int16_t rightY;
uint8_t reserved[6];
} XInputReport;
_Static_assert(sizeof(XInputReport) == XINPUT_REPORT_SIZE,
"XInput input report must be exactly 20 bytes");
extern USBD_HandleTypeDef hUsbDeviceFS;
static uint8_t IsPressed(GPIO_TypeDef *port, uint16_t pin)
{
return (HAL_GPIO_ReadPin(port, pin) == GPIO_PIN_RESET) ? 1U : 0U;
}
void XInput_AppProcess(void)
{
XInputReport report = {0};
report.reportId = 0x00U;
report.reportSize = sizeof(report);
if (IsPressed(UP_GPIO_Port, UP_Pin))
{
report.buttons1 |= (1U << 0);
}
if (IsPressed(DOWN_GPIO_Port, DOWN_Pin))
{
report.buttons1 |= (1U << 1);
}
if (IsPressed(LEFT_GPIO_Port, LEFT_Pin))
{
report.buttons1 |= (1U << 2);
}
if (IsPressed(RIGHT_GPIO_Port, RIGHT_Pin))
{
report.buttons1 |= (1U << 3);
}
if (IsPressed(Start_GPIO_Port, Start_Pin))
{
report.buttons1 |= (1U << 4);
}
if (IsPressed(Select_GPIO_Port, Select_Pin))
{
report.buttons1 |= (1U << 5);
}
if (IsPressed(LB_GPIO_Port, LB_Pin))
{
report.buttons2 |= (1U << 0);
}
if (IsPressed(RB_GPIO_Port, RB_Pin))
{
report.buttons2 |= (1U << 1);
}
if (IsPressed(A_GPIO_Port, A_Pin))
{
report.buttons2 |= (1U << 4);
}
if (IsPressed(B_GPIO_Port, B_Pin))
{
report.buttons2 |= (1U << 5);
}
if (IsPressed(X_GPIO_Port, X_Pin))
{
report.buttons2 |= (1U << 6);
}
if (IsPressed(Y_GPIO_Port, Y_Pin))
{
report.buttons2 |= (1U << 7);
}
report.leftTrigger = IsPressed(LT_GPIO_Port, LT_Pin) ? 0xFFU : 0x00U;
report.rightTrigger = IsPressed(RT_GPIO_Port, RT_Pin) ? 0xFFU : 0x00U;
(void)USBD_XINPUT_SendReport(&hUsbDeviceFS,
(uint8_t *)&report,
sizeof(report));
}