FDD-book/examples/part-01/ch04-a-first-look-at-the-c-programming-language/final-practice/lab1-bit-flags/flags.c
Edson Brandi 6e6dac94fe Add book companion files under examples/
Organize companion code and materials by book part:

  part-01/      Foundations
  part-02/      Kernel & driver fundamentals
  part-03/      Character & block devices
  part-04/      Networking & storage
  part-05/      Buses & advanced topics
  part-06/      Debugging & performance
  part-07/      Real-world drivers
  appendices/   Supplementary material
  README.md     Overview and build instructions
2026-04-25 16:51:00 +00:00

24 lines
862 B
C

#include "flags.h"
/*
* Print both the hex value and a friendly list of which flags are set.
* This mirrors how driver debug output often looks in practice.
*
* The `first` variable tracks whether we are printing the first flag
* (no leading comma) or a later one (comma separator).
*/
void
print_state(uint32_t st)
{
int first = 1;
printf("State: 0x%08x [", st);
if (test_flag(st, DF_ENABLED)) { printf("%sENABLED", first ? "" : ","); first = 0; }
if (test_flag(st, DF_OPEN)) { printf("%sOPEN", first ? "" : ","); first = 0; }
if (test_flag(st, DF_ERROR)) { printf("%sERROR", first ? "" : ","); first = 0; }
if (test_flag(st, DF_TX_BUSY)) { printf("%sTX_BUSY", first ? "" : ","); first = 0; }
if (test_flag(st, DF_RX_READY)) { printf("%sRX_READY", first ? "" : ","); first = 0; }
if (first)
printf("none");
printf("]\n");
}