SPI_I2S_ReceiveData始终返回0xff或0x00

时间:2015-10-12 10:17:31

标签: c embedded stm32

我想创建固件到stm32f4发现,当电路板移动时闪烁灯光。但是SPI_I2S_ReceiveData总是返回0xff或0x00。我认为问题出在我的SPI初始化中,但我不知道它究竟在哪里。

这是我的代码。

#include <stm32f4xx.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_spi.h>
#include <stm32f4xx_i2c.h>

void InitLEDs(void);

void InitSPI(void);

void Delay(int iTicks);

void DiodeFlash(void);

unsigned short ReadACCELEROMETER(void);

unsigned char WriteSPI(unsigned char cbData);

int main(void)
{
    unsigned short cbPrevRead = 0;
    unsigned short cbCurrentRead = 0;

    InitLEDs();
    InitSPI();

    while(1)
    {
        cbCurrentRead = ReadACCELEROMETER();

        if (cbCurrentRead != cbPrevRead)
        {
            DiodeFlash();

            cbPrevRead = cbCurrentRead;
        }
    }
}


void InitLEDs(void)
{
    GPIO_InitTypeDef PortD;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

    PortD.GPIO_Mode = GPIO_Mode_OUT;
    PortD.GPIO_OType = GPIO_OType_PP;
    PortD.GPIO_PuPd = GPIO_PuPd_NOPULL;
    PortD.GPIO_Speed = GPIO_Speed_100MHz;
    PortD.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

    GPIO_Init(GPIOD, &PortD);
}


void InitSPI(void)
{
    GPIO_InitTypeDef PortA;
    GPIO_InitTypeDef PortE;

    SPI_InitTypeDef SPI1Conf;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

    PortA.GPIO_Mode = GPIO_Mode_AF;
    PortA.GPIO_OType = GPIO_OType_PP;
    PortA.GPIO_Speed = GPIO_Speed_50MHz;
    PortA.GPIO_PuPd = GPIO_PuPd_NOPULL;
    PortA.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;

    GPIO_Init(GPIOA, &PortA);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);


    PortE.GPIO_Mode = GPIO_Mode_OUT;
    PortE.GPIO_OType = GPIO_OType_PP;
    PortE.GPIO_Speed = GPIO_Speed_50MHz;
    PortE.GPIO_PuPd = GPIO_PuPd_NOPULL;
    PortE.GPIO_Pin = GPIO_Pin_3;

    GPIO_Init(GPIOE, &PortE);

    GPIOE->ODR = 0x0008; // Disable CS

    SPI1Conf.SPI_DataSize = SPI_DataSize_8b;
    SPI1Conf.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
    SPI1Conf.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI1Conf.SPI_FirstBit = SPI_FirstBit_MSB; // ACCELEROMETER PROTOCOL
    SPI1Conf.SPI_Mode = SPI_Mode_Master;
    SPI1Conf.SPI_CPHA = SPI_CPHA_2Edge;
    SPI1Conf.SPI_CPOL = SPI_CPOL_High;
    SPI1Conf.SPI_CRCPolynomial = 7;
    SPI1Conf.SPI_NSS = SPI_NSS_Soft;
    SPI_Init(SPI1, &SPI1Conf);
    SPI_Cmd(SPI1, ENABLE);

    WriteSPI(0x23); WriteSPI(0xc9);
    WriteSPI(0x20); WriteSPI(0x97);
    WriteSPI(0x24); WriteSPI(0x00);

    WriteSPI(0x10); WriteSPI(0x00);
    WriteSPI(0x11); WriteSPI(0x00);
    WriteSPI(0x12); WriteSPI(0x00);
}


void Delay(int iTicks)
{
    while ((iTicks--) > 0);
}


void DiodeFlash(void)
{
    GPIO_Write(GPIOD, 1UL << 12);
    Delay(100000);
    GPIO_Write(GPIOD, 1UL << 13);
    Delay(100000);
    GPIO_Write(GPIOD, 1UL << 14);
    Delay(100000);
    GPIO_Write(GPIOD, 1UL << 15);
    Delay(100000);
    GPIO_Write(GPIOD, 0x0000);
}

unsigned short ReadACCELEROMETER(void)
{
    unsigned short nAxisX = 0x0000;
    unsigned short nAxisY = 0x0000;
    unsigned short nAxisZ = 0x0000;

    unsigned char cbAddress = 0x80;

    //**********************************************************
    // Forming X Value
    WriteSPI(cbAddress | 0x0f);
    nAxisX |= WriteSPI(0x00);

    WriteSPI(cbAddress | 0x2A);
    nAxisX |= WriteSPI(0x00) << 8;

    //**********************************************************
    // Forming Y Value
    WriteSPI(cbAddress | 0x2B);
    nAxisX |= WriteSPI(0x00);

    WriteSPI(cbAddress | 0x2C);
    nAxisX |= WriteSPI(0x00) << 8;

    //**********************************************************
    // Forming Z Value
    WriteSPI(cbAddress | 0x2D);
    nAxisX |= WriteSPI(0x00);

    WriteSPI(cbAddress | 0x2E);
    nAxisX |= WriteSPI(0x00) << 8;

    return nAxisX ^ nAxisY ^ nAxisZ;
}

unsigned char WriteSPI(unsigned char cbData)
{
    unsigned char cbResult = 0x00;

    GPIOE->ODR = 0x0000; // Enable CS

    // Wait for ready status
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);

    SPI_I2S_SendData(SPI1, cbData);

    // Wait for ready status
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);

    cbResult = SPI_I2S_ReceiveData(SPI1);

    GPIOE->ODR = 0x0008; // Disable CS

    return cbResult;
}

3 个答案:

答案 0 :(得分:3)

首先,您没有指定加速度计。我猜,它是ST LISxx 在这种情况下,加速度计的数据传输不正确。

纠正写入顺序:
- 激活chipselect
- 写入寄存器号码
- 写入寄存器值
- 停用chipselect
使用类似的顺序读取每个寄存器。

接下来,ST不推荐你的算法用于低级SPI传输:

  

请勿使用BSY标志来处理每次数据传输或接收。最好使用TXE和RXNE标志

(参见参考手册)。简单地使用

SPIx->DR = out;  
while (!(SPIx->SR & SPI_SR_RXNE)) ;  
return SPIx->DR;  

另外,我正在使用LOL3DH,CPOL = 0&amp; CPHA = 0 SPI设置(我不知道,它如何与您的CPOL = 1,CPHA = 1一起使用)。

提示:要检查SPI通信,请尝试阅读WHO_AM_I寄存器 - 启用alawys且始终具有已知值。

答案 1 :(得分:1)

你写了寄存器的地址,取消选择你的​​设备后,所以写的地址是清理的。在这种情况下,你必须做这样的事情。

unsigned char WriteSPI(unsigned char cbData)
{
    unsigned char cbResult = 0x00;

    // Wait for ready status
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);

    SPI_I2S_SendData(SPI1, cbData);

    // Wait for ready status
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);

    cbResult = SPI_I2S_ReceiveData(SPI1);

    return cbResult;
}


unsigned char WriteReg(unsigned char cbAddress, unsigned char cbData)
{
    unsigned char cbResult = 0x00;

    GPIOE->ODR = 0x0000; // select device
    WriteSPI(cbAddress);
    cbResult = WriteSPI(cbData);
    GPIOE->ODR = 0x0008; // deselect device

    return cbResult;
}

unsigned char ReadReg(unsigned char cbAddress)
{
    unsigned char cbResult = 0x00;

    GPIOE->ODR = 0x0000; // select device
    WriteSPI(cbAddress);
    cbResult = WriteSPI(0x00);
    GPIOE->ODR = 0x0008; // deselect device

    return cbResult;
}

答案 2 :(得分:1)

您的SPI初始化没有任何问题。主要的错误是写入寄存器地址。在读取寄存器值之前,不得取消选择器件,而必须执行写入地址 - 一次读取读取值。这意味着

  1. 选择设备。

  2. 写入寄存器地址。

  3. 读取寄存器值。

  4. 取消选择设备。