/* * request_buffer.c * * Created on: 19-Jun-2023 * Author: vivek */ #include "request_fifo.h" #include #include device_descriptor device_descriptor_data; config_descriptor config_descriptor_data; interface_descriptor interface_descriptor_data = {0}; endpoint_descriptor endpoint_descriptor_bin_1_data = {0}; endpoint_descriptor endpoint_descriptor_bout_1_data = {0}; endpoint_descriptor endpoint_descriptor_iin_5_data = {0}; endpoint_descriptor endpoint_descriptor_iout_5_data; device_qualifier_descriptor device_qualifier_descriptor_data; string_language_descriptor string_language_descriptor_data; uint8_t string_descriptor_manufacturer_id[256]; uint8_t string_descriptor_product_id[256]; uint8_t string_descriptor_serial_id[256]; uint32_t string_descriptor_manufacturer_id_len; uint32_t string_descriptor_product_id_len; uint32_t string_descriptor_serial_id_len; uint32_t UART_Enable; uint32_t USB_SPEED; int ltcusb_request_fifo_read (uint32_t* request_data_lower, uint32_t* request_data_higher) { if(ltcusb_fifo_read(REQ_FIFO_ADDR_RV, request_data_lower) != 0) { return -1; } if(ltcusb_fifo_read(REQ_FIFO_ADDR_RV, request_data_higher) != 0) { return -2; } return 0; } int usb_enumeration() { uint32_t data_higher; uint32_t data_lower; uint32_t descriptor_type; uint32_t descriptor_index; uint32_t descriptor_length; if(ltcusb_request_fifo_read(&data_lower,&data_higher)!=0) { return -1; } // Get Descriptor Request if ((data_lower & (0x0000FFFF)) == (0x00000680)) { descriptor_type = (data_lower & (0xff000000)); descriptor_index = (data_lower & (0x00ff0000)); descriptor_length = (data_higher & (0x00ff0000)); if(event_while(ep_event_XferNotReady,0,0x1)!=0) { return -2; } if (descriptor_type == 0x01000000) // Device Descriptor { if(get_device_descriptor()!=0) { return -3; } if(UART_Enable) { printf("1"); } } else if (descriptor_type == 0x02000000) // Configuartion Descriptor { if ((descriptor_length == 0x00090000) | (descriptor_length == 0x00ff0000)) // Configuration descriptor -- If Descriptor Length is 9 or 255 { if(get_config_descriptor()!=0) { return -4; } if(UART_Enable) { printf("2"); } } else // Configuration & Interface & Endpoint descriptor -- If Descriptor Length is not 9 { if(get_config_interface_endpoint_descriptor()!=0) { return -5; } if(UART_Enable) { printf("3"); } } } else if (descriptor_type == 0x06000000) // Device Qualifier Descriptor { if(get_device_qualifier_descriptor()!=0) { return -6; } if(UART_Enable) { printf("Q"); } } /**@todo In GUI App Device Name (using String Descriptor) is not correct */ else if (descriptor_type == 0x03000000) // String Descriptor { if (descriptor_index == 0x00000000) // String - language descriptor { if(get_string_language_descriptor()!=0) { return -7; } if(UART_Enable) { printf("5"); } } else if (descriptor_index == 0x00010000) // String - index 1 descriptor { if(get_string_index1_descriptor3(string_descriptor_manufacturer_id, string_descriptor_manufacturer_id_len) != 0) // if(get_string_index1_descriptor()!=0) { return -8; } if(UART_Enable) { printf("7"); } } else if (descriptor_index == 0x00020000) // String - index 2 descriptor { // if(get_string_index1_descriptor()!=0) if(get_string_index1_descriptor3(string_descriptor_product_id,string_descriptor_product_id_len)!=0) { return -9; } if(UART_Enable) { printf("6"); } } else // String - index 3 descriptor { // if(get_string_index1_descriptor()!=0) if(get_string_index1_descriptor3(string_descriptor_serial_id,string_descriptor_serial_id_len)!=0) { return -10; } if(UART_Enable) { printf("4"); } } } if(event_while(ep_event_XferNotReady,0,0x0)!=0) //wait for XferNotReady EP0 { return -11; } if(TRB_control_status3(0x0)!=0) { return -12; } if(event_while(ep_event_XferComplete,0,0x0)!=0) { return -13; } } else if ((data_lower & (0x0000FFFF)) == (0x00000500)) // Set Address Request { if(ltcusb_set_address_request(data_lower)!=0) { return -14; } if(UART_Enable) { printf("A"); } } else if ((data_lower & (0x0000FFFF)) == (0x00000900)) // Set Configuartion Request { if(Initialization_on_SetConfiguration()!=0) { return -15; } if(event_while(ep_event_XferComplete,0,0x1)!=0) { return -16; } if(UART_Enable) { printf("P"); } } else if ((data_lower & (0x000000FF)) == (0x00000040)) // Control OUT { if(ltcusb_control_out_request()!=0) { return -17; } if(UART_Enable) { printf("CO"); } } else if ((data_lower & (0x000000FF)) == (0x000000C0)) // Control IN { if(ltcusb_control_in_request(data_higher)!=0) { return -18; } if(UART_Enable) { printf("CI"); } } else // Stall { if(event_while(ep_event_XferNotReady,0,0x1)!=0) { return -19; } if(DEPSSTALL_cmd(0x0)!=0) { return -20; } } // if(UART_Enable) // { // printf("0"); // } if(TRB_control_setup()!=0) // For New packet { return -21; } return 0; } int get_device_descriptor() { if(TRB_control_data2(device_descriptor_data.bLength, sizeof(device_descriptor_data), (uint32_t*)&device_descriptor_data) != 0) { return -1; } return 0; } int get_config_descriptor() { if(TRB_control_data2(config_descriptor_data.bLength, sizeof(config_descriptor_data), (uint32_t*)&config_descriptor_data) != 0) { return -1; } return 0; } /**@todo fixme*/ static int data_cpy(void* dst, void* src, uint32_t len) { uint8_t* local_dst = (uint8_t*)dst; uint8_t* local_src = (uint8_t*)src; for(uint32_t i=0;i> 16); /* * Program device speed [2:0] bits and periodic frame interval * DCFG.DEVSPD=3'b000: High-speed * DCFG.DEVSPD=3'b001: Full-speed * DCFG.DEVSPD=3'b100: Super-speed */ DCFG_data *dcfg = dcfg_setup(); /* * Device Address. */ dcfg ->DEVADDR = (dev_addr & 0x7F); /* * Device Speed. */ dcfg ->DEVSPD = (USB_SPEED & 0x7); /**@todo why reserved bit are set, ask FPGA team.*/ dcfg ->reserved_11_10 = 0x2; /* * Refer Table number 1-78 for more information. */ dcfg ->NUMP = 0x4; if(dcfg_write() != 0) { return -1; } // wait for XferNotReady EP1 if(event_while(ep_event_XferNotReady,0,0x1) != 0) { return -2; } if(TRB_control_status2()!=0) { return -3; } // wait for XferComplete Event on EP1 if(event_while(ep_event_XferComplete,0,0x1) != 0) { return -4; } return 0; } int ltcusb_control_out_request() { // wait for XferNotReady EP1 if(event_while(ep_event_XferNotReady,0,0x1) != 0) { return -1; } /* TRB Control Status for 3 stages */ if(TRB_control_status3(0x1) != 0 ) { return -2; } // wait for XferComplete EP1 if(event_while(ep_event_XferComplete,0,0x1) != 0) { return -3; } return 0; } int ltcusb_control_in_request(uint32_t request_data_higher) { uint32_t control_in_address = (request_data_higher & (0x0000FFFF)); // wait for XferNotReady EP1 if(event_while(ep_event_XferNotReady,0,0x1) != 0) { return -1; } LtcUSB_TRB control_data_trb = {0}; control_data_trb.BPTRL = (CONTROL_ADDRESS_USB23 | control_in_address); control_data_trb.BUFSIZE = 0x00000008; control_data_trb.HWO = 1; control_data_trb.LST = 1; control_data_trb.TRBCTL = control_data_trbctl; control_data_trb.IOC = 1; if(ltcusb_trb_write(&control_data_trb) !=0 ) { return -2; } // This is for control transfer, the IP requires it to be 0x1 at endpoint DEPSTRTXFER_Par1 Depstrtxfer_par1_data = {0}; Depstrtxfer_par1_data.TDAddr_Low = TRB_FIFO_ADDR_USB23; DEPSTRTXFER_Par0 Depstrtxfer_par0_data = {0}; Depstrtxfer_par0_data.TDAddr_High = 0; if(DEPSTRTXFER_cmd(0x1, &Depstrtxfer_par1_data, &Depstrtxfer_par0_data) != 0) { return -3; } // wait for XferNotReady EP0 if(event_while(ep_event_XferNotReady,0,0x0) != 0) { return -4; } /* TRB Control Status for 3 stages */ if(TRB_control_status3(0x0) != 0 ) { return -5; } // wait for XferComplete EP0 if(event_while(ep_event_XferComplete,0,0x0) != 0) { return -6; } return 0; }