luat_fota_air105.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "luat_base.h"
  22. #include "luat_mcu.h"
  23. #include "luat_spi.h"
  24. #include "app_interface.h"
  25. #include "luat_fota.h"
  26. #include "FreeRTOS.h"
  27. int luat_fota_init(uint32_t start_address, uint32_t len, luat_spi_device_t* spi_device, const char *path, uint32_t pathlen)
  28. {
  29. PV_Union uPV;
  30. CoreUpgrade_HeadStruct *Head = luat_heap_malloc(sizeof(CoreUpgrade_HeadStruct));
  31. memset(Head, 0, sizeof(CoreUpgrade_HeadStruct));
  32. Head->MaigcNum = __APP_START_MAGIC__;
  33. uPV.u8[0] = CORE_OTA_MODE_FULL;
  34. Head->DataStartAddress = start_address;
  35. if (path && pathlen)
  36. {
  37. uPV.u8[1] = CORE_OTA_IN_FILE;
  38. memcpy(Head->FilePath, path, pathlen);
  39. Head->Param1 = uPV.u32;
  40. }
  41. else
  42. {
  43. if (start_address > __FLASH_BASE_ADDR__)
  44. {
  45. uPV.u8[1] = CORE_OTA_IN_FLASH;
  46. }
  47. else
  48. {
  49. uPV.u8[1] = CORE_OTA_OUT_SPI_FLASH;
  50. uPV.u8[2] = luat_spi_get_hw_bus(spi_device->bus_id);
  51. Head->Param1 = uPV.u32;
  52. switch(luat_spi_get_hw_bus(spi_device->bus_id))
  53. {
  54. case HSPI_ID0:
  55. uPV.u8[0] = GPIOC_13;
  56. uPV.u8[1] = GPIOC_12;
  57. uPV.u8[2] = GPIOC_15;
  58. break;
  59. case SPI_ID0:
  60. uPV.u8[0] = GPIOB_14;
  61. uPV.u8[1] = GPIOB_15;
  62. uPV.u8[2] = GPIOB_12;
  63. break;
  64. case SPI_ID1:
  65. uPV.u8[0] = GPIOA_08;
  66. uPV.u8[1] = GPIOA_09;
  67. uPV.u8[2] = GPIOA_06;
  68. break;
  69. case SPI_ID2:
  70. uPV.u8[0] = GPIOB_04;
  71. uPV.u8[1] = GPIOB_05;
  72. uPV.u8[2] = GPIOB_02;
  73. break;
  74. }
  75. uPV.u8[3] = spi_device->spi_config.cs;
  76. Head->Param2 = uPV.u32;
  77. }
  78. }
  79. return Core_OTAInit(Head, len);
  80. }
  81. int luat_fota_write(uint8_t *data, uint32_t len)
  82. {
  83. return Core_OTAWrite(data, len);
  84. }
  85. int luat_fota_done(void)
  86. {
  87. return Core_OTACheckDone();
  88. }
  89. int luat_fota_end(uint8_t is_ok)
  90. {
  91. Core_OTAEnd(is_ok);
  92. return 0;
  93. }
  94. uint8_t luat_fota_wait_ready(void)
  95. {
  96. return Core_OTACheckReadyStart();
  97. }