#include #include #include #include "inc/hw_ints.h" #include "inc/hw_memmap.h" #include "inc/hw_types.h" #include "driverlib/debug.h" #include "driverlib/fpu.h" #include "driverlib/gpio.h" #include "driverlib/interrupt.h" #include "driverlib/pin_map.h" #include "driverlib/sysctl.h" #include "driverlib/systick.h" #include "driverlib/timer.h" #include "driverlib/rom.h" #include "usblib/usblib.h" #include "usblib/usb-ids.h" #include "usblib/device/usbdevice.h" #include "usblib/device/usbdbulk.h" #include "utils/ustdlib.h" #include "usb_bulk_structs.h" #include "driverlib/eeprom.h" #include "ADC_TM4C123G.h" #include "ADC_TM4C123.h" //***************************************************************************** // The system tick rate expressed both as ticks per second and a millisecond // period. //***************************************************************************** #define SYSTICKS_PER_SECOND 100 #define SYSTICK_PERIOD_MS (1000 / SYSTICKS_PER_SECOND) //***************************************************************************** // The global system tick counter. //***************************************************************************** volatile uint32_t g_ui32SysTickCount = 0; //***************************************************************************** // Variables tracking transmit and receive counts. //***************************************************************************** volatile uint32_t g_ui32TxCount = 0; volatile uint32_t g_ui32RxCount = 0; //***************************************************************************** // // Flags used to pass commands from interrupt context to the main loop. // //***************************************************************************** #define COMMAND_PACKET_RECEIVED 0x00000001 #define COMMAND_STATUS_UPDATE 0x00000002 volatile uint32_t g_ui32Flags = 0; uint32_t pui32Data[256]; unsigned char Error_data[5]={0x77,0x72,0x6F,0x6E,0x67};//"wrong data"; //***************************************************************************** // Global flag indicating that a USB configuration has been set. //***************************************************************************** static volatile bool g_bUSBConfigured = false; //***************************************************************************** // Interrupt handler for the system tick counter. //***************************************************************************** void SysTickIntHandler(void) { g_ui32SysTickCount++; } /*********************************************************************************************************** * Function Name : Ascii_To_Num * Description of the function : This function is used to hex values into actual numbers. * Passed parameter : Rx_Data * Return value : Rx_Data ***********************************************************************************************************/ int Ascii_To_Num(int Rx_Data) { if((Rx_Data >= 0x30) && (Rx_Data <= 0x39)) { Rx_Data = Rx_Data - 0x30; } else if((Rx_Data >= 0x61) && (Rx_Data <= 0x66 )) { switch(Rx_Data) { case 0x61 : Rx_Data = 0x0A; break; case 0x62 : Rx_Data = 0x0B; break; case 0x63 : Rx_Data = 0x0C; break; case 0x64 : Rx_Data = 0x0D; break; case 0x65 : Rx_Data = 0x0E; break; case 0x66 : Rx_Data = 0x0F; break; } } return Rx_Data; } /*********************************************************************************************************** * Function Name : EchoNewDataToHost * Description of the function : Receive new data and echo it back to the host. * Passed parameter : tUSBDBulkDevice *psDevice : instance data for the device whose data is to be processed. uint8_t *pui8Data : points to the newly received data in the USB receive buffer. uint32_t ui32NumBytes : is the number of bytes of data available to be processed. * Return value : static uint32_t ui32Count : is the number of bytes of data processed. ***********************************************************************************************************/ static uint32_t EchoNewDataToHost(tUSBDBulkDevice *psDevice, uint8_t *pui8Data, uint32_t ui32NumBytes) { uint32_t ui32Loop, ui32Space, ui32Count; uint32_t ui32ReadIndex; uint32_t ui32WriteIndex; tUSBRingBufObject sTxRing; unsigned char Data_Merge_flag=0,Data_count=0,Addr_shift_Cnt=8,Rx_Arr_Index=0,EEPROM_Buff_Index=0,Addr_Flag=0,Error_Data_Index=0; uint32_t pui32eprom = 0x00, addrdata=0x00; uint32_t pui32Weprom[64]={0x00}; USBBufferInfoGet(&g_sTxBuffer, &sTxRing); ui32Space = USBBufferSpaceAvailable(&g_sTxBuffer); ui32Loop = (ui32Space < ui32NumBytes) ? ui32Space : ui32NumBytes; ui32Count = ui32Loop; g_ui32RxCount += ui32NumBytes; ui32ReadIndex = (uint32_t)(pui8Data - g_pui8USBRxBuffer); ui32WriteIndex = sTxRing.ui32WriteIndex; for(Rx_Arr_Index=0x00;Rx_Arr_Index 0xfff)) { goto error; } if((addrdata >= 0x000) && (addrdata <= 0x0f0)) Addr_Flag = 1; } } if(Rx_Arr_Index==0x04) { Data_count = (Ascii_To_Num(pui32Data[Rx_Arr_Index-1]) << 4) | Ascii_To_Num(pui32Data[Rx_Arr_Index]); if(Addr_Flag == 1) { Data_count = (Data_count * 0x02)+0x04; } else { Data_count = Data_count +0x04; } } if((Rx_Arr_Index>=0x05)&&(Rx_Arr_Index<=Data_count)) { if( Addr_Flag == 1) { if(Data_Merge_flag == 0) { pui32eprom = (Ascii_To_Num(pui32Data[Rx_Arr_Index]) << 4); Data_Merge_flag=1; } else { pui32eprom = pui32eprom | Ascii_To_Num(pui32Data[Rx_Arr_Index]); if(pui32eprom > 0x59) { error: for(Error_Data_Index=0;Error_Data_Index<5;Error_Data_Index++,ui32WriteIndex++) { g_pui8USBTxBuffer[ui32WriteIndex] = Error_data[Error_Data_Index]; } goto end; } pui32Data[Rx_Arr_Index] = pui32eprom; Data_Merge_flag=0; goto write; } } if((Rx_Arr_Index>Data_count) || (Addr_Flag == 0)) { write: pui32Weprom[EEPROM_Buff_Index++]=pui32Data[Rx_Arr_Index]; pui32eprom = 0x00; } if((Rx_Arr_Index>Data_count) && (Addr_Flag == 0)) break; } } EEPROMProgram(pui32Weprom, addrdata, sizeof(pui32Weprom)); end: USBBufferDataWritten(&g_sTxBuffer, ui32Count); return(ui32Count); } /*********************************************************************************************************** * Function Name : TxHandler * Description of the function : Handles bulk driver notifications related to the transmit channel (data to the USB host). * Passed parameter : void *pvCBData : is the client-supplied callback pointer for this channel. uint32_t ui32Event : identifies the event we are being notified about. uint32_t ui32MsgValue : is an event-specific value. void *pvMsgData : is an event-specific pointer. * Return value : uint32_t : The return value is event-specific. ***********************************************************************************************************/ uint32_t TxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData) { if(ui32Event == USB_EVENT_TX_COMPLETE) { g_ui32TxCount += ui32MsgValue; } return(0); } /*********************************************************************************************************** * Function Name : RxHandler * Description of the function : Handles bulk driver notifications related to the receive channel (data from the USB host). * Passed parameter : void *pvCBData : is the client-supplied callback pointer for this channel. uint32_t ui32Event : identifies the event we are being notified about. uint32_t ui32MsgValue : is an event-specific value. void *pvMsgData : is an event-specific pointer. * Return value : uint32_t : The return value is event-specific. ***********************************************************************************************************/ uint32_t RxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData) { switch(ui32Event) { case USB_EVENT_CONNECTED: { g_bUSBConfigured = true; USBBufferFlush(&g_sTxBuffer); USBBufferFlush(&g_sRxBuffer); break; } case USB_EVENT_DISCONNECTED: { g_bUSBConfigured = false; break; } case USB_EVENT_RX_AVAILABLE: { tUSBDBulkDevice *psDevice; psDevice = (tUSBDBulkDevice *)pvCBData; return(EchoNewDataToHost(psDevice, pvMsgData, ui32MsgValue)); } case USB_EVENT_SUSPEND: case USB_EVENT_RESUME: { break; } default: { break; } } return(0); } /*********************************************************************************************************** * Function Name : USB_Code * Description of the function : This is the main application entry function. * Passed parameter : None. * Return value : integer value. ***********************************************************************************************************/ int USB_Code(void) { volatile uint32_t ui32Loop; ROM_FPULazyStackingEnable(); ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); SysCtlPeripheralEnable(SYSCTL_PERIPH_EEPROM0); EEPROMInit(); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_3 | GPIO_PIN_2 | GPIO_PIN_1 ); g_bUSBConfigured = false; ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); ROM_GPIOPinTypeUSBAnalog(GPIO_PORTD_BASE, GPIO_PIN_4 | GPIO_PIN_5); ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / SYSTICKS_PER_SECOND); ROM_SysTickIntEnable(); ROM_SysTickEnable(); USBBufferInit(&g_sTxBuffer); USBBufferInit(&g_sRxBuffer); USBStackModeSet(0, eUSBModeForceDevice, 0); USBDBulkInit(0, &g_sBulkDevice); while(1) { } } /*********************************************************************************************************** * File Name : main.c * Description of the File : * Author : ***********************************************************************************************************/ #include "inc/tm4c123gh6pm.h" #include #include #include #include "inc/hw_memmap.h" #include "inc/hw_types.h" #include "driverlib/debug.h" #include "driverlib/fpu.h" #include "driverlib/gpio.h" #include "driverlib/interrupt.h" #include "driverlib/pin_map.h" #include "driverlib/sysctl.h" #include "driverlib/systick.h" #include "driverlib/timer.h" #include "driverlib/rom.h" #include "driverlib/usb.h" #include "usblib/usblib.h" #include "usblib/usb-ids.h" #include "usblib/device/usbdevice.h" #include "usblib/device/usbdbulk.h" //#include "utils/ustdlib.h" #include "usb_bulk_structs.h" #include "driverlib/eeprom.h" #include "usb_dev_bulk.h" #include "ADC_TM4C123.h" #include "ADC_TM4C123G.h" int main(void) { if(adc_value(0x04) > 0x935) { USB_Code(); } if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_2)) { GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0); } else { GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 4); } }