Skip to content

Commit 46d83d9

Browse files
iabdalkaderdpgeorge
authored andcommitted
mimxrt/sdio: Add SDIO driver.
This is a basic SDIO driver for the mimxrt port, that was added mainly to support the CYW43 WiFi driver, and as such it only supports the commands required by the CYW43 driver (but more commands can be added easily). The driver performs non-blocking DMA transfers, and can detect and recover from errors. Note: because the mimxrt port is missing static alternate functions, named pins and other pin related functions, currently the alternate functions for USDHC 1 and 2 are hard-coded in the driver. Signed-off-by: iabdalkader <[email protected]>
1 parent 6b407d5 commit 46d83d9

File tree

3 files changed

+375
-0
lines changed

3 files changed

+375
-0
lines changed

ports/mimxrt/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ SRC_C += \
216216
pendsv.c \
217217
pin.c \
218218
sdcard.c \
219+
sdio.c \
219220
systick.c \
220221
ticks.c \
221222
tusb_port.c \

ports/mimxrt/sdio.c

Lines changed: 334 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,334 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 Ibrahim Abdelkader <[email protected]>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
#include "py/mperrno.h"
29+
#include "py/mphal.h"
30+
#include "pin.h"
31+
#include "pendsv.h"
32+
33+
#include "fsl_usdhc.h"
34+
#include "fsl_iomuxc.h"
35+
36+
#if MICROPY_PY_NETWORK_CYW43
37+
38+
#if MICROPY_HW_SDIO_SDMMC == 1
39+
#define SDMMC USDHC1
40+
#define SDMMC_IRQn USDHC1_IRQn
41+
#define SDMMC_CLOCK_DIV kCLOCK_Usdhc1Div
42+
#define SDMMC_CLOCK_MUX kCLOCK_Usdhc1Mux
43+
#ifndef MICROPY_HW_SDIO_CLK_ALT
44+
#define MICROPY_HW_SDIO_CMD_ALT (0)
45+
#define MICROPY_HW_SDIO_CLK_ALT (0)
46+
#define MICROPY_HW_SDIO_D0_ALT (0)
47+
#define MICROPY_HW_SDIO_D1_ALT (0)
48+
#define MICROPY_HW_SDIO_D2_ALT (0)
49+
#define MICROPY_HW_SDIO_D3_ALT (0)
50+
#endif
51+
#else
52+
#define SDMMC USDHC2
53+
#define SDMMC_IRQn USDHC2_IRQn
54+
#define SDMMC_CLOCK_DIV kCLOCK_Usdhc2Div
55+
#define SDMMC_CLOCK_MUX kCLOCK_Usdhc2Mux
56+
#ifndef MICROPY_HW_SDIO_CLK_ALT
57+
#define MICROPY_HW_SDIO_CMD_ALT (6)
58+
#define MICROPY_HW_SDIO_CLK_ALT (6)
59+
#define MICROPY_HW_SDIO_D0_ALT (6)
60+
#define MICROPY_HW_SDIO_D1_ALT (6)
61+
#define MICROPY_HW_SDIO_D2_ALT (6)
62+
#define MICROPY_HW_SDIO_D3_ALT (6)
63+
#endif
64+
#endif
65+
66+
#define SDMMC_CLOCK_400KHZ (400000U)
67+
#define SDMMC_CLOCK_25MHZ (25000000U)
68+
#define SDMMC_CLOCK_50MHZ (50000000U)
69+
70+
#if SDIO_DEBUG
71+
#define debug_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__)
72+
#else
73+
#define debug_printf(...)
74+
#endif
75+
76+
#define DMA_DESCRIPTOR_BUFFER_SIZE (32U)
77+
AT_NONCACHEABLE_SECTION_ALIGN(
78+
static uint32_t sdio_adma_descriptor_table[DMA_DESCRIPTOR_BUFFER_SIZE], USDHC_ADMA2_ADDRESS_ALIGN);
79+
80+
typedef struct _mimxrt_sdmmc_t {
81+
USDHC_Type *inst;
82+
usdhc_handle_t handle;
83+
volatile uint32_t xfer_flags;
84+
volatile uint32_t xfer_error;
85+
} mimxrt_sdmmc_t;
86+
87+
static mimxrt_sdmmc_t sdmmc = {
88+
.inst = SDMMC,
89+
};
90+
91+
typedef enum {
92+
SDIO_TRANSFER_DATA_COMPLETE = (1 << 0),
93+
SDIO_TRANSFER_CMD_COMPLETE = (1 << 1),
94+
SDIO_TRANSFER_ERROR = (1 << 2),
95+
} sdio_xfer_flags_t;
96+
97+
static uint32_t sdio_base_clk(void) {
98+
return CLOCK_GetSysPfdFreq(kCLOCK_Pfd0) / (CLOCK_GetDiv(kCLOCK_Usdhc1Div) + 1U);
99+
}
100+
101+
static uint32_t sdio_response_type(uint32_t cmd) {
102+
switch (cmd) {
103+
case 3:
104+
return kCARD_ResponseTypeR6;
105+
case 5:
106+
return kCARD_ResponseTypeR4;
107+
case 7:
108+
return kCARD_ResponseTypeR1;
109+
case 52:
110+
return kCARD_ResponseTypeR5;
111+
default:
112+
return kCARD_ResponseTypeNone;
113+
}
114+
}
115+
116+
static void sdio_transfer_callback(USDHC_Type *base,
117+
usdhc_handle_t *handle, status_t status, void *userData) {
118+
if (status == kStatus_USDHC_TransferDataComplete) {
119+
sdmmc.xfer_flags |= SDIO_TRANSFER_DATA_COMPLETE;
120+
} else if (status == kStatus_USDHC_SendCommandSuccess) {
121+
sdmmc.xfer_flags |= SDIO_TRANSFER_CMD_COMPLETE;
122+
} else if (status != kStatus_USDHC_BusyTransferring) {
123+
sdmmc.xfer_error = status;
124+
sdmmc.xfer_flags |= SDIO_TRANSFER_ERROR;
125+
}
126+
}
127+
128+
static void sdio_interrupt_callback(USDHC_Type *base, void *userData) {
129+
extern void (*cyw43_poll)(void);
130+
131+
USDHC_DisableInterruptSignal(base, kUSDHC_CardInterruptFlag);
132+
USDHC_ClearInterruptStatusFlags(base, kUSDHC_CardInterruptFlag);
133+
134+
if (cyw43_poll) {
135+
pendsv_schedule_dispatch(PENDSV_DISPATCH_CYW43, cyw43_poll);
136+
}
137+
}
138+
139+
void sdio_init(uint32_t irq_pri) {
140+
machine_pin_config(MICROPY_HW_SDIO_CMD, PIN_MODE_ALT, PIN_PULL_UP_100K, PIN_DRIVE_6, 0, MICROPY_HW_SDIO_CMD_ALT);
141+
machine_pin_config(MICROPY_HW_SDIO_CLK, PIN_MODE_ALT, PIN_PULL_DISABLED, PIN_DRIVE_6, 0, MICROPY_HW_SDIO_CLK_ALT);
142+
machine_pin_config(MICROPY_HW_SDIO_D0, PIN_MODE_ALT, PIN_PULL_UP_100K, PIN_DRIVE_6, 0, MICROPY_HW_SDIO_D0_ALT);
143+
machine_pin_config(MICROPY_HW_SDIO_D1, PIN_MODE_ALT, PIN_PULL_UP_100K, PIN_DRIVE_6, 0, MICROPY_HW_SDIO_D1_ALT);
144+
machine_pin_config(MICROPY_HW_SDIO_D2, PIN_MODE_ALT, PIN_PULL_UP_100K, PIN_DRIVE_6, 0, MICROPY_HW_SDIO_D2_ALT);
145+
machine_pin_config(MICROPY_HW_SDIO_D3, PIN_MODE_ALT, PIN_PULL_UP_100K, PIN_DRIVE_6, 0, MICROPY_HW_SDIO_D3_ALT);
146+
147+
// Configure PFD0 of PLL2 (system PLL) fractional divider to 24 resulting in:
148+
// with PFD0_clk = PLL2_clk * 18 / N
149+
// PFD0_clk = 528MHz * 18 / 24 = 396MHz
150+
CLOCK_InitSysPfd(kCLOCK_Pfd0, 24U);
151+
CLOCK_SetDiv(SDMMC_CLOCK_DIV, 1U); // USDHC_input_clk = PFD0_clk / 2
152+
CLOCK_SetMux(SDMMC_CLOCK_MUX, 1U); // Select PFD0 as clock input for USDHC
153+
154+
// Initialize USDHC
155+
const usdhc_config_t config = {
156+
.endianMode = kUSDHC_EndianModeLittle,
157+
.dataTimeout = 0xFU,
158+
.readBurstLen = 0,
159+
.writeBurstLen = 0,
160+
.readWatermarkLevel = 128U,
161+
.writeWatermarkLevel = 128U,
162+
};
163+
164+
USDHC_Init(sdmmc.inst, &config);
165+
USDHC_Reset(SDMMC, kUSDHC_ResetAll, 1000U);
166+
USDHC_DisableInterruptSignal(SDMMC, kUSDHC_AllInterruptFlags);
167+
USDHC_SetSdClock(sdmmc.inst, sdio_base_clk(), SDMMC_CLOCK_25MHZ);
168+
USDHC_SetDataBusWidth(sdmmc.inst, kUSDHC_DataBusWidth1Bit);
169+
170+
mp_hal_delay_ms(10);
171+
172+
NVIC_SetPriority(SDMMC_IRQn, irq_pri);
173+
EnableIRQ(SDMMC_IRQn);
174+
175+
usdhc_transfer_callback_t callbacks = {
176+
.SdioInterrupt = sdio_interrupt_callback,
177+
.TransferComplete = sdio_transfer_callback,
178+
};
179+
USDHC_TransferCreateHandle(sdmmc.inst, &sdmmc.handle, &callbacks, NULL);
180+
}
181+
182+
void sdio_deinit(void) {
183+
}
184+
185+
void sdio_reenable(void) {
186+
}
187+
188+
void sdio_enable_irq(bool enable) {
189+
if (enable) {
190+
USDHC_ClearInterruptStatusFlags(sdmmc.inst, kUSDHC_CardInterruptFlag);
191+
USDHC_EnableInterruptStatus(sdmmc.inst, kUSDHC_CardInterruptFlag);
192+
USDHC_EnableInterruptSignal(sdmmc.inst, kUSDHC_CardInterruptFlag);
193+
} else {
194+
USDHC_DisableInterruptStatus(sdmmc.inst, kUSDHC_CardInterruptFlag);
195+
USDHC_ClearInterruptStatusFlags(sdmmc.inst, kUSDHC_CardInterruptFlag);
196+
USDHC_DisableInterruptSignal(sdmmc.inst, kUSDHC_CardInterruptFlag);
197+
}
198+
}
199+
200+
void sdio_enable_high_speed_4bit(void) {
201+
USDHC_SetSdClock(sdmmc.inst, sdio_base_clk(), SDMMC_CLOCK_50MHZ);
202+
USDHC_SetDataBusWidth(sdmmc.inst, kUSDHC_DataBusWidth4Bit);
203+
}
204+
205+
static status_t sdio_transfer_dma(USDHC_Type *base,
206+
usdhc_handle_t *handle, usdhc_transfer_t *transfer, uint32_t timeout_ms) {
207+
status_t status;
208+
usdhc_adma_config_t dma_config = {
209+
.dmaMode = kUSDHC_DmaModeAdma2,
210+
#if !FSL_FEATURE_USDHC_HAS_NO_RW_BURST_LEN
211+
.burstLen = kUSDHC_EnBurstLenForINCR,
212+
#endif
213+
.admaTable = sdio_adma_descriptor_table,
214+
.admaTableWords = DMA_DESCRIPTOR_BUFFER_SIZE,
215+
};
216+
217+
sdmmc.xfer_flags = 0;
218+
sdmmc.xfer_error = 0;
219+
220+
uint32_t xfer_flags = SDIO_TRANSFER_CMD_COMPLETE;
221+
if (transfer->data != NULL) {
222+
xfer_flags |= SDIO_TRANSFER_DATA_COMPLETE;
223+
}
224+
225+
status = USDHC_TransferNonBlocking(base, handle, &dma_config, transfer);
226+
if (status != kStatus_Success) {
227+
debug_printf("sdio_transfer_dma failed to start transfer error: %lu\n", status);
228+
return status;
229+
}
230+
231+
uint32_t start = mp_hal_ticks_ms();
232+
while ((sdmmc.xfer_flags != xfer_flags) &&
233+
!(sdmmc.xfer_flags & SDIO_TRANSFER_ERROR) &&
234+
(mp_hal_ticks_ms() - start) < timeout_ms) {
235+
MICROPY_EVENT_POLL_HOOK;
236+
}
237+
238+
if (sdmmc.xfer_flags == 0) {
239+
debug_printf("sdio_transfer_dma transfer timeout.\n");
240+
return kStatus_Timeout;
241+
} else if (sdmmc.xfer_flags != xfer_flags) {
242+
debug_printf("sdio_transfer_dma transfer failed: %lu\n", sdmmc.xfer_error);
243+
USDHC_Reset(base, kUSDHC_ResetCommand, 100);
244+
if (xfer_flags & SDIO_TRANSFER_DATA_COMPLETE) {
245+
USDHC_Reset(base, kUSDHC_ResetData, 100);
246+
}
247+
return sdmmc.xfer_error;
248+
}
249+
250+
return kStatus_Success;
251+
}
252+
253+
int sdio_transfer(uint32_t cmd, uint32_t arg, uint32_t *resp) {
254+
status_t status;
255+
usdhc_command_t command = {
256+
.index = cmd,
257+
.argument = arg,
258+
.type = kCARD_CommandTypeNormal,
259+
.responseType = sdio_response_type(cmd),
260+
.responseErrorFlags = 0
261+
};
262+
263+
usdhc_transfer_t transfer = {
264+
.data = NULL,
265+
.command = &command,
266+
};
267+
268+
status = sdio_transfer_dma(sdmmc.inst, &sdmmc.handle, &transfer, 5000);
269+
270+
if (status != kStatus_Success) {
271+
debug_printf("sdio_transfer failed!\n");
272+
return -MP_EIO;
273+
}
274+
275+
if (resp != NULL) {
276+
*resp = command.response[0];
277+
}
278+
279+
return 0;
280+
}
281+
282+
int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t len, uint8_t *buf) {
283+
usdhc_data_t data = {
284+
.enableAutoCommand12 = false,
285+
.enableAutoCommand23 = false,
286+
.enableIgnoreError = false,
287+
.dataType = kUSDHC_TransferDataNormal,
288+
};
289+
290+
usdhc_command_t command = {
291+
.index = 53,
292+
.argument = arg,
293+
.type = kCARD_CommandTypeNormal,
294+
.responseType = kCARD_ResponseTypeR5,
295+
.responseErrorFlags = 0
296+
};
297+
298+
usdhc_transfer_t transfer = {
299+
.data = &data,
300+
.command = &command,
301+
};
302+
303+
if (write) {
304+
data.rxData = NULL;
305+
data.txData = (uint32_t *)buf;
306+
} else {
307+
data.txData = NULL;
308+
data.rxData = (uint32_t *)buf;
309+
}
310+
311+
if (arg & (1 << 27)) {
312+
// SDIO_BLOCK_MODE
313+
data.blockSize = block_size;
314+
data.blockCount = len / block_size;
315+
} else {
316+
// SDIO_BYTE_MODE
317+
data.blockSize = block_size;
318+
data.blockCount = 1;
319+
}
320+
321+
debug_printf("cmd53 rw: %d addr 0x%p blocksize %u blockcount %lu total %lu len %d\n",
322+
write, buf, data.blockSize, data.blockCount, data.blockSize * data.blockCount, len);
323+
324+
status_t status = sdio_transfer_dma(sdmmc.inst, &sdmmc.handle, &transfer, 5000);
325+
326+
if (status != kStatus_Success) {
327+
debug_printf("sdio_transfer_cmd53 failed!\n");
328+
return -1;
329+
}
330+
331+
return 0;
332+
}
333+
334+
#endif

ports/mimxrt/sdio.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 Ibrahim Abdelkader <[email protected]>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_MIMXRT_SDIO_H
27+
#define MICROPY_INCLUDED_MIMXRT_SDIO_H
28+
29+
#include <stdbool.h>
30+
#include <stdint.h>
31+
32+
void sdio_init(uint32_t irq_pri);
33+
void sdio_deinit(void);
34+
void sdio_reenable(void);
35+
void sdio_enable_irq(bool enable);
36+
void sdio_enable_high_speed_4bit(void);
37+
int sdio_transfer(uint32_t cmd, uint32_t arg, uint32_t *resp);
38+
int sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t len, uint8_t *buf);
39+
40+
#endif // MICROPY_INCLUDED_MIMXRT_SDIO_H

0 commit comments

Comments
 (0)