/************************** 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;
|
}
|