SHARE
TWEET

Untitled

a guest Sep 30th, 2018 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /****************************************************************************************//**
  2.  *  @file       _start.c
  3.  *
  4.  *  @author     Fotis Panagiotopoulos
  5.  *
  6.  *  Startup code. _start should be called by the Reset Handler, and never return.
  7.  *
  8.  *******************************************************************************************/
  9.  
  10. #include "LPC17xx.h"
  11. #include "hal.h"
  12. #include "hal_config.h"
  13. #include "interrupt.h"
  14. #include "hal_types.h"
  15. #include "hal_assert.h"
  16.  
  17. /** Begin address for the initialization values of the .data section. */
  18. extern unsigned int __textdata__;
  19. /** Begin address for the .data section. */
  20. extern unsigned int __data_start__;
  21. /** End address for the .data section. */
  22. extern unsigned int __data_end__;
  23. /** Start of the exception stack */
  24. extern const unsigned int __exception_stack_start__;
  25. /** End of the exception stack */
  26. extern const unsigned int __exception_stack_end__;
  27. /** Begin address for the .bss section. */
  28. extern unsigned int __bss_start__;
  29. /** End address for the .bss section */
  30. extern unsigned int __bss_end__;
  31.  
  32.  
  33. /** Main program entry point. */
  34. extern int main(void);
  35.  
  36. /** Exit system call. */
  37. extern void _exit(int code);
  38.  
  39. /** Initializes the data section. */
  40. static void __attribute__((always_inline)) __initialize_data (unsigned int* from, unsigned int* region_begin, unsigned int* region_end);
  41.  
  42. /** Initializes the BSS section. */
  43. static void __attribute__((always_inline)) __initialize_bss (unsigned int* region_begin, unsigned int* region_end);
  44.  
  45. /** Start-up code. */
  46. void __attribute__ ((section(".after_vectors"), noreturn, used)) _start(void);
  47.  
  48.  
  49. void _start (void)
  50. {
  51.     //Disable the interrupts, just to be safe. The system is not
  52.     //yet ready to call functions.
  53.     Interrupts_globalDisable();
  54.  
  55.     //If there is an exception/interrupt stack allocated,
  56.     //then the stack pointers in the CPU have to be set
  57.     //accordingly. Furthermore the CPU has to be turned to
  58.     //thread (unprivileged) mode.
  59.     if (&__exception_stack_end__ - &__exception_stack_start__ != 0)
  60.     {
  61.         asm volatile (
  62.                         "ldr     r0, = __exception_stack_end__  \n\t" /* Load the exception stack top to register r0 */
  63.                         "msr     MSP, r0                        \n\t" /* Move the exception stack top value to the MSP */
  64.                         "isb                                    \n\t" /* Flush the pipeline */
  65.  
  66.                         "ldr     r0, = __stack                  \n\t" /* Load the process stack top to register r0 */
  67.                         "msr     PSP, r0                        \n\t" /* Move the process stack top value to the PSP */
  68.                         "movs    r0, #2                         \n\t" /* Load the constant 2 to register r0 */
  69.                         "msr     CONTROL, r0                    \n\t" /* Move the constant to the CONTROL register,
  70.                                                                         setting the CPU in non-privileged mode. */
  71.                         "isb                                    \n\t" /* Flush the pipeline */
  72.  
  73.                         : : : "r0");
  74.     }
  75.  
  76.  
  77.     // Initialize hardware right after reset, to switch clock to higher
  78.     // frequency and have the rest of the initializations run faster.
  79.     SystemInit();
  80.  
  81.  
  82.     // Copy the DATA segment from Flash to RAM (inlined).
  83.     __initialize_data(&__textdata__, &__data_start__, &__data_end__);
  84.  
  85.     // Zero fill the BSS section (inlined).
  86.     __initialize_bss(&__bss_start__, &__bss_end__);
  87.  
  88.     //Core is running normally, RAM is initialized properly,
  89.     //now the system must be fully functional.
  90.  
  91.     //Set the default priority for all interrupts.
  92.     for (int i = 0; i < 35; i++)  //The LPC1769 has 35 priorities, see the CMSIS file.
  93.     {
  94.         NVIC_SetPriority(i, INTERRUPT_DEFAULT_PRIORITY);
  95.     }
  96.  
  97.     //Update the SystemCoreClock variable.
  98.     SystemCoreClockUpdate();
  99.  
  100.     //Check the PLLs are working normally, and that the system
  101.     //frequency is the expected.
  102.     HAL_ASSERT(SystemCoreClock == CPU_FREQ);
  103.  
  104.     //Now that everything is up and running, enable the interrupts
  105.     Interrupts_globalEnable();
  106.  
  107.     //NOTE!!! Until here the OS must NOT be running. Any OS related
  108.     //functions must only be called within main!
  109.  
  110.     // Call the main entry point, and save the exit code.
  111.     int code = main();
  112.  
  113.  
  114.     //Main should never return. If it does, let the system exit gracefully.
  115.     //Again, here it is assumed that the OS has stopped. The OS must be
  116.     //functional only within main.
  117.     _exit (code);
  118.  
  119.     // Should never reach this, _exit() should have already
  120.     // performed a reset.
  121.     while(1);
  122. }
  123.  
  124. static inline void __initialize_data (unsigned int* from, unsigned int* region_begin, unsigned int* region_end)
  125. {
  126.     // Iterate and copy word by word.
  127.     // It is assumed that the pointers are word aligned.
  128.     unsigned int *p = region_begin;
  129.     while (p < region_end)
  130.         *p++ = *from++;
  131. }
  132.  
  133. static inline void __initialize_bss (unsigned int* region_begin, unsigned int* region_end)
  134. {
  135.     // Iterate and clear word by word.
  136.     // It is assumed that the pointers are word aligned.
  137.     unsigned int *p = region_begin;
  138.     while (p < region_end)
  139.         *p++ = 0;
  140. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top