cancel
Showing results for 
Search instead for 
Did you mean: 

STM8S103 not reading data in SPI Slave mode sent by STM8S103 SPI master?

KRath.2
Associate

STM8S103 not reading data in SPI Slave mode sent by STM8S103 SPI master. Even all the configurations of Slave and Master are the same. I am using IAR IDE.

Please review the code of both Slave and Master and help me

STM8S SLAVE CODE:

#include "STM8S.h"
#include "stm8s_spi.h"   
   
#define SPI_BUFF_SIZE   1
 
int test_var = 1, test_var3=2;
uint8_t read_data = 0;
 
void clock_setup(void);
void GPIO_setup(void);
void SPI_setup(void);
 
 void Delay(uint16_t nCount);
 
void main()
{
    clock_setup();
    GPIO_setup();
    SPI_setup();
        
    while(TRUE)
    {
        while(SPI_GetFlagStatus(SPI_FLAG_RXNE) == SET)
        {
             read_data = SPI_ReceiveData();
              test_var += 5;
        }
        
         if(SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET)
           test_var3 += 3;
 
          Delay(100000);
    };
}
 
void Delay(uint16_t nCount)
{
  /* Decrement nCount value */
  while (nCount != 0)
  {
    nCount--;
  }
}
 
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);
     CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
                
     CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, 
     DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
                
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, ENABLE);
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, DISABLE);
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, DISABLE);
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, DISABLE);
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);
}
 
 
 
void SPI_setup(void)
{
     SPI_DeInit();
     // SPI GPIO Initialization
      GPIO_DeInit(GPIOC);
      GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)(GPIO_PIN_5), GPIO_MODE_OUT_PP_HIGH_FAST); //SPI_SCLK_PIN
      GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)(GPIO_PIN_6), GPIO_MODE_OUT_PP_HIGH_FAST); //SPI_MOSI_PIN
      GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)(GPIO_PIN_7), GPIO_MODE_IN_PU_NO_IT); //SPI_MISO_PIN
      GPIO_DeInit(GPIOA);
      GPIO_Init(GPIOA,(GPIO_Pin_TypeDef)(GPIO_PIN_3), GPIO_MODE_IN_PU_NO_IT); 
     
     SPI_Init(SPI_FIRSTBIT_MSB, 
              SPI_BAUDRATEPRESCALER_2, 
              SPI_MODE_SLAVE, 
              SPI_CLOCKPOLARITY_HIGH, 
              SPI_CLOCKPHASE_1EDGE, 
              SPI_DATADIRECTION_1LINE_RX, 
              SPI_NSS_SOFT, 
              0x00);
              
     SPI_ITConfig(SPI_IT_RXNE, ENABLE);// Receive buffer not empty interrupt Enable 
     SPI_Cmd(ENABLE);
}

STM8s103 MASTER // ***(Merged code of 2 files)

#include "STM8S.h"
#include "MAX72XX.h"
 
 
void clock_setup(void);
void GPIO_setup(void);
void SPI_setup(void);
 
 void Delay(uint16_t nCount);
 
void main()
{            
    clock_setup();
    GPIO_setup();
    SPI_setup();
    MAX72xx_init();
   
                
    while(TRUE)
    {
          
            //MAX72xx_write(1, 0x7E);
            MAX72xx_write(1, 8);
            
             MAX72xx_write(3, 3 );
            
            MAX72xx_write(2, 2);
     
            MAX72xx_write(4, 8);
             
             MAX72xx_write(5, 3);
             
              MAX72xx_write(4, 8);
             
             MAX72xx_write(7, 7 );
             MAX72xx_write(8, 14);
                          
             Delay(100000);
    };
}
 
 
void Delay(uint16_t nCount)
{
  /* Decrement nCount value */
  while (nCount != 0)
  {
    nCount--;
  }
}
 
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);
     CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
                
     CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, 
     DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
                
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, ENABLE);
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, DISABLE);
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, DISABLE);
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, DISABLE);
     CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);
}
 
 
void GPIO_setup(void)
{
     GPIO_DeInit(GPIOC);
     GPIO_Init(GPIOC, ((GPIO_Pin_TypeDef)GPIO_PIN_5 | GPIO_PIN_6), 
               GPIO_MODE_OUT_PP_HIGH_FAST);
}
 
 
void SPI_setup(void)
{
     SPI_DeInit();
     SPI_Init(SPI_FIRSTBIT_MSB, 
              SPI_BAUDRATEPRESCALER_2, 
              SPI_MODE_MASTER, 
              SPI_CLOCKPOLARITY_HIGH, 
              SPI_CLOCKPHASE_1EDGE, 
              SPI_DATADIRECTION_1LINE_TX, 
              SPI_NSS_SOFT, 
              0x00);
     SPI_Cmd(ENABLE);
}
 
void MAX72xx_init(void)
{
    GPIO_Init(CS_port, CS_pin, GPIO_MODE_OUT_PP_HIGH_FAST);
 
    MAX72xx_write(shutdown_reg, run_cmd);                 
    MAX72xx_write(decode_mode_reg, 0xFF);
    MAX72xx_write(scan_limit_reg, 0x07);
    MAX72xx_write(intensity_reg, 0x0D);
    
    MAX72xx_write(display_test_reg, test_cmd);     
    Delay(10000000);
    MAX72xx_write(display_test_reg, no_test_cmd);
}
 
 
 
void MAX72xx_write(unsigned char address, unsigned char value)
{
    while(SPI_GetFlagStatus(SPI_FLAG_BSY));
    GPIO_WriteLow(CS_port, CS_pin);
    
    SPI_SendData(address);
    while(!SPI_GetFlagStatus(SPI_FLAG_TXE));
      
    SPI_SendData(value);
    while(!SPI_GetFlagStatus(SPI_FLAG_TXE));
    GPIO_WriteHigh(CS_port, CS_pin);
}

STM8S103 Slave not reading the data from the master but MAX7219 can read the smae data from Master STM8S.

The connection between STM8s103 Master and slave are as follow

STM8s Master | STM8S Slave

MOSI (PC6) --------------------------------> MOSI (PC6)

SCLK (PC5) --------------------------------> SCLK (PC5)

SS (PC4) ---------------------------------> NSS (A3)

Anyone understand the problem, please help me out.

Thank you

2 REPLIES 2
Upate.1
Associate II

Did you find the solution to the problem?

I am facing the same issue

WilkoL
Senior

It looks like you enable SPI interrupts on the slave

SPI_ITConfig(SPI_IT_RXNE, ENABLE);

but I do not seen an interrupt handler for it. Instead you read the received data in main.

Now, I also do not see that you enable global interrupts,

enableInterrupts();	

but maybe that is somewhere else in the code. If it is, the program will probably hang as soon as the first data is received.