嗨,我正在阅读文件从一个新的微型SD卡使用PIC32 EF非密码起动器套件和多媒体扩展板II(MEB II)。我正在读取一个文件并显示在一个128×32的RGB LED显示屏上。它运行良好(计划优化更多的速度)使用波特率低于700千赫,但当我把速度改变到上面的任何初始化后,它被卡住(我会强调它在代码-见SPI2Read())。文件说要初始化卡在400千赫,我这样做。我已经证实波特率确实在一个范围内转换。这些信号看起来很干净,而且相当平直。我的代码张贴在下面,我注意到它被卡在哪里。它只是不断尝试发送,好像卡没有接收或发送任何东西回来。我有什么遗漏了吗?MEB II板上有什么东西会弄脏这个吗?如果有人知道这个,我会很感激的。相关代码如下:
以上来自于百度翻译
以下为原文
Hi, I'm reading files from a new Micro SD card using the PIC32 EF non-crypto Starter Kit and Mul
timedia Expansion Board II (MEB II). I'm reading a file and displaying it on a 128 x 32 RGB LED display. It's working fine (planning on optimizing for more speed) using a baud rate below ~700KHz, but when I change the speed to anything above that after initialization, it gets stuck (I'll highlight it in the code - see SPI2Read()). The documentation says to initialize the card at 400KHz, which I do. I've confirmed that the baud rate does switch with a scope. The signals look clean and fairly square. My code is posted below and I note where it gets stuck. It just keeps trying to transmit as if the card isn't receiving or sending anything back. Is there something I'm missing? Is there something on the MEB II board that could mess this up? If anyone has a clue about this, I would appreciate it. Relevant code posted below:
/***** spi.c *****/
#include "main.h"
unsigned char r, poo;
int t;
void InitSPI(void)
{
int rData, i;
IEC4CLR = 0x1c000; // disable all interrupts
SPI2CON = 0; // Stops and resets the SPI2.
rData = SPI2BUF;// clears the receive buffer
IFS4CLR = 0x1c000; // clear any existing event
IPC35CLR = 0x1f000000; // clear the priority
IPC35SET = 0x0d000000; // Set IPL=3, Subpriority 1
SPI2BRG = 124;// buad rate = PBCLK2 (100MHz) / ((SPI2BRG + 1) / 2) or 400KHz
SPI2STATCLR = 0x40;// clear the Overflow
SPI2CON = 0x8220;// SPI ON, 8 bits transfer, SMP=1, Master mode
// from now on, the device is ready to transmit and receive data
}
void ChangeSPISpeed(unsigned int newspeed)
{
DESELECT
SPI2CON = 0; // Stops and resets the SPI2.
SPI2BRG = 124; // 10 MHz - I've tried other speeds
SPI2CON = 0x8220;// SPI ON, 8 bits transfer, SMP=1, Master mode
SELECT
}
unsigned char SPI2_Read(void)
{
Timer1 = 10;
SPI2BUF = 0xff;
while (!SPI2STATbits.SPITBE && Timer1); /*************** code getting stuck here ***************/
return SPI2BUF;
}
// Transmit a byte via SPI channel 2
void SPI2_Xmit( unsigned char data )
{
SPI2BUF = (unsigned short)data; // place the data in the buffer
while (!SPI2STATbits.SPITBE); // wait until the transmit buffer is empty (data has been sent)
while (SPI2STATbits.SPIRBE); // wait until a byte is received
poo = SPI2BUF; // byte received during transmit is junk
}
// Send a command packet
unsigned char Cmd_Send (
unsigned char cmd, /* Command unsigned char */
unsigned int arg, /* Argument */
unsigned char crc /* CRC */
)
{
if (cmd & 0x80) // if the command is an ACMD (application command), send CMD55 first
{
cmd &= 0x7F; // all ACMDs have the 7th bit set, so mask it out
r = Cmd_Send(CMD55, 0, 0xff); // send CMD55 first
if (r > 1) return r; // response should be 0 or 1
}
DESELECT
SELECT
while(SPI2_Read() != 0xff); // give the SD card some time to prepare for the next instruction and flush the bytes or something
Timer1 = 10;
while (Timer1);
/* Send command packet */
SPI2_Xmit(cmd); /* Command */
SPI2_Xmit((unsigned char)(arg >> 24)); /* Argument[31..24] */
SPI2_Xmit((unsigned char)(arg >> 16)); /* Argument[23..16] */
SPI2_Xmit((unsigned char)(arg >> 8)); /* Argument[15..8] */
SPI2_Xmit((unsigned char)arg); /* Argument[7..0] */
SPI2_Xmit(crc);
t = 10;
do
{
r = SPI2_Read();
}
while ((r & 0x80) && --t); // read until bit 7 is not set
return r;
}