1.1 --- a/test.c Wed Mar 13 15:25:27 2013 +0000
1.2 +++ b/test.c Wed Mar 13 16:13:55 2013 +0000
1.3 @@ -33,9 +33,13 @@
1.4 typedef enum
1.5 {
1.6 MAX_DEVSTATE_INIT = 0,
1.7 - MAX_DEVSTATE_CONNECTED = 1,
1.8 - MAX_DEVSTATE_RESET = 2,
1.9 - MAX_DEVSTATE_READY = 3
1.10 + MAX_DEVSTATE_CONNECTED,
1.11 + MAX_DEVSTATE_START,
1.12 + MAX_DEVSTATE_RESET,
1.13 + MAX_DEVSTATE_INSPECTED,
1.14 + MAX_DEVSTATE_RESET_AGAIN,
1.15 + MAX_DEVSTATE_ADDRESSING,
1.16 + MAX_DEVSTATE_READY
1.17 } max_devstate;
1.18
1.19 /* Device details. */
1.20 @@ -562,9 +566,6 @@
1.21 max_write(MAX_REG_MODE, MAX_MODE_HOST_FULLSPEED);
1.22 }
1.23
1.24 - /* Reset the device. */
1.25 -
1.26 - max_write(MAX_REG_HCTL, MAX_HCTL_BUSRST);
1.27 return true;
1.28 }
1.29
1.30 @@ -603,7 +604,7 @@
1.31 * Get a descriptor from the device having the given type, value and index.
1.32 * The descriptor buffer is static and must be copied if it is to be preserved.
1.33 */
1.34 -uint8_t *max_get_descriptor(max_device *device, uint8_t type, uint8_t value, uint8_t index)
1.35 +uint8_t *max_get_descriptor(max_device *device, uint8_t type, uint8_t value, uint8_t index, bool initial)
1.36 {
1.37 static uint8_t data[64];
1.38 uint8_t len = 64, setup[8];
1.39 @@ -612,7 +613,9 @@
1.40
1.41 max_write(MAX_REG_PERADDR, device->address);
1.42
1.43 - setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, usb_descriptor_type(type) | value, index, usb_descriptor_size(type));
1.44 + setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, usb_descriptor_type(type) | value, index,
1.45 + initial ? 8 : 64);
1.46 +
1.47 max_control(setup);
1.48 if (!max_control_input(data, &len, device))
1.49 {
1.50 @@ -645,7 +648,32 @@
1.51
1.52 /* Send a "get descriptor" request for the device descriptor. */
1.53
1.54 - desc = (struct usb_device_descriptor *) max_get_descriptor(device, USB_DT_DEVICE, 0, 0);
1.55 + desc = (struct usb_device_descriptor *) max_get_descriptor(device, USB_DT_DEVICE, 0, 0, true);
1.56 +
1.57 + if (desc != NULL)
1.58 + {
1.59 + device->max_packet_size = device->desc.bMaxPacketSize0;
1.60 +
1.61 + /* Reset the device. */
1.62 +
1.63 + max_write(MAX_REG_HCTL, MAX_HCTL_BUSRST);
1.64 + return true;
1.65 + }
1.66 +
1.67 + return false;
1.68 +}
1.69 +
1.70 +bool max_complete_device(max_device *device)
1.71 +{
1.72 + struct usb_device_descriptor *desc;
1.73 +
1.74 + printf("Sending control request to address 0, endpoint 0...\n");
1.75 +
1.76 + device->address = 0;
1.77 +
1.78 + /* Send a "get descriptor" request for the device descriptor. */
1.79 +
1.80 + desc = (struct usb_device_descriptor *) max_get_descriptor(device, USB_DT_DEVICE, 0, 0, false);
1.81
1.82 if (desc != NULL)
1.83 {
1.84 @@ -670,7 +698,6 @@
1.85 printf("iSerialNumber: %d\n", device->desc.iSerialNumber);
1.86 printf("bNumConfigurations: %d\n", device->desc.bNumConfigurations);
1.87
1.88 - device->max_packet_size = device->desc.bMaxPacketSize0;
1.89 return true;
1.90 }
1.91
1.92 @@ -788,7 +815,7 @@
1.93
1.94 int main(int argc, char *argv[])
1.95 {
1.96 - uint8_t status = 0, revision = 0;
1.97 + uint8_t status = 0, revision = 0, framecount;
1.98 uint16_t count;
1.99 bool bus_event, data_event, suspended_event, connection_event, frame_event;
1.100 max_device device;
1.101 @@ -872,6 +899,7 @@
1.102 if (devicechanged())
1.103 {
1.104 devstate = MAX_DEVSTATE_CONNECTED;
1.105 + framecount = 200;
1.106 }
1.107
1.108 /* Enter the main processing loop. */
1.109 @@ -908,30 +936,87 @@
1.110 if ((devstate == MAX_DEVSTATE_INIT) && connection_event && devicechanged())
1.111 {
1.112 devstate = MAX_DEVSTATE_CONNECTED;
1.113 + framecount = 200;
1.114 printf("CONNECTED\n");
1.115 }
1.116
1.117 + /* Handle device connection. */
1.118 +
1.119 + else if ((devstate == MAX_DEVSTATE_CONNECTED) && frame_event)
1.120 + {
1.121 + if (framecount)
1.122 + framecount--;
1.123 + else
1.124 + {
1.125 + /* Reset the device. */
1.126 +
1.127 + max_write(MAX_REG_HCTL, MAX_HCTL_BUSRST);
1.128 + devstate = MAX_DEVSTATE_START;
1.129 + printf("START\n");
1.130 + }
1.131 + }
1.132 +
1.133 /* Handle device reset initiation. */
1.134
1.135 - else if ((devstate == MAX_DEVSTATE_CONNECTED) && bus_event)
1.136 + else if ((devstate == MAX_DEVSTATE_START) && bus_event)
1.137 {
1.138 - max_write(MAX_REG_MODE, max_read(MAX_REG_MODE, NULL));
1.139 devstate = MAX_DEVSTATE_RESET;
1.140 + framecount = 200;
1.141 printf("RESET\n");
1.142 }
1.143
1.144 /* Handle device reset completion, getting device details and
1.145 - initiating a second reset. */
1.146 + resetting the device again. */
1.147 +
1.148 + else if ((devstate == MAX_DEVSTATE_RESET) && frame_event)
1.149 + {
1.150 + if (framecount)
1.151 + framecount--;
1.152 + else if (!max_init_device(&device))
1.153 + printf("FAILED: RESET -> INSPECTED\n");
1.154 + else
1.155 + {
1.156 + devstate = MAX_DEVSTATE_INSPECTED;
1.157 + printf("INSPECTED\n");
1.158 + }
1.159 + }
1.160
1.161 - else if ((devstate == MAX_DEVSTATE_RESET) && frame_event && max_can_send(&status))
1.162 + /* Handle second reset initiation. */
1.163 +
1.164 + else if ((devstate == MAX_DEVSTATE_INSPECTED) && bus_event)
1.165 {
1.166 - if (!max_init_device(&device))
1.167 - printf("FAILED: RESET -> READY\n");
1.168 + devstate = MAX_DEVSTATE_RESET_AGAIN;
1.169 + framecount = 200;
1.170 + printf("RESET_AGAIN\n");
1.171 + }
1.172 +
1.173 + /* Handle second reset completion, setting the address. */
1.174 +
1.175 + else if ((devstate == MAX_DEVSTATE_RESET_AGAIN) && frame_event)
1.176 + {
1.177 + if (framecount)
1.178 + framecount--;
1.179 + else if (!max_complete_device(&device))
1.180 + printf("FAILED: RESET_AGAIN -> ADDRESSING\n");
1.181 else
1.182 {
1.183 max_set_address(&device);
1.184 - usb_show_languages(max_get_descriptor(&device, USB_DT_STRING, 0, 0));
1.185 - usb_show_configuration(max_get_descriptor(&device, USB_DT_CONFIG, 0, 0));
1.186 + devstate = MAX_DEVSTATE_ADDRESSING;
1.187 + framecount = 30;
1.188 + printf("ADDRESSING\n");
1.189 + }
1.190 + }
1.191 +
1.192 + /* Post-addressing. */
1.193 +
1.194 + else if ((devstate == MAX_DEVSTATE_ADDRESSING) && frame_event)
1.195 + {
1.196 + if (framecount)
1.197 + framecount--;
1.198 + else
1.199 + {
1.200 + usb_show_languages(max_get_descriptor(&device, USB_DT_STRING, 0, 0, false));
1.201 + usb_show_configuration(max_get_descriptor(&device, USB_DT_CONFIG, 0, 0, false));
1.202 devstate = MAX_DEVSTATE_READY;
1.203 printf("READY\n");
1.204 }