From f0eeb38c49f2f63a41a5cb460174f78090330cd4 Mon Sep 17 00:00:00 2001 From: klaute Date: Wed, 17 Aug 2016 16:00:51 +0200 Subject: [PATCH] Prepared to use UART. --- src/USB2SerialMux.c | 64 ++++++++++++++++++++++++++++++--------------- src/USB2SerialMux.h | 9 ++++--- 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/USB2SerialMux.c b/src/USB2SerialMux.c index 954748b..16f3d84 100755 --- a/src/USB2SerialMux.c +++ b/src/USB2SerialMux.c @@ -70,6 +70,9 @@ int main(void) { SetupHardware(); + + uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) ); + SET_ERR_MASK(ERRMASK_USB_NOTREADY); GlobalInterruptEnable(); @@ -77,6 +80,7 @@ int main(void) { CDC1_Task(); CDC2_Task(); + USB_USBTask(); } } @@ -220,8 +224,6 @@ void CDC1_Task(void) // TODO disable this line if data should be received Endpoint_ClearOUT(); // Throw away any received data from the host at this point - // TODO Read the data from the host - /* Create a temp buffer big enough to hold the incoming endpoint packet */ uint8_t Buffer[Endpoint_BytesInEndpoint()]; @@ -234,7 +236,8 @@ void CDC1_Task(void) /* Finalize the stream transfer to send the last packet */ Endpoint_ClearOUT(); - // TODO process the received data and descide to do an action + // TODO Read the data from the host + // process the received data and descide to do an action // uint8_t cmd = processReceivedControlData(&Buffer, DataLength); // if (cmd != NULL) // { @@ -300,23 +303,42 @@ void CDC2_Task(void) // TODO at this point send the data to the USART // Send USART &Buffer - - // TODO read the USART data and send them to the host - // Fill &Buffer with USART data or send the USART input buffer direct - - /* Select the Serial Tx Endpoint */ - Endpoint_SelectEndpoint(CDC2_TX_EPADDR); - - /* Write the received data to the endpoint */ - Endpoint_Write_Stream_LE(&Buffer, DataLength, NULL); - - /* Finalize the stream transfer to send the last packet */ - Endpoint_ClearIN(); - - /* Wait until the endpoint is ready for the next packet */ - Endpoint_WaitUntilReady(); - - /* Send an empty packet to prevent host buffering */ - Endpoint_ClearIN(); + for (uint16_t i = 0; i < DataLength; i++) + { + uart_putc(Buffer[i]); + } } + + //=========================================================================== + uint8_t outBuffer[OUTPUT_BUFFER_SIZE]; + // TODO read the USART data and send them to the host + // Fill &Buffer with USART data or send the USART input buffer direct + uint16_t cnt = 0; + uint8_t c = uart_getc(); + while (c != UART_NO_DATA && cnt < OUTPUT_BUFFER_SIZE) + { + outBuffer[cnt] = c; + c = uart_getc(); + cnt++; + } + + // send the data which was received from the uart connection + if (cnt > 0) + { + /* Select the Serial Tx Endpoint */ + Endpoint_SelectEndpoint(CDC2_TX_EPADDR); + + /* Write the received data to the endpoint */ + Endpoint_Write_Stream_LE(&outBuffer, cnt, NULL); + + /* Finalize the stream transfer to send the last packet */ + Endpoint_ClearIN(); + + /* Wait until the endpoint is ready for the next packet */ + Endpoint_WaitUntilReady(); + + /* Send an empty packet to prevent host buffering */ + Endpoint_ClearIN(); + } } + diff --git a/src/USB2SerialMux.h b/src/USB2SerialMux.h index 39094bd..5701436 100755 --- a/src/USB2SerialMux.h +++ b/src/USB2SerialMux.h @@ -49,13 +49,14 @@ #include #include - #include "uart/uart.h" - - uint8_t error_mask = 0x00; + uint8_t error_mask = 0x00; /* UART definitions: */ - #define UART_BAUD_RATE 115200 + #define UART_BAUD_RATE 115200 + #define OUTPUT_BUFFER_SIZE 100 + + #include "uart/uart.h" // include after the definition /* Macros: */