⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb_cdc_uart.c

📁 USB CDC and HID composite unit.
💻 C
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// USB_CDC_UART.c
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include "USB_Type.h"
#include "USB_Configuration.h"
#include "USB_Register.h"
#include "USB_Standard_Requests.h"
#include "USB_ISR.h"
#include "FIFO_RW.h"
#include "USB_CDC_UART.h"

//-----------------------------------------------------------------------------
// Constant
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------

TLine_Coding idata uart_line_coding = {		// line coding structure and its default value
	115200,		// baudrate
	0,			// stop bit:	1
	0,			// parity:		none
	8			// data bits:	8
};

bit          cs_Line_State_Update;			// update line state

volatile bit uart_DTR = FALSE;				// Line status output
volatile bit uart_RTS = FALSE;

static bit cs_Bulk_IN_full_packet = FALSE;	// the last packet was full-length packet


//
// TX and RX buffers
//
Ttxbuffer TXBuffer[ TXBUFSIZE ];			// Ring buffer for TX and RX
Trxbuffer RXBuffer[ RXBUFSIZE ];
static Ttxcnt TXRDIdx;						// index for ring buffer
static Ttxcnt TXWTIdx;
static Trxcnt RXRDIdx;
static Trxcnt RXWTIdx;
Ttxcnt TXcount;								// number of data to transmit from UART
Trxcnt RXcount;								// number of data to be recieved by host
bit TXReady;								// TX buffer has data to transmit
bit RXReady;								// RX buffer has room

//-----------------------------------------------------------------------------
// UART Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Set_Line_Coding
//-----------------------------------------------------------------------------
//
//	Check the line coding setting (baudrate, databits, parity and stop bit)
//	If it is acceptable, copy it to line_coding structure,
//	and set it to the UART, flush the communication buffer
//	Otherwise, return FALSE
//
// Line coding structure
//	0-3 dwDTERate    Data terminal rate (baudrate), in bits per second
//	4   bCharFormat  Stop bits: 0 - 1 Stop bit, 1 - 1.5 Stop bits, 2 - 2 Stop bits
//	5   bParityType  Parity:    0 - None, 1 - Odd, 2 - Even, 3 - Mark, 4 - Space
//	6   bDataBits    Data bits: 5, 6, 7, 8, 16
//
//	called by USB0 interrupt
//-----------------------------------------------------------------------------

//---------------------------
// SDCC suport
//---------------------------
#if defined SDCC
#pragma save
#pragma nooverlay
#endif // SDCC

bit Set_Line_Coding( TLine_Coding idata * setting )
{
	BYTE cnt;
	BYTE idata * src;
	BYTE idata * dst;

	// Check the setting is acceptable or not

	// Copy setting
	src = (BYTE idata *)setting;
	dst = (BYTE idata *)&uart_line_coding;
	for ( cnt = sizeof(TLine_Coding); cnt > 0; cnt-- )
		*dst++ = *src++;

	// Apply setting to UART

	// Flush COM buffers
	Flush_COMbuffers();

	return TRUE;
}

/*

//
// structure for baudrate setting
//
typedef struct {
	ULONG baudrate;
	BYTE  ckcon;
	BYTE  th1;
} TUART_baud;

static TUART_baud code uart_baud[] =
{
	// baud  CKCON   TH1
	{   9600, 0x00, 0x98 },			// SYSCLK = 24 MHz
	{  19200, 0x01, 0x64 },
	// ...
	{ 115200, 0x08, 0x98 },
};
#define UART_BAUD_COUNT		sizeof( uart_baud ) / sizeof( TUART_baud )

//
// structure for data bits and parity
//
typedef struct {
	BYTE databit;
	BYTE parity;
	BYTE scon0;
} TUART_bits_parity;

enum { PARITY_NONE, PARITY_ODD, PARITY_EVEN };

static TUART_bits_parity code uart_bits_parity[] =
{
	//				  SCON0
	{ 7, PARITY_ODD,  0x00 },						// 0
	{ 7, PARITY_EVEN, 0x00 },						// 1
	{ 8, PARITY_NONE, 0x00 },						// 2
	{ 8, PARITY_ODD,  0x80 },						// 3
	{ 8, PARITY_EVEN, 0x80 },						// 4
};
#define UART_BITS_PARITY_COUNT		sizeof( uart_bits_parity ) / sizeof( TUART_bits_parity )

BYTE uart_bits_index = 2;				// export setting to UART routine

bit Set_Line_Coding( TLine_Coding idata * setting )
{
	BYTE cnt;
	BYTE baud_idx, bits_idx;
	BYTE idata * src;
	BYTE idata * dst;
										// Check the setting is acceptable or not
												// scan baudrate table
	for ( baud_idx = 0; baud_idx < UART_BAUD_COUNT; baud_idx++ ) {
		if ( setting->baudrate == uart_baud[ baud_idx ].baudrate )
			break;								// found matched baud
	}
	if ( baud_idx ] == UART_BAUD_COUNT )		//   no matched baudrate,
		return FALSE;							//   cancel request

												// Allow these stop bit
	if (	(setting->stopbit != 0)				//   1   stopbit
		 && (setting->stopbit != 1)				//   1.5
		 && (setting->stopbit != 2) )			//   2
		 return FALSE;
												// scan data bits - parity table
	for ( bits_idx = 0; bits_idx < UART_BITS_PARITY_COUNT; bits_idx++ ) {
		if (  ( setting->databit == uart_bits_parity[ bits_idx ].databit )
		   && ( setting->parity  == uart_bits_parity[ bits_idx ].parity  ) )
			break;								// found matched bits and parity
	}
	if ( bits_idx == UART_BITS_PARITY_COUNT )	// the end of table
		return FALSE;							//   cancel request

										// Copy setting
	src = (BYTE idata *)setting;
	dst = (BYTE idata *)&uart_line_coding;
	for ( cnt = sizeof(TLine_Coding); cnt > 0; cnt-- )
		*dst++ = *src++;
												// Apply the setting to UART
												// setup timer1 for baudrate
	TR1	=  0;									//   stop timer1
	CKCON &= ~0x0B;								//   clear T1M(3), SCA1(1), SCA2(0)
	CKCON |=  uart_baud[ baud_idx ].ckcon;		//   setup timer1
	TH1	=  uart_baud[ baud_idx ].th1;
	SCON0 &= ~0x80;								// S0MODE - 8bit / 9bit
	SCON0 |=  uart_bits_parity[ bits_idx ].scon0;
	uart_bits_index = bits_idx;					// export bits-parity setting
	TR1	=  1;									// start timer1

												// Flush COM buffers
	Flush_COMbuffers();

	return TRUE;
}

*/


//---------------------------
// SDCC suport
//---------------------------
#if defined SDCC
#pragma restore
#endif // SDCC

//-----------------------------------------------------------------------------
// Set_Line_State
//-----------------------------------------------------------------------------
//	Set/reset RTS/DTR of UART
//		paramter
//			bit 1  RTS
//			bit 0  DTR
//
//	called by USB0 interrupt
//-----------------------------------------------------------------------------

//---------------------------
// SDCC suport
//---------------------------
#if defined SDCC
#pragma save
#pragma nooverlay
#endif // SDCC

void Set_Line_State( BYTE st )
{
	uart_DTR = ((st & CDC_DTR) != 0);
	uart_RTS = ((st & CDC_RTS) != 0);
}

//---------------------------
// SDCC suport
//---------------------------
#if defined SDCC
#pragma restore
#endif // SDCC

//-----------------------------------------------------------------------------
// Update_Line_State
//-----------------------------------------------------------------------------
//  Send line state update to host
//
//  UART State Bitmap
//    7:  (no spec extra) CTS
//    6:  bOverRun    overrun error
//    5:  bParity     parity error
//    4:  bFraming    framing error
//    3:  bRingSignal RI
//    2:  bBreak      break reception
//    1:  bTxCarrier  DSR
//    0:  bRxCarrier  DCD
//-----------------------------------------------------------------------------

static BYTE  data  cs_Line_State;			// serial line state

void Update_Line_State( BYTE st )
{
	EIE1 &= ~0x02;					// disable USB interrupt
	cs_Line_State = st;
	cs_Line_State_Update = TRUE;
	EIE1 |=  0x02;					// enable USB interrupt
}

//-----------------------------------------------------------------------------
// Send_Break
//-----------------------------------------------------------------------------
//	Send break from UART TX port, for specified duration.
//		paramter
//		dur: duration to output break (msec)
//			0x0000: stop break
//			0xFFFF: send break continuously
//
//	called by USB0 interrupt
//-----------------------------------------------------------------------------

//---------------------------
// SDCC suport
//---------------------------
#if defined SDCC
#pragma save
#pragma nooverlay
#endif // SDCC

void Send_Break( UINT dur )
{
	dur = dur;		// suppress warning
	// To do: Send break for 'dur' msec
}

//---------------------------
// SDCC suport
//---------------------------
#if defined SDCC
#pragma restore
#endif // SDCC

//-----------------------------------------------------------------------------
// Flush_COMbuffers, COMGetByte, COMPutByte
//-----------------------------------------------------------------------------
// Ring buffers for TX (host --> UART) and RX (UART --> host) over bulk EP
//

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -