cancel
Showing results for 
Search instead for 
Did you mean: 

How can SPI Master ReceiveData from SPI Slave ?

M?nh Quân
Associate II
Posted on November 18, 2017 at 19:32

  • Sorry about my english.

    Hi everybody . I'm using SPI slave sendata to SPI master. But it not working. I'm using SPI2 to mode slave and SPI1 to mode master . It's my project: 

    /*.......main.c.......*/

    &sharpinclude 'stm32f4xx.h'

    /* MASTER SLAVE

    PA4 --- CONTROL_SS PB12 --- SPI2_SS

    PA5 --- SPI1_SCK PB13 --- SPI2_SCK

    PA6 --- SPI1_MISO PB14 --- SPI2_MISO

    PA7 --- SPI1_MOSI PB15 --- SPI2_MOSI

    */

    &sharpdefine SS_DIS GPIO_ResetBits(GPIOA, GPIO_Pin_4)

    &sharpdefine SS_EN GPIO_SetBits(GPIOA, GPIO_Pin_4)

    volatile uint8_t SPI_data_send,SPI_data_get,a,b=0;

    GPIO_InitTypeDef GPIO_InitStructure;

    SPI_InitTypeDef SPI_InitStructure;

    NVIC_InitTypeDef NVIC_InitStructure;

    void SPI_Configuration(void);

    void Delay(__IO uint32_t nCount);

    int main(void)

    {

    SPI_Configuration();

    while (1)

    {

    SS_DIS;

    a = SPI_I2S_ReceiveData(SPI1);

    SPI_I2S_SendData(SPI2, b);

    SPI_I2S_ReceiveData(SPI1);

    Delay(5000000);

    b++;

    }

    }

    void SPI_Configuration(void)

    {

    /* SPI_MASTER configuration ------------------------------------------------*/

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1);

    GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1);

    GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1);

    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

    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_256;

    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

    SPI_InitStructure.SPI_CRCPolynomial = 7;

    SPI_Init(SPI1, &SPI_InitStructure);

    /* SPI_SLAVE configuration ------------------------------------------------*/

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_SPI2); // only connect to

    GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_SPI2); // only connect to

    GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_SPI2); // only connect to

    GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_SPI2); // only connect to

    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

    SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;

    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_Hard;

    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;

    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

    SPI_InitStructure.SPI_CRCPolynomial = 7;

    SPI_Init(SPI2, &SPI_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

    SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);

    /* Enable SPI_SLAVE */

    SPI_Cmd(SPI2, ENABLE);

    /* Enable SPI_MASTER */

    SPI_Cmd(SPI1, ENABLE);

    }

    void Delay(__IO uint32_t nCount)

    {

    while(nCount--)

    {

    }

    }

    &sharpifdef USE_FULL_ASSERT

    void assert_failed(uint8_t* file, uint32_t line)

    {

    /* User can add his own implementation to report the file name and line number,

    ex: printf('Wrong parameters value: file %s on line %d\r\n', file, line) */

    /* Infinite loop */

    while (1)

    {

    }

    }

    &sharpendif/*...........end main.c.......*/

    /*.............stm32f4xx_it.c......../
  • &sharpinclude 'stm32f4xx_it.h'

    &sharpinclude 'stm32f4xx.h'

    extern uint8_t SPI_data_get;

    void NMI_Handler(void)

    {

    }

    void HardFault_Handler(void)

    {

    /* Go to infinite loop when Hard Fault exception occurs */

    while (1)

    {

    }

    }

    void MemManage_Handler(void)

    {

    /* Go to infinite loop when Memory Manage exception occurs */

    while (1)

    {

    }

    }

    void BusFault_Handler(void)

    {

    /* Go to infinite loop when Bus Fault exception occurs */

    while (1)

    {

    }

    }

    void UsageFault_Handler(void)

    {

    /* Go to infinite loop when Usage Fault exception occurs */

    while (1)

    {

    }

    }

    void SVC_Handler(void)

    {

    }

    void DebugMon_Handler(void)

    {

    }

    void PendSV_Handler(void)

    {

    }

    void SysTick_Handler(void)

    {

    }

    void SPI2_IRQHandler(void)

    {

    if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) != RESET)

    {

    SPI_data_get = SPI_I2S_ReceiveData(SPI2);

    //SPI_I2S_ClearFlag(SPI2, SPI_I2S_IT_RXNE);

    }

    }

    /*......end stm32f4xx_it.c.........*/

    I want SPI1 master can receive data from SPI2 Master .....!

    Thank you so much !

#help #spi #spimaster
4 REPLIES 4
S.Ma
Principal
Posted on November 19, 2017 at 15:34

Hmm, there are cube example code for SPI between master and slave for nucleo board.

This could be a better headstart.

Things to remember about SPI: The master drives the clock. The slave must push out the data before the master clocks bits back to him. It is unwise to use SPI slave without interrupt processing, so I assume this test code is only for test...

Posted on November 19, 2017 at 17:54

I do not understand exactly what you say. Do you have any slave using data transfer for the master. Is it possible for me to refer? Thank you very much.

Posted on November 19, 2017 at 20:49

Download STM32Cube. There should be examples for the Nucleo and Discovery of the respective STM32 Family, by peripheral. Find the SPI folder and among the few projects, find the one which does SPI master <=> SPI slave communication example. The example will be a starting point.

Posted on November 20, 2017 at 05:49

I will try it. Thank you !