Code clean.

This commit is contained in:
klaute 2016-09-01 16:51:29 +02:00
parent 0e1c9b85bd
commit a4fc0273c5

View file

@ -1,3 +1,11 @@
/**
* description: Modified LUFA example to get two virtual serial USB devices.
* author: Kai Lauterbach
* date: 08/2016
* version: v0.1
* license: GPLv3
*/
/*
LUFA Library
Copyright (C) Dean Camera, 2015.
@ -30,8 +38,8 @@
/** \file
*
* Main source file for the iUSB2SerialMux demo. This file contains the main tasks of the demo and
* is responsible for the initial application hardware configuration.
* Main source file for the USB2SerialMux. This file contains the main tasks of the application and
* is responsible for the initial hardware configuration.
*/
#include "main.h"
@ -70,6 +78,7 @@ static CDC_LineEncoding_t LineEncoding2 = { .BaudRateBPS = 0,
.ParityType = CDC_PARITY_None,
.DataBits = 8 };
/**************************************************************************************/
/** Main program entry point. This routine configures the hardware required by the application, then
* enters a loop to run the application tasks in sequence.
@ -96,6 +105,8 @@ int main(void)
}
}
/**************************************************************************************/
/** Configures the board hardware and chip peripherals for the demo's functionality. */
void SetupHardware(void)
{
@ -140,6 +151,7 @@ void SetupHardware(void)
eeprom_write_dword(&eep_baudrate, baudrate);
}
// initialize the uart to the default baudrate
uart_init( UART_BAUD_SELECT(baudrate, F_CPU) );
sei();
@ -147,6 +159,152 @@ void SetupHardware(void)
USB_Init();
}
/**************************************************************************************/
/** Function to manage CDC data transmission and reception to and from the host for the first CDC interface,
* which sends answers or response data to the host.
*/
void CDC1_Task(void)
{
/* Device must be connected and configured for the task to run */
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
/* Select the Serial Rx Endpoint */
Endpoint_SelectEndpoint(CDC1_RX_EPADDR);
if (Endpoint_IsOUTReceived())
{
/* Create a temp buffer big enough to hold the incoming endpoint packet */
uint8_t Buffer[Endpoint_BytesInEndpoint()];
/* Remember how large the incoming packet is */
uint16_t DataLength = Endpoint_BytesInEndpoint();
/* Read in the incoming packet into the buffer */
Endpoint_Read_Stream_LE(&Buffer, DataLength, NULL);
/* Finalize the stream transfer to send the last packet */
Endpoint_ClearOUT();
for (uint16_t i = 0; i < DataLength; i++)
{
// process the received data and descide to do an action
LED0_ON;
cc_processData(Buffer[i]);
LED0_OFF;
}
}
}
/**************************************************************************************/
void USB_serialStreamWriteC(char *data, uint16_t len)
{
/* Determine if data/answeres should be sent to the host
* the previous RX section should be clarify that behaviour.
*/
/* Flag management - Only allow one string to be sent per action */
if (data != NULL && len > 0 && LineEncoding1.BaudRateBPS)
{
LED1_ON;
/* Select the Serial Tx Endpoint */
Endpoint_SelectEndpoint(CDC1_TX_EPADDR);
/* Write the String to the Endpoint */
Endpoint_Write_Stream_LE(data, len, NULL);
/* Finalize the stream transfer to send the last packet */
Endpoint_ClearIN();
/* Wait until the endpoint is ready for another packet */
Endpoint_WaitUntilReady();
/* Send an empty packet to ensure that the host does not buffer data sent to it */
Endpoint_ClearIN();
LED1_OFF;
}
}
/**************************************************************************************/
/** Function to manage CDC data transmission and reception to and from the host for the second CDC interface,
* which sends all data received from a node (mux) during USART to the host.
*/
void CDC2_Task(void)
{
/* Device must be connected and configured for the task to run */
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
//=====================================
/* Select the Serial Rx Endpoint */
Endpoint_SelectEndpoint(CDC2_RX_EPADDR);
/* Check to see if any data has been received */
if (Endpoint_IsOUTReceived())
{
/* Create a temp buffer big enough to hold the incoming endpoint packet */
uint8_t Buffer[Endpoint_BytesInEndpoint()];
/* Remember how large the incoming packet is */
uint16_t DataLength = Endpoint_BytesInEndpoint();
/* Read in the incoming packet into the buffer */
Endpoint_Read_Stream_LE(&Buffer, DataLength, NULL);
/* Finalize the stream transfer to send the last packet */
Endpoint_ClearOUT();
// TODO at this point send the data to the USART
// Send USART &Buffer
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;
int c = uart_getc();
while (!(c & UART_NO_DATA) && cnt < OUTPUT_BUFFER_SIZE)
{
//LED0_ON;
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();
}
}
/**************************************************************************************/
/** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
* starts the library USB task to begin the enumeration and USB management process.
*/
@ -235,152 +393,4 @@ void EVENT_USB_Device_ControlRequest(void)
}
}
/** Function to manage CDC data transmission and reception to and from the host for the first CDC interface,
* which sends answers or response data to the host.
*/
void CDC1_Task(void)
{
/* Device must be connected and configured for the task to run */
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
//===========================================================================
/* Select the Serial Rx Endpoint */
Endpoint_SelectEndpoint(CDC1_RX_EPADDR);
if (Endpoint_IsOUTReceived())
{
/* Create a temp buffer big enough to hold the incoming endpoint packet */
uint8_t Buffer[Endpoint_BytesInEndpoint()];
/* Remember how large the incoming packet is */
uint16_t DataLength = Endpoint_BytesInEndpoint();
/* Read in the incoming packet into the buffer */
Endpoint_Read_Stream_LE(&Buffer, DataLength, NULL);
/* Finalize the stream transfer to send the last packet */
Endpoint_ClearOUT();
for (uint16_t i = 0; i < DataLength; i++)
{
// process the received data and descide to do an action
LED0_ON;
cc_processData(Buffer[i]);
LED0_OFF;
}
}
}
void USB_serialStreamWriteC(char *data, uint16_t len)
{
//===========================================================================
/* Determine if data/answeres should be sent to the host
* the previous RX section should be clarify that behaviour.
*/
/* Flag management - Only allow one string to be sent per action */
if (data != NULL && len > 0 && LineEncoding1.BaudRateBPS)
{
LED1_ON;
/* Select the Serial Tx Endpoint */
Endpoint_SelectEndpoint(CDC1_TX_EPADDR);
/* Write the String to the Endpoint */
Endpoint_Write_Stream_LE(data, len, NULL);
/* Finalize the stream transfer to send the last packet */
Endpoint_ClearIN();
/* Wait until the endpoint is ready for another packet */
Endpoint_WaitUntilReady();
/* Send an empty packet to ensure that the host does not buffer data sent to it */
Endpoint_ClearIN();
LED1_OFF;
}
}
/** Function to manage CDC data transmission and reception to and from the host for the second CDC interface,
* which sends all data received from a node (mux) during USART to the host.
*/
void CDC2_Task(void)
{
/* Device must be connected and configured for the task to run */
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
//===========================================================================
/* Select the Serial Rx Endpoint */
Endpoint_SelectEndpoint(CDC2_RX_EPADDR);
/* Check to see if any data has been received */
if (Endpoint_IsOUTReceived())
{
/* Create a temp buffer big enough to hold the incoming endpoint packet */
uint8_t Buffer[Endpoint_BytesInEndpoint()];
/* Remember how large the incoming packet is */
uint16_t DataLength = Endpoint_BytesInEndpoint();
/* Read in the incoming packet into the buffer */
Endpoint_Read_Stream_LE(&Buffer, DataLength, NULL);
/* Finalize the stream transfer to send the last packet */
Endpoint_ClearOUT();
// TODO at this point send the data to the USART
// Send USART &Buffer
for (uint16_t i = 0; i < DataLength; i++)
{
uart_putc(Buffer[i]);
}
}
//return;
//===========================================================================
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;
int c = uart_getc();
while (!(c & UART_NO_DATA) && cnt < OUTPUT_BUFFER_SIZE)
{
//LED0_ON;
outBuffer[cnt] = c;
c = uart_getc();
cnt++;
}
/*
cnt = 1;
outBuffer[0] = '2';
*/
// 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();
}
}
/**************************************************************************************/