Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Mozi
rt-thread
提交
7b8a7836
R
rt-thread
项目概览
Mozi
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7b8a7836
编写于
1月 16, 2018
作者:
L
liu2guang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Bsp] add sdcard drive | 添加sd卡驱动
上级
ff6b7c4c
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
372 addition
and
3 deletion
+372
-3
bsp/stm32f10x-HAL/Kconfig
bsp/stm32f10x-HAL/Kconfig
+13
-0
bsp/stm32f10x-HAL/drivers/SConscript
bsp/stm32f10x-HAL/drivers/SConscript
+2
-2
bsp/stm32f10x-HAL/drivers/drv_sdcard.c
bsp/stm32f10x-HAL/drivers/drv_sdcard.c
+324
-0
bsp/stm32f10x-HAL/drivers/drv_sdcard.h
bsp/stm32f10x-HAL/drivers/drv_sdcard.h
+30
-0
bsp/stm32f10x-HAL/drivers/stm32f1xx_hal_conf.h
bsp/stm32f10x-HAL/drivers/stm32f1xx_hal_conf.h
+3
-1
未找到文件。
bsp/stm32f10x-HAL/Kconfig
浏览文件 @
7b8a7836
...
...
@@ -182,3 +182,16 @@ if RT_USING_SPI
default n
endif
if RT_USING_DEVICE_IPC && (STM32F103RC || STM32F103RD || STM32F103RE || STM32F103RF || STM32F103RG ||STM32F103VC || STM32F103VD || STM32F103VE || STM32F103VF || STM32F103VG ||STM32F103ZC || STM32F103ZD || STM32F103ZE || STM32F103ZF || STM32F103ZG)
config RT_USING_SDCARD
bool "Using sdcard with sdio"
default n
config USING_SD_RX_DMA
bool "SDCARD Rx Using Dma Mode"
default y
depends on RT_USING_SDCARD
config USING_SD_TX_DMA
bool "SDCARD Tx Using Dma Mode"
default n
depends on RT_USING_SDCARD
endif
bsp/stm32f10x-HAL/drivers/SConscript
浏览文件 @
7b8a7836
...
...
@@ -18,8 +18,8 @@ if GetDepend(['RT_USING_SPI']):
src
+=
[
'drv_spi.c'
]
if
GetDepend
([
'RT_USING_USB_DEVICE'
]):
src
+=
[
'drv_usb.c'
]
if
GetDepend
([
'RT_USING_SDCARD'
]):
src
+=
[
'drv_sdcard.c'
]
CPPPATH
=
[
cwd
]
...
...
bsp/stm32f10x-HAL/drivers/drv_sdcard.c
0 → 100644
浏览文件 @
7b8a7836
/*
* File : drv_sdcard.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2018-01-13 Liu2guang the first version.
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "board.h"
#include "drv_sdcard.h"
#ifndef SDIO_CLK_DIV
#define SDIO_CLK_DIV 2
#endif
#define SDIO_TIMEOUT ((uint32_t)0x100000)
static
SD_HandleTypeDef
hsdcard
;
static
DMA_HandleTypeDef
hdma
;
static
struct
rt_semaphore
sd_lock
;
void
SDIO_IRQHandler
(
void
)
{
rt_interrupt_enter
();
HAL_SD_IRQHandler
(
&
hsdcard
);
rt_interrupt_leave
();
}
#if defined(USING_SD_RX_DMA) || defined(USING_SD_TX_DMA)
void
DMA2_Channel4_5_IRQHandler
(
void
)
{
rt_interrupt_enter
();
HAL_DMA_IRQHandler
(
&
hdma
);
rt_interrupt_leave
();
}
#endif
rt_err_t
stm32_read_blocks
(
uint32_t
*
data
,
uint32_t
addr
,
uint32_t
num
)
{
uint32_t
timeout
=
0
;
HAL_SD_StateTypeDef
state_return
;
HAL_SD_CardStateTypeDef
sd_card_state_return
;
#if defined(USING_SD_RX_DMA) && defined(USING_SD_TX_DMA)
hdma
.
Init
.
Direction
=
DMA_PERIPH_TO_MEMORY
;
hdma
.
Init
.
PeriphInc
=
DMA_PINC_DISABLE
;
hdma
.
Init
.
MemInc
=
DMA_MINC_ENABLE
;
HAL_DMA_DeInit
(
&
hdma
);
HAL_DMA_Init
(
&
hdma
);
#endif
#if defined(USING_SD_RX_DMA)
if
(
HAL_SD_ReadBlocks_DMA
(
&
hsdcard
,
(
uint8_t
*
)
data
,
addr
,
num
)
!=
HAL_OK
)
#else
if
(
HAL_SD_ReadBlocks
(
&
hsdcard
,
(
uint8_t
*
)
data
,
addr
,
num
,
SDIO_TIMEOUT
)
!=
HAL_OK
)
#endif
{
return
RT_EIO
;
}
do
{
state_return
=
HAL_SD_GetState
(
&
hsdcard
);
timeout
++
;
}
while
((
HAL_SD_STATE_BUSY
==
state_return
)
&&
(
SDIO_TIMEOUT
>
timeout
));
if
(
HAL_SD_STATE_READY
!=
state_return
)
{
return
RT_ERROR
;
}
do
{
sd_card_state_return
=
HAL_SD_GetCardState
(
&
hsdcard
);
timeout
++
;
}
while
((
HAL_SD_CARD_TRANSFER
!=
sd_card_state_return
)
&&
(
SDIO_TIMEOUT
>
timeout
));
if
(
SDIO_TIMEOUT
<=
timeout
)
{
return
RT_ETIMEOUT
;
}
return
RT_EOK
;
}
rt_err_t
stm32_write_blocks
(
uint32_t
*
data
,
uint32_t
addr
,
uint32_t
num
)
{
uint32_t
timeout
=
0
;
HAL_SD_StateTypeDef
state_return
;
HAL_SD_CardStateTypeDef
sd_card_state_return
;
#if defined(USING_SD_RX_DMA) && defined(USING_SD_TX_DMA)
hdma
.
Init
.
Direction
=
DMA_MEMORY_TO_PERIPH
;
hdma
.
Init
.
PeriphInc
=
DMA_MINC_ENABLE
;
hdma
.
Init
.
MemInc
=
DMA_PINC_DISABLE
;
HAL_DMA_DeInit
(
&
hdma
);
HAL_DMA_Init
(
&
hdma
);
#endif
#if defined(USING_SD_TX_DMA)
if
(
HAL_SD_WriteBlocks_DMA
(
&
hsdcard
,
(
uint8_t
*
)
data
,
addr
,
num
)
!=
HAL_OK
)
#else
if
(
HAL_SD_WriteBlocks
(
&
hsdcard
,
(
uint8_t
*
)
data
,
addr
,
num
,
SDIO_TIMEOUT
)
!=
HAL_OK
)
#endif
{
return
RT_ERROR
;
}
do
{
state_return
=
HAL_SD_GetState
(
&
hsdcard
);
timeout
++
;
}
while
((
HAL_SD_STATE_BUSY
==
state_return
)
&&
(
SDIO_TIMEOUT
>
timeout
));
if
(
HAL_SD_STATE_READY
!=
state_return
)
{
return
RT_ERROR
;
}
do
{
sd_card_state_return
=
HAL_SD_GetCardState
(
&
hsdcard
);
timeout
++
;
}
while
((
HAL_SD_CARD_TRANSFER
!=
sd_card_state_return
)
&&
(
SDIO_TIMEOUT
>
timeout
));
if
(
SDIO_TIMEOUT
<=
timeout
)
{
return
RT_ETIMEOUT
;
}
return
RT_EOK
;
}
static
rt_err_t
stm32_sdcard_init
(
rt_device_t
dev
)
{
GPIO_InitTypeDef
GPIO_InitStruct
;
if
(
rt_sem_init
(
&
sd_lock
,
"sdlock"
,
1
,
RT_IPC_FLAG_FIFO
)
!=
RT_EOK
)
{
return
RT_ERROR
;
}
__HAL_RCC_GPIOD_CLK_ENABLE
();
__HAL_RCC_GPIOC_CLK_ENABLE
();
GPIO_InitStruct
.
Pin
=
GPIO_PIN_8
|
GPIO_PIN_9
|
GPIO_PIN_10
|
GPIO_PIN_11
|
GPIO_PIN_12
;
GPIO_InitStruct
.
Mode
=
GPIO_MODE_AF_PP
;
GPIO_InitStruct
.
Speed
=
GPIO_SPEED_FREQ_HIGH
;
HAL_GPIO_Init
(
GPIOC
,
&
GPIO_InitStruct
);
GPIO_InitStruct
.
Pin
=
GPIO_PIN_2
;
HAL_GPIO_Init
(
GPIOD
,
&
GPIO_InitStruct
);
#if defined(USING_SD_RX_DMA) || defined(USING_SD_TX_DMA)
__HAL_RCC_DMA2_CLK_ENABLE
();
hdma
.
Instance
=
DMA2_Channel4
;
hdma
.
Init
.
PeriphDataAlignment
=
DMA_PDATAALIGN_WORD
;
hdma
.
Init
.
MemDataAlignment
=
DMA_MDATAALIGN_WORD
;
hdma
.
Init
.
Mode
=
DMA_NORMAL
;
hdma
.
Init
.
Priority
=
DMA_PRIORITY_HIGH
;
#if defined(USING_SD_RX_DMA)
hdma
.
Init
.
Direction
=
DMA_PERIPH_TO_MEMORY
;
hdma
.
Init
.
PeriphInc
=
DMA_PINC_DISABLE
;
hdma
.
Init
.
MemInc
=
DMA_MINC_ENABLE
;
__HAL_LINKDMA
(
&
hsdcard
,
hdmarx
,
hdma
);
#endif
#if defined(USING_SD_TX_DMA)
hdma
.
Init
.
Direction
=
DMA_MEMORY_TO_PERIPH
;
hdma
.
Init
.
PeriphInc
=
DMA_MINC_ENABLE
;
hdma
.
Init
.
MemInc
=
DMA_PINC_DISABLE
;
__HAL_LINKDMA
(
&
hsdcard
,
hdmatx
,
hdma
);
#endif
HAL_DMA_DeInit
(
&
hdma
);
if
(
HAL_DMA_Init
(
&
hdma
)
!=
HAL_OK
)
{
rt_kprintf
(
"HAL_DMA_Init error
\n
"
);
return
RT_EIO
;
}
#endif
HAL_NVIC_SetPriority
(
DMA2_Channel4_5_IRQn
,
3
,
0
);
HAL_NVIC_EnableIRQ
(
DMA2_Channel4_5_IRQn
);
__HAL_RCC_SDIO_CLK_ENABLE
();
hsdcard
.
Instance
=
SDIO
;
hsdcard
.
Init
.
ClockEdge
=
SDIO_CLOCK_EDGE_RISING
;
hsdcard
.
Init
.
ClockBypass
=
SDIO_CLOCK_BYPASS_DISABLE
;
hsdcard
.
Init
.
ClockPowerSave
=
SDIO_CLOCK_POWER_SAVE_DISABLE
;
hsdcard
.
Init
.
BusWide
=
SDIO_BUS_WIDE_1B
;
hsdcard
.
Init
.
HardwareFlowControl
=
SDIO_HARDWARE_FLOW_CONTROL_ENABLE
;
hsdcard
.
Init
.
ClockDiv
=
SDIO_CLK_DIV
;
HAL_SD_DeInit
(
&
hsdcard
);
if
(
HAL_SD_Init
(
&
hsdcard
)
!=
HAL_OK
)
{
rt_kprintf
(
"HAL_SD_Init error
\n
"
);
return
RT_EIO
;
}
HAL_NVIC_SetPriority
(
SDIO_IRQn
,
1
,
0
);
HAL_NVIC_EnableIRQ
(
SDIO_IRQn
);
if
(
HAL_SD_ConfigWideBusOperation
(
&
hsdcard
,
SDIO_BUS_WIDE_4B
)
!=
HAL_OK
)
{
rt_kprintf
(
"HAL_SD_ConfigWideBusOperation error
\n
"
);
return
RT_EIO
;
}
return
RT_EOK
;
}
static
rt_err_t
stm32_sdcard_open
(
rt_device_t
dev
,
rt_uint16_t
oflag
)
{
return
RT_EOK
;
}
static
rt_err_t
stm32_sdcard_close
(
rt_device_t
dev
)
{
return
RT_EOK
;
}
static
rt_size_t
stm32_sdcard_read
(
rt_device_t
dev
,
rt_off_t
pos
,
void
*
buffer
,
rt_size_t
size
)
{
int
ret
=
RT_EOK
;
rt_sem_take
(
&
sd_lock
,
RT_WAITING_FOREVER
);
ret
=
stm32_read_blocks
((
uint32_t
*
)
buffer
,
pos
,
size
);
rt_sem_release
(
&
sd_lock
);
if
(
ret
!=
RT_EOK
)
{
return
0
;
}
return
size
;
}
static
rt_size_t
stm32_sdcard_write
(
rt_device_t
dev
,
rt_off_t
pos
,
const
void
*
buffer
,
rt_size_t
size
)
{
int
ret
=
RT_EOK
;
rt_sem_take
(
&
sd_lock
,
RT_WAITING_FOREVER
);
ret
=
stm32_write_blocks
((
uint32_t
*
)
buffer
,
pos
,
size
);
rt_sem_release
(
&
sd_lock
);
if
(
ret
!=
RT_EOK
)
{
return
0
;
}
return
size
;
}
static
rt_err_t
stm32_sdcard_control
(
rt_device_t
dev
,
int
cmd
,
void
*
args
)
{
RT_ASSERT
(
dev
!=
RT_NULL
);
// RT_DEVICE_CTRL_BLK_GETGEOME
if
(
cmd
==
RT_DEVICE_CTRL_BLK_GETGEOME
)
{
HAL_SD_CardInfoTypeDef
sdcard_info
;
struct
rt_device_blk_geometry
*
geometry
;
HAL_SD_GetCardInfo
(
&
hsdcard
,
&
sdcard_info
);
geometry
=
(
struct
rt_device_blk_geometry
*
)
args
;
geometry
->
bytes_per_sector
=
sdcard_info
.
BlockSize
;
geometry
->
block_size
=
sdcard_info
.
BlockSize
;
geometry
->
sector_count
=
sdcard_info
.
BlockNbr
;
}
return
RT_EOK
;
}
static
struct
rt_device
device
;
int
rt_hw_sdcard_init
(
void
)
{
rt_err_t
ret
=
RT_EOK
;
device
.
type
=
RT_Device_Class_Block
;
device
.
init
=
stm32_sdcard_init
;
device
.
open
=
stm32_sdcard_open
;
device
.
read
=
stm32_sdcard_read
;
device
.
write
=
stm32_sdcard_write
;
device
.
control
=
stm32_sdcard_control
;
device
.
close
=
stm32_sdcard_close
;
ret
=
rt_device_register
(
&
device
,
"sd0"
,
RT_DEVICE_FLAG_REMOVABLE
|
RT_DEVICE_FLAG_RDWR
|
RT_DEVICE_FLAG_STANDALONE
);
if
(
ret
!=
RT_EOK
)
{
return
ret
;
}
return
RT_EOK
;
}
INIT_DEVICE_EXPORT
(
rt_hw_sdcard_init
);
bsp/stm32f10x-HAL/drivers/drv_sdcard.h
0 → 100644
浏览文件 @
7b8a7836
/*
* File : drv_sdcard.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2018-01-13 Liu2guang the first version.
*/
#ifndef __DRV_SDCARD_H_
#define __DRV_SDCARD_H_
int
rt_hw_sdcard_init
(
void
);
#endif
bsp/stm32f10x-HAL/drivers/stm32f1xx_hal_conf.h
浏览文件 @
7b8a7836
...
...
@@ -80,7 +80,9 @@
#define HAL_PWR_MODULE_ENABLED
#define HAL_RCC_MODULE_ENABLED
// #define HAL_RTC_MODULE_ENABLED
// #define HAL_SD_MODULE_ENABLED
#ifdef RT_USING_SDCARD
#define HAL_SD_MODULE_ENABLED
#endif
// #define HAL_SMARTCARD_MODULE_ENABLED
#ifdef RT_USING_SPI
#define HAL_SPI_MODULE_ENABLED
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录