资源简介
本代码是为基于RTThread操作系统的STM32F407单片机开发的SD卡驱动,能够进行文件读写功能。
代码片段和文件信息
/*
* @File: drv_sdcard.c
* @Author: tianqf
* @Date: 2018-1-23 17:35:46
*
* @LICENSE:
* The code owner is tianqf. Other personnel may
* not be used or modified.
*
* Change Logs:
* Date Author Notes
* 2018-1-23 tianqf The first version.
*/
#include “drv_sdcard.h“
#include “stm32f4xx.h“
#include “sdio_sd.h“
#ifndef SDCARD_SECTOR_SIZE
#define SDCARD_SECTOR_SIZE 512
#endif
rt_err_t sdcard_init(void)
{
rt_uint8_t ret = SD_OK;
ret = SD_Init();
if (ret == SD_OK)
{
return SD_OK;
}
else
{
return SD_ERROR;
}
}
rt_uint8_t sdcard_read_blocks(rt_uint32_t *buf uint64_t sector rt_uint32_t cnt)
{
rt_uint8_t ret = SD_OK;
#if defined (SD_DMA_MODE)
ret = SD_ReadBlock((uint8_t *)buf sector cnt);
if(ret != SD_OK)
{
return RT_ERROR;
}
ret = SD_WaitReadOperation();
if(ret != SD_OK)
{
return RT_ERROR;
}
#else
ret = SD_ReadBlock((uint8_t *)buf sector cnt);
if(ret != SD_OK)
{
return RT_ERROR;
}
#endif
return RT_EOK;
}
rt_uint8_t sdcard_write_blocks(rt_uint32_t *buf uint64_t sector rt_uint32_t cnt)
{
rt_uint8_t ret = SD_OK;
#if defined (SD_DMA_MODE)
ret = SD_WriteBlock((uint8_t *)buf sector cnt);
if(ret != SD_OK)
{
return RT_ERROR;
}
ret = SD_WaitWriteOperation();
if(ret != SD_OK)
{
return RT_ERROR;
}
#else
ret = SD_WriteBlock((uint8_t *)buf sector cnt);
if(ret != SD_OK)
{
return RT_ERROR;
}
#endif
return RT_EOK;
}
static struct rt_device sdcard_device;
static struct rt_semaphore sd_lock;
/* RT-Thread Device Driver Interface */
static rt_err_t rt_sdcard_init(rt_device_t dev)
{
if (rt_sem_init(&sd_lock “sdlock“ 1 RT_IPC_FLAG_FIFO) != RT_EOK)
{
return RT_ERROR;
}
return RT_EOK;
}
static rt_err_t rt_sdcard_open(rt_device_t dev rt_uint16_t oflag)
{
return RT_EOK;
}
static rt_err_t rt_sdcard_close(rt_device_t dev)
{
return RT_EOK;
}
static rt_uint32_t sdio_buffer[512/sizeof(uint32_t)];
static rt_size_t rt_sdcard_read(rt_device_t dev rt_off_t pos void* buffer rt_size_t size)
{
rt_uint8_t status = SD_OK;
rt_sem_take(&sd_lock RT_WAITING_FOREVER);
if(((rt_uint32_t)buffer & 0x03) != 0)
{
/* non-aligned. */
uint32_t i;
uint64_t sector_adr;
uint8_t* copy_buffer;
sector_adr = (uint64_t)pos*SDCARD_SECTOR_SIZE;
copy_buffer = (uint8_t*)buffer;
for(i=0; i {
status = sdcard_read_blocks(sdio_buffer sector_adr 1);
rt_memcpy(copy_buffer sdio_buffer SDCARD_SECTOR_SIZE);
sector_adr += SDCARD_SECTOR_SIZE;
copy_buffer += SDCARD_SECTOR_SIZE;
}
}
else
{
status =
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 6072 2018-05-25 13:42 drv_sdcard.c
文件 478 2018-05-25 13:42 drv_sdcard.h
文件 90898 2018-05-25 13:42 sdio_sd.c
文件 18540 2018-05-25 13:42 sdio_sd.h
- 上一篇:bme280温湿度大气压强传感器驱动程序
- 下一篇:NS2中输出队列长度!!!
相关资源
- FreeModbus_Slave+STM32F407+USART2代码亲测可
- STM32F407 串口配置 串口1~6 六串口同时
- STM32F407_6个串口同时使用的代码
- STM32F407实时时钟_DS1302实时时钟模块和
- stm32f407读写flash的demo,基于正点原子
- 基于STM32F407的视频采集与传输系统设
- STM32 3.5固件库带sdio版本V4.5.0 bug修改
- STM32F407ZG_ESP8266例程
- Keil5下的STM32F407程序
- USART基于hal库stm32f407串口通讯.rar
- 完整的STM32F407_MODBUS通信工程代码。
- STM32指针式时钟工程项目
- stm32f407和摇杆制作的控制手柄代码
- STM32F407. 基于PID速度环的步进电机速度
- STM32F407 基于232或485的57&42;步进电机旋
- STM32F407与OV2640的二维码识别实验
- stm32f407+ov5640颜色识别与追踪
- NRF24L01收发程序STM32F407
- STM32F429DISCOVERY外接SD卡使用SDIO4BIT方式
- uCOS-III+STemWin+Fatfs+usb在STM32F407上成功移
- STM32F407串口1-6 CAN1&2测试例程
- 基于STM32F407的MLX90614驱动程序(寄存器
- stm32f405/stm32f407单片机nrf24l01无线通信
- 基于STM32F407的漏电检测系统
- STM32F407ZG接入ONENET平台代码
- STM32F407 SD卡IAP升级
- 基于stm32f407芯片双ADC多通道开启AD转换
- STM32F407ZET/ZGT STEMWIN ILI9341 SPI LCD液晶屏
- stm32f407对ds18b20温度传感器数据的采集
- STM32F407_OLED显示
评论
共有 条评论