Skip to main content
SSmit.0
Associate III
October 21, 2018
Question

One Pulse and HAL_SPI_Receive

  • October 21, 2018
  • 3 replies
  • 904 views

I am using soft pulse triggering via "__HAL_TIM_ENABLE(&htim1)" but have a problem with the code shown below.

HAL_TIM_OnePulse_Start(&htim1, TIM_CHANNEL_1);

volatile uint16_t array [300];

HAL_Delay(1000);      // let ADC power up

      

while(1)

       {

      for(int n=0;n<300;n++)            // acquisition loop

         {

            if (hspi1.State != HAL_SPI_STATE_BUSY_RX)

            {

               __HAL_TIM_ENABLE(&htim1);   //send pulse   

            HAL_SPI_Receive(&hspi1,(uint8_t*)array[n],1,1);   //clock data

               HAL_Delay(1);

            }

         }

      }

With the HAL_Delay(1); in the loop, the pulses look fine but for some reason the HAL_SPI_Receive function isn't initiated - there are no clocks. When I remove the delay, the clock is there but the pulse output is almost inverted being high most of the time, and even runs over the receive clock? 

I don't quite understand what's going on as I have the != HAL_SPI_STATE_BUSY_RX there but I think it's being ignored?

Thanks

This topic has been closed for replies.

3 replies

T J
Senior III
October 21, 2018

I use this for SPI receive:

unsigned char receiveSPI1(unsigned short data) {
 
 char RxSPI;
 // Clear_SPI1_nSS_Out();
 while (!(hspi1.Instance->SR & SPI_FLAG_TXE)) // Tx buffer is empty ?
 ; 
 // is empty now
 
 *((__IO uint8_t *)&hspi1.Instance->DR) = data; // force the SPI to transceive 8 bit
 while (!(hspi1.Instance->SR & SPI_FLAG_TXE)) // wait for the transmisson buffer to clear
 ;
 while ((hspi1.Instance->SR & SPI_FLAG_BSY)) // wait for the data to leave the pin
 ; // here we are loading the Rx buffer
 while ((hspi1.Instance->SR & SPI_FLAG_RXNE)) // unload Rxbuffer until its empty
 RxSPI = hspi1.Instance->DR;
 // Set_SPI1_nSS_Out(); 
 
 return RxSPI; // the last received byte 
}

SSmit.0
SSmit.0Author
Associate III
October 21, 2018

Thanks for the reply, I will bear that in mind.

I am working with others unfamiliar with coding and the rest of the project is using HAL, so we would like to get the HAL working if possible. I am keen to understand what is wrong in my code?

Thanks 

waclawek.jan
Super User
October 21, 2018

Timer period lasting longer than the SPI frame?

JW