flash.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include "flash.h"
  2. #include "nrf_delay.h"
  3. #include "uart.h"
  4. #include "bootloader.h"
  5. //#define NUM_FILE 15984
  6. //static bool store_flag=false;
  7. //static bool update_flag=false;
  8. //static bool load_flag=false;
  9. //static bool clear_flag=false;
  10. //static pstorage_handle_t m_storage_handle_app1;
  11. //static pstorage_handle_t m_storage_handle_app2;
  12. ////static pstorage_handle_t block_hander;
  13. //static void upgrade_pstorage_cb_handler(pstorage_handle_t * p_handle,
  14. // uint8_t op_code,
  15. // uint32_t result,
  16. // uint8_t * p_data,
  17. // uint32_t data_len)
  18. //{
  19. // switch(op_code)
  20. // {
  21. // case PSTORAGE_LOAD_OP_CODE:
  22. // if (result == NRF_SUCCESS)
  23. // {
  24. // load_flag=true;
  25. // printf("BOOTLOADER PSTORAGE_LOAD_OP_CODE success \r\n");
  26. // }
  27. // else
  28. // {
  29. // printf("BOOTLOADER PSTORAGE_LOAD_OP_CODE fail \r\n");
  30. // }
  31. // break;
  32. // case PSTORAGE_STORE_OP_CODE:
  33. // if (result == NRF_SUCCESS)
  34. // {
  35. // store_flag=true;
  36. // printf("BOOTLOADER PSTORAGE_STORE_OP_CODE success \r\n");
  37. // }
  38. // else
  39. // {
  40. // printf("BOOTLOADER PSTORAGE_STORE_OP_CODE fail \r\n");
  41. // }
  42. // break;
  43. // case PSTORAGE_CLEAR_OP_CODE:
  44. // if (result == NRF_SUCCESS)
  45. // {
  46. // clear_flag=true;
  47. // printf("BOOTLOADER PSTORAGE_CLEAR_OP_CODE success \r\n");
  48. // }
  49. // else
  50. // {
  51. // printf("BOOTLOADER PSTORAGE_CLEAR_OP_CODE fail \r\n");
  52. // }
  53. // break;
  54. // case PSTORAGE_UPDATE_OP_CODE:
  55. // if (result == NRF_SUCCESS)
  56. // {
  57. // update_flag=true;
  58. // printf("BOOTLOADER PSTORAGE_UPDATE_OP_CODE success \r\n");
  59. // }
  60. // else
  61. // {
  62. // printf("BOOTLOADER PSTORAGE_UPDATE_OP_CODE fail \r\n");
  63. // }
  64. // break;
  65. // default:break;
  66. // }
  67. //}
  68. //uint32_t flash_upgrade_op(void)
  69. //{
  70. // pstorage_module_param_t param;
  71. // uint32_t err_code;
  72. // //uint32_t i;
  73. //
  74. // param.cb = upgrade_pstorage_cb_handler;
  75. //// m_storage_handle_app1.block_id = DFU_BANK_0_REGION_START;
  76. //
  77. //
  78. //// err_code = pstorage_init();
  79. //// if(err_code == NRF_SUCCESS)
  80. //// {
  81. //// printf("BOOTLOADER flash pstorage init success!\r\n");
  82. //// }
  83. //// else
  84. //// {
  85. //// printf("BOOTLOADER flash pstorage init fail!\r\n");
  86. //// return err_code;
  87. //// }
  88. // err_code = pstorage_register(&param,&m_storage_handle_app1);
  89. // if(err_code == NRF_SUCCESS)
  90. // {
  91. // printf("BOOTLOADER flash pstorage register success!\r\n");
  92. // }
  93. // else
  94. // {
  95. // printf("BOOTLOADER flash pstorage register fail!\r\n");
  96. // return err_code;
  97. // }
  98. //
  99. // m_storage_handle_app1.block_id = DFU_BANK_0_REGION_START;
  100. // m_storage_handle_app2 = m_storage_handle_app1;
  101. // m_storage_handle_app2.block_id = DFU_BANK_1_REGION_START;
  102. //
  103. // clear_flag = false;
  104. // err_code = pstorage_clear(&m_storage_handle_app1,DFU_IMAGE_MAX_SIZE_BANKED);
  105. // if(err_code == NRF_SUCCESS)
  106. // {
  107. // printf("BOOTLOADER flash pstorage clear success!\r\n");
  108. // }
  109. // else
  110. // {
  111. // printf("BOOTLOADER flash pstorage clear fail!\r\n");
  112. // }
  113. // while(!clear_flag)
  114. // {
  115. // printf("BOOTLOADER flash clear wating...\r\n");
  116. // nrf_delay_ms(200);
  117. // }
  118. // store_flag = false;
  119. // err_code = pstorage_store(&m_storage_handle_app1,(uint8_t *)m_storage_handle_app2.block_id,NUM_FILE,0);
  120. // if(err_code == NRF_SUCCESS)
  121. // {
  122. // printf("BOOTLOADER flash pstorage store success!\r\n");
  123. // }
  124. // else
  125. // {
  126. // printf("BOOTLOADER flash pstorage store fail!\r\n");
  127. // }
  128. // while(!store_flag)
  129. // {
  130. // printf("BOOTLOADER flash store wating...\r\n");
  131. // nrf_delay_ms(200);
  132. // }
  133. // printf("BOOTLOADER flash pstorage store all success,start app now!\r\n");
  134. // //nrf_delay_ms(50);
  135. // //bootloader_app_start(DFU_BANK_0_REGION_START);
  136. //
  137. // return err_code;
  138. //}
  139. uint32_t dfu_app_image_swap(void)
  140. {
  141. sd_mbr_command_t sd_mbr_cmd;
  142. //uint32_t len;
  143. uint32_t err_code;
  144. sd_mbr_cmd.command = SD_MBR_COMMAND_COPY_SD;
  145. sd_mbr_cmd.params.copy_sd.src = (uint32_t *) 0x2AC00;
  146. sd_mbr_cmd.params.copy_sd.dst = (uint32_t *) 0x1B000;
  147. //sd_mbr_cmd.params.copy_sd.len = 4987;
  148. sd_mbr_cmd.params.copy_sd.len = (*((uint32_t *)DFU_APP_DATA_USER_START))/4;
  149. err_code = sd_mbr_command(&sd_mbr_cmd);
  150. if (err_code != NRF_SUCCESS)
  151. {
  152. // printf("BOOTLOADER swap app image fail!err_code is %d\r\n",err_code);
  153. return err_code;
  154. }
  155. sd_mbr_cmd.command = SD_MBR_COMMAND_COMPARE;
  156. return sd_mbr_command(&sd_mbr_cmd);
  157. }
  158. uint16_t reset_data_flash_update(uint8_t *src,uint16_t size,uint16_t offset)
  159. {
  160. return 0;
  161. }