Skip to content

Commit ec84502

Browse files
JorgeGzmxiaoxiang781216
authored andcommitted
board/weact-stm32h743 Add sdcard support
This commit adds SD card support to the WeAct STM32H743 board configuration. The implementation enables the board to interface with SD cards through the SDMMC peripheral, allowing file system operations and data storage capabilities. Signed-off-by: Jorge Guzman <[email protected]>
1 parent d5eda10 commit ec84502

File tree

9 files changed

+493
-14
lines changed

9 files changed

+493
-14
lines changed

Documentation/platforms/arm/stm32h7/boards/weact-stm32h743/index.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ USART1
5656
RX PB15
5757
====== =====
5858

59+
60+
SDMMC
61+
======
62+
63+
The WeAct STM32H743 has one SDCard slot connected as below:
64+
65+
========== =====
66+
SDMMC1 PINS
67+
========== =====
68+
SDMMC_D0 PC8
69+
SDMMC_D1 PC9
70+
SDMMC_D2 PC10
71+
SDMMC_D3 PC11
72+
SDMMC_DK PC12
73+
========== =====
74+
75+
=============== =====
76+
GPIO PINS
77+
=============== =====
78+
SDCARD_DETECTED PD4
79+
=============== =====
80+
5981
==============
6082

6183
Each weact-stm32h743 configuration is maintained in a sub-directory and
@@ -95,3 +117,18 @@ After flashing and reboot your board you should see in your dmesg logs::
95117

96118
You may need to press **ENTER** 3 times before the NSH show up.
97119

120+
sdcard
121+
------
122+
123+
Configures the NuttShell (nsh) and enables SD card support. The board has an onboard microSD slot that should be
124+
automatically registered as the block device /dev/mmcsd0 when an SD card is present.
125+
126+
The SD card can then be mounted by the NSH commands::
127+
128+
nsh> mount -t vfat /dev/mmcsd0 /mnt
129+
nsh> mount
130+
nsh> echo "Hello World!!" > /mnt/test_file.txt
131+
nhs> ls /mnt/
132+
test_file.txt
133+
nsh> cat /mnt/test_file.txt
134+
Hello World!!
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#
2+
# This file is autogenerated: PLEASE DO NOT EDIT IT.
3+
#
4+
# You can use "make menuconfig" to make any modifications to the installed .config file.
5+
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
6+
# modifications.
7+
#
8+
# CONFIG_MMCSD_HAVE_WRITEPROTECT is not set
9+
# CONFIG_MMCSD_MMCSUPPORT is not set
10+
# CONFIG_STANDARD_SERIAL is not set
11+
# CONFIG_STM32H7_USE_LEGACY_PINMAP is not set
12+
CONFIG_ARCH="arm"
13+
CONFIG_ARCH_BOARD="weact-stm32h743"
14+
CONFIG_ARCH_BOARD_WEACT_STM32H743=y
15+
CONFIG_ARCH_CHIP="stm32h7"
16+
CONFIG_ARCH_CHIP_STM32H743VI=y
17+
CONFIG_ARCH_CHIP_STM32H7=y
18+
CONFIG_ARCH_CHIP_STM32H7_CORTEXM7=y
19+
CONFIG_ARCH_STACKDUMP=y
20+
CONFIG_ARMV7M_DCACHE=y
21+
CONFIG_ARMV7M_DCACHE_WRITETHROUGH=y
22+
CONFIG_ARMV7M_DTCM=y
23+
CONFIG_ARMV7M_ICACHE=y
24+
CONFIG_BOARD_LOOPSPERMSEC=43103
25+
CONFIG_BUILTIN=y
26+
CONFIG_DEBUG_SYMBOLS=y
27+
CONFIG_EXAMPLES_ALARM=y
28+
CONFIG_FAT_DMAMEMORY=y
29+
CONFIG_FAT_LCNAMES=y
30+
CONFIG_FAT_LFN=y
31+
CONFIG_FS_FAT=y
32+
CONFIG_FS_PROCFS=y
33+
CONFIG_GRAN=y
34+
CONFIG_GRAN_INTR=y
35+
CONFIG_INIT_ENTRYPOINT="nsh_main"
36+
CONFIG_INIT_STACKSIZE=4096
37+
CONFIG_INTELHEX_BINARY=y
38+
CONFIG_LIBM=y
39+
CONFIG_LINE_MAX=64
40+
CONFIG_MMCSD=y
41+
CONFIG_MMCSD_SDIO=y
42+
CONFIG_MMCSD_SDIOWAIT_WRCOMPLETE=y
43+
CONFIG_MM_REGIONS=4
44+
CONFIG_NSH_ARCHINIT=y
45+
CONFIG_NSH_BUILTIN_APPS=y
46+
CONFIG_NSH_DISABLE_IFUPDOWN=y
47+
CONFIG_NSH_FILEIOSIZE=512
48+
CONFIG_NSH_READLINE=y
49+
CONFIG_PREALLOC_TIMERS=4
50+
CONFIG_RAM_SIZE=245760
51+
CONFIG_RAM_START=0x20010000
52+
CONFIG_RAW_BINARY=y
53+
CONFIG_RR_INTERVAL=200
54+
CONFIG_RTC_ALARM=y
55+
CONFIG_RTC_DATETIME=y
56+
CONFIG_RTC_DRIVER=y
57+
CONFIG_SCHED_HPWORK=y
58+
CONFIG_SCHED_WAITPID=y
59+
CONFIG_SDMMC1_SDIO_MODE=y
60+
CONFIG_START_DAY=11
61+
CONFIG_START_MONTH=5
62+
CONFIG_START_YEAR=2024
63+
CONFIG_STM32H7_PWR=y
64+
CONFIG_STM32H7_RTC=y
65+
CONFIG_STM32H7_SDMMC1=y
66+
CONFIG_STM32H7_USART1=y
67+
CONFIG_SYSTEM_NSH=y
68+
CONFIG_TASK_NAME_SIZE=0
69+
CONFIG_USART1_SERIAL_CONSOLE=y

boards/arm/stm32h7/weact-stm32h743/include/board.h

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,30 @@
141141
#define STM32_PLLCFG_PLL2Q RCC_PLL2DIVR_Q2(40)
142142
#define STM32_PLLCFG_PLL2R RCC_PLL2DIVR_R2(3)
143143

144-
/* PLL3 */
145-
146-
#define STM32_PLLCFG_PLL3CFG 0
147-
#define STM32_PLLCFG_PLL3M 0
148-
#define STM32_PLLCFG_PLL3N 0
149-
#define STM32_PLLCFG_PLL3P 0
150-
#define STM32_PLLCFG_PLL3Q 0
151-
#define STM32_PLLCFG_PLL3R 0
152-
153-
#define STM32_VCO3_FREQUENCY
154-
#define STM32_PLL3P_FREQUENCY
155-
#define STM32_PLL3Q_FREQUENCY
156-
#define STM32_PLL3R_FREQUENCY
144+
/* PLL3 - 25 MHz input, enable DIVP, DIVQ, DIVR
145+
*
146+
* PLL3_VCO = (25 MHz / 1) * 35 = 875 MHz
147+
*
148+
* PLL3P = PLL3_VCO/2 = 875 MHz / 2 = 437.7 MHz
149+
* PLL3Q = PLL3_VCO/1 = 875 MHz / 1 = 875 MHz
150+
* PLL3R = PLL3_VCO/20 = 875 MHz / 20 = 43.75 MHz
151+
*/
152+
#define STM32_PLLCFG_PLL3CFG (RCC_PLLCFGR_PLL3VCOSEL_WIDE| \
153+
RCC_PLLCFGR_PLL3RGE_8_16_MHZ| \
154+
RCC_PLLCFGR_DIVP3EN| \
155+
RCC_PLLCFGR_DIVQ3EN| \
156+
RCC_PLLCFGR_DIVR3EN)
157+
158+
#define STM32_PLLCFG_PLL3M RCC_PLLCKSELR_DIVM3(1)
159+
#define STM32_PLLCFG_PLL3N RCC_PLL3DIVR_N3(35)
160+
#define STM32_PLLCFG_PLL3P RCC_PLL3DIVR_P3(2)
161+
#define STM32_PLLCFG_PLL3Q RCC_PLL3DIVR_Q3(1)
162+
#define STM32_PLLCFG_PLL3R RCC_PLL3DIVR_R3(20)
163+
164+
#define STM32_VCO3_FREQUENCY ((STM32_HSE_FREQUENCY / 1) * 35)
165+
#define STM32_PLL3P_FREQUENCY (STM32_VCO3_FREQUENCY / 2)
166+
#define STM32_PLL3Q_FREQUENCY (STM32_VCO3_FREQUENCY / 1)
167+
#define STM32_PLL3R_FREQUENCY (STM32_VCO3_FREQUENCY / 20)
157168

158169
/* SYSCLK = PLL1P = 480 MHz
159170
* CPUCLK = SYSCLK / 1 = 480 MHz
@@ -165,7 +176,7 @@
165176

166177
/* Configure Clock Assignments */
167178

168-
/* AHB clock (HCLK) is SYSCLK/2 (480 MHz max)
179+
/* AHB clock (HCLK) is SYSCLK/2 (240 MHz max)
169180
* HCLK1 = HCLK2 = HCLK3 = HCLK4
170181
*/
171182

@@ -248,6 +259,17 @@
248259

249260
#define STM32_RCC_D3CCIPR_ADCSRC RCC_D3CCIPR_ADCSEL_PLL2
250261

262+
/* FDCAN 1 2 clock source, use STM32_HSE_FREQUENCY */
263+
264+
#define STM32_RCC_D2CCIP1R_FDCANSEL RCC_D2CCIP1R_FDCANSEL_HSE
265+
266+
/* SDMMC 1 2 clock source, use STM32_PLL1Q_FREQUENCY */
267+
268+
#define STM32_RCC_D1CCIPR_SDMMCSEL RCC_D1CCIPR_SDMMC_PLL1
269+
270+
/* FMC clock source, use STM32_PLL1Q_FREQUENCY */
271+
#define BOARD_FMC_CLK RCC_D1CCIPR_FMCSEL_HCLK
272+
251273
/* FLASH wait states
252274
*
253275
* ------------ ---------- -----------
@@ -285,6 +307,13 @@
285307

286308
#define STM32_SDMMC_CLKCR_EDGE STM32_SDMMC_CLKCR_NEGEDGE
287309

310+
#define GPIO_SDMMC1_CK (GPIO_SDMMC1_CK_0|GPIO_SPEED_100MHz) /* PC12 */
311+
#define GPIO_SDMMC1_CMD (GPIO_SDMMC1_CMD_0|GPIO_SPEED_100MHz) /* PD2 */
312+
#define GPIO_SDMMC1_D0 (GPIO_SDMMC1_D0_0|GPIO_SPEED_100MHz) /* PC8 */
313+
#define GPIO_SDMMC1_D1 (GPIO_SDMMC1_D1_0|GPIO_SPEED_100MHz) /* PC9 */
314+
#define GPIO_SDMMC1_D2 (GPIO_SDMMC1_D2_0|GPIO_SPEED_100MHz) /* PC10 */
315+
#define GPIO_SDMMC1_D3 (GPIO_SDMMC1_D3_0|GPIO_SPEED_100MHz) /* PC11 */
316+
288317
/* LED definitions **********************************************************/
289318

290319
/* The board has 1 user LED that could be used this diagnostic LED too.

boards/arm/stm32h7/weact-stm32h743/src/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ else()
2828
list(APPEND SRCS stm32_userleds.c)
2929
endif()
3030

31+
if(CONFIG_STM32H7_SDMMC)
32+
list(APPEND SRCS stm32_sdmmc.c)
33+
endif()
34+
35+
if(CONFIG_FAT_DMAMEMORY)
36+
list(APPEND SRCS stm32_dma_alloc.c)
37+
endif()
38+
3139
if(CONFIG_BOARDCTL)
3240
list(APPEND SRCS stm32_appinitialize.c)
3341
endif()

boards/arm/stm32h7/weact-stm32h743/src/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ else
3030
CSRCS += stm32_userleds.c
3131
endif
3232

33+
ifeq ($(CONFIG_STM32H7_SDMMC),y)
34+
CSRCS += stm32_sdmmc.c
35+
endif
36+
37+
ifeq ($(CONFIG_FAT_DMAMEMORY),y)
38+
CSRCS += stm32_dma_alloc.c
39+
endif
40+
3341
ifeq ($(CONFIG_BOARDCTL),y)
3442
CSRCS += stm32_appinitialize.c
3543
endif

boards/arm/stm32h7/weact-stm32h743/src/stm32_bringup.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,23 @@ int stm32_bringup(void)
7878
}
7979
#endif /* CONFIG_FS_PROCFS */
8080

81+
#if defined(CONFIG_FAT_DMAMEMORY)
82+
if (stm32_dma_alloc_init() < 0)
83+
{
84+
syslog(LOG_ERR, "DMA alloc FAILED");
85+
}
86+
#endif
87+
88+
#ifdef HAVE_SDIO
89+
/* Initialize the SDIO block driver */
90+
91+
ret = stm32_sdio_initialize();
92+
if (ret < 0)
93+
{
94+
syslog(LOG_ERR,
95+
"ERROR: Failed to initialize MMC/SD driver: %d\n", ret);
96+
}
97+
#endif
98+
8199
return OK;
82100
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/****************************************************************************
2+
* boards/arm/stm32h7/weact-stm32h743/src/stm32_dma_alloc.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <nuttx/config.h>
28+
#include <syslog.h>
29+
#include <stdint.h>
30+
#include <errno.h>
31+
#include <nuttx/mm/gran.h>
32+
33+
#include "weact-stm32h743.h"
34+
35+
#if defined(CONFIG_FAT_DMAMEMORY)
36+
37+
/****************************************************************************
38+
* Pre-processor Definitions
39+
****************************************************************************/
40+
41+
#if !defined(CONFIG_GRAN)
42+
# error microSD DMA support requires CONFIG_GRAN
43+
#endif
44+
45+
#define BOARD_DMA_ALLOC_POOL_SIZE (8*512)
46+
47+
/****************************************************************************
48+
* Private Data
49+
****************************************************************************/
50+
51+
static GRAN_HANDLE dma_allocator;
52+
53+
/* The DMA heap size constrains the total number of things that can be
54+
* ready to do DMA at a time.
55+
*
56+
* For example, FAT DMA depends on one sector-sized buffer per
57+
* filesystem plus one sector-sized buffer per file.
58+
*
59+
* We use a fundamental alignment / granule size of 64B; this is
60+
* sufficient to guarantee alignment for the largest STM32 DMA burst
61+
* (16 beats x 32bits).
62+
*/
63+
64+
static uint8_t g_dma_heap[BOARD_DMA_ALLOC_POOL_SIZE]
65+
aligned_data(64);
66+
67+
/****************************************************************************
68+
* Public Functions
69+
****************************************************************************/
70+
71+
/****************************************************************************
72+
* Name: stm32_dma_alloc_init
73+
*
74+
* Description:
75+
* All boards may optionally provide this API to instantiate a pool of
76+
* memory for uses with FAST FS DMA operations.
77+
*
78+
****************************************************************************/
79+
80+
int stm32_dma_alloc_init(void)
81+
{
82+
dma_allocator = gran_initialize(g_dma_heap,
83+
sizeof(g_dma_heap),
84+
7, /* 128B granule - must be > alignment (XXX bug?) */
85+
6); /* 64B alignment */
86+
87+
if (dma_allocator == NULL)
88+
{
89+
return -ENOMEM;
90+
}
91+
92+
return OK;
93+
}
94+
95+
/* DMA-aware allocator stubs for the FAT filesystem. */
96+
97+
void *fat_dma_alloc(size_t size)
98+
{
99+
return gran_alloc(dma_allocator, size);
100+
}
101+
102+
void fat_dma_free(void *memory, size_t size)
103+
{
104+
gran_free(dma_allocator, memory, size);
105+
}
106+
107+
#endif /* CONFIG_FAT_DMAMEMORY */

0 commit comments

Comments
 (0)