Shop OBEX P1 Docs P2 Docs Learn Events
problem using bit-field with INA — Parallax Forums

problem using bit-field with INA

proplemproplem Posts: 233
edited 2012-01-08 19:00 in Propeller 1
Hello developers,

is there a way to use INA, OUTA etc. as variables pointable via bit-fields?

Compiling the code below i'm getting this error:
make -k
propeller-elf-gcc -c -Os -wall -mlmm prjh0040.c -o prjh0040.o
prjh0040.c: In function 'main':
prjh0040.c:102:34: error: address of global register variable '_INA' requested
make: *** [prjh0040.o] Error 1
make: Target `prjh0040.elf' not remade because of errors.

Or maybe you could implement it in future?
Using bit-fields are syntactic sugar in C i would greatly appreciate.

Thanks in advance,
proplem
/*
    2012-01-07 07:14 schmidt5 toggle pins with switches

    This shows an example for creating an LMM program and Makefile.  It
    simply toggles pins as indicated by switches 'S1' and 'S2'.

    Hardware: Propeller D40-1 V1.0 Board ([URL="http://www.tdus.de"]www.tdus.de[/URL])

*/

/* #include "prjh0040.h" */
/* #include "bitops.h" */
#include <propeller.h>

struct pin_view {
    unsigned int P00 : 1;
    unsigned int P01 : 1;
    unsigned int P02 : 1;
    unsigned int P03 : 1;
    unsigned int P04 : 1;
    unsigned int P05 : 1;
    unsigned int P06 : 1;
    unsigned int P07 : 1;
    unsigned int P08 : 1;
    unsigned int P09 : 1;
    unsigned int P10 : 1;
    unsigned int P11 : 1;
    unsigned int P12 : 1;
    unsigned int P13 : 1;
    unsigned int P14 : 1;
    unsigned int P15 : 1;

    unsigned int P16 : 1;
    unsigned int P17 : 1;
    unsigned int P18 : 1;
    unsigned int P19 : 1;
    unsigned int P20 : 1;
    unsigned int P21 : 1;
    unsigned int P22 : 1;
    unsigned int P23 : 1;
    unsigned int P24 : 1;
    unsigned int P25 : 1;
    unsigned int P26 : 1;
    unsigned int P27 : 1;
    unsigned int P28 : 1;
    unsigned int P29 : 1;
    unsigned int P30 : 1;
    unsigned int P31 : 1;
};

struct func_view {
    unsigned int P00 : 1;
    unsigned int P01 : 1;
    unsigned int P02 : 1;
    unsigned int P03 : 1;
    unsigned int P04 : 1;
    unsigned int P05 : 1;
    unsigned int P06 : 1;
    unsigned int P07 : 1;
    unsigned int P08 : 1;
    unsigned int P09 : 1;
    unsigned int P10 : 1;
    unsigned int P11 : 1;
    unsigned int P12 : 1;
    unsigned int P13 : 1;
    unsigned int S01 : 1;
    unsigned int S02 : 1;

    unsigned int LED01 : 1;
    unsigned int LED02 : 1;
    unsigned int LED03 : 1;
    unsigned int LED04 : 1;
    unsigned int LED05 : 1;
    unsigned int LED06 : 1;
    unsigned int LED07 : 1;
    unsigned int LED08 : 1;
    unsigned int P24 : 1;
    unsigned int P25 : 1;
    unsigned int P26 : 1;
    unsigned int P27 : 1;
    unsigned int P28 : 1;
    unsigned int P29 : 1;
    unsigned int P30 : 1;
    unsigned int P31 : 1;
};

int main(int argc, char* argv[])
{
    unsigned int out_pins = 0;
    unsigned int in_pins = 0;

    /* out_pins = bitset(0, 16,17,18,19,20,21,22,23, -1); /\* select pins to */
    /*                                                                                                             toggle *\/ */

    struct pin_view *pin = (struct pin_view *)&out_pins;
    struct func_view *func = (struct func_view *)&out_pins;
    pin->P16 = 1;
    pin->P17 = 1;
    pin->P18 = 1;
    func->LED08 = 1;

    struct func_view *ina = (struct func_view *)&INA;

    int duration = 1;                            /* toggle duration in seconds*/
    DIRA = out_pins;                                    /* 1: out, 0: in */
    for(;;)
        {

            OUTA ^= out_pins;                                /* toggling (XOR with 1 inverts
                                                                                 selected bits) */
            waitcnt( (CLKFREQ * duration) / 2 + CNT);
        }
}

Comments

  • Heater.Heater. Posts: 21,230
    edited 2012-01-07 02:41
    Problem is that INA like all the other COG registers are not in the memory address space. All COG locations are registers. So it is not possible get their address into a pointer.
    It's rather like on processors that access IO registers with special instructions that operate in IO space rather than memory space.
  • ReinhardReinhard Posts: 489
    edited 2012-01-07 07:13
    hi,

    nice word game: proplem
    I like that.

    Reinhard
  • jazzedjazzed Posts: 11,803
    edited 2012-01-07 11:03
    Reinhard wrote: »
    nice word game: proplem


    Yes, it's propably one of my favorites so far.
  • proplemproplem Posts: 233
    edited 2012-01-07 11:56
    maybe a minute has gone looking at the post ...
    propably
    ... i can't stop laughing.

    This is recursion. Have fun!
  • ersmithersmith Posts: 6,099
    edited 2012-01-08 18:08
    proplem wrote: »
    is there a way to use INA, OUTA etc. as variables pointable via bit-fields?

    You can't do it with pointers, but you can do it with unions and the __asm__() declaration to give the assembly language name of a variable. Use __asm__("INA") to get the INA register, __asm__("OUTA") for OUTA, and so on (see for example the cog.h header file). In the union make sure the first member is an "unsigned int" and that no members are more than 32 bits. Here's an example:
    typedef union {
        unsigned int longval;
        struct {
        unsigned int P00 : 1;
        unsigned int P01 : 1;
        unsigned int P02 : 1;
        unsigned int P03 : 1;
        unsigned int P04 : 1;
        unsigned int P05 : 1;
        unsigned int P06 : 1;
        unsigned int P07 : 1;
        unsigned int P08 : 1;
        unsigned int P09 : 1;
        unsigned int P10 : 1;
        unsigned int P11 : 1;
        unsigned int P12 : 1;
        unsigned int P13 : 1;
        unsigned int P14 : 1;
        unsigned int P15 : 1;
    
        unsigned int P16 : 1;
        unsigned int P17 : 1;
        unsigned int P18 : 1;
        unsigned int P19 : 1;
        unsigned int P20 : 1;
        unsigned int P21 : 1;
        unsigned int P22 : 1;
        unsigned int P23 : 1;
        unsigned int P24 : 1;
        unsigned int P25 : 1;
        unsigned int P26 : 1;
        unsigned int P27 : 1;
        unsigned int P28 : 1;
        unsigned int P29 : 1;
        unsigned int P30 : 1;
        unsigned int P31 : 1;
        } pins;
    } MultiView;
    
    extern _COGMEM volatile MultiView ina __asm__("_INA");
    
    int
    getp0(void)
    {
        return ina.pins.P00;
    }
    
  • KyeKye Posts: 2,200
    edited 2012-01-08 19:00
    My teachers never taught me about this... I learned something useful today.

    This would have been useful for my x86 virtual memory implementation for my Operating Systems class.
Sign In or Register to comment.