Cant read SPI Gyro

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
KevlarCoated
Posts: 3
Joined: Thu Dec 01, 2011 2:12 am

Cant read SPI Gyro

Post by KevlarCoated »

Hi,
Im currently trying to interface my STM32F4 discovery board with a silicon sensing CRG20 gyro Datasheet
Normally they output a 6 byte packet but all I am receiving is a 1 byte with 2 bits set (same ones each time) and 5 bytes of 0. I have tested that the SPI reading is working be connecting the MOSI and MISO pins on the discovery board and getting the same input as output.

I have based my code off the tutorial for the board and I dont have a logic analyser to check what is happing

Can anyone give me some advice on how to get this to work? Any help would be greatly appreciated

spi config:

Code: Select all

static const SPIConfig spicfg = {
  spicb,
  /* HW dependent part.*/
  GPIOB,
  12,
  0
  | SPI_CR1_CPHA
  | SPI_CR1_MSTR
  | SPI_CR1_BR_1
  | SPI_CR1_SPE    //Enable SPI
};


SPI initialisation:

Code: Select all

  spiStart(&SPID2, &spicfg);
  palSetPad(GPIOB, 12);
  palSetPadMode(GPIOB, 12, PAL_MODE_OUTPUT_PUSHPULL |
                           PAL_STM32_OSPEED_HIGHEST);           /* NSS.     */
  palSetPadMode(GPIOB, 13, PAL_MODE_ALTERNATE(5) |
                           PAL_STM32_OSPEED_HIGHEST);           /* SCK.     */
  palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(5));              /* MISO.    */
  palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(5) |
                           PAL_STM32_OSPEED_HIGHEST);           /* MOSI.    */


Reading from SPI

Code: Select all

    
    int gyroReq[6]={3,0,0,0,0,252};
    spiSelect(&SPID2);
    for(i=0;i<6;i++){ 
      tempOut[0]=gyroReq[i];
      spiExchange(&SPID2,1,tempOut,tempIn);
      buf[i]=tempIn[0];
    }
     
    spiUnselect(&SPID2);

I did try doing the spiExchange with all 6 bytes but it didnt seem to work to I opted to do it byte by byte (although I would prefer to do it all with one line of code
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Cant read SPI Gyro

Post by Giovanni »

Hi,

I think the problem is in the SPI configuration structure, you must not specify SPI_CR1_SPE because it is handled internally to the driver, by specifying it the SPI is enabled prematurely. You also don't need to specify SPI_CR1_MSTR, it is enforced internally.

This is how it is done in the driver:

Code: Select all

  /* SPI setup and enable.*/
  spip->spi->CR1  = 0;
  spip->spi->CR1  = spip->config->cr1 | SPI_CR1_MSTR | SPI_CR1_SSM |
                    SPI_CR1_SSI;
  spip->spi->CR2  = SPI_CR2_SSOE | SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN;
  spip->spi->CR1 |= SPI_CR1_SPE;


Note that the SPE bit is activated at the very last, after everything else is initialized.

Giovanni
KevlarCoated
Posts: 3
Joined: Thu Dec 01, 2011 2:12 am

Re: Cant read SPI Gyro

Post by KevlarCoated »

Thanks for the advice, I have tried that, config is now just

Code: Select all

static const SPIConfig spicfg = {
  spicb,
  /* HW dependent part.*/
  GPIOB,
  12,
  SPI_CR1_BR_1
};


But still getting the same data out.
From looking at the gyro data sheet the timing diagram for its SPI shows that there be a minimum 5us delay between the spi select and the start of clock, is it likely that this is the problem? And if so is there any way to put in a small delay to fix this?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Cant read SPI Gyro

Post by Giovanni »

Hi,

If you need to insert a small delay you can use the GPT driver, the function gptPolledDelay() allows to insert delays with any resolution, gptStartOneShot() can be used for non polled delays (it throws a callback).

Giovanni
KevlarCoated
Posts: 3
Joined: Thu Dec 01, 2011 2:12 am

Re: Cant read SPI Gyro

Post by KevlarCoated »

Thanks, I ended up making wiht work by selecting and deselecting the spi after each byte, not how the gyros timing diagram shows it should be done but it works so Im happy.
My next issue is making 2 gyros read on the same bus, can this be done by just creating another SPI config using the same SPI port and a different select pin and just having each SPI with a different name or failing that could I just use turning a a regular gpio pad on and off to do the select for the second gyro?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Cant read SPI Gyro

Post by Giovanni »

Hi,

Correct, you need another regular output pad and 2 SPI configs. Just switch using spiStart() (no need to spiStop()) with different configs. Be careful to spuUnselect() the device before switching to the other one.

Giovanni
Post Reply