Skip to main content
Visitor II
December 31, 2018
Question

Problem with SPI in stm8l152c6

  • December 31, 2018
  • 4 replies
  • 685 views

i have configured spi in polling method.I have loaded the data into the transmit buffer and waited for TXE flag to be set.Unfortunately the flag is not being set and what could be the possbile reasons for that?I have attached the code configuration below

#include "iostm8.h"

#include "stm8l.h"

#include <stdio.h>

#include <string.h>

#define BIT(x) 1<<x

void SetupSPI(void);            //Intialize spi

void internal_clk_enable(void); //Enable oscilator

void Spi_write(char ,int);       //for writing through spi

char Spi_read(char);        //for reading through spi

void test(char *k,int t);

int data = 0;

char v[10];

int addr_data_array_length= 0,array_count = 0;

char *read_buffer;

char receiving_array[2];

void main()

{

   internal_clk_enable(); // enable internal RC oscilator

   SetupSPI(); //initialize spi

   Spi_write(0x01,1);

   while(1);

}

void SetupSPI()

{

   PB_DDR |= BIT(6) | BIT(5);    //configure mosi and sck as output

   PB_DDR &= ~BIT(7);            // configure miso as input

   PB_CR1 |= BIT(6) | BIT(5);    // enable push pull mode

   PB_CR2 |= BIT(6) | BIT(5);    // enable fast mode

   SPI_CR1 = 0x3C;                  // Enable clock polarity,Capture MSB on first edge ,set baud_rate,configure as master

   SPI_CR2 = 0x00;                         

   SPI_CR1 |= BIT(6); //enable spi

}

void internal_clk_enable()

{

   CLK_CKDIVR = 0x00;                  // Ensure the clocks are running at full speed(16mhz).

   CLK_PCKENR = 0x10;                  // Enable peripheral clocks for spi.

}

void Spi_write(char address,int length)

{

   int i;

   char collect;

   for(i=0;i<length;i++)

      {

            SPI_DR = address;                  // write data to be transmitted to the SPI data register

            while(!(SPI_SR & 0x02));           // wait until transmit complete   

            while(!(SPI_SR & 0x01));           // wait until rx buffer loaded   

       collect = SPI_DR;                  // Store received data

   }

}

    This topic has been closed for replies.

    4 replies

    Visitor II
    December 31, 2018

    try this:

    while((SPI_SR & 0x02)==0);           // wait until transmit complete :  write when it's ok. wait while TXE=0

    SPI_DR = address;                  // write data to be transmitted to the SPI data register

    while((SPI_SR & 0x01)==0);           // wait until rx buffer loaded   : wait as long as RXNE is zero

    collect = SPI_DR;                  // Store received data

    Amal BabuAuthor
    Visitor II
    January 1, 2019

    Hai,

    i tried out the same but unfortunately it didnt workout.Do you suspect any other configuration issues?Do you have any samples code for the same

    Visitor II
    January 1, 2019

    Not for STM8, was using SW SPI by GPIO...

    Amal BabuAuthor
    Visitor II
    January 1, 2019

    hai,

    i found the problem with my code:

    it was missing header file.we need to include stm8l152x.h since we SHOULD BE using SPI1_SR & SPPI1_DR instead of SPI_SR & SPI_DR.

    Thanks for your support.