/MicroFrameworkPK_v4_2/Application/TinyBooter/TinyBooter.cpp

https://bitbucket.org/pmfsampaio/netmf-lpc · C++ · 139 lines · 90 code · 33 blank · 16 comment · 14 complexity · 9ad2c7a7ab848f53933789a2e44fa9e6 MD5 · raw file

  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4. #include "TinyBooter.h"
  5. #include "TinyCLR_Version.h"
  6. #include <tinybooterentry.h>
  7. #include "ConfigurationManager.h"
  8. extern bool WaitForTinyBooterUpload( INT32 &timeout_ms );
  9. ////////////////////////////////////////////////////////////////////////////////
  10. //--//
  11. Loader_Engine g_eng;
  12. //--//
  13. HAL_DECLARE_CUSTOM_HEAP( SimpleHeap_Allocate, SimpleHeap_Release, SimpleHeap_ReAllocate );
  14. //--//
  15. void ApplicationEntryPoint()
  16. {
  17. INT32 timeout = 20000; // 20 second timeout
  18. bool enterBootMode = false;
  19. // crypto API needs to allocate memory. Initialize simple heap for it.
  20. UINT8* BaseAddress;
  21. UINT32 SizeInBytes;
  22. HeapLocation ( BaseAddress, SizeInBytes );
  23. SimpleHeap_Initialize( BaseAddress, SizeInBytes );
  24. g_eng.Initialize( HalSystemConfig.DebuggerPorts[ 0 ] );
  25. // internal reset and stop check
  26. enterBootMode = g_PrimaryConfigManager.IsBootLoaderRequired( timeout );
  27. // ODM defined method to enter bootloader mode
  28. if(!enterBootMode)
  29. {
  30. enterBootMode = WaitForTinyBooterUpload( timeout );
  31. }
  32. if(!enterBootMode)
  33. {
  34. if(!g_eng.EnumerateAndLaunch())
  35. {
  36. timeout = -1;
  37. enterBootMode = true;
  38. }
  39. }
  40. if(enterBootMode)
  41. {
  42. LCD_Clear();
  43. hal_fprintf( STREAM_LCD, "TinyBooter v%d.%d.%d.%d\r\n", VERSION_MAJOR, VERSION_MINOR, VERSION_BUILD, VERSION_REVISION);
  44. hal_fprintf( STREAM_LCD, "%s Build Date:\r\n\t%s %s\r\n", HalName, __DATE__, __TIME__ );
  45. DebuggerPort_Initialize( HalSystemConfig.DebuggerPorts[ 0 ] );
  46. TinyBooter_OnStateChange( State_EnterBooterMode, NULL );
  47. DebuggerPort_Flush( HalSystemConfig.DebugTextPort );
  48. hal_printf( "TinyBooter v%d.%d.%d.%d\r\n", VERSION_MAJOR, VERSION_MINOR, VERSION_BUILD, VERSION_REVISION);
  49. hal_printf( "%s Build Date: %s %s\r\n", HalName, __DATE__, __TIME__ );
  50. #if defined(__GNUC__)
  51. hal_printf("GNU Compiler version %d\r\n", __GNUC__);
  52. #elif defined(_ARC)
  53. hal_printf("ARC Compiler version %d\r\n", _ARCVER);
  54. #elif defined(__ADSPBLACKFIN__)
  55. hal_printf( "Blackfin Compiler version %d\r\n", __VERSIONNUM__ );
  56. #elif defined(__RENESAS__)
  57. hal_printf( "Renesas Compiler version %d\r\n", __RENESAS_VERSION__ );
  58. #else
  59. hal_printf( "ARM Compiler version %d\r\n", __ARMCC_VERSION );
  60. #endif
  61. DebuggerPort_Flush( HalSystemConfig.DebugTextPort );
  62. //
  63. // Send "presence" ping.
  64. //
  65. {
  66. CLR_DBG_Commands::Monitor_Ping cmd;
  67. cmd.m_source = CLR_DBG_Commands::Monitor_Ping::c_Ping_Source_TinyBooter;
  68. g_eng.m_controller.SendProtocolMessage( CLR_DBG_Commands::c_Monitor_Ping, WP_Flags::c_NonCritical, sizeof(cmd), (UINT8*)&cmd );
  69. }
  70. UINT64 ticksStart = HAL_Time_CurrentTicks();
  71. //
  72. // Wait for somebody to press a button; if no button press comes in, lauch the image
  73. //
  74. do
  75. {
  76. const UINT32 c_EventsMask = SYSTEM_EVENT_FLAG_COM_IN |
  77. SYSTEM_EVENT_FLAG_USB_IN |
  78. SYSTEM_EVENT_FLAG_BUTTON;
  79. UINT32 events = ::Events_WaitForEvents( c_EventsMask, timeout );
  80. if(events != 0)
  81. {
  82. Events_Clear( events );
  83. }
  84. if(events & SYSTEM_EVENT_FLAG_BUTTON)
  85. {
  86. TinyBooter_OnStateChange( State_ButtonPress, (void*)&timeout );
  87. }
  88. if(events & (SYSTEM_EVENT_FLAG_COM_IN | SYSTEM_EVENT_FLAG_USB_IN))
  89. {
  90. g_eng.ProcessCommands();
  91. }
  92. if(LOADER_ENGINE_ISFLAGSET(&g_eng, Loader_Engine::c_LoaderEngineFlag_ValidConnection))
  93. {
  94. LOADER_ENGINE_CLEARFLAG(&g_eng, Loader_Engine::c_LoaderEngineFlag_ValidConnection);
  95. TinyBooter_OnStateChange( State_ValidCommunication, (void*)&timeout );
  96. ticksStart = HAL_Time_CurrentTicks();
  97. }
  98. else if((timeout != -1) && (HAL_Time_CurrentTicks()-ticksStart) > CPU_MillisecondsToTicks((UINT32)timeout))
  99. {
  100. TinyBooter_OnStateChange( State_Timeout, NULL );
  101. g_eng.EnumerateAndLaunch();
  102. }
  103. } while(true);
  104. }
  105. ::CPU_Reset();
  106. }