Skip to main content
KOzdi
Associate II
October 7, 2018
Question

Problem with form of SCK clock of SPI protocol !

  • October 7, 2018
  • 2 replies
  • 2632 views

I have been trying to perform SPI communication and observe outgoing and incoming data through MOSI & MISO. There is a weird problem in SCK weveform

0690X000006C8kwQAC.pngThere are the codes I wrote :

#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_spi.h"
 
 
#define SPIx_RCC RCC_APB2Periph_SPI1
#define SPIx SPI1
#define SPI_GPIO_RCC RCC_APB2Periph_GPIOA
#define SPI_GPIO GPIOA
#define SPI_PIN_MOSI GPIO_Pin_7
#define SPI_PIN_MISO GPIO_Pin_6
#define SPI_PIN_SCK GPIO_Pin_5
#define SPI_PIN_SS GPIO_Pin_4
 
void SPIx_Init(void);
uint8_t SPIx_Transfer(uint8_t data);
void SPIx_EnableSlave(void);
void SPIx_DisableSlave(void);
 
uint8_t receivedByte;
 
int main()
{
 
	// Initialization struct
 SPI_InitTypeDef SPI_InitStruct;
 GPIO_InitTypeDef GPIO_InitStruct;
 
 // Step 1: Initialize SPI
 RCC_APB2PeriphClockCmd(SPIx_RCC, ENABLE);
 SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
 SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
 SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
 SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
 SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
 SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
 SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
 SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
 SPI_Init(SPIx, &SPI_InitStruct); 
 SPI_Cmd(SPIx, ENABLE);
 
 // Step 2: Initialize GPIO
 RCC_APB2PeriphClockCmd(SPI_GPIO_RCC, ENABLE);
 // GPIO pins for MOSI, MISO, and SCK
 GPIO_InitStruct.GPIO_Pin = SPI_PIN_MOSI | SPI_PIN_MISO | SPI_PIN_SCK;
 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(SPI_GPIO, &GPIO_InitStruct);
 // GPIO pin for SS
 GPIO_InitStruct.GPIO_Pin = SPI_PIN_SS;
 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(SPI_GPIO, &GPIO_InitStruct);
 
 // Disable SPI slave device
 SPIx_DisableSlave();
		
		
		receivedByte = SPIx_Transfer(0x15);
 
	
 while(1) 
	{
		
 
	
	}
}
uint8_t SPIx_Transfer(uint8_t data)
{ 
		uint8_t receivedOne;
	
		SPIx_EnableSlave();
	
 // Write data to be transmitted to the SPI data register
 SPIx->DR = data;
 // Wait until transmit complete
 while (!(SPIx->SR & (SPI_I2S_FLAG_TXE)));
 // Wait until receive complete
 while (!(SPIx->SR & (SPI_I2S_FLAG_RXNE)));
 // Wait until SPI is not busy anymore
 while (SPIx->SR & (SPI_I2S_FLAG_BSY));
 // Return received data from SPI data register
 receivedOne = SPIx->DR;
	
	 SPIx_DisableSlave();
 
		return receivedOne;
}
 
void SPIx_EnableSlave()
{
 // Set slave SS pin low
 SPI_GPIO->BRR = SPI_PIN_SS;
}
 
void SPIx_DisableSlave()
{
 // Set slave SS pin high
 SPI_GPIO->BSRR = SPI_PIN_SS;
}

What seems to be my problem ?

Thanks in Advance !

    This topic has been closed for replies.

    2 replies

    waclawek.jan
    Super User
    October 7, 2018

    Too low sampling rate of the logic analyzer?

    JW

    KOzdi
    KOzdiAuthor
    Associate II
    October 7, 2018

    Oh ! Never came to my mind. I was puzzling my brain on it :) It works thanks a lot !