cancel
Showing results for 
Search instead for 
Did you mean: 

Internal Working of SPI?

Sandeep Dhokne
Associate II
Posted on July 26, 2017 at 12:15

Hi,

When i checked on DSO  (scope)  i found that after chip select SPI_NSS latch to ground.

there in clock signals on clk pin.

there is no data on SI and SO pin.

Why there no data on SPI's Pin?

Why SPI_NSS latch to ground? 

Anybody please help.

What is the internal working of HAL SPI protocol?

Initialization of SPI

void MX_SPI1_Init(void)

{

hspi1.Instance = SPI1;

hspi1.Init.Mode = SPI_MODE_MASTER;

hspi1.Init.Direction = SPI_DIRECTION_2LINES;

hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;

hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;

hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;

hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi1.Init.TIMode = SPI_TIMODE_DISABLED;

hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;

hspi1.Init.CRCPolynomial = 10;

HAL_SPI_Init(&hspi1);

}

My Code:

void InitMemory(void)

{

PageAdd = 0; 

ByteAdd = 0;

ByteCount = 1;

ReadMemory(&TotalReportNo, PageAdd, ByteAdd, ByteCount);

ReportNo = TotalReportNo + 1;

}

void ReadMemory(UCHAR *bData, UINT PageId, UINT ByteId, UINT NoOfBytes)

{

int Status;

Status = 1;

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

while(Status != HAL_OK)

{

Status = HAL_SPI_TransmitReceive(&hspi1,(uint8_t*)(0xd2),(uint8_t*)(&aRxBuffer),1,1000);

}

while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);

//

--------------------------------------------------------------------------------------------------------------------------------------------------------------

while(Status != HAL_OK)

{

Status = HAL_SPI_TransmitReceive(&hspi1,(uint8_t*)((UCHAR)((PageId>>7) & 0xFF)),(uint8_t*)(&aRxBuffer),1,1000);

}

while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);

//

--------------------------------------------------------------------------------------------------------------------------------------------------------------

while(Status != HAL_OK)

{

Status = HAL_SPI_TransmitReceive(&hspi1,(uint8_t*)((UCHAR)((PageId<<1)|(ByteId>>8)) & 0xFF),(uint8_t*)(&aRxBuffer),1,1000);

}

while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);

//

--------------------------------------------------------------------------------------------------------------------------------------------------------------

while(Status != HAL_OK)

{

Status = HAL_SPI_TransmitReceive(&hspi1,(uint8_t*)((UCHAR)(ByteId & 0xFF)),(uint8_t*)(&aRxBuffer),1,1000);

}

while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);

//

--------------------------------------------------------------------------------------------------------------------------------------------------------------

while(Status != HAL_OK)

{

Status = HAL_SPI_TransmitReceive(&hspi1,(uint8_t*)(0x00),(uint8_t*)(&bData),NoOfBytes,1000);

}

while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);

//

--------------------------------------------------------------------------------------------------------------------------------------------------------------

}

Thank you.

16 REPLIES 16
Posted on July 28, 2017 at 07:51

Thank you.

Posted on July 28, 2017 at 13:45

Thank you.

I did the changes as per your suggestions.

Now i received the clock signal on SPI CLK pin,

But till SPI NSS problem is persist.

As soon as i call SPI write function SPI_NSS

 

latch to ground permanently.

Power ON Status of SPI_NSS

 

is High (3.3V).

 

Posted on July 28, 2017 at 14:48

Hello Sandeep. 

This is the normal behaviour of NSS.

When you are in master mode NSS as output, this pin is held LOW to ''Declare'' to other devices(with same capability) in bus that ''I am the Master''

In slave mode acts ''as chip select'',

0690X0000060409QAA.jpg0690X00000603z6QAA.jpg

read also next paragraphs!(The NSS signal is driven low when the master starts the communication and is kept low until the SPI is disabled'')

In Atmel memory there is not NSS pin.(because allways acts as a slave)

I supose your intention is  to connect NSS PIN (in Master mode ) to CS pin at your Atmel memory, and the MCU to take the responsibility for the rest(timings) .

MCU , dont know the timings of your chip or any other chip. So it can not be done automaticaly. (you must develop some code or use it as an ordinary pin)   The  timing of CS pin of your device is your responsibility.

Posted on July 30, 2017 at 06:28

Thank you.

Shall i use any other pin as chip select in this case?(In master mode)

is it work(SPI Master)?

Posted on July 30, 2017 at 07:41

Hi,

The problem I'm having right now is that the NSS pin is always low. Clock and MOSI are perfect. What I would like is that the NSS is high, and goes low during the transmission, then high again.

Posted on July 30, 2017 at 08:11

If your memory does not have a chip select, does it have a reset pin? At any time, and in particular during communication, an ESD or else could generate a glitch on clock or data line, desynchronizing the bus until the slave is resynchronized. Use stm32 nss as gpio reset signal or, if output current allows, as supply voltage to the memory (you can group gpio to increase max current, then there is a limit on the mcu vdd current). In both cases, nss is not used as spi hw function.

Posted on August 01, 2017 at 14:39

You can use the same pin only configured as a push/pull GPIO output.