hal_flash.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file hal_flash.c
  15. * @brief Generic flash driver class code.
  16. *
  17. * @addtogroup HAL_FLASH
  18. * @{
  19. */
  20. #include "hal.h"
  21. #include "hal_flash.h"
  22. /*===========================================================================*/
  23. /* Driver local definitions. */
  24. /*===========================================================================*/
  25. /*===========================================================================*/
  26. /* Driver exported variables. */
  27. /*===========================================================================*/
  28. /*===========================================================================*/
  29. /* Driver local variables and types. */
  30. /*===========================================================================*/
  31. /*===========================================================================*/
  32. /* Driver local functions. */
  33. /*===========================================================================*/
  34. /*===========================================================================*/
  35. /* Driver exported functions. */
  36. /*===========================================================================*/
  37. /**
  38. * @brief Waits until the current erase operation is finished.
  39. *
  40. * @param[in] devp pointer to a @p BaseFlash object
  41. *
  42. * @return An error code.
  43. * @retval FLASH_NO_ERROR if there is no erase operation in progress.
  44. * @retval FLASH_ERROR_ERASE if the erase operation failed.
  45. * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
  46. */
  47. flash_error_t flashWaitErase(BaseFlash *devp) {
  48. while (true) {
  49. flash_error_t err;
  50. uint32_t msec;
  51. /* Checking operation state.*/
  52. err = flashQueryErase(devp, &msec);
  53. if (err != FLASH_BUSY_ERASING) {
  54. return err;
  55. }
  56. /* Interval because nice waiting.*/
  57. osalThreadSleepMilliseconds(msec);
  58. }
  59. }
  60. /**
  61. * @brief Returns the offset of a sector.
  62. *
  63. * @param[in] devp pointer to a @p BaseFlash object
  64. * @param[in] sector flash sector number
  65. *
  66. * @return the offset of the sector
  67. */
  68. flash_offset_t flashGetSectorOffset(BaseFlash *devp,
  69. flash_sector_t sector) {
  70. flash_offset_t offset;
  71. const flash_descriptor_t *descriptor = flashGetDescriptor(devp);
  72. osalDbgAssert(sector < descriptor->sectors_count, "invalid sector");
  73. if (descriptor->sectors != NULL) {
  74. offset = descriptor->sectors[sector].offset;
  75. }
  76. else {
  77. offset = (flash_offset_t)sector * (flash_offset_t)descriptor->sectors_size;
  78. }
  79. return offset;
  80. }
  81. /**
  82. * @brief Returns the size of a sector.
  83. *
  84. * @param[in] devp pointer to a @p BaseFlash object
  85. * @param[in] sector flash sector number
  86. *
  87. * @return the size of the sector
  88. */
  89. uint32_t flashGetSectorSize(BaseFlash *devp,
  90. flash_sector_t sector) {
  91. uint32_t size;
  92. const flash_descriptor_t *descriptor = flashGetDescriptor(devp);
  93. osalDbgAssert(sector < descriptor->sectors_count, "invalid sector");
  94. if (descriptor->sectors != NULL) {
  95. size = descriptor->sectors[sector].size;
  96. }
  97. else {
  98. size = descriptor->sectors_size;
  99. }
  100. return size;
  101. }
  102. /** @} */