|
| 1 | +--- |
| 2 | +title: Journey Through STM32F103VE Startup - From Power-on to Main() |
| 3 | +date: 2021-11-20 14:30:00 +0800 |
| 4 | +categories: [Embedded, STM32] |
| 5 | +tags: [stm32, startup, arm, embedded] # TAG names should always be lowercase |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | +# Journey Through STM32F103VE Startup - From Power-on to Main() |
| 10 | + |
| 11 | +> Have you ever wondered what happens when you press the reset button on your STM32 board? Let's embark on a journey through the startup process of the STM32F103VE, breaking down this complex process into an easy-to-understand story. |
| 12 | +
|
| 13 | +## The Wake-up Call |
| 14 | + |
| 15 | +Imagine your STM32F103VE as a person waking up in the morning. Just like how we need to follow a specific routine to start our day, the MCU follows its own "morning routine" defined in the startup file (`startup_stm32f10x_hd.s`). |
| 16 | + |
| 17 | +### First Things First: Getting Oriented |
| 18 | + |
| 19 | +```assembly |
| 20 | +Stack_Size EQU 0x00000400 ; Like choosing how much space you need |
| 21 | + ; 1KB of stack - your working space |
| 22 | +
|
| 23 | + AREA STACK, NOINIT, READWRITE, ALIGN=3 |
| 24 | +Stack_Mem SPACE Stack_Size |
| 25 | +__initial_sp ; Your desk is now ready! |
| 26 | +
|
| 27 | + AREA HEAP, NOINIT, READWRITE, ALIGN=3 |
| 28 | +__heap_base |
| 29 | +Heap_Mem SPACE Heap_Size ; Storage space for later use |
| 30 | +__heap_limit |
| 31 | +``` |
| 32 | + |
| 33 | +Just as you need to know where your things are when you wake up, the MCU needs to set up its workspace (stack) and storage area (heap). |
| 34 | + |
| 35 | +## The Morning Checklist (Vector Table) |
| 36 | + |
| 37 | +```assembly |
| 38 | + AREA RESET, DATA, READONLY |
| 39 | + EXPORT __Vectors |
| 40 | + |
| 41 | +__Vectors DCD __initial_sp ; Where to find things |
| 42 | + DCD Reset_Handler ; The wake-up routine |
| 43 | + DCD NMI_Handler ; Emergency contact |
| 44 | + DCD HardFault_Handler ; When things go wrong |
| 45 | +``` |
| 46 | + |
| 47 | +Think of this as your morning emergency contacts list - who to call when different types of problems occur. |
| 48 | + |
| 49 | +## The Wake-up Routine (Reset_Handler) |
| 50 | + |
| 51 | +```assembly |
| 52 | +Reset_Handler PROC |
| 53 | + EXPORT Reset_Handler [WEAK] |
| 54 | + IMPORT __main |
| 55 | + IMPORT SystemInit |
| 56 | + |
| 57 | + ; First cup of coffee - essential initialization |
| 58 | + LDR R0, =SystemInit |
| 59 | + BLX R0 |
| 60 | + |
| 61 | + ; Time to start the day! |
| 62 | + LDR R0, =__main |
| 63 | + BX R0 |
| 64 | + ENDP |
| 65 | +``` |
| 66 | + |
| 67 | +This is like your morning routine - get up (SystemInit), get ready, and start your day (main function). |
| 68 | + |
| 69 | +## Real-World Customizations |
| 70 | + |
| 71 | +### Quick Start Mode |
| 72 | +For when you need to get going fast: |
| 73 | + |
| 74 | +```assembly |
| 75 | +; Like grabbing a quick breakfast |
| 76 | +Reset_Handler PROC |
| 77 | + CPSID I ; Do Not Disturb mode |
| 78 | + LDR R0, =QuickInit ; Essential tasks only |
| 79 | + BLX R0 |
| 80 | + LDR R0, =main |
| 81 | + BX R0 ; Start the day |
| 82 | + ENDP |
| 83 | +``` |
| 84 | + |
| 85 | +### Safety Check Mode |
| 86 | +When you need extra security: |
| 87 | + |
| 88 | +```assembly |
| 89 | +; Like double-checking everything before leaving |
| 90 | + LDR R0, =SafetyCheck |
| 91 | + BLX R0 |
| 92 | + CMP R0, #0 ; All good? |
| 93 | + BNE Error_Handler ; Something's wrong! |
| 94 | +``` |
| 95 | + |
| 96 | +## Debugging Tips: When Things Go Wrong |
| 97 | + |
| 98 | +### 1. LED Breadcrumbs |
| 99 | +```c |
| 100 | +void DebugPoint1(void) |
| 101 | +{ |
| 102 | + // Like leaving a trail of breadcrumbs |
| 103 | + LED_ON(); |
| 104 | + Delay_ms(100); |
| 105 | + LED_OFF(); |
| 106 | +} |
| 107 | +``` |
| 108 | +
|
| 109 | +### 2. Simple Status Messages |
| 110 | +```c |
| 111 | +void Debug_Print(const char* msg) |
| 112 | +{ |
| 113 | + // Quick note to yourself |
| 114 | + while(*msg) |
| 115 | + { |
| 116 | + USART1->DR = *msg++; |
| 117 | + while(!(USART1->SR & USART_SR_TXE)); |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +## Optimization Tips |
| 123 | + |
| 124 | +### For Power Efficiency |
| 125 | +```c |
| 126 | +void LowPowerInit(void) |
| 127 | +{ |
| 128 | + // Like turning off unnecessary lights |
| 129 | + RCC->AHBENR &= ~(RCC_AHBENR_DMA1EN); |
| 130 | + PWR->CR |= PWR_CR_LPDS; |
| 131 | +} |
| 132 | +``` |
| 133 | +
|
| 134 | +### For Quick Response |
| 135 | +```c |
| 136 | +void FastInit(void) |
| 137 | +{ |
| 138 | + // Like preparing everything the night before |
| 139 | + FLASH->ACR |= FLASH_ACR_PRFTBE; // Quick access mode |
| 140 | + NVIC_SetPriority(SysTick_IRQn, 0); // Priority to alarm clock |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +## Food for Thought |
| 145 | + |
| 146 | +Your startup routine should adapt to your needs: |
| 147 | +- Need to wake up quickly? Streamline your morning routine |
| 148 | +- Safety critical? Add more checks |
| 149 | +- Power conscious? Turn off unnecessary systems |
| 150 | + |
| 151 | + |
| 152 | +Remember: The startup file is like the foundation of your house - take time to build it right, and your application will stand strong. |
| 153 | + |
| 154 | +These concepts become much clearer when we think of them in familiar terms. Just as we optimize our morning routines, we can optimize our MCU's startup process for different needs and scenarios. |
0 commit comments