2018-04-20 05:41 PM
2018-04-20 06:11 PM
this works
unsigned char SPI_t::transfer_receive(unsigned short data) {
char RxSPI;
// Clear_SPI1_nSS_Out();
while (!(hspi1.Instance->SR & SPI_FLAG_TXE)) ; // make sure we are readywhile ((hspi1.Instance->SR & SPI_FLAG_RXNE))
RxSPI = hspi1.Instance->DR; //clear out / read all the bytes left in the Rx register*((__IO uint8_t *)&hspi1.Instance->DR) = data; // force the SPI to transceive 8 bit
while (!(hspi1.Instance->SR & SPI_FLAG_TXE)) ; //wait for buffer to start shifting while ((hspi1.Instance->SR & SPI_FLAG_BSY)) ; // wait for all bits to completely leave the chip while ((hspi1.Instance->SR & SPI_FLAG_RXNE)) RxSPI = hspi1.Instance->DR; //read all the bytes left in the Rx register, the last one is for your reading.// Set_SPI1_nSS_Out();
return RxSPI;}2018-04-20 07:12 PM
TJ,
I don't have question for SPI -- it works fine.
My question is about the spi non-related delay loop:
for(n=0; n<1500; n++); that it took too long but I don't know what had caused the problem.
Brian
2018-04-20 10:37 PM
I guess you already know that it is bad practice to use a counter delay like that.
that's why they installed timers in the first place, to be processor clock/cache/wait variable type, etc. independent.
a float would count much slower than an int.
2018-04-21 12:20 AM
This is just a simple test code to emulate a camera controlling the lens via a sort of SPI bus. The delay counting loop is a quick way for the test; without any interrupts the countering loop should be precise. No floating here; just unsigned int. I'd like to know where to look for the cause of this behavior.
Brian
2018-04-21 12:32 AM
if you have a scope, you could show us all...
there has to be another process interfering with your execution.
2018-04-21 01:14 AM
I had attached the delay timings. What other things you like to see? Even if there is other task that interrupted the counting loop, why it consistently only interrupted the delay loop in the canon_w but never the canon_read()?
2018-04-21 01:53 AM
You have a strange aberration ..
let me suggest that you create the new Variable in the FOR loop, they are just registers anyway.
for ( int I =0;I<1500;I++);
2018-04-21 01:58 PM
Instruction cache enabled?
2018-04-21 05:37 PM
Thanks for the suggestion. I started with variable n in all the loops, then changed to m in the troubled loop with no difference, then defined m as Static, but no difference.