Lundin wrote:
It would be great if Freescale made their registers compatible with the C language. C programmers clearing flag registers with read-modify-write
Freescale registers *are* compatible with C.
is a very common bug I see in code written for Freescale processors. From a C programming point-of-view, the programmer has done nothing wrong. His code can be fully compatible with ISO C, as well as a bunch
Nothing wrong, just a code with a bug.
of programming standards for safety-critical systems, yet it contains this bug.
The same applies to oddities such as the SPI and SCI status flags that are cleared by reading the register. The C standard does not cover that.
Freescale isn't alone and not the first to use such technique. For example old already Philips SCC2692 UART chip. IIRC it had status/control register. When you read it you read status, when you write it you write access write-only control register.
A read instruction can never be a write instruction, or so you thought before writing code for Freescale micros. Plus, all the debugger
Something new to learn, thought after code is done :smileyhappy:.
manufacturers are unaware of these registers and like to read them as often as possible, ruining your debug-session. Codewarrior is one example of this.
It's bad, but it's also good. When FSL releases old family but new chip, with more SCI's or different addresses, debugger doesn't have to be redone for this tiny change. Codewarrior is part of FSL and has all necessary informations in time, but what about third parties?
You can always argue and say that the programmer should know the hardware. But it would be a whole lot easier for people to learn the
That's right, programmer should know hardware.
hardware if a flag was cleared in a sane way, ie by writing 0 to it. Not by writing 1 to it, or reading it. All three combinations exist in the S12.
No.
In FSL, in order to keep flags untouched, we write all zeros to flags register. In Microchip PIC - we should write all ones! Why PIC works with such "careless programming" and FSL doesn't? I think it's because PIC peripherals are synchronized to instruction clock, which is osc/4; and PIC completes its R-M-W instruction (everything: read, modify and write) at osc clock. Hardware can't set more flags while CPU is modifying others using bit set/ bit clear instructions. In contrast FSL CPU12 and peripherals are operating at the same clock. Instruction takes more than one clock tick. Peripheral can flip some bits after CPU read flags but before it wrote back to flags. Writing an one or writing a zero ... it's practically the same. I prefer writing 0x10 to clear bit4, rather than writing 0xEF. Two solutions exist:
1) PIC variant: clocking peripherals at bset/bclr instruction time. 2) S08 variant: not packing more than one flag to the same register. (Note again that recent S08 additions: IIC, flash, and maybe more peripherals have more than one flag per register and thus are not bitfields compatible).
Regards