コード例 #1
0
ファイル: V2Protocol.c プロジェクト: Codingboy/ucuni
/** Handler for the CMD_SET_PARAMETER and CMD_GET_PARAMETER commands from the host, setting or
 *  getting a device parameter's value from the parameter table.
 *
 *  \param[in] V2Command  Issued V2 Protocol command byte from the host
 */
static void V2Protocol_GetSetParam(const uint8_t V2Command)
{
	uint8_t ParamID = Endpoint_Read_8();
	uint8_t ParamValue;

	if (V2Command == CMD_SET_PARAMETER)
	  ParamValue = Endpoint_Read_8();

	Endpoint_ClearOUT();
	Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
	Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);

	Endpoint_Write_8(V2Command);

	uint8_t ParamPrivs = V2Params_GetParameterPrivileges(ParamID);

	if ((V2Command == CMD_SET_PARAMETER) && (ParamPrivs & PARAM_PRIV_WRITE))
	{
		Endpoint_Write_8(STATUS_CMD_OK);
		V2Params_SetParameterValue(ParamID, ParamValue);
	}
	else if ((V2Command == CMD_GET_PARAMETER) && (ParamPrivs & PARAM_PRIV_READ))
	{
		Endpoint_Write_8(STATUS_CMD_OK);
		Endpoint_Write_8(V2Params_GetParameterValue(ParamID));
	}
	else
	{
		Endpoint_Write_8(STATUS_CMD_FAILED);
	}

	Endpoint_ClearIN();
}
コード例 #2
0
ファイル: LufaUtil.c プロジェクト: hokim72/ATmega32U4
int16_t Device_ReceiveByte(USB_EPInfo_Device_t* const EPInfo)
{
	if (USB_DeviceState != DEVICE_STATE_Configured)
		return -1;

	int16_t ReceivedByte = -1;

	Endpoint_SelectEndpoint(EPInfo->DataOUTEPAddress);

	if (Endpoint_IsOUTReceived())
	// Endpoint_AVR8.h
	// Determines if the selected OUT endpoint has received new packet from 
	// the host.
	{
		if (Endpoint_BytesInEndpoint())
		// Endpoint_AVR8.h
		// Indicates the number of bytes currently stored in the current 
		// endpoint's selected bank.
			ReceivedByte = Endpoint_Read_8();
			// Endpoint_AVR8.h
			// Reads one byte from the currently selected endpoint's bank, 
			// for OUT direction endpoints.
		if (!(Endpoint_BytesInEndpoint()))
			Endpoint_ClearOUT();
			// Endpoint_AVR8.h
			// Acknowledges an OUT packet to the host on the currently selected
			// endpoint, freeing up the endpoint for the next packet and 
			// switching to the alternative endpoint bank if double banked.
	}

	return ReceivedByte;

}
コード例 #3
0
ファイル: XPROGProtocol.c プロジェクト: Steffen-Engel/lufa
/** Handler for the CMD_XPROG command, which wraps up XPROG commands in a V2 wrapper which need to be
 *  removed and processed so that the underlying XPROG command can be handled.
 */
void XPROGProtocol_Command(void)
{
	uint8_t XPROGCommand = Endpoint_Read_8();

	switch (XPROGCommand)
	{
		case XPROG_CMD_ENTER_PROGMODE:
			XPROGProtocol_EnterXPROGMode();
			break;
		case XPROG_CMD_LEAVE_PROGMODE:
			XPROGProtocol_LeaveXPROGMode();
			break;
		case XPROG_CMD_ERASE:
			XPROGProtocol_Erase();
			break;
		case XPROG_CMD_WRITE_MEM:
			XPROGProtocol_WriteMemory();
			break;
		case XPROG_CMD_READ_MEM:
			XPROGProtocol_ReadMemory();
			break;
		case XPROG_CMD_CRC:
			XPROGProtocol_ReadCRC();
			break;
		case XPROG_CMD_SET_PARAM:
			XPROGProtocol_SetParam();
			break;
	}
}
コード例 #4
0
uint8_t Endpoint_Read_Stream_LE(void* const Buffer,
			                                uint16_t Length,
			                                uint16_t* const BytesProcessed)
{
	uint16_t i;
  if (BytesProcessed != NULL)
  {
    *BytesProcessed = 0;
  }
	if (endpointselected==ENDPOINT_CONTROLEP){
		if(usb_data_buffer_size == 0) return ENDPOINT_RWSTREAM_IncompleteTransfer;
	}else
		if(usb_data_buffer_OUT_size == 0) return ENDPOINT_RWSTREAM_IncompleteTransfer;
	
	for(i=0;i<Length;i++)
	{
		#if defined(__LPC17XX__) || defined(__LPC177X_8X__)
		if (endpointselected!=ENDPOINT_CONTROLEP)
			while(usb_data_buffer_OUT_size == 0); /* Current Fix for LPC17xx, havent checked for others */
		#elif defined(__LPC43XX__)
		if (endpointselected!=ENDPOINT_CONTROLEP)
			while(usb_data_buffer_OUT_size == 0); /* EA ANDLI: Current Fix for LPC43xx to prevent overrun */
		#endif
		((uint8_t*)Buffer)[i] = Endpoint_Read_8();
	}
  if (BytesProcessed != NULL)
  {
    *BytesProcessed = Length;
  }
	return ENDPOINT_RWSTREAM_NoError;
}
コード例 #5
0
ファイル: KeyboardMouse.c プロジェクト: 0xdec/tmk_keyboard
/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
 *  the device from the USB host before passing along unhandled control requests to the library for processing
 *  internally.
 */
void EVENT_USB_Device_ControlRequest(void)
{
	uint8_t* ReportData;
	uint8_t  ReportSize;

	/* Handle HID Class specific requests */
	switch (USB_ControlRequest.bRequest)
	{
		case HID_REQ_GetReport:
			if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();

				/* Determine if it is the mouse or the keyboard data that is being requested */
				if (!(USB_ControlRequest.wIndex))
				{
					ReportData = (uint8_t*)&KeyboardReportData;
					ReportSize = sizeof(KeyboardReportData);
				}
				else
				{
					ReportData = (uint8_t*)&MouseReportData;
					ReportSize = sizeof(MouseReportData);
				}

				/* Write the report data to the control endpoint */
				Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
				Endpoint_ClearOUT();

				/* Clear the report data afterwards */
				memset(ReportData, 0, ReportSize);
			}

			break;
		case HID_REQ_SetReport:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();

				/* Wait until the LED report has been sent by the host */
				while (!(Endpoint_IsOUTReceived()))
				{
					if (USB_DeviceState == DEVICE_STATE_Unattached)
					  return;
				}

				/* Read in the LED report from the host */
				uint8_t LEDStatus = Endpoint_Read_8();

				Endpoint_ClearOUT();
				Endpoint_ClearStatusStage();

				/* Process the incoming LED report */
				Keyboard_ProcessLEDReport(LEDStatus);
			}

			break;
	}
}
コード例 #6
0
ファイル: KeyboardMouse.c プロジェクト: 0xdec/tmk_keyboard
/** Keyboard task. This generates the next keyboard HID report for the host, and transmits it via the
 *  keyboard IN endpoint when the host is ready for more data. Additionally, it processes host LED status
 *  reports sent to the device via the keyboard OUT reporting endpoint.
 */
void Keyboard_HID_Task(void)
{
	uint8_t JoyStatus_LCL = Joystick_GetStatus();

	/* Device must be connected and configured for the task to run */
	if (USB_DeviceState != DEVICE_STATE_Configured)
	  return;

	/* Check if board button is not pressed, if so mouse mode enabled */
	if (!(Buttons_GetStatus() & BUTTONS_BUTTON1))
	{
		/* Make sent key uppercase by indicating that the left shift key is pressed */
		KeyboardReportData.Modifier = HID_KEYBOARD_MODIFIER_LEFTSHIFT;

		if (JoyStatus_LCL & JOY_UP)
		  KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_A;
		else if (JoyStatus_LCL & JOY_DOWN)
		  KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_B;

		if (JoyStatus_LCL & JOY_LEFT)
		  KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_C;
		else if (JoyStatus_LCL & JOY_RIGHT)
		  KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_D;

		if (JoyStatus_LCL & JOY_PRESS)
		  KeyboardReportData.KeyCode[0] = HID_KEYBOARD_SC_E;
	}

	/* Select the Keyboard Report Endpoint */
	Endpoint_SelectEndpoint(KEYBOARD_IN_EPADDR);

	/* Check if Keyboard Endpoint Ready for Read/Write */
	if (Endpoint_IsReadWriteAllowed())
	{
		/* Write Keyboard Report Data */
		Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData), NULL);

		/* Finalize the stream transfer to send the last packet */
		Endpoint_ClearIN();

		/* Clear the report data afterwards */
		memset(&KeyboardReportData, 0, sizeof(KeyboardReportData));
	}

	/* Select the Keyboard LED Report Endpoint */
	Endpoint_SelectEndpoint(KEYBOARD_OUT_EPADDR);

	/* Check if Keyboard LED Endpoint Ready for Read/Write */
	if (Endpoint_IsReadWriteAllowed())
	{
		/* Read in and process the LED report from the host */
		Keyboard_ProcessLEDReport(Endpoint_Read_8());

		/* Handshake the OUT Endpoint - clear endpoint and ready for next report */
		Endpoint_ClearOUT();
	}
}
コード例 #7
0
int USBSerial::readRXEndpoint(void)
{
        Endpoint_SelectEndpoint(CDC_RX_EPADDR);
        
        if (Endpoint_IsOUTReceived()) {
                if (Endpoint_BytesInEndpoint()) {
                        return (unsigned char)Endpoint_Read_8();
                } else {
                        Endpoint_ClearOUT();
                        __asm__("nop\n\t""nop\n\t");
                        __asm__("nop\n\t""nop\n\t");
                        if (Endpoint_IsOUTReceived()) {
                                return (unsigned char)Endpoint_Read_8();
                        }
                }
        }
        
        return -1;
}
コード例 #8
0
ファイル: spiout.c プロジェクト: amcinnes/uc_am_xmit
void main_loop(void) {
	while (Endpoint_IsReadWriteAllowed()) {
		do {
			while (!(UCSR1A&0x20)) {
			}
			UDR1 = Endpoint_Read_8();
		} while (Endpoint_IsReadWriteAllowed());
		Endpoint_ClearOUT();
	}
}
コード例 #9
0
ファイル: usbutil.c プロジェクト: fulnonono/cantranslator
void readFromHost(UsbDevice* usbDevice, bool (*callback)(uint8_t*)) {
    uint8_t previousEndpoint = Endpoint_GetCurrentEndpoint();
    Endpoint_SelectEndpoint(OUT_ENDPOINT_NUMBER);

    while(Endpoint_IsOUTReceived()) {
        for(int i = 0; i < usbDevice->outEndpointSize; i++) {
            if(!QUEUE_PUSH(uint8_t, &usbDevice->receiveQueue,
                        Endpoint_Read_8())) {
                debug("Dropped write from host -- queue is full");
            }
        }
        processQueue(&usbDevice->receiveQueue, callback);
        Endpoint_ClearOUT();
    }
    Endpoint_SelectEndpoint(previousEndpoint);
}
コード例 #10
0
ファイル: NANDORway.c プロジェクト: GotoHack/NANDORway
// pure usb receive takes 16secs (1019KB/s) for 16MB
void speedtest_receive(void) {
	uint8_t k, byte;
	uint16_t i = 0;

	/* Select the OUT stream endpoint */
	Endpoint_SelectEndpoint(OUT_EP);

	while (i < NOR_BSS_32) {
		/* Check if the current endpoint can be read */
		while (!Endpoint_IsOUTReceived()) USB_USBTask();

		for (k = 0; k < RX_BUFFER_SIZE; ++k)
			byte = Endpoint_Read_8();

		Endpoint_ClearOUT();

		i += RX_BUFFER_SIZE;
	}
	usbio_set_byte(RES_SUCCESS, 1);
}
コード例 #11
0
ファイル: CDC.c プロジェクト: cloud-hot/Jennisense
int16_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
	if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
	  return -1;

	int16_t ReceivedByte = -1;

	Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpointNumber);

	if (Endpoint_IsOUTReceived())
	{
		if (Endpoint_BytesInEndpoint())
		  ReceivedByte = Endpoint_Read_8();

		if (!(Endpoint_BytesInEndpoint()))
		  Endpoint_ClearOUT();
	}

	return ReceivedByte;
}
コード例 #12
0
ファイル: PrinterClassDevice.c プロジェクト: 40000ft/lufa
int16_t PRNT_Device_ReceiveByte(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo)
{
	if (USB_DeviceState != DEVICE_STATE_Configured)
	  return -1;

	int16_t ReceivedByte = -1;

	Endpoint_SelectEndpoint(PRNTInterfaceInfo->Config.DataOUTEndpoint.Address);

	if (Endpoint_IsOUTReceived())
	{
		if (Endpoint_BytesInEndpoint())
		  ReceivedByte = Endpoint_Read_8();

		if (!(Endpoint_BytesInEndpoint()))
		  Endpoint_ClearOUT();
	}

	return ReceivedByte;
}
int16_t CDC_Device_ReceiveByte(
						USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
	int16_t Rx_bytes = -1;
	if ((USB_DeviceState == DEVICE_STATE_Configured) &&	(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS != 0))
	{
		Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpoint.Address);

		if (Endpoint_IsOUTReceived())
		{
			if (Endpoint_BytesInEndpoint())
			{
				Rx_bytes = Endpoint_Read_8();
			}
			else
			{
				Endpoint_ClearOUT();
			}

		}

	}
	return Rx_bytes;
}
コード例 #14
0
ファイル: DeviceStandardReq.c プロジェクト: Eih3/v0.83
void USB_Device_ProcessControlRequest(void)
{
	USB_ControlRequest.bmRequestType = Endpoint_Read_8();
	USB_ControlRequest.bRequest      = Endpoint_Read_8();
	USB_ControlRequest.wValue        = Endpoint_Read_16_LE();
	USB_ControlRequest.wIndex        = Endpoint_Read_16_LE();
	USB_ControlRequest.wLength       = Endpoint_Read_16_LE();

	EVENT_USB_Device_ControlRequest();

	if (Endpoint_IsSETUPReceived())
	{
		uint8_t bmRequestType = USB_ControlRequest.bmRequestType;

		switch (USB_ControlRequest.bRequest)
		{
			case REQ_GetStatus:
				if ((bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE)) ||
					(bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_ENDPOINT)))
				{
					USB_Device_GetStatus();
				}

				break;
			case REQ_ClearFeature:
			case REQ_SetFeature:
				if ((bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE)) ||
					(bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT)))
				{
					USB_Device_ClearSetFeature();
				}

				break;
			case REQ_SetAddress:
				if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE))
				  USB_Device_SetAddress();

				break;
			case REQ_GetDescriptor:
				if ((bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE)) ||
					(bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE)))
				{
					USB_Device_GetDescriptor();
				}

				break;
			case REQ_GetConfiguration:
				if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE))
				  USB_Device_GetConfiguration();

				break;
			case REQ_SetConfiguration:
				if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE))
				  USB_Device_SetConfiguration();

				break;
		}
	}

	if (Endpoint_IsSETUPReceived())
	{
		Endpoint_StallTransaction();
		Endpoint_ClearSETUP();
	}
}
コード例 #15
0
/** Master V2 Protocol packet handler, for received V2 Protocol packets from a connected host.
 *  This routine decodes the issued command and passes off the handling of the command to the
 *  appropriate function.
 */
void V2Protocol_ProcessCommand(void)
{
    uint8_t V2Command = Endpoint_Read_8();

    /* Reset timeout counter duration and start the timer */
    TimeoutTicksRemaining = COMMAND_TIMEOUT_TICKS;
    TCCR0B = ((1 << CS02) | (1 << CS00));

    switch (V2Command)
    {
    case CMD_SIGN_ON:
        V2Protocol_SignOn();
        break;
    case CMD_SET_PARAMETER:
    case CMD_GET_PARAMETER:
        V2Protocol_GetSetParam(V2Command);
        break;
    case CMD_LOAD_ADDRESS:
        V2Protocol_LoadAddress();
        break;
    case CMD_RESET_PROTECTION:
        V2Protocol_ResetProtection();
        break;
#if defined(ENABLE_ISP_PROTOCOL)
    case CMD_ENTER_PROGMODE_ISP:
        ISPProtocol_EnterISPMode();
        break;
    case CMD_LEAVE_PROGMODE_ISP:
        ISPProtocol_LeaveISPMode();
        break;
    case CMD_PROGRAM_FLASH_ISP:
    case CMD_PROGRAM_EEPROM_ISP:
        ISPProtocol_ProgramMemory(V2Command);
        break;
    case CMD_READ_FLASH_ISP:
    case CMD_READ_EEPROM_ISP:
        ISPProtocol_ReadMemory(V2Command);
        break;
    case CMD_CHIP_ERASE_ISP:
        ISPProtocol_ChipErase();
        break;
    case CMD_READ_FUSE_ISP:
    case CMD_READ_LOCK_ISP:
    case CMD_READ_SIGNATURE_ISP:
    case CMD_READ_OSCCAL_ISP:
        ISPProtocol_ReadFuseLockSigOSCCAL(V2Command);
        break;
    case CMD_PROGRAM_FUSE_ISP:
    case CMD_PROGRAM_LOCK_ISP:
        ISPProtocol_WriteFuseLock(V2Command);
        break;
    case CMD_SPI_MULTI:
        ISPProtocol_SPIMulti();
        break;
#endif
#if defined(ENABLE_XPROG_PROTOCOL)
    case CMD_XPROG_SETMODE:
        XPROGProtocol_SetMode();
        break;
    case CMD_XPROG:
        XPROGProtocol_Command();
        break;
#endif
    default:
        V2Protocol_UnknownCommand(V2Command);
        break;
    }

    /* Disable the timeout management timer */
    TCCR0B = 0;

    Endpoint_WaitUntilReady();
    Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPADDR);
    Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);
}
コード例 #16
0
ファイル: V2Protocol.c プロジェクト: Codingboy/ucuni
/** Master V2 Protocol packet handler, for received V2 Protocol packets from a connected host.
 *  This routine decodes the issued command and passes off the handling of the command to the
 *  appropriate function.
 */
void V2Protocol_ProcessCommand(void)
{
	uint8_t V2Command = Endpoint_Read_8();

	/* Start the watchdog with timeout interrupt enabled to manage the timeout */
	TimeoutExpired = false;
	wdt_enable(WDTO_1S);
	WDTCSR |= (1 << WDIE);

	switch (V2Command)
	{
		case CMD_SIGN_ON:
			V2Protocol_SignOn();
			break;
		case CMD_SET_PARAMETER:
		case CMD_GET_PARAMETER:
			V2Protocol_GetSetParam(V2Command);
			break;
		case CMD_LOAD_ADDRESS:
			V2Protocol_LoadAddress();
			break;
		case CMD_RESET_PROTECTION:
			V2Protocol_ResetProtection();
			break;
#if defined(ENABLE_ISP_PROTOCOL)
		case CMD_ENTER_PROGMODE_ISP:
			ISPProtocol_EnterISPMode();
			break;
		case CMD_LEAVE_PROGMODE_ISP:
			ISPProtocol_LeaveISPMode();
			break;
		case CMD_PROGRAM_FLASH_ISP:
		case CMD_PROGRAM_EEPROM_ISP:
			ISPProtocol_ProgramMemory(V2Command);
			break;
		case CMD_READ_FLASH_ISP:
		case CMD_READ_EEPROM_ISP:
			ISPProtocol_ReadMemory(V2Command);
			break;
		case CMD_CHIP_ERASE_ISP:
			ISPProtocol_ChipErase();
			break;
		case CMD_READ_FUSE_ISP:
		case CMD_READ_LOCK_ISP:
		case CMD_READ_SIGNATURE_ISP:
		case CMD_READ_OSCCAL_ISP:
			ISPProtocol_ReadFuseLockSigOSCCAL(V2Command);
			break;
		case CMD_PROGRAM_FUSE_ISP:
		case CMD_PROGRAM_LOCK_ISP:
			ISPProtocol_WriteFuseLock(V2Command);
			break;
		case CMD_SPI_MULTI:
			ISPProtocol_SPIMulti();
			break;
#endif
#if defined(ENABLE_XPROG_PROTOCOL)
		case CMD_XPROG_SETMODE:
			XPROGProtocol_SetMode();
			break;
		case CMD_XPROG:
			XPROGProtocol_Command();
			break;
#endif
		default:
			V2Protocol_UnknownCommand(V2Command);
			break;
	}

	/* Disable the timeout management watchdog timer */
	wdt_disable();

	Endpoint_WaitUntilReady();
	Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPNUM);
	Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);
}
コード例 #17
0
ファイル: OnyxWalker.c プロジェクト: jwatte/robotcode
void OnyxWalker_Task(void) {

    unsigned short now = MY_GetTicks();
    if (now < lastTicks) {
        ++numWraps;
        LCD_DrawUint(numWraps, WIDTH-7, 3);
    }
    if (now - lastVolts > 15000) {
        lastVolts = now;
        ++voltBlink;
        if ((power_cvolts > 1320 && !power_failure) || (voltBlink & 1)) {
            LCD_DrawFrac(power_cvolts, 2, 0, 3);
            LCD_DrawChar(' ', 6, 3);
            LCD_DrawChar('V', 7, 3);
            PORTC &= ~(1 << 6);
        }
        else {
            LCD_DrawChar(' ', 0, 3);
            for (unsigned char i = 1; i != 8; ++i) {
                LCD_DrawChar('-', i, 3);
            }
            if (!(voltBlink & 15) && power_cvolts > 0) {
                PORTC |= (1 << 6);
            }
            else {
                PORTC &= ~(1 << 6);
            }
        }
    }
    lastTicks = now;
    LCD_Flush();
    if (lastTicks - last_cvolts > 10000) {
        power_tick();
        last_cvolts = lastTicks;
    }

    if (USB_DeviceState != DEVICE_STATE_Configured) {
        return;
    }

    /* see if host has requested data */
    Endpoint_SelectEndpoint(DATA_RX_EPNUM);
    Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
    epic = Endpoint_IsConfigured();
    epiir = epic && Endpoint_IsINReady();
    epirwa = epiir && Endpoint_IsReadWriteAllowed();
    if (epirwa && (in_packet_ptr || (now - last_flush > FLUSH_TICK_INTERVAL))) {
        last_flush = now;
        if (in_packet_ptr == 0) {
            in_packet_ptr = 1;  //  repeat the last received serial
        }
        //  send packet in
        for (unsigned char ch = 0; ch < in_packet_ptr; ++ch) {
            Endpoint_Write_8(in_packet[ch]);
        }
        Endpoint_ClearIN();
        in_packet_ptr = 0;
    }

    /* see if there's data from the host */
    Endpoint_SelectEndpoint(DATA_TX_EPNUM);
    Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);
    MY_SetLed(LED_act, false);
    if (Endpoint_IsConfigured() && 
        Endpoint_IsOUTReceived() && 
        Endpoint_IsReadWriteAllowed()) {
        uint8_t n = Endpoint_BytesInEndpoint();
        if (n > sizeof(out_packet)) {
            MY_Failure("OUT too big", n, sizeof(out_packet));
        }
        out_packet_ptr = 0;
        MY_SetLed(LED_act, true);
        while (n > 0) {
            epic = Endpoint_Read_8();
            out_packet[out_packet_ptr++] = epic;
            --n;
        }
        Endpoint_ClearOUT();
        dispatch_out();
    }
}
コード例 #18
0
void CDC_Device_ProcessControlRequest(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
	if (!(Endpoint_IsSETUPReceived()))
	  return;

	if (USB_ControlRequest.wIndex != CDCInterfaceInfo->Config.ControlInterfaceNumber)
	  return;

	switch (USB_ControlRequest.bRequest)
	{
		case CDC_REQ_GetLineEncoding:
			if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();

				while (!(Endpoint_IsINReady()));

				Endpoint_Write_32_LE(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
				Endpoint_Write_8(CDCInterfaceInfo->State.LineEncoding.CharFormat);
				Endpoint_Write_8(CDCInterfaceInfo->State.LineEncoding.ParityType);
				Endpoint_Write_8(CDCInterfaceInfo->State.LineEncoding.DataBits);

				Endpoint_ClearIN();
				Endpoint_ClearStatusStage();
			}

			break;
		case CDC_REQ_SetLineEncoding:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();

				while (!(Endpoint_IsOUTReceived()))
				{
					if (USB_DeviceState == DEVICE_STATE_Unattached)
						return;
				}

				CDCInterfaceInfo->State.LineEncoding.BaudRateBPS = Endpoint_Read_32_LE();
				CDCInterfaceInfo->State.LineEncoding.CharFormat  = Endpoint_Read_8();
				CDCInterfaceInfo->State.LineEncoding.ParityType  = Endpoint_Read_8();
				CDCInterfaceInfo->State.LineEncoding.DataBits    = Endpoint_Read_8();

				Endpoint_ClearOUT();
				Endpoint_ClearStatusStage();

				EVENT_CDC_Device_LineEncodingChanged(CDCInterfaceInfo);
			}

			break;
		case CDC_REQ_SetControlLineState:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();
				Endpoint_ClearStatusStage();

				CDCInterfaceInfo->State.ControlLineStates.HostToDevice = USB_ControlRequest.wValue;

				EVENT_CDC_Device_ControLineStateChanged(CDCInterfaceInfo);
			}

			break;
		case CDC_REQ_SendBreak:
			if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
			{
				Endpoint_ClearSETUP();
				Endpoint_ClearStatusStage();

				EVENT_CDC_Device_BreakSent(CDCInterfaceInfo, (uint8_t)USB_ControlRequest.wValue);
			}

			break;
	}
}
コード例 #19
0
void USB_Device_ProcessControlRequest(void)
{
	if(Endpoint_IsSETUPReceived())
	{
	uint8_t* RequestHeader = (uint8_t*)&USB_ControlRequest;

	for (uint8_t RequestHeaderByte = 0; RequestHeaderByte < sizeof(USB_Request_Header_t); RequestHeaderByte++)
	  *(RequestHeader++) = Endpoint_Read_8();
	}

	EVENT_USB_Device_ControlRequest();

	if (Endpoint_IsSETUPReceived())
	{
		uint8_t bmRequestType = USB_ControlRequest.bmRequestType;

		switch (USB_ControlRequest.bRequest)
		{
			case REQ_GetStatus:
				if ((bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE)) ||
					(bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_ENDPOINT)))
				{
					USB_Device_GetStatus();
				}

				break;
			case REQ_ClearFeature:
			case REQ_SetFeature:
				if ((bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE)) ||
					(bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT)))
				{
					USB_Device_ClearSetFeature();
				}

				break;
			case REQ_SetAddress:
				if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE))
				  USB_Device_SetAddress();

				break;
			case REQ_GetDescriptor:
				if ((bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE)) ||
					(bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE)))
				{
					USB_Device_GetDescriptor();
				}

				break;
			case REQ_GetConfiguration:
				if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE))
				  USB_Device_GetConfiguration();

				break;
			case REQ_SetConfiguration:
				if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE))
				  USB_Device_SetConfiguration();

				break;
		}
	}
#if 0 // for test
	if (Endpoint_IsSETUPReceived())
	{
		Endpoint_StallTransaction();
		Endpoint_ClearSETUP();
	}
#endif
}