void CPU_InitializeCommunication() { NATIVE_PROFILE_PAL_COM(); // STDIO can be different from the Debug Text port // do these first so we can print out messages if(COM_IsSerial(HalSystemConfig.DebugTextPort)) { USART_Initialize( ConvertCOM_ComPort(HalSystemConfig.DebugTextPort), HalSystemConfig.USART_DefaultBaudRate, USART_PARITY_NONE, 8, USART_STOP_BITS_ONE, USART_FLOW_NONE ); } if(COM_IsSerial(HalSystemConfig.stdio)) { USART_Initialize( ConvertCOM_ComPort(HalSystemConfig.stdio), HalSystemConfig.USART_DefaultBaudRate, USART_PARITY_NONE, 8, USART_STOP_BITS_ONE, USART_FLOW_NONE ); } if(COM_IsUsb(HalSystemConfig.DebugTextPort)) { USB_Configure( ConvertCOM_UsbController(HalSystemConfig.DebugTextPort), NULL ); USB_Initialize( ConvertCOM_UsbController(HalSystemConfig.DebugTextPort) ); USB_OpenStream( ConvertCOM_UsbStream(HalSystemConfig.DebugTextPort), USB_DEBUG_EP_WRITE, USB_DEBUG_EP_READ ); } Network_Initialize(); }
/************************************************************************* * Function Name: UsbSetDevState * Parameters: UsbT9DevState_t DevState * * Return: none * * Description: Set device state * *************************************************************************/ void UsbSetDevState (UsbT9DevState_t DevState) { pUsbStandardInterfaceDescriptor_t pInterface; if(UsbDevCtrl.State.DS == UsbDevStatusConfigured) { // deconfigure device for (Int32U i = 0; i < UsbDevCtrl.Interfaces; ++i) { pInterface = UsbFindInterface(UsbDevCtrl.pCnfg,i,UsbDevCtrl.AlternateSetting[i]); UsbEpInterfaceCnfg(pInterface,FALSE); } } switch (DevState) { case UsbDevStatusAttached: case UsbDevStatusPowered: case UsbDevStatusDefault: USB_Configure(FALSE); USB_SetDefAdd(); // Set address 0 USB_CONFIGURE_HOOK(NULL); UsbDevCtrl.DevAdd = UsbDevCtrl.Configuration = 0; break; case UsbDevStatusAddress: USB_Configure(FALSE); UsbDevCtrl.Configuration = 0; break; case UsbDevStatusConfigured: // configure device for (Int32U i = 0; i < UsbDevCtrl.Interfaces; ++i) { pInterface = UsbFindInterface(UsbDevCtrl.pCnfg,i,UsbDevCtrl.AlternateSetting[i]); UsbEpInterfaceCnfg(pInterface,TRUE); } USB_Configure(TRUE); break; default: // unknown state - disconnected or in process of a connection USB_Configure(FALSE); USB_CONFIGURE_HOOK(NULL); UsbDevCtrl.DevAdd = UsbDevCtrl.Configuration = 0; } UsbDevCtrl.State.DS = DevState; }
BOOL DebuggerPort_Initialize( COM_HANDLE ComPortNum ) { NATIVE_PROFILE_PAL_COM(); switch(ExtractTransport(ComPortNum)) { case USART_TRANSPORT: return USART_Initialize( ConvertCOM_ComPort(ComPortNum), HalSystemConfig.USART_DefaultBaudRate, USART_PARITY_NONE, 8, USART_STOP_BITS_ONE, USART_FLOW_NONE ); case USB_TRANSPORT: if(USB_CONFIG_ERR_OK != USB_Configure( ConvertCOM_UsbController(ComPortNum), NULL )) return FALSE; if(!USB_Initialize( ConvertCOM_UsbController(ComPortNum) )) return FALSE; return USB_OpenStream( ConvertCOM_UsbStream(ComPortNum), USB_DEBUG_EP_WRITE, USB_DEBUG_EP_READ ); case SOCKET_TRANSPORT: return SOCKETS_Initialize(ConvertCOM_SockPort(ComPortNum)); } return FALSE; }
/************************************************************************* * Function Name: USB_HwReset * Parameters: none * * Return: none * * Description: Reset USB engine * *************************************************************************/ void USB_HwReset (void) { Int32U Count; // Clear realized EP flag for(Count = 0; Count < ENP_MAX_NUMB; Count++) { EpCnfg[Count].pEpCtrl = NULL; } // Free entire USB packet memory for (Count = 0; Count < EP_MAX_SLOTS*2; Count++) { PacketMemBuff[Count].Size = 0; } // Resume delay counter DlyCnt = 0; // USB_Configure USB_Configure(FALSE); // Set Buffer table address USB_BTABLE = USB_OffsetOfDBT; // Init Packet memory table pPacketMemUse = NULL; // Control EP Init USB_RealizeEp(&USB_CtrlEpDescr0,&USB_CtrlEpExt0,TRUE); USB_RealizeEp(&USB_CtrlEpDescr1,&USB_CtrlEpExt1,TRUE); // Set address 0 USB_SetDefAdd(); // Enable Device interrupts USB_CNTR = bmCTRM | bmRESETM | bmSUSPM | bmWKUPM | (USB_SOF_EVENT ? bmSOFM : 0) | (USB_ERROR_EVENT ? bmERRM : 0) | (USB_PMAOVR_EVENT ? bmPMAOVRM : 0) ; }
__inline uint32_t USB_ReqSetConfiguration (void) { USB_COMMON_DESCRIPTOR *pD; uint32_t alt = 0; uint32_t n, m; switch (SetupPacket.bmRequestType.BM.Recipient) { case REQUEST_TO_DEVICE: if (SetupPacket.wValue.WB.L) { pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor; while (pD->bLength) { switch (pD->bDescriptorType) { case USB_CONFIGURATION_DESCRIPTOR_TYPE: if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue == SetupPacket.wValue.WB.L) { USB_Configuration = SetupPacket.wValue.WB.L; USB_NumInterfaces = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bNumInterfaces; for (n = 0; n < USB_IF_NUM; n++) { USB_AltSetting[n] = 0; } for (n = 1; n < USB_LOGIC_EP_NUM; n++) { if (USB_EndPointMask & (1 << n)) { USB_DisableEP(n); } if (USB_EndPointMask & ((1 << 16) << n)) { USB_DisableEP(n | 0x80); } } USB_EndPointMask = 0x00010001; USB_EndPointHalt = 0x00000000; USB_EndPointStall= 0x00000000; USB_Configure(TRUE); if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_POWERED_MASK) { USB_DeviceStatus |= USB_GETSTATUS_SELF_POWERED; } else { USB_DeviceStatus &= ~USB_GETSTATUS_SELF_POWERED; } } else { (uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; continue; } break; case USB_INTERFACE_DESCRIPTOR_TYPE: alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting; break; case USB_ENDPOINT_DESCRIPTOR_TYPE: if (alt == 0) { n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F; m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n); USB_EndPointMask |= m; USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD); USB_EnableEP(n); USB_ResetEP(n); } break; } (uint8_t *)pD += pD->bLength; } } else { USB_Configuration = 0; for (n = 1; n < USB_LOGIC_EP_NUM; n++) { if (USB_EndPointMask & (1 << n)) { USB_DisableEP(n); } if (USB_EndPointMask & ((1 << 16) << n)) { USB_DisableEP(n | 0x80); } } USB_EndPointMask = 0x00010001; USB_EndPointHalt = 0x00000000; USB_EndPointStall = 0x00000000; USB_Configure(FALSE); } if (USB_Configuration != SetupPacket.wValue.WB.L) { return (FALSE); } break; default: return (FALSE); } return (TRUE); }
inline uint32_t USB_ReqGetConfiguration (void) { #else __inline uint32_t USB_ReqGetConfiguration (void) { #endif switch (SetupPacket.bmRequestType.BM.Recipient) { case REQUEST_TO_DEVICE: EP0Data.pData = &USB_Configuration; break; default: return (FALSE); } return (TRUE); } /* * Set Configuration USB Request * Parameters: None (global SetupPacket) * Return Value: TRUE - Success, FALSE - Error */ #ifdef __IAR_SYSTEMS_ICC__ inline uint32_t USB_ReqSetConfiguration (void) { #else __inline uint32_t USB_ReqSetConfiguration (void) { #endif USB_COMMON_DESCRIPTOR *pD; uint32_t alt = 0; uint32_t n, m; uint32_t tmp; switch (SetupPacket.bmRequestType.BM.Recipient) { case REQUEST_TO_DEVICE: if (SetupPacket.wValue.WB.L) { pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor; while (pD->bLength) { switch (pD->bDescriptorType) { case USB_CONFIGURATION_DESCRIPTOR_TYPE: if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue == SetupPacket.wValue.WB.L) { USB_Configuration = SetupPacket.wValue.WB.L; USB_NumInterfaces = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bNumInterfaces; for (n = 0; n < USB_IF_NUM; n++) { USB_AltSetting[n] = 0; } for (n = 1; n < 16; n++) { if (USB_EndPointMask & (1 << n)) { USB_DisableEP(n); } if (USB_EndPointMask & ((1 << 16) << n)) { USB_DisableEP(n | 0x80); } } USB_EndPointMask = 0x00010001; USB_EndPointHalt = 0x00000000; USB_EndPointStall= 0x00000000; USB_Configure(TRUE); if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_POWERED_MASK) { USB_DeviceStatus |= USB_GETSTATUS_SELF_POWERED; } else { USB_DeviceStatus &= ~USB_GETSTATUS_SELF_POWERED; } } else { //(uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; tmp = (uint32_t) pD; tmp += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; pD = (USB_COMMON_DESCRIPTOR *)tmp; continue; } break; case USB_INTERFACE_DESCRIPTOR_TYPE: alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting; break; case USB_ENDPOINT_DESCRIPTOR_TYPE: if (alt == 0) { n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F; m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n); USB_EndPointMask |= m; USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD); USB_EnableEP(n); USB_ResetEP(n); } break; } //(uint8_t *)pD += pD->bLength; tmp = (uint32_t) pD; tmp += pD->bLength; pD = (USB_COMMON_DESCRIPTOR *)tmp; } } else { USB_Configuration = 0; for (n = 1; n < 16; n++) { if (USB_EndPointMask & (1 << n)) { USB_DisableEP(n); } if (USB_EndPointMask & ((1 << 16) << n)) { USB_DisableEP(n | 0x80); } } USB_EndPointMask = 0x00010001; USB_EndPointHalt = 0x00000000; USB_EndPointStall = 0x00000000; USB_Configure(FALSE); } if (USB_Configuration != SetupPacket.wValue.WB.L) { return (FALSE); } break; default: return (FALSE); } return (TRUE); }
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] ); } } } }
inline uint32_t USB_SetConfiguration (void) { #else __inline uint32_t USB_SetConfiguration (void) { #endif USB_COMMON_DESCRIPTOR *pD; uint32_t alt, n; uint32_t tmp; if (SetupPacket.wValue.WB.L) { pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor; while (pD->bLength) { switch (pD->bDescriptorType) { case USB_CONFIGURATION_DESCRIPTOR_TYPE: if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue == SetupPacket.wValue.WB.L) { USB_Configuration = SetupPacket.wValue.WB.L; USB_Configure(TRUE); /* if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_SELF_POWERED) { USB_DeviceStatus |= USB_GETSTATUS_SELF_POWERED; } else { USB_DeviceStatus &= ~USB_GETSTATUS_SELF_POWERED; } */ } else { // (uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; tmp = (uint32_t)pD; tmp += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; pD = (USB_COMMON_DESCRIPTOR *)tmp; continue; } break; case USB_INTERFACE_DESCRIPTOR_TYPE: alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting; break; case USB_ENDPOINT_DESCRIPTOR_TYPE: if (alt == 0) { n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F; USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD); USB_EnableEP(n); USB_ResetEP(n); } break; } // (uint8_t *)pD += pD->bLength; tmp = (uint32_t)pD; tmp += pD->bLength; pD = (USB_COMMON_DESCRIPTOR *)tmp; } } else { USB_Configuration = 0; USB_Configure(FALSE); } if (USB_Configuration == SetupPacket.wValue.WB.L) { return (TRUE); } else { return (FALSE); } }