I've been trying to decide the best forum to ask this question.  Given the last 
few subjects on both STM32s and CANopen I'll start here and if not perhaps 
someone can point me to a better place or we can take it off line.
I'm trying to learn a bit more about the Hardware Abstraction Layer (HAL) used 
in LinuxCNC (and on the STMBL servo drive I have).  Even WIN-2K way back when 
used a HAL but they all seem to be a tiny bit different.
So first some background.  A microprocessor has output and input ports.  Often 
a single pin can be programmed to be either in/out along with having all sorts 
of other functionality.  For my stuff, because my client keeps moving wires 
around I do something similar but not nearly as complex.
For example say I want to print out whether an external PC is running or not.  
The PC supplies a 12V out signal if it's powered and running.  The signal is 
brought into a controller running CANopen.  To see if it's on it's a simple 
test and a diagnostic message in this example.
    if (CPU2_ON) (void)printf(" CPU2_ON"); else (void)printf(" CPU2_OFF");
We don't really know what hardware pin is attached to "CPU2_ON" but this line 
in one of the include files gives a hint.
#define CPU2_ON                  ((ObjectDictionary[RIM2_INPUTS_NDX] & 
First I have this definition which states that it's a device called a RIM pin 
R11.  This is a remote module on CAN bus connected by CANopen protocol
#define fCPU2_OFF_SENSE         RIM_INPUT6         // RIM Pin R11
The RIM devices gather the various input pins and consolidate them into a 
single byte (8 inputs) that is then periodically transmitted on the CANopen bus 
as a PDO (Process Data Object) message.
The RIM_INPUT6 is bit 5 (0..7) and the masks are declared as follows.
// Bit map of RIM Inputs as set in the Object Dictionary image
#define RIM_INPUT6  0x20
Since the system can have a number of RIM modules, the information from the 
modules is stored into what is called an Object Dictionary, in this case 
indexed by RIM2_INPUTS_NDX.  So the CPU2 status is connected to RIM #2 Pin R11. 
 We don't really care which processor pin that is inside a RIM (in this case 
it's a SPI based shift register and not even on the processor)
Again at the application level this is all hidden or abstracted.
   if (CPU2_ON) (void)printf(" CPU2_ON"); else (void)printf(" CPU2_OFF");
If the CPU pin was changed to a different pin or even device all that has to 
change is the definition from
#define CPU2_ON                  ((ObjectDictionary[RIM2_INPUTS_NDX] & 
To something like
#define fCPU2_OFF_SENSE         PCM_INPUT1      // Power Control Module Input 
And then CPU2_ON becomes 
#define CPU2_ON                  ((ObjectDictionary[PCM_INPUTS_NDX] & 
Although this may all appear really convoluted when the compiler is all done, 
this is the code it generates for the test. One single machine code instruction 
taken directly from the .lst file.
275:      if (CPU2_ON) (void)printf(" CPU2_ON"); else (void)printf(" CPU2_OFF");
  0020 1f00002005   [5]     BRCLR ObjectDictionary:9,#32,*+10 ;abs = 002a
If I had a similar test on a LinuxCNC system and wanted to examine a switch 
input what would the code look like and how would it break down into the final 
machine instructions?
The problem I'm running into is that there is too much information out there, 
some of it outdated but I don't know what is current.  Much of it already 
assumes prior knowledge so once again I don't know what I don't know so it's 
hard to figure out.
Can someone knowledgeable with HAL and programming for LinuxCNC perhaps post a 
similar example.  Say you have an I/O board plugged into the PC bus that gives 
you 8 inputs and 8 outputs.  How does a simple test like:
if (COOLANT_SWITCH == 1) PUMP1 = 1; else PUMP1 = 0;
become abstracted through the HAL into the actual low level? How is it even 
described. If one were to write a simple HAL (is this even possible?) what 
would it look like?
I've tried to look at the STMBL code which also uses a HAL approach.  There a 
Python program reads files and generates C code and include files.  But there's 
so much I'm really becoming quite confused on what may well be really easy.  I 
lose track because the STMBL does so much,  So maybe a "dummy's guide to HAL on 
John Dammeyer

Emc-users mailing list

Reply via email to