USB drivers with FIDO2 HID interface demo

This commit is contained in:
Conor Patrick
2018-10-23 22:56:12 -04:00
parent 5b858af27b
commit cdac84c93f
36 changed files with 19271 additions and 36 deletions

View File

@@ -7,4 +7,6 @@
void hw_init(void);
#endif

View File

@@ -1,5 +1,6 @@
#include "device.h"
#include "usbd_def.h"
uint32_t __65_seconds = 0;
void TIM6_DAC_IRQHandler()
@@ -9,6 +10,13 @@ void TIM6_DAC_IRQHandler()
__65_seconds += 1;
}
extern PCD_HandleTypeDef hpcd;
// Global USB interrupt handler
void USB_IRQHandler(void)
{
/*printf("USB int !\r\n");*/
HAL_PCD_IRQHandler(&hpcd);
}
void delay(uint32_t ms)
{
@@ -16,5 +24,3 @@ void delay(uint32_t ms)
while ((millis() - time) < ms)
;
}

View File

@@ -0,0 +1,101 @@
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include "fifo.h"
FIFO_CREATE(hidmsg,100,100)
#if TEST_FIFO
FIFO_CREATE(test,10,100)
void fifo_test()
{
int ret;
uint8_t data[10][100];
uint8_t verif[10][100];
printf("init\r\n");
for (int i = 0; i < 10; i++)
{
memset(data[i],i,100);
}
for (int i = 0; i < 10; i++)
{
ret = fifo_test_add(data[i]);
printf("%d\r\n",i);
if (ret != 0)
{
printf("fifo_test_add fail\r\n");
goto end;
}
}
for (int i = 0; i < 10; i++)
{
ret = fifo_test_take(verif[i]);
printf("%d\r\n",i );
if (ret != 0)
{
printf("fifo_test_take fail\r\n");
goto end;
}
if (memcmp(verif[i], data[i], 100) != 0)
{
printf("fifo_test_take result fail\r\n");
dump_hex(data[i],100);
dump_hex(verif[i],100);
goto end;
}
}
for (int i = 0; i < 10; i++)
{
ret = fifo_test_add(data[i]);
if (ret != 0)
{
printf("fifo_test_add 2 fail\r\n");
goto end;
}
}
ret = fifo_test_add(data[0]);
if (ret == 0)
{
printf("fifo_test_add should have failed\r\n");
goto end;
}
for (int i = 0; i < 10; i++)
{
ret = fifo_test_take(verif[i]);
if (ret != 0)
{
printf("fifo_test_take fail\r\n");
goto end;
}
if (memcmp(verif[i], data[i], 100) != 0)
{
printf("fifo_test_take result fail\r\n");
goto end;
}
}
ret = fifo_test_take(verif[0]);
if (ret == 0)
{
printf("fifo_test_take should have failed\r\n");
goto end;
}
printf("test pass!\r\n");
end:
while(1)
;
}
#endif

View File

@@ -0,0 +1,58 @@
#ifndef _FIFO_H_
#define _FIFO_H_
#include "app.h"
#define TEST_FIFO 0
#define FIFO_CREATE(NAME,LENGTH,BYTES)\
int __##NAME##_WRITE_PTR = 0;\
int __##NAME##_READ_PTR = 0;\
int __##NAME##_SIZE = 0;\
static uint8_t __##NAME##_WRITE_BUF[BYTES * LENGTH];\
\
int fifo_##NAME##_add(uint8_t * c)\
{\
if (__##NAME##_WRITE_PTR != __##NAME##_READ_PTR || !__##NAME##_SIZE)\
{\
memmove(__##NAME##_WRITE_BUF + __##NAME##_WRITE_PTR * BYTES, c, BYTES);\
__##NAME##_WRITE_PTR ++;\
if (__##NAME##_WRITE_PTR >= LENGTH)\
__##NAME##_WRITE_PTR = 0;\
__##NAME##_SIZE++;\
return 0;\
}\
return -1;\
}\
\
int fifo_##NAME##_take(uint8_t * c)\
{\
memmove(c, __##NAME##_WRITE_BUF + __##NAME##_READ_PTR * BYTES, BYTES);\
if (__##NAME##_READ_PTR != __##NAME##_WRITE_PTR || __##NAME##_SIZE)\
{\
__##NAME##_READ_PTR ++;\
if (__##NAME##_READ_PTR >= LENGTH)\
__##NAME##_READ_PTR = 0;\
__##NAME##_SIZE --;\
return 0;\
}\
return -1;\
}\
\
uint32_t fifo_##NAME##_size()\
{\
return (__##NAME##_SIZE);\
}\
#define FIFO_CREATE_H(NAME,LENGTH,BYTES)\
int fifo_##NAME##_add(uint8_t * c);\
int fifo_##NAME##_take(uint8_t * c);\
uint32_t fifo_##NAME##_size();\
FIFO_CREATE_H(hidmsg,10,64)
FIFO_CREATE_H(test,100,100)
void fifo_test();
#endif

View File

@@ -3,6 +3,7 @@
#include "stm32l4xx.h"
#include "stm32l4xx_ll_gpio.h"
#include "stm32l4xx_ll_rcc.h"
#include "stm32l4xx_ll_crs.h"
#include "stm32l4xx_ll_system.h"
#include "stm32l4xx_ll_pwr.h"
#include "stm32l4xx_ll_utils.h"
@@ -12,6 +13,13 @@
#include "stm32l4xx_ll_bus.h"
#include "stm32l4xx_ll_tim.h"
#include "stm32l4xx_ll_rng.h"
#include "stm32l4xx_ll_usb.h"
#include "stm32l4xx_hal_pcd.h"
#include "stm32l4xx_hal.h"
#include "usbd_core.h"
#include "usbd_desc.h"
#include "usbd_hid.h"
/* USER CODE BEGIN Includes */
@@ -19,10 +27,7 @@
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
USBD_HandleTypeDef Solo_USBD_Device;
/* Private function prototypes -----------------------------------------------*/
static void LL_Init(void);
@@ -32,6 +37,7 @@ static void MX_USART1_UART_Init(void);
static void MX_TIM2_Init(void);
static void MX_TIM6_Init(void);
static void MX_RNG_Init(void);
static void usb_init();
#define Error_Handler() _Error_Handler(__FILE__,__LINE__)
void _Error_Handler(char *file, int line);
@@ -44,10 +50,20 @@ void hw_init(void)
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
LL_Init();
// enable power clock
SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_PWREN);
// enable USB power
SET_BIT(PWR->CR2, PWR_CR2_USV);
// Enable USB Clock
SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_USBFSEN);
/* USER CODE BEGIN Init */
/* USER CODE END Init */
@@ -71,10 +87,12 @@ void hw_init(void)
__enable_irq();
NVIC_EnableIRQ(TIM6_IRQn);
usb_init();
}
static void LL_Init(void)
{
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
@@ -110,16 +128,23 @@ void SystemClock_Config(void)
if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2)
{
Error_Handler();
Error_Handler();
}
LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);
LL_RCC_HSI48_Enable();
/* Wait till HSI48 is ready */
while(LL_RCC_HSI48_IsReady() != 1)
{
}
LL_RCC_MSI_Enable();
/* Wait till MSI is ready */
while(LL_RCC_MSI_IsReady() != 1)
{
}
LL_RCC_MSI_EnableRangeSelection();
@@ -132,11 +157,11 @@ void SystemClock_Config(void)
/* Wait till System clock is ready */
while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_MSI)
{
}
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_16);
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_16);
@@ -148,10 +173,37 @@ void SystemClock_Config(void)
LL_RCC_SetUSARTClockSource(LL_RCC_USART1_CLKSOURCE_PCLK2);
LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_HSI48);
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_CRS);
LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_CRS);
LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_CRS);
LL_CRS_SetSyncDivider(LL_CRS_SYNC_DIV_1);
LL_CRS_SetSyncPolarity(LL_CRS_SYNC_POLARITY_RISING);
LL_CRS_SetSyncSignalSource(LL_CRS_SYNC_SOURCE_USB);
LL_CRS_SetReloadCounter(__LL_CRS_CALC_CALCULATE_RELOADVALUE(48000000,1000));
LL_CRS_SetFreqErrorLimit(34);
LL_CRS_SetHSI48SmoothTrimming(32);
/* SysTick_IRQn interrupt configuration */
NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
}
static void usb_init()
{
USBD_Init(&Solo_USBD_Device, &HID_Desc, 0);
USBD_RegisterClass(&Solo_USBD_Device, &USBD_HID);
USBD_Start(&Solo_USBD_Device);
}
/* TIM2 init function */
static void MX_TIM2_Init(void)
{
@@ -207,10 +259,10 @@ static void MX_TIM2_Init(void)
LL_TIM_DisableMasterSlaveMode(TIM2);
/**TIM2 GPIO Configuration
/**TIM2 GPIO Configuration
PA1 ------> TIM2_CH2
PA2 ------> TIM2_CH3
PA3 ------> TIM2_CH4
PA3 ------> TIM2_CH4
*/
GPIO_InitStruct.Pin = LL_GPIO_PIN_1|LL_GPIO_PIN_2|LL_GPIO_PIN_3;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
@@ -234,10 +286,10 @@ static void MX_USART1_UART_Init(void)
/* Peripheral clock enable */
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
/**USART1 GPIO Configuration
/**USART1 GPIO Configuration
PB6 ------> USART1_TX
PB7 ------> USART1_RX
PB7 ------> USART1_RX
*/
GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
@@ -285,7 +337,7 @@ static void MX_TIM6_Init(void)
// 48 MHz sys clock --> 6 MHz timer clock
// 6 MHz / 6000 == 1000 Hz
TIM_InitStruct.Prescaler = 6000;
TIM_InitStruct.Prescaler = 48000;
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
TIM_InitStruct.Autoreload = 0xffff;
LL_TIM_Init(TIM6, &TIM_InitStruct);

View File

@@ -11,23 +11,33 @@
#include "stm32l4xx_ll_gpio.h"
#include "stm32l4xx_ll_usart.h"
#include "stm32l4xx_ll_bus.h"
#include "stm32l4xx_ll_usb.h"
#include "stm32l4xx_hal_pcd.h"
#include "usbd_core.h"
#include "usbd_desc.h"
#include "usbd_hid.h"
/*#include "usbd_hid.h"*/
#include "app.h"
#include "flash.h"
#include "rng.h"
#include "led.h"
#include "device.h"
#include "util.h"
#include "fifo.h"
#define Error_Handler() _Error_Handler(__FILE__,__LINE__)
int main(void)
{
uint8_t str[] = "YouCompleteMe: a code-completion engine for Vim";
uint8_t buf[5000];
uint32_t i = 0;
uint8_t buf[sizeof(str)];
uint32_t i = 100;
int inc = 1;
float ent;
float test = 1235.889944f;
uint8_t hidbuf[HID_PACKET_SIZE];
hw_init();
@@ -49,28 +59,45 @@ int main(void)
ent = rng_test(64 * 1024);
printf("entropy of 64KB from RNG: %.6f\r\n", ent);
printf("test float: %.2f\r\n", test);
// Test PWM + weighting of RGB
led_test_colors();
/*// Test PWM + weighting of RGB*/
/*led_test_colors();*/
uint32_t t0 = millis();
memset(hidbuf,0,sizeof(hidbuf));
while (1)
{
led_rgb(i | (i << 8) | (i << 16));
delay(1000);
printf("%lu: %lu\r\n", i+=50, millis());
if (i > 250 || i ==0)
{
inc *= -1;
}
if (millis() - t0 > 500)
{
t0 = millis();
printf("%lu\r\n", millis());
}
if (fifo_hidmsg_size())
{
printf("got message:\r\n");
fifo_hidmsg_take(hidbuf);
dump_hex(hidbuf, HID_PACKET_SIZE);
}
}
}
void _Error_Handler(char *file, int line)
{
printf("Error: %s: %d\r\n", file, line);
while(1)
{
}
}

View File

@@ -2,8 +2,34 @@
#include "app.h"
int WRITE_PTR = 0;
int READ_PTR = 0;
#define BUF_SIZE 20000
static uint8_t WRITE_BUF[BUF_SIZE];
void add2buf(uint8_t c)
{
WRITE_BUF[WRITE_PTR++] = c;
if (WRITE_PTR >= BUF_SIZE)
WRITE_PTR = 0;
}
uint8_t takebuf()
{
uint8_t c;
c = WRITE_BUF[READ_PTR++];
if (READ_PTR >= BUF_SIZE)
READ_PTR = 0;
return c;
}
uint8_t bufavail()
{
return (READ_PTR < WRITE_PTR);
}
void _putchar(char c)
{
// add2buf(c);
while (! LL_USART_IsActiveFlag_TXE(DEBUG_UART))
;
LL_USART_TransmitData8(DEBUG_UART,c);
@@ -18,4 +44,3 @@ int _write (int fd, const void *buf, long int len)
}
return 0;
}