diff options
| author | Shlomo Hecht <shlomo@twine-s.com> | 2018-04-08 18:39:20 +0300 |
|---|---|---|
| committer | Shlomo Hecht <shlomo@twine-s.com> | 2018-04-08 18:39:20 +0300 |
| commit | f22f64fec07df88dc302c7d541197a01bec50879 (patch) | |
| tree | 6582b163793d13ff57d5497cd6c464b98dfa12f9 /Software/Embedded_SW/Embedded/Drivers/USB_Communication | |
| parent | 692d71abcd583ade2977d4b6012b9c2a6de7d4d8 (diff) | |
| download | Tango-f22f64fec07df88dc302c7d541197a01bec50879.tar.gz Tango-f22f64fec07df88dc302c7d541197a01bec50879.zip | |
Communication Tx, Communication Rx tasks.
UART comunication (no buffers yet!)
Diffstat (limited to 'Software/Embedded_SW/Embedded/Drivers/USB_Communication')
6 files changed, 10 insertions, 2239 deletions
diff --git a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD - Copy.new b/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD - Copy.new deleted file mode 100644 index f8b333c8a..000000000 --- a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD - Copy.new +++ /dev/null @@ -1,1063 +0,0 @@ - -/************************************************************************************ - * this module is represents the access layer to USB STACK DRIVER - * used to communication over USB protocol - * the USB protocol implementation is inside the module no need for another includes - * ======== USBCDCD.c ======== - ************************************************************************************/ - -/* XDCtools Header files */ -#include <xdc/std.h> -#include <xdc/runtime/Error.h> -#include <xdc/runtime/System.h> - -/* BIOS Header files */ -#include <ti/sysbios/BIOS.h> -#include <ti/sysbios/gates/GateMutex.h> -#include <ti/sysbios/hal/Hwi.h> -#include <ti/sysbios/knl/Semaphore.h> -#include <ti/sysbios/knl/Task.h> - -#include <stdbool.h> -#include <stdint.h> - -/* driverlib Header files */ -#include <inc/hw_ints.h> -#include <inc/hw_types.h> - -#include <driverlib/rom.h> -#include <driverlib/sysctl.h> -#include <driverlib/usb.h> -#include <inc/hw_memmap.h> - -/* usblib Header files */ -#include <usblib/usb-ids.h> -#include <usblib/usblib.h> -#include <usblib/usbcdc.h> -#include <usblib/device/usbdevice.h> -#include <usblib/device/usbdcdc.h> -#include <usblib/device/usbddfu-rt.h> -#include <usblib/device/usbdcomp.h> -#include "Drivers/USB_Communication/USBCDCD.h" - -//#include "Common/Sys_Configuration/Configuration.h" -//#include "Communication/Communication.h" - -#include <utils/ustdlib.h> -#include "Common/Utilities/Utils.h" -//#include "Communication/HostMessaging.h" - -#if defined(TIVAWARE) -typedef uint32_t USBCDCDEventType; -#else -#define eUSBModeForceDevice USB_MODE_FORCE_DEVICE -typedef unsigned long USBCDCDEventType; -#endif - -/* Defines */ -#define USBBUFFERSIZE 256 -//**************************************************************************** -// -// A buffer into which the composite device can write the combined config -// descriptor. -// -//**************************************************************************** -#define DESCRIPTOR_BUFFER_SIZE (COMPOSITE_DDFU_SIZE + COMPOSITE_DCDC_SIZE) - -extern Semaphore_Handle updateSem; -extern Semaphore_Handle ReconnectSem; - -uint8_t g_pui8DescriptorBuffer[DESCRIPTOR_BUFFER_SIZE]; - -/* Typedefs */ -typedef volatile enum { - USBCDCD_STATE_IDLE = 0, - USBCDCD_STATE_INIT, - USBCDCD_STATE_UNCONFIGURED -} USBCDCD_USBState; - -/* Static variables and handles */ -static volatile USBCDCD_USBState state; -static unsigned char receiveBuffer[USBBUFFERSIZE]; -static unsigned char transmitBuffer[USBBUFFERSIZE]; - -static GateMutex_Handle gateTxSerial; -static GateMutex_Handle gateRxSerial; -static GateMutex_Handle gateUSBWait; -static Semaphore_Handle semTxSerial; -static Semaphore_Handle semRxSerial; -static Semaphore_Handle semUSBConnected; -extern Semaphore_Handle initConnectionSem; - -/* Function prototypes */ -static USBCDCDEventType cbRxHandler(void *cbData, USBCDCDEventType event, - USBCDCDEventType _eventMsgData, - void *eventMsgPtr); -static USBCDCDEventType cbSerialHandler(void *cbData, USBCDCDEventType event, - USBCDCDEventType _eventMsgData, - void *eventMsgPtr); -static USBCDCDEventType cbTxHandler(void *cbData, USBCDCDEventType event, - USBCDCDEventType _eventMsgData, - void *eventMsgPtr); -void USBCDCD_hwiHandler(UArg arg0); -static unsigned int rxData(unsigned char *_pBuff, - unsigned int _length, - unsigned int _timeout); -static unsigned int txData(const unsigned char *_pBuff, - int _length, unsigned int _timeout); -void USBCDCD_init(void); - -unsigned int USBCDCD_receiveData(unsigned char *_pBuff, - unsigned int _length, - unsigned int _timeout); -unsigned int USBCDCD_sendData(const unsigned char *_pBuff, - unsigned int _length, - unsigned int _timeout); -bool USBCDCD_waitForConnect(unsigned int _timeout); - -uint32_t DFUDetachCallback(void *pvCBData, uint32_t ui32Event, - uint32_t ui32MsgData, void *pvMsgData); - -/* The languages supported by this device. */ -const unsigned char langDescriptor[] = { - 4, - USB_DTYPE_STRING, - USBShort(USB_LANG_EN_US) -}; - -/* The manufacturer string. */ -const unsigned char manufacturerString[] = { - (17 + 1) * 2, - USB_DTYPE_STRING, - 'T', 0, 'e', 0, 'x', 0, 'a', 0, 's', 0, ' ', 0, 'I', 0, 'n', 0, 's', 0, - 't', 0, 'r', 0, 'u', 0, 'm', 0, 'e', 0, 'n', 0, 't', 0, 's', 0, -}; - -/* The product string. */ -const unsigned char productString[] = { - 2 + (13 * 2), - USB_DTYPE_STRING, - 'T', 0, 'w', 0, 'i', 0, 'n', 0, 'e', 0, ' ', 0, 'J', 0, 'i', 0, - 'g', 0, ' ', 0, 'U', 0, 'S', 0, 'B', 0, -}; - -/* The serial number string. */ -const unsigned char serialNumberString[] = { - (8 + 1) * 2, - USB_DTYPE_STRING, - '1', 0, '2', 0, '3', 0, '4', 0, '5', 0, '6', 0, '7', 0, '8', 0 -}; - -/* The interface description string. */ -const unsigned char controlInterfaceString[] = { - 2 + (21 * 2), - USB_DTYPE_STRING, - 'J', 0, 'i', 0, 'g', 0, ' ', 0, 'C', 0, 'o', 0, 'n', 0, 't', 0, - 'r', 0, 'o', 0, 'l', 0, ' ', 0, 'I', 0, 'n', 0, 't', 0, 'e', 0, - 'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0 -}; - -/* The configuration description string. */ -const unsigned char configString[] = { - 2 + (26 * 2), - USB_DTYPE_STRING, - 'S', 0, 'e', 0, 'l', 0, 'f', 0, ' ', 0, 'P', 0, 'o', 0, 'w', 0, - 'e', 0, 'r', 0, 'e', 0, 'd', 0, ' ', 0, 'C', 0, 'o', 0, 'n', 0, - 'f', 0, 'i', 0, 'g', 0, 'u', 0, 'r', 0, 'a', 0, 't', 0, 'i', 0, - 'o', 0, 'n', 0 -}; - -//***************************************************************************** -// -// The descriptor string table. -// -//***************************************************************************** -const uint8_t * const g_ppui8StringDescriptors[] = -{ - langDescriptor, - manufacturerString, - productString, - serialNumberString, - controlInterfaceString, - configString -}; - -#define NUM_STRING_DESCRIPTORS (sizeof(g_ppui8StringDescriptors) / \ - sizeof(g_ppui8StringDescriptors[0])) - -/* The descriptor string table. */ -const unsigned char * const stringDescriptors[] = { - langDescriptor, - manufacturerString, - productString, - serialNumberString, - controlInterfaceString, - configString -}; - -#define STRINGDESCRIPTORSCOUNT (sizeof(stringDescriptors) / \ - sizeof(unsigned char *)) - -#if defined(TIVAWARE) -tUSBBuffer txBuffer; -tUSBBuffer rxBuffer; -static tUSBDCDCDevice g_sCDCDevice; - -tUSBBuffer rxBuffer = -{ - false, /* This is a receive buffer. */ - cbRxHandler, /* pfnCallback */ - (void *)&g_sCDCDevice, /* Callback data is our device pointer. */ - USBDCDCPacketRead, /* pfnTransfer */ - USBDCDCRxPacketAvailable, /* pfnAvailable */ - (void *)&g_sCDCDevice, /* pvHandle */ - receiveBuffer, /* pcBuffer */ - USBBUFFERSIZE, /* ulBufferSize */ - {{0, 0, 0, 0}, 0, 0} /* private data workspace */ -}; - -tUSBBuffer txBuffer = -{ - true, /* This is a transmit buffer. */ - cbTxHandler, /* pfnCallback */ - (void *)&g_sCDCDevice, /* Callback data is our device pointer. */ - USBDCDCPacketWrite, /* pfnTransfer */ - USBDCDCTxPacketAvailable, /* pfnAvailable */ - (void *)&g_sCDCDevice, /* pvHandle */ - transmitBuffer, /* pcBuffer */ - USBBUFFERSIZE, /* ulBufferSize */ - {{0, 0, 0, 0}, 0, 0} /* private data workspace */ -}; - -static tUSBDCDCDevice g_sCDCDevice = -{ - USB_VID_TI_1CBE, - USB_PID_SERIAL, - 0, - USB_CONF_ATTR_SELF_PWR, - - cbSerialHandler, - (void *)&g_sCDCDevice, - - USBBufferEventCallback, - (void *)&rxBuffer, - - USBBufferEventCallback, - (void *)&txBuffer, - - stringDescriptors, - STRINGDESCRIPTORSCOUNT -}; - -//***************************************************************************** -// -// The DFU runtime interface initialization and customization structures -// -//***************************************************************************** -tUSBDDFUDevice g_sDFUDevice = -{ - DFUDetachCallback, - (void *)&g_sDFUDevice -}; - -//**************************************************************************** -// -// The number of device class instances that this composite device will -// use. -// -//**************************************************************************** -#define NUM_DEVICES 2 - -//**************************************************************************** -// -// The array of devices supported by this composite device. -// -//**************************************************************************** -tCompositeEntry g_psCompDevices[NUM_DEVICES]; - -//**************************************************************************** -// -// Additional workspace required by the composite driver to hold a lookup -// table allowing mapping of composite interface and endpoint numbers to -// individual device class instances. -// -//**************************************************************************** -uint32_t g_pui32CompWorkspace[NUM_DEVICES]; - -//**************************************************************************** -// -// The instance data for this composite device. -// -//**************************************************************************** -tCompositeInstance g_sCompInstance; - -//**************************************************************************** -// -// Allocate the Device Data for the top level composite device class. -// -//**************************************************************************** -tUSBDCompositeDevice g_sCompDevice = -{ - // - // Stellaris VID. - // - USB_VID_TI_1CBE, - - // - // Stellaris PID for composite SERIAL/DFU device. - // - USB_PID_COMP_SERIAL, - - // - // This is in milliamps. - // - 250, - - // - // Bus powered device. - // - USB_CONF_ATTR_BUS_PWR, - - // - // Device event handler function pointer (receives connect, disconnect - // and other device-level notifications). - // - cbSerialHandler, - - // - // The string table. - // - g_ppui8StringDescriptors, - NUM_STRING_DESCRIPTORS, - - // - // The Composite device array. - // - NUM_DEVICES, - g_psCompDevices, -}; - -#else -#define USB_BUFFER_WORKSPACE_SIZE 1200 -const tUSBBuffer rxBuffer; -const tUSBBuffer txBuffer; -static unsigned char receiveBufferWorkspace[USB_BUFFER_WORKSPACE_SIZE]; -static unsigned char transmitBufferWorkspace[USB_BUFFER_WORKSPACE_SIZE]; - -static tCDCSerInstance serialInstance; -const tUSBDCDCDevice g_sCDCDevice; - -const tUSBBuffer rxBuffer = { - false, /* This is a receive buffer. */ - cbRxHandler, /* pfnCallback */ - (void *)&g_sCDCDevice, /* Callback data is our device pointer. */ - USBDCDCPacketRead, /* pfnTransfer */ - USBDCDCRxPacketAvailable, /* pfnAvailable */ - (void *)&g_sCDCDevice, /* pvHandle */ - receiveBuffer, /* pcBuffer */ - USBBUFFERSIZE, /* ulBufferSize */ - receiveBufferWorkspace /* pvWorkspace */ -}; - -const tUSBBuffer txBuffer = { - true, /* This is a transmit buffer. */ - cbTxHandler, /* pfnCallback */ - (void *)&g_sCDCDevice, /* Callback data is our device pointer. */ - USBDCDCPacketWrite, /* pfnTransfer */ - USBDCDCTxPacketAvailable, /* pfnAvailable */ - (void *)&g_sCDCDevice, /* pvHandle */ - transmitBuffer, /* pcBuffer */ - USBBUFFERSIZE, /* ulBufferSize */ - transmitBufferWorkspace /* pvWorkspace */ -}; - -const tUSBDCDCDevice g_sCDCDevice = { - USB_VID_TI_1CBE, - USB_PID_SERIAL, - 0, - USB_CONF_ATTR_SELF_PWR, - - cbSerialHandler, - NULL, - - USBBufferEventCallback, - (void *)&rxBuffer, - - USBBufferEventCallback, - (void *)&txBuffer, - - stringDescriptors, - STRINGDESCRIPTORSCOUNT, - - &serialInstance /* Old usblib stores a pointer */ -}; -#endif - -static tLineCoding g_sLineCoding = { - 115200, /* 115200 baud rate. */ - 1, /* 1 Stop Bit. */ - 0, /* No Parity. */ - 8 /* 8 Bits of data. */ -}; - -int cpt[20] = {0}; -//***************************************************************************** -// -//! Waits for a character from the USB port. -//! -//! This function gets a character from the USB receive buffer. -//! If there are no characters available, this function waits until a -//! character is received before returning. -//! -//! \return Returns the character read from the USB port. -// -//***************************************************************************** -char USBGetChar(void) -{ - uint8_t ucChar; - int len = 0; - //int leng; -// char str[60]; - - len = USBCDCD_receiveData(&ucChar, 1, BIOS_WAIT_FOREVER); - if (len !=1) - { -// leng = usnprintf(str, 60, "\r\n USBGetChar len error %d",len ); -// cb_push_back (str, leng); - Task_sleep(1); - len = USBCDCD_receiveData(&ucChar, 1, BIOS_WAIT_FOREVER); -// leng = usnprintf(str, 60, "\r\n USBGetChar len second %d",len ); -// cb_push_back (str, leng); - } - // - // Now return the char. - // - return (ucChar); -} - -unsigned int USBSendData(const unsigned char *_pBuff,unsigned int _length) -{ - //StoreUSBByte ((uint8_t *)_pBuff,_length,false); - return USBCDCD_sendData(_pBuff,_length,10/*BIOS_WAIT_FOREVER*/); -} - -void USBFlush(void) -{ - USBBufferFlush(&rxBuffer); -} - -/****************************************************************************** - * ======== cbRxHandler ======== - * Callback handler for the USB stack. - * - * Callback handler call by the USB stack to notify us on what has happened in - * regards to the keyboard. - * - * @param(_pcbData) A callback pointer provided by the client. - * - * @param(_event) Identifies the event that occurred in regards to - * this device. - * - * @param(_eventMsgData) A data value associated with a particular event. - * - * @param(_pEventMsgPtr) A data pointer associated with a particular event. - * - ******************************************************************************/ -static USBCDCDEventType cbRxHandler(void *_pcbData, USBCDCDEventType _event, - USBCDCDEventType _eventMsgData, - void *_pEventMsgPtr) -{ - cpt[0]++; - switch (_event) - { - case USB_EVENT_RX_AVAILABLE: - { - cpt[1]++; - Semaphore_post(semRxSerial); - break; - } - case USB_EVENT_DATA_REMAINING: - break; - - case USB_EVENT_REQUEST_BUFFER: - break; - - default: - break; - } - - return (0); -} -//***************************************************************************** -// -// This is the callback from the USB DFU runtime interface driver. -// -// \param pvCBData is ignored by this function. -// \param ui32Event is one of the valid events for a DFU device. -// \param ui32MsgParam is defined by the event that occurs. -// \param pvMsgData is a pointer to data that is defined by the event that -// occurs. -// -// This function will be called to inform the application when a change occurs -// during operation as a DFU device. Currently, the only event passed to this -// callback is USBD_DFU_EVENT_DETACH which tells the recipient that they should -// pass control to the boot loader at the earliest, non-interrupt context -// point. -// -// \return This function will return 0. -// -//***************************************************************************** -uint32_t -DFUDetachCallback(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgData, - void *pvMsgData) -{ - if(ui32Event == USBD_DFU_EVENT_DETACH) - { - // - // Set the flag that the main loop uses to determine when it is time - // to transfer control back to the boot loader. Note that we - // absolutely DO NOT call USBDDFUUpdateBegin() here since we are - // currently in interrupt context and this would cause bad things to - // happen (and the boot loader to not work). - // - // - // Release updateSem - // - Semaphore_post(updateSem); - } - - return (0); -} - -/****************************************************************************** - * ======== cbSerialHandler ======== - * Callback handler for the USB stack. - * - * Callback handler call by the USB stack to notify us on what has happened in - * regards to the keyboard. - * - * @param(_pcbData) A callback pointer provided by the client. - * - * @param(_event) Identifies the event that occurred in regards to - * this device. - * - * @param(_eventMsgData) A data value associated with a particular event. - * - * @param(_pEventMsgPtr) A data pointer associated with a particular event. - * - *****************************************************************************/ -static USBCDCDEventType cbSerialHandler(void *_pcbData, USBCDCDEventType _event, - USBCDCDEventType _eventMsgData, - void *_pEventMsgPtr) -{ - tLineCoding *psLineCoding; - static bool initiated = false; - cpt[2]++; - - /* Determine what event has happened */ - switch (_event) - { - case USB_EVENT_CONNECTED: - { - cpt[3]++; - if(state == USBCDCD_STATE_UNCONFIGURED) - { - state = USBCDCD_STATE_INIT; - //SetCommunication(USBGetChar, USBSendData,USBFlush); - Semaphore_post(semUSBConnected); - Semaphore_post(initConnectionSem); - Semaphore_post(semTxSerial); - initiated = true; - } - break; - } - case USB_EVENT_DISCONNECTED: - { - state = USBCDCD_STATE_UNCONFIGURED; - break; - } - case USBD_CDC_EVENT_GET_LINE_CODING: - { - cpt[7]++; - /* Create a pointer to the line coding information. */ - psLineCoding = (tLineCoding *)_pEventMsgPtr; - - /* Copy the current line coding information into the structure. */ - *(psLineCoding) = g_sLineCoding; - break; - } - case USBD_CDC_EVENT_SET_LINE_CODING: - { - cpt[6]++; - /* Create a pointer to the line coding information. */ - psLineCoding = (tLineCoding *)_pEventMsgPtr; - - /* - * Copy the line coding information into the current line coding - * structure. - */ - g_sLineCoding = *(psLineCoding); - break; - } - case USBD_CDC_EVENT_SET_CONTROL_LINE_STATE: - break; - - case USBD_CDC_EVENT_SEND_BREAK: - break; - - case USBD_CDC_EVENT_CLEAR_BREAK: - break; - - case USB_EVENT_SUSPEND: - cpt[4]++; - if (state != USBCDCD_STATE_UNCONFIGURED) - { - state = USBCDCD_STATE_UNCONFIGURED; - if (initiated == true) - { - Semaphore_post(ReconnectSem); - //Semaphore_post(semTxSerial); - } - } - break; - - case USB_EVENT_RESUME: - cpt[5]++; - break; - - default: - break; - } - - return (0); -} - -/******************************************************************************** - * ======== cbTxHandler ======== - * Callback handler for the USB stack. - * - * Callback handler call by the USB stack to notify us on what has happened in - * regards to the keyboard. - * - * @param(_pcbData) A callback pointer provided by the client. - * - * @param(_event) Identifies the event that occurred in regards to - * this device. - * - * @param(_eventMsgData) A data value associated with a particular event. - * - * @param(_pEventMsgPtr) A data pointer associated with a particular event. - * - ********************************************************************************/ -static USBCDCDEventType cbTxHandler(void *_pcbData, USBCDCDEventType _event, - USBCDCDEventType _eventMsgData, - void *_pEventMsgPtr) -{ - cpt[8]++; - switch (_event) - { - case USB_EVENT_TX_COMPLETE: - { - cpt[9]++; - /* - * Data was sent, so there should be some space available on the - * buffer - */ - Semaphore_post(semTxSerial); - break; - } - default: - break; - } - - return (0); -} - -/******************************************************************** - * ======== USBCDCD_hwiHandler ======== - * This function calls the USB library's device interrupt handler. - ********************************************************************/ -void USBCDCD_hwiHandler(UArg arg0) -{ - USB0DeviceIntHandler(); -} - -/******************************************************************** - * ======== rxData ======== - *******************************************************************/ -static unsigned int rxData(unsigned char *_pBuff, - unsigned int _length, - unsigned int _timeout) -{ - unsigned int read = 0; - cpt[10]++; - - if (USBBufferDataAvailable(&rxBuffer) || Semaphore_pend(semRxSerial, _timeout)) - { - read = USBBufferRead(&rxBuffer, _pBuff, _length); - } - - return (read); -} - - -/******************************************************************** - * ======== txData ======== - ********************************************************************/ -static unsigned int txData(const unsigned char *_pBuff, - int _length, unsigned int _timeout) -{ - unsigned int bufferedCount = 0; - unsigned int sendCount = 0; - cpt[11]++; - - while (bufferedCount != _length) - { - /* Determine the buffer _size available */ - unsigned int buffAvailSize = USBBufferSpaceAvailable(&txBuffer); - - /* Determine how much needs to be sent */ - if ((_length - bufferedCount) > buffAvailSize) - { - sendCount = buffAvailSize; - } - else - { - sendCount = _length - bufferedCount; - } - - unsigned char *sendPtr; - /* Adjust the pointer to the data */ - sendPtr = (unsigned char *)_pBuff + bufferedCount; - - /* Place the contents into the USB BUffer */ - bufferedCount += USBBufferWrite(&txBuffer, sendPtr, sendCount); - - /* Pend until some data was sent through the USB*/ - if (!Semaphore_pend(semTxSerial, _timeout)) - { - break; - } - } - - return (bufferedCount); -} -//***************************************************************************** -// -// Interrupt handler for the UART which we are redirecting via USB. -// -//***************************************************************************** -void USB0Handler(void) -{ - USBCDCD_init(); -} - -void * USBDComposite = NULL; -/******************************************************************* - * ======== USBCDCD_init ======== - *******************************************************************/ -void USBCDCD_init(void) -{ - Semaphore_Params semParams; - Error_Block eb; - - Error_init(&eb); - /* Hwi_Handle hwi; - - // Install interrupt handler - hwi = Hwi_create(INT_USB0, USBCDCD_hwiHandler, NULL, &eb); - if (hwi == NULL) - { - System_abort("Can't create USB Hwi"); - } -*/ - /* RTOS primitives */ - Semaphore_Params_init(&semParams); - semParams.mode = Semaphore_Mode_BINARY; - semTxSerial = Semaphore_create(0, &semParams, &eb); - if (semTxSerial == NULL) - { - System_abort("Can't create TX semaphore"); - } - - semRxSerial = Semaphore_create(0, &semParams, &eb); - if (semRxSerial == NULL) - { - System_abort("Can't create RX semaphore"); - } - - semUSBConnected = Semaphore_create(0, &semParams, &eb); - if (semUSBConnected == NULL) - { - System_abort("Can't create USB semaphore"); - } - - gateTxSerial = GateMutex_create(NULL, &eb); - if (gateTxSerial == NULL) - { - System_abort("Can't create gate"); - } - - gateRxSerial = GateMutex_create(NULL, &eb); - if (gateRxSerial == NULL) - { - System_abort("Can't create gate"); - } - - gateUSBWait = GateMutex_create(NULL, &eb); - if (gateUSBWait == NULL) - { - System_abort("Could not create USB Wait gate"); - } - - /* State specific variables */ - state = USBCDCD_STATE_UNCONFIGURED; - - /* Set the USB stack mode to Device mode with VBUS monitoring */ - USBStackModeSet(0, eUSBModeForceDevice, 0); - - //should be done here only once for supporting firmware upgrade as composite devise - //if done several times in different places doesn't work in the upgrade. - USBBufferInit(&txBuffer); - USBBufferInit(&rxBuffer); - - //if (!USBDCDCCompositeInit(0, &g_sCDCDevice, &(g_sCompDevice.psDevices[0]))) - if (!USBDCDCInit(0, &g_sCDCDevice)) - { - System_abort("Error initializing the serial device"); - } - - /*if (!USBDDFUCompositeInit(0, &g_sDFUDevice, &(g_sCompDevice.psDevices[1]))) - { - System_abort("Error initializing the DFU device"); - } - // - // Pass the USB library our device information, initialize the USB - // controller and connect the device to the bus. - // - g_sCompDevice.sPrivateData.sDeviceDescriptor.bcdUSB = 0X200; - USBDComposite = USBDCompositeInit(0, &g_sCompDevice, DESCRIPTOR_BUFFER_SIZE, g_pui8DescriptorBuffer); - if (!USBDComposite) - { - System_abort("Error initializing the composite device"); - }*/ -} - -//----------------------------------------------------------- -void USBCDCD_Reinit(void) -{ - Error_Block eb; - Semaphore_Params semParams; - - Error_init(&eb); - - /* RTOS primitives */ - Semaphore_Params_init(&semParams); - semParams.mode = Semaphore_Mode_BINARY; - semTxSerial = Semaphore_create(0, &semParams, &eb); - if (semTxSerial == NULL) - { - System_abort("Can't create TX semaphore"); - } - - /* State specific variables */ - state = USBCDCD_STATE_UNCONFIGURED; - - /* Set the USB stack mode to Device mode with VBUS monitoring */ - USBStackModeSet(0, eUSBModeForceDevice, 0); - - // if (!USBDCDCInit(0, &g_sCDCDevice, &(g_sCompDevice.psDevices[0]))) - if (!USBDCDCInit(0, &g_sCDCDevice)) - { - System_abort("Error initializing the serial device"); - } -/* if (!USBDDFUCompositeInit(0, &g_sDFUDevice, &(g_sCompDevice.psDevices[1]))) - { - System_abort("Error initializing the DFU device"); - } - - // Pass the USB library our device information, initialize the USB - // controller and connect the device to the bus. - // - g_sCompDevice.sPrivateData.sDeviceDescriptor.bcdUSB = 0X200; - USBDComposite = USBDCompositeInit(0, &g_sCompDevice, DESCRIPTOR_BUFFER_SIZE, g_pui8DescriptorBuffer); - if (!USBDComposite) - { - System_abort("Error initializing the composite device"); - } -*/ -} - -//----------------------------------------------------------- -void USBCDC_close(void) -{ - uint32_t tick = 0; - char str[60]; - uint8_t len = 0; - tick = UsersysTickGet(); - - USBDCompositeTerm(USBDComposite); - - //Semaphore_post(semTxSerial); - //Semaphore_delete(&semTxSerial); - - //messageFlush(); - USBCDCD_Reinit(); - - len = usnprintf(str, 60, "\r\n USBCDCD_Reinit tick %d state %d",tick, state ); - cb_push_back (str, len); -} - -/************************************************************** - * ======== USBCDCD_receiveData ======== -************************************************************* */ -unsigned int USBCDCD_receiveData(unsigned char *_pBuff, - unsigned int _length, - unsigned int _timeout) -{ - unsigned int retValue = 0; - unsigned int key; - - switch (state) - { - case USBCDCD_STATE_UNCONFIGURED: - { - USBCDCD_waitForConnect(_timeout); - break; - } - case USBCDCD_STATE_INIT: - { - /* Acquire lock */ - key = GateMutex_enter(gateRxSerial); - - state = USBCDCD_STATE_IDLE; - - retValue = rxData(_pBuff, _length, _timeout); - - /* Release lock */ - GateMutex_leave(gateRxSerial, key); - break; - } - case USBCDCD_STATE_IDLE: - { - /* Acquire lock */ - key = GateMutex_enter(gateRxSerial); - - retValue = rxData(_pBuff, _length, _timeout); - - /* Release lock */ - GateMutex_leave(gateRxSerial, key); - break; - } - default: - break; - } - - return (retValue); -} - -/******************************************************* - * ======== USBCDCD_sendData ======== - *******************************************************/ -unsigned int USBCDCD_sendData(const unsigned char *_pBuff, - unsigned int _length, - unsigned int _timeout) -{ - unsigned int retValue = 0; - unsigned int key; - cpt[13]++; - - switch (state) - { - case USBCDCD_STATE_UNCONFIGURED: - { - USBCDCD_waitForConnect(_timeout); - break; - } - case USBCDCD_STATE_INIT: - { - /* Acquire lock */ - key = GateMutex_enter(gateTxSerial); - - state = USBCDCD_STATE_IDLE; - - retValue = txData(_pBuff, _length, _timeout); - - /* Release lock */ - GateMutex_leave(gateTxSerial, key); - break; - } - case USBCDCD_STATE_IDLE: - { - /* Acquire lock */ - key = GateMutex_enter(gateTxSerial); - - retValue = txData(_pBuff, _length, _timeout); - - /* Release lock */ - GateMutex_leave(gateTxSerial, key); - break; - } - default: - break; - } - - return (retValue); -} - -/************************************************ - * ======== USBCDCD_waitForConnect ======== - *************************************************/ -bool USBCDCD_waitForConnect(unsigned int _timeout) -{ - bool ret = true; - unsigned int key; - cpt[12]++; - - // Need exclusive access to prevent a race condition - key = GateMutex_enter(gateUSBWait); - - if (state == USBCDCD_STATE_UNCONFIGURED) - { - if (!Semaphore_pend(semUSBConnected, _timeout)) - { - ret = false; - } - } - - GateMutex_leave(gateUSBWait, key); - - return (ret); -} - -uint32_t SendChars(char* buffer,size_t length) -{ - USBSendData(buffer, length); - return 0; -} - -/****************************************************************************** - * ======== communicationTask ======== - * Task for this function is created statically. See the project's .cfg file. - * this communication task is created statically in system initialization, in blocking mode - * over one of the chosen ommunication methods (USB or Blutooth). - ******************************************************************************/ -void communicationTask(UArg arg0, UArg arg1) -{ - -// USBCDCD_init(); - cpt[14]++; - //UartBTInit(); - /* Block while the device is NOT connected to the USB */ - Semaphore_pend(initConnectionSem, BIOS_WAIT_FOREVER); - cpt[15]++; - - // Loop forever receiving commands - while(true) - { - cpt[16]++; -// receive_callback - //handle incoming data - } -} - diff --git a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.bak b/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.bak deleted file mode 100644 index 89f467e97..000000000 --- a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.bak +++ /dev/null @@ -1,1053 +0,0 @@ - -/************************************************************************************ - * this module is represents the access layer to USB STACK DRIVER - * used to communication over USB protocol - * the USB protocol implementation is inside the module no need for another includes - * ======== USBCDCD.c ======== - ************************************************************************************/ - -/* XDCtools Header files */ -#include <xdc/std.h> -#include <xdc/runtime/Error.h> -#include <xdc/runtime/System.h> - -/* BIOS Header files */ -#include <ti/sysbios/BIOS.h> -#include <ti/sysbios/gates/GateMutex.h> -#include <ti/sysbios/hal/Hwi.h> -#include <ti/sysbios/knl/Semaphore.h> -#include <ti/sysbios/knl/Task.h> - -#include <stdbool.h> -#include <stdint.h> - -/* driverlib Header files */ -#include <inc/hw_ints.h> -#include <inc/hw_types.h> -#include <inc/hw_uart.h> - -#include <driverlib/rom.h> -#include <driverlib/sysctl.h> -#include <driverlib/usb.h> -#include <inc/hw_memmap.h> - -/* usblib Header files */ -#include <usblib/usb-ids.h> -#include <usblib/usblib.h> -#include <usblib/usbcdc.h> -#include <usblib/device/usbdevice.h> -#include <usblib/device/usbdcdc.h> -#include <usblib/device/usbddfu-rt.h> -#include <usblib/device/usbdcomp.h> -#include "Drivers/USB_Communication/USBCDCD.h" - - -#include <utils/ustdlib.h> -#include "Common/Utilities/Utils.h" -//#include "usb_serial_structs.h" -#include "usb_serial_buffer.h" - -#if defined(TIVAWARE) -typedef uint32_t USBCDCDEventType; -#else -#define eUSBModeForceDevice USB_MODE_FORCE_DEVICE -typedef unsigned long USBCDCDEventType; -#endif - -/* Defines */ -#define USBBUFFERSIZE 256 -//**************************************************************************** -// -// A buffer into which the composite device can write the combined config -// descriptor. -// -//**************************************************************************** -#define DESCRIPTOR_BUFFER_SIZE (COMPOSITE_DDFU_SIZE + COMPOSITE_DCDC_SIZE) - -extern Semaphore_Handle updateSem; -extern Semaphore_Handle ReconnectSem; - -uint8_t g_pui8DescriptorBuffer[DESCRIPTOR_BUFFER_SIZE]; - -/* Typedefs */ -typedef volatile enum { - USBCDCD_STATE_IDLE = 0, - USBCDCD_STATE_INIT, - USBCDCD_STATE_UNCONFIGURED -} USBCDCD_USBState; - -/* Static variables and handles */ -static volatile USBCDCD_USBState state; -static unsigned char receiveBuffer[USBBUFFERSIZE]; -static unsigned char transmitBuffer[USBBUFFERSIZE]; -SerialBuffer inBuffer; -int expected_message_size; -int current_message_size; -static volatile uint32_t g_RxCount; -static GateMutex_Handle gateTxSerial; -static GateMutex_Handle gateRxSerial; -static GateMutex_Handle gateUSBWait; -static Semaphore_Handle semTxSerial; -static Semaphore_Handle semRxSerial; -static Semaphore_Handle semUSBConnected; -extern Semaphore_Handle initConnectionSem; - -#define FLAG_STATUS_UPDATE 0 -#define FLAG_USB_CONFIGURED 1 -#define FLAG_SENDING_BREAK 2 -static volatile uint32_t g_ui32Flags; - -/* Function prototypes */ -/* -static USBCDCDEventType cbRxHandler(void *cbData, USBCDCDEventType event, - USBCDCDEventType _eventMsgData, - void *eventMsgPtr); -static USBCDCDEventType cbSerialHandler(void *cbData, USBCDCDEventType event, - USBCDCDEventType _eventMsgData, - void *eventMsgPtr); -static USBCDCDEventType cbTxHandler(void *cbData, USBCDCDEventType event, - USBCDCDEventType _eventMsgData, - void *eventMsgPtr); - */ -uint32_t checkpoints [18] = {0}; -uint32_t TxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData); -uint32_t RxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue,void *pvMsgData); -uint32_t ControlHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData); -static Void USBCDCD_hwiHandler(UArg arg0); -void USBCDCD_init(void); - -unsigned int USBCDCD_receiveData(unsigned char *_pBuff, - unsigned int _length, - unsigned int _timeout); -bool USBCDCD_waitForConnect(unsigned int _timeout); - -static void (*callback)(char* buffer, size_t length); - -/* The languages supported by this device. */ -const unsigned char langDescriptor[] = { - 4, - USB_DTYPE_STRING, - USBShort(USB_LANG_EN_US) -}; - -/* The manufacturer string. */ -const unsigned char manufacturerString[] = { - (17 + 1) * 2, - USB_DTYPE_STRING, - 'T', 0, 'e', 0, 'x', 0, 'a', 0, 's', 0, ' ', 0, 'I', 0, 'n', 0, 's', 0, - 't', 0, 'r', 0, 'u', 0, 'm', 0, 'e', 0, 'n', 0, 't', 0, 's', 0, -}; - -/* The product string. */ -const unsigned char productString[] = { - 2 + (13 * 2), - USB_DTYPE_STRING, - 'T', 0, 'w', 0, 'i', 0, 'n', 0, 'e', 0, ' ', 0, 'J', 0, 'i', 0, - 'g', 0, ' ', 0, 'U', 0, 'S', 0, 'B', 0, -}; - -/* The serial number string. */ -const unsigned char serialNumberString[] = { - (8 + 1) * 2, - USB_DTYPE_STRING, - '1', 0, '2', 0, '3', 0, '4', 0, '5', 0, '6', 0, '7', 0, '8', 0 -}; - -/* The interface description string. */ -const unsigned char controlInterfaceString[] = { - 2 + (21 * 2), - USB_DTYPE_STRING, - 'J', 0, 'i', 0, 'g', 0, ' ', 0, 'C', 0, 'o', 0, 'n', 0, 't', 0, - 'r', 0, 'o', 0, 'l', 0, ' ', 0, 'I', 0, 'n', 0, 't', 0, 'e', 0, - 'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0 -}; - -/* The configuration description string. */ -const unsigned char configString[] = { - 2 + (26 * 2), - USB_DTYPE_STRING, - 'S', 0, 'e', 0, 'l', 0, 'f', 0, ' ', 0, 'P', 0, 'o', 0, 'w', 0, - 'e', 0, 'r', 0, 'e', 0, 'd', 0, ' ', 0, 'C', 0, 'o', 0, 'n', 0, - 'f', 0, 'i', 0, 'g', 0, 'u', 0, 'r', 0, 'a', 0, 't', 0, 'i', 0, - 'o', 0, 'n', 0 -}; - -//***************************************************************************** -// -// The descriptor string table. -// -//***************************************************************************** -const uint8_t * const g_ppui8StringDescriptors[] = -{ - langDescriptor, - manufacturerString, - productString, - serialNumberString, - controlInterfaceString, - configString -}; - -#define NUM_STRING_DESCRIPTORS (sizeof(g_ppui8StringDescriptors) / \ - sizeof(g_ppui8StringDescriptors[0])) - -/* The descriptor string table. */ -const unsigned char * const stringDescriptors[] = { - langDescriptor, - manufacturerString, - productString, - serialNumberString, - controlInterfaceString, - configString -}; - -#define STRINGDESCRIPTORSCOUNT (sizeof(stringDescriptors) / \ - sizeof(unsigned char *)) - -tUSBBuffer txBuffer; -tUSBBuffer rxBuffer; -static tUSBDCDCDevice g_sCDCDevice; - -tUSBBuffer rxBuffer = -{ - false, /* This is a receive buffer. */ - RxHandler, /* pfnCallback */ - (void *)&g_sCDCDevice, /* Callback data is our device pointer. */ - USBDCDCPacketRead, /* pfnTransfer */ - USBDCDCRxPacketAvailable, /* pfnAvailable */ - (void *)&g_sCDCDevice, /* pvHandle */ - receiveBuffer, /* pcBuffer */ - USBBUFFERSIZE, /* ulBufferSize */ - {{0, 0, 0, 0}, 0, 0} /* private data workspace */ -}; - -tUSBBuffer txBuffer = -{ - true, /* This is a transmit buffer. */ - TxHandler, /* pfnCallback */ - (void *)&g_sCDCDevice, /* Callback data is our device pointer. */ - USBDCDCPacketWrite, /* pfnTransfer */ - USBDCDCTxPacketAvailable, /* pfnAvailable */ - (void *)&g_sCDCDevice, /* pvHandle */ - transmitBuffer, /* pcBuffer */ - USBBUFFERSIZE, /* ulBufferSize */ - {{0, 0, 0, 0}, 0, 0} /* private data workspace */ -}; - -static tUSBDCDCDevice g_sCDCDevice = -{ - USB_VID_TI_1CBE, - USB_PID_SERIAL, - 0, - USB_CONF_ATTR_SELF_PWR, - - ControlHandler, - (void *)&g_sCDCDevice, - - USBBufferEventCallback, - (void *)&rxBuffer, - - USBBufferEventCallback, - (void *)&txBuffer, - - stringDescriptors, - STRINGDESCRIPTORSCOUNT -}; - - - -static tLineCoding g_sLineCoding = { - 115200, /* 115200 baud rate. */ - 1, /* 1 Stop Bit. */ - 0, /* No Parity. */ - 8 /* 8 Bits of data. */ -}; - - -uint32_t SendChars(char* buffer,size_t length) -{ - USBCDCD_sendData(buffer, length,10); - checkpoints[4]++; - -} -//***************************************************************************** -// -//! Waits for a character from the USB port. -//! -//! This function gets a character from the USB receive buffer. -//! If there are no characters available, this function waits until a -//! character is received before returning. -//! -//! \return Returns the character read from the USB port. -// -//***************************************************************************** -char USBGetChar(void) -{ - uint8_t ucChar; - int len = 0; - //int leng; -// char str[60]; - checkpoints[5]++; - - len = USBCDCD_receiveData(&ucChar, 1, BIOS_WAIT_FOREVER); - if (len !=1) - { -// leng = usnprintf(str, 60, "\r\n USBGetChar len error %d",len ); -// cb_push_back (str, leng); - Task_sleep(1); - len = USBCDCD_receiveData(&ucChar, 1, BIOS_WAIT_FOREVER); -// leng = usnprintf(str, 60, "\r\n USBGetChar len second %d",len ); -// cb_push_back (str, leng); - } - // - // Now return the char. - // - return (ucChar); -} - -/*unsigned int USBSendData(const unsigned char *_pBuff,unsigned int _length) -{ - //StoreUSBByte ((uint8_t *)_pBuff,_length,false); - return USBCDCD_sendData(_pBuff,_length,10); -} -*/ -void USBFlush(void) -{ - USBBufferFlush(&rxBuffer); -} - - -//***************************************************************************** -// -// Set the state of the RS232 RTS and DTR signals. -// -//***************************************************************************** -void SetControlLineState(uint16_t ui16State) -{ - // - // TODO: If configured with GPIOs controlling the handshake lines, - // set them appropriately depending upon the flags passed in the wValue - // field of the request structure passed. - // -} - -//***************************************************************************** -// -// Get the communication parameters in use on the UART. -// -//***************************************************************************** -void GetLineCoding(tLineCoding *psLineCoding) -{ - psLineCoding->ui32Rate = 9600; - psLineCoding->ui8Databits = 8; - psLineCoding->ui8Parity = USB_CDC_PARITY_NONE; - psLineCoding->ui8Stop = USB_CDC_STOP_BITS_1; -} -/******************************************************************** - * ======== USBCDCD_hwiHandler ======== - * This function calls the USB library's device interrupt handler. - ********************************************************************/ -static Void USBCDCD_hwiHandler(UArg arg0) -{ - USB0DeviceIntHandler(); - checkpoints[6]++; - -} -//***************************************************************************** -// -// This function is called whenever serial data is received from the UART. -// It is passed the accumulated error flags from each character received in -// this interrupt and determines from them whether or not an interrupt -// notification to the host is required. -// -// If a notification is required and the control interrupt endpoint is idle, -// we send the notification immediately. If the endpoint is not idle, we -// accumulate the errors in a global variable which will be checked on -// completion of the previous notification and used to send a second one -// if necessary. -// -//***************************************************************************** -void CheckForSerialStateChange(const tUSBDCDCDevice *psDevice, uint32_t ui32Errors) -{ - uint16_t ui16SerialState; - - // - // Clear our USB serial state. Since we are faking the handshakes, always - // set the TXCARRIER (DSR) and RXCARRIER (DCD) bits. - // - ui16SerialState = USB_CDC_SERIAL_STATE_TXCARRIER | - USB_CDC_SERIAL_STATE_RXCARRIER; - - // - // Are any error bits set? - // - if(ui32Errors) - { - // - // At least one error is being notified so translate from our hardware - // error bits into the correct state markers for the USB notification. - // - if(ui32Errors & UART_DR_OE) - { - ui16SerialState |= USB_CDC_SERIAL_STATE_OVERRUN; - } - - if(ui32Errors & UART_DR_PE) - { - ui16SerialState |= USB_CDC_SERIAL_STATE_PARITY; - } - - if(ui32Errors & UART_DR_FE) - { - ui16SerialState |= USB_CDC_SERIAL_STATE_FRAMING; - } - - if(ui32Errors & UART_DR_BE) - { - ui16SerialState |= USB_CDC_SERIAL_STATE_BREAK; - } - - // - // Call the CDC driver to notify the state change. - // - USBDCDCSerialStateChange((void *)psDevice, ui16SerialState); - } -} - -//***************************************************************************** -// -// Handles CDC driver notifications related to control and setup of the device. -// -// \param pvCBData is the client-supplied callback pointer for this channel. -// \param ulEvent identifies the event we are being notified about. -// \param ulMsgValue is an event-specific value. -// \param pvMsgData is an event-specific pointer. -// -// This function is called by the CDC driver to perform control-related -// operations on behalf of the USB host. These functions include setting -// and querying the serial communication parameters, setting handshake line -// states and sending break conditions. -// -// \return The return value is event-specific. -// -//***************************************************************************** -uint32_t ControlHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData) -{ - checkpoints[3]++; - - // - // Which event are we being asked to process? - // - switch(ui32Event) - { - // - // We are connected to a host and communication is now possible. - // - case USB_EVENT_CONNECTED: - { - checkpoints[11]++; - // - // Now connected and ready for normal operation. - // - HWREGBITW(&g_ui32Flags, FLAG_USB_CONFIGURED) = 1; - - // - // Flush our buffers. - // - USBBufferFlush(&txBuffer); - USBBufferFlush(&rxBuffer); - - //TODO: Notify connection! - - // - // Set the command status update flag. - // - HWREGBITW(&g_ui32Flags, FLAG_STATUS_UPDATE) = 1; - - break; - } - - // - // The host has disconnected. - // - case USB_EVENT_DISCONNECTED: - { - //checkpoints[12]++; - // - // No longer connected. - // - HWREGBITW(&g_ui32Flags, FLAG_USB_CONFIGURED) = 0; - - //TODO: Notify disconnection! - - // - // Set the command status update flag. - // - HWREGBITW(&g_ui32Flags, FLAG_STATUS_UPDATE) = 1; - - break; - } - - // - // Return the current serial communication parameters. - // - case USBD_CDC_EVENT_GET_LINE_CODING: - { - //checkpoints[13]++; - GetLineCoding(pvMsgData); - break; - } - - // - // Set the current serial communication parameters. - // - case USBD_CDC_EVENT_SET_LINE_CODING: - { - //checkpoints[14]++; - GetLineCoding(pvMsgData); - break; - } - - // - // Set the current serial communication parameters. - // - case USBD_CDC_EVENT_SET_CONTROL_LINE_STATE: - { - checkpoints[15]++; - SetControlLineState((uint16_t)ui32MsgValue); - break; - } - - // - // Send a break condition on the serial line. - // - case USBD_CDC_EVENT_SEND_BREAK: - { - checkpoints[12]++; - break; - } - - // - // Clear the break condition on the serial line. - // - case USBD_CDC_EVENT_CLEAR_BREAK: - { - checkpoints[13]++; - break; - } - - // - // Ignore SUSPEND and RESUME for now. - // - case USB_EVENT_SUSPEND: - case USB_EVENT_RESUME: - { - checkpoints[14]++; - break; - } - - // - // We don't expect to receive any other events. Ignore any that show - // up in a release build or hang in a debug build. - // - default: - { -#ifdef DEBUG - while(1); -#else - break; -#endif - } - - } - - return(0); -} -void handleRx(void) -{ - uint32_t ui32Read; - uint8_t ui8Char; - uint8_t size[4]; - int size_bar = 0; - checkpoints[2]++; - - if (expected_message_size == 0) - { - do - { - ui32Read = USBBufferRead((tUSBBuffer *)&rxBuffer, &ui8Char, 1); - - if(ui32Read) - { - size[size_bar++] = ui8Char; - } - - } while(size_bar < 4); - - expected_message_size = *(int *)size; - } - - do - { - ui32Read = USBBufferRead((tUSBBuffer *)&rxBuffer, &ui8Char, 1); - - // Did we get a character? - if(ui32Read) - { - insertArray(&inBuffer, ui8Char); - current_message_size++; - } - - if (current_message_size == expected_message_size) - { - g_RxCount += current_message_size; - expected_message_size = 0; - current_message_size = 0; - break; - } - - } while(ui32Read); -} - -//***************************************************************************** -// -// Handles CDC driver notifications related to the transmit channel (data to -// the USB host). -// -// \param pvCBData is the client-supplied callback pointer for this channel. -// \param ui32Event identifies the event we are being notified about. -// \param ui32MsgValue is an event-specific value. -// \param pvMsgData is an event-specific pointer. -// -// This function is called by the CDC driver to notify us of any events -// related to operation of the transmit data channel (the IN channel carrying -// data to the USB host). -// -// \return The return value is event-specific. -// -//***************************************************************************** -uint32_t TxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData) -{ - // - // Which event have we been sent? - // - checkpoints[0]++; - switch(ui32Event) - { - case USB_EVENT_TX_COMPLETE: - { - // - // Since we are using the USBBuffer, we don't need to do anything - // here. - // - break; - } - - // - // We don't expect to receive any other events. Ignore any that show - // up in a release build or hang in a debug build. - // - default: - { -#ifdef DEBUG - while(1); -#else - break; -#endif - } - } - return(0); -} - -//***************************************************************************** -// -// Handles CDC driver notifications related to the receive channel (data from -// the USB host). -// -// \param pvCBData is the client-supplied callback data value for this channel. -// \param ui32Event identifies the event we are being notified about. -// \param ui32MsgValue is an event-specific value. -// \param pvMsgData is an event-specific pointer. -// -// This function is called by the CDC driver to notify us of any events -// related to operation of the receive data channel (the OUT channel carrying -// data from the USB host). -// -// \return The return value is event-specific. -// -//***************************************************************************** -uint32_t RxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue,void *pvMsgData) -{ - // - // Which event are we being sent? - // - checkpoints[1]++; - - switch(ui32Event) - { - // - // A new packet has been received. - // - case USB_EVENT_RX_AVAILABLE: - { - // - // Feed some characters into the UART TX FIFO and enable the - // interrupt so we are told when there is more space. - // - handleRx(); - break; - } - - // - // We are being asked how much unprocessed data we have still to - // process. We return 0 if the UART is currently idle or 1 if it is - // in the process of transmitting something. The actual number of - // bytes in the UART FIFO is not important here, merely whether or - // not everything previously sent to us has been transmitted. - // - case USB_EVENT_DATA_REMAINING: - { - // - // Get the number of bytes in the buffer and add 1 if some data - // still has to clear the transmitter. - return(0); - } - - // - // We are being asked to provide a buffer into which the next packet - // can be read. We do not support this mode of receiving data so let - // the driver know by returning 0. The CDC driver should not be sending - // this message but this is included just for illustration and - // completeness. - // - case USB_EVENT_REQUEST_BUFFER: - { - return(0); - } - - // - // We don't expect to receive any other events. Ignore any that show - // up in a release build or hang in a debug build. - // - default: -#ifdef DEBUG - while(1); -#else - break; -#endif - } - - return(0); -} - - -//Execute this function on UART0 Interrupt; -void InitUSB(void) -{ - checkpoints[9]++; - CheckForSerialStateChange(&g_sCDCDevice, 0); -} -//***************************************************************************** -// -// Interrupt handler for the UART which we are redirecting via USB. -// -//***************************************************************************** -void USB0Handler(void) -{ - InitUSB(); - checkpoints[7]++; - -} - - - - -void * USBDComposite = NULL; -/******************************************************************* - * ======== USBCDCD_init ======== - *******************************************************************/ -void USBCDCD_init(void) -{ - Hwi_Handle hwi; - Error_Block eb; - Semaphore_Params semParams; - - Error_init(&eb); - - /* Install interrupt handler */ - hwi = Hwi_create(INT_USB0, USBCDCD_hwiHandler, NULL, &eb); - if (hwi == NULL) - { - System_abort("Can't create USB Hwi"); - } - - /* RTOS primitives */ - Semaphore_Params_init(&semParams); - semParams.mode = Semaphore_Mode_BINARY; - semTxSerial = Semaphore_create(0, &semParams, &eb); - if (semTxSerial == NULL) - { - System_abort("Can't create TX semaphore"); - } - - semRxSerial = Semaphore_create(0, &semParams, &eb); - if (semRxSerial == NULL) - { - System_abort("Can't create RX semaphore"); - } - - semUSBConnected = Semaphore_create(0, &semParams, &eb); - if (semUSBConnected == NULL) - { - System_abort("Can't create USB semaphore"); - } - - gateTxSerial = GateMutex_create(NULL, &eb); - if (gateTxSerial == NULL) - { - System_abort("Can't create gate"); - } - - gateRxSerial = GateMutex_create(NULL, &eb); - if (gateRxSerial == NULL) - { - System_abort("Can't create gate"); - } - - gateUSBWait = GateMutex_create(NULL, &eb); - if (gateUSBWait == NULL) - { - System_abort("Could not create USB Wait gate"); - } - - /* State specific variables */ - state = USBCDCD_STATE_UNCONFIGURED; - - /* Set the USB stack mode to Device mode with VBUS monitoring */ - USBStackModeSet(0, eUSBModeForceDevice, 0); - - //should be done here only once for supporting firmware upgrade as composite devise - //if done several times in different places doesn't work in the upgrade. - USBBufferInit(&txBuffer); - USBBufferInit(&rxBuffer); -/* - if (!USBDCDCCompositeInit(0, &g_sCDCDevice, &(g_sCompDevice.psDevices[0]))) - { - System_abort("Error initializing the serial device"); - } - - if (!USBDDFUCompositeInit(0, &g_sDFUDevice, &(g_sCompDevice.psDevices[1]))) - { - System_abort("Error initializing the DFU device"); - } - - // - // Pass the USB library our device information, initialize the USB - // controller and connect the device to the bus. - // - g_sCompDevice.sPrivateData.sDeviceDescriptor.bcdUSB = 0X200; - USBDComposite = USBDCompositeInit(0, &g_sCompDevice, DESCRIPTOR_BUFFER_SIZE, g_pui8DescriptorBuffer); - if (!USBDComposite) - { - System_abort("Error initializing the composite device"); - } - */ - if (!USBDCDCInit(0, &g_sCDCDevice)) - { - System_abort("Error initializing the serial device"); - } - -} - -//----------------------------------------------------------- -void USBCDCD_Reinit(void) -{ - Error_Block eb; - Semaphore_Params semParams; - - Error_init(&eb); - - /* RTOS primitives */ - Semaphore_Params_init(&semParams); - semParams.mode = Semaphore_Mode_BINARY; - semTxSerial = Semaphore_create(0, &semParams, &eb); - if (semTxSerial == NULL) - { - System_abort("Can't create TX semaphore"); - } - - /* State specific variables */ - state = USBCDCD_STATE_UNCONFIGURED; - - /* Set the USB stack mode to Device mode with VBUS monitoring */ - USBStackModeSet(0, eUSBModeForceDevice, 0); - - if (!USBDCDCInit(0, &g_sCDCDevice)) - { - System_abort("Error initializing the serial device"); - } - -} - -//----------------------------------------------------------- -void USBCDC_close(void) -{ - uint32_t tick = 0; - char str[60]; - uint8_t len = 0; - tick = UsersysTickGet(); - - USBDCompositeTerm(USBDComposite); - - Semaphore_post(semTxSerial); - Semaphore_delete(&semTxSerial); - - USBCDCD_Reinit(); - - len = usnprintf(str, 60, "\r\n USBCDCD_Reinit tick %d state %d",tick, state ); - cb_push_back (str, len); -} - -/************************************************************** - * ======== USBCDCD_receiveData ======== -************************************************************* */ -unsigned int USBCDCD_receiveData(unsigned char *_pBuff, - unsigned int _length, - unsigned int _timeout) -{ - unsigned int retValue = 0; - unsigned int key; - checkpoints[9]++; - - switch (state) - { - case USBCDCD_STATE_UNCONFIGURED: - { - USBCDCD_waitForConnect(_timeout); - break; - } - case USBCDCD_STATE_INIT: - { - /* Acquire lock */ - key = GateMutex_enter(gateRxSerial); - - state = USBCDCD_STATE_IDLE; - - handleRx(); - //retValue = rxData(_pBuff, _length, _timeout); - - /* Release lock */ - GateMutex_leave(gateRxSerial, key); - break; - } - case USBCDCD_STATE_IDLE: - { - /* Acquire lock */ - key = GateMutex_enter(gateRxSerial); - handleRx(); - //retValue = rxData(_pBuff, _length, _timeout); - - /* Release lock */ - GateMutex_leave(gateRxSerial, key); - break; - } - default: - break; - } - - return (retValue); -} - -/******************************************************* - * ======== USBCDCD_sendData ======== - *******************************************************/ -unsigned int USBCDCD_sendData(char *_pBuff, - unsigned int _length, - unsigned int _timeout) -{ - uint8_t size[4]; - size[3] = (_length>>24) & 0xFF; - size[2] = (_length>>16) & 0xFF; - size[1] = (_length>>8) & 0xFF; - size[0] = _length & 0xFF; - checkpoints[10]++; - - USBBufferWrite((tUSBBuffer *)&txBuffer, size, 4); - return USBBufferWrite((tUSBBuffer *)&txBuffer, (uint8_t*)_pBuff, _length); -} - -/************************************************ - * ======== USBCDCD_waitForConnect ======== - *************************************************/ -bool USBCDCD_waitForConnect(unsigned int _timeout) -{ - bool ret = true; - unsigned int key; - - // Need exclusive access to prevent a race condition - key = GateMutex_enter(gateUSBWait); - checkpoints[16]++; - - if (state == USBCDCD_STATE_UNCONFIGURED) - { - if (!Semaphore_pend(semUSBConnected, _timeout)) - { - ret = false; - } - } - - GateMutex_leave(gateUSBWait, key); - - return (ret); -} -void RegisterReceiveCallback(void (*callback_ptr)(char* buffer, size_t length)) -{ - callback = callback_ptr; -} - -extern Semaphore_Handle initConnectionSem; - -/****************************************************************************** - * ======== communicationTask ======== - * Task for this function is created statically. See the project's .cfg file. - * this communication task is created statically in system initialization, in blocking mode - * over one of the chosen ommunication methods (USB or Blutooth). - ******************************************************************************/ -void communicationTask(UArg arg0, UArg arg1) -{ - - uint32_t ui32RxCount; - - ui32RxCount = 0; - g_RxCount = 0; - - USBCDCD_init(); - //UartBTInit(); - //Clock_Params_init(&clkParams); - - //create clock timeout counter - //clkParams.period = 0; - //clkParams.startFlag = FALSE; - //communicationTimeoutClock = Clock_create(CommunicationLoseClockHandle, 0, &clkParams, NULL); - /* Block while the device is NOT connected to the USB */ - Semaphore_pend(initConnectionSem, BIOS_WAIT_FOREVER); - - // Loop forever receiving commands - while(true) - { - if(ui32RxCount != g_RxCount) - { - ui32RxCount = g_RxCount; - checkpoints[17]++; - - if (callback != NULL) - { - callback(inBuffer.buffer,inBuffer.used); - } - - freeArray(&inBuffer); - initArray(&inBuffer, 1); - } - - } -} - diff --git a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.c b/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.c index 005137f73..130518edf 100644 --- a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.c +++ b/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.c @@ -45,7 +45,8 @@ #include <utils/ustdlib.h> #include "Common/Utilities/Utils.h" -#include "usb_serial_buffer.h" + +#include "Communication/CommunicationTask.h" #if defined(TIVAWARE) typedef uint32_t USBCDCDEventType; @@ -80,15 +81,15 @@ typedef volatile enum { static volatile USBCDCD_USBState state; static unsigned char receiveBuffer[USBBUFFERSIZE]; static unsigned char transmitBuffer[USBBUFFERSIZE]; -SerialBuffer inBuffer; + int expected_message_size; int current_message_size; static volatile uint32_t g_RxCount; -static GateMutex_Handle gateTxSerial; +//static GateMutex_Handle gateTxSerial; static GateMutex_Handle gateRxSerial; static GateMutex_Handle gateUSBWait; static Semaphore_Handle semTxSerial; -static Semaphore_Handle semRxSerial; +//static Semaphore_Handle semRxSerial; static Semaphore_Handle semUSBConnected; extern Semaphore_Handle initConnectionSem; @@ -123,8 +124,6 @@ unsigned int USBCDCD_receiveData(unsigned char *_pBuff, unsigned int _timeout); bool USBCDCD_waitForConnect(unsigned int _timeout); -static void (*callback)(char* buffer, size_t length); - /* The languages supported by this device. */ const unsigned char langDescriptor[] = { 4, @@ -264,15 +263,10 @@ static tLineCoding g_sLineCoding = { 8 /* 8 Bits of data. */ }; -Mailbox_Handle CommunicationRxMsgQ = NULL; -typedef struct CommRxMessage{ - uint16_t messageId; - uint16_t msgSize; -}CommRxMessageStruc; uint32_t SendChars(char* buffer,size_t length) { - return USBCDCD_sendData(buffer, length,10); + return CommunicationTaskSendMessage(buffer, length); } //***************************************************************************** // @@ -561,7 +555,6 @@ void handleRx(void) uint8_t size[4]; int size_bar = 0; checkpoints[2]++; - CommRxMessageStruc Message; if (expected_message_size == 0) { @@ -577,7 +570,7 @@ if (expected_message_size == 0) } while(size_bar < 4); expected_message_size = *(int *)size; - initArray(&inBuffer, expected_message_size); + initArray( expected_message_size); } do @@ -587,17 +580,14 @@ if (expected_message_size == 0) // Did we get a character? if(ui32Read) { - insertArray(&inBuffer, ui8Char); + insertArray(ui8Char); current_message_size++; } if (current_message_size == expected_message_size) { g_RxCount += current_message_size; - Message.messageId = 1; - Message.msgSize = g_RxCount; - if (CommunicationRxMsgQ != NULL) - /*retcode =*/ Mailbox_post(CommunicationRxMsgQ , &Message, BIOS_NO_WAIT); + CommunicationTaskMessageReceived(g_RxCount); expected_message_size = 0; current_message_size = 0; break; @@ -762,9 +752,7 @@ void * USBDComposite = NULL; *******************************************************************/ void USBCDCD_init(void) { - Semaphore_Params semParams; Error_Block eb; - CommunicationRxMsgQ = Mailbox_create(sizeof(CommRxMessageStruc), 5, NULL,NULL); Error_init(&eb); @@ -776,7 +764,6 @@ void USBCDCD_init(void) System_abort("Can't create USB Hwi"); } - initArray(&inBuffer, 1); /* RTOS primitives */ /*Semaphore_Params_init(&semParams); @@ -968,53 +955,5 @@ bool USBCDCD_waitForConnect(unsigned int _timeout) return (ret); } -void RegisterReceiveCallback(void (*callback_ptr)(char* buffer, size_t length)) -{ - callback = callback_ptr; -} - -extern Semaphore_Handle initConnectionSem; - -/****************************************************************************** - * ======== communicationTask ======== - * Task for this function is created statically. See the project's .cfg file. - * this communication task is created statically in system initialization, in blocking mode - * over one of the chosen ommunication methods (USB or Blutooth). - ******************************************************************************/ -void communicationTask(UArg arg0, UArg arg1) -{ - uint32_t ui32RxCount; - CommRxMessageStruc Message; - - ui32RxCount = 0; - g_RxCount = 0; - - USBCDCD_init(); - //UartBTInit(); - /* Block while the device is NOT connected to the USB */ -// Semaphore_pend(initConnectionSem, BIOS_WAIT_FOREVER); - cpt[15]++; - - while(1) - { - Mailbox_pend(CommunicationRxMsgQ , &Message, BIOS_WAIT_FOREVER); - switch (Message.messageId) - { - case 1: - ui32RxCount += Message.msgSize; - if (callback != NULL) - { - callback(inBuffer.buffer,inBuffer.used); - } - - freeArray(&inBuffer); - initArray(&inBuffer, 1); - break; - default: - break; - } - } - -} diff --git a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.h b/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.h index 742f94b1f..c2bedce72 100644 --- a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.h +++ b/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.h @@ -15,6 +15,7 @@ extern "C" { #include <xdc/std.h> #include <stdbool.h> +#include "include.h" /****************************************************************** * ======== USBCDCD_init ======== @@ -64,8 +65,6 @@ unsigned int USBCDCD_sendData(const unsigned char *_pBuff, unsigned int USBCDCD_receiveData(unsigned char *_pBuff, unsigned int _length, unsigned int _timeout); -void RegisterReceiveCallback(void (*callback_ptr)(char* buffer, size_t length)); - uint32_t SendChars(char* buffer,size_t length); /********************************************************** @@ -80,8 +79,6 @@ void USBCDC_close(void); void USBCDCD_hwiHandler(UArg arg0); uint32_t SendChars(char* buffer,size_t length); -void RegisterReceiveCallback(void (*callback_ptr)(char* buffer, size_t length)); - #ifdef __cplusplus } #endif diff --git a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/usb_serial_buffer.c b/Software/Embedded_SW/Embedded/Drivers/USB_Communication/usb_serial_buffer.c deleted file mode 100644 index 65ff1ddb8..000000000 --- a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/usb_serial_buffer.c +++ /dev/null @@ -1,29 +0,0 @@ -/* - * usb_serial_buffer.c - * - * Created on: Sep 24, 2017 - * Author: Roy - */ - -#include "include.h" -#include <stdlib.h> -#include "usb_serial_buffer.h" - -char Buffer[2000]; -void initArray(SerialBuffer *a, size_t initialSize) { - assert (initialSize < 2000); - a->buffer = Buffer; - a->used = 0; - a->size = initialSize; -} - -void insertArray(SerialBuffer *a, char element) { - // a->used is the number of used entries, because a->array[a->used++] updates a->used only *after* the array has been accessed. - // Therefore a->used can go up to a->size - a->buffer[a->used++] = element; -} - -void freeArray(SerialBuffer *a) { - a->used = a->size = 0; -} - diff --git a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/usb_serial_buffer.h b/Software/Embedded_SW/Embedded/Drivers/USB_Communication/usb_serial_buffer.h deleted file mode 100644 index 561f84b29..000000000 --- a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/usb_serial_buffer.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * usb_serial_buffer.h - * - * Created on: Sep 24, 2017 - * Author: Roy - */ -#include <stdlib.h> - -struct serialBuffer { - char *buffer; - size_t used; - size_t size; -} typedef SerialBuffer; - -void initArray(SerialBuffer *a, size_t initialSize); - -void insertArray(SerialBuffer *a, char element); - -void freeArray(SerialBuffer *a); - |
