關(guān)于STM32 SPI NSS問(wèn)題的探討
按照標(biāo)準(zhǔn)的SPI協(xié)議,當(dāng)SPI被配置為主機(jī)模式后,通過(guò)SPI對(duì)從設(shè)備進(jìn)行操作時(shí),其NSS應(yīng)該自動(dòng)置低,從而選中(使能)從設(shè)備;一旦不對(duì)從設(shè)備進(jìn)行操作,NSS立刻置為高。
但是,我在實(shí)際調(diào)試過(guò)程中卻發(fā)現(xiàn):STM32 SPI NSS無(wú)法自動(dòng)實(shí)現(xiàn)跳變。 一旦SPI初始化完成并使能SPI,NSS立刻置低,然后保持不變。
這個(gè)問(wèn)題一直無(wú)法解決,直到我在ST官方論壇上看到國(guó)外有些技術(shù)人員也在討論這個(gè)問(wèn)題,他們得出的結(jié)論是:STM32 SPI NSS無(wú)法自動(dòng)跳變。
RichardE | ![]() Posted 24-07-2009 at 16:07 | ||
![]() Registered on : 11-05-2005 From UK (United Kingdom) Messages :19 ![]() OFF-Line |
|
ST官方技術(shù)人員也證實(shí):STM32 SPI NSS是不會(huì)自動(dòng)置位和復(fù)位的。按照官方說(shuō)法,ST已經(jīng)將其列入了改進(jìn)計(jì)劃。
對(duì)于這個(gè)問(wèn)題,可以采用下面的方法解決:
在SPI初始化時(shí),采用NSS soft模式,然后使能NSS輸出功能。從而將NSS當(dāng)做GPIO使用,通過(guò)軟件set和reset來(lái)實(shí)現(xiàn)NSS的置位和復(fù)位。
具體代碼如下:
/* SPI1 configuration ------------------------------------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
/*Enable SPI1.NSS as a GPIO*/
SPI_SSOutputCmd(SPI1, ENABLE);
/*Configure PA.4(NSS)--------------------------------------------*/
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
通過(guò)將NSS配置為GPIO,在通過(guò)SPI操作從設(shè)備時(shí),就可以通過(guò)軟件來(lái)選中和釋放從設(shè)備了。雖然比起硬件自動(dòng)置位要麻煩,但問(wèn)題畢竟解決了。
進(jìn)一步閱讀長(zhǎng)達(dá)900頁(yè)的Manual,我發(fā)現(xiàn),文中對(duì)于SPI hard模式的描述并非大多數(shù)人所認(rèn)為的硬件SPI,原文如下:
Slave select (NSS) pin management
There are two NSS modes:
● Software NSS mode: this mode is enabled by setting the SSM bit in the SPI_CR1
register (see Figure 209). In this mode, the external NSS pin is free for other
application uses and the internal NSS signal level is driven by writing to the SSI bit in
the SPI_CR1 register.
● Hardware NSS mode: there are two cases:
– NSS output is enabled: when the STM32F20xxx is operating as a Master and the
NSS output is enabled through the SSOE bit in the SPI_CR2 register, the NSS pin
is driven low and all the NSS pins of devices connected to the Master NSS pin see
a low level and become slaves when they are configured in NSS hardware mode.
When an SPI wants to broadcast a message, it has to pull NSS low to inform all
others that there is now a master for the bus. If it fails to pull NSS low, this means
that there is another master communicating, and a Hard Fault error occurs.
– NSS output is disabled: the multimaster capability is allowed.
當(dāng)SPI配置為hard模式后,通過(guò)檢測(cè)NSS可以實(shí)現(xiàn)的是自身主機(jī)和從機(jī)模式的切換,而不是大多數(shù)人所認(rèn)為的自動(dòng)NSS。。。也就是說(shuō):在一個(gè)多SPI系統(tǒng)中,STM32 SPI通過(guò)NSS檢測(cè),一旦發(fā)現(xiàn)系統(tǒng)中無(wú)NSS低信號(hào),自己就輸出低,從而成為主機(jī);當(dāng)系統(tǒng)中有NSS低信號(hào)時(shí)(及已經(jīng)有其它SPI宣布為主機(jī)),自己就配置為從機(jī)。 所謂的hard模式的NSS,實(shí)際就是為了實(shí)現(xiàn)多機(jī)間通信的。
小結(jié):
望文生義很可怕,Manual要仔細(xì)研讀。
STM32的SPI NSS不論是配置為soft還是hard都是無(wú)法自動(dòng)置位的,但這卻是大多數(shù)應(yīng)用所需要的。正如ST 論壇上RichardE所說(shuō):“Everything would be done by the peripheral. Fire and forget.”
,有趣的說(shuō)法:Fire and forget! ~~~~
評(píng)論