Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Include core modules */
- #include "stm32f4xx.h"
- /* Include my libraries here */
- #include "defines.h"
- #include "tm_stm32f4_disco.h"
- #include "tm_stm32f4_delay.h"
- #include "tm_stm32f4_usb_hid_host.h"
- #include <stm32f4xx_usart.h>
- #include <stdio.h>
- void init_usart2(void);
- int diff(int16_t *prevBuff, int16_t *nowBuff);
- int main(void) {
- TM_USB_HIDHOST_Result_t USB_HID_Status; /* USB HID Host status */
- TM_USB_HIDHOST_Keyboard_t Keyboard_Data; /* Keyboard handler */
- TM_USB_HIDHOST_Mouse_t Mouse_Data; /* Mouse handler */
- int16_t nowBuff[5];
- int16_t prevBuff[5];
- const int mouseDataSize = 5;
- /* Initialize system */
- SystemInit();
- /* Leds init */
- TM_DISCO_LedInit();
- /* Delay init */
- TM_DELAY_Init();
- /* Init USB HID */
- TM_USB_HIDHOST_Init();
- init_usart2();
- while (1) {
- /* Process USB HID */
- /* This must be called periodically */
- int change_index = 0, transfer_index = 0;
- TM_USB_HIDHOST_Process();
- /* Get connected device */
- USB_HID_Status = TM_USB_HIDHOST_Device();
- /* Switch status */
- switch (USB_HID_Status) {
- /* Keyboard connected */
- case TM_USB_HIDHOST_Result_KeyboardConnected:
- /* GREEN led ON */
- TM_DISCO_LedOn(LED_GREEN);
- /* Get keyboard data */
- TM_USB_HIDHOST_ReadKeyboard(&Keyboard_Data);
- /* Check if keyboard button is pressed */
- if (Keyboard_Data.ButtonStatus == TM_USB_HIDHOST_Button_Pressed) {
- if (Keyboard_Data.Button == 'b') { /* Turn on RED led on 'b' button */
- TM_DISCO_LedOn(LED_RED);
- } else if (Keyboard_Data.Button == 'v') { /* Turn off RED led on 'v' button */
- TM_DISCO_LedOff(LED_RED);
- }
- }
- break;
- /* Mouse connected */
- case TM_USB_HIDHOST_Result_MouseConnected:
- /* RED led ON */
- TM_DISCO_LedOn(LED_RED);
- /* Get mouse data */
- TM_USB_HIDHOST_ReadMouse(&Mouse_Data);
- nowBuff[0] = Mouse_Data.LeftButton;
- nowBuff[1] = Mouse_Data.RightButton;
- nowBuff[2] = Mouse_Data.MiddleButton;
- nowBuff[3] = Mouse_Data.DiffX;
- nowBuff[4] = Mouse_Data.DiffY;
- //check this mouse signals is different from previous
- if( !diff(prevBuff,nowBuff) )
- {
- //Recode now mouse signals.
- for (change_index = 0; change_index<mouseDataSize; change_index++)
- {
- prevBuff[change_index] = nowBuff[change_index];
- }
- //transfer mouse data to usart
- for ( transfer_index=0; transfer_index <mouseDataSize; transfer_index++)
- {
- USART_SendData(USART2, nowBuff[transfer_index]);
- while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
- }
- }
- break;
- /* No device connected */
- case TM_USB_HIDHOST_Result_Disconnected:
- /* Turn off leds */
- TM_DISCO_LedOff(LED_RED | LED_GREEN);
- break;
- /* Device is not supported */
- case TM_USB_HIDHOST_Result_DeviceNotSupported:
- /* Device is not supported */
- /* Toggle leds forever */
- TM_DISCO_LedToggle(LED_RED | LED_GREEN);
- /* Delay */
- Delayms(50);
- break;
- /* Error occurred somewhere */
- case TM_USB_HIDHOST_Result_Error:
- /* Error occurred */
- /* Toggle RED LED forever */
- TM_DISCO_LedToggle(LED_RED);
- /* Delay */
- Delayms(50);
- break;
- /* Library is not initialized */
- case TM_USB_HIDHOST_Result_LibraryNotInitialized:
- /* Library is not initialized */
- /* Toggle GREEN LED */
- TM_DISCO_LedToggle(LED_GREEN);
- /* Delay */
- Delayms(50);
- break;
- }
- }
- }
- void init_usart2(void){
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- /* enable peripheral clock for USART2 */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- /* GPIOA clock enable */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- /* GPIOA Configuration: USART2 TX on PA2 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* Connect USART2 pins to AF2 */
- // TX = PA2
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
- USART_InitStructure.USART_BaudRate = 9600;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Tx;
- USART_Init(USART2, &USART_InitStructure);
- USART_Cmd(USART2, ENABLE); // enable USART2
- }
- int diff(int16_t *prevBuff, int16_t *nowBuff)
- {
- int buffSize = 5, index;
- for ( index = 0 ; index < buffSize ; index++)
- {
- if (prevBuff[index] != nowBuff[index])
- return 0;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement