1 /*
2  * Copyright (c) 2020-2020, BLUETRUM Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef AB32VG1_HAL_SD_H__
8 #define AB32VG1_HAL_SD_H__
9 
10 #include "ab32vg1_hal_def.h"
11 #include "ab32vg1_ll_sdio.h"
12 #include <stdbool.h>
13 
14 struct sd_card_info
15 {
16     uint32_t rca;           /*!< Specifies the Relative Card Address        */
17     uint32_t capacity;      /*!< Specifies the capacity of the card         */
18     uint8_t  abend;         /*!< Specifies if the card is abnormal end      */
19     uint8_t  flag_sdhc;     /*!< Specifies if the card is SDHC card         */
20     uint8_t  type;          /*!< Specifies the card type                    */
21     uint8_t  state;         /*!< Specifies the card state                   */
22     uint8_t  rw_state;      /*!< Specifies the last r/w state of the card   */
23 };
24 typedef struct sd_card_info* sd_card_info_t;
25 
26 struct sd_cfg
27 {
28     uint16_t go_ready_retry;
29     uint8_t  identification_retry;
30     uint8_t  rw_retry;
31     uint8_t  rw_init_retry;
32     uint8_t  stop_retry;
33     uint8_t  rw_need_stop;
34 };
35 
36 struct sd_handle
37 {
38     hal_sfr_t               instance;
39     struct sdio_init        init;
40     struct sd_card_info     sdcard;
41     struct sd_cfg           cfg;
42 };
43 typedef struct sd_handle* sd_handle_t;
44 
45 #define SD0N                (0x00u)
46 
47 // #define CARD_SDSC           (0x00u)
48 // #define CARD_SDHC           (0x01u)
49 // #define CARD_SECURED        (0x03u)
50 
51 enum
52 {
53     CARD_INVAL = 0x00,
54     CARD_V1,
55     CARD_V2,
56     CARD_MMC
57 };
58 
59 enum
60 {
61     HAL_SD_RW_STATE_IDLE = 0x00,
62     HAL_SD_RW_STATE_READ,
63     HAL_SD_RW_STATE_WRITE,
64 };
65 
66 enum
67 {
68     HAL_SD_STATE_RESET = 0x00,
69     HAL_SD_STATE_NEW,
70     HAL_SD_STATE_OK,
71     HAL_SD_STATE_INVAL,
72 };
73 
74 #define SDMMC_CHECK_PATTERM         (0x000001AAu)
75 
76 #define SDMMC0_BASE     ((hal_sfr_t)&SD0CON)
77 
78 /* Initialization functions */
79 hal_error_t hal_sd_init(sd_handle_t hsd);
80 void hal_sd_deinit(uint32_t sdx);
81 void hal_sd_mspinit(sd_handle_t hsd);
82 
83 hal_error_t hal_sd_control(uint32_t control, uint32_t arg);
84 void hal_sd_write(uint32_t sdx, uint32_t data);
85 bool hal_sd_read(sd_handle_t hsd, void *buf, uint32_t lba);
86 // void hal_uart_write_it(uint32_t uartx, uint8_t data);
87 // uint8_t hal_uart_read_it(uint32_t uartx);
88 
89 #endif
90