HAL_Flash_rtthread.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include "HAL_Flash_Platform.h"
  16. #include <fal.h>
  17. #define DOWNLOAD_PARTITION "download"
  18. void * HAL_Download_Name_Set(void * handle)
  19. {
  20. return (void *)DOWNLOAD_PARTITION;
  21. }
  22. void * HAL_Download_Init(_IN_ void * name)
  23. {
  24. const struct fal_partition * dl_part = RT_NULL;
  25. if ((dl_part = fal_partition_find((char *)name)) == RT_NULL)
  26. {
  27. LOG_ERROR("Firmware download failed! Partition (%s) find error!", name);
  28. return NULL;
  29. }
  30. LOG_INFO("Start erase flash (%s) partition!", dl_part->name);
  31. if (fal_partition_erase_all(dl_part) < 0)
  32. {
  33. LOG_ERROR("Firmware download failed! Partition (%s) erase error!", dl_part->name);
  34. return NULL;
  35. }
  36. LOG_INFO("Erase flash (%s) partition success!", dl_part->name);
  37. return (void *)dl_part;
  38. }
  39. int HAL_Download_Write(_IN_ void * handle,_IN_ uint32_t total_length,_IN_ uint8_t *buffer_read,_IN_ uint32_t length)
  40. {
  41. const struct fal_partition * dl_part = (struct fal_partition *) handle;
  42. if(dl_part==NULL)
  43. return FAILURE_RET;
  44. if (fal_partition_write(dl_part, total_length, buffer_read, length) < 0){
  45. LOG_ERROR("Firmware download failed! Partition (%s) write data error!", dl_part->name);
  46. return FAILURE_RET;
  47. }
  48. return SUCCESS_RET;
  49. }
  50. int HAL_Download_End(_IN_ void * handle)
  51. {
  52. LOG_INFO("System now will restart...");
  53. rt_thread_delay(rt_tick_from_millisecond(5));
  54. /* Reset the device, Start new firmware */
  55. extern void rt_hw_cpu_reset(void);
  56. rt_hw_cpu_reset();
  57. return SUCCESS_RET;
  58. }