whycyhl
2022-05-05 94861120b14313f81fc666e612c48c9e8a81f1e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "USBModule_Driver.h"
#include "Delay.h"
#include "STM32U.h"
#include "TFT_Driver.h"
#include "Define.h"
#include "DS1302_Driver.h"
#include <string.h>
#include <avr/wdt.h>
#include "CRC16.h"
#include "SAMD21U.h"
 
static unsigned char s_UDiskType = Def_UDisk_Ch376;
unsigned char countbak = 0;
 
unsigned char ReadUDiskType(void)
{
    unsigned char VerCMD[8] = {0x57,0xAB,0x01,0x04,0x00,0x00,0x00,0x00};
 
    unsigned int crc = CalCRC16(&VerCMD[2], 4);
    VerCMD[6] = crc&255;
    VerCMD[7] = crc>>8;
    
    Config_M64USART1(1152);
    Delay_MS(1);
    
    unsigned char i = 5;
    while(i) 
    {
        SendSTR_M64USART1( VerCMD, 8 );
        s_UDiskType = ReceiveByte_M64USART2();
        if(s_UDiskType != 0xff)
        {
             break;
        }
        i--;
        wdt_reset();         //ι¹·
        _delay_ms(100);
    }    
//    PrintValue(0, 0, i, " ", 16,  300, 300, blue, white);
    countbak = i;
    
    if((s_UDiskType & 0xc0)==0x80)
        s_UDiskType = Def_UDisk_STM32U;
    else if((s_UDiskType & 0xc0)==0x40)
        s_UDiskType = Def_UDisk_Ch376;
    else 
        s_UDiskType = Def_UDisk_SAMD21U;
        
   
    return s_UDiskType;
}
 
unsigned char getUDiskType(void)
{
    return s_UDiskType;
}
 
unsigned char mInitCHSTM32UDisk(void)
{
    unsigned char Tab[6] = {0x57,0xAB,0x06,0x04,0x00,0x00};
    unsigned char temp;
    
    SendSTR_M64USART1( Tab, 6 );
    temp = ReceiveByte_M64USART1();    
    
    if(0xFB == temp)
        return pdTRUE;
    
    return pdFALSE;
}
 
unsigned char saveSTM32UDiskBaund(unsigned short baund)
{
    unsigned char Tab[6] = {0x57,0xAB,0x02,0x04,0xCC,0x03};
    unsigned char temp=0;
    
    if(96 == baund)
    {
        Tab[4] = 0xB2;
        Tab[5] = 0x02;
    }
    else if(192 == baund)
    {
        Tab[4] = 0xD9;
        Tab[5] = 0x02;
    }
    else if(576 == baund)
    {
        Tab[4] = 0x98;
        Tab[5] = 0x03;
    }
    else if(1152 == baund)
    {
        Tab[4] = 0xCC;
        Tab[5] = 0x03;
    }
    
    SendSTR_M64USART1( Tab, 6 );
    temp = ReceiveByte_M64USART1();    
    
    if(0x51 == temp)
        return pdTRUE;
        
    return pdFALSE;
}
 
unsigned char checkDiskConnect(void)
{
    unsigned char Tab[13] = {0x57,0xAB,0x30,0x0A,0x00,0x00};
    unsigned char temp = 0;
    Date_Time mDateTime;
    GetDateTime(&mDateTime);
    //PrintTime(&mDateTime, ':', YYMDHMS,0,0, blue, white);
    
    Tab[6]  = 20+mDateTime.year;//µ±Ç°Äê·Ý-1980   20´ú±í2000Äê
    Tab[7]  = mDateTime.month;    //1ÔÂ
    Tab[8]  = mDateTime.day;    //1ÈÕ
    Tab[9]  = mDateTime.hour;    //1ʱ
    Tab[10] = mDateTime.minute;    //1·Ö
    Tab[11] = mDateTime.second;    //1Ãë
 
    SendSTR_M64USART1( Tab, 12 );
    
    temp = ReceiveByte_M64USART1();    
    
    if(0x15 == temp)
        return pdTRUE;
        
    return pdFALSE;
}
 
unsigned char STM32U_creatFile(unsigned char *pLongName, unsigned short length)
{
    unsigned char LongNameBuf[128] = {    0x57,0xAB,0x34,0x13,0x0F,0x00,
                                        '2',':','\\','F',
                                        '2','0','1','6',
                                                '-','0','1',
                                                '-','1','9',0x00,
                                        0x20,0x31,0x34,0x3A,0x33,0x32,0x3A,0x30,0x30,0x2E,
                                        0x46,0x42,0x4F,0x00}; 
    unsigned char temp=66;
    
    LongNameBuf[3] = (length+3) + 4;
    LongNameBuf[4] = (length+3) % 256;
    LongNameBuf[5] = (length+3) / 256;
    
    memcpy(&LongNameBuf[9], (char*)pLongName, length);
    
    SendSTR_M64USART1( LongNameBuf, length+9 );
    temp = ReceiveByte_M64USART1();    
 
    
    if( 0 == temp )
    {
        STM32U_closeFile();////2016-4-11ÐÞ¸Ä
        return pdTRUE;
    }
        
    return pdFALSE;
}
 
unsigned char STM32U_OpenFile(unsigned char *pLongName, unsigned short length)
{
    unsigned char LongNameBuf[128] = {    0x57,0xAB,0x34,0x13,0x0F,0x00,
                                        '2',':','\\','F',
                                        '2','0','1','6',
                                                '-','0','1',
                                                '-','1','9',0x00,
                                        0x20,0x31,0x34,0x3A,0x33,0x32,0x3A,0x30,0x30,0x2E,
                                        0x46,0x42,0x4F,0x00}; 
    unsigned char temp=66;
    
    LongNameBuf[3] = (length+3) + 4;
    LongNameBuf[4] = (length+3) % 256;
    LongNameBuf[5] = (length+3) / 256;
    
    memcpy(&LongNameBuf[9], pLongName, length);
    
    SendSTR_M64USART1( LongNameBuf, length+9 );
    
    temp = ReceiveByte_M64USART1();    
    
    if( 0 == temp )
        return pdTRUE;
 
    return pdFALSE;
}
 
unsigned char STM32U_closeFile(void)
{
    unsigned char Tab[6]={0x57,0xAB,0x36,0x04,0x00,0x00};
    unsigned char temp = 66;
    
    SendSTR_M64USART1( Tab, 6 );
    temp = ReceiveByte_M64USART1();    
    
    if( 0 == temp )
        return pdTRUE;
        
    return pdFALSE;
}
 
unsigned char creatFileManage(unsigned char *pLongName, unsigned short length)
{
    unsigned char temp = pdTRUE;
    Config_M64USART1(1152);
    Delay_MS(1);
    
    if( pdFALSE == mInitCHSTM32UDisk() )        //ÊÇ·ñ½¨Á¢Í¨ÐÅ
    {
        Config_M64USART1(192);
        return 100;
    }
    Delay_MS(1);
    if( pdFALSE == checkDiskConnect() )        //Á´½ÓUÅÌ
    {
        Config_M64USART1(192);
        return 101;
    }
    Delay_MS(1);
    if( pdFALSE == STM32U_creatFile(pLongName, length) )
    {
        Config_M64USART1(192);
        return 102;
    }
    Delay_MS(10);
    if( pdFALSE == STM32U_OpenFile(pLongName, length) )
    {
        Config_M64USART1(192);
        return 103;
    }
    Delay_MS(1000);
    wdt_reset(); //ι¹·    
    /*STM32U_writeFile(&temp,1);
    
    Delay_MS(150);
    if( pdFALSE == STM32U_closeFile() )
    {
        Config_M64USART1(192);
        return 103;
    }*/
    
    return temp;
}
 
unsigned char STM32U_writeFile(volatile void *data, unsigned short length)
{
    unsigned short temp = 8; 
    unsigned char Tab[600] = {     0x57,0xAB,0x4C,0x04,0x08,0x00,
                                0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38};
    
    Tab[4] = length % 256;
    Tab[5] = length / 256;
    
    memcpy(&Tab[6], (unsigned char*)data, length);
 
    SendSTR_M64USART1( Tab, 6 );
    _delay_ms(10);
    SendSTR_M64USART1( &Tab[6], length );
    
    temp = ReceiveByte_M64USART1();    
    temp = temp+256*ReceiveByte_M64USART1();    
    
    //PrintValue(0, 0,  temp, "C", 16, 0, 24, blue, red);
    _delay_ms(5);
    
    if( length == temp )
        return pdTRUE;
        
    return pdFALSE;
}