Testing bits:
if (*pStatus & 0x08)
{…}’
(0x4C) 0100 1100 & 0000 1000 (0x08) = 0000 1000
Setting bits:
*pStatus |= 0x10
(0x4C) 0100 1100 | 0001 0000 (0x10) = 0101 1100
Clearing bits:
*pStatus &= ~(0x04)
(0x5C) 0101 1100 & 1111 1011 = 0101 1000
Toggling bits:
*pStatus ^= 0x80
(0x58) 0101 1000 ^ 1000 0000 = 1101 1000
Above is the code to toggle bit 7.
For write-only register, you cannot use ‘ &=, |= …”. A copy of the register’s contents should be held in a variable in RAM to maintain the current state of the write-only register.
status_tmp = STATUS;
*pStatus_writeonly = status_tmp;
/* when you want to change the content of register: */
status_tmp |= STATUS_ENABLE;
*pStatus_writeonly = status_tmp;
Struct Overlays
Benefits of struct overlays are that you can read/write through a pointer to the struct, and the compiler does the address construction at compile time.
typedef struct
{
uint16_t count; /* Offset 0x00 */
uint16_t maxCount; /* Offset 0x02 */
uint16_t _reserved1; /* Offset 0x04 */
uint16_t control; /* Offset 0x06 */
} volatile timer_t;
timer_t *pTimer = (timer_t *)(0xABCD0123);
No comments:
Post a Comment