jianghuai/SYSTEM/sd.h
2024-05-07 09:36:09 +08:00

129 lines
3.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*-------------------------------------------------*/
/* SD卡头文件 */
/*-------------------------------------------------*/
#ifndef _SD_H_
#define _SD_H_
#include "stm32f10x.h"
#include "stdio.h"
#include "string.h"
// SD卡类型定义
#define SD_TYPE_ERR 0X00
#define SD_TYPE_MMC 0X01
#define SD_TYPE_V1 0X02
#define SD_TYPE_V2 0X04
#define SD_TYPE_V2HC 0X06
// SD卡指令表
#define CMD0 0 //卡复位
#define CMD1 1
#define CMD8 8 //命令8 SEND_IF_COND
#define CMD9 9 //命令9 读CSD数据
#define CMD10 10 //命令10读CID数据
#define CMD12 12 //命令12停止数据传输
#define CMD16 16 //命令16设置SectorSize 应返回0x00
#define CMD17 17 //命令17读sector
#define CMD18 18 //命令18读Multi sector
#define CMD23 23 //命令23设置多sector写入前预先擦除N个block
#define CMD24 24 //命令24写sector
#define CMD25 25 //命令25写Multi sector
#define CMD41 41 //命令41应返回0x00
#define CMD55 55 //命令55应返回0x01
#define CMD58 58 //命令58读OCR信息
#define CMD59 59 //命令59使能/禁止CRC应返回0x00
//数据写入回应字意义
#define MSD_DATA_OK 0x05
#define MSD_DATA_CRC_ERROR 0x0B
#define MSD_DATA_WRITE_ERROR 0x0D
#define MSD_DATA_OTHER_ERROR 0xFF
//SD卡回应标记字
#define MSD_RESPONSE_NO_ERROR 0x00
#define MSD_IN_IDLE_STATE 0x01
#define MSD_ERASE_RESET 0x02
#define MSD_ILLEGAL_COMMAND 0x04
#define MSD_COM_CRC_ERROR 0x08
#define MSD_ERASE_SEQUENCE_ERROR 0x10
#define MSD_ADDRESS_ERROR 0x20
#define MSD_PARAMETER_ERROR 0x40
#define MSD_RESPONSE_FAILURE 0xFF
#define SD_CS_PIN (GPIO_Pin_4)
#define SD_CS_PORT GPIOA
#define SD_CS_CTL(x) (x?(GPIO_WriteBit(SD_CS_PORT, SD_CS_PIN, Bit_SET)) : \
(GPIO_WriteBit(SD_CS_PORT, SD_CS_PIN, Bit_RESET)) )
#define SD_INSERT_PIN (GPIO_Pin_4)
#define SD_INSERT_PORT GPIOC
#define SD_CD_INSERT() GPIO_ReadInputDataBit(SD_INSERT_PORT, SD_INSERT_PIN)
extern u8 SD_Type;//SD卡的类型
//函数申明区
u8 SD_SPI_ReadWriteByte(u8 data);
void SD_SPI_SpeedLow(void);
void SD_SPI_SpeedHigh(void);
u8 SD_WaitReady(void); //等待SD卡准备
u8 SD_GetResponse(u8 Response); //获得相应
u8 SD_Init(void); //初始化
u8 SD_ReadDisk(u8*buf,u32 sector,u8 cnt); //读块
u8 SD_WriteDisk(u8*buf,u32 sector,u8 cnt); //写块
u32 SD_GetSectorCount(void); //读扇区数
u8 SD_GetCID(u8 *cid_data); //读SD卡CID
u8 SD_GetCSD(u8 *csd_data); //读SD卡CSD
void SD_SPI_Init(void);
void SD_ReadUserData(void);
void OpenSDFile(void);
int CheckUpload(void);
int programdata(void);
void removeupload(void);
void LED_GPIO_Config(void);
//-------------------------------
void log_printf(char* fmt,...);
#define LOG_DEBUG_TX_SIZE 512
typedef struct
{
unsigned char log_accepted:1;
char server_addr[64];
int server_port;
char server_path[256];
char file_name[64];
int file_length;
int read_length;
char *buff;
int buff_size;
} log_handle_t;
extern log_handle_t log_handle;
extern int check_log_file(void);
extern int log_file_handler(void);
extern void User_Write_Debug(char *loginfo, int loglen);
#endif