我正在尝试实现WINC1500 MLA驱动程序以与ATMEGA2561 MCU一起使用,并且我已经编写了驱动程序代码,并且它被困在“ while(((SPSR&(1 << SPIF))== 0);”行中)。在m2mStub_SpiTxRx函数中。
我不知道为什么它没有进展。我正在为此项目使用Jumpstart ImageCraft IDE。
这是它的实现
void m2mStub_SpiTxRx(uint8_t *p_txBuf,
uint16_t txLen,
uint8_t *p_rxBuf,
uint16_t rxLen)
{
uint16_t byteCount;
uint16_t i;
// Calculate the number of clock cycles necessary, this implies a full-duplex SPI.
byteCount = (txLen >= rxLen) ? txLen : rxLen;
DEBUGOUTF("Calculate the number of clock cycles\n");
DEBUGOUTF("byteCount %d", byteCount, "\n");
DEBUGOUTF("txLen %d", txLen, "\n");
DEBUGOUTF("rxLen %d", rxLen, "\n");
// Read / Transmit.
for (i = 0; i < byteCount; ++i)
{
// Wait for transmitter to be ready. (This is causing the entire thing to crash)
while((SPSR & (1 << SPIF)) == 0);
// Transmit.
if (txLen > 0)
{
// Send data from the transmit buffer.
SPDR = (*p_txBuf++);
--txLen;
}
else
{
// No more Tx data to send, just send something to keep clock active.
SPDR = 0x00U;
}
// Wait for transfer to finish.
while((SPSR & (1 << SPIF)) == 0);
// Send dummy data to slave, so we can read something from it.
SPDR = 0x00U;
// Wait for transfer to finish.
while((SPSR & (1 << SPIF)) == 0);
// Read or throw away data from the slave as required.
if (rxLen > 0)
{
*p_rxBuf++ = SPDR;
--rxLen;
}
else
{
// Clear the registers
volatile uint8_t reg_clear = 0U;
reg_clear = SPDR;
(void)reg_clear;
}
}
}
我没有足够的信息可以肯定地说,但是我的假设是您的SPI连接设置不正确。
特别是,我想您忘记了将/SS
设置为输出,与this problem或this相同。
在数据表中显示:
主模式当SPI配置为主模式时(SPCR中的MSTR为设置),用户可以确定SS引脚的方向。
如果将SS配置为输出,则该引脚为通用输出引脚这不会影响SPI系统。通常,该引脚将驱动SPI从设备的SS引脚。
如果将SS配置为输入,则必须将其保持高电平以确保主SPI操作。如果SS引脚被外围电路驱动为低电平当SPI配置为主机且SS引脚定义为输入,SPI系统将其解释为另一个主设备,选择SPI作为从设备并开始向其发送数据。避免公交车争用时,SPI系统将采取以下措施:
- SPCR中的MSTR位被清除,并且SPI系统成为从机。由于SPI成为从器件,因此MOSI和SCK引脚变为输入。
- SPSR中的SPIF标志被置位,并且如果允许了SPI中断,并且SREG中的I位被置位,则将执行中断程序。
因此,在主模式下使用中断驱动的SPI传输时,并有可能SS被拉低,中断应始终检查MSTR位置1。如果MSTR位已被从属选择清除,用户必须将其设置为重新启用SPI主模式。
因此,您只需要将/SS
引脚配置为输出并设置为高电平,这应该可以解决您的问题。