📄 usb2uart.lst
字号:
248 { //主端点输出描述
249 sizeof(ENDPOINT_DESCRIPTOR_STRUCT), //端点描述符的字节数大小
250 ENDPOINT_DESCRIPTOR, //端点描述符类型编号
251 MAIN_POINT_OUT, //端点号,主输出端点
252 ENDPOINT_TYPE_BULK, //使用的传输类型:批量传输
253 SWAP16(0x0040), //该端点支持的最大包尺寸,64字节
254 0x0A //中断扫描时间:批量端点无效
255 }
256 };
257
258 union //程序标志位
259 {
260 uint16 Register;
261 struct
262 {
263 uint8 bus_reset :1;
264 uint8 suspend :1;
265 uint8 remote_wakeup :1;
266 uint8 int_isr :1;
267 uint8 not_end :1;
268 uint8 usb_idle :1;
269 uint8 usb_busy :1;
270 uint8 setup_packet_in :1;
271 uint8 setup_packet_out :1;
272 uint8 set_addr :1;
273 uint8 usb_endp0_in :1;
274 uint8 usb_endp2_in :1;
275 uint8 usb_endp2_out :1;
276 }flags;
277 }usb_flags;
278
279 union //中断寄存器
280 {
281 uint8 Register[2];
282 struct
283 {
284 uint8 control_out_port :1;
285 uint8 control_in_port :1;
286 uint8 port_out_1 :1;
287 uint8 port_in_1 :1;
288 uint8 main_out_port :1;
289 uint8 main_in_port :1;
290 uint8 bus_reset :1;
291 uint8 suspend_change :1;
292 uint8 DMA_EOT :1;
293 uint8 not_use :7;
294 }Interrupt;
295 }Interrupt_Register;
296
297
298 union //端点最后处理状态
299 {
300 uint8 Register;
301 struct
302 {
C51 COMPILER V7.06 USB2UART 12/11/2007 01:02:42 PAGE 6
303 uint8 successful :1;
304 uint8 error_code :4;
305 uint8 setup_packet :1;
306 uint8 data_1 :1;
307 uint8 prestatus_not_read :1;
308 }Status;
309 }Last_Status;
310
311
312 /***************** 延时x毫秒函数 ***********/
313 void delay(uint16 x)
314 {
315 1 uint16 i;
316 1 uint16 j;
317 1 for(i=0;i<x;i++)
318 1 for(j=0;j<230;j++);
319 1 }
320 /********************************************/
321
322 /*******************************写USB命令******************************************/
323 void write_usb_command(uint8 usb_command)
324 {
325 1 USB_A0=USB_COMMAND_ADD;
326 1 USB_DATA=usb_command;
327 1 USB_WR=0;
328 1 USB_WR=1;
329 1 USB_DATA=0xFF;
330 1 }
331 /******************************************************************************/
332
333 /*********************写一字节USB数据*******************************************/
334 void write_a_usb_data(uint8 usb_data)
335 {
336 1 USB_A0=USB_DATA_ADD;
337 1 USB_DATA=usb_data;
338 1 USB_WR=0;
339 1 USB_WR=1;
340 1 USB_DATA=0XFF;
341 1 }
342 /******************************************************************************/
343
344 /****************************读一字节USB数据************************************/
345 uint8 read_a_usb_data(void)
346 {
347 1 uint8 temp;
348 1 USB_A0=USB_DATA_ADD;
349 1 USB_RD=0;
350 1 temp=USB_DATA;
351 1 USB_RD=1;
352 1 return temp;
353 1 }
354 /******************************************************************************/
355
356 /************************读USB中断寄存器**************************************/
357 void read_interrupt_register(void)
358 {
359 1 write_usb_command(Read_Interrupt_Register);
360 1 Interrupt_Register.Register[0]=read_a_usb_data();
361 1 Interrupt_Register.Register[1]=read_a_usb_data();
362 1 }
363 /******************************************************************************/
364
C51 COMPILER V7.06 USB2UART 12/11/2007 01:02:42 PAGE 7
365 /************************设置USB地址*******************************************/
366 void set_usb_addr(uint8 addr)
367 {
368 1 write_usb_command(Set_Address);
369 1 write_a_usb_data(0x80|addr);
370 1 #ifdef debug
Prints(" 设置地址.\r\n");
Prints(" 地址为: ");
PrintLongInt(addr);
Prints("\r\n");
#endif
376 1 }
377 /******************************************************************************/
378
379 /*************************端点使能******************************************/
380 void set_endpoint_enable(void)
381 {
382 1 write_usb_command(Set_Endpoint_Enable);
383 1 write_a_usb_data(0x01);
384 1 }
385 /******************************************************************************/
386
387 /****************************选择终端点*************************************/
388 uint8 select_endpoint(uint8 endp)
389 {
390 1 write_usb_command(Select_EndPoint+endp);
391 1 return read_a_usb_data();
392 1 }
393 /******************************************************************************/
394
395 /****************************读端点最后状态**********************************/
396 uint8 read_last_status(uint8 endp)
397 {
398 1 write_usb_command(Read_Last_Status+endp);
399 1 return read_a_usb_data();
400 1 }
401 /******************************************************************************/
402
403 /****************************设置端点状态**************************************/
404 void set_endpoint_status(uint8 endp,uint8 status)
405 {
406 1 write_usb_command(0x40+endp);
407 1 write_a_usb_data(!status);
408 1 }
409 /******************************************************************************/
410
411 /*****************************读端点状态**************************************/
412 uint8 read_endpoint_status(uint8 endp)
413 {
414 1 write_usb_command(0x80+endp);
415 1 return read_a_usb_data();
416 1 }
417 /******************************************************************************/
418
419 /************************清缓冲,在读取缓冲数据后调用**************************/
420 void clear_buffer(void)
421 {
422 1 write_usb_command(Clear_Buffer);
423 1 #ifdef debug
Prints("Clear buffer.\r\n");
#endif
426 1 }
C51 COMPILER V7.06 USB2UART 12/11/2007 01:02:42 PAGE 8
427 /******************************************************************************/
428
429 /***********************缓冲区数据有效,在写缓冲后调用**************************/
430 void validate_buffer(void)
431 {
432 1 write_usb_command(Validate_Buffer);
433 1 #ifdef debug
Prints("Validate buffer.\r\n");
#endif
436 1 }
437 /******************************************************************************/
438
439 /***************************应答建立包************************************/
440 void ack_setup(uint8 endp)
441 {
442 1 select_endpoint(endp);
443 1 write_usb_command(Ack_Setup);
444 1 #ifdef debug
Prints("Ack setup ");
PrintLongInt(endp);
Prints(".\r\n");
#endif
449 1 }
450 /******************************************************************************/
451
452 //#define debug2
453 /***********************出错处理******************************************/
454 void error(uint8 number)
455 {
456 1 #ifdef debug2
Prints("有错误发生!!!\r\n错误号: ");
PrintHex(number);
#endif
460 1 number=0;
461 1 }
462 /******************************************************************************/
463
464 /*************************读终端点缓冲****************************************/
465 uint8 read_endpoint_buff(uint8 endp,uint8 len,uint8 * buff)
466 {
467 1 uint8 i,j;
468 1
469 1 if(!(select_endpoint(endp)&0x01)){error(0); return 0;}
470 1 if((read_endpoint_status(endp)&0x60)!=0x60) //两个缓冲没有都满,才能清中断
471 1 {
472 2 read_last_status(endp); //清中断
473 2 }
474 1 write_usb_command(Read_Buffer);
475 1 read_a_usb_data();
476 1 j=read_a_usb_data();
477 1 if(j>len)
478 1 j=len;
479 1 #ifdef debug
Prints("Read endpoint");
PrintLongInt(endp);
Prints("\'s buffer ");
PrintLongInt(j);
Prints(" bytes.\r\n");
#endif
486 1 for(i=0;i<j;i++)
487 1 {
488 2 USB_RD=0;
C51 COMPILER V7.06 USB2UART 12/11/2007 01:02:42 PAGE 9
489 2 *(buff+i)=USB_DATA;
490 2 USB_RD=1;
491 2 #ifdef debug
PrintHex(*(buff+i));
#endif
494 2 }
495 1 #ifdef debug
Prints("\r\n");
#endif
498 1 if(endp)
499 1 {
500 2 select_endpoint(endp);
501 2 clear_buffer();
502 2 }
503 1 return j;
504 1 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -