Subversion Repositories pentevo

Rev

Rev 1134 | Rev 1161 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1134 Rev 1140
Line 6... Line 6...
6
#include "vars.h"
6
#include "vars.h"
7
#include "sdcard.h"
7
#include "sdcard.h"
8
 
8
 
9
#include "util.h"
9
#include "util.h"
10
 
10
 
11
typedef ULONG (WINAPI *CH341GetVersion_t)(); // Get the DLL version number and return the version number
-
 
12
static CH341GetVersion_t CH341GetVersion = nullptr;
-
 
13
typedef ULONG (WINAPI *CH341GetDrvVersion_t)(); // Get the DLL version number and return the version number
-
 
14
static CH341GetDrvVersion_t CH341GetDrvVersion = nullptr;
-
 
15
typedef HANDLE (WINAPI *CH341OpenDevice_t)(ULONG);
-
 
16
static CH341OpenDevice_t CH341OpenDevice = nullptr;
-
 
17
typedef VOID (WINAPI *CH341CloseDevice_t)(ULONG);
-
 
18
static CH341CloseDevice_t CH341CloseDevice = nullptr;
-
 
19
typedef BOOL (WINAPI *CH341StreamSPI4_t)(
-
 
20
        ULONG                   iIndex,  // Specify CH341 device serial number
-
 
21
        ULONG                   iChipSelect,  // Chip select control, if bit 7 is 0, the chip select control is ignored, if bit 7 is 1, the parameters are valid: Bit 1 and bit 0 are 00/01/10, respectively, select the D0/D1/D2 pin as low level. valid chip select
-
 
22
        ULONG                   iLength,  // Number of data bytes to be transmitted
-
 
23
        PVOID                   ioBuffer );
-
 
24
static CH341StreamSPI4_t CH341StreamSPI4 = nullptr;
-
 
25
typedef BOOL (WINAPI *CH341Set_D5_D0_t)(ULONG,ULONG,ULONG);
-
 
26
static CH341Set_D5_D0_t CH341Set_D5_D0 = nullptr;
-
 
27
typedef PVOID   (WINAPI *CH341GetDeviceName_t)(ULONG);
-
 
28
static CH341GetDeviceName_t CH341GetDeviceName = nullptr;
-
 
29
typedef BOOL    (WINAPI *CH341SetStream_t)(ULONG,ULONG);// To specify the mode, see Downlink
-
 
30
// Bit 1-bit 0: I2C interface speed /SCL frequency, 00= low speed /20KHz,01= standard /100KHz(default),10= fast /400KHz,11= high speed /750KHz
-
 
31
// Bit 2: SPI I/O number /IO pins, 0= single in/single out (D3 clock /D5 out /D7 in)(default),1= double in/double out (D3 clock /D5 out D4 out /D7 in D6 in)
-
 
32
// Bit 7: Bit order in SPI bytes, 0= low first, 1= high first
-
 
33
// All other reservations must be 0
-
 
34
static CH341SetStream_t CH341SetStream = nullptr;
-
 
35
 
-
 
36
static HMODULE spiDll = nullptr;
-
 
37
//static int SpiDllInited = 0;
-
 
38
static HANDLE ch341 = NULL;
-
 
39
 
-
 
40
static u8 spiData = 0xff;
-
 
41
 
-
 
42
//-----------------------------------------------------------------------------
11
//-----------------------------------------------------------------------------
43
 
12
 
44
void TSdCard::Reset()
13
void TSdCard::Reset()
45
{
14
{
46
//    printf(__FUNCTION__"\n");
15
//    printf(__FUNCTION__"\n");
Line 107... Line 76...
107
{
76
{
108
    static u32 WrPos = -1U;
77
    static u32 WrPos = -1U;
109
    TState NextState = ST_IDLE;
78
    TState NextState = ST_IDLE;
110
//    printf(__FUNCTION__" Val = %X\n", Val);
79
//    printf(__FUNCTION__" Val = %X\n", Val);
111
   
80
 
112
    if ( IsCh341 != 0 )
81
    if (!Image)
113
    {
-
 
114
        spiData = Val;
82
        return;
115
        CH341StreamSPI4(0, 0, 1, &spiData);
-
 
116
    }
-
 
117
    else if ( ( Image != 0 ) && ( ( spiCfg & 0x02) == 0 ) )
-
 
118
    {
-
 
119
 
83
 
120
        switch(CurrState)
84
    switch(CurrState)
121
        {
85
    {
122
            case ST_IDLE:
86
        case ST_IDLE:
123
            //case ST_WR_DATA_SIG:
87
        //case ST_WR_DATA_SIG:
Line 278... Line 242...
278
                return;
242
            return;
279
        }
243
    }
280
 
244
 
281
        CurrState = NextState;
245
    CurrState = NextState;
282
    }
246
}
283
}
-
 
284
 
247
 
285
//-----------------------------------------------------------------------------
248
//-----------------------------------------------------------------------------
286
 
249
 
287
u8 TSdCard::Rd()
250
u8 TSdCard::Rd()
288
{
251
{
289
    if( IsCh341 != 0 )
-
 
290
    {
-
 
291
        // u8 ret_spi = spiData;
-
 
292
        // spiData = 0xff;
-
 
293
        // CH341StreamSPI4(0, 0, 1, &spiData);
-
 
294
        return spiData;
-
 
295
    }
-
 
296
//    printf(__FUNCTION__" cmd=0x%X, St=0x%X\n", Cmd, CurrState);
252
//    printf(__FUNCTION__" cmd=0x%X, St=0x%X\n", Cmd, CurrState);
-
 
253
    if (!Image)
297
    else if ( ( Image != 0 ) && ( ( spiCfg & 0x02) == 0 ) )
254
        return 0xFF;
298
    {
255
 
299
        switch(Cmd)
256
    switch(Cmd)
300
        {
257
    {
301
        case CMD_GO_IDLE_STATE:
258
    case CMD_GO_IDLE_STATE:
302
            if (CurrState == ST_R1)
259
        if (CurrState == ST_R1)
303
            {
260
        {
Line 605... Line 562...
605
        if (CurrState == ST_R1) // CMD_INVALID
562
    if (CurrState == ST_R1) // CMD_INVALID
606
        {
563
    {
607
            CurrState = ST_IDLE;
564
        CurrState = ST_IDLE;
608
            return 0x05;
565
        return 0x05;
609
        }
566
    }
610
    }
-
 
611
 
567
 
612
    return 0xFF;
568
    return 0xFF;
613
}
569
}
614
 
570
 
615
//-----------------------------------------------------------------------------
571
//-----------------------------------------------------------------------------
Line 663... Line 619...
663
 
619
 
664
 
620
 
665
void TSdCard::Open(const char *Name)
621
void TSdCard::Open(const char *Name)
666
{
622
{
667
//    printf(__FUNCTION__"\n");
623
//    printf(__FUNCTION__"\n");
668
 
-
 
669
    if ( strcmp(Name, "<CH341>") == 0 )
-
 
670
    {
-
 
671
        IsCh341 = 1;
-
 
672
        if ( ch341 == NULL )
-
 
673
        {
-
 
674
            // ULONG iDevIndex = 0;
-
 
675
            if( CH341GetVersion == nullptr )
-
 
676
            {
-
 
677
#ifdef __amd64__
-
 
678
                spiDll = LoadLibrary("CH341DLLA64.DLL");
-
 
679
#else
-
 
680
                spiDll = LoadLibrary("CH341DLL.DLL");
-
 
681
#endif
-
 
682
                if (!spiDll)
-
 
683
                {
-
 
684
#ifdef __amd64__
-
 
685
                    errmsg("failed to load CH341DLLA64.DLL");
-
 
686
#else
-
 
687
                    errmsg("failed to load CH341DLL.DLL");
-
 
688
#endif
-
 
689
                    err_win32();
-
 
690
                    exit(1);
-
 
691
                }
-
 
692
               
-
 
693
                CH341GetVersion = (CH341GetVersion_t)GetProcAddress(spiDll, "CH341GetVersion");
-
 
694
                CH341GetDrvVersion = (CH341GetDrvVersion_t)GetProcAddress(spiDll, "CH341GetDrvVersion");
-
 
695
                CH341OpenDevice = (CH341OpenDevice_t)GetProcAddress(spiDll, "CH341OpenDevice");
-
 
696
                CH341CloseDevice = (CH341CloseDevice_t)GetProcAddress(spiDll, "CH341CloseDevice");
-
 
697
                CH341StreamSPI4 = (CH341StreamSPI4_t)GetProcAddress(spiDll, "CH341StreamSPI4");
-
 
698
                CH341Set_D5_D0 = (CH341Set_D5_D0_t)GetProcAddress(spiDll, "CH341Set_D5_D0");
-
 
699
                CH341GetDeviceName = (CH341GetDeviceName_t)GetProcAddress(spiDll, "CH341GetDeviceName");
-
 
700
                CH341SetStream = (CH341SetStream_t)GetProcAddress(spiDll, "CH341SetStream");
-
 
701
            }
-
 
702
            printf("CH341 version: %lu\r\n", CH341GetVersion( ));
-
 
703
           
-
 
704
            char * n = (char*)CH341GetDeviceName(0);
-
 
705
            if ( n != 0 )printf("CH341 name: %s\r\n", n);
-
 
706
           
-
 
707
            if ( CH341OpenDevice == 0 )
-
 
708
            {
-
 
709
                errmsg("failed to load CH341OpenDevice");
-
 
710
                err_win32();
-
 
711
                exit(1);
-
 
712
            }
-
 
713
            ch341 = CH341OpenDevice(0);
-
 
714
               
-
 
715
            if ( ch341 == 0 ){
-
 
716
                errmsg("OpenDevice ch341 failed");
-
 
717
                err_win32();
-
 
718
                exit(1);
-
 
719
            }
-
 
720
            printf("CH341 drv version: %lu\r\n", CH341GetDrvVersion( ));
-
 
721
           
-
 
722
            CH341SetStream(0, 0x80);
-
 
723
            CH341Set_D5_D0(0, 0x29, 0x21);
-
 
724
            spiCfg = 0x01;
-
 
725
           
-
 
726
            CH341Set_D5_D0(0, 0x29, 0x20);
-
 
727
            u8 spi_str[20] = "DimkaM";
-
 
728
            CH341StreamSPI4(0, 0x00, sizeof(spi_str), &spi_str);
-
 
729
            CH341Set_D5_D0(0, 0x29, 0x21);
-
 
730
        }
-
 
731
    }
-
 
732
    else
-
 
733
    {
-
 
734
        IsCh341 = 0;
-
 
735
       
-
 
736
        assert(!Image);
624
    assert(!Image);
737
        Image = fopen(Name, "r+b");
625
    Image = fopen(Name, "r+b");
738
        if(!Image)
626
    if(!Image)
739
        {
627
    {
740
            if(Name[0])
628
        if(Name[0])
Line 743... Line 631...
743
            }
631
        }
744
            return;
632
        return;
745
        }
633
    }
746
        ImageSize = u32(_filelengthi64(fileno(Image)) / (512 * 1024)) - 1;
634
    ImageSize = u32(_filelengthi64(fileno(Image)) / (512 * 1024)) - 1;
747
        UpdateCsdImageSize();
635
    UpdateCsdImageSize();
748
       
-
 
749
        spiCfg = 0x01;
-
 
750
    }
-
 
751
}
-
 
752
 
-
 
753
//-----------------------------------------------------------------------------
-
 
754
 
-
 
755
void TSdCard::Cs(u8 state)
-
 
756
{
-
 
757
   
-
 
758
    if( ( state & 3 ) != spiCfg )
-
 
759
    {
-
 
760
        spiCfg = state & 3;
-
 
761
       
-
 
762
        if ( IsCh341 != 0 )
-
 
763
        {
-
 
764
            CH341Set_D5_D0(0, 0x29, ((spiCfg>>1) & 0x01)|0x20);
-
 
765
        }
-
 
766
    }
-
 
767
}
636
}
768
 
637
 
769
//-----------------------------------------------------------------------------
638
//-----------------------------------------------------------------------------
770
 
639
 
771
void TSdCard::Close()
640
void TSdCard::Close()
772
{
641
{
773
    if ( Image != 0 )
642
    if (Image)
774
    {
643
    {
775
        fclose(Image);
644
        fclose(Image);
776
        Image = 0;
645
        Image = 0;
777
    }
646
    }
778
   
-
 
779
    if ( ch341 != 0 )
-
 
780
    {
-
 
781
        CH341CloseDevice(0);
-
 
782
        ch341 = 0;
-
 
783
        IsCh341 = 0;
-
 
784
    }
-
 
785
}
647
}
786
 
648
 
787
//-----------------------------------------------------------------------------
649
//-----------------------------------------------------------------------------
788
 
650
 
789
TSdCard SdCard;
651
TSdCard SdCard;