ChibiOS/HAL  6.1.0
hal_mmc_spi.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 /**
18  * @file hal_mmc_spi.h
19  * @brief MMC over SPI driver header.
20  *
21  * @addtogroup MMC_SPI
22  * @{
23  */
24 
25 #ifndef HAL_MMC_SPI_H
26 #define HAL_MMC_SPI_H
27 
28 #if (HAL_USE_MMC_SPI == TRUE) || defined(__DOXYGEN__)
29 
30 /*===========================================================================*/
31 /* Driver constants. */
32 /*===========================================================================*/
33 
34 #define MMC_CMD0_RETRY 10U
35 #define MMC_CMD1_RETRY 100U
36 #define MMC_ACMD41_RETRY 100U
37 #define MMC_WAIT_DATA 10000U
38 
39 /*===========================================================================*/
40 /* Driver pre-compile time settings. */
41 /*===========================================================================*/
42 
43 /**
44  * @name MMC_SPI configuration options
45  * @{
46  */
47 /**
48  * @brief Delays insertions.
49  * @details If enabled this options inserts delays into the MMC waiting
50  * routines releasing some extra CPU time for the threads with
51  * lower priority, this may slow down the driver a bit however.
52  * This option is recommended also if the SPI driver does not
53  * use a DMA channel and heavily loads the CPU.
54  */
55 #if !defined(MMC_NICE_WAITING) || defined(__DOXYGEN__)
56 #define MMC_NICE_WAITING TRUE
57 #endif
58 /** @} */
59 
60 /*===========================================================================*/
61 /* Derived constants and error checks. */
62 /*===========================================================================*/
63 
64 #if (HAL_USE_SPI == FALSE) || (SPI_USE_WAIT == FALSE)
65 #error "MMC_SPI driver requires HAL_USE_SPI and SPI_USE_WAIT"
66 #endif
67 
68 /*===========================================================================*/
69 /* Driver data structures and types. */
70 /*===========================================================================*/
71 
72 /**
73  * @brief MMC/SD over SPI driver configuration structure.
74  */
75 typedef struct {
76  /**
77  * @brief SPI driver associated to this MMC driver.
78  */
80  /**
81  * @brief SPI low speed configuration used during initialization.
82  */
83  const SPIConfig *lscfg;
84  /**
85  * @brief SPI high speed configuration used during transfers.
86  */
87  const SPIConfig *hscfg;
88 } MMCConfig;
89 
90 /**
91  * @brief @p MMCDriver specific methods.
92  */
93 #define _mmc_driver_methods \
94  _mmcsd_block_device_methods
95 
96 /**
97  * @extends MMCSDBlockDeviceVMT
98  *
99  * @brief @p MMCDriver virtual methods table.
100  */
101 struct MMCDriverVMT {
103 };
104 
105 /**
106  * @extends MMCSDBlockDevice
107  *
108  * @brief Structure representing a MMC/SD over SPI driver.
109  */
110 typedef struct {
111  /**
112  * @brief Virtual Methods Table.
113  */
114  const struct MMCDriverVMT *vmt;
116  /**
117  * @brief Current configuration data.
118  */
120  /***
121  * @brief Addresses use blocks instead of bytes.
122  */
123  bool block_addresses;
124 } MMCDriver;
125 
126 /*===========================================================================*/
127 /* Driver macros. */
128 /*===========================================================================*/
129 
130 /**
131  * @name Macro Functions
132  * @{
133  */
134 /**
135  * @brief Returns the card insertion status.
136  * @note This macro wraps a low level function named
137  * @p sdc_lld_is_card_inserted(), this function must be
138  * provided by the application because it is not part of the
139  * SDC driver.
140  *
141  * @param[in] mmcp pointer to the @p MMCDriver object
142  * @return The card state.
143  * @retval false card not inserted.
144  * @retval true card inserted.
145  *
146  * @api
147  */
148 #define mmcIsCardInserted(mmcp) mmc_lld_is_card_inserted(mmcp)
149 
150 /**
151  * @brief Returns the write protect status.
152  *
153  * @param[in] mmcp pointer to the @p MMCDriver object
154  * @return The card state.
155  * @retval false card not inserted.
156  * @retval true card inserted.
157  *
158  * @api
159  */
160 #define mmcIsWriteProtected(mmcp) mmc_lld_is_write_protected(mmcp)
161 /** @} */
162 
163 /*===========================================================================*/
164 /* External declarations. */
165 /*===========================================================================*/
166 
167 #ifdef __cplusplus
168 extern "C" {
169 #endif
170  void mmcInit(void);
171  void mmcObjectInit(MMCDriver *mmcp);
172  void mmcStart(MMCDriver *mmcp, const MMCConfig *config);
173  void mmcStop(MMCDriver *mmcp);
174  bool mmcConnect(MMCDriver *mmcp);
175  bool mmcDisconnect(MMCDriver *mmcp);
176  bool mmcStartSequentialRead(MMCDriver *mmcp, uint32_t startblk);
177  bool mmcSequentialRead(MMCDriver *mmcp, uint8_t *buffer);
178  bool mmcStopSequentialRead(MMCDriver *mmcp);
179  bool mmcStartSequentialWrite(MMCDriver *mmcp, uint32_t startblk);
180  bool mmcSequentialWrite(MMCDriver *mmcp, const uint8_t *buffer);
181  bool mmcStopSequentialWrite(MMCDriver *mmcp);
182  bool mmcSync(MMCDriver *mmcp);
183  bool mmcGetInfo(MMCDriver *mmcp, BlockDeviceInfo *bdip);
184  bool mmcErase(MMCDriver *mmcp, uint32_t startblk, uint32_t endblk);
185  bool mmc_lld_is_card_inserted(MMCDriver *mmcp);
186  bool mmc_lld_is_write_protected(MMCDriver *mmcp);
187 #ifdef __cplusplus
188 }
189 #endif
190 
191 #endif /* HAL_USE_MMC_SPI == TRUE */
192 
193 #endif /* HAL_MMC_SPI_H */
194 
195 /** @} */
Structure representing a MMC/SD over SPI driver.
Definition: hal_mmc_spi.h:110
#define _mmc_driver_methods
MMCDriver specific methods.
Definition: hal_mmc_spi.h:93
bool mmcStartSequentialRead(MMCDriver *mmcp, uint32_t startblk)
Starts a sequential read.
Definition: hal_mmc_spi.c:612
bool mmcStopSequentialWrite(MMCDriver *mmcp)
Stops a sequential write gracefully.
Definition: hal_mmc_spi.c:797
#define _mmcsd_block_device_data
MMCSDBlockDevice specific data.
Definition: hal_mmcsd.h:258
bool mmcConnect(MMCDriver *mmcp)
Performs the initialization procedure on the inserted card.
Definition: hal_mmc_spi.c:455
void mmcStart(MMCDriver *mmcp, const MMCConfig *config)
Configures and activates the MMC peripheral.
Definition: hal_mmc_spi.c:410
MMC/SD over SPI driver configuration structure.
Definition: hal_mmc_spi.h:75
bool mmcStopSequentialRead(MMCDriver *mmcp)
Stops a sequential read gracefully.
Definition: hal_mmc_spi.c:688
void mmcStop(MMCDriver *mmcp)
Disables the MMC peripheral.
Definition: hal_mmc_spi.c:427
const SPIConfig * lscfg
SPI low speed configuration used during initialization.
Definition: hal_mmc_spi.h:83
bool mmcGetInfo(MMCDriver *mmcp, BlockDeviceInfo *bdip)
Returns the media info.
Definition: hal_mmc_spi.c:856
bool mmcErase(MMCDriver *mmcp, uint32_t startblk, uint32_t endblk)
Erases blocks.
Definition: hal_mmc_spi.c:883
bool mmcSync(MMCDriver *mmcp)
Waits for card idle condition.
Definition: hal_mmc_spi.c:825
_mmcsd_block_device_data const MMCConfig * config
Current configuration data.
Definition: hal_mmc_spi.h:119
const struct MMCDriverVMT * vmt
Virtual Methods Table.
Definition: hal_mmc_spi.h:114
bool mmcStartSequentialWrite(MMCDriver *mmcp, uint32_t startblk)
Starts a sequential write.
Definition: hal_mmc_spi.c:722
Block device info.
Definition: hal_ioblock.h:55
void mmcObjectInit(MMCDriver *mmcp)
Initializes an instance.
Definition: hal_mmc_spi.c:394
bool mmcSequentialWrite(MMCDriver *mmcp, const uint8_t *buffer)
Writes a block within a sequential write operation.
Definition: hal_mmc_spi.c:760
Driver configuration structure.
Definition: hal_spi_lld.h:83
SPIDriver * spip
SPI driver associated to this MMC driver.
Definition: hal_mmc_spi.h:79
MMCDriver virtual methods table.
Definition: hal_mmc_spi.h:101
void mmcInit(void)
MMC over SPI driver initialization.
Definition: hal_mmc_spi.c:383
bool mmcDisconnect(MMCDriver *mmcp)
Brings the driver in a state safe for card removal.
Definition: hal_mmc_spi.c:577
const SPIConfig * hscfg
SPI high speed configuration used during transfers.
Definition: hal_mmc_spi.h:87
Structure representing an SPI driver.
Definition: hal_spi_lld.h:128
bool mmcSequentialRead(MMCDriver *mmcp, uint8_t *buffer)
Reads a block within a sequential read operation.
Definition: hal_mmc_spi.c:652