stm8S SPI issue

Utsav Savaliya's picture

Offline

Joined: May 20, 2021

Replies: 3
stm8S SPI issue
May 20, 2021 - 6:08pm

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

 

Sourav Gupta's picture

Offline

Joined: Feb 12, 2018

Replies: 585

Could you make step debug on the reciver side? What ST tool are you using for programming?

Utsav Savaliya's picture

Offline

Joined: May 20, 2021

Replies: 3

I am using STVD. Yes I am able to debug.

Utsav Savaliya's picture

Offline

Joined: May 20, 2021

Replies: 3

Yes there is a step debug option available.

Sourav Gupta's picture

Offline

Joined: Feb 12, 2018

Replies: 585

Please make a step debug to the TXE and check what is happening.

Utsav Savaliya's picture

Offline

Joined: May 20, 2021

Replies: 3

Sir, I have checked that. It is not becoming 1, so stuck at 

while(SPI_GetFlagStatus(SPI_FLAG_TXE)== RESET); loop only.

 

Sourav Gupta's picture

Offline

Joined: Feb 12, 2018

Replies: 585

It seems like a issue on the hardware...

1. check connection first.

2. Check the SPI clock frequency and configuration.

3. Use a different IC to test the same code.