STM32H7 SPI do not transmit - Page 1
Forum section: electronics / general

author: Jdoe
author_rank: Newbie
timestamp: February 26, 2020, 10:50:25 pm
content: I have spent some days with this, and I dont know what is the problem. I have tried numbers of different variations of configuration, but always the same result. No physical actions to pins (SCK/MOSI), no TXC/EOT flag or even TXC-interrupt. No error flags. No results with full speed, step to step, physical measurements, with and without delays. So nothing interesting.Have tried with full-duplex, half-duplex and latest now simplex transmitter. All master transmitters. I have turned stones and tried to find the working solution (e.g CRCSIZE setting above)..When feed TSIZE, I got TXTF but seems like the "transmit event" is drown (CTSIZE does not decrese ever). So if you can point me to direction to go, thank you in advance. I removed from code above several functions, so it contains now only the SPI part. There can be some typos.Code:#include "stm32h743xx.h"#include "stdbool.h"#include "stdlib.h"volatile uint64_t bufferCounter;volatile static bool spiStatus;void send(uint8_t data, uint8_t cmd);void spiInit(void);void delay(void);void cmdPinInit(void);int main(){cmdPinInit();spiInit();send(0xFF, 1);//lcdInit();//setLcdWindow();while(1){}}void send(uint8_t data, uint8_t cmd){spiStatus = false;// CMD defined in headerif(cmd == CMD){GPIOA->BSRR |= GPIO_BSRR_BR2;} else {GPIOA->BSRR |= GPIO_BSRR_BS2;}//SPI1->CR2 |= (SPI_CR2_TSIZE & sizeof(data));SPI1->CR2 |= ~(SPI_CR2_TSIZE);SPI1->CR1 |= SPI_CR1_SPE;// tried too change clk delay lengthsfor(uint8_t i = 0; i<15; i++){__NOP;}// some why the TXC flag is high at this point?SPI1->IFCR |= SPI_IFCR_EOTC;//*((volatile uint16_t*)&(SPI1->TXDR)) = data;// *((volatile uint8_t*)&SPI1->TXDR) = data;*((volatile uint8_t*)&SPI1->TXDR) = data;NVIC_EnableIRQ(SPI1_IRQn);SPI1->CR1 |= SPI_CR1_CSTART;}void spiInit(){spiStatus = true;bufferCounter = 0;// CR1// crc calculation all patternsSPI1->CR1 |= SPI_CR1_TCRCINI;// polynomial not usedSPI1->CR1 &= ~SPI_CR1_CRC33_17;// SS set highSPI1->CR1 |= SPI_CR1_SSI;// spi transmitterSPI1->CR1 |= SPI_CR1_HDDIR;// CFG1// master clock /2SPI1->CFG1 &= ~SPI_CFG1_MBR;// CRC computation disableSPI1->CFG1 &= ~SPI_CFG1_CRCEN;// CRCSIZE = 8-bit (I have tried "all")SPI1->CFG1 |= SPI_CFG1_CRCSIZE_0 | SPI_CFG1_CRCSIZE_1 | SPI_CFG1_CRCSIZE_2;// tx dma disableSPI1->CFG1 &= ~I2C_CR1_TXDMAEN;// rx dma disableSPI1->CFG1 &= ~SPI_CFG1_RXDMAEN;// fifo threshold level = 4-dataSPI1->CFG1 &= ~SPI_CFG1_FTHLV;SPI1->CFG1 |= SPI_CFG1_FTHLV_0 | SPI_CFG1_FTHLV_1;// Dsize = 8bitsSPI1->CFG1 &= ~SPI_CFG1_DSIZE;SPI1->CFG1 |= SPI_CFG1_DSIZE_0 | SPI_CFG1_DSIZE_1 | SPI_CFG1_DSIZE_2;// CFG2// peripheral take no control of GPIOsSPI1->CFG2 &= ~SPI_CFG2_AFCNTR;// SS output management in master mode, active till transfer completedSPI1->CFG2 &= ~SPI_CFG2_SSOM;// multi-master mode disableSPI1->CFG2 |= SPI_CFG2_SSOE;// SS level, high=activeSPI1->CFG2 |= SPI_CFG2_SSIOP;// SS signal input by bitSPI1->CFG2 |= SPI_CFG2_SSM;// clock polarity high on idleSPI1->CFG2 |= SPI_CFG2_CPOL;// clock phase second clock transitionSPI1->CFG2 |= SPI_CFG2_CPHA;// MSB firstSPI1->CFG2 &= ~SPI_CFG2_LSBFRST;// SPI master modeSPI1->CFG2 |= SPI_CFG2_MASTER;// motorola protocolSPI1->CFG2 &= ~SPI_CFG2_SP;// simplex transmitterSPI1->CFG2 |= SPI_CFG2_COMM_0;// EOT interrupt enableSPI1->IER |= SPI_IER_EOTIE;}void delay(){uint64_t i = 2000000;while(i--){;}i = 2000000;while(i--){;}}void cmdPinInit(){// PORTA, B & G clock enableRCC->AHB4ENR |= RCC_AHB4ENR_GPIOAEN;RCC->AHB4ENR |= RCC_AHB4ENR_GPIOBEN;RCC->AHB4ENR |= RCC_AHB4ENR_GPIOGEN;//SPI1 clock enableRCC->APB2ENR |= RCC_APB2ENR_SPI1EN;// SCK & MOSI (open-drain + very high speed)GPIOB->MODER &= ~((GPIO_MODER_MODE3_0) | (GPIO_MODER_MODE5_0));GPIOB->MODER |= ((GPIO_MODER_MODE3_1) | (GPIO_MODER_MODE5_1));GPIOB->OTYPER |= (GPIO_OTYPER_OT3|GPIO_OTYPER_OT5);GPIOB->OSPEEDR |= GPIO_OSPEEDR_OSPEED3 | GPIO_OSPEEDR_OSPEED5;// SS (open-drain)GPIOG->MODER &= ~(GPIO_MODER_MODE10_0);GPIOG->MODER |= (GPIO_MODER_MODE10_1);GPIOG->OTYPER |= (GPIO_OTYPER_OT10);// SCK, MOSI, SS SPI1 alternate functions, AF5GPIOB->AFR |= 5<<12 | 5<<20;GPIOG->AFR |= 5<<8;// ledGPIOA->MODER |= GPIO_MODER_MODE1_0;GPIOA->MODER &= ~(GPIO_MODER_MODE1_1);GPIOA->OTYPER |= GPIO_OTYPER_OT1;GPIOA->OSPEEDR |= GPIO_OSPEEDR_OSPEED1_1;GPIOA->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED1_0);GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPD1);GPIOA->BSRR |= GPIO_BSRR_BR1;// d/cGPIOA->MODER |= GPIO_MODER_MODE2_0;GPIOA->MODER &= ~(GPIO_MODER_MODE2_1);GPIOA->OTYPER |= GPIO_OTYPER_OT2;GPIOA->OSPEEDR |= GPIO_OSPEEDR_OSPEED2_1;GPIOA->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED2_0);GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPD2);}void SPI1_IRQHandler(void){if(((SPI1->SR)& SPI_SR_EOT) == SPI_SR_EOT){NVIC_DisableIRQ(SPI1_IRQn);spiStatus = true;SPI1->CR1 &= ~SPI_CR1_SPE;GPIOA->BSRR |= GPIO_BSRR_BS1;}if(((SPI1->SR)& SPI_SR_TXC) == SPI_SR_TXC){NVIC_DisableIRQ(SPI1_IRQn);spiStatus = true;SPI1->CR1 &= ~SPI_CR1_SPE;GPIOA->BSRR |= GPIO_BSRR_BS1;}

author: rstofer
author_rank: Super Contributor
timestamp: February 26, 2020, 11:16:30 pm
content: Reference:https://www.st.com/content/ccc/resource/training/technical/product_training/group0/52/17/7a/28/2b/90/41/7d/STM32H7-Peripheral-Serial_Peripheral_interface_SPI/files/STM32H7-Peripheral-Serial_Peripheral_interface_SPI.pdf/_jcr_content/translations/en.STM32H7-Peripheral-Serial_Peripheral_interface_SPI.pdfFlowcharts:https://pdf1.alldatasheet.com/datasheet-pdf/view/700953/STMICROELECTRONICS/AN4286.htmlSample code:https://visualgdb.com/tutorials/arm/stm32/spi/

author: frmdstryr
author_rank: Contributor
timestamp: March 15, 2020, 02:14:44 pm
content: Did you get this sorted out? I'm having the same problem with the stm32h7 using the NUCLEO-H743ZI and mbed-os.The clock line does nothing and just sits idle so it just hangs waiting for the EOT.Code:#include "mbed.h"#include "platform/mbed_thread.h"DigitalOut led(LED1);DigitalOut led2(LED2);DigitalOut led3(LED3);InterruptIn btn(USER_BUTTON);Ticker flipper;SPI spi(D11, D12, D13);DigitalOut cs(D0);void flip() {led2 = !led2;}void toggle() {led = !led;}int main() {flipper.attach(&flip, 2.0);btn.fall(&toggle);spi.format(8, 3);spi.frequency(1000000);cs = 0;while (true) {led = !led;spi.write(0xaa);wait(1000);}}