📄 sdhal.c.bak
字号:
/****************************************Copyright (c)**************************************************
** Guangzhou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: sdhal.c
** Last modified Date: 2005-1-6
** Last Version: V1.0
** Descriptions: sd 卡驱动软件包: 硬件抽象层 ---- SPI操作函数
** Soft Packet of SD Card Driver: hard abstrast layer ---- function of SPI operation
**
**------------------------------------------------------------------------------------------------------
** Created by: Ming Yuan Zheng
** Created date: 2005-1-6
** Version: V1.0
** Descriptions: The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#include <SM89516A.H>
#include "INC\CONFIG.H"
#include "INC\SDHAL.H"
#include <intrins.h>
/**************************************************************
读写SD卡的SPI接口函数: SPI接口设置,发送/接收字节函数
**************************************************************/
/*******************************************************************************************************************
** 函数名称: void SD_Power() Name: void SD_Power()
** 功能描述: 对卡先下电,再上电 Function: turn off the card's power, and turn on
** 输 入: 无 Input: NULL
** 输 出: 无 Output: NULL
********************************************************************************************************************/
void SD_Power(void)
{
INT32U i;
SD_POWER_OFF(); /* 关闭 SD 卡电源 turn off power of sd card */
SPI_SCK_CLR(); /* SCK 引脚置低 set SCK to zero */
SPI_MISO_CLR(); /* MISO 引脚置低 set MISO to zero */
SPI_MOSI_CLR(); /* MOSI 引脚置低 set MOSI to zero */
SPI_CS_CLR(); /* CS 引脚置低 set CS to zero */
for(i = 0; i < 0x9000; i++); /* 关闭电源延时 delay after turn off power of sd card */
SD_POWER_ON(); /* 打开 SD 卡电源 turn on power of sd card */
}
/*******************************************************************************************************************
** 函数名称: void SD_HardWareInit() Name: void SD_HardWareInit()
** 功能描述: 初始化访问SD卡的硬件条件 Function: initialize the hardware condiction that access sd card
** 输 入: 无 Input: NULL
** 输 出: 无 Output: NULL
********************************************************************************************************************/
void SD_HardWareInit(void)
{
SD_Power(); /* 对卡先下电,再上电 turn off power of card, and turn on it */
SPI_CS_SET(); /* CS置高 set CS to high voltage */
}
/*******************************************************************************************************************
** 函数名称: void SPI_SendByte() Name: void SPI_SendByte()
** 功能描述: 通过SPI接口发送一个字节 Function: send a byte by SPI interface
** 输 入: INT8U byte: 发送的字节 Input: INT8U byte: the byte that will be send
** 输 出: 无 Output: NULL
********************************************************************************************************************/
void SPI_SendByte(INT8U c)
{
INT8U i;
SPI_CS = 0;
for(i = 0;i < 8; i++)
{
SPI_SCK = 0;
if ((c & 0x80) == 0)
SPI_MOSI = 0;
else
SPI_MOSI = 1;
SPI_SCK = 1;
c <<= 1;
}
SPI_SCK = 0;
SPI_CS = 1;
}
/*******************************************************************************************************************
** 函数名称: INT8U SPI_RecByte() Name: INT8U SPI_RecByte()
** 功能描述: 从SPI接口接收一个字节 Function: receive a byte from SPI interface
** 输 入: 无 Input: NULL
** 输 出: 收到的字节 Output: the byte that be received
********************************************************************************************************************/
INT8U SPI_RecByte(void)
{
INT8U i=0,ch=0;
// MISO = 1;
SPI_CS = 0;
for(i = 0; i < 8; i++)
{
SPI_SCK = 0;
ch = ch << 1;
SPI_SCK = 1;
if(SPI_MISO==1) ch = ch | 0x01;
}
SPI_SCK = 0;
SPI_CS = 1;
return (ch);
}
/*******************************************************************************************************************
** 函数名称: void SPI_CS_Assert() Name: void SPI_CS_Assert()
** 功能描述: 片选SPI从机 Function: select the SPI slave
** 输 入: 无 Input: NULL
** 输 出: 无 Output: NULL
********************************************************************************************************************/
void SPI_CS_Assert(void)
{
SPI_CS_CLR(); /* 片选SPI从机 select the SPI slave */
}
/*******************************************************************************************************************
** 函数名称: void SPI_CS_Deassert() Name: void SPI_CS_Deassert()
** 功能描述: 不片选SPI从机 Function: not select the SPI slave
** 输 入: 无 Input: NULL
** 输 出: 无 Output: NULL
********************************************************************************************************************/
void SPI_CS_Deassert(void)
{
SPI_CS_SET(); /* 不片选SPI从机 not select the SPI slave */
}
/*******************************************************************************************************************
** 函数名称: void SD_ChkCard() Name: void SD_ChkCard()
** 功能描述: 检测卡是否完全插入 Function: check weather card is insert entirely
** 输 入: 无 Input: NULL
** 输 出: 1: 卡完全插入 0: 卡没有完全插入 Output: 1: insert entirely 0: not insert entirely
********************************************************************************************************************/
INT8U SD_ChkCard(void)
{
if (SD_INSERT_STATUS() != 0)
return 0; /* 未完全插入 not insert entirely */
else
return 1; /* 完全插入 insert entirely */
}
/*******************************************************************************************************************
** 函数名称: void SD_ChkCardWP() Name: void SD_ChkCardWP()
** 功能描述: 检测卡写保护 Function: check weather card is write protect
** 输 入: 无 Input: NULL
** 输 出: 1: 卡已写保护 0: 卡未写保护 Output: 1: insert write protect 0: not write protect
********************************************************************************************************************/
INT8U SD_ChkCardWP(void)
{
if (SD_WP_STATUS() != 0)
return 1; /* 卡写保护 */
else
return 0; /* 卡未写保护 */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -