Electronic – STM32F103 SPI Master example

armcspistm32f10x

I want to use SPI master configuration and send some data just to capture it with Logic Analyzer. It seems that I cannot trigger on clock and capture anything.
Using code from standard peripheral library examples I don't see what could be wrong (newbie), could some please have a look into it? (big tnx!)

I am using avrgcc and SPL for STM32F103C8T6 small eval board with CooCox studio.

I've tried several speed configurations and nothing seems to make it capture.
It should output clock on pin 13 when all starts, so triggering on high should at least start capture of data.

I also played with SPI_BaudRatePrescaler_*, as I understood this will divide 72MHz clock by prescaler value (right ?).

Here is the code

main.c

#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"    
#include "stm32f10x.h"
#include "platform_config.h"



/* Private define ------------------------------------------------------------*/
#define BufferSize 32
#define SEND_LIMIT 3

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
SPI_InitTypeDef   SPI_InitStructure;

uint8_t SPIz_Buffer_Tx[BufferSize] = {0x1, 0x2, 0x4, 0x54, 0x55, 0x56, 0x57,
                                      0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E,
                                      0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65,
                                      0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C,
                                      0x6D, 0x6E, 0x6F, 0x70};

__IO uint8_t TxIdx = 0, RxIdx = 0, k = 0;

/* Private functions ---------------------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(uint16_t SPIz_Mode);



int main(void)
{


      /* System clocks configuration ---------------------------------------------*/
      RCC_Configuration();

      /* GPIO configuration ------------------------------------------------------*/
      GPIO_Configuration(SPI_Mode_Master);

      /* SPIy Config -------------------------------------------------------------*/
      SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
      SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
      SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
      SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
      SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
      SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
      SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
      SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
      SPI_InitStructure.SPI_CRCPolynomial = 7;
      SPI_Init(SPIz, &SPI_InitStructure);

      /* Enable SPIz */
      SPI_Cmd(SPIz, ENABLE);

      /* Transfer procedure */
      while (TxIdx < SEND_LIMIT)
      {
        /* Wait for SPIy Tx buffer empty */
        while (SPI_I2S_GetFlagStatus(SPIz, SPI_I2S_FLAG_TXE) == RESET);
        /* Send SPIz data */
        SPI_I2S_SendData(SPIz, SPIz_Buffer_Tx[TxIdx++]);

      }


      while (1)
      {}


} /***** END OF MAIN *****/


void RCC_Configuration(void)
{
  /* PCLK2 = HCLK/2 */
  RCC_PCLK2Config(RCC_HCLK_Div2);

  /* Enable GPIO clock for SPIz */
  RCC_APB2PeriphClockCmd(SPIz_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);

  /* Enable SPIz Periph clock */
  RCC_APB1PeriphClockCmd(SPIz_CLK, ENABLE);
}


void GPIO_Configuration(uint16_t SPIz_Mode)
{
      GPIO_InitTypeDef GPIO_InitStructure;

      /* Configure SPIz pins: SCK, MISO and MOSI ---------------------------------*/
      GPIO_InitStructure.GPIO_Pin = SPIz_PIN_SCK | SPIz_PIN_MOSI;
      /* Configure SCK and MOSI pins as Alternate Function Push Pull */
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

      /**** ADDED THIS TO WORK ****/
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      **/**** ADDED THIS TO WORK ****/**


      GPIO_Init(SPIz_GPIO, &GPIO_InitStructure);


      GPIO_InitStructure.GPIO_Pin = SPIz_PIN_MISO;
      /* Configure MISO pin as Input Floating  */
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
      GPIO_Init(SPIz_GPIO, &GPIO_InitStructure);

}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {}
}

#endif

platform_config.h

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __PLATFORM_CONFIG_H
#define __PLATFORM_CONFIG_H


/* Define the STM32F10x hardware depending on the used evaluation board */

  #define SPIy                   SPI1
  #define SPIy_CLK               RCC_APB2Periph_SPI1
  #define SPIy_GPIO              GPIOA
  #define SPIy_GPIO_CLK          RCC_APB2Periph_GPIOA  
  #define SPIy_PIN_SCK           GPIO_Pin_5
  #define SPIy_PIN_MISO          GPIO_Pin_6
  #define SPIy_PIN_MOSI          GPIO_Pin_7

  #define SPIz                    SPI2
  #define SPIz_CLK                RCC_APB1Periph_SPI2
  #define SPIz_GPIO               GPIOB
  #define SPIz_GPIO_CLK           RCC_APB2Periph_GPIOB 
  #define SPIz_PIN_SCK            GPIO_Pin_13
  #define SPIz_PIN_MISO           GPIO_Pin_14
  #define SPIz_PIN_MOSI           GPIO_Pin_15 



/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */

#endif /* __PLATFORM_CONFIG_H */

Best Answer

It seems that I've missed this line.

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

Wow :) it seems it works.

Here is the picture of the signal:

enter image description here

Ok, in short what I have done:

  1. Use SPI2 to send 3 bytes of data (0x1,0x2,0x4)
  2. Attach OpenBench LogicSniffer to SCK and MOSI to monitor data output

Hope this helps someone!