spiExchange/Receive returns strange magic number as a 1st B

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
[email protected]
Posts: 33
Joined: Sat Aug 25, 2012 9:53 pm
Been thanked: 1 time

spiExchange/Receive returns strange magic number as a 1st B

Post by [email protected] »

Hi all!

I am porting my software from plain ST libs to Chibios. One of the functionality is to communicate through nRF24l01+ transceiver.

I have very strange problem each time i am receiving data (through exchange or receive method) from SPI.
I am getting a magic number as a 1st byte- same all the time. And then after that byte the expected data.
For example when I am expecting to receive 1 byte: 0x01 I am getting 0x0E and then a second byte is the expected byte. So i have to exchange always one more byte than i should.

here is my example:

Code: Select all

   SPIConfig spicfg = {
      NULL,
      GPIOB,
      12,
      SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_CPHA | SPI_CR1_MSTR | SPI_CR1_BIDIOE // | SPI_CR1_CPOL
   };

   /*
    * SPI1 I/O pins setup.
    */
   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) | PAL_STM32_OSPEED_HIGHEST); /* MISO.*/
   palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); /* MOSI.*/
   palSetPad(GPIOB, 12);

   uint8_t tx[32] = { 0 };
   uint8_t rx[32] = { 0 };

   spiAcquireBus (&SPID2);
   spiStart(&SPID2, &spicfg);
   spiSelect(&SPID2);
   tx[0] = 0x22; //MEANS WRITE REGISTER NO 0x02
   tx[1] = 0x01; //SETTING VALUE EXPECTED TO BE RETURNED BELOW
   spiSend(&SPID2, 2, tx);

   spiUnselect(&SPID2);
   spiSelect(&SPID2);
   tx[0] = 0x02; //MEANS READ REGISTER NO 0x02
   tx[1] = 0x00; //JUST IN CASE
   spiExchange(&SPID2, 2, tx, rx); //Should be only one byte, but size was set to 2 because of strange magic number here

   if (rx[0] != 0x01) { //rx[0] IS 0x0E but should be 0x01
      palSetPad(GPIOD, GPIOD_LED5);
      if (rx[1] == 0x01) { //Then second byte has a value expected at 1st byte
         palSetPad(GPIOD, GPIOD_LED3); //LED ON as second value
      }
      while (TRUE) {}//HANGS HERE
   }

   spiUnselect(&SPID2); /* Slave Select de-assertion.       */
   spiReleaseBus(&SPID2); /* Ownership release.               */

   while (TRUE) {
      //Success
      palSetPad(GPIOD, GPIOD_LED6);
   }


The magic number is constant.
Changing the SPIConfig bus parameters (SPI_CR1_*) does not make any difference (except total spi communication failure- no communication at all or data corruption when speed is seet too high).

Unfortunately i do not have the SPI analyzer.
The hardware failure is not possible as on my old software everything works well.

My environment:
1. chibios trunk
2. STM32F4 Discovery
3. GNU Toolchain 4.6.2

Additional info- a part of my mcuconf.h:

Code: Select all

/*
 * SPI driver system settings.
 */
#define STM32_SPI_USE_SPI1                  FALSE
#define STM32_SPI_USE_SPI2                  TRUE
#define STM32_SPI_USE_SPI3                  FALSE
#define STM32_SPI_SPI1_RX_DMA_STREAM        STM32_DMA_STREAM_ID(2, 0)
#define STM32_SPI_SPI1_TX_DMA_STREAM        STM32_DMA_STREAM_ID(2, 3)
#define STM32_SPI_SPI2_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 3)
#define STM32_SPI_SPI2_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 4)
#define STM32_SPI_SPI3_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 0)
#define STM32_SPI_SPI3_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 7)
#define STM32_SPI_SPI1_DMA_PRIORITY         1
#define STM32_SPI_SPI2_DMA_PRIORITY         1
#define STM32_SPI_SPI3_DMA_PRIORITY         1
#define STM32_SPI_SPI1_IRQ_PRIORITY         10
#define STM32_SPI_SPI2_IRQ_PRIORITY         10
#define STM32_SPI_SPI3_IRQ_PRIORITY         10
#define STM32_SPI_DMA_ERROR_HOOK(spip)      chSysHalt()



Please note that i have spent 4 days on it :)


Does anybody have any ideas what might be wrong?
Any hints?

Thanks in advance.
Last edited by [email protected] on Fri Aug 31, 2012 6:55 pm, edited 3 times in total.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: spiExchange/Receive returns strange magic number as a 1s

Post by Giovanni »

Exchange transmits and receives [*]at the same time, are you sure you should not first transmit the command then receive the data?

Giovanni
[email protected]
Posts: 33
Joined: Sat Aug 25, 2012 9:53 pm
Been thanked: 1 time

Re: spiExchange/Receive returns strange magic number as a 1s

Post by [email protected] »

Hi Giovanni- once again!

1st of all thank you for helping me once again!

Replacing exchange to 2 separate operations: send and receive doesn't make huge difference. I am still getting 0x0E but w/o excepted bits after 0x0E.
The exchange was changed to:

Code: Select all

   spiSend(&SPID2, 1, tx); /* Atomic transfer operations.      */
   spiUnselect(&SPID2); /* Slave Select de-assertion.       */
   spiSelect(&SPID2); /* Slave Select assertion.          */
   spiReceive(&SPID2, 2, rx);


I do believe that exchange is doing what i need as replacing it to send/receive got it worse.

What i am doing is to write command to nRF24l01+ registry which should produce the value on output.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: spiExchange/Receive returns strange magic number as a 1s

Post by Giovanni »

Try select, transmit, receive, unselect. If you deassert the CS the device will stop the transaction.

Giovanni
[email protected]
Posts: 33
Joined: Sat Aug 25, 2012 9:53 pm
Been thanked: 1 time

Re: spiExchange/Receive returns strange magic number as a 1s

Post by [email protected] »

You are right! :)
Great!

Thanks again!

Piotr
Post Reply