2016-12-23 12:31 AM
Hi everyone
This is my first question in this forum, I hope you will not be so agressive with me I am new with microcontrollers.
I am using STM32F407VG discovery as a USB HOST, I connected it to a lazer scanner which reads barcodes, the scanner is considered as a keyboard.
The connection works just fine but I have some issues that I can't define their causes.
First, I can't connect the scanner directly to STM, I need to connect a mouse first, wait until STM recognize the mouse then I disconnect it and connect the scanner, I don't know why I need to do that but this is the only way to get the scanner connected.
The second issue is that devices take a long time to get connected to STM and some times they don't connect at all, so some times i resolve the problem by take off the supply from STM and putted again, some times i need to run the program again and also I don't know why I need to do that.
So you can imagine if I disconnect the scanner or the STM get off power I need at least 10 minutes to get the connection again.
I did a simple program without interrups,RTOS or using DMA register just I configured the Timer, GPIOs, the USB, I think I have all the files that I need on the library, I don't know the cause of this problem.
This is the code:
&sharpinclude 'defines.h'
&sharpinclude 'tm_stm32f4_usb_hid_host.h'&sharpinclude 'stm32f4xx.h'&sharpinclude 'stm32f4xx_rcc.h'&sharpinclude 'stm32f4xx_gpio.h'&sharpinclude 'stm32f4xx_tim.h'&sharpinclude 'misc.h'&sharpinclude <stdio.h>
uint16_t PrescalerValue, TimPeriod;int i;GPIO_InitTypeDef GPIO_InitStructure;TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;TIM_OCInitTypeDef TIM_OCInitStructure;uint16_t PrescalerValue ;
uint16_t TimPeriod ; void Timer4_Config(void) {RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
// Te=1s// TimPeriod*(PrescalerValue+1)/F(timer4)=Te; // F(timer4)=84Mhz PrescalerValue=8399; TimPeriod=10000; TIM_TimeBaseStructure.TIM_Period = TimPeriod ; TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); TIM_Cmd(TIM4 , ENABLE);}
void GPIO_Config(void)
{RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE );
//Configure LD12---PD15 in output pushpull mode GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;//LD2...LD15 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//Mode Out GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//Fmax=50 MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//Output Push Pull GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//No Pull//GPIOd Periph initialisation GPIO_Init(GPIOD,&GPIO_InitStructure);//Connecter chaque pin au TIM4 en mode AF //void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF); }int main(void) { TM_USB_HIDHOST_Result_t USB_HID_Status; /* USB HID Host status */ TM_USB_HIDHOST_Keyboard_t tableau [18] ; /* Initialize system */ SystemInit(); GPIO_Config();//Configuration du GPIO Timer4_Config();//Configuration du Timer 3 /* Init USB HID */ TM_USB_HIDHOST_Init(); /* Get connected device */ USB_HID_Status = TM_USB_HIDHOST_Device();while (1)
{ TM_USB_HIDHOST_Process(); USB_HID_Status = TM_USB_HIDHOST_Device(); if ( USB_HID_Status==TM_USB_HIDHOST_Result_KeyboardConnected) {for (i=0;i<17;i++)
{ TM_USB_HIDHOST_ReadKeyboard(&tableau[i]); if (tableau[i].ButtonStatus == TM_USB_HIDHOST_Button_Pressed) { if (tableau[i].Button == '5') { if ( tableau[i+1].Button == '1') { GPIO_ResetBits( GPIOD, GPIO_Pin_14); GPIO_ResetBits( GPIOD, GPIO_Pin_13); GPIO_ResetBits( GPIOD, GPIO_Pin_12); GPIO_SetBits( GPIOD, GPIO_Pin_15);}
if (tableau[i+1].Button == '0')
{ GPIO_ResetBits( GPIOD, GPIO_Pin_12); GPIO_ResetBits( GPIOD, GPIO_Pin_13); GPIO_ResetBits( GPIOD, GPIO_Pin_15); GPIO_SetBits( GPIOD, GPIO_Pin_14); } } if (tableau[i].Button == '4') { if (tableau[i+1].Button == '1') { GPIO_ResetBits( GPIOD, GPIO_Pin_12); GPIO_ResetBits( GPIOD, GPIO_Pin_14); GPIO_ResetBits( GPIOD, GPIO_Pin_15); GPIO_SetBits( GPIOD, GPIO_Pin_13);}
if (tableau[i+1].Button == '0')
{ GPIO_ResetBits( GPIOD, GPIO_Pin_15); GPIO_ResetBits( GPIOD, GPIO_Pin_13); GPIO_ResetBits( GPIOD, GPIO_Pin_14); GPIO_SetBits( GPIOD, GPIO_Pin_12);}
} if (USB_HID_Status==TM_USB_HIDHOST_Result_MouseConnected) { for(i=0;i<4200000;i++) GPIO_SetBits( GPIOD, GPIO_Pin_15); for(i=0;i<4200000;i++) GPIO_ResetBits( GPIOD, GPIO_Pin_15); } if (USB_HID_Status==TM_USB_HIDHOST_Result_Disconnected) { for(i=0;i<4200000;i++) GPIO_SetBits( GPIOD, GPIO_Pin_12); for(i=0;i<4200000;i++) GPIO_ResetBits( GPIOD, GPIO_Pin_12); }} }Thank you for helping me.
#usb-otg #keyboard #usb-hid #stm32-usb #usb-host