| BlueNRG-1 DK: BLE examples

- Main Page
- Modules
- Files

- File List

OTA_ResetManager_main.c
Go to the documentation of this file.
1
2 /******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
3 * File Name : OTA_ResetManager_main.c
4 * Author : VMA DIVISION
5 * Version : 1.1.0
6 * Date : 15-January-2016
7 * Description : Code demostrating the BLE OTA Reset Manager application
8 ********************************************************************************
9 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
10 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
11 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
12 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
13 * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
14 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
15 *******************************************************************************/
16
166 /* Includes ------------------------------------------------------------------*/
167
168 #include "BlueNRG1_conf.h"
169 #include "SDK_EVAL_Config.h"
170 #include "bluenrg1_api.h"
171 #include "OTA_btl.h"
172 #include "bluenrg1_it_stub.h"
173
174 typedef void (*pFunction)(void);
175
176 #define RESET_WAKE_DEEPSLEEP_REASONS 0x05
177
181 #define TAG_VALUE(x) (* ((volatile uint32_t*) ((x) + OTA_TAG_VECTOR_TABLE_ENTRY_OFFSET)))
182
190 static uint8_t OTA_Check_Storage_Area(uint32_t start_address, uint32_t end_address)
191 {
192 volatile uint32_t *address;
193 uint32_t i;
194
195 for(i=start_address;i<end_address; i = i +4)
196 {
197 address = (volatile uint32_t *) i;
198 if (*address != 0xFFFFFFFF)
199 return 1; /* do flash erase */
200 }
201
202 return 0; /* no flash erase is required */
203 }
204
212 static void OTA_Erase_Storage_Area(uint16_t startPageNumber, uint16_t endPageNumber)
213 {
214 uint16_t k;
215
216 for(k=startPageNumber;k<=endPageNumber;k++)
217 {
218 FLASH_ErasePage(k);
219 }
220 }
221
231 static uint32_t OTA_Check_Application_Tags_Value(void)
232 {
233 uint32_t appAddress = 0;
234 if ( ((TAG_VALUE(APP_LOWER_ADDRESS) == OTA_INVALID_OLD_TAG) && (TAG_VALUE(APP_HIGHER_ADDRESS) == OTA_INVALID_OLD_TAG)) || /* 1 */
235 ((TAG_VALUE(APP_LOWER_ADDRESS) == OTA_VALID_TAG) && (TAG_VALUE(APP_HIGHER_ADDRESS) == OTA_INVALID_OLD_TAG)) || /* 2 */
236 ((TAG_VALUE(APP_LOWER_ADDRESS) == OTA_VALID_TAG) && (TAG_VALUE(APP_HIGHER_ADDRESS) == OTA_VALID_TAG)) || /* 4 */
237 ((TAG_VALUE(APP_LOWER_ADDRESS) == OTA_INVALID_OLD_TAG) && (TAG_VALUE(APP_HIGHER_ADDRESS) == OTA_IN_PROGRESS_TAG)) || /* 8 */
238 ((TAG_VALUE(APP_LOWER_ADDRESS) == OTA_VALID_TAG) && (TAG_VALUE(APP_HIGHER_ADDRESS) == OTA_IN_PROGRESS_TAG))) /* 9 */
239 {
240 /* Jump to Lower Application */
241 appAddress = APP_LOWER_ADDRESS;
242
243 if (OTA_Check_Storage_Area(APP_HIGHER_ADDRESS,APP_HIGHER_ADDRESS_END))
244 /* Erase OLD Higher application storage area */
245 OTA_Erase_Storage_Area(OTA_HIGHER_APPLICATION_PAGE_NUMBER_START, OTA_HIGHER_APPLICATION_PAGE_NUMBER_END);
246 }
247 else if ( ((TAG_VALUE(APP_LOWER_ADDRESS) == OTA_INVALID_OLD_TAG) && (TAG_VALUE(APP_HIGHER_ADDRESS) == OTA_VALID_TAG)) || /* 3 */
248 ((TAG_VALUE(APP_LOWER_ADDRESS) == OTA_IN_PROGRESS_TAG) && (TAG_VALUE(APP_HIGHER_ADDRESS) == OTA_INVALID_OLD_TAG)) || /* 6 */
249 ((TAG_VALUE(APP_LOWER_ADDRESS) == OTA_IN_PROGRESS_TAG) && (TAG_VALUE(APP_HIGHER_ADDRESS) == OTA_VALID_TAG))) /* 7 */
250 {
251 /* Jump to Higher Application */
252 appAddress = APP_HIGHER_ADDRESS;
253
254 if (OTA_Check_Storage_Area(APP_LOWER_ADDRESS,APP_LOWER_ADDRESS_END))
255 /* Erase OLD Lower application storage area */
256 OTA_Erase_Storage_Area(OTA_LOWER_APPLICATION_PAGE_NUMBER_START, OTA_LOWER_APPLICATION_PAGE_NUMBER_END);
257 }
258 else if ((TAG_VALUE(APP_LOWER_ADDRESS) == OTA_IN_PROGRESS_TAG) && (TAG_VALUE(APP_HIGHER_ADDRESS) == OTA_IN_PROGRESS_TAG)) /* 5 */
259 {
260 /* 5: Is it possible? No. What to do?*/
261 }
262 return appAddress;
263 }
264
272 int main(void)
273 {
274 pFunction Jump_To_Application;
275 uint32_t JumpAddress, appAddress;
276
277 /* Identifies the valid application where to jump based on the OTA application validity tags values placed on
278 reserved vector table entry: OTA_TAG_VECTOR_TABLE_ENTRY_INDEX */
279 appAddress = OTA_Check_Application_Tags_Value();
280
281 if (appAddress == 0) {
282 /* This case indicates that no valida application is present and normally should not happen */
283 while (1);
284 }
285
286 set_irq_base(appAddress);
287 /* Jump to user application */
288 JumpAddress = *(__IO uint32_t*) (appAddress + 4);
289 Jump_To_Application = (pFunction) JumpAddress;
290 /* Initialize user application's Stack Pointer */
291 __set_MSP(*(__IO uint32_t*) appAddress);
292 Jump_To_Application();
293
294 /* Infinite loop */
295 while (1)
296 {
297 }
298 }
299 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/