/** * @brief USB recognized as a standard mouse * cursor moving according to discovery moving * @param None * @retval None */ void USB_Demo(void) { uint8_t *buf; STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOff(LED6); while ((STM_EVAL_PBGetState(BUTTON_USER) != Bit_SET)) { buf = USBD_HID_GetPos(); if((buf[1] != 0) ||(buf[2] != 0)) { USBD_HID_SendReport (&USB_Device_dev, buf, 4); /* Insert 50ms delay */ Delay (5); } } /* Wait for User button is released */ while (STM_EVAL_PBGetState(BUTTON_USER) != Bit_RESET) {} /* Turn Off Leds */ STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED4); STM_EVAL_LEDOff(LED5); STM_EVAL_LEDOff(LED6); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { uint8_t i = 0; /* SysTick end of count event each 10ms */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); /* Configure the USB */ USB_Config(); /* Accelerometer Configuration */ Acc_Config(); /* Init push buttons */ STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_GPIO); /* Init LEDs */ STM_EVAL_LEDInit(LED3); /* Infinite loop */ while (1) { /* Wait for data ready */ while (DataReady != 0x04) { } DataReady = 0x00; /* Get Data Accelerometer */ Acc_ReadData(AccBuffer); for (i = 0; i < 3; i++) AccBuffer[i] /= 100.0f; // Get mouse info Mouse_Buffer = USBD_HID_GetPos(); /* Update the cursor position */ if (!mouseZeroPacketOutputted || (Mouse_Buffer[0] != 0 || Mouse_Buffer[1] != 0 || Mouse_Buffer[2] != 0 || Mouse_Buffer[3] != 0)) { /* Reset the control token to inform upper layer that a transfer is ongoing */ PrevXferComplete = 0; /* Copy mouse position info in ENDP1 Tx Packet Memory Area*/ USB_SIL_Write(EP1_IN, Mouse_Buffer, 4); /* Enable endpoint for transmission */ SetEPTxValid(ENDP1 ); } // We find that the mouse buffer is zero so we know that we have outputted the zero packet // This means we can stop transferring data if((Mouse_Buffer[0] == 0 && Mouse_Buffer[1] == 0 && Mouse_Buffer[2] == 0 && Mouse_Buffer[3] == 0)) mouseZeroPacketOutputted = 1; else mouseZeroPacketOutputted = 0; } }
/** * @brief This function handles SysTick Handler. * @param None * @retval None */ void SysTick_Handler(void) { uint8_t *buf; buf = USBD_HID_GetPos(); if((buf[1] != 0) ||(buf[2] != 0)) { USBD_HID_MSC_SendHidReport (&USB_OTG_dev, buf, 4); } }
void SysTick_Handler(void) { static uint8_t * buf; buf = USBD_HID_GetPos(); // 获取按键值,存入HID_Buffer if((buf[1] != 0) ||(buf[2] != 0)) { USBD_HID_SendReport (&USB_OTG_dev, buf, 64); printf("buf addr: %d : %d %d %d %d \r\n",(uint32_t)buf, buf[0],buf[1],buf[2],buf[3]); } }
/** * @brief This function sends the input report repeatedly. * @param None * @retval None */ void SysTick_Handler(void) { uint8_t *buf; /* Get Joystick position */ buf = USBD_HID_GetPos(); /* Update the cursor position */ if((buf[1] != 0) ||(buf[2] != 0)) { /* Send Report */ USBD_HID_SendReport (&USB_Device_dev, buf, 4); } }
/** * @brief Main program. * @param None * @retval None */ int main(void) { uint8_t i = 0; /* SysTick end of count event each 10ms */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); /* Configure the USB */ USB_Config(); /* Accelerometer Configuration */ Acc_Config(); /* Infinite loop */ while (1) { /* Wait for data ready */ while(DataReady !=0x02) {} DataReady = 0x00; /* Get Data Accelerometer */ Acc_ReadData(AccBuffer); for(i=0;i<3;i++) AccBuffer[i] /= 100.0f; /* Get position */ Mouse_Buffer = USBD_HID_GetPos(); /* Update the cursor position */ if((Mouse_Buffer[1] != 0) ||(Mouse_Buffer[2] != 0)) { /* Reset the control token to inform upper layer that a transfer is ongoing */ PrevXferComplete = 0; /* Copy mouse position info in ENDP1 Tx Packet Memory Area*/ USB_SIL_Write(EP1_IN, Mouse_Buffer, 4); /* Enable endpoint for transmission */ SetEPTxValid(ENDP1); } } }
/** * @brief SYSTICK callback. * @param None * @retval None */ void HAL_SYSTICK_Callback(void) { uint8_t *buf; uint16_t Temp_X, Temp_Y = 0x00; uint16_t NewARR_X, NewARR_Y = 0x00; if (DemoEnterCondition != 0x00) { buf = USBD_HID_GetPos(); if((buf[1] != 0) ||(buf[2] != 0)) { USBD_HID_SendReport (&hUSBDDevice, buf, 4); } Counter ++; if (Counter == 10) { /* Reset Buffer used to get accelerometer values */ Buffer[0] = 0; Buffer[1] = 0; /* Disable All TIM4 Capture Compare Channels */ HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_1); HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_2); HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3); HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4); /* Read Acceleration*/ BSP_ACCELERO_GetXYZ(Buffer); /* Set X and Y positions */ X_Offset = Buffer[0]; Y_Offset = Buffer[1]; /* Update New autoreload value in case of X or Y acceleration*/ /* Basic acceleration X_Offset and Y_Offset are divide by 40 to fir with ARR range */ NewARR_X = TIM_ARR - ABS(X_Offset/40); NewARR_Y = TIM_ARR - ABS(Y_Offset/40); /* Calculation of Max acceleration detected on X or Y axis */ Temp_X = ABS(X_Offset/40); Temp_Y = ABS(Y_Offset/40); MaxAcceleration = MAX_AB(Temp_X, Temp_Y); if(MaxAcceleration != 0) { /* Reset CNT to a lowest value (equal to min CCRx of all Channels) */ __HAL_TIM_SET_COUNTER(&htim4,(TIM_ARR-MaxAcceleration)/2); if (X_Offset < ThreadholdAcceleroLow) { /* Sets the TIM4 Capture Compare for Channel1 Register value */ /* Equal to NewARR_X/2 to have duty cycle equal to 50% */ __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, NewARR_X/2); /* Time base configuration */ __HAL_TIM_SET_AUTORELOAD(&htim4, NewARR_X); /* Enable TIM4 Capture Compare Channel1 */ HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1); } else if (X_Offset > ThreadholdAcceleroHigh) { /* Sets the TIM4 Capture Compare for Channel3 Register value */ /* Equal to NewARR_X/2 to have duty cycle equal to 50% */ __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3, NewARR_X/2); /* Time base configuration */ __HAL_TIM_SET_AUTORELOAD(&htim4, NewARR_X); /* Enable TIM4 Capture Compare Channel3 */ HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3); } if (Y_Offset > ThreadholdAcceleroHigh) { /* Sets the TIM4 Capture Compare for Channel2 Register value */ /* Equal to NewARR_Y/2 to have duty cycle equal to 50% */ __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2,NewARR_Y/2); /* Time base configuration */ __HAL_TIM_SET_AUTORELOAD(&htim4, NewARR_Y); /* Enable TIM4 Capture Compare Channel2 */ HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2); } else if (Y_Offset < ThreadholdAcceleroLow) { /* Sets the TIM4 Capture Compare for Channel4 Register value */ /* Equal to NewARR_Y/2 to have duty cycle equal to 50% */ __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_4, NewARR_Y/2); /* Time base configuration */ __HAL_TIM_SET_AUTORELOAD(&htim4, NewARR_Y); /* Enable TIM4 Capture Compare Channel4 */ HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4); } } Counter = 0x00; } } }
/** * @brief This function handles SysTick Handler. * @param None * @retval None */ void SysTick_Handler(void) { uint8_t *buf; uint8_t temp1, temp2 = 0x00; if (DemoEnterCondition == 0x00) { TimingDelay_Decrement(); } else { buf = USBD_HID_GetPos(); if((buf[1] != 0) ||(buf[2] != 0)) { USBD_HID_SendReport (&USB_OTG_dev, buf, 4); } Counter ++; if (Counter == 10) { Buffer[0] = 0; Buffer[2] = 0; /* Disable All TIM4 Capture Compare Channels */ TIM_CCxCmd(TIM4, TIM_Channel_1, DISABLE); TIM_CCxCmd(TIM4, TIM_Channel_2, DISABLE); TIM_CCxCmd(TIM4, TIM_Channel_3, DISABLE); TIM_CCxCmd(TIM4, TIM_Channel_4, DISABLE); LIS302DL_Read(Buffer, LIS302DL_OUT_X_ADDR, 6); /* Remove the offsets values from data */ Buffer[0] -= X_Offset; Buffer[2] -= Y_Offset; /* Update autoreload and capture compare registers value*/ temp1 = ABS((int8_t)(Buffer[0])); temp2 = ABS((int8_t)(Buffer[2])); TempAcceleration = MAX(temp1, temp2); if(TempAcceleration != 0) { if ((int8_t)Buffer[0] < -2) { /* Enable TIM4 Capture Compare Channel 4 */ TIM_CCxCmd(TIM4, TIM_Channel_4, ENABLE); /* Sets the TIM4 Capture Compare4 Register value */ TIM_SetCompare4(TIM4, TIM_CCR/TempAcceleration); } if ((int8_t)Buffer[0] > 2) { /* Enable TIM4 Capture Compare Channel 2 */ TIM_CCxCmd(TIM4, TIM_Channel_2, ENABLE); /* Sets the TIM4 Capture Compare2 Register value */ TIM_SetCompare2(TIM4, TIM_CCR/TempAcceleration); } if ((int8_t)Buffer[2] > 2) { /* Enable TIM4 Capture Compare Channel 1 */ TIM_CCxCmd(TIM4, TIM_Channel_1, ENABLE); /* Sets the TIM4 Capture Compare1 Register value */ TIM_SetCompare1(TIM4, TIM_CCR/TempAcceleration); } if ((int8_t)Buffer[2] < -2) { /* Enable TIM4 Capture Compare Channel 3 */ TIM_CCxCmd(TIM4, TIM_Channel_3, ENABLE); /* Sets the TIM4 Capture Compare3 Register value */ TIM_SetCompare3(TIM4, TIM_CCR/TempAcceleration); } /* Time base configuration */ TIM_SetAutoreload(TIM4, TIM_ARR/TempAcceleration); } Counter = 0x00; } } }