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
#include "USBModule_Driver.h"
#include "Delay.h"
#include "SAMD21U.h"
#include "TFT_Driver.h"
#include "Define.h"
#include "DS1302_Driver.h"
#include <string.h>
#include <avr/wdt.h>
#include "STM32U.h"
#include "CRC16.h"
 
 
extern unsigned char s_UDiskType ;
 
unsigned char mInitSAMD21UDisk(void)
{
    unsigned char Tab[8] = {0x57,0xAB,0x06,0x04,0x00,0x00,0x00,0x00};
    unsigned char temp;
    
    unsigned int crc = CalCRC16(&Tab[2], 4);
    Tab[6] = crc&255;
    Tab[7] = crc>>8;
    
    SendSTR_M64USART1( Tab, 8 );
 
    temp = ReceiveByte_M64USART1_21();    
    
    if(0xFB == temp)
        return pdTRUE;
    
    return pdFALSE;
}
 
unsigned char saveSAMD21UDiskBaund(unsigned short baund)
{
    unsigned char Tab[8] = {0x57,0xAB,0x02,0x04,0xCC,0x03,0x00,0x00};
    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;
    }
    
    unsigned int crc = CalCRC16(&Tab[2], 4);
    Tab[6] = crc&255;
    Tab[7] = crc>>8;
    
    SendSTR_M64USART1( Tab, 8 );
 
    temp = ReceiveByte_M64USART1_21();    
    
    if(0x51 == temp)
        return pdTRUE;
        
    return pdFALSE;
}
 
unsigned char checkDiskConnect_21(void)
{
    unsigned char Tab[14] = {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Ãë
    
    unsigned int crc = CalCRC16(&Tab[2],10);
    Tab[12] = (unsigned char)( crc & 0x00ff);
    Tab[13] = (unsigned char)( (crc >>8) & 0x00ff);
 
    SendSTR_M64USART1( Tab, 14 );
    
    temp = ReceiveByte_M64USART1();    
    
    if(0x15 == temp)
        return pdTRUE;
        
    return pdFALSE;
}
 
unsigned char SAMD21U_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);
    
    unsigned int crc = CalCRC16(&LongNameBuf[2],length+7);
    LongNameBuf[length+9] = (unsigned char)( crc & 0x00ff);
    LongNameBuf[length+10] = (unsigned char)( (crc >>8) & 0x00ff);
    
    SendSTR_M64USART1( LongNameBuf, length+11 );
    temp = ReceiveByte_M64USART1_21();    //delay 900mS
 
    
    if( 0 == temp )
    {
        SAMD21U_closeFile();////2016-4-11ÐÞ¸Ä
        return pdTRUE;
    }
        
    return pdFALSE;
}
 
unsigned char SAMD21U_OpenFile(unsigned char *pLongName, unsigned short length)
{
    unsigned char LongNameBuf[128] = {    0x57,0xAB,0x32,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);
    
    unsigned int crc = CalCRC16(&LongNameBuf[2],length+7);
    LongNameBuf[length+9] = (unsigned char)( crc & 0x00ff);
    LongNameBuf[length+10] = (unsigned char)( (crc >>8) & 0x00ff);
    
    SendSTR_M64USART1( LongNameBuf, length+11 );
    temp = ReceiveByte_M64USART1_21();    //delay 900mS
    
    if( 0 == temp )
        return pdTRUE;
 
    return pdFALSE;
}
 
unsigned char SAMD21U_closeFile(void)
{
    unsigned char Tab[8]={0x57,0xAB,0x36,0x04,0x00,0x00,0x00,0x00};
    unsigned char temp = 66;
    
    unsigned int crc = CalCRC16(&Tab[2], 4);
    Tab[6] = crc&255;
    Tab[7] = crc>>8;
    
    SendSTR_M64USART1( Tab, 8 );
 
    temp = ReceiveByte_M64USART1();    
    
    if( 0 == temp )
        return pdTRUE;
        
    return pdFALSE;
}
 
unsigned char creatFileManage_21(unsigned char *pLongName, unsigned short length)
{
    unsigned char temp = pdTRUE;
//    Config_M64USART1(1152);
//    Delay_MS(1);
    
    if( pdFALSE == mInitSAMD21UDisk() )        //ÊÇ·ñ½¨Á¢Í¨ÐÅ
    {
//        Config_M64USART1(192);
        return 100;
    }
    Delay_MS(1);
    if( pdFALSE == checkDiskConnect_21() )        //Á´½ÓUÅÌ
    {
//        Config_M64USART1(192);
        return 101;
    }
    Delay_MS(1);
    if( pdFALSE == SAMD21U_creatFile(pLongName, length) )
    {
//        Config_M64USART1(192);
        return 102;
    }
    Delay_MS(10);
    if( pdFALSE == SAMD21U_OpenFile(pLongName, length) )
    {
//        Config_M64USART1(192);
        return 103;
    }
    Delay_MS(1000);
    wdt_reset(); //ι¹·    
    
    return temp;
}
 
unsigned char SAMD21U_writeFile(volatile void *data, unsigned short length, unsigned short n)
{
    static unsigned short temp ; 
    unsigned char res;
    unsigned char Tab[600] = {     0x57,0xAB,0x4C,0x04,0x08,0x00,
                                0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38};
    
    Tab[3] = (unsigned char)n;
    Tab[4] = length % 256;
    Tab[5] = length / 256;
    
    memcpy(&Tab[6], (unsigned char*)data, length);
    
    unsigned int crc = CalCRC16(&Tab[2],length+4);
    Tab[length+6] = (unsigned char)( crc & 0x00ff);
    Tab[length+7] = (unsigned char)( (crc >>8) & 0x00ff);
 
//    SendSTR_M64USART1( Tab, 6 );
//    _delay_ms(10);
    SendSTR_M64USART1( Tab, length+8 );
    
    res = ReceiveSTR_M64USART1((unsigned char *)&temp, 2, 500000);//ReceiveByte_M64USART1_21();    
    
    _delay_ms(5);
    
    if(res==2&& temp==length)
        return pdTRUE;
    else    
        return pdFALSE;
}