Skip to main content
Upate.1
Associate II
May 20, 2021
Question

I am trying to implement simple spi communication between 2 stm8s. Sending 0x04( address of pin2) and passing the received data as argument to GPIO high .Slave code is txe is not becoming 1 and program stuck there.

  • May 20, 2021
  • 4 replies
  • 2462 views

I am trying to implement simple spi communication between 2 stm8s. Sending 0x04( address of pin2) and passing the received data as argument to GPIO high function so that LED connected at PORT D PIN 2 will glow. Master code is working properly, degugging properly, but in slave the TXE is not becoming 1 and program is stuck there only. Does any one can help locating the problem if its in code?

Master code:

#include “stm8s.h�?

#include “stm8s_gpio.h�?

#include “stm8s_spi.h�?

#include “stm8s_clk.h�?

void GPIO_setup(void);

void clock_setup(void);

void GPIO_setup(void)

{ GPIO_DeInit(GPIOC);

GPIO_DeInit(GPIOB);

GPIO_DeInit(GPIOD);

GPIO_DeInit(GPIOA);

// led pin

GPIO_Init(GPIOA, GPIO_PIN_3,GPIO_MODE_OUT_PP_HIGH_FAST);//NSS

GPIO_Init(GPIOC,GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST );//sck pin 5

GPIO_Init(GPIOC, GPIO_PIN_6, GPIO_MODE_OUT_PP_HIGH_FAST);//mosi pin 6

GPIO_Init(GPIOC, GPIO_PIN_7, GPIO_MODE_IN_PU_NO_IT);//miso pin 7

}

void clock_setup(void)

{

CLK_DeInit();

CLK_HSECmd(DISABLE);

CLK_LSICmd(DISABLE);

CLK_HSICmd(ENABLE);

while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);

CLK_ClockSwitchCmd(ENABLE);

CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//00

CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV8);//0x80

CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO,CLK_SOURCE_HSI,DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);

CLK_PeripheralClockConfig(CLK_PCKENR1_SPI,ENABLE);

}

void main(void)

{

clock_setup();

GPIO_setup();

SPI_DeInit();

SPI_Init(SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_2, SPI_MODE_MASTER, SPI_CLOCKPOLARITY_HIGH,

SPI_CLOCKPHASE_1EDGE,SPI_DATADIRECTION_2LINES_FULLDUPLEX,SPI_NSS_SOFT,0x00);

SPI_NSSInternalSoftwareCmd(DISABLE);

SPI_Cmd(ENABLE);

GPIO_WriteLow(GPIOA,GPIO_PIN_3);// pull nss low

SPI_SendData(0x04);//pin 2 address

while(SPI_GetFlagStatus(SPI_FLAG_TXE)== RESET);

while(SPI_GetFlagStatus(SPI_FLAG_BSY));

GPIO_WriteHigh(GPIOA,GPIO_PIN_3);// pull NSS high

SPI_Cmd(DISABLE);// disable spi

}

SLAVE CODE:

#define STM8S103

#include “stm8s.h�?

#include “stm8s_clk.h�?

#include “stm8s_gpio.h�?

#include “stm8s_spi.h�?

uint8_t RxBuffer2=0;

void main(void)

{CLK_DeInit();

CLK_PeripheralClockConfig(CLK_PCKENR1_SPI,ENABLE);

GPIO_DeInit(GPIOD);

GPIO_DeInit(GPIOA);

GPIO_DeInit(GPIOC);

GPIO_DeInit(GPIOB);

GPIO_Init(GPIOC,GPIO_PIN_5,GPIO_MODE_IN_PU_NO_IT);//sck pin 5

GPIO_Init(GPIOC,GPIO_PIN_6,GPIO_MODE_IN_PU_NO_IT );//mosi pin 6

GPIO_Init(GPIOC,GPIO_PIN_7,GPIO_MODE_OUT_PP_HIGH_FAST);//miso

GPIO_Init(GPIOA,GPIO_PIN_3,GPIO_MODE_IN_PU_NO_IT);//nss

SPI_DeInit();

SPI_Init(SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_2, SPI_MODE_SLAVE, SPI_CLOCKPOLARITY_HIGH,

SPI_CLOCKPHASE_1EDGE,SPI_DATADIRECTION_2LINES_FULLDUPLEX,SPI_NSS_SOFT,0x00);

SPI_NSSInternalSoftwareCmd(DISABLE);

SPI_Cmd(ENABLE);

SPI_SendData(0x04);//pin 2

while(SPI_GetFlagStatus(SPI_FLAG_TXE)== RESET);

while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET);

RxBuffer2 = SPI_ReceiveData();

GPIO_Init(GPIOD,RxBuffer2,GPIO_MODE_OUT_PP_HIGH_SLOW);// turn on led at this address

GPIO_WriteHigh(GPIOD,RxBuffer2);

while(SPI_GetFlagStatus(SPI_FLAG_BSY));

}

    This topic has been closed for replies.

    4 replies

    CPINA
    ST Employee
    May 20, 2021

    Hello,

    Are you sure the Slave is ready before the master is sending the clock and the data ? Best is to configure the slave first, this one waiting the clock coming from the master to send the data to the master and so to unload the transmit buffer to generate TXE...

    Br

    CPINA
    ST Employee
    May 20, 2021

    to complete my information, the data should into the TX buffer of the slave before the master is starting to send the clock and the data for slave reception. Otherwise, if the master starts to send the clock before the TX buffer is loaded, it will not be generated since the buffer will not be completely sent by the slave.

    Upate.1
    Upate.1Author
    Associate II
    May 24, 2021

    Hello sir, thank you for replying

    How do I configure slave first,

    I have tried writing the SPI->DR after spi deinti in slave code, but no results?

    Could you guide how do I configure slave first or any other solution you might be aware of ?