whycyhl
2022-05-05 683c912f9ac1935a7b109fb402bb276dba450f6b
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
/************************** Copyright (c) **********************************
**                 FUZHOU FUGUANG ELECTRONICS Co.,LTD.
**                        ¸£Öݸ£¹âµç×ÓÓÐÏÞ¹«Ë¾
**                       http://www.fuguang.com
**
**-------------- File Info -------------------------------------------------
** File name:            USBModule_Driver.c
** Last modified Date:  2008-11-16
** Last Version:        1.0
** Descriptions:        USBModule Drivers lib
**
**--------------------------------------------------------------------------
** Created by:            mxpopstar
** Created date:        2008-11-16
** Version:                1.0
** Descriptions:        The original version
**
**--------------------------------------------------------------------------
** Modified by:            mxpopstar
** Modified date:        2008-11-20
** Version:
** Descriptions:        USBModule Drivers lib
**
***************************************************************************/
//
 
#include "MessageBox.h"
#include "USBModule_Driver.h"
#include "ch376inc.h"
#include "Delay.h"
#include "M64USART_Driver.h"
#include <avr/wdt.h>
 
 
void InitIO_M64USART1(void)
{
    DDRD &= (~0x04); //RXD1¶ËÅäÖÃΪÊäÈë
    DDRD |= 0x08;      //TXD1¶ËÅäÖÃΪÊä³ö
}
//
/////////////////////////´Ë´¦ÒÔÏÂÓÉCH376°æÌæ»»¶øÀ´////////////////////////
void Config_M64USART1(const unsigned int baud)
{
    UCSR1B = 0;
    _delay_us(10);
    
    if(baud == 192)
    {
        UBRR1H = 0;
        UBRR1L = 23; //7.3728M¾§Õñ,19200 ²¨ÌØÂÊ
    }
    else if(baud == 576)
    {
        UBRR1H = 0;
        UBRR1L = 7; //7.3728M¾§Õñ,57600 ²¨ÌØÂÊ
    }
    else if(baud == 1152)
    {
        UBRR1H = 0;
        UBRR1L = 3; //7.3728M¾§Õñ,115200 ²¨ÌØÂÊ
    }
    
    UCSR1B = (1<<RXEN1)|(1<<TXEN1);//USAR1½ÓÊÕÆ÷Óë·¢ËÍÆ÷ʹÄÜ
    
    _delay_ms(10);
}
//
 
void ClearUSART1RXBuffer(void)
{
    unsigned char res = 0;
    res = UDR1;
    res = UDR1;//Çå¿ÕUDR0µÄÄÚÈÝ
}
//
 
//--------------------------------------------------------------------------------------------------
void RestartM64USART1(void)
{
    UCSR1B = 0;
    _delay_us(100);
    UCSR1B = (1<<RXEN1)|(1<<TXEN1);//USAR1½ÓÊÕÆ÷Óë·¢ËÍÆ÷ʹÄÜ
    _delay_us(10);
}
//--------------------------------------------------------------------------------------------------
void TXByteToCH376(const unsigned char byte)
{
    while ( !( UCSR1A & (1<<UDRE1)) )
        ;
    UDR1 = byte;
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
unsigned char RXByte_M64USART1(unsigned char *error, unsigned int timeup_us)
{
    while ( !(UCSR1A & (1<<RXC1)) )
    {
        _delay_us(1);
        if(timeup_us-- < 2)
        {
            *error = 1;
            return 0xFF;
        }
    }
    
    *error = 0;
    return UDR1; /* ´Ó»º³åÆ÷ÖлñÈ¡²¢·µ»ØÊý¾Ý*/ 
}
//--------------------------------------------------------------------------------------------------
unsigned char RXByteFromCH376(unsigned char *res)
{
    unsigned int count = 0;
    *res = 0;
    wdt_reset(); //ι¹·
    while ( !(UCSR1A & (1<<RXC1)) )
    {
        count++;
        _delay_us(1);
        if(count > 50000)
        {
            count = 0;
            *res = 1;
            return 0xFF;
        }
    }
    
    return UDR1; /* ´Ó»º³åÆ÷ÖлñÈ¡²¢·µ»ØÊý¾Ý*/ 
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void xWriteCH376Cmd(const unsigned char mCmd )        /* ÏòCH376дÃüÁî */
{
    TXByteToCH376(SER_SYNC_CODE1);
    TXByteToCH376(SER_SYNC_CODE2);
    TXByteToCH376(mCmd);
}
//--------------------------------------------------------------------------------------------------
void xWriteCH376Data(const unsigned char mData )    /* ÏòCH376дÊý¾Ý */
{
    TXByteToCH376(mData);
}
//--------------------------------------------------------------------------------------------------
unsigned char xReadCH376Data( void )                /* ´ÓCH376¶ÁÊý¾Ý */
{
    unsigned char data = 0;
    unsigned char res = 0;
    data = RXByteFromCH376(&res);
    if(res == 0) return data;
    else return( 0 );                                // ²»Ó¦¸Ã·¢ÉúµÄÇé¿ö
}
//--------------------------------------------------------------------------------------------------
unsigned char Query376Interrupt( void )                // ²éѯCH376ÖжÏ״̬
{
    unsigned char Int_data;
    
    if(UCSR1A & (1<<RXC1))                            /* µÈ´ý½ÓÊÕÖжϽÓÊÕÊý¾Ý*/
    {
        Int_data = UDR1;
        Int_data = UDR1;
        return( 1 );
    }
 
    return( 0 );
}
//--------------------------------------------------------------------------------------------------
unsigned char mInitCH376Host( void )        //ÔÚʹÓÃCH376ǰҪ¶ÔËü½ø³õʼ»¯CH376
{
    unsigned char res;
 
    xWriteCH376Cmd(CMD11_CHECK_EXIST);        //²âÊÔµ¥Æ¬»úÓëCH376Ö®¼äµÄͨѶ½Ó¿Ú
    xWriteCH376Data(0xAA);
    res = xReadCH376Data();
    if (res != (0x55)) 
    {
        return( ERR_USB_UNKNOWN );            //ͨѶ½Ó¿Ú²»Õý³£,¿ÉÄÜÔ­ÒòÓÐ:½Ó¿ÚÁ¬½ÓÒì³£,ÆäËüÉ豸ӰÏì(Ƭѡ²»Î¨Ò»),´®¿Ú²¨ÌØÂÊ,Ò»Ö±ÔÚ¸´Î»,¾§Õñ²»¹¤×÷
    }
    
    res=0;
    xWriteCH376Cmd( CMD11_SET_USB_MODE );    //É豸USB¹¤×÷ģʽ
    xWriteCH376Data( 0x06 );                //¶ÔUÅ̲Ù×÷ģʽ´úÂëΪ06H,·ñÔò²»ÄÜͨÐÅ
    
    _delay_ms(50);
    res = xReadCH376Data( );
    if ( res == CMD_RET_SUCCESS )
    {
        return( USB_INT_SUCCESS );
    }
    else return( ERR_USB_UNKNOWN );            //ÉèÖÃģʽ´íÎó
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void SendSTR_M64USART1(volatile const void *data, unsigned int count)
{
    unsigned char *pbuf = (unsigned char *)data;
    while(count--)
    {
        while ( !( UCSR1A & (1<<UDRE1)) )
            ;
            
        UDR1 = *pbuf++;
    }
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
unsigned char ReceiveByte_M64USART1(void)
{
    unsigned long count = 0;
    while ( !(UCSR1A & (1<<RXC1)) )
    {
        count++;
        wdt_reset(); //ι¹·
        if(count>500000)
        {
            count = 0;
            return 0xFF;
        }
    }
    return UDR1;
}
//--------------------------------------------------------------------------------------------------
unsigned char ReceiveSTR_M64USART1(void *buf, const unsigned char len, const unsigned long timeup)
{
    unsigned char *pbuf = (unsigned char *)buf;
    unsigned char bytecount = len;
    unsigned long count = 0;
    while(bytecount--)
    {
        while ( !(UCSR1A & (1<<RXC1)) )
        {
            count++;
            wdt_reset(); //ι¹·
            if(count > timeup)
            {
                count = 0;
                return 0;
            }
        }
        
        *pbuf++ = UDR1;
    }
    
    return len;
}
//------------------------------------------------------------------------
unsigned char ReceiveByte_M64USART1_21(void)
{
    unsigned long count = 0;
    while ( !(UCSR1A & (1<<RXC1)) )
    {
        count++;
        wdt_reset(); //ι¹·
        if(count>50000000)
        {
            count = 0;
            return 0xFF;
        }
    }
    return UDR1;
}