void MainTask(void) { USB_Init(); USB_EnableIAD(); _AddCDC(); _AddHID(); USB_Start(); BSP_SetLED(0); OS_CREATETASK(&_HIDTCB, "HIDTask", _HIDTask, 200, _aHIDStack); while (1) { char ac[64]; int NumBytesReceived; // // Wait for configuration // while ((USB_GetState() & (USB_STAT_CONFIGURED | USB_STAT_SUSPENDED)) != USB_STAT_CONFIGURED) { BSP_ToggleLED(0); USB_OS_Delay(50); } BSP_SetLED(0); NumBytesReceived = USB_CDC_Receive(&ac[0], sizeof(ac)); if (NumBytesReceived > 0) { USB_CDC_Write(&ac[0], NumBytesReceived); } } }
/** * @brief Configure the usb serial communication. * Blocking function. * @param Void * @return Void */ void usbCommInit( void ) { Set_System(); Set_USBClock(); USB_Interrupts_Config(); USB_Init(); while (USB_GetState() != CONFIGURED) {} // Wait USB is ready usbCommInitPeriodicSending(); }
//------------------------------------------------------------------------------ // \brief Sends the currently selected configuration to the host. // \param pClass Pointer to a class driver instance //------------------------------------------------------------------------------ static void STD_GetConfiguration(S_std_class *pClass) { if (ISSET(USB_GetState(pClass->pUsb), USB_STATE_CONFIGURED)) { pClass->wData = 1; } else { pClass->wData = 0; } USB_Write(pClass->pUsb, 0, &(pClass->wData), 1, 0, 0); }
void MainTask(void) { USB_Init(); _AddMSD(); USB_Start(); while (1) { while ((USB_GetState() & (USB_STAT_CONFIGURED | USB_STAT_SUSPENDED)) != USB_STAT_CONFIGURED) { BSP_ToggleLED(0); USB_OS_Delay(50); } BSP_SetLED(0); USB_MSD_Task(); } }
/********************************************************************* * * _USBTask */ static void _USBTask(void) { _AddMSD(); USB_Start(); while(1) { // // Wait for configuration // while ((USB_GetState() & (USB_STAT_CONFIGURED | USB_STAT_SUSPENDED)) != USB_STAT_CONFIGURED) { BSP_ToggleLED(0); USB_OS_Delay(50); } BSP_SetLED(0); FS_Unmount(""); USB_MSD_Task(); // Task, does only return when device is non-configured mode FS_Mount(""); } }
/********************************************************************* * * _HIDTask * * Function description * Task to make the mouse jump from left to right. */ static void _HIDTask(void) { // // Loop: Receive data byte by byte, send back (data + 1) // while (1) { // // Wait for configuration // while ((USB_GetState() & (USB_STAT_CONFIGURED | USB_STAT_SUSPENDED)) != USB_STAT_CONFIGURED) { BSP_ToggleLED(1); USB_OS_Delay(50); } BSP_SetLED(1); USB_HID_Read(&_aHIDData[0], OUTPUT_REPORT_SIZE); _aHIDData[0]++; USB_HID_Write(&_aHIDData[0], INPUT_REPORT_SIZE); USB_OS_Delay(50); } }
/********************************************************************* * * _USBTask */ static void _USBTask(void) { while(1) { // // Loop: Receive data byte by byte, send back (data + 1) // while (1) { char c; while ((USB_GetState() & (USB_STAT_CONFIGURED | USB_STAT_SUSPENDED)) != USB_STAT_CONFIGURED) { BSP_ToggleLED(0); USB_OS_Delay(50); } BSP_SetLED(0); // LED on to indicate we are waiting for data USB_BULK_Read(&c, 1); BSP_ClrLED(0); c++; USB_BULK_Write(&c, 1); } } }
/**************************************************************************** * Name : USBInterfaceReceiveTask * * * * Inputs : None * * Outputs : None * * Returns : None * * * * Description : This is a Receive Task which handles all the USB data * * Receive. * * 1. When USB is not ready it goes into teh delayed state * * 2. When USB is ready it blocks to receive data over USB. * * 3. Task unblocks when data is received or error on USB * * occurred. When data is received it calls the virtual * * driver functions to pass the entire received packet to * * the application. * * * ***************************************************************************** */ void USBInterfaceReceiveTask(void) { GF_UINT8 num_bytes_received; GF_UINT8 indx; /* Set the Initial interface layer state to Receive */ usb_int_state = RX_READY; while (1) { while ((USB_GetState() & (USB_STAT_CONFIGURED | USB_STAT_SUSPENDED)) != USB_STAT_CONFIGURED) { ///\Todo 20150723 -> Change from 50ms to 150ms dur to larger Objcet USB_OS_Delay(150); } num_bytes_received = 0; /* Blocking call to receive bytes on USB */ num_bytes_received = USB_CDC_Receive(usb_rxd_buf, GENI_TGM_MAX_SIZE); while (usb_int_state != RX_READY) { USB_OS_Delay(10); } /* If any bytes is received then process it.*/ if (num_bytes_received > 0) { for (indx = 0; indx < num_bytes_received; indx++) { SaveRxByte(USB_CH_NO, usb_rxd_buf[indx]); } } } }
/********************************************************************* * * _OnStatusChange * */ static void _OnStatusChange(void * pContext) { (void)pContext; _State = USB_GetState(); OS_EVENT_Pulse(&_Event); }
//------------------------------------------------------------------------------ // Main //------------------------------------------------------------------------------ int main() { unsigned int dPioStatus; S_mse_report *pReport = &(sMse.sReport); bool isChanged; TRACE_INIT(); TRACE_INFO("\n\rMain HID mouse\n\r"); // Initialize the HID mouse driver MSE_Init(&sMse, &sUsb); TRACE_INFO("Connecting ... "); // Wait for the device to be powered before connecting it while (!ISSET(USB_GetState(&sUsb), USB_STATE_POWERED)); USB_Connect(&sUsb); TRACE_INFO("OK\n\r"); // Main loop while (1) { // Retrieve PIO status change isChanged = false; dPioStatus = SWITCH_PIO->PIO_PDSR; // Check for clicks on any button // Left mouse button if (ISCLEARED(dPioStatus, SW_LEFTCLICK)) { SET(pReport->bButtons, MSE_LEFT_BUTTON); CLEAR(dPioStatus, SW_LEFTCLICK); isChanged = true; } else { // Check if button was previously pressed if (ISSET(pReport->bButtons, MSE_LEFT_BUTTON)) { CLEAR(pReport->bButtons, MSE_LEFT_BUTTON); isChanged = true; } } // Right mouse button if (ISCLEARED(dPioStatus, SW_RIGHTCLICK)) { SET(pReport->bButtons, MSE_RIGHT_BUTTON); CLEAR(dPioStatus, SW_RIGHTCLICK); isChanged = true; } else { // Check if button was previously pressed if (ISSET(pReport->bButtons, MSE_RIGHT_BUTTON)) { CLEAR(pReport->bButtons, MSE_RIGHT_BUTTON); isChanged = true; } } // Check pointer for movement // Left if (ISCLEARED(dPioStatus, SW_LEFT)) { pReport->bX = -SPEED_X; isChanged = true; } // Right else if (ISCLEARED(dPioStatus, SW_RIGHT)) { pReport->bX = SPEED_X; isChanged = true; } else { pReport->bX = 0; } // Up if (ISCLEARED(dPioStatus, SW_UP)) { pReport->bY = -SPEED_Y; isChanged = true; } // Down else if (ISCLEARED(dPioStatus, SW_DOWN)) { pReport->bY = SPEED_Y; isChanged = true; } else { pReport->bY = 0; } // Send report if a change has occured if (isChanged) { LED_TOGGLE(LED_MEM); MSE_SendReport(&sMse, 0, 0); } } }