1 /*
2 * Copyright (c) 2020 - 2024 Renesas Electronics Corporation and/or its affiliates
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6 
7 #ifndef R_SDHI_H
8 #define R_SDHI_H
9 
10 /***********************************************************************************************************************
11  * Includes
12  **********************************************************************************************************************/
13 #include "bsp_api.h"
14 #include "r_sdhi_cfg.h"
15 #include "r_sdmmc_api.h"
16 
17 /* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */
18 FSP_HEADER
19 
20 /*******************************************************************************************************************//**
21  * @addtogroup SDHI
22  **********************************************************************************************************************/
23 
24 /***********************************************************************************************************************
25  * Macro definitions
26  **********************************************************************************************************************/
27 
28 #define SDHI_MAX_BLOCK_SIZE    (512U)
29 
30 /***********************************************************************************************************************
31  * Typedef definitions
32  **********************************************************************************************************************/
33 
34 /* Private enumeration used in sdhi_instance_ctrl_t. */
35 typedef enum e_sdhi_transfer_dir
36 {
37     SDHI_TRANSFER_DIR_NONE,
38     SDHI_TRANSFER_DIR_READ,
39     SDHI_TRANSFER_DIR_WRITE
40 } sdhi_transfer_dir_t;
41 
42 /* Private structure used in sdhi_instance_ctrl_t. */
43 typedef union
44 {
45     uint32_t word;
46 
47     struct s_sdhi_event_type
48     {
49         uint32_t response_end       : 1; // < response end detected
50         uint32_t reserved_1         : 1;
51         uint32_t access_end         : 1; // < access end detected
52         uint32_t card_removed       : 1; // < card removal detected by cd pin
53         uint32_t card_inserted      : 1; // < card insertion detected by cd pin
54         uint32_t reserved_5         : 3;
55         uint32_t card_dat3_removed  : 1; // < card removal detected by dat3 pin
56         uint32_t card_dat3_inserted : 1; // < card inserion detected by dat3 pin
57         uint32_t reservered_10      : 6;
58         uint32_t cmd_err            : 1; // < command error
59         uint32_t crc_err            : 1; // < crc error
60         uint32_t end_err            : 1; // < end bit error
61         uint32_t dto                : 1; // < data timeout
62         uint32_t ilw                : 1; // < illegal write address
63         uint32_t ilr                : 1; // < illegal read address
64         uint32_t rspt               : 1; // < response timeout
65         uint32_t event_error        : 1; // < all error flags combined
66         uint32_t bre                : 1; // < buffer read enable
67         uint32_t bwe                : 1; // < buffer write enable
68         uint32_t reserved_26        : 5;
69         uint32_t ila_err            : 1; // < illegal access
70     } bit;
71 } sdhi_event_t;
72 
73 /** SDMMC instance control block.  This is private to the FSP and should not be used or modified by the application. */
74 typedef struct st_sdmmc_instance_ctrl
75 {
76     uint32_t              open;
77     sdmmc_cfg_t const   * p_cfg;
78     sdmmc_device_t        device;
79     bool                  sector_addressing;
80     bool                  transfer_in_progress;
81     bool                  initialized;
82     R_SDHI0_Type        * p_reg;
83     volatile sdhi_event_t sdhi_event;
84     sdhi_transfer_dir_t   transfer_dir;
85     uint8_t             * p_transfer_data;
86     uint32_t              transfer_blocks_total;
87     uint32_t              transfer_block_current;
88     uint32_t              transfer_block_size;
89     uint32_t              aligned_buff[SDHI_MAX_BLOCK_SIZE / sizeof(uint32_t)];
90 
91     void (* p_callback)(sdmmc_callback_args_t *); // Pointer to callback
92     sdmmc_callback_args_t * p_callback_memory;    // Pointer to optional callback argument memory
93     void const            * p_context;            // Pointer to context to be passed into callback function
94 } sdhi_instance_ctrl_t;
95 
96 /**********************************************************************************************************************
97  * Exported global variables
98  **********************************************************************************************************************/
99 
100 /** @cond INC_HEADER_DEFS_SEC */
101 /** Filled in Interface API structure for this Instance. */
102 extern const sdmmc_api_t g_sdmmc_on_sdhi;
103 
104 /** @endcond */
105 
106 /**********************************************************************************************************************
107  * Function Prototypes
108  **********************************************************************************************************************/
109 
110 fsp_err_t R_SDHI_Open(sdmmc_ctrl_t * const p_api_ctrl, sdmmc_cfg_t const * const p_cfg);
111 fsp_err_t R_SDHI_MediaInit(sdmmc_ctrl_t * const p_api_ctrl, sdmmc_device_t * const p_device);
112 fsp_err_t R_SDHI_Read(sdmmc_ctrl_t * const p_api_ctrl,
113                       uint8_t * const      p_dest,
114                       uint32_t const       start_sector,
115                       uint32_t const       sector_count);
116 fsp_err_t R_SDHI_Write(sdmmc_ctrl_t * const  p_api_ctrl,
117                        uint8_t const * const p_source,
118                        uint32_t const        start_sector,
119                        uint32_t const        sector_count);
120 fsp_err_t R_SDHI_ReadIo(sdmmc_ctrl_t * const p_api_ctrl,
121                         uint8_t * const      p_data,
122                         uint32_t const       function,
123                         uint32_t const       address);
124 fsp_err_t R_SDHI_WriteIo(sdmmc_ctrl_t * const        p_api_ctrl,
125                          uint8_t * const             p_data,
126                          uint32_t const              function,
127                          uint32_t const              address,
128                          sdmmc_io_write_mode_t const read_after_write);
129 fsp_err_t R_SDHI_ReadIoExt(sdmmc_ctrl_t * const     p_api_ctrl,
130                            uint8_t * const          p_dest,
131                            uint32_t const           function,
132                            uint32_t const           address,
133                            uint32_t * const         count,
134                            sdmmc_io_transfer_mode_t transfer_mode,
135                            sdmmc_io_address_mode_t  address_mode);
136 fsp_err_t R_SDHI_WriteIoExt(sdmmc_ctrl_t * const     p_api_ctrl,
137                             uint8_t const * const    p_source,
138                             uint32_t const           function,
139                             uint32_t const           address,
140                             uint32_t const           count,
141                             sdmmc_io_transfer_mode_t transfer_mode,
142                             sdmmc_io_address_mode_t  address_mode);
143 fsp_err_t R_SDHI_IoIntEnable(sdmmc_ctrl_t * const p_api_ctrl, bool enable);
144 fsp_err_t R_SDHI_StatusGet(sdmmc_ctrl_t * const p_api_ctrl, sdmmc_status_t * const p_status);
145 fsp_err_t R_SDHI_Erase(sdmmc_ctrl_t * const p_api_ctrl, uint32_t const start_sector, uint32_t const sector_count);
146 fsp_err_t R_SDHI_CallbackSet(sdmmc_ctrl_t * const          p_api_ctrl,
147                              void (                      * p_callback)(sdmmc_callback_args_t *),
148                              void const * const            p_context,
149                              sdmmc_callback_args_t * const p_callback_memory);
150 fsp_err_t R_SDHI_Close(sdmmc_ctrl_t * const p_api_ctrl);
151 
152 /* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */
153 FSP_FOOTER
154 
155 #endif                                 // R_SDHI_H
156 
157 /*******************************************************************************************************************//**
158  * @} (end addtogroup SDHI)
159  **********************************************************************************************************************/
160