• R/O
  • SSH
  • HTTPS

panda-p: 提交


Commit MetaInfo

修訂7 (tree)
時間2012-06-13 14:57:09
作者tekken_boss

Log Message

000.001.007: 2012/06/13 14:45 : Fine tuning of SL811 driver class.

Modification

  • [SCI] Add retry sequence for write as same as read.
  • [SCI] Add wait sequence for read/write.
      • for write : initial 5ms, worth case 10ms
      • for read : initial 5ms, worth case 50ms
  • [ETC] Add printf debug code.
      • function address for printf : 0x00002b84
      • function address for scanf : 0x00002bd6
  • [FAT] Add 2 kinds of error case,
      • FAT_ILLEGAL_MBR_SIGNATURE
      • FAT_CAN_NOT_SUPPORT_NTFS.

Confirmation

  • Compilable.
  • Confirmed the following USB memory is workable.
    • Buffalo 128MB (RUF-C128M/U2)
    • Sony 4GB(USM4GLX/PA)
    • Sony 2GB(USM2GE)
    • Hagiwara 128MB (UD)
    • Imation 2GB

Change Summary

差異

--- trunk/src/sl811.c (revision 6)
+++ trunk/src/sl811.c (revision 7)
@@ -10,13 +10,13 @@
1010 #include "sl811.h"
1111 //#include "sc1602.h"
1212
13-#define SL811_ADDRREG (*(volatile unsigned char *)0x200000)
13+#define SL811_ADDRREG (*(volatile unsigned char *)0x200000)
1414 #define SL811_DATAREG (*(volatile unsigned char *)0x200002)
1515 #define CONTROL_BUFFER_SIZE 256
1616
1717 #define SLOW_RETRY_TH 5
1818 #define RETRY_OUT 20
19-
19+
2020 // -------------------------------------------
2121 // Proto type definitions
2222 // -------------------------------------------
@@ -37,14 +37,14 @@
3737 unsigned char IsPacketStatus(void);
3838
3939 // Locals
40-static unsigned char sl811_read_reg(unsigned char reg);
41-static void sl811_write_reg(unsigned char reg, unsigned char data);
42-static void sl811_read_buf(unsigned char reg, unsigned char* buf, short size);
43-static void sl811_write_buf(unsigned char reg, unsigned char* buf, short size);
44-static SUB_PROC_RESULT sl811_write_setup(P_SETUP_FORMAT pSetupFormat);
45-static SUB_PROC_RESULT sl811_write(unsigned char ep, unsigned char* data, int size);
46-static SUB_PROC_RESULT sl811_read(unsigned char ep, unsigned char* data, int size);
47-
40+static unsigned char sl811_read_reg(unsigned char reg);
41+static void sl811_write_reg(unsigned char reg, unsigned char data);
42+static void sl811_read_buf(unsigned char reg, unsigned char* buf, short size);
43+static void sl811_write_buf(unsigned char reg, unsigned char* buf, short size);
44+static SUB_PROC_RESULT sl811_write_setup(P_SETUP_FORMAT pSetupFormat);
45+static SUB_PROC_RESULT sl811_write(unsigned char ep, unsigned char* data, int size);
46+static SUB_PROC_RESULT sl811_read(unsigned char ep, unsigned char* data, int size);
47+
4848 static int ui_function_usb_debug(UI_COMMAND uicmd, int param);
4949 static int ui_function_usb_nop(UI_COMMAND uicmd, int param);
5050 static int ui_function_usb_desc_device(UI_COMMAND uicmd, int param);
@@ -51,12 +51,13 @@
5151 static int ui_function_usb_desc_config(UI_COMMAND uicmd, int param);
5252 static int ui_function_usb_desc_interface(UI_COMMAND uicmd, int param);
5353 static int ui_function_usb_desc_endpoints(UI_COMMAND uicmd, int param);
54-
54+
5555 // -------------------------------------------
5656 // Variables
5757 // -------------------------------------------
5858 // Locals
5959 static int sl811_wait_timer;
60+static int sl811_reg_wait_timer;
6061 static int sl811_disable_timer;
6162
6263 static unsigned char sl811_speed;
@@ -190,7 +191,7 @@
190191 UI_USB_01_TOP_LEVEL,
191192 UI_USB_02_EXECUTING,
192193 };
193-
194+
194195 enum sl811_error_code {
195196 USB_ERR_NONE, // 00
196197 USB_ERR_REG_TEST_FAIL, // 01
@@ -283,19 +284,20 @@
283284 // -------------------------------------------
284285 void sl811_1ms_handler(void) {
285286 if( sl811_wait_timer ) sl811_wait_timer--;
287+ if( sl811_reg_wait_timer ) sl811_reg_wait_timer--;
286288 if( sl811_disable_timer ) sl811_disable_timer--;
287289 }
288290
289291 void sl811_chip_irq(void) {
290- unsigned char reg;
291-
292-// reg = sl811_read_reg(SL811HS_STATUS);
293-// sl811_write_reg(SL811HS_STATUS, reg); // 割り込みクリア
294-// tprintf("SL811HS_STATUS : %02lX\n", (int)reg);
295-// reg = SL811_read_reg(SL811HS_USBA_HOSTSTATUS);
296-// tprintf("SL811HS_HOSTSTATUS : %02lX\n", (int)reg);
297-// reg = SL811_read_reg(SL811HS_INTENV);
298-// tprintf("SL811HS_INTENV : %02lX\n", (int)reg);
292+ unsigned char reg;
293+
294+// reg = sl811_read_reg(SL811HS_STATUS);
295+// sl811_write_reg(SL811HS_STATUS, reg); // 割り込みクリア
296+// tprintf("SL811HS_STATUS : %02lX\n", (int)reg);
297+// reg = SL811_read_reg(SL811HS_USBA_HOSTSTATUS);
298+// tprintf("SL811HS_HOSTSTATUS : %02lX\n", (int)reg);
299+// reg = SL811_read_reg(SL811HS_INTENV);
300+// tprintf("SL811HS_INTENV : %02lX\n", (int)reg);
299301 }
300302
301303 // -------------------------------------------
@@ -302,11 +304,12 @@
302304 // Initialize
303305 // -------------------------------------------
304306 int sl811_initialize(void) {
305- /* BUS initialize */
306- BSC.ASTCR.BYTE = 0xff;
307- P8DDR = 0xee; /* CS1-3 is enable */
307+ /* BUS initialize */
308+ BSC.ASTCR.BYTE = 0xff;
309+ P8DDR = 0xee; /* CS1-3 is enable */
308310
309311 sl811_wait_timer = 0;
312+ sl811_reg_wait_timer = 0;
310313 sl811_proc = USB_REG_TEST;
311314 sl811_error = USB_ERR_NONE;
312315
@@ -357,10 +360,10 @@
357360 unsigned char sl811_ctrl_receive(P_SETUP_FORMAT p_fmt) {
358361 if( sl811_proc != USB_IDLE ) return CLASS_REQ_BUSY;
359362
360- setup_fmt.bmRequestType = p_fmt->bmRequestType;
361- setup_fmt.bRequest = p_fmt->bRequest;
362- setup_fmt.wValue = p_fmt->wValue;
363- setup_fmt.wIndex = p_fmt->wIndex;
363+ setup_fmt.bmRequestType = p_fmt->bmRequestType;
364+ setup_fmt.bRequest = p_fmt->bRequest;
365+ setup_fmt.wValue = p_fmt->wValue;
366+ setup_fmt.wIndex = p_fmt->wIndex;
364367 setup_fmt.wLength = p_fmt->wLength << 8;
365368
366369 sl811_proc = USB_CONTROL_RECEIVE_01;
@@ -426,7 +429,7 @@
426429 // -------------------------------------------
427430 int sl811_process(void) {
428431 int i, data, result;
429- unsigned char *p;
432+ unsigned char *p;
430433
431434 switch( sl811_proc ) {
432435 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@@ -461,22 +464,25 @@
461464 // for ( i=0; i<sizeof(descriptor_endpoints[0])*MAX_EP; i++ ) *(p++) = 0;
462465
463466 // Register test
464- result = 0;
465- for(i = 16;i < 256; i++) {
466- buf[i] = sl811_read_reg(i);
467- sl811_write_reg(i, i);
468- }
469- for(i = 16;i < 256; i++) {
470- data = sl811_read_reg(i);
471- if(data != i) {
472- result = -1;
473- }
474- }
475- for(i = 16;i < 256;i++) sl811_write_reg(i, buf[i]);
467+ result = 0;
468+ for(i = 16;i < 256; i++) {
469+ buf[i] = sl811_read_reg(i);
470+ sl811_write_reg(i, i);
471+ }
472+ for(i = 16;i < 256; i++) {
473+ data = sl811_read_reg(i);
474+ if(data != i) {
475+ result = -1;
476+ }
477+ }
478+ for(i = 16;i < 256;i++) sl811_write_reg(i, buf[i]);
476479 if( result == -1 ) {
477480 sl811_error = USB_ERR_REG_TEST_FAIL;
478481 sl811_proc = USB_ERROR_STOP;
479482 } else {
483+#ifdef SL811HS_DEBUG_ON
484+ printf("[USB] RegTest OK\r\n");
485+#endif
480486 sl811_proc = USB_RESET_01;
481487 }
482488 break;
@@ -485,8 +491,8 @@
485491 // * SL811HST Reset sequence *
486492 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
487493 case USB_RESET_01:
488- sl811_write_reg(SL811HS_SOFCOUNTH, 0xae);
489- sl811_write_reg(SL811HS_CONTROL1, 0x08); // reset USB
494+ sl811_write_reg(SL811HS_SOFCOUNTH, 0xae);
495+ sl811_write_reg(SL811HS_CONTROL1, 0x08); // reset USB
490496 sl811_wait_timer = 20;
491497 sl811_proc = USB_RESET_02;
492498 break;
@@ -501,53 +507,59 @@
501507
502508 case USB_RESET_03:
503509 if( !sl811_wait_timer ) {
504- sl811_write_reg(SL811HS_STATUS, 0xff); // clear all interrupt bits
505- data = sl811_read_reg(SL811HS_STATUS);
506- if(data & 0x40){ // Check if device is removed
507- sl811_speed = 0; // None
510+ sl811_write_reg(SL811HS_STATUS, 0xff); // clear all interrupt bits
511+ data = sl811_read_reg(SL811HS_STATUS);
512+ if(data & 0x40){ // Check if device is removed
513+ sl811_speed = 0; // None
508514 sl811_write_reg(SL811HS_INTENV,
509- SL811HS_INTENV_BIT_USBA |
515+ SL811HS_INTENV_BIT_USBA |
510516 SL811HS_INTENV_BIT_SOFTIMER |
511- SL811HS_INTENV_BIT_INSRMV);
517+ SL811HS_INTENV_BIT_INSRMV);
512518 sl811_error = USB_NO_DEVICE;
513519 sl811_proc = USB_ERROR_STOP;
514520 } else {
515- sl811_write_reg(SL811HS_USBB_HOSTBASEDLEN, 0); //zero lenth
516- sl811_write_reg(SL811HS_USBB_HOSTPID, 0x50); //send SOF to EP0
517- sl811_write_reg(SL811HS_USBB_HOSTDEVADDR, 0x01); //address0
521+ sl811_write_reg(SL811HS_USBB_HOSTBASEDLEN, 0); //zero lenth
522+ sl811_write_reg(SL811HS_USBB_HOSTPID, 0x50); //send SOF to EP0
523+ sl811_write_reg(SL811HS_USBB_HOSTDEVADDR, 0x01); //address0
518524 sl811_write_reg(SL811HS_SOFCOUNTL, 0xe0);
519525
520526 sl811_write_reg(SL811HS_CONTROL1, 0x8);
521- sl811_wait_timer = 20;
527+ sl811_wait_timer = 20;
522528 if(!(data & 0x80)) sl811_proc = USB_RESET_OK_LOW;
523529 else sl811_proc = USB_RESET_OK_FULL;
524- }
530+ }
525531 }
526532 break;
527533
528534 case USB_RESET_OK_LOW:
529535 if( !sl811_wait_timer ) {
530- sl811_speed = USB_LOW; // Low
531- sl811_write_reg(SL811HS_SOFCOUNTH, 0xee);
532- sl811_write_reg(SL811HS_CONTROL1, 0x21);
533- sl811_write_reg(SL811HS_USBB_HOSTCTRL, 0x01);
536+ sl811_speed = USB_LOW; // Low
537+ sl811_write_reg(SL811HS_SOFCOUNTH, 0xee);
538+ sl811_write_reg(SL811HS_CONTROL1, 0x21);
539+ sl811_write_reg(SL811HS_USBB_HOSTCTRL, 0x01);
534540 for(i=0;i<20;i++) sl811_write_reg(SL811HS_STATUS, 0xff);
535- sl811_write_reg(SL811HS_INTENV, SL811HS_INTENV_BIT_USBA |
536- SL811HS_INTENV_BIT_SOFTIMER|SL811HS_INTENV_BIT_INSRMV);
541+ sl811_write_reg(SL811HS_INTENV, SL811HS_INTENV_BIT_USBA |
542+ SL811HS_INTENV_BIT_SOFTIMER|SL811HS_INTENV_BIT_INSRMV);
537543 sl811_proc = USB_DETECTION_01;
544+#ifdef SL811HS_DEBUG_ON
545+ printf("[USB] Low SPD detected\r\n");
546+#endif
538547 }
539548 break;
540549
541550 case USB_RESET_OK_FULL:
542551 if( !sl811_wait_timer ) {
543- sl811_speed = USB_FULL; // Full
544- sl811_write_reg(SL811HS_SOFCOUNTH, 0xae);
545- sl811_write_reg(SL811HS_CONTROL1, 0x01 );
546- sl811_write_reg(SL811HS_USBB_HOSTCTRL, 0x01);
552+ sl811_speed = USB_FULL; // Full
553+ sl811_write_reg(SL811HS_SOFCOUNTH, 0xae);
554+ sl811_write_reg(SL811HS_CONTROL1, 0x01 );
555+ sl811_write_reg(SL811HS_USBB_HOSTCTRL, 0x01);
547556 sl811_write_reg(SL811HS_STATUS, 0xff);
548- sl811_write_reg(SL811HS_INTENV, SL811HS_INTENV_BIT_USBA |
549- SL811HS_INTENV_BIT_SOFTIMER|SL811HS_INTENV_BIT_INSRMV);
557+ sl811_write_reg(SL811HS_INTENV, SL811HS_INTENV_BIT_USBA |
558+ SL811HS_INTENV_BIT_SOFTIMER|SL811HS_INTENV_BIT_INSRMV);
550559 sl811_proc = USB_DETECTION_01;
560+#ifdef SL811HS_DEBUG_ON
561+ printf("[USB] Hi SPD detected.\r\n");
562+#endif
551563 }
552564 break;
553565
@@ -556,15 +568,18 @@
556568 // * To get max packet size firstly. *
557569 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
558570 case USB_DETECTION_01: // SETUP stage
559- setup_fmt.bmRequestType = DIR_DEV2HOST | RECEIPIENT_DEVICE;
560- setup_fmt.bRequest = REQ_TYPE_GET_DESCRIPTOR;
561- setup_fmt.wValue = DESCRIPTOR_TYPE_DEVICE;
562- setup_fmt.wIndex = 0;
571+ setup_fmt.bmRequestType = DIR_DEV2HOST | RECEIPIENT_DEVICE;
572+ setup_fmt.bRequest = REQ_TYPE_GET_DESCRIPTOR;
573+ setup_fmt.wValue = DESCRIPTOR_TYPE_DEVICE;
574+ setup_fmt.wIndex = 0;
563575 setup_fmt.wLength = (ep_info[0].size) << 8;
564576
565577 ep_info[0].toggle = 0;
566578 switch( sl811_write_setup(&setup_fmt) ) {
567579 case SUB_PROC_DONE:
580+#ifdef SL811HS_DEBUG_ON
581+ printf("[USB] Detection\r\n");
582+#endif
568583 sl811_proc = USB_DETECTION_02;
569584 break;
570585 case SUB_PROC_ERROR:
@@ -616,14 +631,17 @@
616631 // * Descriptor collection (DEVICE) *
617632 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
618633 case USB_GET_DESC_DEVICE_01: // SETUP stage
619- setup_fmt.bmRequestType = DIR_DEV2HOST | RECEIPIENT_DEVICE;
620- setup_fmt.bRequest = REQ_TYPE_GET_DESCRIPTOR;
621- setup_fmt.wValue = DESCRIPTOR_TYPE_DEVICE;
622- setup_fmt.wIndex = 0;
634+ setup_fmt.bmRequestType = DIR_DEV2HOST | RECEIPIENT_DEVICE;
635+ setup_fmt.bRequest = REQ_TYPE_GET_DESCRIPTOR;
636+ setup_fmt.wValue = DESCRIPTOR_TYPE_DEVICE;
637+ setup_fmt.wIndex = 0;
623638 setup_fmt.wLength = (descriptor_device.bLength) << 8;
624639
625640 switch( sl811_write_setup(&setup_fmt) ) {
626641 case SUB_PROC_DONE:
642+#ifdef SL811HS_DEBUG_ON
643+ printf("[USB] Desc.Device\r\n");
644+#endif
627645 sl811_proc = USB_GET_DESC_DEVICE_02;
628646 break;
629647 case SUB_PROC_ERROR:
@@ -688,14 +706,17 @@
688706 // * Descriptor collection (CONFIG) *
689707 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
690708 case USB_GET_DESC_CONFIG_01:
691- setup_fmt.bmRequestType = DIR_DEV2HOST | RECEIPIENT_DEVICE;
692- setup_fmt.bRequest = REQ_TYPE_GET_DESCRIPTOR;
693- setup_fmt.wValue = DESCRIPTOR_TYPE_CONFIG;
694- setup_fmt.wIndex = 0;
709+ setup_fmt.bmRequestType = DIR_DEV2HOST | RECEIPIENT_DEVICE;
710+ setup_fmt.bRequest = REQ_TYPE_GET_DESCRIPTOR;
711+ setup_fmt.wValue = DESCRIPTOR_TYPE_CONFIG;
712+ setup_fmt.wIndex = 0;
695713 setup_fmt.wLength = sizeof(descriptor_config) << 8;
696714
697715 switch( sl811_write_setup(&setup_fmt) ) {
698716 case SUB_PROC_DONE:
717+#ifdef SL811HS_DEBUG_ON
718+ printf("[USB] Desc.Config\r\n");
719+#endif
699720 sl811_proc = USB_GET_DESC_CONFIG_02;
700721 break;
701722 case SUB_PROC_ERROR:
@@ -756,14 +777,17 @@
756777 // * TODO : to apply this sequence to multi interface device. *
757778 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
758779 case USB_GET_DESC_ALL_01:
759- setup_fmt.bmRequestType = DIR_DEV2HOST | RECEIPIENT_DEVICE;
760- setup_fmt.bRequest = REQ_TYPE_GET_DESCRIPTOR;
761- setup_fmt.wValue = DESCRIPTOR_TYPE_CONFIG;
762- setup_fmt.wIndex = 0;
780+ setup_fmt.bmRequestType = DIR_DEV2HOST | RECEIPIENT_DEVICE;
781+ setup_fmt.bRequest = REQ_TYPE_GET_DESCRIPTOR;
782+ setup_fmt.wValue = DESCRIPTOR_TYPE_CONFIG;
783+ setup_fmt.wIndex = 0;
763784 setup_fmt.wLength = (descriptor_config.wTotalLength) << 8;
764785
765786 switch( sl811_write_setup(&setup_fmt) ) {
766787 case SUB_PROC_DONE:
788+#ifdef SL811HS_DEBUG_ON
789+ printf("[USB] Desc.All\r\n");
790+#endif
767791 sl811_proc = USB_GET_DESC_ALL_02;
768792 break;
769793 case SUB_PROC_ERROR:
@@ -811,11 +835,17 @@
811835
812836 if( ( (descriptor_endpoints[data].bEndpointAddress & 0x80) == 0x80 ) // IN
813837 && ( (descriptor_endpoints[data].bmAttributes & 0x03) == 0x02 ) ) { // BULK
838+#ifdef SL811HS_DEBUG_ON
839+ printf("[USB] ep(IN) =%s\r\n",hex2char[data+1]);
840+#endif
814841 bulk_in_ep = data+1;
815842 // bulk_out_ep = data+1;
816843 }
817844 if( ( (descriptor_endpoints[data].bEndpointAddress & 0x80) == 0x00 ) // OUT
818845 && ( (descriptor_endpoints[data].bmAttributes & 0x03) == 0x02 ) ) { // BULK
846+#ifdef SL811HS_DEBUG_ON
847+ printf("[USB] ep(OUT)=%s\r\n",hex2char[data+1]);
848+#endif
819849 bulk_out_ep = data+1;
820850 // bulk_in_ep = data+1;
821851 }
@@ -855,15 +885,18 @@
855885 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
856886 case USB_SETADDRESS_01:
857887 // device_address = DEFAULT_DEVICE_ADDRESS;
858- setup_fmt.bmRequestType = DIR_HOST2DEV | RECEIPIENT_DEVICE;
859- setup_fmt.bRequest = REQ_TYPE_SET_ADDRESS;
860-// setup_fmt.wValue = ((unsigned short)device_address) << 8;
861- setup_fmt.wValue = (DEFAULT_DEVICE_ADDRESS) << 8;
862- setup_fmt.wIndex = 0;
888+ setup_fmt.bmRequestType = DIR_HOST2DEV | RECEIPIENT_DEVICE;
889+ setup_fmt.bRequest = REQ_TYPE_SET_ADDRESS;
890+// setup_fmt.wValue = ((unsigned short)device_address) << 8;
891+ setup_fmt.wValue = (DEFAULT_DEVICE_ADDRESS) << 8;
892+ setup_fmt.wIndex = 0;
863893 setup_fmt.wLength = 0;
864894
865895 switch( sl811_write_setup(&setup_fmt) ) {
866896 case SUB_PROC_DONE:
897+#ifdef SL811HS_DEBUG_ON
898+ printf("[USB] Set Addr\r\n");
899+#endif
867900 sl811_proc = USB_SETADDRESS_02;
868901 break;
869902 case SUB_PROC_ERROR:
@@ -897,15 +930,18 @@
897930 // * Set device configuration sequence *
898931 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
899932 case USB_SET_CONFIGURATION_01:
900- setup_fmt.bmRequestType = DIR_HOST2DEV | RECEIPIENT_DEVICE;
901- setup_fmt.bRequest = REQ_TYPE_SET_CONFIG;
902- setup_fmt.wValue = (descriptor_config.bConfigurationValue) << 8;
903- setup_fmt.wIndex = 0;
933+ setup_fmt.bmRequestType = DIR_HOST2DEV | RECEIPIENT_DEVICE;
934+ setup_fmt.bRequest = REQ_TYPE_SET_CONFIG;
935+ setup_fmt.wValue = (descriptor_config.bConfigurationValue) << 8;
936+ setup_fmt.wIndex = 0;
904937 setup_fmt.wLength = 0;
905938 ep_info[0].toggle = 1;
906939
907940 switch( sl811_write_setup(&setup_fmt) ) {
908941 case SUB_PROC_DONE:
942+#ifdef SL811HS_DEBUG_ON
943+ printf("[USB] Set Conf\r\n");
944+#endif
909945 sl811_proc = USB_SET_CONFIGURATION_02;
910946 break;
911947 case SUB_PROC_ERROR:
@@ -1170,63 +1206,63 @@
11701206 // -------------------------------------------
11711207 // [LOCAL] Register read
11721208 // -------------------------------------------
1173-static unsigned char sl811_read_reg(unsigned char reg) {
1174- SL811_ADDRREG = (unsigned char)reg;
1175- return SL811_DATAREG;
1176-}
1177-
1209+static unsigned char sl811_read_reg(unsigned char reg) {
1210+ SL811_ADDRREG = (unsigned char)reg;
1211+ return SL811_DATAREG;
1212+}
1213+
11781214 // -------------------------------------------
11791215 // [LOCAL] Register write
11801216 // -------------------------------------------
1181-static void sl811_write_reg(unsigned char reg, unsigned char data) {
1182- SL811_ADDRREG = (unsigned char)reg;
1217+static void sl811_write_reg(unsigned char reg, unsigned char data) {
1218+ SL811_ADDRREG = (unsigned char)reg;
11831219 SL811_DATAREG = data;
11841220
1185- if(reg<10) reg_debug[reg] = data;
1186-}
1187-
1221+ if(reg<10) reg_debug[reg] = data;
1222+}
1223+
11881224 // -------------------------------------------
11891225 // [LOCAL] Buffer read
11901226 // -------------------------------------------
1191-static void sl811_read_buf(unsigned char reg, unsigned char* buf, short size) {
1192- if(size <= 0) return;
1193- SL811_ADDRREG = reg;
1194- while(size--) *buf++ = SL811_DATAREG;
1195-}
1196-
1227+static void sl811_read_buf(unsigned char reg, unsigned char* buf, short size) {
1228+ if(size <= 0) return;
1229+ SL811_ADDRREG = reg;
1230+ while(size--) *buf++ = SL811_DATAREG;
1231+}
1232+
11971233 // -------------------------------------------
11981234 // [LOCAL] Buffer write
11991235 // -------------------------------------------
1200-static void sl811_write_buf(unsigned char reg, unsigned char* buf, short size) {
1201- if(size <= 0) return;
1202- SL811_ADDRREG = reg;
1203- while(size--) {
1204- SL811_DATAREG = *buf;
1205- buf++;
1206- }
1207-}
1208-
1236+static void sl811_write_buf(unsigned char reg, unsigned char* buf, short size) {
1237+ if(size <= 0) return;
1238+ SL811_ADDRREG = reg;
1239+ while(size--) {
1240+ SL811_DATAREG = *buf;
1241+ buf++;
1242+ }
1243+}
1244+
12091245 // -------------------------------------------
12101246 // [LOCAL] SL811 SETUP packet send
12111247 // -------------------------------------------
1212-static SUB_PROC_RESULT sl811_write_setup(P_SETUP_FORMAT pSetupFormat)
1213-{
1214- SUB_PROC_RESULT ret;
1248+static SUB_PROC_RESULT sl811_write_setup(P_SETUP_FORMAT pSetupFormat)
1249+{
1250+ SUB_PROC_RESULT ret;
12151251
12161252 ret = SUB_PROC_BUSY;
12171253
12181254 switch( write_setup_proc ) {
1219- case 0x00:
1220- sl811_write_buf(0x10, (unsigned char*) pSetupFormat, 8);
1221- sl811_write_reg(SL811HS_USBA_HOSTBASEADDR, 0x10);
1222- sl811_write_reg(SL811HS_USBA_HOSTBASEDLEN, 8);
1223- sl811_write_reg(SL811HS_USBA_HOSTDEVADDR, device_address);
1224- sl811_write_reg(SL811HS_USBA_HOSTPID, PID_SETUP);
1255+ case 0x00:
1256+ sl811_write_buf(0x10, (unsigned char*) pSetupFormat, 8);
1257+ sl811_write_reg(SL811HS_USBA_HOSTBASEADDR, 0x10);
1258+ sl811_write_reg(SL811HS_USBA_HOSTBASEDLEN, 8);
1259+ sl811_write_reg(SL811HS_USBA_HOSTDEVADDR, device_address);
1260+ sl811_write_reg(SL811HS_USBA_HOSTPID, PID_SETUP);
12251261 sl811_write_reg(SL811HS_USBA_HOSTCTRL, DATA0_WR); // SETUP always uses a DATA0 PID. (Ref, 8.5.3 "Control Transfers")
12261262 wait_100us(); wait_100us(); // 200us wait
12271263 write_setup_proc++;
1228- break;
1229- case 0x01:
1264+ break;
1265+ case 0x01:
12301266 if(sl811_read_reg(SL811HS_STATUS) & SL811HS_INTENV_BIT_USBA == 0) {
12311267 break;
12321268 } else {
@@ -1234,10 +1270,10 @@
12341270 break;
12351271 }
12361272 break;
1237- case 0x02:
1273+ case 0x02:
12381274 packet_status = sl811_read_reg(SL811HS_USBA_HOSTSTATUS);
12391275 if( packet_status & SL811HS_HOSTSTATUS_BIT_ACK ) {
1240- ret = SUB_PROC_DONE;
1276+ ret = SUB_PROC_DONE;
12411277 write_setup_proc = 0x00;
12421278 } else {
12431279 ret = SUB_PROC_ERROR;
@@ -1246,64 +1282,104 @@
12461282 default:
12471283 ret = SUB_PROC_ERROR;
12481284 break;
1249- }
1250- return ret;
1251-}
1252-
1285+ }
1286+ return ret;
1287+}
1288+
12531289 // -------------------------------------------
12541290 // [LOCAL] SL811 data send process
12551291 // TODO: Maintainance of recovery sequence
12561292 // -------------------------------------------
1257-static SUB_PROC_RESULT sl811_write(unsigned char ep, unsigned char* data, int size)
1258-{
1293+static SUB_PROC_RESULT sl811_write(unsigned char ep, unsigned char* data, int size)
1294+{
12591295 static int remained_size;
1260- static unsigned char packet_size;
1296+ static unsigned char packet_size, retry_cnt;
12611297 SUB_PROC_RESULT ret;
1262-
1298+
12631299 ret = SUB_PROC_BUSY;
12641300
12651301 switch( write_proc ) {
12661302 case 0x00:
1303+ retry_cnt = 0;
12671304 remained_size = size;
1305+ sl811_reg_wait_timer = 0;
12681306 if(size < 0) {
12691307 ret = SUB_PROC_ERROR;
12701308 break;
12711309 }
1310+#ifdef SL811HS_DEBUG_ON
1311+ printf("[USB] WR:");
1312+#endif
12721313 write_proc++;
12731314 // break;
12741315
1275- case 0x01:
1316+ case 0x01:
12761317 // Packet setting
12771318 if( remained_size == 0 ) packet_size = 0;
12781319 else if( ep_info[ep].size <= remained_size ) packet_size = ep_info[ep].size;
12791320 else packet_size = remained_size;
12801321
1281- sl811_write_buf(0x10, data+(size-remained_size), packet_size);
1282- sl811_write_reg(SL811HS_USBA_HOSTBASEADDR, 0x10);
1283- sl811_write_reg(SL811HS_USBA_HOSTBASEDLEN, packet_size);
1322+ sl811_write_buf(0x10, data+(size-remained_size), packet_size);
1323+ sl811_write_reg(SL811HS_USBA_HOSTBASEADDR, 0x10);
1324+ sl811_write_reg(SL811HS_USBA_HOSTBASEDLEN, packet_size);
12841325 sl811_write_reg(SL811HS_USBA_HOSTDEVADDR, device_address);
1285- sl811_write_reg(SL811HS_USBA_HOSTPID, PID_OUT | (ep_info[ep].address&0x0F) );
1286- sl811_write_reg(SL811HS_USBA_HOSTCTRL, ( (ep_info[ep].toggle & 0x01) == 0) ? DATA0_WR : DATA1_WR);
1287- wait_100us(); wait_100us(); // 200us
1326+ sl811_write_reg(SL811HS_USBA_HOSTPID, PID_OUT | (ep_info[ep].address&0x0F) );
12881327 write_proc++;
12891328 // break;
12901329
12911330 case 0x02:
1292- if(sl811_read_reg(SL811HS_STATUS) & SL811HS_INTENV_BIT_USBA == 0) {
1331+#ifdef SL811HS_DEBUG_ON
1332+ if ((ep_info[ep].toggle & 0x01) == 0) printf(" t0"); else printf(" t1");
1333+ printf("e");printf("%02s",hex2char[ep]);
1334+#endif
1335+ sl811_write_reg(SL811HS_USBA_HOSTCTRL, ( (ep_info[ep].toggle & 0x01) == 0) ? DATA0_WR : DATA1_WR);
1336+ if( retry_cnt < SLOW_RETRY_TH ) {
1337+ sl811_reg_wait_timer = 5;
1338+ } else if( retry_cnt < RETRY_OUT ) {
1339+ sl811_reg_wait_timer = 10;
1340+ } else {
1341+ ret = SUB_PROC_ERROR;
1342+ }
1343+ write_proc = 0x03;
1344+ break;
1345+
1346+ case 0x03:
1347+ // Wait for transfer complete
1348+ if( sl811_reg_wait_timer ) break;
1349+ if(sl811_read_reg(SL811HS_STATUS) & SL811HS_INTENV_BIT_USBA) {
1350+ packet_status = sl811_read_reg(SL811HS_USBA_HOSTSTATUS);
1351+ if(packet_status & SL811HS_HOSTSTATUS_BIT_NAK) {
1352+#ifdef SL811HS_DEBUG_ON
1353+ printf("/");
1354+#endif
1355+ retry_cnt++;
1356+ if( retry_cnt < RETRY_OUT ) {
1357+ write_proc = 0x02;
1358+ } else {
1359+ ret = SUB_PROC_ERROR;
1360+ }
1361+ } else {
1362+#ifdef SL811HS_DEBUG_ON
1363+ printf(".");
1364+#endif
1365+ write_proc++;
1366+ }
12931367 break;
12941368 } else {
1295- write_proc++;
1369+ write_proc = 0x04;
12961370 break;
12971371 }
12981372
1299- case 0x03:
1300- packet_status = sl811_read_reg(SL811HS_USBA_HOSTSTATUS);
1373+ case 0x04:
13011374 if( packet_status & SL811HS_HOSTSTATUS_BIT_ACK ) {
13021375 remained_size -= packet_size;
13031376 ep_info[ep].toggle++;
13041377 if( remained_size > 0 ) write_proc = 0x01;
1305- else {
1306- ret = SUB_PROC_DONE;
1378+ else {
1379+#ifdef SL811HS_DEBUG_ON
1380+ printf(" Done.\r\n");
1381+#endif
1382+ ret = SUB_PROC_DONE;
13071383 write_proc = 0x00;
13081384 }
13091385 } else {
@@ -1314,30 +1390,37 @@
13141390 ret = SUB_PROC_ERROR;
13151391 break;
13161392 }
1317- return ret;
1318-}
1319-
1393+#ifdef SL811HS_DEBUG_ON
1394+ if(ret == SUB_PROC_ERROR) printf(" Fail\r\n");
1395+#endif
1396+ return ret;
1397+}
1398+
13201399 // -------------------------------------------
13211400 // [LOCAL] SL811 data receive process
13221401 // TODO: Maintainance of recovery sequence
13231402 // -------------------------------------------
1324-static SUB_PROC_RESULT sl811_read(unsigned char ep, unsigned char* data, int size)
1325-{
1326- static int timeout_counter;
1403+static SUB_PROC_RESULT sl811_read(unsigned char ep, unsigned char* data, int size)
1404+{
13271405 static int remained_size_r;
13281406 static unsigned char packet_size_r, retry_cnt;
1329- SUB_PROC_RESULT ret;
1330-
1407+ SUB_PROC_RESULT ret;
1408+
1409+
13311410 ret = SUB_PROC_BUSY;
1332-
1411+
13331412 switch( read_proc ) {
13341413 case 0x00:
13351414 retry_cnt = 0;
13361415 remained_size_r = size;
1416+ sl811_reg_wait_timer = 0;
13371417 if(size < 0) {
13381418 ret = SUB_PROC_ERROR;
13391419 break;
13401420 }
1421+#ifdef SL811HS_DEBUG_ON
1422+ printf("[USB] RD:");
1423+#endif
13411424 read_proc++;
13421425 // break;
13431426
@@ -1346,38 +1429,40 @@
13461429 if( remained_size_r == 0 ) packet_size_r = 0;
13471430 else if( ep_info[ep].size <= remained_size_r ) packet_size_r = ep_info[ep].size;
13481431 else packet_size_r = remained_size_r;
1349-
1350- sl811_write_reg(SL811HS_USBA_HOSTBASEADDR, 0x10);
1351- sl811_write_reg(SL811HS_USBA_HOSTBASEDLEN, packet_size_r);
1352- sl811_write_reg(SL811HS_USBA_HOSTDEVADDR, device_address);
1353- sl811_write_reg(SL811HS_USBA_HOSTPID, PID_IN | (ep_info[ep].address&0x0F) );
1354- timeout_counter = 200;
1432+
1433+ sl811_write_reg(SL811HS_USBA_HOSTBASEADDR, 0x10);
1434+ sl811_write_reg(SL811HS_USBA_HOSTBASEDLEN, packet_size_r);
1435+ sl811_write_reg(SL811HS_USBA_HOSTDEVADDR, device_address);
1436+ sl811_write_reg(SL811HS_USBA_HOSTPID, PID_IN | (ep_info[ep].address&0x0F) );
13551437 read_proc++;
13561438 //break;
13571439
13581440 case 0x02:
13591441 // SETUP command (IN) transfer
1360- sl811_write_reg(SL811HS_USBA_HOSTCTRL, ((ep_info[ep].toggle & 0x01) == 0) ? DATA0_RD : DATA1_RD);
1442+#ifdef SL811HS_DEBUG_ON
1443+ if ((ep_info[ep].toggle & 0x01) == 0) printf(" t0"); else printf(" t1");
1444+ printf("e");printf("%02s",hex2char[ep]);
1445+#endif
1446+ sl811_write_reg(SL811HS_USBA_HOSTCTRL, ((ep_info[ep].toggle & 0x01) == 0) ? DATA0_RD : DATA1_RD);
13611447 if( retry_cnt < SLOW_RETRY_TH ) {
1362- wait_100us(); wait_100us(); // 200us
1448+ sl811_reg_wait_timer = 5;
13631449 } else if( retry_cnt < RETRY_OUT ) {
1364- wait_100us(); wait_100us(); // 200us
1365- wait_100us(); wait_100us(); // 200us
1366- wait_100us(); wait_100us(); // 200us
1367- wait_100us(); wait_100us(); // 200us
1368- wait_100us(); wait_100us(); // 200us
1450+ sl811_reg_wait_timer = 50;
13691451 } else {
13701452 ret = SUB_PROC_ERROR;
13711453 }
1372- read_proc = 0x03;
1454+ read_proc = 0x03;
13731455 break;
13741456
13751457 case 0x03:
13761458 // Wait for transfer complete
1459+ if( sl811_reg_wait_timer ) break;
13771460 if(sl811_read_reg(SL811HS_STATUS) & SL811HS_INTENV_BIT_USBA) {
13781461 packet_status = sl811_read_reg(SL811HS_USBA_HOSTSTATUS);
1379-// timeout_counter--;
13801462 if(packet_status & SL811HS_HOSTSTATUS_BIT_NAK) {
1463+#ifdef SL811HS_DEBUG_ON
1464+ printf("/");
1465+#endif
13811466 retry_cnt++;
13821467 if( retry_cnt < RETRY_OUT ) {
13831468 read_proc = 0x02;
@@ -1385,6 +1470,9 @@
13851470 ret = SUB_PROC_ERROR;
13861471 }
13871472 } else {
1473+#ifdef SL811HS_DEBUG_ON
1474+ printf(".");
1475+#endif
13881476 read_proc++;
13891477 }
13901478 }
@@ -1398,8 +1486,11 @@
13981486 remained_size_r -= packet_size_r;
13991487 ep_info[ep].toggle++;
14001488 if( remained_size_r > 0 ) read_proc = 0x01;
1401- else {
1402- ret = SUB_PROC_DONE;
1489+ else {
1490+#ifdef SL811HS_DEBUG_ON
1491+ printf(" Done.\r\n");
1492+#endif
1493+ ret = SUB_PROC_DONE;
14031494 read_proc = 0x00;
14041495 }
14051496 } else {
@@ -1410,7 +1501,10 @@
14101501 ret = SUB_PROC_ERROR;
14111502 break;
14121503 }
1413- return ret;
1504+#ifdef SL811HS_DEBUG_ON
1505+ if(ret == SUB_PROC_ERROR) printf(" Fail\r\n");
1506+#endif
1507+ return ret;
14141508 }
14151509
14161510
--- trunk/src/fat.c (revision 6)
+++ trunk/src/fat.c (revision 7)
@@ -120,9 +120,11 @@
120120
121121 enum fat_error_code {
122122 FAT_ERR_NONE,
123- FAT_ILLEGAL_SECTOR_SIZE, // Corrently, olny 512 byte sector size can be supported.
124- FAT_ZERO_SECTOR_SIZE,
125- FAT_CAN_NOT_SUPPORT_FAT12,
123+ FAT_ILLEGAL_SECTOR_SIZE, // 0x01 Corrently, olny 512 byte sector size can be supported.
124+ FAT_ZERO_SECTOR_SIZE, // 0x02
125+ FAT_CAN_NOT_SUPPORT_FAT12, // 0x03
126+ FAT_ILLEGAL_MBR_SIGNATURE, // 0x04
127+ FAT_CAN_NOT_SUPPORT_NTFS, // 0x05
126128 FAT_PROCESS_ERROR = 100,
127129 };
128130
@@ -295,7 +297,17 @@
295297 case FAT_READ_MBR_02:
296298 if( usbms_status(CLASS_REQ_NONE) == CLASS_STS_READY ) {
297299 // **** Take in MBR information from buffer
298- MBR_info.fat_type = p_buffer_addr[450];
300+ if( WindowBuffer[510]!=0x55 || WindowBuffer[511]!=0xAA ) {
301+ fat_proc = FAT_ERROR_STOP;
302+ fat_error = FAT_ILLEGAL_MBR_SIGNATURE;
303+ break;
304+ }
305+ MBR_info.fat_type = WindowBuffer[450];
306+ if( MBR_info.fat_type == 0x07 ) {
307+ fat_proc = FAT_ERROR_STOP;
308+ fat_error = FAT_CAN_NOT_SUPPORT_NTFS;
309+ break;
310+ }
299311 MBR_info.fat_BPB_sector = WindowBuffer[454]
300312 + WindowBuffer[455] * 0x100
301313 + WindowBuffer[456] * 0x10000
@@ -345,9 +357,11 @@
345357 if( BPB_info.BytsPerSec == 0 ) {
346358 fat_error = FAT_ZERO_SECTOR_SIZE;
347359 fat_proc = FAT_ERROR_STOP;
360+ break;
348361 }else if( BPB_info.BytsPerSec != WINDOW_SIZE ) {
349362 fat_error = FAT_ILLEGAL_SECTOR_SIZE;
350363 fat_proc = FAT_ERROR_STOP;
364+ break;
351365 }
352366
353367 // **** Calculate file access parameters
--- trunk/src/main.c (revision 6)
+++ trunk/src/main.c (revision 7)
@@ -18,6 +18,26 @@
1818
1919 int counter_500us;
2020
21+const char hex2char[256][3] = {
22+ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F",
23+ "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1A", "1B", "1C", "1D", "1E", "1F",
24+ "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F",
25+ "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", "3C", "3D", "3E", "3F",
26+ "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F",
27+ "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F",
28+ "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A", "6B", "6C", "6D", "6E", "6F",
29+ "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7A", "7B", "7C", "7D", "7E", "7F",
30+ "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F",
31+ "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F",
32+ "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF",
33+ "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF",
34+ "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF",
35+ "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF",
36+ "E0", "E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF",
37+ "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF"
38+};
39+
40+
2141 // -------------------------------------------
2242 // Proto type definitions
2343 // -------------------------------------------
--- trunk/src/usb_ms.c (revision 6)
+++ trunk/src/usb_ms.c (revision 7)
@@ -264,6 +264,9 @@
264264 setup_fmt.wIndex = 0;
265265 setup_fmt.wLength = 1;
266266
267+#ifdef MSCLASS_DEBUG_ON
268+ printf("[MSC] GetMaxLUN\r\n");
269+#endif
267270 if( sl811_ctrl_receive(&setup_fmt) == CLASS_REQ_ACCEPTED )
268271 usbms_proc = USBMS_GET_MAX_LUN_02;
269272 else
@@ -305,6 +308,9 @@
305308 CmdBlkWrapper[14] = SCSI_LEN_TEST_UNIT_READY;
306309 CmdBlkWrapper[15] = SCSI_CMD_TEST_UNIT_READY;
307310
311+#ifdef MSCLASS_DEBUG_ON
312+ printf("[MSC] TUR\r\n");
313+#endif
308314 if( sl811_bulk_out_request(CmdBlkWrapper, CBW_SIZE) == CLASS_REQ_ACCEPTED )
309315 usbms_proc = USBMS_TESTUNIT_READY_02;
310316 else
@@ -361,6 +367,9 @@
361367 CmdBlkWrapper[15] = SCSI_CMD_REQUEST_SENSE; // 0x03
362368 CmdBlkWrapper[19] = SENSE_BUF_SIZE; // 0x0E
363369
370+#ifdef MSCLASS_DEBUG_ON
371+ printf("[MSC] GetSense\r\n");
372+#endif
364373 if( sl811_bulk_out_request(CmdBlkWrapper, CBW_SIZE) == CLASS_REQ_ACCEPTED )
365374 usbms_proc = USBMS_GET_SENSE_INFO_02;
366375 else
@@ -430,6 +439,9 @@
430439 CmdBlkWrapper[14] = SCSI_LEN_READ_CAPACITY;
431440 CmdBlkWrapper[15] = SCSI_CMD_READ_CAPACITY;
432441
442+#ifdef MSCLASS_DEBUG_ON
443+ printf("[MSC] ReadCapa\r\n");
444+#endif
433445 if( sl811_bulk_out_request(CmdBlkWrapper, CBW_SIZE) == CLASS_REQ_ACCEPTED )
434446 usbms_proc = USBMS_READ_CAPACITY_02;
435447 else
@@ -533,6 +545,9 @@
533545 CmdBlkWrapper[22] = (read10_param.transfer_length >> 8) & 0xFF;
534546 CmdBlkWrapper[23] = (read10_param.transfer_length >> 0) & 0xFF;
535547
548+#ifdef MSCLASS_DEBUG_ON
549+ printf("[MSC] READ10\r\n");
550+#endif
536551 if( sl811_bulk_out_request(CmdBlkWrapper, CBW_SIZE) == CLASS_REQ_ACCEPTED )
537552 usbms_proc = USBMS_READ10_02;
538553 else
--- trunk/src/common.h (revision 6)
+++ trunk/src/common.h (revision 7)
@@ -13,6 +13,11 @@
1313
1414 #include "3069s.h"
1515
16+#define DEBUG_ON
17+#ifdef DEBUG_ON
18+ #define SL811HS_DEBUG_ON
19+ #define MSCLASS_DEBUG_ON
20+#endif
1621 #define SUB_PROC_RESULT unsigned char
1722 enum sub_proc_result_code {
1823 SUB_PROC_BUSY,
@@ -42,6 +47,9 @@
4247 #define CLASS_STS_UNKNOWN 0xFF
4348
4449
50+#define printf ((int (*)(const char *,...))0x00002b84)
51+#define scanf ((int (*)(const char *,...))0x00002bd6)
4552
53+extern const char hex2char[256][3];
4654
4755 #endif
Show on old repository browser