1 /**
2  * @file    sdhc.h
3  * @brief   Secure Digital High Capacity (SDHC) function prototypes and data types.
4  */
5 
6 /******************************************************************************
7  *
8  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9  * Analog Devices, Inc.),
10  * Copyright (C) 2023-2024 Analog Devices, Inc.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  ******************************************************************************/
25 
26 /* Define to prevent redundant inclusion */
27 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_SDHC_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_SDHC_H_
29 
30 /* **** Includes **** */
31 #include "mxc_device.h"
32 #include "sdhc_regs.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /**
39  * @defgroup sdhc SDHC
40  * @ingroup periphlibs
41  * @{
42  */
43 
44 /* **** Definitions **** */
45 #define MXC_SDHC_Bus_Voltage_1_8 5
46 #define MXC_SDHC_Bus_Voltage_3_0 6
47 #define MXC_SDHC_Bus_Voltage_3_3 7
48 
49 #define MXC_SDHC_DIRECTION_CFG 0
50 #define MXC_SDHC_DIRECTION_READ 1
51 #define MXC_SDHC_DIRECTION_WRITE 2
52 
53 /**
54  * @brief   Used to configure voltage and clock for sdhc interface
55  *
56  */
57 typedef struct {
58     unsigned int bus_voltage; /**< Use constants above for 1.8V, 3.0V, 3.3V. */
59     unsigned int block_gap; /**< Set block gap register default is 0       */
60     unsigned int clk_div; /**< Divider for SD clock                      */
61 } mxc_sdhc_cfg_t;
62 
63 /**
64  * @brief   Callback function type used in asynchromous SDHC communications requests.
65  * @details The function declaration for the SDHC callback is:
66  * @code
67  *      void callback(int error_code);
68  * @endcode
69  * |        |                                            |
70  * | -----: | :----------------------------------------- |
71  * | \p error_code | An error code if the active transaction had a failure or #E_NO_ERROR if successful. |
72  */
73 typedef void (*mxc_sdhc_callback_fn)(int error_code);
74 
75 /**
76  * @brief   Used to configure sdhc interface
77  *
78  */
79 typedef struct {
80     uint32_t
81         sdma; /**< SDMA register for read or write transaction                                                            */
82     uint32_t
83         block_size; /**< Size of transfer block in bytes                                                                        */
84     uint32_t
85         block_count; /**< Number of blocks to transfer                                                                           */
86     uint32_t
87         arg_1; /**< Argument 1 holds the arguments for the commands sent to the card                                       */
88     unsigned int
89         dma; /**< DMA enable bit                                                                                         */
90     unsigned int
91         direction; /**< Direction of transfer                                                                                  */
92     uint32_t
93         command; /**< Command to be issued on bus (CMD0, CMD1, ...)                                                          */
94     uint32_t
95         host_control_1; /**< Host control register 1 to be assigned before command is issued                                        */
96     mxc_sdhc_callback_fn
97         callback; /**< Function pointer to completion callback function, NULL if not desired                                  */
98 } mxc_sdhc_cmd_cfg_t;
99 
100 /* **** Function Prototypes **** */
101 
102 /**
103  * @brief      Initialize and enable SDHC module.
104  * @param      cfg    Pointer to SDHC configuration.
105  *
106  * @returns    #E_NO_ERROR SDHC initialized successfully, @ref MXC_Error_Codes "error" if
107  *             unsuccessful.
108  */
109 int MXC_SDHC_Init(const mxc_sdhc_cfg_t *cfg);
110 
111 /**
112  * @brief      Enable SDHC Bus Power
113  */
114 void MXC_SDHC_PowerUp(void);
115 
116 /**
117  * @brief      Disable SDHC Bus Power
118  */
119 void MXC_SDHC_PowerDown(void);
120 
121 /**
122  * @brief      Shutdown SDHC module.
123  *
124  * @returns    #E_NO_ERROR SDHC shutdown successfully, @ref MXC_Error_Codes "error" if
125  *             unsuccessful.
126  */
127 int MXC_SDHC_Shutdown(void);
128 
129 /**
130  * @brief      Set clock divider
131  * @param      clk_div  Divider setting
132  */
133 void MXC_SDHC_Set_Clock_Config(unsigned int clk_div);
134 
135 /**
136  * @brief      Get clock divider
137  * @return     Clock divider setting
138  * s
139  * @returns    #E_NO_ERROR SDHC shutdown successfully, @ref MXC_Error_Codes "error" if
140  *             unsuccessful.
141  */
142 unsigned int MXC_SDHC_Get_Clock_Config(void);
143 
144 /**
145  * @brief      Get the input clock frequency for the SDHC peripheral.
146  * @return     Input clock frequency in Hz
147  */
148 unsigned int MXC_SDHC_Get_Input_Clock_Freq(void);
149 
150 /**
151  * @brief      Send Command, <em>blocking</em>.
152  *
153  * @param      sd_cmd_cfg  The sd command configuration
154  *
155  * @return     E_NO_ERROR, @ref MXC_Error_Codes "error" if
156  *             unsuccessful.
157  */
158 int MXC_SDHC_SendCommand(mxc_sdhc_cmd_cfg_t *sd_cmd_cfg);
159 /**
160  * @brief      Send SDHC command <em>Async</em>.
161  *
162  * @param      sd_cmd_cfg  The sd command configuration
163  *
164  * @return     E_NO_ERROR, @ref MXC_Error_Codes "error" if
165  *             unsuccessful.
166  */
167 int MXC_SDHC_SendCommandAsync(mxc_sdhc_cmd_cfg_t *sd_cmd_cfg);
168 
169 /**
170  * @brief      SDHC interrupt handler.
171  * @details    This function should be called by the application from the
172  *             interrupt handler if SDHC interrupts are enabled. Alternately,
173  *             this function can be periodically called by the application if
174  *             SDHC interrupts are disabled. Only necessary to call this when
175  *             using asynchronous functions.
176  *
177  */
178 void MXC_SDHC_Handler(void);
179 
180 /**
181  * @brief      See if card is inserted
182  *
183  * @return     1 is card inserted, 0 is card not inserted
184  */
185 int MXC_SDHC_Card_Inserted(void);
186 
187 /**
188  * @brief      Clear interrupt flags.
189  *
190  * @param      mask  Mask of the SDHC interrupts to clear, see
191  *                   @ref SDHC_INTFL Register.
192  */
193 void MXC_SDHC_ClearFlags(uint32_t mask);
194 
195 /**
196  * @brief      Get interrupt flags.
197  *
198  *
199  * @return     Mask of active flags.
200  */
201 unsigned MXC_SDHC_GetFlags(void);
202 
203 /**
204  * @brief      Resets SDHC back to default settings
205  *
206  */
207 void MXC_SDHC_Reset(void);
208 
209 /**
210  * @brief      Abort any command or data transaction in progress on controller
211  *
212  */
213 void MXC_SDHC_Reset_CMD_DAT(void);
214 
215 /**
216  * @brief      Check if Card is busy
217  *
218  * @return     1 card busy , 0 card is not busy
219  */
220 int MXC_SDHC_Card_Busy(void);
221 
222 /**
223  * @brief      Retrieve host control 1 register
224  *
225  * @return     host control register
226  */
227 unsigned int MXC_SDHC_Get_Host_Cn_1(void);
228 
229 /**
230  * @brief      Read a 32-bit command response
231  * @details    This function may be used to read response
232  *             types R1, R1a, R3, R4, R5, R5b, and R6
233  * @return     Command response
234  */
235 uint32_t MXC_SDHC_Get_Response32(void);
236 
237 /**
238  * @brief      Read a 32-bit command response for Auto commands
239  * @details    This function may be used to read response
240  *             types R1b and R1 after an Auto CMD12 or Auto CMD23
241  * @return     Command response
242  */
243 uint32_t MXC_SDHC_Get_Response32_Auto(void);
244 
245 /**
246  * @brief      Read a 128-bit command response
247  * @param      response  Pointer to storage for response (16 bytes)
248  * @details    This function may be used to read response
249  *             type R2 (CID or CSD)
250  */
251 void MXC_SDHC_Get_Response128(unsigned char *response);
252 
253 /**@} end of group sdhc */
254 
255 #ifdef __cplusplus
256 }
257 #endif
258 
259 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_SDHC_H_
260