Skip to content

Hcs08 init trim - #2

Open
udv2g wants to merge 2 commits into
0xJustus:mainfrom
udv2g:hcs08-init-trim
Open

udv2g wants to merge 2 commits into
0xJustus:mainfrom
udv2g:hcs08-init-trim

Conversation

@udv2g

@udv2g udv2g commented Oct 7, 2026

Copy link
Copy Markdown

HCS08: flash bring-up (reset, COP, clock, FCDIV) and oscillator trim

Stacked on the XFTMRK PR; please review HCS08: flash bring-up ... only until that merges.

Problem

The small-code HCS08 routine assumes a halted target, a loaded flash divider and a COP
that won't fire. Nothing in the tool makes that true:

  • program on a running part stops with a BDM error; on a halted one it fails with
    FACCERR because FCDIV was never loaded.
  • The page erase also takes the sector holding the oscillator trim (0xFFAE/F on ICS
    parts), so a "successful" program of a normal image leaves the part untrimmed and
    running ~30 % slow.

What changed

Before the routine runs, HCS08 flash commands now do what UsbdmFlashProgrammer does
(src/hcs08.zig):

  • software special reset, connect; halt, and a check that BDMACT is set
  • COP off (SOPT=0x22)
  • ICS clock to a known speed, then FCDIV from the measured bus clock (flash clock
    150-200 kHz, PRDIV8 above 12.8 MHz) and FPROT unprotected. The BDC is switched to the
    bus clock (CLKSW=1) so the pod's reading is the bus; otherwise it reports the alternate
    clock, 2x the bus on ICS parts. connect now labels the latter "bdc clock".
  • by default the chip's own trim bytes are copied into the image before the erase, where
    the image leaves them unset
  • --trim <kHz> re-trims (src/clock.zig): BDM clock measured at a low speed while the
    trim is searched (8-step binary search on TRIM, then a +/-8 sweep of the 9-bit value, 2
    samples each, least-squares fit), result written into the image, merging FTRIM into an
    NVFTRIM the image already defines. --nvloc overrides the location, whose default is by
    clock type (it lives in USBDM's DeviceData.cpp, not the device XML). ICS variants only.
  • the device table gains the clock type/address and SOPT for HCS08 rows; other families'
    rows are unchanged
  • mass erase / unsecure say that the trim went with it; unsecure carries on past setup
    steps a secured part refuses

The simulator now models FCDIV (write-once, FACCERR on a command while unloaded,
enforced on --sim) and an ICS clock, so all of this runs end to end against the real
routine on the CPU08 core.

Tested

MC9S08SH16 (ICSV2), JS16 pod, Linux. Bring-up leaves bus 8.629 MHz and FCDIV=0xAB,
the same as PGO. --trim against PGO -trim=, same image; TRIM/FTRIM bytes and the
firmware's resulting bus clock:

target this tool PGO bus ideal
31.25 kHz 00 99 00 99 7.983 MHz 8.000
33.6 kHz 01 81 01 81 8.56-8.61 MHz 8.602
35.0 kHz 01 75 01 75 8.961 MHz 8.960

Mass-erased part -> program --trim 33.6 restores 01 81 and a flash identical to the
image. zig build test: 202 pass.

Please look at

  • Behaviour change: every HCS08 command that uses the routine (program, verify,
    erase, blank-check, unsecure, secure) now resets and halts the target first.
  • Hardware special reset didn't work here: the CPU stayed running with FCDIV already
    locked, even with a halt. The software reset (the DB's default for HCS08, what PGO uses)
    does. I don't know why.
  • The first version zeroed TRIM to clear FTRIM and lost the BDM at the 50 % clock swing
    (PGO only clears the LSB). Fixed, with a regression test.
  • Not covered: the gdb flash-load path doesn't get the bring-up; ICG/MCG parts get the
    preserve behaviour but no --trim; ICSV4/V5 follow the reference's parameters but are
    untested.

Vendor USBDM's HCS12-XMMC-XFTMRK-flash-program.s19 and route the paged
S12X device entries to it. The routine uses the same large-code ABI as
GMMC, so the driver is unchanged. Five parts gain geometry:
mc9s12xep100-family, xs64, xs128, xs256 and xf512. Every existing table
entry is byte-identical.

- gen_devices: accept the XFTMRK routine. XMMC entries give the reset
  page (0xFE) as pageReset and size the window with `pages`, so the
  lowest page is derived from pageEnd and the page count (1024K -> C0).
  MMCV4 keeps using pageReset.
- prepareFlash: every HCS12 flash operation now starts with a
  special-mode hardware reset and a reconnect, as the reference
  programmer does. A target running its own code acks memory access but
  not the PC write that starts the routine (BDM_RC_ACK_TIMEOUT), so
  flashing a running board failed.
- XFTMRK expects FCLKDIV to be loaded already and otherwise reports an
  invalid divider. Port the reference Tcl initTarget/initFlash: COP off,
  FCLKDIV from 2x the special-mode bus clock, FPROT/EPROT unprotected.
- --sim refuses XFTMRK parts; there is no S12XE flash model and the FTS
  one would report a meaningless pass.

Tested on an MC9S12XET256 (SDID 0xC081) with a JS16 USBDM on macOS:
unsecure (mass erase, then FSEC=0xFE), then program with verify of a
61 KB image across both fixed windows and four PPAGE pages. A raw-BDM
readback of every window, setting PPAGE directly, matched all 61,825
bytes. After a normal reset the board runs the image: halted in the
application's main loop, PPAGE=0xFB.
The small-code HCS08 routine assumes a halted target with a loaded flash
clock divider and a COP that is not about to fire. Nothing in the tool made
that true: `program` on a running part stopped on a BDM error, and on a
halted one failed with FACCERR because FCDIV was never loaded. And because
the page erase takes the sector holding the oscillator trim (0xFFAE/F on ICS
parts), a successful program of a normal image left the part untrimmed and
running ~30 % slow.

Do what UsbdmFlashProgrammer does before the routine runs (hcs08.zig):

- software special reset, `connect; halt` and a check that BDMACT is set. The
  halt is required: BDCSCR reads 0x88 right after the reset, the firmware is
  already running and FCDIV is locked. The software reset is the device DB's
  default for HCS08; on a bench SH16 a hardware special reset left FCDIV
  locked even with the halt (cause not established).
- COP off (SOPT=0x22, the address now comes from the device table).
- the ICS clock to a known speed, then FCDIV from the measured bus clock
  (flash clock 150..200 kHz, PRDIV8 above 12.8 MHz) and FPROT unprotected.
  The BDC is switched to the bus clock (CLKSW=1) so the pod's reading IS the
  bus; without it the pod reports the alternate clock, 2x the bus on ICS.
  `connect` now labels that figure "bdc clock" when CLKSW is clear.
- by default, the chip's own trim bytes are copied into the image before the
  erase wherever the image leaves them unset.
- `--trim <kHz>` re-trims (clock.zig): the BDM clock is measured at a low
  speed while the trim is searched (8-step binary search on TRIM, then a +/-8
  sweep of the 9-bit value, 2 samples each, least-squares fit), and the trim
  bytes are written into the image, merging FTRIM into an NVFTRIM the image
  already defines. `--nvloc` overrides the location, whose default is by
  clock type (it lives in USBDM's DeviceData.cpp, not the device XML).
  Unlike the reference's stale loop-variable range test, the fitted value is
  range-checked. ICS variants only; ICG/MCG get the preserve behaviour.
- the device table gains the clock type and register address and SOPT for
  HCS08 (gen_devices); no other family's rows change.
- mass erase / unsecure say that the trim went with it; `unsecure` carries on
  past setup steps a secured part refuses.

The simulator now models FCDIV (write-once, FACCERR on a command while
unloaded, enforced on the --sim path) and an ICS clock, so these paths run
end to end against the real routine on the CPU08 core. Tests: 200 pass.

Tested on an MC9S08SH16 (ICSV2) through a JS16 pod on Linux. Programming the
bench test image: bus 8.629 MHz and FCDIV 0xAB, the same values PGO leaves;
the existing trim survives the sector erase. `--trim` against PGO
(-trim=, same image), trim bytes TRIM/FTRIM and the firmware's resulting bus
clock:

  33.60 kHz   01 81   01 81   8.56/8.61 MHz  (ideal 8.602)
  31.25 kHz   00 99   00 99   7.983 MHz      (ideal 8.000)
  35.00 kHz   01 75   01 75   8.961 MHz      (ideal 8.960)

Mass-erased part -> `program --trim 33.6` restores 01 81 and a flash
identical to the image. A first version zeroed the TRIM byte to clear FTRIM
and lost the BDM at the 50 % clock swing (PGO only clears the LSB); fixed and
covered by a test.

Not covered: the gdb flash-load path still builds its routine without this
bring-up; clock types other than ICS are not trimmed; ICSV4/V5 follow the
reference's parameters but are untested.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant