Problem with SPI in stm8l152c6
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
}
}
