54
54
fn \
55
55
}
56
56
57
- stc_sd_handle_t *handle;
57
+ stc_sd_handle_t *handle = nullptr ;
58
58
59
59
bool SDIO_Init () {
60
60
// Configure SDIO pins
@@ -66,36 +66,45 @@ bool SDIO_Init() {
66
66
GPIO_SetFunc (BOARD_SDIO_CMD, Func_Sdio);
67
67
GPIO_SetFunc (BOARD_SDIO_DET, Func_Sdio);
68
68
69
+ // If a handle is already initialized, free it before creating a new one
70
+ // otherwise, we will leak memory, which will eventually crash the system
71
+ if (handle != nullptr ) {
72
+ delete handle->pstcDmaInitCfg ;
73
+ delete handle->pstcCardInitCfg ;
74
+ delete handle;
75
+ handle = nullptr ;
76
+ }
77
+
69
78
// Create DMA configuration
70
79
stc_sdcard_dma_init_t *dmaConf = new stc_sdcard_dma_init_t ;
71
80
dmaConf->DMAx = SDIO_DMA_PERIPHERAL;
72
81
dmaConf->enDmaCh = SDIO_DMA_CHANNEL;
73
82
83
+ // Create card configuration
84
+ // This should be a fairly safe configuration for most cards
85
+ stc_sdcard_init_t *cardConf = new stc_sdcard_init_t ;
86
+ cardConf->enBusWidth = SdiocBusWidth4Bit;
87
+ cardConf->enClkFreq = SdiocClk400K;
88
+ cardConf->enSpeedMode = SdiocNormalSpeedMode;
89
+ cardConf->pstcInitCfg = nullptr ;
90
+
74
91
// Create handle in DMA mode
75
92
handle = new stc_sd_handle_t ;
76
93
handle->SDIOCx = SDIO_PERIPHERAL;
77
94
handle->enDevMode = SdCardDmaMode;
78
95
handle->pstcDmaInitCfg = dmaConf;
79
-
80
- // Create card configuration
81
- // This should be a fairly safe configuration for most cards
82
- stc_sdcard_init_t cardConf = {
83
- .enBusWidth = SdiocBusWidth4Bit,
84
- .enClkFreq = SdiocClk400K,
85
- .enSpeedMode = SdiocNormalSpeedMode,
86
- // .pstcInitCfg = NULL,
87
- };
96
+ // handle->pstcCardInitCfg = cardConf; // assigned in SDCARD_Init
88
97
89
98
// Initialize sd card
90
- en_result_t rc = SDCARD_Init (handle, & cardConf);
99
+ en_result_t rc = SDCARD_Init (handle, cardConf);
91
100
if (rc != Ok) printf (" SDIO_Init() error (rc=%u)\n " , rc);
92
101
93
102
return rc == Ok;
94
103
}
95
104
96
105
bool SDIO_ReadBlock (uint32_t block, uint8_t *dst) {
97
- CORE_ASSERT (handle != NULL , " SDIO not initialized" );
98
- CORE_ASSERT (dst != NULL , " SDIO_ReadBlock dst is NULL" );
106
+ CORE_ASSERT (handle != nullptr , " SDIO not initialized" , return false );
107
+ CORE_ASSERT (dst != nullptr , " SDIO_ReadBlock dst is NULL" , return false );
99
108
100
109
WITH_RETRY (SDIO_READ_RETRIES, {
101
110
en_result_t rc = SDCARD_ReadBlocks (handle, block, 1 , dst, SDIO_READ_TIMEOUT);
@@ -107,8 +116,8 @@ bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) {
107
116
}
108
117
109
118
bool SDIO_WriteBlock (uint32_t block, const uint8_t *src) {
110
- CORE_ASSERT (handle != NULL , " SDIO not initialized" );
111
- CORE_ASSERT (src != NULL , " SDIO_WriteBlock src is NULL" );
119
+ CORE_ASSERT (handle != nullptr , " SDIO not initialized" , return false );
120
+ CORE_ASSERT (src != nullptr , " SDIO_WriteBlock src is NULL" , return false );
112
121
113
122
WITH_RETRY (SDIO_WRITE_RETRIES, {
114
123
en_result_t rc = SDCARD_WriteBlocks (handle, block, 1 , (uint8_t *)src, SDIO_WRITE_TIMEOUT);
@@ -120,12 +129,12 @@ bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
120
129
}
121
130
122
131
bool SDIO_IsReady () {
123
- CORE_ASSERT (handle != NULL , " SDIO not initialized" );
132
+ CORE_ASSERT (handle != nullptr , " SDIO not initialized" , return false );
124
133
return bool (handle->stcCardStatus .READY_FOR_DATA );
125
134
}
126
135
127
136
uint32_t SDIO_GetCardSize () {
128
- CORE_ASSERT (handle != NULL , " SDIO not initialized" );
137
+ CORE_ASSERT (handle != nullptr , " SDIO not initialized" , return 0 );
129
138
130
139
// Multiply number of blocks with block size to get size in bytes
131
140
const uint64_t cardSizeBytes = uint64_t (handle->stcSdCardInfo .u32LogBlockNbr ) * uint64_t (handle->stcSdCardInfo .u32LogBlockSize );
0 commit comments