?? sdhal.c
字號:
/*****************************************************************************************************
** Descriptions:sd 卡驅(qū)動軟件包: 硬件抽象層 ---- SPI操作函數(shù)
********************************************************************************************************/
#include "define.h"
/**************************************************************
讀寫SD卡的SPI接口函數(shù): SPI接口設(shè)置,發(fā)送/接收字節(jié)函數(shù)
**************************************************************/
/*
*********************************************************************************************************
** 函數(shù)名稱:SSP_Init()
** 函數(shù)功能:將SSP控制器設(shè)置為主機(jī)SPI。
** 入口參數(shù):無
** 出口參數(shù):無
*********************************************************************************************************
*/
void SPI_INIT(void)
{
SSPCR1 = 0x00;
PINSEL1 |= (0x02 << 2) | //連接SSP模塊
(0x02 << 4) |
(0x02 << 6);
SPI_CS_OUT(); //片選腳設(shè)置為輸出
SPI_CS_SET(); //設(shè)置片選腳為高電平
SSPCR0 = 0x0707; //8位數(shù)據(jù)長度,CPOL = 0, CPHA = 0,SCR=15,SPI模式
SSPCPSR = 0x2; //時鐘分頻器
SSPCR1 = 0x02; //SSP使能
}
/*******************************************************************************************************************
** 函數(shù)名稱: 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)
{
SPI_INIT(); /* 初始化SPI接口 */
SPI_CS_SET(); /* CS置高 */
}
/*******************************************************************************************************************
** 函數(shù)名稱: void SPI_Clk400k() Name: void SPI_Clk400k()
** 功能描述: 設(shè)置SPI的時鐘小于400kHZ Function: set the clock of SPI less than 400kHZ
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
********************************************************************************************************************/
void SPI_Clk400k(void)
{
SSPCPSR = 128; /* 設(shè)置SPI時鐘分頻值為128 Set the value of dividing frequency to 128 */
}
/*******************************************************************************************************************
** 函數(shù)名稱: void SPI_ClkToMax() Name: void SPI_ClkToMax()
** 功能描述: 設(shè)置SPI的clock到最大值 Function: set the clock of SPI to maximum
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
********************************************************************************************************************/
void SPI_ClkToMax(void)
{
SSPCPSR = 8; /* 設(shè)置SPI時鐘分頻值為8 Set the value of dividing frequency to 8 */
}
/*******************************************************************************************************************
** 函數(shù)名稱: void SPI_SendByte() Name: void SPI_SendByte()
** 功能描述: 通過SPI接口發(fā)送一個字節(jié) Function: send a byte by SPI interface
** 輸 入: INT8U byte: 發(fā)送的字節(jié) Input: INT8U byte: the byte that will be send
** 輸 出: 無 Output: NULL
********************************************************************************************************************/
void SPI_SendByte(uint8 byte)
{
uint8 temp;
SSPDR = byte;
while ( (SSPSR & 0x01) == 0 );
temp = SSPDR; /* 刷新 RxFIFO */
}
/*******************************************************************************************************************
** 函數(shù)名稱: INT8U SPI_RecByte() Name: INT8U SPI_RecByte()
** 功能描述: 從SPI接口接收一個字節(jié) Function: receive a byte from SPI interface
** 輸 入: 無 Input: NULL
** 輸 出: 收到的字節(jié) Output: the byte that be received
********************************************************************************************************************/
uint8 SPI_RecByte(void)
{
SSPDR = 0xFF;
while(SSPSR & 0x10); /* 等待SPIF置位,即等待收到數(shù)據(jù) */
/* wait for SPIF being set, that is, wait for being received data */
return(SSPDR); /* 讀取收到的字節(jié) read the byte received */
}
/*******************************************************************************************************************
** 函數(shù)名稱: void SPI_CS_Assert() Name: void SPI_CS_Assert()
** 功能描述: 片選SPI從機(jī) Function: select the SPI slave
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
********************************************************************************************************************/
void SPI_CS_Assert(void)
{
SPI_CS_CLR(); /* 片選SPI從機(jī) select the SPI slave */
}
/*******************************************************************************************************************
** 函數(shù)名稱: void SPI_CS_Deassert() Name: void SPI_CS_Deassert()
** 功能描述: 不片選SPI從機(jī) Function: not select the SPI slave
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
********************************************************************************************************************/
void SPI_CS_Deassert(void)
{
SPI_CS_SET(); /* 不片選SPI從機(jī) not select the SPI slave */
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -