Skip to main content
VVlas.1
Associate II
February 10, 2021
Question

How to set up SPI? STM32h743ZI

  • February 10, 2021
  • 6 replies
  • 2002 views

I'm still beginner to programming STM32. I haven't been able to configure SPI for two weeks and don't understand what I am doing wrong. I dug up all the forums looking for an answer, but nothing helped to solve my problem. I checked the MOSI and SCK pin with an oscilloscope and there are no signals on these pins. I tried different ways to configure SPI but still nothing helped. Tried configuring for SPI1, SPI2, SPI3, SPI4, SPI5 etc, same problem. Everything works in HAL, but I want to understand what I am doing wrong using CMSIS. Please give me at least a little hint what I'm setting up wrong. I'm using STM32H743ZI debug board. I really hope for your help, thank you.

#include "main.h"
 
 
#define cs_set() GPIOC->BSRR |= GPIO_BSRR_BR11;
#define cs_reset() GPIOC->BSRR |= GPIO_BSRR_BS11;
 
void SPIinit (void);
void Settings7219 (void);
void SPITransmit (uint8_t adress, uint8_t data);
 
int main (void) {
	SPIinit();
	Settings7219();
	GPIOB->BSRR |= GPIO_BSRR_BS0;
	while(1){
 
	}
}
 
void SPITransmit (uint8_t adress, uint8_t data)
 {
	SPI3->CR1 |= SPI_CR1_CSTART;
	uint8_t tx_buffer[1] = {0};
	while (!(SPI3->SR & SPI_SR_TXP));
	cs_set(); // SS enable
	tx_buffer[0] = adress;
	SPI3->TXDR = tx_buffer[0];
	tx_buffer[0] = data;
	SPI3->TXDR = tx_buffer[0];
	cs_reset(); // SS disable
	SPI3->CR1 &= ~SPI_CR1_CSTART;
 }
 
void Settings7219 (void)
{
	SPITransmit(0x09, 0x00); // no decoding
	SPITransmit(0x0a, 0x01); // brightness
	SPITransmit(0x0b, 0x07); // scan limit = 8 LEDs
	SPITransmit(0x0c, 0x01); // power down =0,normal mode = 1
	SPITransmit(0x0f, 0x01); // no display test
}
 
 
 
 /*__________________________SPI_Conf____________________*/
void SPIinit (void){
	/*_______________________RCC_________________________*/
 
	RCC->AHB4ENR |= RCC_AHB4ENR_GPIOCEN;
	RCC->AHB4ENR |= RCC_AHB4ENR_GPIOBEN;
	RCC->APB1LENR |= RCC_APB1LENR_SPI3EN;
 
	/*_______________________SET_MODE_____________________*/
	GPIOC->MODER &= ~GPIO_MODER_MODE12;
	GPIOC->MODER &= ~GPIO_MODER_MODE11;
	GPIOC->MODER &= ~GPIO_MODER_MODE10;
	GPIOC->MODER |= GPIO_MODER_MODE12_1 | GPIO_MODER_MODE11_0 | GPIO_MODER_MODE10_1;
	
	GPIOB->MODER &= ~GPIO_MODER_MODE0;
	GPIOB->MODER |= GPIO_MODER_MODE0_0;
	/*_____________________SET_AF_________________________*/
	GPIOC->AFR[1] |= GPIO_AFRH_AFSEL10_1 | GPIO_AFRH_AFSEL10_2; // MOSI AF6
	GPIOC->AFR[1] |= GPIO_AFRH_AFSEL12_1 | GPIO_AFRH_AFSEL12_2; // SCK AF6
 
	/*_____________________SET_SPI________________________*/
	SPI3->CFG1 |= SPI_CFG1_MBR_1;
	//SPI3->CFG1 |= SPI_CFG1_CRCEN;
	//SPI3->CFG1 |= SPI_CFG1_FTHLV_0;
	SPI3->CFG2 |= SPI_CFG2_MASTER;
	SPI3->CFG2 |= SPI_CFG2_COMM_0;
	SPI3->CFG2 |= SPI_CFG2_CPOL;
	//SPI3->CFG2 |= SPI_CFG2_SSOE;
	//SPI3->CFG2 |= SPI_CFG2_SSM;
	//SPI3->CFG2 |= SPI_CFG2_SSOM;
	//SPI3->CR2 |= SPI_CR2_TSIZE;
	//SPI3->CR2 |= SPI_CR2_TSER;
	//SPI3->IER |= SPI_IER_TXPIE;
	//SPI3->CR1 |= SPI_CR1_SSI;
	//SPI3->IER |= SPI_IER_TXTFIE;
	SPI3->CR1 |= SPI_CR1_SPE;
	NVIC_EnableIRQ(SPI3_IRQn);
}

This topic has been closed for replies.

6 replies

Houssem CHAABANI
Visitor II
February 10, 2021

Hello @VVlas.1​ ,

You can use STM32CUBEMX to configure you project:

Choose your MCU then under Connectivity you can find SPI1 ... and configure them.

Hope that this helps you.

When your question is answered, please close this topic by choosing Select as Best. This will help other users find that answer faster.

Thanks!

VVlas.1
VVlas.1Author
Associate II
February 10, 2021

????

Tesla DeLorean
Guru
February 10, 2021

Understand that most here don't have the time/energy to wade through the bit level stuff because you don't want to use the libraries.

It's not even efficient the way you're doing it. The compiler will not fold multiple RMW against the same register.

BSRR does not need an RMW action, just write the pattern to the 32-bit register

The pin level for PC10/PC12 as AF6, look ok.

Perhaps you can dump the register content in the working (library) and non-working (register level) case and see what's different. Perhaps do as a hybrid where you incrementally replace the HAL code with your own. Try doing bisection until you find the problem.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
VVlas.1
VVlas.1Author
Associate II
February 10, 2021

I wrote that using STM32CUBEMX I managed to configure SPI transmission, but I can't configure SPI transmission using CMSIS. Give me a little help what I am configuring wrong using CMSIS. Please help me