spiExchange/Receive returns strange magic number as a 1st B
Posted: Fri Aug 31, 2012 5:53 pm
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:
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:
Please note that i have spent 4 days on it
Does anybody have any ideas what might be wrong?
Any hints?
Thanks in advance.
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.