Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
programon a running part stops with a BDM error; on a halted one it fails withFACCERRbecauseFCDIVwas never loaded.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
UsbdmFlashProgrammerdoes(
src/hcs08.zig):connect; halt, and a check that BDMACT is setSOPT=0x22)FCDIVfrom the measured bus clock (flash clock150-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 alternateclock, 2x the bus on ICS parts.
connectnow labels the latter "bdc clock".the image leaves them unset
--trim <kHz>re-trims (src/clock.zig): BDM clock measured at a low speed while thetrim 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.
--nvlocoverrides the location, whose default is byclock type (it lives in USBDM's
DeviceData.cpp, not the device XML). ICS variants only.rows are unchanged
unsecuresay that the trim went with it;unsecurecarries on past setupsteps a secured part refuses
The simulator now models
FCDIV(write-once,FACCERRon a command while unloaded,enforced on
--sim) and an ICS clock, so all of this runs end to end against the realroutine on the CPU08 core.
Tested
MC9S08SH16 (ICSV2), JS16 pod, Linux. Bring-up leaves bus 8.629 MHz and
FCDIV=0xAB,the same as PGO.
--trimagainst PGO-trim=, same image; TRIM/FTRIM bytes and thefirmware's resulting bus clock:
00 9900 9901 8101 8101 7501 75Mass-erased part ->
program --trim 33.6restores01 81and a flash identical to theimage.
zig build test: 202 pass.Please look at
program,verify,erase,blank-check,unsecure,secure) now resets and halts the target first.FCDIValreadylocked, even with a halt. The software reset (the DB's default for HCS08, what PGO uses)
does. I don't know why.
(PGO only clears the LSB). Fixed, with a regression test.
preserve behaviour but no
--trim; ICSV4/V5 follow the reference's parameters but areuntested.