0
votes

The project is collecting Voltage & Current in 3.6Ksps, EMI in 2.4Msps by stm32f4 discovery and send them to Raspberry Pi. The first step is testing data transition from stm32f4 to RPI by spi. I define a unsigned data 111 in stm32f4, I am expecting it showing in RPI, but the print screen in RPI is showing 255 all the time.

Here is the code in stm32f4 as slave mode:

void SPI1_Init(void)
{    
  GPIO_InitTypeDef  GPIO_InitStructure;
  SPI_InitTypeDef  SPI_InitStructure;

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


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_SPI1);
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_SPI1);
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_SPI1);


    RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE);

    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; 
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;    
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;   
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;  
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;  
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Cmd(SPI1, ENABLE);
    SPI1_ReadWriteByte(0xff);    
}   
SPI_BaudRate_Prescaler:SPI_BaudRatePrescaler_2~SPI_BaudRatePrescaler_256  

void SPI1_SetSpeed(u8 SPI_BaudRatePrescaler)
{
  assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
    SPI1->CR1&=0XFFC7;
    SPI1->CR1|=SPI_BaudRatePrescaler;
    SPI_Cmd(SPI1,ENABLE);
} 

u8 SPI1_ReadWriteByte(u8 TxData)
{                    

  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){}

    SPI_I2S_SendData(SPI1, TxData);
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){}

    return SPI_I2S_ReceiveData(SPI1);
}

int main(void)
{ 
    u8 adcx=111;
    SPI1_Init();
    while(1)
    { 
        if(SPI1_ReadWriteByte(adcx))
        {
            SPI1_ReadWriteByte(adcx);               //send out data
        }
    }
}

Here is the code in RPI as Master mode in python:

import spidev
import time

spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 25000000
spi.lsbfirst = False
spi.mode = 0b11


while True:
    resp = spi.xfer([0x01])
    print(resp[0])
    time.sleep(1)
1
"But it seems like something wrong with the code." Is not even close to a specific problem description. Use a debugger to find the actualy problem.too honest for this site
And use stdint.h types, not these 1990ies homebrew names like u8. C has evolved since these days.too honest for this site
I am sorry for your confusion, the problem is that the screen in RPI shows 255 all the time, I am expecting 111 as it is defined in stm32f4. Thanks for your advice!zb1872

1 Answers

0
votes

I'm pretty sure that your raspberry pi is too fast. Try a much slower SPI speed.

In my personal experience I have not been able to make the stm32f4 faster than 7 Mhz as SPI Slave and even that only with DMA.

If your application permits it, go to 100 kHz and it should work fine.