int alt_up_usb_retrieve_mouse_packet(alt_up_usb_dev * usb_device, struct alt_up_usb_mouse_packet * mouse_packet) {

    struct ptd_struct int_control_packet_ptd;
    unsigned int int_control_packet[128];
    unsigned int int_return_packet[128];
    unsigned int timeout_counter;

    // Check if the USB mouse is still connected 
    if ((alt_up_usb_hc_reg_read_16(usb_device, ALT_UP_USB_HcRhPortStatus2) & 0x01) != 0x01) {
        return ALT_UP_USB_MOUSE_NOT_CONNECTED;
    }

    // Send out DATA IN packet
    alt_up_usb_init_ptd(&int_control_packet_ptd, ALT_UP_USB_DIR_TOKEN_IN, 1, 4, toggle % 2, mouse_addr, mouse_port);
    int_control_packet_ptd.startingFrame = 0x00;
    alt_up_usb_convert_ptd_to_array(int_control_packet, &int_control_packet_ptd);
    timeout_counter = alt_up_usb_send_int_packet(usb_device, int_control_packet, int_return_packet);

    if (timeout_counter != 0) {
        mouse_packet->x_movement = (signed char) (int_return_packet[4] >> 8);
        mouse_packet->y_movement = (signed char) (int_return_packet[5] & 0x00FF);
        mouse_packet->buttons = (signed char) (int_return_packet[4] & 0x000F);
        //printf ("\nNew Mouse Packet:\n");
        //printf ("\tX movement is %d\n", *X);
        //printf ("\tY movement is %d\n", *Y);
        //printf ("\tButtons are %X\n", *B);

        toggle++;

        return ALT_UP_USB_MOUSE_PACKET_OK;
    }
Ejemplo n.º 2
0
/********************************************************************************
 * 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 );
       
   }
   
  }