hpm_sdmmc_common.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2021-2024 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_SDMMC_COMMON_H
  8. #define HPM_SDMMC_COMMON_H
  9. /**
  10. *
  11. * @brief HPM SDMMC common APIs
  12. * @defgroup hpm_sdmmc HPM SDMMC stack
  13. * @ingroup hpm_sdmmc_interfaces
  14. * @{
  15. *
  16. */
  17. #include "hpm_common.h"
  18. #include "hpm_sdmmc_card.h"
  19. #include "hpm_sdmmc_host.h"
  20. /******************************************************************
  21. *
  22. * @brief SD/MMC Typical Clock frequency definitions
  23. *
  24. ******************************************************************/
  25. #define SDMMC_CLOCK_400KHZ (400000UL)
  26. #define SD_CLOCK_25MHZ (25000000UL)
  27. #define SD_CLOCK_50MHZ (50000000UL)
  28. #define SD_CLOCK_100MHZ (100000000UL)
  29. #define SD_CLOCK_208MHZ (208000000UL)
  30. #define MMC_CLOCK_26MHz (26000000UL)
  31. #define MMC_CLOCK_52MHz (52000000UL)
  32. #define MMC_CLOCK_DDR52 (52000000UL)
  33. #define MMC_CLOCK_HS200 (200000000UL)
  34. #define MMC_CLOCK_HS400 (200000000UL)
  35. enum {
  36. sdmmc_state_idle = 0,
  37. sdmmc_state_ready = 1,
  38. sdmmc_state_identify = 2,
  39. sdmmc_state_standby = 3,
  40. sdmmc_state_transfer = 4,
  41. sdmmc_state_send_data = 5,
  42. sdmmc_state_receive_data = 6,
  43. sdmmc_state_program = 7,
  44. sdmmc_state_disconnect = 8
  45. };
  46. typedef enum {
  47. sdmmc_operation_voltage_1v8 = 5,
  48. sdmmc_operation_voltage_3v0 = 6,
  49. sdmmc_operation_voltage_3v3 = 7,
  50. } sdmmc_operation_voltage_t;
  51. typedef enum {
  52. sdmmc_resp_none = 0,
  53. sdmmc_resp_r1,
  54. sdmmc_resp_r1b,
  55. sdmmc_resp_r2,
  56. sdmmc_resp_r3,
  57. sdmmc_resp_r4,
  58. sdmmc_resp_r5,
  59. sdmmc_resp_r5b,
  60. sdmmc_resp_r6,
  61. sdmmc_resp_r7,
  62. } sdmmc_resp_type_t;
  63. enum {
  64. status_sdmmc_card_not_support = MAKE_STATUS(status_group_sdmmc, 0),
  65. status_sdmmc_wait_card_insert_timeout = MAKE_STATUS(status_group_sdmmc, 1),
  66. status_sdmmc_no_sd_card_inserted = MAKE_STATUS(status_group_sdmmc, 2),
  67. status_sdmmc_device_init_required = MAKE_STATUS(status_group_sdmmc, 3),
  68. status_sdmmc_wait_busy_timeout = MAKE_STATUS(status_group_sdmmc, 4),
  69. };
  70. #ifndef HPM_SDMMC_MALLOC
  71. #define HPM_SDMMC_MALLOC malloc
  72. #endif
  73. #ifndef HPM_SDMMC_FREE
  74. #define HPM_SDMMC_FREE free
  75. #endif
  76. #ifdef __cplusplus
  77. extern "C" {
  78. #endif
  79. /**
  80. * @brief Switch device to Idle state
  81. * @param [in/out] host SD/MMC Host Context
  82. * @param [in] argument Argument for CMD0
  83. * @return status_success if operation is successful
  84. */
  85. hpm_stat_t sdmmc_go_idle_state(sdmmc_host_t *host, uint32_t argument);
  86. /**
  87. * @brief Switch device to Inactive state
  88. * @param [in/out] host SD/MMC Host Context
  89. * @param [in] relative_addr device relative address
  90. * @return status_success if operation is successful
  91. */
  92. hpm_stat_t sdmmc_go_inactive_state(sdmmc_host_t *host, uint16_t relative_addr);
  93. /**
  94. * @brief Select/De-select the device
  95. * @param [in/out] host SD/MMC Host Context
  96. * @param [in] relative_addr device relative address
  97. * @param [in] is_selected true: select, false: de-select
  98. * @return status_success if operation is successful
  99. */
  100. hpm_stat_t sdmmc_select_card(sdmmc_host_t *host, uint16_t relative_addr, bool is_selected);
  101. /**
  102. * @brief Send Application Command
  103. * @param [in/out] host SD/MMC Host Context
  104. * @param [in] relative_addr device related address
  105. * @return status_success if operation is successful
  106. */
  107. hpm_stat_t sdmmc_send_application_command(sdmmc_host_t *host, uint16_t relative_addr);
  108. /**
  109. * @brief Set block count
  110. * @param [in/out] host SD/MMC Host Context
  111. * @param [in] block_count SD/MMC Block count
  112. * @return status_success if operation is successful
  113. */
  114. hpm_stat_t sdmmc_set_block_count(sdmmc_host_t *host, uint32_t block_count);
  115. /**
  116. * @brief Set Block size
  117. * @param [in/out] host SD/MMC Host Context
  118. * @param [in] block_size SD/MMC Block size
  119. * @return status_success if operation is successful
  120. */
  121. hpm_stat_t sdmmc_set_block_size(sdmmc_host_t *host, uint32_t block_size);
  122. /**
  123. * @brief Enable Auto Tuning mode
  124. * @param [in/out] host SD/MMC Host Context
  125. * @return status_success if operation is successful
  126. */
  127. hpm_stat_t sdmmc_enable_auto_tuning(const sdmmc_host_t *host);
  128. /**
  129. * @brief Extract Fields from raw CSD data
  130. * @param [in] raw_csd Raw CSD data array
  131. * @param [in] end_offset end offset of the specific field (in terms of bit)
  132. * @param [in] start_offset start offset of the specific field (in terms of bit)
  133. * @return Extracted CSD field
  134. */
  135. uint32_t extract_csd_field(const uint32_t *raw_csd, uint8_t end_offset, uint8_t start_offset);
  136. /**
  137. * @brief Get System address
  138. * @param [in] host SD/MMC Host Context
  139. * @param [in] addr memory address
  140. * @return Converted system address
  141. */
  142. extern uint32_t sdmmc_get_sys_addr(const sdmmc_host_t *host, uint32_t addr);
  143. #ifdef __cplusplus
  144. }
  145. #endif
  146. /**
  147. * @}
  148. */
  149. #endif /* HPM_SDMMC_COMMON_H */