cancel
Showing results for 
Search instead for 
Did you mean: 

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.

Upate.1
Associate II

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

}

4 REPLIES 4
CPINA
ST Employee

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

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.

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 ?

​Hello,

I have at first a couple of questions :

 1- Did you check that the clock from the master is well present on the SPI_SCK pin with the correct data sent to the SPI slave ( I mean probe the GPIO , SCK, MOSI) ?

 2- The slave should be ready to receive before the master is starting the transfer. It means that the SPI slave initialisation should be ready and the data written into the transmit buffer (slave), before the master is having its owns data written into their own transmit buffer (it will trig the clock generation and the transfer to take place on the bus). To do that, you can use an GPIO which can be set by the slave device to let the master knows that the slave is ready. When detecting this condition, the master can finally starts the transfer by the data is written into his transmit buffer).

 3 - Be sure the NSS is well low at the slave place when the master initiates the transfer

For sure for master as well as for slave, better is to configure the IP before loading the transmit buffer as explained above.

BR