implemented more console / usb handling

still untested, probably doesn't work ... but there's hope
This commit is contained in:
true 2024-11-07 00:25:15 -08:00
parent 5cd5005ae3
commit d4a02f0899
11 changed files with 231 additions and 199 deletions

View File

@ -49,24 +49,45 @@ void EP1_IN_Callback (void)
*/ */
void EP2_OUT_Callback (void) void EP2_OUT_Callback (void)
{ {
uint32_t len; uint16_t cnt;
len = GetEPRxCount( EP2_OUT & 0x7F ); uint16_t remain;
PMAToUserBufferCopy( &CDC_Tx_Buf[ ( Cdc.Tx_LoadNum * DEF_USB_FS_PACK_LEN ) ], GetEPRxAddr( EP2_OUT & 0x7F ), len ); uint8_t dat[DEF_USB_FS_PACKET_LEN];
Cdc.Tx_PackLen[ Cdc.Tx_LoadNum ] = len; uint8_t *buf = dat;
Cdc.Tx_LoadNum++;
if( Cdc.Tx_LoadNum >= DEF_CDC_TX_BUF_NUM_MAX )
{
Cdc.Tx_LoadNum = 0x00;
}
Cdc.Tx_RemainNum++;
if( Cdc.Tx_RemainNum >= ( DEF_CDC_TX_BUF_NUM_MAX - 2 ) )
{ // receive data
Cdc.USB_Down_StopFlag = 0x01; cnt = GetEPRxCount(EP2_OUT & 0x7F);
PMAToUserBufferCopy(buf, GetEPRxAddr(EP2_OUT & 0x7F), cnt);
// copy data to buffer
// note: this is slower, but we are using circular buffer,
// so this is easier without seriously reworking code
remain = cnt;
if (cnt >= DEF_CDC_FROM_HOST_BUF_LEN - Cdc.from_host_ptr) {
// if we've got too much data, then copy what will fit to end of buffer
remain = DEF_CDC_FROM_HOST_BUF_LEN - Cdc.from_host_ptr;
memcpy(cdc_from + Cdc.from_host_ptr, buf, remain);
// set pointers
Cdc.from_host_ptr = 0;
buf += remain;
remain = cnt - remain;
}
// fill the buffer
memcpy(cdc_from + Cdc.from_host_ptr, buf, remain);
// todo: handle not overflowing
/*
if( Cdc.Tx_RemainNum >= ( DEF_CDC_TX_BUF_NUM_MAX - 2 ) ) {
Cdc.usb_from_host_stop_flag = 1;
} }
else else
*/
{ {
SetEPRxValid( ENDP2 ); SetEPRxValid(ENDP2);
} }
} }
@ -81,7 +102,7 @@ void EP2_OUT_Callback (void)
void EP3_IN_Callback (void) void EP3_IN_Callback (void)
{ {
USBD_Endp3_Busy = 0; USBD_Endp3_Busy = 0;
Cdc.USB_Up_IngFlag = 0x00; Cdc.usb_send_host_flag = 0;
} }

View File

@ -392,7 +392,7 @@ uint8_t *USB_CDC_GetLineCoding( uint16_t Length )
return( NULL ); return( NULL );
} }
return (uint8_t *)&Cdc.Com_Cfg[ 0 ]; return (uint8_t *)&Cdc.cdc_cfg[ 0 ];
} }
/********************************************************************* /*********************************************************************
@ -410,7 +410,7 @@ uint8_t *USB_CDC_SetLineCoding( uint16_t Length )
pInformation->Ctrl_Info.Usb_wLength = 7; pInformation->Ctrl_Info.Usb_wLength = 7;
return( NULL ); return( NULL );
} }
return(uint8_t *)&Cdc.Com_Cfg[ 0 ]; return(uint8_t *)&Cdc.cdc_cfg[ 0 ];
} }

View File

@ -48,7 +48,7 @@ typedef enum _DEVICE_STATE
extern __IO uint32_t bDeviceState; /* USB device status */ extern __IO uint32_t bDeviceState; /* USB device status */
extern __IO uint8_t fSuspendEnabled; /* true when suspend is possible */ extern __IO uint8_t fSuspendEnabled; /* true when suspend is possible */

View File

@ -9,7 +9,10 @@
* Attention: This software (modified or not) and binary are used for * Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/ *******************************************************************************/
#include "../../driver/inc/usb_lib.h"
#include "usb_lib.h"
/* Global define */ /* Global define */
#define ValBit(VAR,Place) (VAR & (1 << Place)) #define ValBit(VAR,Place) (VAR & (1 << Place))

View File

@ -9,7 +9,10 @@
* Attention: This software (modified or not) and binary are used for * Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/ *******************************************************************************/
#include "../../driver/inc/usb_lib.h"
#include "usb_lib.h"
/* Private variables */ /* Private variables */
__IO uint16_t SaveRState; __IO uint16_t SaveRState;
@ -123,11 +126,3 @@ void CTR_HP(void)
} }
} }

View File

@ -9,7 +9,9 @@
* Attention: This software (modified or not) and binary are used for * Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/ *******************************************************************************/
#include "../../driver/inc/usb_lib.h"
#include "usb_lib.h"
/******************************************************************************* /*******************************************************************************
@ -67,8 +69,3 @@ void PMAToUserBufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNByt
} }
} }

View File

@ -9,7 +9,10 @@
* Attention: This software (modified or not) and binary are used for * Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/ *******************************************************************************/
#include "../../driver/inc/usb_lib.h"
#include "usb_lib.h"
/******************************************************************************* /*******************************************************************************
* @fn SetCNTR. * @fn SetCNTR.

View File

@ -10,7 +10,9 @@
* Attention: This software (modified or not) and binary are used for * Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/ *******************************************************************************/
#include "../../driver/inc/usb_lib.h"
#include "usb_lib.h"
/******************************************************************************* /*******************************************************************************
@ -70,8 +72,3 @@ uint32_t USB_SIL_Read(uint8_t bEpAddr, uint8_t* pBufferPointer)
return DataLength; return DataLength;
} }

View File

@ -198,13 +198,17 @@ int main(void)
usb_intr_init(); usb_intr_init();
// then initialize shell // then initialize shell
console_set_gets(0); console_set_gets(usb_cdc_gets);
console_set_puts(0); console_set_puts(usb_cdc_puts);
console_init(); console_init();
// let's do this // let's do this
while (1) { while (1) {
// todo: add check to see if USB is connected // process usb data
cdc_from_host_process();
cdc_send_host_process();
// process console
if (bDeviceState == CONFIGURED) { if (bDeviceState == CONFIGURED) {
console_process(); console_process();
} else { } else {

View File

@ -26,8 +26,8 @@
/* The following are serial port transmit and receive related variables and buffers */ /* The following are serial port transmit and receive related variables and buffers */
volatile CDC_CTL Cdc; volatile CDC_CTL Cdc;
__attribute__ ((aligned(4))) uint8_t CDC_Tx_Buf[DEF_CDC_TX_BUF_LEN]; /* Serial port 2 transmit data buffer */ __attribute__((aligned(4))) uint8_t cdc_from[DEF_CDC_FROM_HOST_BUF_LEN]; /* Serial port 2 transmit data buffer */
__attribute__ ((aligned(4))) uint8_t CDC_Rx_Buf[DEF_CDC_RX_BUF_LEN]; /* Serial port 2 receive data buffer */ __attribute__((aligned(4))) uint8_t cdc_send[DEF_CDC_SEND_HOST_BUF_LEN]; /* Serial port 2 receive data buffer */
extern uint8_t USBD_Endp3_Busy; extern uint8_t USBD_Endp3_Busy;
@ -68,17 +68,18 @@ void USB_TIM_Init( void )
} }
/********************************************************************* /*********************************************************************
* @fn UART2_ParaInit * @fn CDC_ParaInit
* *
* @brief Cdc2 parameters initialization * @brief UART parameters initialization
* mode = 0 : Used in usb modify initialization * mode = 0 : Used in usb modify initialization
* mode = 1 : Used in default initializations * mode = 1 : Used in default initializations
* @return none * @return none
*/ */
void CDC_ParaInit(uint8_t mode) void CDC_ParaInit(uint8_t mode)
{ {
uint8_t i; // uint8_t i;
/*
Cdc.Rx_LoadPtr = 0x00; Cdc.Rx_LoadPtr = 0x00;
Cdc.Rx_DealPtr = 0x00; Cdc.Rx_DealPtr = 0x00;
Cdc.Rx_RemainLen = 0x00; Cdc.Rx_RemainLen = 0x00;
@ -89,7 +90,7 @@ void CDC_ParaInit(uint8_t mode)
Cdc.Tx_DealNum = 0x00; Cdc.Tx_DealNum = 0x00;
Cdc.Tx_RemainNum = 0x00; Cdc.Tx_RemainNum = 0x00;
for(i = 0; i < DEF_CDC_TX_BUF_NUM_MAX; i++) { for(i = 0; i < DEF_CDC_FROM_HOST_NUM_MAX; i++) {
Cdc.Tx_PackLen[ i ] = 0x00; Cdc.Tx_PackLen[ i ] = 0x00;
} }
@ -101,16 +102,20 @@ void CDC_ParaInit(uint8_t mode)
Cdc.USB_Up_TimeOut = 0x00; Cdc.USB_Up_TimeOut = 0x00;
Cdc.USB_Up_Pack0_Flag = 0x00; Cdc.USB_Up_Pack0_Flag = 0x00;
Cdc.USB_Down_StopFlag = 0x00; Cdc.USB_Down_StopFlag = 0x00;
*/
if(mode) { Cdc.send_host_ptr = 0;
Cdc.Com_Cfg[ 0 ] = (uint8_t)( DEF_CDC_BAUDRATE ); Cdc.send_host_idx = 0;
Cdc.Com_Cfg[ 1 ] = (uint8_t)( DEF_CDC_BAUDRATE >> 8 ); Cdc.send_host_count = 0;
Cdc.Com_Cfg[ 2 ] = (uint8_t)( DEF_CDC_BAUDRATE >> 16 );
Cdc.Com_Cfg[ 3 ] = (uint8_t)( DEF_CDC_BAUDRATE >> 24 ); if (mode) {
Cdc.Com_Cfg[ 4 ] = DEF_CDC_STOPBIT; Cdc.cdc_cfg[ 0 ] = (uint8_t)( DEF_CDC_BAUDRATE );
Cdc.Com_Cfg[ 5 ] = DEF_CDC_PARITY; Cdc.cdc_cfg[ 1 ] = (uint8_t)( DEF_CDC_BAUDRATE >> 8 );
Cdc.Com_Cfg[ 6 ] = DEF_CDC_DATABIT; Cdc.cdc_cfg[ 2 ] = (uint8_t)( DEF_CDC_BAUDRATE >> 16 );
Cdc.Com_Cfg[ 7 ] = DEF_CDC_RX_TIMEOUT; Cdc.cdc_cfg[ 3 ] = (uint8_t)( DEF_CDC_BAUDRATE >> 24 );
Cdc.cdc_cfg[ 4 ] = DEF_CDC_STOPBIT;
Cdc.cdc_cfg[ 5 ] = DEF_CDC_PARITY;
Cdc.cdc_cfg[ 6 ] = DEF_CDC_DATABIT;
} }
} }
@ -127,10 +132,10 @@ void CDC_USB_Init(void)
uint8_t stopbits __attribute__((unused)); uint8_t stopbits __attribute__((unused));
uint8_t parity __attribute__((unused)); uint8_t parity __attribute__((unused));
baudrate = ( uint32_t )( Cdc.Com_Cfg[ 3 ] << 24 ) + ( uint32_t )( Cdc.Com_Cfg[ 2 ] << 16 ); baudrate = ( uint32_t )( Cdc.cdc_cfg[ 3 ] << 24 ) + ( uint32_t )( Cdc.cdc_cfg[ 2 ] << 16 );
baudrate += ( uint32_t )( Cdc.Com_Cfg[ 1 ] << 8 ) + ( uint32_t )( Cdc.Com_Cfg[ 0 ] ); baudrate += ( uint32_t )( Cdc.cdc_cfg[ 1 ] << 8 ) + ( uint32_t )( Cdc.cdc_cfg[ 0 ] );
stopbits = Cdc.Com_Cfg[ 4 ]; stopbits = Cdc.cdc_cfg[ 4 ];
parity = Cdc.Com_Cfg[ 5 ]; parity = Cdc.cdc_cfg[ 5 ];
// this is the point where you would apply these settings. // this is the point where you would apply these settings.
// since we are virtual only with no real UART, there is nothing to do here. // since we are virtual only with no real UART, there is nothing to do here.
@ -144,12 +149,11 @@ void CDC_USB_Init(void)
* *
* @return none * @return none
*/ */
void CDC_DataTx_Process( void ) void cdc_from_host_process(void)
{ {
// uint16_t count; // uint16_t count;
/* uart1 transmission processing */ if(Cdc.from_host_flag) {
if( Cdc.Tx_Flag ) {
// process incoming data from the host. // process incoming data from the host.
/* /*
@ -172,7 +176,7 @@ void CDC_DataTx_Process( void )
{ {
Cdc.Tx_PackLen[ Cdc.Tx_DealNum ] = 0x0000; Cdc.Tx_PackLen[ Cdc.Tx_DealNum ] = 0x0000;
Cdc.Tx_DealNum++; Cdc.Tx_DealNum++;
if( Cdc.Tx_DealNum >= DEF_CDC_TX_BUF_NUM_MAX ) if( Cdc.Tx_DealNum >= DEF_CDC_FROM_HOST_NUM_MAX )
{ {
Cdc.Tx_DealNum = 0x00; Cdc.Tx_DealNum = 0x00;
} }
@ -190,19 +194,16 @@ void CDC_DataTx_Process( void )
NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn); NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
NVIC_EnableIRQ(USB_HP_CAN1_TX_IRQn); NVIC_EnableIRQ(USB_HP_CAN1_TX_IRQn);
//} //}
} } else {
else /*
{
// Load data from the serial port send buffer to send // Load data from the serial port send buffer to send
if(Cdc.Tx_RemainNum) { if(Cdc.Tx_RemainNum) {
// Determine whether to load from the last unsent buffer or from a new buffer // Determine whether to load from the last unsent buffer or from a new buffer
if( Cdc.Tx_CurPackLen == 0x00 ) { if( Cdc.Tx_CurPackLen == 0x00 ) {
Cdc.Tx_CurPackLen = Cdc.Tx_PackLen[ Cdc.Tx_DealNum ]; Cdc.Tx_CurPackLen = Cdc.Tx_PackLen[ Cdc.Tx_DealNum ];
Cdc.Tx_CurPackPtr = ( Cdc.Tx_DealNum * DEF_USB_FS_PACK_LEN ); Cdc.Tx_CurPackPtr = ( Cdc.Tx_DealNum * DEF_USB_FS_PACKET_LEN );
} }
// todo: figure out wtf all this does.
/*
// Configure DMA and send // Configure DMA and send
USART_ClearFlag( USART2, USART_FLAG_TC ); USART_ClearFlag( USART2, USART_FLAG_TC );
DMA_Cmd( DEF_UART2_TX_DMA_CH, DISABLE ); DMA_Cmd( DEF_UART2_TX_DMA_CH, DISABLE );
@ -210,9 +211,9 @@ void CDC_DataTx_Process( void )
DEF_UART2_TX_DMA_CH->CNTR = Cdc.Tx_CurPackLen; DEF_UART2_TX_DMA_CH->CNTR = Cdc.Tx_CurPackLen;
DMA_Cmd( DEF_UART2_TX_DMA_CH, ENABLE ); DMA_Cmd( DEF_UART2_TX_DMA_CH, ENABLE );
USART2->CTLR3 |= USART_DMAReq_Tx; USART2->CTLR3 |= USART_DMAReq_Tx;
Cdc.Tx_Flag = 0x01; Cdc.from_host_flag = 0x01;
*/
} }
*/
} }
} }
@ -223,110 +224,60 @@ void CDC_DataTx_Process( void )
* *
* @return none * @return none
*/ */
void CDC_DataRx_Process( void ) void cdc_send_host_process(void)
{ {
// uint16_t temp16; uint16_t remain;
// uint32_t remain_len; uint16_t len;
// uint16_t packlen;
/* Serial port 1 data DMA receive processing */
NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn);
// process sending data over USB to the host. // process sending data over USB to the host.
/* if (Cdc.send_host_count) {
CDC_Rx_DMACurCount = DEF_UART2_RX_DMA_CH->CNTR; if (!Cdc.usb_send_flag) {
if( CDC_Rx_DMALastCount != CDC_Rx_DMACurCount ) // Calculate the length of this transmission
remain = Cdc.send_host_count;
{ if (remain >= DEF_USBD_MAX_PACK_SIZE ) {
if( CDC_Rx_DMALastCount > CDC_Rx_DMACurCount ) len = DEF_USBD_MAX_PACK_SIZE;
{ } else {
temp16 = CDC_Rx_DMALastCount - CDC_Rx_DMACurCount; len = remain;
}
else
{
temp16 = DEF_CDC_RX_BUF_LEN - CDC_Rx_DMACurCount;
temp16 += CDC_Rx_DMALastCount;
}
CDC_Rx_DMALastCount = CDC_Rx_DMACurCount;
if( ( Cdc.Rx_RemainLen + temp16 ) > DEF_CDC_RX_BUF_LEN )
{
// Overflow handling
// Save frame error status
printf("U0_O:%08lx\n",(uint32_t)Cdc.Rx_RemainLen);
}
else
{
Cdc.Rx_RemainLen += temp16;
}
// Setting reception status
Cdc.Rx_TimeOut = 0x00;
}
*/
NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
NVIC_EnableIRQ(USB_HP_CAN1_TX_IRQn);
/*****************************************************************/
/* Serial port 1 data processing via USB upload and reception */
if( Cdc.Rx_RemainLen ) {
if( Cdc.USB_Up_IngFlag == 0 ) {
// todo: figure out wtf this does.
/*
// Calculate the length of this upload
remain_len = Cdc.Rx_RemainLen;
packlen = 0x00;
if( remain_len >= DEF_USBD_MAX_PACK_SIZE ) {
packlen = DEF_USBD_MAX_PACK_SIZE;
}
else {
if( Cdc.Rx_TimeOut >= Cdc.Rx_TimeOutMax ) {
packlen = remain_len;
}
} }
if( packlen > ( DEF_CDC_RX_BUF_LEN - Cdc.Rx_DealPtr ) ) { // only send end of buffer if we need to loop it
packlen = ( DEF_CDC_RX_BUF_LEN - Cdc.Rx_DealPtr ); if (len > (DEF_CDC_SEND_HOST_BUF_LEN - Cdc.send_host_idx)) {
len = (DEF_CDC_SEND_HOST_BUF_LEN - Cdc.send_host_idx);
} }
// Upload serial data via usb // send data via usb
if( packlen ) { if (len) {
NVIC_DisableIRQ( USB_LP_CAN1_RX0_IRQn ); Cdc.usb_send_flag = 1;
NVIC_DisableIRQ( USB_HP_CAN1_TX_IRQn );
Cdc.USB_Up_IngFlag = 0x01; // disable interrupts while configuring
Cdc.USB_Up_TimeOut = 0x00; NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
USBD_ENDPx_DataUp( ENDP3, &UART2_Rx_Buf[ Cdc.Rx_DealPtr], packlen); NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn);
// Calculate the variables of interest
Cdc.Rx_RemainLen -= packlen; USBD_ENDPx_DataUp(ENDP3, &cdc_send[Cdc.send_host_idx], len);
Cdc.Rx_DealPtr += packlen;
if( Cdc.Rx_DealPtr >= DEF_CDC_RX_BUF_LEN ) // calculate new pointer locations
{ Cdc.send_host_count -= len;
Cdc.Rx_DealPtr = 0x00; Cdc.send_host_idx += len;
// reset send pointer to beginning of buffer on overflow
if (Cdc.send_host_idx >= DEF_CDC_SEND_HOST_BUF_LEN) {
Cdc.send_host_idx = 0;
} }
// Start 0-length packet timeout timer // re-enable interrupts
if( packlen == DEF_CDC_RX_BUF_LEN ) NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
{ NVIC_EnableIRQ(USB_HP_CAN1_TX_IRQn);
Cdc.USB_Up_Pack0_Flag = 0x01;
}
NVIC_EnableIRQ( USB_LP_CAN1_RX0_IRQn );
NVIC_EnableIRQ( USB_HP_CAN1_TX_IRQn );
} }
*/
} }
else { else {
/* Set the upload success flag directly if the upload is not successful after the timeout */ // this is where we could handle a timeout and reset, but ...
if(Cdc.USB_Up_TimeOut >= DEF_CDC_USB_UP_TIMEOUT) {
Cdc.USB_Up_IngFlag = 0x00;
USBD_Endp3_Busy = 0;
}
} }
} }
/*****************************************************************/ /*****************************************************************/
/* Determine if a 0-length packet needs to be uploaded (required for CDC mode) */ /* Determine if a 0-length packet needs to be uploaded (required for CDC mode)
* todo: figure out wtf this is about */
/*
if( Cdc.USB_Up_Pack0_Flag ) { if( Cdc.USB_Up_Pack0_Flag ) {
if( Cdc.USB_Up_IngFlag == 0 ) { if( Cdc.USB_Up_IngFlag == 0 ) {
if( Cdc.USB_Up_TimeOut >= ( DEF_CDC_RX_TIMEOUT * 20 ) ) { if( Cdc.USB_Up_TimeOut >= ( DEF_CDC_RX_TIMEOUT * 20 ) ) {
@ -344,14 +295,77 @@ void CDC_DataRx_Process( void )
} }
} }
} }
*/
} }
int usb_cdc_puts(char *buf, int cnt)
{
int remain;
// no overflowing the buffer
// note: you can overflow on consecutive writes, be careful...
if (cnt > DEF_CDC_SEND_HOST_BUF_LEN) {
return 0;
}
remain = cnt;
if (cnt >= DEF_CDC_SEND_HOST_BUF_LEN - Cdc.send_host_ptr) {
// if we've got too much data, then copy what will fit to end of buffer
remain = DEF_CDC_SEND_HOST_BUF_LEN - Cdc.send_host_ptr;
memcpy(cdc_send + Cdc.send_host_ptr, buf, remain);
// set pointers
Cdc.send_host_ptr = 0;
buf += remain;
remain = cnt - remain;
}
// fill the buffer
memcpy(cdc_send + Cdc.send_host_ptr, buf, remain);
return cnt;
}
int usb_cdc_gets(char *buf, int cnt)
{
uint16_t idx = Cdc.from_host_idx;
uint16_t len = 0;
// can only get data if a length is requested
if (!cnt) {
return 0;
}
// there is no data to actually get
if (idx == Cdc.from_host_ptr) {
return 0;
}
for (len = 0; len < cnt; len++) {
*buf = cdc_from[idx];
buf++;
idx++;
if (idx >= DEF_CDC_FROM_HOST_BUF_LEN) {
idx = 0;
}
if (idx == Cdc.from_host_ptr) {
break;
}
}
Cdc.from_host_idx = idx;
return len;
}
__attribute__((interrupt("WCH-Interrupt-fast"))) __attribute__((interrupt("WCH-Interrupt-fast")))
void TIM4_IRQHandler(void) void TIM4_IRQHandler(void)
{ {
// uart timeout counts // uart timeout counts
Cdc.Rx_TimeOut++; //Cdc.Rx_TimeOut++;
Cdc.USB_Up_TimeOut++; //Cdc.USB_Up_TimeOut++;
// clear status // clear status
TIM4->INTFR = (uint16_t)~TIM_IT_Update; TIM4->INTFR = (uint16_t)~TIM_IT_Update;

View File

@ -19,51 +19,47 @@ extern "C" {
/******************************************************************************/ // CDC buffer
/* Related macro definitions */ #define DEF_CDC_SEND_HOST_BUF_LEN ( 2 * 512 ) // device to host buffer size
/* Serial buffer related definitions */ #define DEF_CDC_FROM_HOST_BUF_LEN ( 2 * 512 ) // host to device buffer size
#define DEF_CDC_RX_BUF_LEN ( 4 * 512 ) /* Serial x receive buffer size */ #define DEF_USB_FS_PACKET_LEN 64 // USB full speed mode packet size for CDC data
#define DEF_CDC_TX_BUF_LEN ( 2 * 512 ) /* Serial x transmit buffer size */ #define DEF_CDC_FROM_HOST_BUF_NUM_MAX (DEF_CDC_FROM_HOST_BUF_LEN / DEF_USB_FS_PACKET_LEN) // Serial x transmit buffer size
#define DEF_USB_FS_PACK_LEN 64 /* USB full speed mode packet size for serial x data */
#define DEF_CDC_TX_BUF_NUM_MAX (DEF_CDC_TX_BUF_LEN / DEF_USB_FS_PACK_LEN) /* Serial x transmit buffer size */
/* Serial port receive timeout related macro definition */ // CDC default settings
#define DEF_CDC_BAUDRATE 115200 /* Default baud rate for serial port */ #define DEF_CDC_BAUDRATE 115200 /* Default baud rate for serial port */
#define DEF_CDC_STOPBIT 0 /* Default stop bit for serial port */ #define DEF_CDC_STOPBIT 0 /* Default stop bit for serial port */
#define DEF_CDC_PARITY 0 /* Default parity bit for serial port */ #define DEF_CDC_PARITY 0 /* Default parity bit for serial port */
#define DEF_CDC_DATABIT 8 /* Default data bit for serial port */ #define DEF_CDC_DATABIT 8 /* Default data bit for serial port */
#define DEF_CDC_RX_TIMEOUT 30 /* Serial port receive timeout, in 100uS */
#define DEF_CDC_USB_UP_TIMEOUT 60000 /* Serial port receive upload timeout, in 100uS */
/************************************************************/
/* Serial port X related structure definition */
typedef struct __attribute__((packed)) _UART_CTL typedef struct __attribute__((packed)) _UART_CTL
{ {
uint16_t Rx_LoadPtr; /* Serial x data receive buffer load pointer */ uint16_t send_host_ptr; // data location for data being added to the buffer
uint16_t Rx_DealPtr; /* Pointer to serial x data receive buffer processing */ uint16_t send_host_idx; // data location for data being sent over USB
volatile uint16_t Rx_RemainLen; /* Remaining unprocessed length of the serial x data receive buffer */ volatile uint16_t send_host_count; // amount of data to send
uint8_t Rx_TimeOut; /* Serial x data receive timeout */
uint8_t Rx_TimeOutMax; /* Serial x data receive timeout maximum */ uint16_t from_host_ptr;
uint16_t from_host_idx;
uint16_t from_host_count;
volatile uint16_t Tx_LoadNum; /* Serial x data send buffer load number */ volatile uint16_t Tx_LoadNum; /* Serial x data send buffer load number */
volatile uint16_t Tx_DealNum; /* Serial x data send buffer processing number */ volatile uint16_t Tx_DealNum; /* Serial x data send buffer processing number */
volatile uint16_t Tx_RemainNum; /* Serial x data send buffer remaining unprocessed number */ volatile uint16_t from_host_remain; /* Serial x data send buffer remaining unprocessed number */
volatile uint16_t Tx_PackLen[DEF_CDC_TX_BUF_NUM_MAX]; /* The current packet length of the serial x data send buffer */ volatile uint16_t Tx_PackLen[DEF_CDC_FROM_HOST_BUF_NUM_MAX]; /* The current packet length of the serial x data send buffer */
uint8_t Tx_Flag; /* Serial x data send status */ uint8_t from_host_flag; /* Serial x data send status */
uint8_t Recv1; uint8_t pad1;
uint16_t Tx_CurPackLen; /* The current packet length sent by serial port x */ uint16_t Tx_CurPackLen; /* The current packet length sent by serial port x */
uint16_t Tx_CurPackPtr; /* Pointer to the packet currently being sent by serial port x */ uint16_t Tx_CurPackPtr; /* Pointer to the packet currently being sent by serial port x */
uint8_t USB_Up_IngFlag; /* Serial xUSB packet being uploaded flag */ uint8_t usb_send_host_flag; /* Serial xUSB packet being uploaded flag */
uint8_t Recv2; uint8_t usb_send_host_zero_flag; /* Serial xUSB data needs to upload 0-length packet flag */
uint16_t USB_Up_TimeOut; /* Serial xUSB packet upload timeout timer */ uint8_t usb_from_host_stop_flag; /* Serial xUSB packet stop down flag */
uint8_t USB_Up_Pack0_Flag; /* Serial xUSB data needs to upload 0-length packet flag */ uint8_t pad2;
uint8_t USB_Down_StopFlag; /* Serial xUSB packet stop down flag */
uint8_t Com_Cfg[ 8 ]; /* Serial x parameter configuration (default baud rate is 115200, 1 stop bit, no parity, 8 data bits) */ uint8_t cdc_cfg[ 8 ]; /* Serial x parameter configuration (default baud rate is 115200, 1 stop bit, no parity, 8 data bits) */
uint8_t Recv3; uint8_t usb_send_flag; /* Serial x interrupt upload status */
uint8_t USB_Int_UpFlag; /* Serial x interrupt upload status */ uint8_t pad3;
uint16_t USB_Int_UpTimeCount; /* Serial x interrupt upload timing */ uint16_t USB_Int_UpTimeCount; /* Serial x interrupt upload timing */
} CDC_CTL, *PCDC_CTL; } CDC_CTL, *PCDC_CTL;
@ -71,16 +67,18 @@ typedef struct __attribute__((packed)) _UART_CTL
/* Constant, variable extents */ /* Constant, variable extents */
/* The following are serial port transmit and receive related variables and buffers */ /* The following are serial port transmit and receive related variables and buffers */
extern volatile CDC_CTL Cdc; /* Serial x control related structure */ extern volatile CDC_CTL Cdc; /* Serial x control related structure */
extern __attribute__ ((aligned(4))) uint8_t CDC_Tx_Buf[DEF_CDC_TX_BUF_LEN]; /* Serial x transmit buffer */ extern __attribute__((aligned(4))) uint8_t cdc_from[DEF_CDC_FROM_HOST_BUF_LEN];
extern __attribute__ ((aligned(4))) uint8_t CDC_Rx_Buf[DEF_CDC_RX_BUF_LEN]; /* Serial x transmit buffer */ extern __attribute__((aligned(4))) uint8_t cdc_send[DEF_CDC_SEND_HOST_BUF_LEN];
/***********************************************************************************************************************/ /***********************************************************************************************************************/
void CDC_USB_Init(void); void CDC_USB_Init(void);
void CDC_DataTx_Process( void ); /* Serial port 1 data sending processing */ void cdc_from_host_process(void);
void CDC_DataRx_Process( void ); /* Serial port 1 data reception processing */ void cdc_send_host_process(void);
int usb_cdc_puts(char *buf, int cnt);
int usb_cdc_gets(char *buf, int cnt);