?? sdhal.c
字號:
/****************************************Copyright (c)**************************************************
** Guangzhou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: sdhal.c
** Last modified Date: 2005-3-11
** Last Version: V2.0
** Descriptions: SD/MMC卡讀寫模塊: 硬件抽象層 ---- SPI操作函數
** Soft Packet of SD/MMC Card: hard abstrast layer ---- function of SPI operation
**
**------------------------------------------------------------------------------------------------------
** Created by: Ming Yuan Zheng
** Created date: 2005-1-6
** Version: V1.0
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#include "config.h"
#include "sdconfig.h"
#include "sdhal.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_GPIO();
SD_POWER_OUT();
SD_POWER_OFF(); /* 關閉 SD 卡電源 turn off power of sd card */
SPI_SCK_GPIO();
SPI_SCK_OUT();
SPI_SCK_CLR(); /* SCK 引腳置低 set SCK to zero */
SPI_MISO_GPIO();
SPI_MISO_OUT();
SPI_MISO_CLR(); /* MISO 引腳置低 set MISO to zero */
SPI_MOSI_GPIO();
SPI_MOSI_OUT();
SPI_MOSI_CLR(); /* MOSI 引腳置低 set MOSI to zero */
SPI_CS_GPIO();
SPI_CS_OUT();
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_INIT(); /* 初始化SPI接口 initialize SPI interface */
SD_INSERT_GPIO();
SD_INSERT_IN(); /* 檢測卡完全插入口為輸入口 the port checking card is input */
SD_WP_GPIO();
SD_WP_IN(); /* 寫保護檢測口為輸入口 the port written protect is input */
SPI_CS_SET(); /* CS置高 set CS to high voltage */
SPI_Clk400k(); /* 設置SPI頻率小于等于400kHZ set frequency of SPI below 400kHZ */
SPI_SPCR = (0x01 << 4) + (0x01 << 5); /* 設置SPI接口模式,MSTR = 1,CPOL = 1,CPHA = 0,LSBF=0 */
} /* configure SPI interface */
/*******************************************************************************************************************
** 函數名稱: void SPI_Clk400k() Name: void SPI_Clk400k()
** 功能描述: 設置SPI的時鐘小于400kHZ Function: set the clock of SPI less than 400kHZ
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
********************************************************************************************************************/
void SPI_Clk400k(void)
{
SPI_SPCCR = 128; /* 設置SPI時鐘分頻值為128 Set the value of dividing frequency to 128 */
}
/*******************************************************************************************************************
** 函數名稱: void SPI_ClkToMax() Name: void SPI_ClkToMax()
** 功能描述: 設置SPI的clock到最大值 Function: set the clock of SPI to maximum
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
********************************************************************************************************************/
void SPI_ClkToMax(void)
{
SPI_SPCCR = 8; /* 設置SPI時鐘分頻值為8 Set the value of dividing frequency to 8 */
}
/*******************************************************************************************************************
** 函數名稱: 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 byte)
{
INT8U temp;
SPI_SPDR = byte; /* 發送數據放入SPI數據寄存器 */
while(0 == (SPI_SPSR & 0x80)); /* 等待SPIF置位,即等待數據發送完畢 */
/* wait for SPIF being set, that is, wait for finishing of data being send */
temp = SPI_SPDR;
}
/*******************************************************************************************************************
** 函數名稱: 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)
{
SPI_SPDR = 0xFF;
while(0 == (SPI_SPSR & 0x80)); /* 等待SPIF置位,即等待收到數據 */
/* wait for SPIF being set, that is, wait for being received data */
return(SPI_SPDR); /* 讀取收到的字節 read the byte received */
}
/*******************************************************************************************************************
** 函數名稱: 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 + -