32
32
33
33
#define SDIO_PERIPHERAL M4_SDIOC1
34
34
35
- // use DMA2 channel 0
35
+ // Use DMA2 channel 0
36
36
#define SDIO_DMA_PERIPHERAL M4_DMA2
37
37
#define SDIO_DMA_CHANNEL DmaCh0
38
38
@@ -77,20 +77,18 @@ bool SDIO_Init() {
77
77
handle->enDevMode = SdCardDmaMode;
78
78
handle->pstcDmaInitCfg = dmaConf;
79
79
80
- // create card configuration
81
- // this should be a fairly safe configuration for most cards
80
+ // Create card configuration
81
+ // This should be a fairly safe configuration for most cards
82
82
stc_sdcard_init_t cardConf = {
83
- .enBusWidth = SdiocBusWidth4Bit,
84
- .enClkFreq = SdiocClk400K,
85
- .enSpeedMode = SdiocNormalSpeedMode,
86
- // .pstcInitCfg = NULL,
83
+ .enBusWidth = SdiocBusWidth4Bit,
84
+ .enClkFreq = SdiocClk400K,
85
+ .enSpeedMode = SdiocNormalSpeedMode,
86
+ // .pstcInitCfg = NULL,
87
87
};
88
88
89
- // initialize sd card
89
+ // Initialize sd card
90
90
en_result_t rc = SDCARD_Init (handle, &cardConf);
91
- if (rc != Ok) {
92
- printf (" SDIO_Init() error (rc=%u)\n " , rc);
93
- }
91
+ if (rc != Ok) printf (" SDIO_Init() error (rc=%u)\n " , rc);
94
92
95
93
return rc == Ok;
96
94
}
@@ -101,12 +99,8 @@ bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) {
101
99
102
100
WITH_RETRY (SDIO_READ_RETRIES, {
103
101
en_result_t rc = SDCARD_ReadBlocks (handle, block, 1 , dst, SDIO_READ_TIMEOUT);
104
- if (rc == Ok) {
105
- return true ;
106
- }
107
- else {
108
- printf (" SDIO_ReadBlock error (rc=%u; ErrorCode=%lu)\n " , rc, handle->u32ErrorCode );
109
- }
102
+ if (rc == Ok) return true ;
103
+ printf (" SDIO_ReadBlock error (rc=%u; ErrorCode=%lu)\n " , rc, handle->u32ErrorCode );
110
104
})
111
105
112
106
return false ;
@@ -118,12 +112,8 @@ bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
118
112
119
113
WITH_RETRY (SDIO_WRITE_RETRIES, {
120
114
en_result_t rc = SDCARD_WriteBlocks (handle, block, 1 , (uint8_t *)src, SDIO_WRITE_TIMEOUT);
121
- if (rc == Ok) {
122
- return true ;
123
- }
124
- else {
125
- printf (" SDIO_WriteBlock error (rc=%u; ErrorCode=%lu)\n " , rc, handle->u32ErrorCode );
126
- }
115
+ if (rc == Ok) return true ;
116
+ printf (" SDIO_WriteBlock error (rc=%u; ErrorCode=%lu)\n " , rc, handle->u32ErrorCode );
127
117
})
128
118
129
119
return false ;
@@ -137,15 +127,11 @@ bool SDIO_IsReady() {
137
127
uint32_t SDIO_GetCardSize () {
138
128
CORE_ASSERT (handle != NULL , " SDIO not initialized" );
139
129
140
- // multiply number of blocks with block size to get size in bytes
141
- uint64_t cardSizeBytes = uint64_t (handle->stcSdCardInfo .u32LogBlockNbr ) * uint64_t (handle->stcSdCardInfo .u32LogBlockSize );
142
-
143
- // if the card is bigger than ~4Gb (maximum a 32bit integer can hold), clamp to the maximum value of a 32 bit integer
144
- if (cardSizeBytes >= UINT32_MAX) {
145
- return UINT32_MAX;
146
- }
130
+ // Multiply number of blocks with block size to get size in bytes
131
+ const uint64_t cardSizeBytes = uint64_t (handle->stcSdCardInfo .u32LogBlockNbr ) * uint64_t (handle->stcSdCardInfo .u32LogBlockSize );
147
132
148
- return uint32_t (cardSizeBytes);
133
+ // If the card is bigger than ~4Gb (maximum a 32bit integer can hold), clamp to the maximum value of a 32 bit integer
134
+ return _MAX (cardSizeBytes, UINT32_MAX);
149
135
}
150
136
151
137
#endif // ARDUINO_ARCH_HC32
0 commit comments