void ApplicationEntryPoint() { UINT32 ComEvent; g_State.Initialize(); ComEvent = 0; if(COM_IsSerial(g_State.UsartPort) && (g_State.UsartPort != COM_NULL)) { ComEvent = SYSTEM_EVENT_FLAG_COM_IN; } #if !defined(TARGETLOCATION_RAM) g_State.WaitForActivity = (g_State.ProgramCount != 0); // is there a main app? #else g_State.WaitForActivity = FALSE; // forever #endif { UINT32 ButtonsPressed; UINT32 ButtonsReleased; char c; // clear any events present from startup while(Events_Get( SYSTEM_EVENT_FLAG_ALL )); // clear any junk from com port buffers while(DebuggerPort_Read( g_State.UsartPort, &c, sizeof(c) )); // clear any junk from button buffer while(Buttons_GetNextStateChange( ButtonsPressed, ButtonsReleased )); } { BOOL ProcessingSREC = FALSE; BOOL ProcessingXREC = FALSE; INT32 ProcessingZENFLASH = 0; INT32 Mode = 0; INT32 MenuChoice = 1; // main application location when USB not enabled BOOL MenuUpdate = TRUE; UINT32 USBEvent = 0; COM_HANDLE ReadPort = COM_NULL; int MenuOffset = 1; // no USB enabled INT64 WaitTimeout = 0; #if defined(PLATFORM_ARM_MOTE2) CPU_GPIO_SetPinState( LED1_GREEN, LED_ON ); #endif if(g_State.WaitForActivity) { WaitTimeout = HAL_Time_CurrentTime() + (INT64)g_State.WaitInterval * (10 * 1000); hal_printf( "Waiting %d.%03d second(s) for hex upload\r\n", g_State.WaitInterval / 1000, g_State.WaitInterval % 1000 ); } else { hal_printf( "Waiting forever for hex upload\r\n" ); } // checking the existence of Usb Driver, all the default values are set to no USB if( USB_DEVICE_STATE_NO_CONTROLLER != USB_GetStatus( ConvertCOM_UsbController(g_State.UsbPort) ) ) { g_State.UsingUsb = TRUE; MenuChoice = 2; MenuOffset = 2; } while(true) { if(MenuUpdate) { MenuUpdate = FALSE; LCD_Clear(); hal_fprintf( STREAM_LCD, "\f"); switch(Mode) { case 0: hal_fprintf( STREAM_LCD, " Zen Boot\r\n\r\n" ); hal_fprintf( STREAM_LCD, "%c PortBooter\r\n", MenuChoice == 0 ? '*' : ' ' ); if(g_State.UsingUsb) { hal_fprintf( STREAM_LCD, "%c FlashUSB\r\n", MenuChoice == 1 ? '*' : ' ' ); } for(int i = 0; i < g_State.ProgramCount; i++) { hal_fprintf( STREAM_LCD, "%c Prg:%08x\r\n", (i+MenuOffset) == MenuChoice ? '*' : ' ', g_State.Programs[i] ); } break; case 1: hal_printf( "PortBooter v%d.%d.%d.%d\r\n", VERSION_MAJOR, VERSION_MINOR, VERSION_BUILD, VERSION_REVISION); hal_fprintf( STREAM_LCD, "Waiting forever for hex upload\r\n" ); break; case 2: hal_fprintf( STREAM_LCD, "FlashUSB\r\n" ); hal_fprintf( STREAM_LCD, "Waiting forever for hex upload\r\n" ); break; } } UINT32 Events = Events_WaitForEvents( ComEvent | SYSTEM_EVENT_FLAG_BUTTON | SYSTEM_EVENT_FLAG_USB_IN, 2000 ); if(Events & SYSTEM_EVENT_FLAG_BUTTON) { UINT32 ButtonsPressed; UINT32 ButtonsReleased; Events_Clear( SYSTEM_EVENT_FLAG_BUTTON ); while(Buttons_GetNextStateChange( ButtonsPressed, ButtonsReleased )); { if(g_State.SerialPortActive == FALSE) { //printf("%02x %02x\r\n", ButtonsPressed, ButtonsReleased); // up if(ButtonsPressed & BUTTON_UP) { switch(Mode) { case 0: MenuChoice = __max( MenuChoice-1, 0 ); break; } g_State.WaitForActivity = FALSE; MenuUpdate = TRUE; } // down if(ButtonsPressed & BUTTON_DOWN) { switch(Mode) { case 0: MenuChoice = __min( MenuChoice+1, g_State.ProgramCount + MenuOffset-1 ); break; } g_State.WaitForActivity = FALSE; MenuUpdate = TRUE; } // enter button if(ButtonsPressed & BUTTON_ENTR) { switch(Mode) { case 0: if(MenuChoice == 0) { Mode = 1; //UsingUsb = FALSE; } else if(g_State.UsingUsb && MenuChoice == 1) { Mode = 2; USB_Configure( ConvertCOM_UsbController(g_State.UsbPort), &UsbDefaultConfiguration ); USB_Initialize( ConvertCOM_UsbController(g_State.UsbPort) ); USB_OpenStream( ConvertCOM_UsbStream(g_State.UsbPort), USB_DEBUG_EP_WRITE, USB_DEBUG_EP_READ ); //UsingUsb = TRUE; } else { StartApplication( (void (*)())g_State.Programs[MenuChoice-MenuOffset] ); } break; case 1: Mode = 0; break; case 2: // USB_Uninitialize(); Mode = 0; break; } g_State.WaitForActivity = FALSE; MenuUpdate = TRUE; } if(ButtonsReleased) { MenuUpdate = TRUE; } } } } if((Events & ComEvent) || (Events & SYSTEM_EVENT_FLAG_USB_IN)) { char c; if(Events & ComEvent) { Events_Clear( ComEvent ); ReadPort = g_State.UsartPort; g_State.pStreamOutput = ReadPort; } else { USBEvent = USB_GetEvent( ConvertCOM_UsbController(g_State.UsbPort), USB_EVENT_ALL ); if( !(USBEvent & g_State.UsbEventCode) ) continue; g_State.pStreamOutput = g_State.UsbPort; ReadPort = g_State.UsbPort; } while(DebuggerPort_Read( ReadPort, &c, sizeof(c) )) { if(ProcessingSREC) { ProcessingSREC = g_SREC.Process( c ); } else if(ProcessingXREC) { ProcessingXREC = g_XREC.Process( c ); } else if(ProcessingZENFLASH) { const char Signature[] = "ZENFLASH\r"; //printf( "Got %d at %d\r\n", c, ProcessingZENFLASH ); if(Signature[ProcessingZENFLASH++] == c) { if(Signature[ProcessingZENFLASH] == 0) { SignalActivity(); ProcessingZENFLASH = 0; } } else { ProcessingZENFLASH = 0; } } else if('S' == c) { ProcessingSREC = TRUE; } else if('X' == c) { ProcessingXREC = TRUE; } else if('Z' == c) { ProcessingZENFLASH = 1; } } } if(g_State.WaitForActivity && WaitTimeout < HAL_Time_CurrentTime()) { #if defined(PLATFORM_ARM_MOTE2) CPU_GPIO_SetPinState(LED1_GREEN, LED_OFF); // Turn off Green LED for iMOTE2 #endif // we didn't see anything on serial port for wait interval (2 seconds nominally), // continue with code - just run the normal application StartApplication( (void (*)())g_State.Programs[0] ); } } } }
void USB_EndPoint0 (uint32_t event) { switch (event) { case USB_EVT_SETUP: USB_SetupStage(); USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir); EP0Data.Count = SetupPacket.wLength; switch (SetupPacket.bmRequestType.BM.Type) { case REQUEST_STANDARD: switch (SetupPacket.bRequest) { case USB_REQUEST_GET_STATUS: if (!USB_GetStatus()) { goto stall_i; } break; case USB_REQUEST_CLEAR_FEATURE: if (!USB_SetClrFeature(0)) { goto stall_i; } USB_StatusInStage(); #if USB_FEATURE_EVENT USB_Feature_Event(); #endif break; case USB_REQUEST_SET_FEATURE: if (!USB_SetClrFeature(1)) { goto stall_i; } USB_StatusInStage(); #if USB_FEATURE_EVENT USB_Feature_Event(); #endif break; case USB_REQUEST_SET_ADDRESS: switch (SetupPacket.bmRequestType.BM.Recipient) { case REQUEST_TO_DEVICE: USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L; USB_StatusInStage(); break; default: goto stall_i; } break; case USB_REQUEST_GET_DESCRIPTOR: if (!USB_GetDescriptor()) { goto stall_i; } break; case USB_REQUEST_SET_DESCRIPTOR: /*stall_o:*/ USB_SetStallEP(0x00); EP0Data.Count = 0; break; case USB_REQUEST_GET_CONFIGURATION: switch (SetupPacket.bmRequestType.BM.Recipient) { case REQUEST_TO_DEVICE: EP0Data.pData = &USB_Configuration; USB_DataInStage(); break; default: goto stall_i; } break; case USB_REQUEST_SET_CONFIGURATION: switch (SetupPacket.bmRequestType.BM.Recipient) { case REQUEST_TO_DEVICE: if (!USB_SetConfiguration()) { goto stall_i; } USB_StatusInStage(); #if USB_CONFIGURE_EVENT USB_Configure_Event(); #endif break; default: goto stall_i; } break; case USB_REQUEST_GET_INTERFACE: switch (SetupPacket.bmRequestType.BM.Recipient) { case REQUEST_TO_INTERFACE: if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) { EP0Data.pData = USB_AltSetting + SetupPacket.wIndex.WB.L; USB_DataInStage(); } else { goto stall_i; } break; default: goto stall_i; } break; case USB_REQUEST_SET_INTERFACE: switch (SetupPacket.bmRequestType.BM.Recipient) { case REQUEST_TO_INTERFACE: if (!USB_SetInterface()) { goto stall_i; } USB_StatusInStage(); #if USB_INTERFACE_EVENT USB_Interface_Event(); #endif break; default: goto stall_i; } break; default: goto stall_i; } break; case REQUEST_CLASS: #if USB_CLASS switch (SetupPacket.bmRequestType.BM.Recipient) { case REQUEST_TO_INTERFACE: #if USB_HID if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) { switch (SetupPacket.bRequest) { case HID_REQUEST_GET_REPORT: if (HID_GetReport()) { EP0Data.pData = EP0Buf; USB_DataInStage(); goto class_ok; } break; case HID_REQUEST_SET_REPORT: EP0Data.pData = EP0Buf; goto class_ok; case HID_REQUEST_GET_IDLE: if (HID_GetIdle()) { EP0Data.pData = EP0Buf; USB_DataInStage(); goto class_ok; } break; case HID_REQUEST_SET_IDLE: if (HID_SetIdle()) { USB_StatusInStage(); goto class_ok; } break; case HID_REQUEST_GET_PROTOCOL: if (HID_GetProtocol()) { EP0Data.pData = EP0Buf; USB_DataInStage(); goto class_ok; } break; case HID_REQUEST_SET_PROTOCOL: if (HID_SetProtocol()) { USB_StatusInStage(); goto class_ok; } break; } } #endif /* USB_HID */ #if USB_MSC if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) { switch (SetupPacket.bRequest) { case MSC_REQUEST_RESET: if (MSC_Reset()) { USB_StatusInStage(); goto class_ok; } break; case MSC_REQUEST_GET_MAX_LUN: if (MSC_GetMaxLUN()) { EP0Data.pData = EP0Buf; USB_DataInStage(); goto class_ok; } break; } } #endif /* USB_MSC */ #if USB_AUDIO if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM) || (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) || (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) { if (SetupPacket.bmRequestType.BM.Dir) { if (ADC_IF_GetRequest()) { EP0Data.pData = EP0Buf; USB_DataInStage(); goto class_ok; } } else { EP0Data.pData = EP0Buf; goto class_ok; } } #endif /* USB_AUDIO */ goto stall_i; #if USB_AUDIO case REQUEST_TO_ENDPOINT: if (SetupPacket.bmRequestType.BM.Dir) { if (ADC_EP_GetRequest()) { EP0Data.pData = EP0Buf; USB_DataInStage(); goto class_ok; } } else { EP0Data.pData = EP0Buf; goto class_ok; } goto stall_i; #endif /* USB_AUDIO */ default: goto stall_i; } class_ok: break; #else goto stall_i; #endif /* USB_CLASS */ case REQUEST_VENDOR: goto stall_i; default: stall_i: USB_SetStallEP(0x80); EP0Data.Count = 0; break; } break; case USB_EVT_OUT: if (SetupPacket.bmRequestType.BM.Dir == 0) { if (EP0Data.Count) { USB_DataOutStage(); if (EP0Data.Count == 0) { switch (SetupPacket.bmRequestType.BM.Type) { case REQUEST_STANDARD: goto stall_i; #if (USB_CLASS) case REQUEST_CLASS: switch (SetupPacket.bmRequestType.BM.Recipient) { case REQUEST_TO_INTERFACE: #if USB_HID if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) { if (!HID_SetReport()) { goto stall_i; } break; } #endif #if USB_AUDIO if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM) || (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) || (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) { if (!ADC_IF_SetRequest()) { goto stall_i; } break; } #endif goto stall_i; case REQUEST_TO_ENDPOINT: #if USB_AUDIO if (ADC_EP_SetRequest()) break; #endif goto stall_i; default: goto stall_i; } break; #endif default: goto stall_i; } USB_StatusInStage(); } } } else { USB_StatusOutStage(); } break; case USB_EVT_IN: if (SetupPacket.bmRequestType.BM.Dir == 1) { USB_DataInStage(); } else { if (USB_DeviceAddress & 0x80) { USB_DeviceAddress &= 0x7F; USB_SetAddress(USB_DeviceAddress); } } break; case USB_EVT_IN_STALL: USB_ClrStallEP(0x80); break; case USB_EVT_OUT_STALL: USB_ClrStallEP(0x00); break; } }