void usb_initialization(){ printf("USB Initialization\n"); alt_up_usb_dev * usb_dev; usb_dev = alt_up_usb_open_dev(USB_0_NAME); assert(usb_dev); usb_device_init(usb_dev, USB_0_IRQ); printf("Polling USB device. Run middleman now!\n"); alt_timestamp_start(); int clocks = 0; while (clocks < 50000000 * 10) { clocks = alt_timestamp(); usb_device_poll(); } printf("Done polling USB\n"); }
/******************************************************************************** * This program demonstrates use of the media ports in the DE2 Media Computer * * It performs the following: * 1. records audio for about 10 seconds when KEY[1] is pressed. LEDG[0] is * lit while recording * 2. plays the recorded audio when KEY[2] is pressed. LEDG[1] is lit while * playing * 3. Draws a blue box on the VGA display, and places a text string inside * the box * 4. Shows a text message on the LCD display * 5. Displays the last three bytes of data received from the PS/2 port * on the HEX displays on the DE2 board ********************************************************************************/ int main(void) { alt_up_usb_dev *usb_dev; int errors = 0, count = 0; unsigned long rhp1, rhp2; unsigned int status; unsigned int ccode=0; unsigned int rbuf[128]; unsigned int iManufacturer,iProduct; unsigned int mouse01=0,mouse02=0; unsigned int mycode; // alt_up_parallel_port_dev *KEY_dev; // alt_up_ps2_dev *PS2_dev; // alt_up_audio_dev *audio_dev; // alt_up_character_lcd_dev *lcd_dev; // alt_up_char_buffer_dev *char_buffer_dev; // alt_up_pixel_buffer_dma_dev *pixel_buffer_dev; // /* used for audio record/playback */ // int record = 0, play = 0, buffer_index = 0; // unsigned int l_buf[BUF_SIZE]; // unsigned int r_buf[BUF_SIZE]; // int num_read; int num_written; // /* used for PS/2 port data */ // unsigned char PS2_data; // unsigned char byte1 = 0, byte2 = 0, byte3 = 0; /* create a message to be displayed on the VGA and LCD displays */ // char text_top_row[40] = "Altera DE2\0"; // char text_bottom_row[40] = "Media Computer\0"; /* open the usb controller */ usb_dev = alt_up_usb_open_dev ("/dev/USB_avalon_usb_slave"); if (usb_dev == NULL) { alt_printf ("Error: could not open USB controller device\n"); return -1; } else alt_printf ("Opened USB controller device\n"); printf ("USB Base Address: %4X\n", usb_dev->base); printf ("Read Chip ID: %4X\n", alt_up_usb_hc_reg_read_16(usb_dev, 0x27)); // Test HC scratch reg do { alt_up_usb_hc_reg_write_16(usb_dev, 0x28, count); if (count != alt_up_usb_hc_reg_read_16(usb_dev, 0x28)) { printf("Error Encountered at %d\n", count); errors++; } } while (++count <= 0xFFFF); if (errors == 0) { printf("No errors writing and reading from HC scratch reg.\n"); } else { printf("There were %d errors writing and reading from HC scratch reg.\n", errors); } // End test HC scratch reg printf("Initialize defaults for HC controller\n"); alt_up_usb_hc_initialize_defaults(usb_dev); printf("Setup USB Operational mode\n"); alt_up_usb_hc_set_operational(usb_dev); // printf("Disable all USB interrupts\n"); // alt_up_usb_hc_disable_all_interrupts(usb_dev); printf("Enable Ports\n"); alt_up_usb_enable_ports(usb_dev); printf("Length of ATL: %4d\n", alt_up_usb_hc_reg_read_16(usb_dev, ALT_UP_USB_HcATLBufferSize)); printf("Length of PTL: %4d\n", alt_up_usb_hc_reg_read_16(usb_dev, ALT_UP_USB_HcISTLBufferSize)); printf("Length of INT: %4d\n", alt_up_usb_hc_reg_read_16(usb_dev, ALT_UP_USB_HcINTLBufferSize)); // Start checking if something is connected rhp1 = alt_up_usb_hc_reg_read_32( usb_dev, ALT_UP_USB_HcRhPortStatus1 ); rhp2 = alt_up_usb_hc_reg_read_32( usb_dev, ALT_UP_USB_HcRhPortStatus2 ); printf("RhP1 = %8lX RhP2 = %8lX\n", rhp1, rhp2); printf("Port1Speed = %2d\n", alt_up_usb_get_port_speed(ALT_UP_USB_PORT_1)); printf("Port2Speed = %2d\n", alt_up_usb_get_port_speed(ALT_UP_USB_PORT_2)); if ((rhp2 & 0x01) == 1) { printf("Port 2 has USB device connected, assigning address 2...\n"); ccode = alt_up_usb_set_address( usb_dev, ALT_UP_USB_PORT_2, 0, 2); status=0x0001|(ccode<<4); printf ("MyStatus = %X\n", status); } // alt_up_usb_hc_reg_write_16(usb_dev, ALT_UP_USB_HcuPInterruptEnable, 0x120); if( (status&0x0001)!=0) //port 2 active { // Check port 2 for mouse mycode=alt_up_usb_get_control(usb_dev, rbuf,2,'D',0,2); printf("MyCode = %X\n", mycode); if(mycode==0x0300) { iManufacturer = rbuf[7]&0xFF; iProduct = (rbuf[7]&0xFF00)>>8; alt_up_usb_addr_info(2,'W','O',iManufacturer); alt_up_usb_addr_info(2,'W','P',iProduct); mycode=alt_up_usb_get_control(usb_dev,rbuf,2,'H',alt_up_usb_addr_info(2,'R','P',0),2); printf("Device found? %4X\n", *(rbuf + 1)); if( *(rbuf+1)==0x0209 ) { printf("Mouse Detected\n"); mouse02=1; mycode = alt_up_usb_set_config( usb_dev, 2, 2, 1 ); printf("Set Config MyCode = %X\n", mycode); if(mycode==0) alt_up_usb_play_mouse( usb_dev, 2, 2 ); } else if( *(rbuf+1)==0x0609 ){ printf("Keyboard Detected\n"); mouse02=1; mycode = alt_up_usb_set_config( usb_dev, 2, 2, 1 ); printf("Set Config MyCode = %X\n", mycode); if(mycode==0) alt_up_usb_play_keyboard( usb_dev, 2, 2 ); } }