Skip to content

Commit

Permalink
migrated to tinyusb library
Browse files Browse the repository at this point in the history
  • Loading branch information
z4yx committed Jun 2, 2024
1 parent 61b83c7 commit 89f069f
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 836 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ set(CMAKE_ASM_FLAGS "-mcpu=${CMAKE_SYSTEM_PROCESSOR} -mthumb -Wall -fdata-sectio
set(CMAKE_EXE_LINKER_FLAGS "-specs=nano.specs -T${MCU_LINKER_SCRIPT} -Wl,-Map=layout.map,--cref -Wl,--gc-sections ${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_C_FLAGS_RELEASE "-Os -g3")

set(TINYUSB_MCU "OPT_MCU_STM32L4" CACHE STRING "TinyUSB MCU name")
#set(TINYUSB_DCD_INCLUDES ${DCD_INCLUDE_DIRS} CACHE STRING "TinyUSB DCD include paths")
add_definitions(-DMBEDTLS_CONFIG_FILE="${CMAKE_SOURCE_DIR}/Inc/mbedtls-config.h")
add_definitions(-DDEVICE_CONFIG_FILE="${CMAKE_SOURCE_DIR}/Inc/device-config.h")
add_subdirectory(canokey-core)

add_definitions(-DCFG_TUSB_MCU=${TINYUSB_MCU})
set(TINYUSB_DCD_SRC canokey-core/tinyusb/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c)
aux_source_directory(Src SOURCE_CODE_COMMON)
aux_source_directory(Drivers/STM32L4xx_HAL_Driver/Src SOURCE_CODE_BOARD)
add_executable(canokey ${SOURCE_CODE_COMMON} ${SOURCE_CODE_BOARD} ${STARTUP_ASM_FILE})
add_executable(canokey ${SOURCE_CODE_COMMON} ${SOURCE_CODE_BOARD} ${TINYUSB_DCD_SRC} ${STARTUP_ASM_FILE})
target_link_libraries(canokey PRIVATE canokey-core c m nosys)

target_compile_definitions(canokey PRIVATE USE_HAL_DRIVER USE_FULL_LL_DRIVER STM32L432xx)
Expand Down
2 changes: 1 addition & 1 deletion Inc/stm32l4xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
/*#define HAL_OPAMP_MODULE_ENABLED */
/*#define HAL_OSPI_MODULE_ENABLED */
/*#define HAL_OSPI_MODULE_ENABLED */
#define HAL_PCD_MODULE_ENABLED
/*#define HAL_PCD_MODULE_ENABLED */
/*#define HAL_PKA_MODULE_ENABLED */
/*#define HAL_QSPI_MODULE_ENABLED */
/*#define HAL_QSPI_MODULE_ENABLED */
Expand Down
23 changes: 0 additions & 23 deletions Src/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,29 +218,6 @@ uint8_t stm32_hw_variant_probe(void) {
return result;
}

/* Override the function defined in usb_device.c */
void usb_resources_alloc(void) {
uint8_t iface = 0;
uint8_t ep = 1;

memset(&IFACE_TABLE, 0xFF, sizeof(IFACE_TABLE));
memset(&EP_TABLE, 0xFF, sizeof(EP_TABLE));

EP_TABLE.ctap_hid = ep++;
IFACE_TABLE.ctap_hid = iface++;
EP_SIZE_TABLE.ctap_hid = 64;

IFACE_TABLE.webusb = iface++;

EP_TABLE.ccid = ep++;
IFACE_TABLE.ccid = iface++;
EP_SIZE_TABLE.ccid = 64;

EP_TABLE.kbd_hid = ep;
IFACE_TABLE.kbd_hid = iface;
EP_SIZE_TABLE.kbd_hid = 8;
}

int device_atomic_compare_and_swap(volatile uint32_t *var, uint32_t expect, uint32_t update) {
int status = 0;
do {
Expand Down
17 changes: 8 additions & 9 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */
extern uint32_t _stack_boundary;
uint8_t device_loop_enable, usb_init_done;
uint8_t device_loop_enable;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
Expand Down Expand Up @@ -270,6 +270,12 @@ static void config_usb_mode(void) {
LL_SPI_SetBaudRatePrescaler(hspi1.Instance, LL_SPI_BAUDRATEPRESCALER_DIV8);
MX_USART2_UART_Init();

/* Enable USB power on Pwrctrl CR2 register. */
HAL_PWREx_EnableVddUSB();
/* Peripheral clock enable */
__HAL_RCC_USB_CLK_ENABLE();
/* Peripheral interrupt init */
HAL_NVIC_SetPriority(USB_IRQn, 1, 0);
usb_device_init();
// enable the device_periodic_task, which controls LED and Touch sensing
device_loop_enable = 1;
Expand All @@ -282,12 +288,6 @@ static int check_is_nfc_en(void) {
return val == 0xFFFFFFFFU || // ST production default value
val == 0xFFFF802a; // magic written by admin_vendor_nfc_enable()
}

// Called by core library
void USBD_LL_Init_Done(void)
{
usb_init_done = 1;
}
/* USER CODE END 0 */

/**
Expand Down Expand Up @@ -361,8 +361,7 @@ int main(void) {
DBG_MSG("Touch calibrating...\n");
GPIO_Touch_Calibrate();
}
if (usb_init_done)
device_loop(1);
device_loop(1);
++i;
}
}
Expand Down
6 changes: 3 additions & 3 deletions Src/stm32l4xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "device-stm32.h"
#include "tusb.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -57,7 +58,6 @@
/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern PCD_HandleTypeDef hpcd_USB_FS;
extern TIM_HandleTypeDef htim6;
/* USER CODE BEGIN EV */
extern uint32_t device_loop_enable;
Expand Down Expand Up @@ -254,9 +254,9 @@ void TIM6_DAC_IRQHandler(void)
void USB_IRQHandler(void)
{
/* USER CODE BEGIN USB_IRQn 0 */

tud_int_handler(0);
return;
/* USER CODE END USB_IRQn 0 */
HAL_PCD_IRQHandler(&hpcd_USB_FS);
/* USER CODE BEGIN USB_IRQn 1 */

/* USER CODE END USB_IRQn 1 */
Expand Down
27 changes: 27 additions & 0 deletions Src/usb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <admin.h>

#include <string.h>
#include <usb_device.h>

/* Override the function defined in usb_device.c */
void usb_resources_alloc(void) {
uint8_t iface = 0;
uint8_t ep = 1;

memset(&IFACE_TABLE, 0xFF, sizeof(IFACE_TABLE));
memset(&EP_TABLE, 0xFF, sizeof(EP_TABLE));

EP_TABLE.ctap_hid = ep++;
IFACE_TABLE.ctap_hid = iface++;

IFACE_TABLE.webusb = iface++;

EP_TABLE.ccid = ep++;
IFACE_TABLE.ccid = iface++;

//if (cfg_is_kbd_interface_enable()) {
EP_TABLE.kbd_hid = ep;
IFACE_TABLE.kbd_hid = iface;
//}

}
Loading

0 comments on commit 89f069f

Please sign in to comment.