I was looking into this ITE8712 watchdog timer demo code when I saw this:
void InitWD(char cSetWatchDogUnit, char cSetTriggerSignal)
{
OpenIoConfig(); //open super IO of configuration for Super I/O
SelectIoDevice(0x07); //select device7
//set watch dog counter of unit
WriteIoCR(0x72, cSetWatchDogUnit|cSetTriggerSignal);
//CloseIoConfig(); //close super IO of configuration for Super I/O
}
and, I wonder what is meant by this line:
cSetWatchDogUnit|cSetTriggerSignal
because the WriteIoCR function looks like this:
void WriteIoCR(char cIndex, char cData)
{
//super IO of index port for Super I/O
//select super IO of index register for Super I/O
outportb(equIndexPort,cIndex);
//super IO of data for Super I/O
//write data to data register
outportb(equDataPort,cData);
}
So cIndex should be 0x72, but what about the cData? I really don’t get the “|” thing as I’ve only used it for OR (“||”) in a conditional statement.
It’s a bitwise
or, as distinct to your normal logicalor. It basically sets the bits in the target variable if the corresponding bit in either of the source variables was set.For example, the expression
43 | 17can be calculated as:See this answer for a more thorough examination of the various bitwise operators.
It’s typically used when you want to manipulate specific bits within a data type, such as control of a watchdog timer in an embedded system (your particular use case).
You can use
or (|)to turn bits on andand (&)to turn them off (with the inversion of the bitmask that’s used to turn them on.So, to turn on the
b3bit, use:To turn it off, use:
To detect if
b3is currently set, use:You’ll typically see the bitmasks defined something like:
or:
As to what this means:
More than likely,
0x72will be an I/O port of some sort that you’re writing to andcSetWatchDogUnitandcSetTriggerSignalwill be bitmasks that you combine to output the command (set the trigger signal and use a unit value for the watchdog). What that command means in practice can be inferred but you’re safer referring to the documentation for the watchdog circuitry itself.And, on the off chance that you don’t know what a watchdog circuit is for, it’s a simple circuit that, if you don’t kick it often enough (with another command), it will reset your system, probably by activating the reset pin on whatever processor you’re using.
It’s a way to detect badly behaving software automatically and return a device to a known initial state, subscribing to the theory that it’s better to reboot than continue executing badly.