amiro-blt / Target / Source / ARMCM4_STM32 / cpu.c @ cc06d380
History | View | Annotate | Download (6.512 KB)
1 | 69661903 | Thomas Schöpping | /************************************************************************************//** |
---|---|---|---|
2 | * \file Source\ARMCM4_STM32\cpu.c
|
||
3 | * \brief Bootloader cpu module source file.
|
||
4 | * \ingroup Target_ARMCM4_STM32
|
||
5 | * \internal
|
||
6 | *----------------------------------------------------------------------------------------
|
||
7 | * C O P Y R I G H T
|
||
8 | *----------------------------------------------------------------------------------------
|
||
9 | * Copyright (c) 2013 by Feaser http://www.feaser.com All rights reserved
|
||
10 | *
|
||
11 | *----------------------------------------------------------------------------------------
|
||
12 | * L I C E N S E
|
||
13 | *----------------------------------------------------------------------------------------
|
||
14 | * This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
|
||
15 | * modify it under the terms of the GNU General Public License as published by the Free
|
||
16 | * Software Foundation, either version 3 of the License, or (at your option) any later
|
||
17 | * version.
|
||
18 | *
|
||
19 | * OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||
20 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||
21 | * PURPOSE. See the GNU General Public License for more details.
|
||
22 | *
|
||
23 | * You should have received a copy of the GNU General Public License along with OpenBLT.
|
||
24 | * If not, see <http://www.gnu.org/licenses/>.
|
||
25 | *
|
||
26 | * A special exception to the GPL is included to allow you to distribute a combined work
|
||
27 | * that includes OpenBLT without being obliged to provide the source code for any
|
||
28 | * proprietary components. The exception text is included at the bottom of the license
|
||
29 | * file <license.html>.
|
||
30 | *
|
||
31 | * \endinternal
|
||
32 | ****************************************************************************************/
|
||
33 | |||
34 | /****************************************************************************************
|
||
35 | * Include files
|
||
36 | ****************************************************************************************/
|
||
37 | #include "boot.h" /* bootloader generic header */ |
||
38 | |||
39 | |||
40 | /****************************************************************************************
|
||
41 | * Macro definitions
|
||
42 | ****************************************************************************************/
|
||
43 | /** \brief Pointer to the user program's reset vector. */
|
||
44 | #define CPU_USER_PROGRAM_STARTADDR_PTR ((blt_addr)(FlashGetUserProgBaseAddress() + 0x00000004)) |
||
45 | /** \brief Pointer to the user program's vector table. */
|
||
46 | #define CPU_USER_PROGRAM_VECTABLE_OFFSET ((blt_int32u)FlashGetUserProgBaseAddress())
|
||
47 | |||
48 | |||
49 | /****************************************************************************************
|
||
50 | * Register definitions
|
||
51 | ****************************************************************************************/
|
||
52 | /** \brief Vector table offset register. */
|
||
53 | #define SCB_VTOR (*((volatile blt_int32u *) 0xE000ED08)) |
||
54 | |||
55 | |||
56 | /****************************************************************************************
|
||
57 | * Hook functions
|
||
58 | ****************************************************************************************/
|
||
59 | #if (BOOT_CPU_USER_PROGRAM_START_HOOK > 0) |
||
60 | extern blt_bool CpuUserProgramStartHook(void); |
||
61 | #endif
|
||
62 | |||
63 | |||
64 | /****************************************************************************************
|
||
65 | * External functions
|
||
66 | ****************************************************************************************/
|
||
67 | extern void reset_handler(void); /* implemented in cstart.s */ |
||
68 | |||
69 | |||
70 | /************************************************************************************//** |
||
71 | ** \brief Starts the user program, if one is present. In this case this function
|
||
72 | ** does not return.
|
||
73 | ** \return none.
|
||
74 | **
|
||
75 | ****************************************************************************************/
|
||
76 | void CpuStartUserProgram(void) |
||
77 | { |
||
78 | void (*pProgResetHandler)(void); |
||
79 | blt_addr msp; |
||
80 | |||
81 | /* check if a user program is present by verifying the checksum */
|
||
82 | if (NvmVerifyChecksum() == BLT_FALSE)
|
||
83 | { |
||
84 | /* not a valid user program so it cannot be started */
|
||
85 | return;
|
||
86 | } |
||
87 | #if (BOOT_CPU_USER_PROGRAM_START_HOOK > 0) |
||
88 | /* invoke callback */
|
||
89 | if (CpuUserProgramStartHook() == BLT_FALSE)
|
||
90 | { |
||
91 | /* callback requests the user program to not be started */
|
||
92 | return;
|
||
93 | } |
||
94 | #endif
|
||
95 | #if (BOOT_COM_ENABLE > 0) |
||
96 | /* release the communication interface */
|
||
97 | ComFree(); |
||
98 | #endif
|
||
99 | /* reset the timer */
|
||
100 | TimerReset(); |
||
101 | /* remap user program's vector table */
|
||
102 | SCB_VTOR = CPU_USER_PROGRAM_VECTABLE_OFFSET & (blt_int32u)0x1FFFFF80;
|
||
103 | |||
104 | /* set the mainstack to vector table for user program start.
|
||
105 | */
|
||
106 | msp = *((blt_addr*)CPU_USER_PROGRAM_VECTABLE_OFFSET); |
||
107 | asm volatile ("MSR MSP, %0" : : "r" (msp) ); |
||
108 | |||
109 | /* set the address where the bootloader needs to jump to. this is the address of
|
||
110 | * the 2nd entry in the user program's vector table. this address points to the
|
||
111 | * user program's reset handler.
|
||
112 | */
|
||
113 | pProgResetHandler = (void(*)(void))(*((blt_addr*)CPU_USER_PROGRAM_STARTADDR_PTR)); |
||
114 | /* start the user program by activating its reset interrupt service routine */
|
||
115 | pProgResetHandler(); |
||
116 | } /*** end of CpuStartUserProgram ***/
|
||
117 | |||
118 | |||
119 | /************************************************************************************//** |
||
120 | ** \brief Copies data from the source to the destination address.
|
||
121 | ** \param dest Destination address for the data.
|
||
122 | ** \param src Source address of the data.
|
||
123 | ** \param len length of the data in bytes.
|
||
124 | ** \return none.
|
||
125 | **
|
||
126 | ****************************************************************************************/
|
||
127 | void CpuMemCopy(blt_addr dest, blt_addr src, blt_int16u len)
|
||
128 | { |
||
129 | blt_int8u *from, *to; |
||
130 | |||
131 | /* set casted pointers */
|
||
132 | from = (blt_int8u *)src; |
||
133 | to = (blt_int8u *)dest; |
||
134 | |||
135 | /* copy all bytes from source address to destination address */
|
||
136 | while(len-- > 0) |
||
137 | { |
||
138 | /* store byte value from source to destination */
|
||
139 | *to++ = *from++; |
||
140 | /* keep the watchdog happy */
|
||
141 | CopService(); |
||
142 | } |
||
143 | } /*** end of CpuMemCopy ***/
|
||
144 | |||
145 | |||
146 | /************************************************************************************//** |
||
147 | ** \brief Perform a soft reset of the microcontroller by starting from the reset ISR.
|
||
148 | ** \return none.
|
||
149 | **
|
||
150 | ****************************************************************************************/
|
||
151 | void CpuReset(void) |
||
152 | { |
||
153 | /* perform a software reset by calling the reset ISR routine */
|
||
154 | reset_handler(); |
||
155 | } /*** end of CpuReset ***/
|
||
156 | |||
157 | |||
158 | /*********************************** end of cpu.c **************************************/
|