How do I make better use of ETPU_function parameters from inside a subordinate C function?


ETPU_function parameters don't just pertain to the code in the ETPU_function body; they are relevant to everything that happens in the thread. It would be undesirable to pass these parameters around, and very difficult to get a globally-valid reference.

This item describes a way to use a matched set of ETPU_function parameters and a special global pointer to get access to the parameters in a C function.

Procedure: Accessing C pointers

  1. Declare a global structure of the eTPU function parameters, and declare the chan_base register as a pointer to the structure. The parameters of the current can then be referenced by name by the called function. This does not incur code or execution time overhead.

    struct _xyz_arg {
      int chan_arg1,
    } register_chan_base *pxyz_arg;

    register_chan_base is the processor-specific type for the chan_base register; any declared variable of this type refers to chan_base.

  2. Define an ETPU_function with struct _xyz_arg as the parameters. This is a slight inconvenience, but has the same effect as if the structure members were individual parameters.

    int i;
    #pragma ETPU_function xyz @ 3;
    void xyz (struct _xyz_arg args)
      if (hsr == 3)
          i = args.chan_arg2;

    Within the ETPU_function, code accesses the parameters indirectly:

     i = args.chan_arg2;
  3. C functions access the ETPU_function prameters through the global chan_base pointer; it always points to the current ETPU_function parameter list.

    void func ( void )
      int x;
      x = pxyz_arg -> chan_arg3;

    Dereferencing this pointer by the "->" operator is the same as direct access to the eTPU function parameters, and doesn't require the address calculation at run-time in software. There is no code penalty to access parameters this way. The generated access code is identical to the parameter access in the eTPU function.