compiler/tests/board/stm32h753_cached/app/main.ldn
skip to the library navigation
1-- The STM32H753's cached profile on a NUCLEO-H753ZI (D975): USART3's
2-- receive ring and a transmit, each by DMA with the Cortex-M7's data cache
3-- on and its buffers in the cached AXI SRAM, maintained through core/cache
4-- and platform/cortex_m/l1 as D227 orders, and beside each the control
5-- that leaves the maintenance out and shows what the cache then does. The
6-- ring is serial's, the prototype-1 receive contract the cacheless board
7-- runs, which does D227's maintenance itself through the provider it is
8-- given (D978): this program gives it `l1.new()` and writes no
9-- maintenance of the ring's. environments/boards/stm32h753_cached/board.py
10-- sends the bytes and checks each frame against its oracle.
11--
12-- The stale-read control leaves the ring's maintenance out by giving the
13-- same routines the cacheless provider, whose operations are the barriers
14-- alone: the module's own `restart` and `read` then run as they do on the
15-- cacheless board, so what the control shows is what that code does on a
16-- cached chip, and nothing of the control is a path the maintained ring
17-- does not take. Its one maintenance of its own, a clean of the ring's
18-- refill, is the control's setup: it leaves the refill in memory and in
19-- the cache, so a line the stream writes behind it stays stale.
20--
21-- The protocol, in the cacheless board's frames: the host's first byte,
22-- read through the maintained ring, starts the run, and the boot frame
23-- `b` reports CCR, CCSIDR and the line. Then:
24--
25-- * `c`: the ring is refilled with 0x5a, cleaned but not invalidated,
26-- and rearmed through the cacheless provider, so its lines stay
27-- cached; the host sends 64 bytes, two lines, read through the same
28-- provider, and `C` reports how many the read returned as 0x5a, the
29-- stale copy, which lines were 0x5a throughout, and the FNV-1a hashes
30-- of what it returned and, once a maintained restart has cleaned and
31-- invalidated the ring, of what memory holds;
32-- * the host then sends 1024 bytes, read through the maintained ring,
33-- which invalidates its lines between each snapshot and its copy, and
34-- `E` reports their count and hash;
35-- * `d`: a line written to memory as 0x11, then dirtied as 0x22 and
36-- invalidated, loses the 0x22; four ranges that are not whole lines
37-- are refused, and the line they cover keeps its dirty 0x44;
38-- * `x` and `X` bracket a transmit whose buffer memory holds `o` and
39-- whose cached copy holds the message, never cleaned, so the line
40-- carries the `o`s; `y` and `Y` bracket the same buffer's transmit
41-- after a clean, which carries the message;
42-- * `e` reports CCR, still on, and the entry returns: reset's
43-- counterpart cleans and disables the caches (D973), and the board
44-- rests in the fault handler the returned entry reaches, where the
45-- host reads CCR clear.
46--
47-- A failure halts with an `h` frame whose `s` names the step, caches on.
48import bringup
49import core/cache
50import core/line
51import descriptions/stm32h753_cached
52import platform/cortex_m
53import platform/cortex_m/l1
54import report
55import scs
56import serial
57import stm32h753/dma1
58import stm32h753/dmamux1
59import stm32h753/usart3
60
61link(align: 256)
62mut rx_storage: [256]u8 = zeroed
63-- The transmit buffer and the probe of the CPU-only controls, each whole
64-- lines aligned to the line, so no line they occupy holds another datum.
65link(align: 32)
66mut tx_storage: [64]u8 = zeroed
67link(align: 32)
68mut probe: [64]u8 = zeroed
69mut notification: u32 = 0
70mut timer_events: u32 = 0
71
72budget: u32 = 4096
73-- The stale-read control's two lines, and the stream after it.
74control_bytes: usize = 64
75stream_bytes: usize = 1024
76-- How long a step waits for the host, in milliseconds: long enough for
77-- the host to read CCR over SWD between two frames.
78patience: u32 = 20000
79-- What the controls leave cached: the ring's refill, and the transmit
80-- buffer's memory under its unclean message.
81ring_fill: u8 = 0x5a
82transmit_memory: u8 = 0x6f
83-- DMA_LISR's stream 0 flags (RM0433 15.5.1, p. 659): TCIF0 5, TEIF0 3,
84-- DMEIF0 2 and FEIF0 0; DMA_LIFCR clears each. A transfer error stops the
85-- stream. In direct mode a memory-to-peripheral stream sets FEIF on an
86-- underrun when the memory bus was not granted before the peripheral's
87-- request, and an underrun neither stops the stream nor loses data
88-- (15.3.20, pp. 657 and 658), so FEIF and DMEIF decide nothing of when the
89-- stream has ended and are only reported.
90transfer_error_flag: u32 = 0x08
91stream_flags: u32 = 0x3d
92-- DMA_SxCR's EN, which the stream clears once its count reaches zero in
93-- normal mode or a transfer error stops it (15.3.15, p. 654).
94stream_enable: u32 = 0x01
95-- USART_ISR's TC, bit 6: the last frame has left the shift register and
96-- the transmit FIFO is empty (48.8.9, p. 2085).
97transmission_complete: u32 = 0x40
98-- What the last transmit left: DMA_LISR's stream 0 flags and S0NDTR.
99mut transmit_flags: u32 = 0
100mut transmit_left: u32 = 0
101
102-- DMA1 stream 0's interrupt, DMA_STR0, IRQ 11, which arms the ring's next
103-- lap (RM0433 Table 147, p. 754).
104link(vector: 27)
105extern(interrupt) dma_irq: () -> none =
106 serial.completed()
107 notification = 1
108 cortex_m.completion_barrier()
109end dma_irq
110
111-- SysTick, every millisecond: the wakeup for a lap in progress, and the
112-- clock each wait is bounded by.
113link(vector: 15)
114extern(interrupt) tick_irq: () -> none =
115 timer_events = timer_events +% 1
116 notification = 1
117end tick_irq
118
119-- USART3's, IRQ 39 (Table 147, p. 755), raised by IDLE.
120link(vector: 55)
121extern(interrupt) usart3_irq: () -> none =
122 if serial.idle() then notification = 1 end if
123 cortex_m.completion_barrier()
124end usart3_irq
125
126take_events: () -> none =
127 saved := cortex_m.disable_interrupts()
128 defer cortex_m.restore_interrupts(saved)
129 notification = 0
130end take_events
131
132rest: () -> none =
133 saved := cortex_m.disable_interrupts()
134 if notification == 0 then cortex_m.wait_for_interrupt() end if
135 cortex_m.restore_interrupts(saved)
136end rest
137
138halt: (step: u32) -> noreturn =
139 report.open_frame(104)
140 report.field(115, step)
141 report.close_frame()
142 loop do cortex_m.wait_for_interrupt() end loop
143end halt
144
145-- FNV-1a over BYTES, from HASH, which the host computes alike.
146fnv: (hash: u32, bytes: []u8) -> (next: u32) =
147 next = hash
148 for byte in bytes do next = (next ^ u32(byte)) *% 16777619 end for
149end fnv
150
151fnv_basis: u32 = 0x811c9dc5
152
153fill: (into: []mut u8, value: u8) -> none =
154 for index in 0..<lenof into do into[index] = value end for
155end fill
156
157-- How many of BYTES are VALUE.
158matching: (bytes: []u8, value: u8) -> (count: u32) =
159 count = 0
160 for byte in bytes do
161 if byte == value then count += 1 end if
162 end for
163end matching
164
165-- One bit for each 32-byte line of BYTES that is VALUE throughout, the
166-- first line's the lowest.
167whole_lines: (bytes: []u8, value: u8) -> (mask: u32) =
168 mask = 0
169 for line in 0..<lenof bytes / 32 do
170 if matching(bytes[line * 32..<line * 32 + 32], value) == 32 then
171 mask = mask | (u32(1) << u32(line))
172 end if
173 end for
174end whole_lines
175
176-- Wait until the ring holds WANTED bytes, or LIMIT milliseconds pass.
177await: (inout r: serial.rx, wanted: usize, limit: u32)
178 -> (have: usize) ! serial.receive_error =
179 began := timer_events
180 loop do
181 take_events()
182 have = try serial.available(r)
183 break when have >= wanted or timer_events -% began >= limit
184 rest()
185 end loop
186end await
187
188-- One transmit of COUNT bytes of the buffer on DMA1 stream 0, memory to
189-- USART3's TDR, ordered as RM0433's transmission using DMA orders it
190-- (48.5.19, p. 2057) and as the stream's direct mode needs it (D979). In
191-- direct mode a memory-to-peripheral stream, once enabled, preloads its
192-- first byte from memory and sends it at the peripheral's first request
193-- (15.3.8, p. 644; 15.3.14, p. 653); a request already raised when the
194-- stream is enabled arrives before that preload, an underrun that sets
195-- FEIF (15.3.20, p. 657), and on the first chip run with CR3's DMAT held
196-- set across both transmits, FEIF was set at each and each lost its first
197-- byte with its count at zero. So the stream is configured and enabled
198-- with DMAT clear, TC is cleared through ICR's TCCF, and only then is DMAT
199-- set, raising USART3's request with the first byte preloaded. The end
200-- is awaited as 48.5.19 says: the stream's EN clear with its count at
201-- zero, DMAT cleared so no request is raised while no stream serves it,
202-- and then TC, every byte, the FIFO's among them, on the line before the
203-- CPU writes the next frame, which would otherwise race the stream for the
204-- FIFO's last slot. D227's device barrier stands before the enable; the
205-- buffer's clean, or its omission, is the caller's. Whether every byte
206-- went with no transfer error.
207transmit: (count: u32) -> (done: bool) =
208 done = false
209 dma1.s0m0ar_write(dma1.base, u32(usize(addr tx_storage[0])))
210 dma1.s0ndtr_write(dma1.base, count)
211 dma1.lifcr_write(dma1.base, stream_flags)
212 mut stream := dma1.s0cr_decode(0)
213 stream.f_dir = 1
214 stream.f_minc = 1
215 stream.f_trbuff = 1
216 config := dma1.s0cr_encode(stream)
217 dma1.s0cr_write(dma1.base, config)
218 cortex_m.device_barrier()
219 dma1.s0cr_write(dma1.base, config | stream_enable)
220 mut cleared := usart3.icr_decode(0)
221 cleared.f_tccf = 1
222 usart3.icr_write(usart3.base, usart3.icr_encode(cleared))
223 mut requests := usart3.cr3_decode(0)
224 requests.f_dmat = 1
225 cortex_m.device_barrier()
226 usart3.cr3_write(usart3.base, usart3.cr3_encode(requests))
227 mut polls: u32 = 0
228 while (dma1.s0cr_read(dma1.base) & stream_enable) <> 0 do
229 polls += 1
230 if polls == 10_000_000 then
231 usart3.cr3_write(usart3.base, 0)
232 return
233 end if
234 end while
235 usart3.cr3_write(usart3.base, 0)
236 transmit_flags = dma1.lisr_read(dma1.base) & stream_flags
237 transmit_left = dma1.s0ndtr_read(dma1.base)
238 polls = 0
239 while (usart3.isr_read(usart3.base) & transmission_complete) == 0 do
240 polls += 1
241 return when polls == 10_000_000
242 end while
243 done = transmit_left == 0 and (transmit_flags & transfer_error_flag) == 0
244 dma1.lifcr_write(dma1.base, stream_flags)
245 cortex_m.device_barrier()
246end transmit
247
248-- The message a maintained transmit carries: A to Z, again and again.
249message: (index: usize) -> (byte: u8) = byte = u8(65 + index % 26) end message
250
251start: () -> none =
252 control := scs.ccr()
253 geometry := l1.geometry()
254 bringup.peripherals()
255 caches := l1.new()
256 -- The control's provider: the maintenance left out.
257 bare := cache.cacheless(32) else (problem)
258 _ = problem
259 halt(1)
260 end
261 ring := rx_storage[0..<256]
262 mut receiver := serial.open(caches, line.default_settings, budget,
263 rx_storage[0..<256]) else (problem)
264 _ = problem
265 halt(2)
266 end
267 scs.enable_interrupt(11)
268 scs.enable_interrupt(39)
269 scs.start_ticks(bringup.cycles_ms)
270 mut scratch: [64]u8 = zeroed
271 mut first: usize = 0
272 while first == 0 do
273 take_events()
274 first = serial.read(caches, receiver, scratch[0..<1]) else (problem)
275 _ = problem
276 halt(3)
277 end
278 if first == 0 then rest() end if
279 end while
280 report.open_frame(98)
281 report.field(99, control)
282 report.field(103, geometry)
283 report.field(108, u32(cache.line(caches)))
284 report.field(118, scs.vtor())
285 report.field(115, u32(scratch[0]))
286 report.close_frame()
287
288 -- The stale-read control: the ring's refill left cached and clean, and
289 -- the stream rearmed and read through the cacheless provider, without
290 -- the invalidates D227 orders.
291 serial.stop(receiver) else (problem)
292 _ = problem
293 halt(4)
294 end
295 fill(rx_storage[0..<256], ring_fill)
296 cache.clean(caches, ring) else (problem)
297 _ = problem
298 halt(4)
299 end
300 serial.restart(bare, receiver) else (problem)
301 _ = problem
302 halt(4)
303 end
304 report.open_frame(99)
305 report.field(110, u32(control_bytes))
306 report.close_frame()
307 have := await(receiver, control_bytes, patience) else (problem)
308 _ = problem
309 halt(5)
310 end
311 if have < control_bytes then halt(5) end if
312 seen := serial.read(bare, receiver, scratch[0..<control_bytes]) else (problem)
313 _ = problem
314 halt(5)
315 end
316 stale := matching(scratch[0..<seen], ring_fill)
317 stale_lines := whole_lines(scratch[0..<seen], ring_fill)
318 seen_hash := fnv(fnv_basis, scratch[0..<seen])
319 -- The ring handed back with its maintenance: restart cleans and
320 -- invalidates it, so what is read of it next is memory's, the bytes
321 -- the stream wrote.
322 serial.restart(caches, receiver) else (problem)
323 _ = problem
324 halt(5)
325 end
326 memory_hash := fnv(fnv_basis, rx_storage[0..<control_bytes])
327 report.open_frame(67)
328 report.field(110, u32(seen))
329 report.field(115, stale)
330 report.field(107, stale_lines)
331 report.field(97, seen_hash)
332 report.field(109, memory_hash)
333 report.close_frame()
334
335 -- The maintained stream.
336 mut total: usize = 0
337 mut hash := fnv_basis
338 while total < stream_bytes do
339 waiting := await(receiver, 1, patience) else (problem)
340 _ = problem
341 halt(6)
342 end
343 if waiting == 0 then halt(6) end if
344 room := if stream_bytes - total < 64 then stream_bytes - total else 64 end if
345 n := serial.read(caches, receiver, scratch[0..<room]) else (problem)
346 _ = problem
347 halt(6)
348 end
349 hash = fnv(hash, scratch[0..<n])
350 total += n
351 end while
352 report.open_frame(69)
353 report.field(110, u32(total))
354 report.field(120, hash)
355 report.field(119, serial.laps())
356 report.close_frame()
357
358 -- The lost-dirty control and the refused ranges, the CPU's alone.
359 fill(probe[0..<32], 0x11)
360 cache.clean(caches, probe[0..<32]) else (problem)
361 _ = problem
362 halt(7)
363 end
364 fill(probe[0..<32], 0x22)
365 cache.invalidate(caches, probe[0..<32]) else (problem)
366 _ = problem
367 halt(7)
368 end
369 lost := matching(probe[0..<32], 0x11)
370 fill(probe[32..<64], 0x33)
371 cache.clean(caches, probe[32..<64]) else (problem)
372 _ = problem
373 halt(7)
374 end
375 fill(probe[32..<64], 0x44)
376 mut refused: u32 = 0
377 cache.invalidate(caches, probe[33..<64]) else (problem)
378 if problem == cache.misaligned then refused += 1 end if
379 end
380 cache.invalidate(caches, probe[32..<63]) else (problem)
381 if problem == cache.misaligned then refused += 1 end if
382 end
383 cache.clean_invalidate(caches, probe[16..<48]) else (problem)
384 if problem == cache.misaligned then refused += 1 end if
385 end
386 cache.invalidate(caches, probe[0..<48]) else (problem)
387 if problem == cache.misaligned then refused += 1 end if
388 end
389 kept := matching(probe[32..<64], 0x44)
390 cache.clean_invalidate(caches, probe[32..<64]) else (problem)
391 _ = problem
392 halt(7)
393 end
394 written := matching(probe[32..<64], 0x44)
395 report.open_frame(100)
396 report.field(108, lost)
397 report.field(114, refused)
398 report.field(107, kept)
399 report.field(119, written)
400 report.close_frame()
401
402 -- The transmits: stream 0 now serves USART3's transmitter, DMAMUX1
403 -- request 46, usart3_tx_dma (Table 124, p. 697), and CR3's DMAT.
404 serial.stop(receiver) else (problem)
405 _ = problem
406 halt(8)
407 end
408 mut request := dmamux1.dmamux_c0cr_decode(0)
409 request.f_dmareq_id = 46
410 dmamux1.dmamux_c0cr_write(dmamux1.base, dmamux1.dmamux_c0cr_encode(request))
411 dma1.s0par_write(dma1.base, u32(usart3.tdr_address))
412 -- Neither of CR3's DMA requests: the receive's is the stopped ring's,
413 -- and the transmit's is raised by each transmit once its stream has
414 -- its first byte.
415 usart3.cr3_write(usart3.base, 0)
416 -- The lost-clean control: memory `o`, the message only in the cache.
417 fill(tx_storage[0..<64], transmit_memory)
418 cache.clean(caches, tx_storage[0..<64]) else (problem)
419 _ = problem
420 halt(8)
421 end
422 for index in 0..<lenof tx_storage do tx_storage[index] = message(index) end for
423 report.open_frame(120)
424 report.field(110, 64)
425 report.close_frame()
426 unclean := transmit(64)
427 report.open_frame(88)
428 report.field(116, if unclean then 1 else 0 end if)
429 report.field(102, transmit_flags)
430 report.field(114, transmit_left)
431 report.close_frame()
432 cache.clean(caches, tx_storage[0..<64]) else (problem)
433 _ = problem
434 halt(9)
435 end
436 report.open_frame(121)
437 report.field(110, 64)
438 report.close_frame()
439 cleaned := transmit(64)
440 report.open_frame(89)
441 report.field(116, if cleaned then 1 else 0 end if)
442 report.field(102, transmit_flags)
443 report.field(114, transmit_left)
444 report.close_frame()
445
446 report.open_frame(101)
447 report.field(99, scs.ccr())
448 report.close_frame()
449end start