1
votes

I am using a STM32f103 chip with a Cortex-m3 core in a project. According to the manual 3.3.1. Cortex-M3 instructions, load a 32bit word with a single LRD instruction takes 2 CPU cycles to complete (assuming the destination is not PC).

My understanding is that this is only true for reading from internal memories (Flash or internal SRAM)

When reading from an external SRAM via the FSMC, it must take more cycles to complete the read operation. During the read operation, does the CPU stall until the FSMC is able to put the data together? In other words, do I lose CPU cycles when accessing external memories?

Thank you.

Edit 1: Also assume all access are aligned 32bit access.

2
Short answer: Yes. What other possibility could there be?David Schwartz
OK, does this also mean access 32bit from the external memory is atomic? since the CPU is stalled during the process?Frank Liu
I'm not sure what you mean by "atomic" exactly. Do you mean with respect to interrupts?David Schwartz
yes, does the CPU stop servicing the interrupts while waiting for FSMC?Frank Liu
Memory reads are considered "fast" and not interruptible.David Schwartz

2 Answers

1
votes

The STM32F1xx FSMC has programmable wait states - if for your memory that is not set to zero, then it will indeed take additional cycles. The data bus for the external memory is either 16 or 8 bits, so 32 bit accesses will also take additional cycles. Also the write FIFO can cause the insertion of wait states.

On the other hand the Cortex-M is a Harvard architecture core with different memories on different buses so that instruction and data fetches can occur simultaneously, minimising ot some extent processor stalling.

1
votes

LDR and STR instructions are not interruptible. The FSMC is bridged from the AHB, and can run at a much slower rate, as you already know. For reads, the pipeline will stall until the data is ready, and this may cause increased worst-case interrupt latency. The write may or may not stall the pipe, depending on configuration. The reference manual says there is a two-word write buffer, but it appears that may only be used to buffer bursting memories. If you were using a CRAM (PSRAM) with a bursting interface, subsequent writes would likely not complete before the next instruction is executing, but a subsequent read would stall (longer) to allow the write to finish before initiating the read.

If using LDM and STM instructions to perform multiple reads or writes, these instructions are interruptible, and it is implementation defined as to whether they will restart from the beginning or continue when returned to. I haven't been able to find out how ST has chosen to implement this behavior. In either case, each individual bus transaction would should not be interrupted.

In regards to LDRD and STRD for working on 64-bit values, I found this discussion which references the following from the ARM-ARM:

"... LDRD, ... STRD, ... instructions are executed as a sequence of word-aligned word accesses. Each 32-bit word access is guaranteed to be single-copy atomic. The architecture does not require subsequences of two or more word accesses from the sequence to be single-copy atomic."

So, it appears that LDRD and STRD are likely to function the same way LDM and STM function.