main.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**@file
  13. *
  14. * @defgroup ble_sdk_app_bootloader_main main.c
  15. * @{
  16. * @ingroup dfu_bootloader_api
  17. * @brief Bootloader project main file.
  18. *
  19. * -# Receive start data packet.
  20. * -# Based on start packet, prepare NVM area to store received data.
  21. * -# Receive data packet.
  22. * -# Validate data packet.
  23. * -# Write Data packet to NVM.
  24. * -# If not finished - Wait for next packet.
  25. * -# Receive stop data packet.
  26. * -# Activate Image, boot application.
  27. *
  28. */
  29. #include "dfu_transport.h"
  30. #include "bootloader.h"
  31. #include "bootloader_util.h"
  32. #include <stdint.h>
  33. #include <string.h>
  34. #include <stddef.h>
  35. #include "nordic_common.h"
  36. #include "nrf.h"
  37. #include "nrf_soc.h"
  38. #include "app_error.h"
  39. #include "nrf_gpio.h"
  40. #include "ble.h"
  41. #include "nrf.h"
  42. #include "ble_hci.h"
  43. #include "app_scheduler.h"
  44. #include "app_timer_appsh.h"
  45. #include "nrf_error.h"
  46. #include "bsp.h"
  47. #include "softdevice_handler_appsh.h"
  48. #include "pstorage_platform.h"
  49. #include "nrf_mbr.h"
  50. #include "nrf_log.h"
  51. #include "uart.h" //zhou
  52. #include "flash.h" //zhou
  53. #include "nrf_delay.h" //zhou
  54. #if BUTTONS_NUMBER < 1
  55. #error "Not enough buttons on board"
  56. #endif
  57. #if LEDS_NUMBER < 1
  58. #error "Not enough LEDs on board"
  59. #endif
  60. #define IS_SRVC_CHANGED_CHARACT_PRESENT 1 /**< Include the service_changed characteristic. For DFU this should normally be the case. */
  61. #define BOOTLOADER_BUTTON BSP_BUTTON_3 /**< Button used to enter SW update mode. */
  62. #define UPDATE_IN_PROGRESS_LED BSP_LED_2 /**< Led used to indicate that DFU is active. */
  63. #define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */
  64. #define APP_TIMER_OP_QUEUE_SIZE 4 /**< Size of timer operation queues. */
  65. #define SCHED_MAX_EVENT_DATA_SIZE MAX(APP_TIMER_SCHED_EVT_SIZE, 0) /**< Maximum size of scheduler events. */
  66. #define SCHED_QUEUE_SIZE 20 /**< Maximum number of events in the scheduler queue. */
  67. /**@brief Callback function for asserts in the SoftDevice.
  68. *
  69. * @details This function will be called in case of an assert in the SoftDevice.
  70. *
  71. * @warning This handler is an example only and does not fit a final product. You need to analyze
  72. * how your product is supposed to react in case of Assert.
  73. * @warning On assert from the SoftDevice, the system can only recover on reset.
  74. *
  75. * @param[in] line_num Line number of the failing ASSERT call.
  76. * @param[in] file_name File name of the failing ASSERT call.
  77. */
  78. void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
  79. {
  80. app_error_handler(0xDEADBEEF, line_num, p_file_name);
  81. }
  82. /**@brief Function for initialization of LEDs.
  83. */
  84. //static void leds_init(void)
  85. //{
  86. // nrf_gpio_range_cfg_output(LED_START, LED_STOP);
  87. // nrf_gpio_pins_set(LEDS_MASK);
  88. //}
  89. /**@brief Function for initializing the timer handler module (app_timer).
  90. */
  91. static void timers_init(void)
  92. {
  93. // Initialize timer module, making it use the scheduler.
  94. APP_TIMER_APPSH_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, true);
  95. }
  96. /**@brief Function for initializing the button module.
  97. */
  98. //static void buttons_init(void)
  99. //{
  100. // nrf_gpio_cfg_sense_input(BOOTLOADER_BUTTON,
  101. // BUTTON_PULL,
  102. // NRF_GPIO_PIN_SENSE_LOW);
  103. //}
  104. /**@brief Function for dispatching a BLE stack event to all modules with a BLE stack event handler.
  105. *
  106. * @details This function is called from the scheduler in the main loop after a BLE stack
  107. * event has been received.
  108. *
  109. * @param[in] p_ble_evt Bluetooth stack event.
  110. */
  111. static void sys_evt_dispatch(uint32_t event)
  112. {
  113. pstorage_sys_event_handler(event);
  114. }
  115. /**@brief Function for initializing the BLE stack.
  116. *
  117. * @details Initializes the SoftDevice and the BLE event interrupt.
  118. *
  119. * @param[in] init_softdevice true if SoftDevice should be initialized. The SoftDevice must only
  120. * be initialized if a chip reset has occured. Soft reset from
  121. * application must not reinitialize the SoftDevice.
  122. */
  123. static void ble_stack_init(bool init_softdevice)
  124. {
  125. uint32_t err_code;
  126. sd_mbr_command_t com = {SD_MBR_COMMAND_INIT_SD, };
  127. nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;
  128. if (init_softdevice)
  129. {
  130. err_code = sd_mbr_command(&com);
  131. APP_ERROR_CHECK(err_code);
  132. }
  133. err_code = sd_softdevice_vector_table_base_set(BOOTLOADER_REGION_START);
  134. APP_ERROR_CHECK(err_code);
  135. SOFTDEVICE_HANDLER_APPSH_INIT(&clock_lf_cfg, true);
  136. // Enable BLE stack.
  137. ble_enable_params_t ble_enable_params;
  138. // Only one connection as a central is used when performing dfu.
  139. err_code = softdevice_enable_get_default_config(1, 1, &ble_enable_params);
  140. APP_ERROR_CHECK(err_code);
  141. ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT;
  142. err_code = softdevice_enable(&ble_enable_params);
  143. APP_ERROR_CHECK(err_code);
  144. err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
  145. APP_ERROR_CHECK(err_code);
  146. }
  147. /**@brief Function for event scheduler initialization.
  148. */
  149. static void scheduler_init(void)
  150. {
  151. APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE);
  152. }
  153. /**@brief Function for bootloader main entry.
  154. */
  155. int main(void)
  156. {
  157. uint32_t err_code;
  158. bool dfu_start = false;
  159. bool app_reset = (NRF_POWER->GPREGRET == BOOTLOADER_DFU_START);
  160. bool app_gprs_reset = (NRF_POWER->GPREGRET == BOOTLOADER_DFU_GPRS_START);
  161. bool app_gprs_reset2 = ((*((uint8_t *)(DFU_APP_DATA_USER_START+40))) == BOOTLOADER_DFU_GPRS_START);
  162. if (app_reset)
  163. {
  164. NRF_POWER->GPREGRET = 0;
  165. }
  166. // else if(app_gprs_reset)
  167. // {
  168. // NRF_POWER->GPREGRET = 0;
  169. // }
  170. // leds_init();
  171. // uart_init();
  172. // This check ensures that the defined fields in the bootloader corresponds with actual
  173. // setting in the chip.
  174. APP_ERROR_CHECK_BOOL(*((uint32_t *)NRF_UICR_BOOT_START_ADDRESS) == BOOTLOADER_REGION_START);
  175. APP_ERROR_CHECK_BOOL(NRF_FICR->CODEPAGESIZE == CODE_PAGE_SIZE);
  176. // Initialize.
  177. timers_init();
  178. //buttons_init();
  179. // printf("BOOTLOADER START!app_gprs_reset is %d\r\n",app_gprs_reset);
  180. // nrf_delay_ms(200);
  181. //
  182. // if (*((uint32_t *)DFU_APP_DATA_USER_START) == EMPTY_FLASH_MASK)
  183. // {
  184. // //printf("BOOT filenum no value\r\n");
  185. // nrf_delay_ms(200);
  186. // }
  187. // else
  188. // {
  189. // //printf("BOOT filenum is %d\r\n",*((uint32_t *)DFU_APP_DATA_USER_START));
  190. // nrf_delay_ms(200);
  191. // }
  192. // if(app_gprs_reset && *((uint32_t *)DFU_APP_DATA_USER_START) != EMPTY_FLASH_MASK)
  193. // {
  194. // //uart_init();
  195. // printf("BOOTLOADER GPRS UPGRADE!\r\n");
  196. // nrf_delay_ms(200);
  197. // dfu_app_image_swap();
  198. // nrf_delay_ms(4000);
  199. // bootloader_app_start(DFU_BANK_0_REGION_START);
  200. // //NVIC_SystemReset();
  201. // }
  202. // if(app_gprs_reset)
  203. // {
  204. // //uart_init();
  205. // printf("BOOTLOADER GPRS UPGRADE!\r\n");
  206. // nrf_delay_ms(200);
  207. // dfu_app_image_swap();
  208. // nrf_delay_ms(4000);
  209. // bootloader_app_start(DFU_BANK_0_REGION_START);
  210. // //NVIC_SystemReset();
  211. // }
  212. (void)bootloader_init();
  213. // if(app_gprs_reset)
  214. // {
  215. // //uart_init();
  216. // printf("BOOTLOADER GPRS UPGRADE!\r\n");
  217. // nrf_delay_ms(200);
  218. // dfu_app_image_swap();
  219. // nrf_delay_ms(4000);
  220. // bootloader_app_start(DFU_BANK_0_REGION_START);
  221. // //NVIC_SystemReset();
  222. // }
  223. if (bootloader_dfu_sd_in_progress())
  224. {
  225. //nrf_gpio_pin_clear(UPDATE_IN_PROGRESS_LED);
  226. err_code = bootloader_dfu_sd_update_continue();
  227. APP_ERROR_CHECK(err_code);
  228. ble_stack_init(!app_reset);
  229. scheduler_init();
  230. err_code = bootloader_dfu_sd_update_finalize();
  231. APP_ERROR_CHECK(err_code);
  232. // nrf_gpio_pin_set(UPDATE_IN_PROGRESS_LED);
  233. }
  234. else
  235. {
  236. // If stack is present then continue initialization of bootloader.
  237. ble_stack_init(!app_reset);
  238. scheduler_init();
  239. // printf("stack start!\r\n");
  240. }
  241. nrf_delay_ms(200);
  242. dfu_start = app_reset;
  243. //dfu_start |= ((nrf_gpio_pin_read(BOOTLOADER_BUTTON) == 0) ? true: false);
  244. if(app_gprs_reset || app_gprs_reset2)
  245. {
  246. //uart_init();
  247. // printf("BOOTLOADER GPRS UPGRADE!\r\n");
  248. // nrf_delay_ms(200);
  249. //flash_upgrade_op();
  250. err_code = dfu_app_image_swap();
  251. APP_ERROR_CHECK(err_code);
  252. nrf_delay_ms(4000);
  253. //NRF_POWER->GPREGRET = 0;
  254. sd_power_gpregret_clr(0xFF);
  255. bootloader_app_start(DFU_BANK_0_REGION_START);
  256. //NVIC_SystemReset();
  257. }
  258. else if (dfu_start || (!bootloader_app_is_valid(DFU_BANK_0_REGION_START)))
  259. {
  260. // printf("ble upgrade start!\r\n");
  261. //nrf_gpio_pin_clear(UPDATE_IN_PROGRESS_LED);
  262. // Initiate an update of the firmware.
  263. err_code = bootloader_dfu_start();
  264. APP_ERROR_CHECK(err_code);
  265. //nrf_gpio_pin_set(UPDATE_IN_PROGRESS_LED);
  266. }
  267. if (bootloader_app_is_valid(DFU_BANK_0_REGION_START) && !bootloader_dfu_sd_in_progress())
  268. {
  269. // Select a bank region to use as application region.
  270. // @note: Only applications running from DFU_BANK_0_REGION_START is supported.
  271. bootloader_app_start(DFU_BANK_0_REGION_START);
  272. }
  273. NVIC_SystemReset();
  274. }