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_MAX32650_SDHC_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32650_SDHC_H_
29 
30 /* **** Includes **** */
31 #include "mxc_sys.h"
32 #include "sdhc_regs.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /**
39  * @defgroup sdhc Secure Digital High Capacity(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  * @returns #E_NO_ERROR upon success, @ref MXC_Error_Codes "error" if
134  *             unsuccessful.
135  */
136 void MXC_SDHC_Set_Clock_Config(unsigned int clk_div);
137 
138 /**
139  * @brief   Get clock divider
140  * @return  Clock divider setting
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 /**
161  * @brief      Send SDHC command <em>Async</em>.
162  *
163  * @param      sd_cmd_cfg  The sd command configuration
164  *
165  * @return     E_NO_ERROR, @ref MXC_Error_Codes "error" if
166  *             unsuccessful.
167  */
168 int MXC_SDHC_SendCommandAsync(mxc_sdhc_cmd_cfg_t *sd_cmd_cfg);
169 
170 /**
171  * @brief      SDHC interrupt handler.
172  * @details    This function should be called by the application from the
173  *             interrupt handler if SDHC interrupts are enabled. Alternately,
174  *             this function can be periodically called by the application if
175  *             SDHC interrupts are disabled. Only necessary to call this when
176  *             using asynchronous functions.
177  *
178  */
179 void MXC_SDHC_Handler(void);
180 
181 /**
182  * @brief      See if card is inserted
183  *
184  * @return     1 is card inserted, 0 is card not inserted
185  */
186 int MXC_SDHC_Card_Inserted(void);
187 
188 /**
189  * @brief      Clear interrupt flags.
190  *
191  * @param      mask  Mask of the SDHC interrupts to clear, see
192  *                   @ref SDHC_INT_STAT Register.
193  */
194 void MXC_SDHC_ClearFlags(uint32_t mask);
195 
196 /**
197  * @brief      Get interrupt flags.
198  *
199  *
200  * @return     Mask of active flags.
201  */
202 unsigned MXC_SDHC_GetFlags(void);
203 
204 /**
205  * @brief      Resets SDHC back to default settings
206  *
207  */
208 void MXC_SDHC_Reset(void);
209 
210 /**
211  * @brief      Abort any command or data transaction in progress on controller
212  *
213  */
214 void MXC_SDHC_Reset_CMD_DAT(void);
215 
216 /**
217  * @brief      Check if Card is busy
218  *
219  * @return     1 card busy , 0 card is not busy
220  */
221 int MXC_SDHC_Card_Busy(void);
222 
223 /**
224  * @brief      Retrieve host control 1 register
225  *
226  * @return     host control register
227  */
228 unsigned int MXC_SDHC_Get_Host_Cn_1(void);
229 
230 /**
231  * @brief      Read a 32-bit command response
232  * @details    This function may be used to read response
233  *             types R1, R1a, R3, R4, R5, R5b, and R6
234  * @return     Command response
235  */
236 uint32_t MXC_SDHC_Get_Response32(void);
237 
238 /**
239  * @brief      Read a 32-bit command response for Auto commands
240  * @details    This function may be used to read response
241  *             types R1b and R1 after an Auto CMD12 or Auto CMD23
242  * @return     Command response
243  */
244 uint32_t MXC_SDHC_Get_Response32_Auto(void);
245 
246 /**
247  * @brief      Read a 128-bit command response
248  * @param      response  Pointer to storage for response (16 bytes)
249  * @details    This function may be used to read response
250  *             type R2 (CID or CSD)
251  */
252 void MXC_SDHC_Get_Response128(unsigned char *response);
253 
254 /**@} end of group sdhc */
255 
256 #ifdef __cplusplus
257 }
258 #endif
259 
260 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32650_SDHC_H_
261