SPI stops working correctly after 1.6ms - Page 1
Forum section: electronics / general

author: calvingloster
author_rank: Regular Contributor
timestamp: July 27, 2025, 12:33:09 pm
content: I've been having trouble reading a SPI sensor. Turns out the SPI stops working after exactly 1.6ms and I have no idea why. Ive tried this on my project board and an evaluation board with nothing connected to it. When i try it on the project board the sensor responds with its ID as expected but after 1.6ms it stops working. I though it may be a poorly soldered IC as its a tiny tiny BGA package so I moved to the eval board which is a sam4sd32 explained board. I was hoping to just see it output SPI but it also stops working correctly after 1.6ms. Ive attached two images of a logic analyzer showing what the SPI is doing.Code:while (1){while (!(SPI->SPI_SR & SPI_SR_TDRE));//write 16 bytesSPI->SPI_TDR = ((((READ << 7) | CHIP_ID) << 8) | 0x80);//read 16 byteswhile (!(SPI->SPI_SR & SPI_SR_RDRF));buffer = REG_SPI_RDR;}

author: ataradov
author_rank: Super Contributor
timestamp: July 27, 2025, 03:27:08 pm
content: Not enough information. Have you checked that the code loop is still running? I would add GPIO toggle at the beginning of the loop and check what it is doing during that time.

author: xvr
author_rank: Frequent Contributor
timestamp: July 28, 2025, 12:35:18 pm
content: Logic analizer shows that SPI clock and data still going. Add SPI CS to LA inputs.

author: calvingloster
author_rank: Regular Contributor
timestamp: July 28, 2025, 01:16:01 pm
content: Good point this solved the problem. The code block was not running and it was just the SPI having itself a session. I never even thought of this but makes sense because SPI is hardware based and can do things without the CPU. The problem was my master clock setup for the microcontroller never took effect because I wrote two registers at once and was not allowed to. Apparently enabling the fast RC oscillator (MOSCRCEN = 1) and changing the fast RC frequency (MOSCCRF) at the same time is not allowed!! I fixed this and then double checked the SPI clock frequency to be what I expected and this solved the issue. looking at the code i just posted it still doesnt make sense to me, the fast RC oscillator is enabled by default. I was just changing its frequency to 8MHz from 4Mhz and that didn't work. FML.Wrong code:Quotecorrected code:Code:void setupMainClock(void){//remember you must declare F_CPU in compiler symbols and it must be equal to the frequency set here.PMC->CKGR_MOR |= (CKGR_MOR_KEY_PASSWD) | CKGR_MOR_MOSCRCEN;//Enabling the fast RC oscillator (MOSCRCEN = 1) and//changing the fast RC frequency (MOSCCRF) at the same time is not allowed!!//The fast RC must be enabled first and its frequency changed in a second stepPMC->CKGR_MOR |= (CKGR_MOR_KEY_PASSWD) | (CKGR_MOR_MOSCRCF_8_MHz);//wait until main clock readywhile (!(PMC->PMC_SR & PMC_SR_MCKRDY));//set the multiplier and divider//The PLLB Clock frequency is the PLLB input frequency multiplied by MULB + 1.PMC->CKGR_PLLBR |= CKGR_PLLBR_MULB(14) | CKGR_PLLBR_DIVB(1);//wait until main clock readywhile (!(PMC->PMC_SR & PMC_SR_MCKRDY));//set wait statesREG_EFC0_FMR |= EEFC_FMR_FWS(6);//select PLLB as the master clockPMC->PMC_MCKR |= PMC_MCKR_CSS_PLLB_CLK;}

author: SteveThackery
author_rank: Super Contributor
timestamp: July 28, 2025, 10:45:01 pm
content: Thanks for posting the solution. Someone is going to find that very useful one day. 😁