cancel
Showing results for 
Search instead for 
Did you mean: 

SPI NSS?

Dick Lin
Senior
Posted on July 13, 2018 at 18:36

Hi,

I am working on Nucleo-144 SPI. The NSS GPIO ios PA4. The only way I can have the NSS high is use 

__HAL_SPI_DISABLE() function. The HAL_GPIO_WritePin() just never work.

I can see from the scope to verify it. Doesn't seems to be the right way to turn off the SPI for set NSS high.

Anyone has experience turn NSS high/low?

/**SPI1 GPIO Configuration

PA4 ------> SPI1_NSS

PA5 ------> SPI1_SCK

PA6 ------> SPI1_MISO

PA7 ------> SPI1_MOSI

*/

HAL_GPIO_WritePin( GPIOA, GPIO_PIN_4, GPIO_PIN_SET ); // NSS1 high

__HAL_SPI_DISABLE(m_spi);

Thx

10 REPLIES 10
Andreas Bolsch
Lead II
Posted on July 13, 2018 at 18:58

You can't easily use the same pin with 'alternate' pin setting (that happens if you use the hardware NSS signal) assigned to the SPI interface and with output setting (simple GPIO). Check the corresponding reference manual:

'Alternate function configuration: ... The output buffer is driven by the signals coming from the peripheral (transmitter

enable and data) ...'. Peripheral here means SPI interface.

Either 'automatic' or 'manual', but not both at the same time ...

Posted on July 13, 2018 at 19:06

I am assuming calling HAL_SPI_Transmit/Receive function doesn't automatically turn off NSS. The NSS always high after the function. 

I am looking for a way to turn NSS on/off before/after the HAL function.

Thx

Posted on July 13, 2018 at 19:46

>>

The HAL_GPIO_WritePin() just never work.

Configure the pin as a GPIO. Sounds like you're not doing that properly. Show code, then people don't have to guess.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on July 13, 2018 at 20:03

Here is the code code configing SPI GPIO. Thx

GPIO_InitTypeDef GPIO_InitStruct;

if(spiHandle->Instance==SPI1)

{

/* USER CODE BEGIN SPI1_MspInit 0 */

/* USER CODE END SPI1_MspInit 0 */

/* SPI1 clock enable */

__HAL_RCC_SPI1_CLK_ENABLE();

/**SPI1 GPIO Configuration

PA4 ------> SPI1_NSS

PA5 ------> SPI1_SCK

PA6 ------> SPI1_MISO

PA7 ------> SPI1_MOSI

*/

GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;

GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

Posted on July 13, 2018 at 23:21

Dick Lin wrote:

I am assuming calling HAL_SPI_Transmit/Receive function doesn't automatically turn off NSS. The NSS always high after the function. 

I am looking for a way to turn NSS on/off before/after the HAL function.

'Assuming' isn't very useful. Either it does or it does not. The inactive state of NSS is high, so what's the problem after all?  And, as I said: Either the SPI is configured to activate/deactivate NSS automatically (and NSS as 'alternate function'), or this had to be done manually. In the latter case the SPI *MUST NOT* control NSS, but the pin you intend to use as NSS *MUST* be configured as a normal GPIO.

turboscrew
Senior III
Posted on July 15, 2018 at 09:50

I think you described the way, SPI with HW NSS is supposed to work on STM32s. The SPI HW NSS is, how ever, known to have bugs in some STM32-chips.

From F103 fererence:

NSS output enabled (SSM = 0, SSOE = 1)

This configuration is used only when the device operates in master mode. The

NSS signal is driven low when the master starts the communication and is kept

low until the SPI is disabled.
Posted on July 15, 2018 at 21:10

It's not a mistake.

Contact NSS should work this way.
Posted on July 15, 2018 at 23:45

I kinda of feel that way since in HAL transmit/receive function there is a check to enable the SPI.

This is the only MCU I have been experienced to disable SPI in order to active high CS.

Thx

Posted on July 16, 2018 at 04:08

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // This is NOT GPIO mode

GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;

A pin you configure in AF mode is not going to be controllable via HAL_GPIO_WritePin( )

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..