0
\$\begingroup\$

I am working with a sensor which communicates through SPI. I am

trying to read MISO line, but it returns 0xFFFF. first I send 0xC000 on

MOSI and the sensor places 0x0032 on MISO. I read MISO using oscilloscope,

but in code, this value is 0xFFFF. I tried to send dummy data but nothing

changed. here is my code:

#include "stm32f10x_spi.h"
#include "delay.h"

#define SPIx_RCC      RCC_APB2Periph_SPI1
#define SPIx          SPI1
#define SPI_GPIO_RCC  RCC_APB2Periph_GPIOA
#define SPI_GPIO      GPIOA
#define SPI_PIN_MOSI  GPIO_Pin_7
#define SPI_PIN_MISO  GPIO_Pin_6
#define SPI_PIN_SCK   GPIO_Pin_5
#define SPI_PIN_SS    GPIO_Pin_4

uint16_t data;

SPI_InitTypeDef SPI_InitStructure; 
GPIO_InitTypeDef GPIO_InitStruct;
int main(void)
{
RCC_APB2PeriphClockCmd(SPIx_RCC, ENABLE);
SPI_InitStructure.SPI_Mode=SPI_Mode_Master;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_NSS=SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPIx, ENABLE);

  RCC_APB2PeriphClockCmd(SPI_GPIO_RCC, ENABLE);
// GPIO pins for MOSI, MISO, and SCK
GPIO_InitStruct.GPIO_Pin = SPI_PIN_MOSI | SPI_PIN_MISO | SPI_PIN_SCK;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_GPIO, &GPIO_InitStruct);

  GPIO_InitStruct.GPIO_Pin = SPI_PIN_SS;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_GPIO, &GPIO_InitStruct);


RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);

while(1){
    SPI_GPIO->BRR = SPI_PIN_SS;
    SPI_I2S_SendData(SPIx, 0x4B01);
    while (!(SPIx->SR & (SPI_I2S_FLAG_TXE)));
    SPI_GPIO->BSRR = SPI_PIN_SS;
    // delay for applying setting stably
    delay_ms(2);
    SPI_GPIO->BRR = SPI_PIN_SS;
    SPI_I2S_SendData(SPIx, 0x4C00);
    while (!(SPIx->SR & (SPI_I2S_FLAG_TXE)));
    //while (SPIx->SR & (SPI_I2S_FLAG_BSY));
    SPI_GPIO->BSRR = SPI_PIN_SS;
    delay_ms(2);

    SPI_GPIO->BRR = SPI_PIN_SS;
    SPI_I2S_SendData(SPIx, 0xC000);
    //SPI_I2S_SendData(SPIx, 0x8000);
    while (!(SPIx->SR & (SPI_I2S_FLAG_TXE)));
    while (!(SPIx->SR & (SPI_I2S_FLAG_RXNE)));
    data=SPI_I2S_ReceiveData(SPIx);
    // data is always 0xffff which should be 0x0032
    SPI_GPIO->BSRR = SPI_PIN_SS;
    delay_ms(1);
    }}

Thanks.

\$\endgroup\$
  • 2
    \$\begingroup\$ Error is in code not shown, e.g. pin configuration. \$\endgroup\$ – Turbo J Aug 26 '18 at 11:20
  • \$\begingroup\$ Thanks @TurboJ '#define SPI_PIN_SS GPIO_Pin_4' . I am using port A. \$\endgroup\$ – Mr. Nobody Aug 26 '18 at 11:20
  • \$\begingroup\$ Dear @TurboJ , as I mentioned, I checked the MISO signal using oscilloscope and read the correct data, but in code, I can't capture the right value. It means pin configurations is OK. \$\endgroup\$ – Mr. Nobody Aug 26 '18 at 11:23
  • 1
    \$\begingroup\$ You're not showing how (or even if) you're configuring the port pins. \$\endgroup\$ – brhans Aug 26 '18 at 13:11
  • \$\begingroup\$ Dear @brhans , I edited the question and inserted full code. \$\endgroup\$ – Mr. Nobody Aug 26 '18 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.