amiro-blt / Target / Modules / PowerManagement_1-1 / Boot / main.c @ 367c0652
History | View | Annotate | Download (60.885 KB)
1 | 69661903 | Thomas Schöpping | /************************************************************************************//** |
---|---|---|---|
2 | * \file Demo\ARMCM4_STM32_Olimex_STM32E407_GCC\Boot\main.c
|
||
3 | * \brief Bootloader application source file.
|
||
4 | * \ingroup Boot_ARMCM4_STM32_Olimex_STM32E407_GCC
|
||
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 | 470d0567 | Thomas Schöpping | * 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 | 69661903 | Thomas Schöpping | * proprietary components. The exception text is included at the bottom of the license
|
29 | * file <license.html>.
|
||
30 | 470d0567 | Thomas Schöpping | *
|
31 | 69661903 | Thomas Schöpping | * \endinternal
|
32 | ****************************************************************************************/
|
||
33 | |||
34 | /****************************************************************************************
|
||
35 | * Include files
|
||
36 | ****************************************************************************************/
|
||
37 | #include "boot.h" /* bootloader generic header */ |
||
38 | #include "stm32f4xx.h" /* STM32 registers */ |
||
39 | #include "stm32f4xx_conf.h" /* STM32 peripheral drivers */ |
||
40 | #include "com.h" |
||
41 | #include "ARMCM4_STM32/types.h" |
||
42 | 470d0567 | Thomas Schöpping | #include "AMiRo/amiroblt.h" |
43 | 69661903 | Thomas Schöpping | #include "AMiRo/helper.h" |
44 | |||
45 | /****************************************************************************************
|
||
46 | * Defines
|
||
47 | ****************************************************************************************/
|
||
48 | #define WKUP_GPIO GPIOA
|
||
49 | #define WKUP_PIN GPIO_Pin_0
|
||
50 | #define SYS_UART_TX_GPIO GPIOA
|
||
51 | #define SYS_UART_TX_PIN GPIO_Pin_2
|
||
52 | #define SYS_UART_RX_GPIO GPIOA
|
||
53 | #define SYS_UART_RX_PIN GPIO_Pin_3
|
||
54 | #define SYS_SPI_SS0_N_GPIO GPIOA
|
||
55 | #define SYS_SPI_SS0_N_PIN GPIO_Pin_4
|
||
56 | #define SYS_SPI_SCLK_GPIO GPIOA
|
||
57 | #define SYS_SPI_SCLK_PIN GPIO_Pin_5
|
||
58 | #define SYS_SPI_MISO_GPIO GPIOA
|
||
59 | #define SYS_SPI_MISO_PIN GPIO_Pin_6
|
||
60 | #define SYS_SPI_MOSI_GPIO GPIOA
|
||
61 | #define SYS_SPI_MOSI_PIN GPIO_Pin_7
|
||
62 | #define SYS_REG_EN_GPIO GPIOA
|
||
63 | #define SYS_REG_EN_PIN GPIO_Pin_8
|
||
64 | #define PROG_RX_GPIO GPIOA
|
||
65 | #define PROG_RX_PIN GPIO_Pin_9
|
||
66 | #define PROG_TX_GPIO GPIOA
|
||
67 | #define PROG_TX_PIN GPIO_Pin_10
|
||
68 | #define CAN_RX_GPIO GPIOA
|
||
69 | #define CAN_RX_PIN GPIO_Pin_11
|
||
70 | #define CAN_TX_GPIO GPIOA
|
||
71 | #define CAN_TX_PIN GPIO_Pin_12
|
||
72 | #define SWDIO_GPIO GPIOA
|
||
73 | #define SWDIO_PIN GPIO_Pin_13
|
||
74 | #define SWCLK_GPIO GPIOA
|
||
75 | #define SWCLK_PIN GPIO_Pin_14
|
||
76 | #define SYS_SPI_SS1_N_GPIO GPIOA
|
||
77 | #define SYS_SPI_SS1_N_PIN GPIO_Pin_15
|
||
78 | |||
79 | #define IR_INT1_N_GPIO GPIOB
|
||
80 | #define IR_INT1_N_PIN GPIO_Pin_0
|
||
81 | #define VSYS_SENSE_GPIO GPIOB
|
||
82 | #define VSYS_SENSE_PIN GPIO_Pin_1
|
||
83 | #define POWER_EN_GPIO GPIOB
|
||
84 | #define POWER_EN_PIN GPIO_Pin_2
|
||
85 | #define SYS_UART_DN_GPIO GPIOB
|
||
86 | #define SYS_UART_DN_PIN GPIO_Pin_3
|
||
87 | #define CHARGE_STAT2A_GPIO GPIOB
|
||
88 | #define CHARGE_STAT2A_PIN GPIO_Pin_4
|
||
89 | #define BUZZER_GPIO GPIOB
|
||
90 | #define BUZZER_PIN GPIO_Pin_5
|
||
91 | #define GAUGE_BATLOW2_GPIO GPIOB
|
||
92 | #define GAUGE_BATLOW2_PIN GPIO_Pin_6
|
||
93 | #define GAUGE_BATGD2_N_GPIO GPIOB
|
||
94 | #define GAUGE_BATGD2_N_PIN GPIO_Pin_7
|
||
95 | #define GAUGE_SCL2_GPIO GPIOB
|
||
96 | #define GAUGE_SCL2_PIN GPIO_Pin_8
|
||
97 | #define GAUGE_SDA2_GPIO GPIOB
|
||
98 | #define GAUGE_SDA2_PIN GPIO_Pin_9
|
||
99 | #define GAUGE_SCL1_GPIO GPIOB
|
||
100 | #define GAUGE_SCL1_PIN GPIO_Pin_10
|
||
101 | #define GAUGE_SDA1_GPIO GPIOB
|
||
102 | #define GAUGE_SDA1_PIN GPIO_Pin_11
|
||
103 | #define LED_GPIO GPIOB
|
||
104 | #define LED_PIN GPIO_Pin_12
|
||
105 | #define BT_RTS_GPIO GPIOB
|
||
106 | #define BT_RTS_PIN GPIO_Pin_13
|
||
107 | #define BT_CTS_GPIO GPIOB
|
||
108 | #define BT_CTS_PIN GPIO_Pin_14
|
||
109 | #define SYS_UART_UP_GPIO GPIOB
|
||
110 | #define SYS_UART_UP_PIN GPIO_Pin_15
|
||
111 | |||
112 | #define CHARGE_STAT1A_GPIO GPIOC
|
||
113 | #define CHARGE_STAT1A_PIN GPIO_Pin_0
|
||
114 | #define GAUGE_BATLOW1_GPIO GPIOC
|
||
115 | #define GAUGE_BATLOW1_PIN GPIO_Pin_1
|
||
116 | #define GAUGE_BATGD1_N_GPIO GPIOC
|
||
117 | #define GAUGE_BATGD1_N_PIN GPIO_Pin_2
|
||
118 | #define CHARGE_EN1_N_GPIO GPIOC
|
||
119 | #define CHARGE_EN1_N_PIN GPIO_Pin_3
|
||
120 | #define IR_INT2_N_GPIO GPIOC
|
||
121 | #define IR_INT2_N_PIN GPIO_Pin_4
|
||
122 | #define TOUCH_INT_N_GPIO GPIOC
|
||
123 | #define TOUCH_INT_N_PIN GPIO_Pin_5
|
||
124 | #define SYS_DONE_GPIO GPIOC
|
||
125 | #define SYS_DONE_PIN GPIO_Pin_6
|
||
126 | #define SYS_PROG_N_GPIO GPIOC
|
||
127 | #define SYS_PROG_N_PIN GPIO_Pin_7
|
||
128 | #define PATH_DC_GPIO GPIOC
|
||
129 | #define PATH_DC_PIN GPIO_Pin_8
|
||
130 | #define SYS_SPI_DIR_GPIO GPIOC
|
||
131 | #define SYS_SPI_DIR_PIN GPIO_Pin_9
|
||
132 | #define BT_RX_GPIO GPIOC
|
||
133 | #define BT_RX_PIN GPIO_Pin_10
|
||
134 | #define BT_TX_GPIO GPIOC
|
||
135 | #define BT_TX_PIN GPIO_Pin_11
|
||
136 | #define SYS_SYNC_N_GPIO GPIOC
|
||
137 | #define SYS_SYNC_N_PIN GPIO_Pin_12
|
||
138 | #define SYS_PD_N_GPIO GPIOC
|
||
139 | #define SYS_PD_N_PIN GPIO_Pin_13
|
||
140 | #define SYS_WARMRST_N_GPIO GPIOC
|
||
141 | #define SYS_WARMRST_N_PIN GPIO_Pin_14
|
||
142 | #define BT_RST_GPIO GPIOC
|
||
143 | #define BT_RST_PIN GPIO_Pin_15
|
||
144 | |||
145 | #define OSC_IN_GPIO GPIOD
|
||
146 | #define OSC_IN_PIN GPIO_Pin_0
|
||
147 | #define OSC_OUT_GPIO GPIOD
|
||
148 | #define OSC_OUT_PIN GPIO_Pin_1
|
||
149 | #define CHARGE_EN2_N_GPIO GPIOD
|
||
150 | #define CHARGE_EN2_N_PIN GPIO_Pin_2
|
||
151 | |||
152 | #define HIBERNATE_TIME_MS 5000 |
||
153 | |||
154 | /****************************************************************************************
|
||
155 | * Function prototypes and static variables
|
||
156 | ****************************************************************************************/
|
||
157 | static void Init(void); |
||
158 | |||
159 | static void initGpio(); |
||
160 | static void initExti(); |
||
161 | void configGpioForShutdown();
|
||
162 | void systemPowerDown();
|
||
163 | |||
164 | ErrorStatus handleColdReset(); |
||
165 | ErrorStatus handleSoftwareReset(); |
||
166 | ErrorStatus handleUartDnWakeup(); |
||
167 | ErrorStatus handlePathDcWakeup(); |
||
168 | ErrorStatus handleTouchWakeup(); |
||
169 | ErrorStatus handleIwdgWakeup(); |
||
170 | |||
171 | static void indicateHibernate(); |
||
172 | static void AdcSingleMeasurement(); |
||
173 | |||
174 | ADC_TypeDef* setupADC(ADC_TypeDef* adc, const uint16_t low_th, const uint16_t high_th); |
||
175 | uint16_t configIwdg(const uint16_t ms);
|
||
176 | |||
177 | ErrorStatus shutdownDisambiguationProcedure(const uint8_t type);
|
||
178 | void shutdownToTransportation();
|
||
179 | void shutdownToDeepsleep();
|
||
180 | void shutdownToHibernate();
|
||
181 | void shutdownAndRestart();
|
||
182 | |||
183 | 470d0567 | Thomas Schöpping | volatile blBackupRegister_t backup_reg;
|
184 | 69661903 | Thomas Schöpping | |
185 | /****************************************************************************************
|
||
186 | * Callback configuration
|
||
187 | ****************************************************************************************/
|
||
188 | void blCallbackShutdownTransportation(void); |
||
189 | void blCallbackShutdownDeepsleep(void); |
||
190 | void blCallbackShutdownHibernate(void); |
||
191 | void blCallbackShutdownRestart(void); |
||
192 | void blCallbackHandleShutdownRequest(void); |
||
193 | |||
194 | 470d0567 | Thomas Schöpping | const blCallbackTable_t cbtable __attribute__ ((section ("_callback_table"))) = { |
195 | 69661903 | Thomas Schöpping | .magicNumber = BL_MAGIC_NUMBER, |
196 | f4758731 | Thomas Schöpping | .vBootloader = {BL_VERSION_ID_AMiRoBLT_Release, BL_VERSION_MAJOR, BL_VERSION_MINOR, 1},
|
197 | 470d0567 | Thomas Schöpping | .vSSSP = {BL_VERSION_ID_SSSP, SSSP_VERSION_MAJOR, SSSP_VERSION_MINOR, 0},
|
198 | .vCompiler = {BL_VERSION_ID_GCC, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__}, // currently only GCC is supported
|
||
199 | 69661903 | Thomas Schöpping | .cbShutdownHibernate = blCallbackShutdownHibernate, |
200 | .cbShutdownDeepsleep = blCallbackShutdownDeepsleep, |
||
201 | .cbShutdownTransportation = blCallbackShutdownTransportation, |
||
202 | .cbShutdownRestart = blCallbackShutdownRestart, |
||
203 | .cbHandleShutdownRequest = blCallbackHandleShutdownRequest, |
||
204 | .cb5 = (void*)0, |
||
205 | .cb6 = (void*)0, |
||
206 | .cb7 = (void*)0, |
||
207 | .cb8 = (void*)0, |
||
208 | .cb9 = (void*)0, |
||
209 | .cb10 = (void*)0, |
||
210 | .cb11 = (void*)0 |
||
211 | }; |
||
212 | |||
213 | /************************************************************************************//** |
||
214 | 470d0567 | Thomas Schöpping | ** \brief This is the entry point for the bootloader application and is called
|
215 | 69661903 | Thomas Schöpping | ** by the reset interrupt vector after the C-startup routines executed.
|
216 | ** \return none.
|
||
217 | **
|
||
218 | ****************************************************************************************/
|
||
219 | void main(void) |
||
220 | { |
||
221 | /* initialize the microcontroller */
|
||
222 | Init(); |
||
223 | |||
224 | /* activate some required clocks */
|
||
225 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE); |
||
226 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); |
||
227 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); |
||
228 | |||
229 | /* initialize GPIOs and EXTI lines */
|
||
230 | initGpio(); |
||
231 |