STM32F4板上的SPI从器件设置 [英] SPI Slave setup on STM32F4 board

查看:443
本文介绍了STM32F4板上的SPI从器件设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Master& amp;中的SPI在两个STM32F4发现板之间进行通信.从站配置.我已经有了主服务器的代码,但是对于需要对从服务器的SPI初始化进行的更改,我有些困惑.

I am trying to communicate between two STM32F4 discovery boards via SPI in Master & Slave configuration. I already have the code for the master but I am a bit confused on the changes I need to make on the SPI initialisation for the slave.

我还想在主机发送数据时实现一个中断,而不是让从机一直轮询RXNE寄存器.但是,我不确定SPI的NVIC的确切配置.

I would also like to implement an interrupt whenever the master is sending data, rather than having the slave poll the RXNE register all the time. However, I am unsure of the exact configurations for the NVIC for the SPI.

下面是主机的配置代码

void init_SPI1(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    SPI_InitTypeDef SPI_InitStruct;

    // enable clock for used IO pins
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

    /* configure pins used by SPI1
     * PA5 = SCK
     * PA6 = MISO
     * PA7 = MOSI
     */
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    // connect SPI1 pins to SPI alternate function
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

    // enable clock for used IO pins
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

    /* Configure the chip select pin
       in this case we will use PE7 */
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOE, &GPIO_InitStruct);

    GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 high

    // enable peripheral clock
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

    /* configure SPI1 in Mode 0 
     * CPOL = 0 --> clock is low when idle
     * CPHA = 0 --> data is sampled at the first edge
     */
    SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
    SPI_InitStruct.SPI_Mode = SPI_Mode_Master;     // transmit in master mode, NSS pin has to be always high
    SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
    SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;        // clock is low when idle
    SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;      // data sampled at first edge
    SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set; // set the NSS management to internal and pull internal NSS high
    SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; // SPI frequency is APB2 frequency / 4
    SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
    SPI_Init(SPI1, &SPI_InitStruct); 

    SPI_Cmd(SPI1, ENABLE); // enable SPI1
}

参考手册指出,我需要将CPOL和CPHA位配置为与主机相同,这对于MSB/LSB第一帧格式也是如此.除此之外,我不确定如何配置其余部分.

The reference manual states that I need the CPOL and CPHA bits to be configured the same as the master, this is also true for MSB/LSB first frame format. Apart from this I am unsure on how to configure the rest.

推荐答案

您应该将模式更改为SPI_Mode_Slave(顺便说一句,SPI_Mode_Master表示SPI_NSSInternalSoft_Set),根据您所选择的从属选择方法设置SPI_NSS将要使用:

You should change mode to SPI_Mode_Slave (by the way, SPI_Mode_Master implies SPI_NSSInternalSoft_Set), set SPI_NSS based on slave select method you are going to use:

  • 如果使用SPI_NSS_Hard,则将相应的引脚配置为带有上拉的AF/OD(如果没有外部上拉电阻),然后使用GPIO_PinAFConfig将其连接到AF.
  • 如果您只有一个从站,请使用SPI_NSS_Soft:它将被自动选择(如果您不设置SPI_NSSInternalSoft_Set).
  • 如果您使用非标准引脚进行从设备选择,请配置EXTI以接收从设备选择中断,并通过软件用SPI_NSSInternalSoft_Reset重置内部NSS.
  • If you use SPI_NSS_Hard, configure appropriate pin as AF/OD with pull-up (if you haven't external pull-up resistor) and connect it to AF using GPIO_PinAFConfig.
  • If you have only one slave, use SPI_NSS_Soft: it'll be automatically selected (if you won't set SPI_NSSInternalSoft_Set).
  • If you use non-standard pin for slave select, configure EXTI to recieve interrupt on slave selection and reset internal NSS with SPI_NSSInternalSoft_Reset by software.

不建议使用最后一个,这有点不合时宜.

The last one is unrecommended, it's kind of kludge.

因此,要进行简单配置,请使用NSS_Soft(永久选择).如果您有多个从站,请使用NSS_Hard.

So, to get simple configuration use NSS_Soft (permanently selected). If you have more than 1 slave, use NSS_Hard.

此外,您还必须配置GPIO. SCKMOSI应该是主机上的AF/PP输出和从机上的AF输入; MISO在主机上应为AF输入,在从机上应为AF/PP输出; NSS应该是从站上的AF输入.

Also you have to configure GPIO. SCK and MOSI should be AF/PP outputs on master and AF inputs on slave; MISO should be AF input on master and AF/PP output on slave; NSS should be AF input on slave.

这篇关于STM32F4板上的SPI从器件设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆