cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Master Receive problem

wattos
Associate II
Posted on March 31, 2009 at 21:06

SPI Master Receive problem

14 REPLIES 14
wattos
Associate II
Posted on May 17, 2011 at 13:08

Hey,

I think I am not really clear. What I meant in the first post is that I ran that simple loopback SPI example (SPI1 connected to SPI2) and it actually fails for the master. That is why I am really confused

ccowdery9
Associate III
Posted on May 17, 2011 at 13:08

Compare it with my ADC routine, which are via SPI3. They work great!

Code:

u32 touch_transfer(u8 mosi)

{

// sends byte out of SPI3, and returns received 24 bits

u32 miso=0;

TOUCH_NSS_LOW(); // NSS low

miso=touch_SendByte(mosi) <<16; //

miso+=(touch_SendByte(0) <<8);

miso+=touch_SendByte(0);

TOUCH_NSS_HIGH(); // NSS high

return ((miso>>4)&0x0fff); // just return 12 bits

}

u8 touch_SendByte(u8 byte)

{

// flush away any rogue data in rx buffer

if (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == SET) SPI_I2S_ReceiveData(SPI3);

/* Loop while DR register in not empty */

while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET);

/* Send byte through the SPI1 peripheral */

SPI_I2S_SendData(SPI3, byte);

/* Wait to receive a byte */

while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET);

/* Return the byte read from the SPI bus */

return SPI_I2S_ReceiveData(SPI3);

}

init stuff:

/* SPI2 & 3 and I2C1 Periph clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 | RCC_APB1Periph_SPI3 | RCC_APB1Periph_I2C1 , ENABLE);

/* Configure SPI3 pins: SCK , MISO and MOSI - used by Touch. Default alternate function */

GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE); // disable JTAG i/f (GPIO_Remap_SWJ_JTAGDisable should be enough)

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOB, &GPIO_InitStructure);

// Port A - NSS

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Set NSS */

TOUCH_NSS_HIGH();

/* SPI3 Config - Touch*/

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //CPOL=1

SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //CPHA=1

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_InitStructure.SPI_CRCPolynomial = 0x07;

SPI_Init(SPI3, &SPI_InitStructure);

/* SPI3 enable */

SPI_Cmd(SPI3, ENABLE);

Might be a clue in there somewhere!

Chris.

wattos
Associate II
Posted on May 17, 2011 at 13:08

Hey,

Thanks a lot for all your effort. After hours of debugging it turned out to be a problem with the performance stick itself. We used the performance stick along with the prototyping board which gives you easy access to the pins of the stm32. Problem was, that although one connection was labeled as PA6 it was actually not connected to anything. So we connected a small wire directly to the stm32 and now everything works like a charm.

Thanks a lot for the help again

jj
Associate II
Posted on May 17, 2011 at 13:08

Glad you got it - thanks to Chris - must register one note of protest...

''SPI receive works when in Slave mode.'' This of course could NOT have been true/correct - you must have been using a separate port as the ''floating pin'' would have persisted. For all troubleshooting - precision saves hours - best to ''ohm out'' all vital connection paths and to conduct tests which ''truly'' execute upon your suspect pins/functions...

wattos
Associate II
Posted on May 17, 2011 at 13:08

I guess,

However, that was the observable behaviour. The SPI while being in slave mode worked in both cases as only MISO was not being connected. So MOSI was connected and thus gave this weird effect.

I think it was my bad that I assumed that what the datasheet of the hitex protoboard said was true.... but you know, Its usually not the fault of the manufacturer but the fault of the user (especially if he/she has little experience). Thus I was really being confused.

Thanks again for the help 🙂