Embedded chip documentation is sometimes very bad, and very confusing. Frequently it's difficult to really understand the ins and outs of a given chip without simply getting experience.

Which is why Mr. Scrith was a bit surprised with this code, which came from someone who definitely should have known better.

This code is for a "walking 1s" memory test- it simply visits every address in memory and writes a "1" and confirms that it can read back that "1". Such a test poses an interesting challenge: you can't use any variables, because they will live in RAM somewhere, so you need to limit yourself only to the registers exposed by your CPU.

And that's where this developer made their odd choice. This particular CPU had plenty of general purpose registers, and a bunch of special purpose registers, like a set of registers for controlling PWM generators. The developer either couldn't find the correct way to access the general purpose registers or didn't care to try, but either way, the end result was that they used the PWM registers for everything:

PWMDTY01 = RAM_BASE; // address PWMDTY23 = START_VALUE; // value PWMDTY4 = 30; // counter PWMDTY5 = 0; // retries for ( PWMDTY5 = 0; PWMDTY5 < 2; PWMDTY5 ++ ) { for (PWMDTY01 = RAM_BASE; PWMDTY01 < RAM_END; PWMDTY01 += sizeof(uint16_t) ) { *((uint16_t*)PWMDTY01) = PWMDTY23; PWMDTY23 <<= 1; if ( PWMDTY23 == 0 ) { PWMDTY23 = START_VALUE; } } for ( PWMDTY4 = 0; PWMDTY4 < 16; PWMDTY4 ++ ) { PWMDTY23 = _PowOfTwo_16[PWMDTY4]; for (PWMDTY01 = RAM_BASE; PWMDTY01 < RAM_END; PWMDTY01 += sizeof(uint16_t) ) { if ( *((uint16_t*)PWMDTY01) != PWMDTY23 ) { PWMDTY4 = 30; PWMDTY01 = RAM_END; } PWMDTY23 <<= 1; if ( PWMDTY23 == 0 ) PWMDTY23 = START_VALUE; *((uint16_t*)PWMDTY01) = PWMDTY23; } } if ( PWMDTY4 <= 16 ) { for (PWMDTY01 = RAM_BASE; PWMDTY01 < RAM_END; PWMDTY01 += sizeof(uint16_t) ) { *((uint16_t*)PWMDTY01) = 0; PWMDTY5 = 2; } } } if ( PWMDTY4 > 16 ) { ramTestPassed = FALSE; } else { ramTestPassed = TRUE; }

The code works, but it's certainly an odd choice. When Mr. Scrith suggested that PWMDTY23 wasn't the clearest choice of register, the developer said, "Oh, should I #define a macro no name it more clearly?"

[Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today!