Hello,
using the PWM module with the Märklin "M" track is effectively not advisable.
Isolating every block from its neighbors is not easy.
It might be possible to modify the PWM modules to have a common ground, but I have never tried this.
The L298 has "Full-Bridge" drivers but it is always possible to use each driver in an "Half-Bridge" configuration. Only one output of each driver would be used.
In this case a dual supply is needed (e.g.: +16V, -16V), for powering the PWM modules and the train layout.
The processor must have its own supply (0, +5V), with the logical ground (0V) connected to -16V.
This way, the logic should still operate normally. The current sensing will only be active on the negative supply.
The positive supply will have no more current sensing capability.
This should not be a problem for detecting a locomotive but a current limiter is needed on the +16V to avoid destroying the emitter-follower in case of short-cut on the track.
(The L298 is very reliable, but this "half-bridge" configuration is not the standard way to use it!).
All the 2k2 resistors for the EMF measurement must be removed because the mean track voltage related to the signal ground is no more 0V but +16V!
Anyway, measuring the EMF is useless, Märklin AC motors do not produce EMF...
This is only a suggestion, this needs to be experimented...!
Best regards,
Joel