fsl_flashiap.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "fsl_flashiap.h"
  31. #define HZ_TO_KHZ_DIV 1000
  32. /*******************************************************************************
  33. * Code
  34. ******************************************************************************/
  35. static status_t translate_iap_status(uint32_t status)
  36. {
  37. /* Translate IAP return code to sdk status code */
  38. if (status == kStatus_Success)
  39. {
  40. return status;
  41. }
  42. else
  43. {
  44. return MAKE_STATUS(kStatusGroup_FLASHIAP, status);
  45. }
  46. }
  47. status_t FLASHIAP_PrepareSectorForWrite(uint32_t startSector, uint32_t endSector)
  48. {
  49. uint32_t command[5], result[4];
  50. command[0] = kIapCmd_FLASHIAP_PrepareSectorforWrite;
  51. command[1] = startSector;
  52. command[2] = endSector;
  53. iap_entry(command, result);
  54. return translate_iap_status(result[0]);
  55. }
  56. status_t FLASHIAP_CopyRamToFlash(uint32_t dstAddr, uint32_t *srcAddr, uint32_t numOfBytes, uint32_t systemCoreClock)
  57. {
  58. uint32_t command[5], result[4];
  59. command[0] = kIapCmd_FLASHIAP_CopyRamToFlash;
  60. command[1] = dstAddr;
  61. command[2] = (uint32_t)srcAddr;
  62. command[3] = numOfBytes;
  63. command[4] = systemCoreClock / HZ_TO_KHZ_DIV;
  64. iap_entry(command, result);
  65. return translate_iap_status(result[0]);
  66. }
  67. status_t FLASHIAP_EraseSector(uint32_t startSector, uint32_t endSector, uint32_t systemCoreClock)
  68. {
  69. uint32_t command[5], result[4];
  70. command[0] = kIapCmd_FLASHIAP_EraseSector;
  71. command[1] = startSector;
  72. command[2] = endSector;
  73. command[3] = systemCoreClock / HZ_TO_KHZ_DIV;
  74. iap_entry(command, result);
  75. return translate_iap_status(result[0]);
  76. }
  77. status_t FLASHIAP_ErasePage(uint32_t startPage, uint32_t endPage, uint32_t systemCoreClock)
  78. {
  79. uint32_t command[5], result[4];
  80. command[0] = kIapCmd_FLASHIAP_ErasePage;
  81. command[1] = startPage;
  82. command[2] = endPage;
  83. command[3] = systemCoreClock / HZ_TO_KHZ_DIV;
  84. iap_entry(command, result);
  85. return translate_iap_status(result[0]);
  86. }
  87. status_t FLASHIAP_BlankCheckSector(uint32_t startSector, uint32_t endSector)
  88. {
  89. uint32_t command[5], result[4];
  90. command[0] = kIapCmd_FLASHIAP_BlankCheckSector;
  91. command[1] = startSector;
  92. command[2] = endSector;
  93. iap_entry(command, result);
  94. return translate_iap_status(result[0]);
  95. }
  96. status_t FLASHIAP_Compare(uint32_t dstAddr, uint32_t *srcAddr, uint32_t numOfBytes)
  97. {
  98. uint32_t command[5], result[4];
  99. command[0] = kIapCmd_FLASHIAP_Compare;
  100. command[1] = dstAddr;
  101. command[2] = (uint32_t)srcAddr;
  102. command[3] = numOfBytes;
  103. iap_entry(command, result);
  104. return translate_iap_status(result[0]);
  105. }