logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

flash.h (4350B)


  1. // Copyright 2024 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #include <stdint.h>
  8. #include <stdlib.h>
  9. #include <stdbool.h>
  10. /**
  11. * @brief The status of a flash operation.
  12. */
  13. enum {
  14. FLASH_STATUS_SUCCESS = 0, //< The operation completed successfully.
  15. FLASH_STATUS_ERROR = -1, //< An error occurred during the operation.
  16. FLASH_STATUS_TIMEOUT = -2, //< The operation timed out.
  17. FLASH_STATUS_BAD_ADDRESS = -3, //< The address is out of bounds.
  18. FLASH_STATUS_BUSY = -4, //< The flash is busy.
  19. };
  20. /**
  21. * @brief The status of a flash operation.
  22. */
  23. typedef int16_t flash_status_t;
  24. /**
  25. * @brief Initializes the flash driver.
  26. *
  27. * This function initializes the flash driver and prepares it for use.
  28. * It should be called before any other flash-related functions are used.
  29. */
  30. void flash_init(void);
  31. /**
  32. * @brief Checks if the flash is busy.
  33. *
  34. * This function checks if the flash is currently busy with an operation.
  35. *
  36. * @return FLASH_STATUS_SUCCESS if the flash is not busy, FLASH_STATUS_BUSY if the flash is busy, or FLASH_STATUS_ERROR if an error occurred.
  37. */
  38. flash_status_t flash_is_busy(void);
  39. /**
  40. * @brief Initiates a chip erase operation.
  41. *
  42. * This function does not wait for the flash to become ready.
  43. *
  44. * @return FLASH_STATUS_SUCCESS if the erase command was successfully sent, FLASH_STATUS_TIMEOUT if the flash is busy, or FLASH_STATUS_ERROR if an error occurred.
  45. */
  46. flash_status_t flash_begin_erase_chip(void);
  47. /**
  48. * @brief Waits for the chip erase operation to complete.
  49. *
  50. * This function waits for the chip erase operation to complete.
  51. *
  52. * @return FLASH_STATUS_SUCCESS if the chip erase operation completed successfully, FLASH_STATUS_TIMEOUT if the flash was still busy, or FLASH_STATUS_ERROR if an error occurred.
  53. */
  54. flash_status_t flash_wait_erase_chip(void);
  55. /**
  56. * @brief Erases the entire flash memory chip.
  57. *
  58. * This function initiates an erase operation to erase the entire flash memory chip.
  59. * It waits for the operation to complete.
  60. *
  61. * @return FLASH_STATUS_SUCCESS if the erase was successfully executed, FLASH_STATUS_TIMEOUT if the flash is busy, or FLASH_STATUS_ERROR if an error occurred.
  62. */
  63. flash_status_t flash_erase_chip(void);
  64. /**
  65. * @brief Erases a block of flash memory.
  66. *
  67. * This function initiates an erase operation to erase a block of flash memory.
  68. * It waits for the operation to complete.
  69. *
  70. * @param addr The address of the block to erase.
  71. *
  72. * @return FLASH_STATUS_SUCCESS if the erase was successfully executed, FLASH_STATUS_TIMEOUT if the flash is busy, or FLASH_STATUS_ERROR if an error occurred.
  73. */
  74. flash_status_t flash_erase_block(uint32_t addr);
  75. /**
  76. * @brief Erases a sector of flash memory.
  77. *
  78. * This function initiates an erase operation to erase a sector of flash memory.
  79. * It waits for the operation to complete.
  80. *
  81. * @param addr The address of the sector to erase.
  82. *
  83. * @return FLASH_STATUS_SUCCESS if the erase was successfully executed, FLASH_STATUS_TIMEOUT if the flash is busy, or FLASH_STATUS_ERROR if an error occurred.
  84. */
  85. flash_status_t flash_erase_sector(uint32_t addr);
  86. /**
  87. * @brief Reads a range of flash memory.
  88. *
  89. * This function reads a range of flash memory into a buffer.
  90. *
  91. * @param addr The address of the range to read.
  92. * @param buf A pointer to the buffer to read the range into.
  93. * @param len The length of the range to read.
  94. *
  95. * @return FLASH_STATUS_SUCCESS if the range was successfully read, FLASH_STATUS_BAD_ADDRESS if the address is out of bounds, FLASH_STATUS_TIMEOUT if the flash is busy, or FLASH_STATUS_ERROR if an error occurred.
  96. */
  97. flash_status_t flash_read_range(uint32_t addr, void *buf, size_t len);
  98. /**
  99. * @brief Writes a range of flash memory.
  100. *
  101. * This function writes a range of flash memory from a buffer.
  102. *
  103. * @param addr The address of the range to write.
  104. * @param buf A pointer to the buffer to write to the range.
  105. * @param len The length of the range to write.
  106. *
  107. * @return FLASH_STATUS_SUCCESS if the range was successfully written, FLASH_STATUS_BAD_ADDRESS if the address is out of bounds, FLASH_STATUS_TIMEOUT if the flash is busy, or FLASH_STATUS_ERROR if an error occurred.
  108. */
  109. flash_status_t flash_write_range(uint32_t addr, const void *buf, size_t len);
  110. #ifdef __cplusplus
  111. }
  112. #endif