AnsweredAssumed Answered

STM32F429 Stopping SPI

Question asked by Eric Lin on Apr 18, 2018
Latest reply on Apr 21, 2018 by Brian Dang

Hi, there


This might be a basic question, but I've been trying to solve it for a few days now. I hope there will be an SPI expert who is willing to help. My codes are as follows:


=======================================================================        RESET_PIN(GPIOF, GPIO_PIN_6); // CS pulled low -- PF6 is my Chip Select PIN


        item_count = 82; jmax = 400; // I need to read out 82*2 = 164 bytes per loop. Then I'll have to

                                                        // do some data processing  on the data read, before reading

                                                        // the next 164 bytes, etc. The data width is 16-bit. jmax is

                                                        // simply a variable used for testing at this stage.
        for(volatile int j=0; j<jmax; j++){
            ii = 0;
            SPI1->CR1 |= SPI_CR1_SPE; // Enable SPI1 clock
            while(ii < item_count){


                if(SPI1->SR & SPI_SR_RXNE){
                    //if(ii == 81)SPI1->CR1 &= (~SPI_CR1_SPE);
                    SPI_rx_tmp[ii] = SPI1->DR;

                    if(ii==80){  // This is the second to last RXNE
                        for(volatile int jj=0; jj<2;jj++); // Software delay of roughly 1.5 SPI clock cycle.
                        SPI1->CR1 &= (~SPI_CR1_SPE);
                      // Disable SPI1 clock. The above 2 lines of codes take about 0.1 us to complete.
                      // 0.1 us is about one clock cycle for a clock of 10MHz, which is what the SPI-1
                      // has been configured. According to the manual, DM00031020.pdf: Page 897/1745,
                      // the SPI clock should be halted after one SPI clock cycle, following the second to
                      // last occurrence of RXNE.
            First2Bytes_Arry[j] = SPI_rx_tmp[0];


            if((First2Bytes&0x0FFF) == 0x0000){ // 0th frame
                //Readout the entire frame..
                //for(volatile int jj=0; jj<80;jj++)fb[jj]=SPI_rx[2+jj+j*82]; // Read the first line.
                //ReadOneFrame(&fb[80]); // The first line has just been read.

                //for(volatile int jj=0; jj<30;jj++);

        SET_PIN(GPIOF, GPIO_PIN_6); // CS pulled high to disable the SPI reading.



Unless I misunderstand what the reference manual says on section 28.3.8, what happened

was that if I disabled the SPI clock following the steps outlined on that page, then the

last RXNE=1 never came. On the other hand, if I disabled the SPI clock following the very

last RXNE=1, the SPI clock was not stopped correctly, meaning that more data was clocked

out after the SPI was stopped. I would miss these data. 


I've also tried to use the HAL_SPI_Receive() and the SPI clock was not stopped correctly either.


I hope it makes sense. Thank you for your time.