Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Memory Bits To Bitfield Definition

Memory Bits To Bitfield Definition

Scheduled Pinned Locked Moved C / C++ / MFC
hardwareperformancetutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Trevor Johansen
    wrote on last edited by
    #1

    I would like to address several bytes of memory but different bits from different locations. The program I am using is on an embedded system and I need access to different pieces of the peripherals. Specifically when dealing with ports where something connected crosses multiple ports and I need a modular way to access them through a simple 'maskport = value' and you only need to modify some macros or a enum to change things instead of each functions code. IS this even allowed or am I out to lunch? (I am using GCC) For example: (this doesn't work btw but it was my attempt)

    struct bits {
    uint8_t b0:1;
    uint8_t b1:1;
    uint8_t b2:1;
    uint8_t b3:1;
    uint8_t b4:1;
    uint8_t b5:1;
    uint8_t b6:1;
    uint8_t b7:1;
    } __attribute__((__packed__));

    #define SBIT(port,pin) ((*(volatile struct bits*)&port).b##pin)

    struct FIELD {
    uint8_t b0:1;
    uint8_t b1:1;
    uint8_t b2:1;
    uint8_t b3:1;
    uint8_t b4:1;
    uint8_t b5:1;
    uint8_t b6:1;
    uint8_t b7:1;
    };

    struct FIELD test;

    test.b0 = SBIT(0xE2, 0);
    test.b1 = SBIT(0xE2, 4);
    test.b2 = SBIT(0xE2, 7);
    test.b3 = SBIT(0xEF, 2);
    test.b4 = SBIT(0xEF, 3);
    test.b5 = SBIT(0x07, 1);
    test.b6 = SBIT(0x07, 5);
    test.b7 = SBIT(0x07, 6);

    test = 0x7A; // 0111 1010

    That way by assigning a value to test: test = 0x7A; // 0111 1010 0xE2: 0001 0000 0xEF: 0000 1100 0x07: 0010 0010

    U 1 Reply Last reply
    0
    • T Trevor Johansen

      I would like to address several bytes of memory but different bits from different locations. The program I am using is on an embedded system and I need access to different pieces of the peripherals. Specifically when dealing with ports where something connected crosses multiple ports and I need a modular way to access them through a simple 'maskport = value' and you only need to modify some macros or a enum to change things instead of each functions code. IS this even allowed or am I out to lunch? (I am using GCC) For example: (this doesn't work btw but it was my attempt)

      struct bits {
      uint8_t b0:1;
      uint8_t b1:1;
      uint8_t b2:1;
      uint8_t b3:1;
      uint8_t b4:1;
      uint8_t b5:1;
      uint8_t b6:1;
      uint8_t b7:1;
      } __attribute__((__packed__));

      #define SBIT(port,pin) ((*(volatile struct bits*)&port).b##pin)

      struct FIELD {
      uint8_t b0:1;
      uint8_t b1:1;
      uint8_t b2:1;
      uint8_t b3:1;
      uint8_t b4:1;
      uint8_t b5:1;
      uint8_t b6:1;
      uint8_t b7:1;
      };

      struct FIELD test;

      test.b0 = SBIT(0xE2, 0);
      test.b1 = SBIT(0xE2, 4);
      test.b2 = SBIT(0xE2, 7);
      test.b3 = SBIT(0xEF, 2);
      test.b4 = SBIT(0xEF, 3);
      test.b5 = SBIT(0x07, 1);
      test.b6 = SBIT(0x07, 5);
      test.b7 = SBIT(0x07, 6);

      test = 0x7A; // 0111 1010

      That way by assigning a value to test: test = 0x7A; // 0111 1010 0xE2: 0001 0000 0xEF: 0000 1100 0x07: 0010 0010

      U Offline
      U Offline
      User 3919723
      wrote on last edited by
      #2

      If I understood, you can try with a union: union FIELDEX { struct FIELD { uint8_t b0:1; uint8_t b1:1; uint8_t b2:1; uint8_t b3:1; uint8_t b4:1; uint8_t b5:1; uint8_t b6:1; uint8_t b7:1; } fld; uint8_t val; }; FIELDEX test; test.val = 0x7A; // 0111 1010 test.fld.b0 = 1; // set bit 0

      T 1 Reply Last reply
      0
      • U User 3919723

        If I understood, you can try with a union: union FIELDEX { struct FIELD { uint8_t b0:1; uint8_t b1:1; uint8_t b2:1; uint8_t b3:1; uint8_t b4:1; uint8_t b5:1; uint8_t b6:1; uint8_t b7:1; } fld; uint8_t val; }; FIELDEX test; test.val = 0x7A; // 0111 1010 test.fld.b0 = 1; // set bit 0

        T Offline
        T Offline
        Trevor Johansen
        wrote on last edited by
        #3

        I would still have the problem of setting separate address bits, however your example would make a good compact bitfield definition. I am trying to take bits from separate registers and create my own 'masked register' so when I write to my register it sends each bit to its specified bits in other registers.

        test.b0 = SBIT(0xE2, 0); // 0xE2: 0000000x
        test.b1 = SBIT(0xE2, 4); // 0xE2: 000x0000
        test.b2 = SBIT(0xE2, 7); // 0xE2: x0000000

        test.b3 = SBIT(0xEF, 2); // 0xEF: 000000x0
        test.b4 = SBIT(0xEF, 3); // 0xEF: 00000x00

        test.b5 = SBIT(0x07, 1); // 0x07: 000000x0
        test.b6 = SBIT(0x07, 5); // 0x07: 00x00000
        test.b7 = SBIT(0x07, 6); // 0x07: 0x000000

        Hopefully this will show more clearly what I am after:

        test = 0x7A; // 0111 1010

        // 0xE2 gets bits xxxxx010
        // xx0 = 0xE2 bit 0
        // x1x = 0xE2 bit 4
        // 0xx = 0xE2 bit 7
        // 0xE2 = 00010000

        // 0xEF gets bits xxx11xxx
        // x1 = 0xEF bit 2
        // 1x = 0xEF bit 3
        // 0xEF = 00001100

        // 0x07 gets bits 011xxxxx
        // xx1 = 0x07 bit 1
        // x1x = 0x07 bit 5
        // 0xx = 0x07 bit 6
        // 0x07 = 00100010

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups