[求助]
【SD卡读写】FATFS移植到G2553时f_mount跑不通,跪求大神指点
无限卡在这条语句然后程序重跑。
我是从别人的5529的SD卡读写程序想挪到G2里面使用。尽可能把程序改的很简单,然后把引脚寄存器改过来之后,跑不通
5529的HAL_SDCard.c如下
********************************************
#include "msp430.h"
#include "HAL_SDCard.h"
// Pins from MSP430 connected to the SD Card
#define SPI_SIMO BIT1
#define SPI_SOMI BIT2
#define SPI_CLK BIT3
#define SD_CS BIT7
// Ports
#define SPI_SEL P4SEL
#define SPI_DIR P4DIR
#define SPI_OUT P4OUT
#define SPI_REN P4REN
#define SD_CS_SEL P3SEL
#define SD_CS_OUT P3OUT
#define SD_CS_DIR P3DIR
/***************************************************************************//**
* @brief Initialize SD Card
* @param None
* @return None
******************************************************************************/
void SDCard_init(void)
{
// Port initialization for SD Card operation
SPI_SEL |= SPI_CLK + SPI_SOMI + SPI_SIMO;
SPI_DIR |= SPI_CLK + SPI_SIMO;
SPI_REN |= SPI_SOMI; // Pull-Ups on SD Card SOMI
SPI_OUT |= SPI_SOMI; // Certain SD Card Brands need pull-ups
SD_CS_SEL &= ~SD_CS;
SD_CS_OUT |= SD_CS;
SD_CS_DIR |= SD_CS;
// Initialize USCI_B1 for SPI Master operation
UCB1CTL1 |= UCSWRST; // Put state machine in reset
UCB1CTL0 = UCCKPL + UCMSB + UCMST + UCMODE_0 + UCSYNC; // 3-pin, 8-bit SPI master
// Clock polarity select - The inactive state is high
// MSB first
UCB1CTL1 = UCSWRST + UCSSEL_2; // Use SMCLK, keep RESET
UCB1BR0 = 63; // Initial SPI clock must be <400kHz
UCB1BR1 = 0; // f_UCxCLK = 25MHz/63 = 397kHz
UCB1CTL1 &= ~UCSWRST; // Release USCI state machine
UCB1IFG &= ~UCRXIFG;
}
/***************************************************************************//**
* @brief Enable fast SD Card SPI transfers. This function is typically
* called after the initial SD Card setup is done to maximize
* transfer speed.
* @param None
* @return None
******************************************************************************/
void SDCard_fastMode(void)
{
UCB1CTL1 |= UCSWRST; // Put state machine in reset
UCB1BR0 = 2; // f_UCxCLK = 25MHz/2 = 12.5MHz
UCB1BR1 = 0;
UCB1CTL1 &= ~UCSWRST; // Release USCI state machine
}
/***************************************************************************//**
* @brief Read a frame of bytes via SPI
* @param pBuffer Place to store the received bytes
* @param size Indicator of how many bytes to receive
* @return None
******************************************************************************/
void SDCard_readFrame(uint8_t *pBuffer, uint16_t size)
{
uint16_t gie = __get_SR_register() & GIE; // Store current GIE state
__disable_interrupt(); // Make this operation atomic
UCB1IFG &= ~UCRXIFG; // Ensure RXIFG is clear
// Clock the actual data transfer and receive the bytes
while (size--){
while (!(UCB1IFG & UCTXIFG)) ; // Wait while not ready for TX
UCB1TXBUF = 0xff; // Write dummy byte
while (!(UCB1IFG & UCRXIFG)) ; // Wait for RX buffer (full)
*pBuffer++ = UCB1RXBUF;
}
__bis_SR_register(gie); // Restore original GIE state
}
/***************************************************************************//**
* @brief Send a frame of bytes via SPI
* @param pBuffer Place that holds the bytes to send
* @param size Indicator of how many bytes to send
* @return None
******************************************************************************/
void SDCard_sendFrame(uint8_t *pBuffer, uint16_t size)
{
uint16_t gie = __get_SR_register() & GIE; // Store current GIE state
__disable_interrupt(); // Make this operation atomic
// Clock the actual data transfer and send the bytes. Note that we
// intentionally not read out the receive buffer during frame transmission
// in order to optimize transfer speed, however we need to take care of the
// resulting overrun condition.
while (size--){
while (!(UCB1IFG & UCTXIFG)) ; // Wait while not ready for TX
UCB1TXBUF = *pBuffer++; // Write byte
}
while (UCB1STAT & UCBUSY) ; // Wait for all TX/RX to finish
UCB1RXBUF; // Dummy read to empty RX buffer
// and clear any overrun conditions
__bis_SR_register(gie); // Restore original GIE state
}
/***************************************************************************//**
* @brief Set the SD Card's chip-select signal to high
* @param None
* @return None
******************************************************************************/
void SDCard_setCSHigh(void)
{
SD_CS_OUT |= SD_CS;
}
/***************************************************************************//**
* @brief Set the SD Card's chip-select signal to low
* @param None
* @return None
******************************************************************************/
void SDCard_setCSLow(void)
{
SD_CS_OUT &= ~SD_CS;
}
/***************************************************************************//**
* @}
******************************************************************************/
G2553的HAL_SDCard.c如下(根据引脚和寄存器改的)
********************************************
#include "msp430.h"
#include "HAL_SDCard.h"
// Pins from MSP430 connected to the SD Card
#define SPI_SIMO BIT7
#define SPI_SOMI BIT6
#define SPI_CLK BIT5
#define SD_CS BIT4
// Ports
#define SPI_SEL P1SEL
#define SPI_DIR P1DIR
#define SPI_OUT P1OUT
#define SPI_REN P1REN
#define SD_CS_SEL P1SEL
#define SD_CS_OUT P1OUT
#define SD_CS_DIR P1DIR
/***************************************************************************//**
* @brief Initialize SD Card
* @param None
* @return None
******************************************************************************/
void SDCard_init(void)
{
// Port initialization for SD Card operation
SPI_SEL |= SPI_CLK + SPI_SOMI + SPI_SIMO;
SPI_DIR |= SPI_CLK + SPI_SIMO;
SPI_REN |= SPI_SOMI; // Pull-Ups on SD Card SOMI
SPI_OUT |= SPI_SOMI; // Certain SD Card Brands need pull-ups
SD_CS_SEL &= ~SD_CS;
SD_CS_OUT |= SD_CS;
SD_CS_DIR |= SD_CS;
// Initialize USCI_B1 for SPI Master operation
UCB0CTL1 |= UCSWRST; // Put state machine in reset
UCB0CTL0 = UCCKPL + UCMSB + UCMST + UCMODE_0 + UCSYNC; // 3-pin, 8-bit SPI master
// Clock polarity select - The inactive state is high
// MSB first
UCB0CTL1 = UCSWRST + UCSSEL_2; // Use SMCLK, keep RESET
UCB0BR0 =50; // Initial SPI clock must be <400kHz
UCB0BR1 = 0;
UCB0CTL1 &= ~UCSWRST; // Release USCI state machine
IFG2 &= ~UCB0RXIFG;
}
/***************************************************************************//**
* @brief Enable fast SD Card SPI transfers. This function is typically
* called after the initial SD Card setup is done to maximize
* transfer speed.
* @param None
* @return None
******************************************************************************/
void SDCard_fastMode(void)
{
UCB0CTL1 |= UCSWRST; // Put state machine in reset
UCB0BR0 = 2; // f_UCxCLK = 25MHz/2 = 12.5MHz
UCB0BR1 = 0;
UCB0CTL1 &= ~UCSWRST; // Release USCI state machine
}
/***************************************************************************//**
* @brief Read a frame of bytes via SPI
* @param pBuffer Place to store the received bytes
* @param size Indicator of how many bytes to receive
* @return None
******************************************************************************/
void SDCard_readFrame(uint8_t *pBuffer, uint16_t size)
{
uint16_t gie = __get_SR_register() & GIE; // Store current GIE state
__disable_interrupt(); // Make this operation atomic
IFG2 &= ~UCB0RXIFG; // Ensure RXIFG is clear
// Clock the actual data transfer and receive the bytes
while (size--){
while (!(IFG2 & UCB0TXIFG)) ; // Wait while not ready for TX
UCB0TXBUF = 0xff; // Write dummy byte
while (!(IFG2 & UCB0RXIFG)) ; // Wait for RX buffer (full)
*pBuffer++ = UCB0RXBUF;
}
__bis_SR_register(gie); // Restore original GIE state
}
/***************************************************************************//**
* @brief Send a frame of bytes via SPI
* @param pBuffer Place that holds the bytes to send
* @param size Indicator of how many bytes to send
* @return None
******************************************************************************/
void SDCard_sendFrame(uint8_t *pBuffer, uint16_t size)
{
uint16_t gie = __get_SR_register() & GIE; // Store current GIE state
__disable_interrupt(); // Make this operation atomic
// Clock the actual data transfer and send the bytes. Note that we
// intentionally not read out the receive buffer during frame transmission
// in order to optimize transfer speed, however we need to take care of the
// resulting overrun condition.
while (size--){
while (!(IFG2 & UCB0TXIFG)) ; // Wait while not ready for TX
UCB0TXBUF = *pBuffer++; // Write byte
}
while (UCB0STAT & UCBUSY) ; // Wait for all TX/RX to finish
UCB0RXBUF; // Dummy read to empty RX buffer
// and clear any overrun conditions
__bis_SR_register(gie); // Restore original GIE state
}
/***************************************************************************//**
* @brief Set the SD Card's chip-select signal to high
* @param None
* @return None
******************************************************************************/
void SDCard_setCSHigh(void)
{
SD_CS_OUT |= SD_CS;
}
/***************************************************************************//**
* @brief Set the SD Card's chip-select signal to low
* @param None
* @return None
******************************************************************************/
void SDCard_setCSLow(void)
{
SD_CS_OUT &= ~SD_CS;
}
/***************************************************************************//**
* @}
******************************************************************************/
你自己都分析了,一直在那,肯定是count一直为非零,找到给它赋值的地方去查吧
可能是WRITE8_ADV()这个进入了死循环,不然的话,count- - 也会归0的。
抓住救命稻草了。让我瞻仰一下大神您的代码看看能不能直接用
是我粗心了,G2553不能搭载FATFS文件系统,内存放不下,只能用petit fatfs
大神们,可以分享一下G2553读写SD卡的程序吗?初学者请指教