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_MAX32570_SDHC_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32570_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  */
139 unsigned int MXC_SDHC_Get_Clock_Config(void);
140 
141 /**
142  * @brief      Get the input clock frequency for the SDHC peripheral.
143  * @return     Input clock frequency in Hz
144  */
145 unsigned int MXC_SDHC_Get_Input_Clock_Freq(void);
146 
147 /**
148  * @brief      Send Command, <em>blocking</em>.
149  *
150  * @param      sd_cmd_cfg  The sd command configuration
151  *
152  * @return     E_NO_ERROR, @ref MXC_Error_Codes "error" if
153  *             unsuccessful.
154  */
155 int MXC_SDHC_SendCommand(mxc_sdhc_cmd_cfg_t *sd_cmd_cfg);
156 /**
157  * @brief      Send SDHC command <em>Async</em>.
158  *
159  * @param      sd_cmd_cfg  The sd command configuration
160  *
161  * @return     E_NO_ERROR, @ref MXC_Error_Codes "error" if
162  *             unsuccessful.
163  */
164 int MXC_SDHC_SendCommandAsync(mxc_sdhc_cmd_cfg_t *sd_cmd_cfg);
165 
166 /**
167  * @brief      SDHC interrupt handler.
168  * @details    This function should be called by the application from the
169  *             interrupt handler if SDHC interrupts are enabled. Alternately,
170  *             this function can be periodically called by the application if
171  *             SDHC interrupts are disabled. Only necessary to call this when
172  *             using asynchronous functions.
173  *
174  */
175 void MXC_SDHC_Handler(void);
176 
177 /**
178  * @brief      See if card is inserted
179  *
180  * @return     1 is card inserted, 0 is card not inserted
181  */
182 int MXC_SDHC_Card_Inserted(void);
183 
184 /**
185  * @brief      Clear interrupt flags.
186  *
187  * @param      mask  Mask of the SDHC interrupts to clear, see
188  *                   @ref SDHC_INTFL Register.
189  */
190 void MXC_SDHC_ClearFlags(uint32_t mask);
191 
192 /**
193  * @brief      Get interrupt flags.
194  *
195  *
196  * @return     Mask of active flags.
197  */
198 unsigned MXC_SDHC_GetFlags(void);
199 
200 /**
201  * @brief      Resets SDHC back to default settings
202  *
203  */
204 void MXC_SDHC_Reset(void);
205 
206 /**
207  * @brief      Abort any command or data transaction in progress on controller
208  *
209  */
210 void MXC_SDHC_Reset_CMD_DAT(void);
211 
212 /**
213  * @brief      Check if Card is busy
214  *
215  * @return     1 card busy , 0 card is not busy
216  */
217 int MXC_SDHC_Card_Busy(void);
218 
219 /**
220  * @brief      Retrieve host control 1 register
221  *
222  * @return     host control register
223  */
224 unsigned int MXC_SDHC_Get_Host_Cn_1(void);
225 
226 /**
227  * @brief      Read a 32-bit command response
228  * @details    This function may be used to read response
229  *             types R1, R1a, R3, R4, R5, R5b, and R6
230  * @return     Command response
231  */
232 uint32_t MXC_SDHC_Get_Response32(void);
233 
234 /**
235  * @brief      Read a 32-bit command response for Auto commands
236  * @details    This function may be used to read response
237  *             types R1b and R1 after an Auto CMD12 or Auto CMD23
238  * @return     Command response
239  */
240 uint32_t MXC_SDHC_Get_Response32_Auto(void);
241 
242 /**
243  * @brief      Read a 128-bit command response
244  * @param      response  Pointer to storage for response (16 bytes)
245  * @details    This function may be used to read response
246  *             type R2 (CID or CSD)
247  */
248 void MXC_SDHC_Get_Response128(unsigned char *response);
249 
250 /**@} end of group sdhc */
251 
252 #ifdef __cplusplus
253 }
254 #endif
255 
256 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32570_SDHC_H_
257