hal_mmc_spi.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_mmc_spi.h
  15. * @brief MMC over SPI driver header.
  16. *
  17. * @addtogroup MMC_SPI
  18. * @{
  19. */
  20. #ifndef HAL_MMC_SPI_H
  21. #define HAL_MMC_SPI_H
  22. #if (HAL_USE_MMC_SPI == TRUE) || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver constants. */
  25. /*===========================================================================*/
  26. #define MMC_CMD0_RETRY 10U
  27. #define MMC_CMD1_RETRY 100U
  28. #define MMC_ACMD41_RETRY 100U
  29. #define MMC_WAIT_DATA 10000U
  30. /*===========================================================================*/
  31. /* Driver pre-compile time settings. */
  32. /*===========================================================================*/
  33. /**
  34. * @name MMC_SPI configuration options
  35. * @{
  36. */
  37. /**
  38. * @brief Delays insertions.
  39. * @details If enabled this options inserts delays into the MMC waiting
  40. * routines releasing some extra CPU time for the threads with
  41. * lower priority, this may slow down the driver a bit however.
  42. * This option is recommended also if the SPI driver does not
  43. * use a DMA channel and heavily loads the CPU.
  44. */
  45. #if !defined(MMC_NICE_WAITING) || defined(__DOXYGEN__)
  46. #define MMC_NICE_WAITING TRUE
  47. #endif
  48. /** @} */
  49. /*===========================================================================*/
  50. /* Derived constants and error checks. */
  51. /*===========================================================================*/
  52. #if (HAL_USE_SPI == FALSE) || (SPI_USE_WAIT == FALSE)
  53. #error "MMC_SPI driver requires HAL_USE_SPI and SPI_USE_WAIT"
  54. #endif
  55. /*===========================================================================*/
  56. /* Driver data structures and types. */
  57. /*===========================================================================*/
  58. /**
  59. * @brief MMC/SD over SPI driver configuration structure.
  60. */
  61. typedef struct {
  62. /**
  63. * @brief SPI driver associated to this MMC driver.
  64. */
  65. SPIDriver *spip;
  66. /**
  67. * @brief SPI low speed configuration used during initialization.
  68. */
  69. const SPIConfig *lscfg;
  70. /**
  71. * @brief SPI high speed configuration used during transfers.
  72. */
  73. const SPIConfig *hscfg;
  74. } MMCConfig;
  75. /**
  76. * @brief @p MMCDriver specific methods.
  77. */
  78. #define _mmc_driver_methods \
  79. _mmcsd_block_device_methods
  80. /**
  81. * @extends MMCSDBlockDeviceVMT
  82. *
  83. * @brief @p MMCDriver virtual methods table.
  84. */
  85. struct MMCDriverVMT {
  86. _mmc_driver_methods
  87. };
  88. /**
  89. * @extends MMCSDBlockDevice
  90. *
  91. * @brief Structure representing a MMC/SD over SPI driver.
  92. */
  93. typedef struct {
  94. /**
  95. * @brief Virtual Methods Table.
  96. */
  97. const struct MMCDriverVMT *vmt;
  98. _mmcsd_block_device_data
  99. /**
  100. * @brief Current configuration data.
  101. */
  102. const MMCConfig *config;
  103. /***
  104. * @brief Addresses use blocks instead of bytes.
  105. */
  106. bool block_addresses;
  107. } MMCDriver;
  108. /*===========================================================================*/
  109. /* Driver macros. */
  110. /*===========================================================================*/
  111. /**
  112. * @name Macro Functions
  113. * @{
  114. */
  115. /**
  116. * @brief Returns the card insertion status.
  117. * @note This macro wraps a low level function named
  118. * @p sdc_lld_is_card_inserted(), this function must be
  119. * provided by the application because it is not part of the
  120. * SDC driver.
  121. *
  122. * @param[in] mmcp pointer to the @p MMCDriver object
  123. * @return The card state.
  124. * @retval false card not inserted.
  125. * @retval true card inserted.
  126. *
  127. * @api
  128. */
  129. #define mmcIsCardInserted(mmcp) mmc_lld_is_card_inserted(mmcp)
  130. /**
  131. * @brief Returns the write protect status.
  132. *
  133. * @param[in] mmcp pointer to the @p MMCDriver object
  134. * @return The card state.
  135. * @retval false card not inserted.
  136. * @retval true card inserted.
  137. *
  138. * @api
  139. */
  140. #define mmcIsWriteProtected(mmcp) mmc_lld_is_write_protected(mmcp)
  141. /** @} */
  142. /*===========================================================================*/
  143. /* External declarations. */
  144. /*===========================================================================*/
  145. #ifdef __cplusplus
  146. extern "C" {
  147. #endif
  148. void mmcInit(void);
  149. void mmcObjectInit(MMCDriver *mmcp);
  150. void mmcStart(MMCDriver *mmcp, const MMCConfig *config);
  151. void mmcStop(MMCDriver *mmcp);
  152. bool mmcConnect(MMCDriver *mmcp);
  153. bool mmcDisconnect(MMCDriver *mmcp);
  154. bool mmcStartSequentialRead(MMCDriver *mmcp, uint32_t startblk);
  155. bool mmcSequentialRead(MMCDriver *mmcp, uint8_t *buffer);
  156. bool mmcStopSequentialRead(MMCDriver *mmcp);
  157. bool mmcStartSequentialWrite(MMCDriver *mmcp, uint32_t startblk);
  158. bool mmcSequentialWrite(MMCDriver *mmcp, const uint8_t *buffer);
  159. bool mmcStopSequentialWrite(MMCDriver *mmcp);
  160. bool mmcSync(MMCDriver *mmcp);
  161. bool mmcGetInfo(MMCDriver *mmcp, BlockDeviceInfo *bdip);
  162. bool mmcErase(MMCDriver *mmcp, uint32_t startblk, uint32_t endblk);
  163. bool mmc_lld_is_card_inserted(MMCDriver *mmcp);
  164. bool mmc_lld_is_write_protected(MMCDriver *mmcp);
  165. #ifdef __cplusplus
  166. }
  167. #endif
  168. #endif /* HAL_USE_MMC_SPI == TRUE */
  169. #endif /* HAL_MMC_SPI_H */
  170. /** @} */