SPI-SPI主机硬件片选功能使用说明

2023-06-02,,

SPI主机硬件片选功能使用说明

SPI协议最早的标准,是由摩托罗拉公司制定。在协议使用的过程中,根据实际需求可能会进行一些扩展和修改。

在一份由飞思卡尔半导体发布的SPI V4.01版本规范中,对片选引脚做了如下的描述:The SS/ output feature automatically drives the SS/ pin low during transmission to select external devices and drives it high during idle to deselect external devices.

原文中仅对SPI主机,在操作片选引脚上做出了明确的规定,并未对使用SPI接口的从机做出相应的说明。实际测试发现,在需要片选信号参与数据收发时要注意,片选信号线处于空闲时应保持高电平,且在需要进行数据传输时,应能够被SPI主机片选引脚拉低。

SPI主机在使用硬件片选时,除了需要在结构体中配置为硬件模式,还应将片选引脚配置为复用推挽模式,并调用SPI_SSOutputCmd函数,开启对应SPI的硬件片选输出功能。

下面是以SPI1为例,使用硬件控制片选引脚的程序清单,其中PA4为片选引脚。

1. void SPI_FullDuplex_Init(void)

2. {

3. GPIO_InitTypeDef GPIO_InitStructure = {0};

4. SPI_InitTypeDef SPI_InitStructure = {0};

5.

6. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE );

7.

8. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

9. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

10. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

11. GPIO_Init( GPIOA, &GPIO_InitStructure );

12.

13. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

14. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

15. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

16. GPIO_Init( GPIOA, &GPIO_InitStructure );

17.

18. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

19. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

20. GPIO_Init( GPIOA, &GPIO_InitStructure );

21.

22. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

23. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

24. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

25. GPIO_Init( GPIOA, &GPIO_InitStructure );

26.

27. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

28. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

29. SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;

30. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

31. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;

32. SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;

33. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;

34. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;

35. SPI_InitStructure.SPI_CRCPolynomial = 7;

36. SPI_Init( SPI1, &SPI_InitStructure );

37.

38. SPI_SSOutputCmd( SPI1, ENABLE );

39.

40. SPI_Cmd( SPI1, ENABLE );

 

SPI-SPI主机硬件片选功能使用说明的相关教程结束。

《SPI-SPI主机硬件片选功能使用说明.doc》

下载本文的Word格式文档,以方便收藏与打印。