Author Topic: making processor independent mpasm delay lib  (Read 4631 times)

0 Members and 1 Guest are viewing this topic.

Offline luxfxTopic starter

  • Contributor
  • Posts: 36
  • Country: us
making processor independent mpasm delay lib
« on: August 01, 2016, 03:03:10 pm »
I'm new to assembly / PIC / MPLABx / MPASM the whole chain, and am trying to port over some of my software knowledge from other areas, like building reusable code libraries that I will reuse in multiple projects.

I've got a delay library working, but it's still dependent on the processor #include "p12F509.inc" line because I'm checking STATUS,Z for my main loop decrement check.

Is there a way to pull these values in from the code that's using the library?
Is it safe to just redefine STATUS and Z locally, is there a concern these will change location in other processors?
Should I just use a different way to check for zero?

thanks!


my main countdown call for the delay, in my lib this is called a varying number of times for 1us, 1ms, and 100ms delays. this is where I'm using STATUS and Z:
Code: [Select]
Cntdwn      DECF    CntDownCounter  ;decrement counter
    nop
    nop
    nop
            BTFSS   STATUS, Z       ;Zero flag set?
            GOTO    Cntdwn          ;No, keep looping
            retlw   0               ;Yes, timeout done
   
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12807
Re: making processor independent mpasm delay lib
« Reply #1 on: August 01, 2016, 03:18:51 pm »
Core registers and bit positions within them are fixed within any product family.  You certainly wont have problems with any other baseline part if you copy the EQUates for STATUS and its flag bits from p12F509.inc to your code.
 

Offline luxfxTopic starter

  • Contributor
  • Posts: 36
  • Country: us
Re: making processor independent mpasm delay lib
« Reply #2 on: August 01, 2016, 04:07:45 pm »
Core registers and bit positions within them are fixed within any product family.  You certainly wont have problems with any other baseline part if you copy the EQUates for STATUS and its flag bits from p12F509.inc to your code.

That's great, thanks! Is there a way to check for product family? Is there a compiler flag set? Something like this section from p12F509.inc but more generic for family:

Code: [Select]
        IFNDEF __12F509
           MESSG "Processor-header file mismatch.  Verify selected processor."
        ENDIF
 

Offline MosherIV

  • Super Contributor
  • ***
  • Posts: 1530
  • Country: gb
Re: making processor independent mpasm delay lib
« Reply #3 on: August 01, 2016, 04:33:35 pm »
Hi

Most Real Time Operating System provide timer facilities by using the Timer peripheral in the processor.

Typically, they will create some kind of linked list of timer events, each event contains either the start tick count (from the timer) and the number of ticks or the end tick count.
Then the ISR for the timer just goes to the top of the list (may need to go through list but some just check the top one) and checks for expired timer counts.

This make timer facilities independent of the processor, only the timer peripheral driver must be tailored for each processor.
 

Offline luxfxTopic starter

  • Contributor
  • Posts: 36
  • Country: us
Re: making processor independent mpasm delay lib
« Reply #4 on: August 01, 2016, 06:17:40 pm »
Most Real Time Operating System provide timer facilities by using the Timer peripheral in the processor.

Thanks for the info. I've yet to start working with timers in my self education. I see my question with the delay library as trying to learn a foundational concept -- a delay routine is just an example, and how I'm choosing to learn this specific topic at the moment.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9886
  • Country: us
Re: making processor independent mpasm delay lib
« Reply #5 on: August 01, 2016, 07:49:50 pm »
How chip independent is your code if you double the clock rate?
Embedded programming is highly hardware dependent and attempts to create 'universal' code are doomed to fail.  Step up a level for code that can be easily ported - the code that calls delay().  What you can do is organize the hardware dependent code into a single file or small set of files rather than burying it along with code that is not hardware dependent.

BTW, spin loops like:
Code: [Select]
while (cnt--)
  ;
will get optimized away unless cnt is declared as 'volatile'.  Any compiler will see that the loop performs nothing and simply toss it if the optimization level is set above zero.
 
The following users thanked this post: Kilrah

Online JPortici

  • Super Contributor
  • ***
  • Posts: 3452
  • Country: it
Re: making processor independent mpasm delay lib
« Reply #6 on: August 01, 2016, 08:54:18 pm »
if you use xc8/16/32 there is already one.
you first need to define the mcu frequency by issuing
#define _XTAL_FREQ xxxxxxxx
where you write the mcu clock frequency (not instruction frequency! not the crystal frequency if you use pll!)

in the compilers folders, the user guide you'll see the complete functions library
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12807
Re: making processor independent mpasm delay lib
« Reply #7 on: August 01, 2016, 10:39:53 pm »
That's great, thanks! Is there a way to check for product family? Is there a compiler flag set? Something like this section from p12F509.inc but more generic for family:
Code: [Select]
        IFNDEF __12F509
           MESSG "Processor-header file mismatch.  Verify selected processor."
        ENDIF
No.

There are four possible product families, each with a different instruction set:
* "Baseline" - 12 bit core, 1 FSR, usually 2 stack levels and no interrupts, but some enhanced baceline devices with 4 stack levels + interrupts exist
* Standard "Midrange" - 14 bit core, 1 FSR, 8 stack levels, interrupts
* "Enhanced Midrange" - Enhanced 14 bit core, 2 FSRs, 16 stack levels, interrupts
* "High Performance" (more usually known as PIC18) - 16 bit core, 3 FSRs, 16 stack levels, interrupts

The C compilers generally have system defines that identify the processor family but MPASM is *D*U*M*B* and does not. See http://www.microchip.com/forums/m617237.aspx for a 'rule of thumb' method of interpreting  8 bit PIC part numbers.  As such, you'll need separate libraries for each family, and it is *YOUR* responsibility as the programmer to use the correct one.

You may find this topic interesting: http://www.microchip.com/forums/m409798.aspx
 

Offline luxfxTopic starter

  • Contributor
  • Posts: 36
  • Country: us
Re: making processor independent mpasm delay lib
« Reply #8 on: August 02, 2016, 12:25:21 am »
How chip independent is your code if you double the clock rate?
Embedded programming is highly hardware dependent and attempts to create 'universal' code are doomed to fail.

I bought a couple each of 10f200, 12f505, 12f508, and 12f509 to follow a set of tutorials I found which graduates through that series. They all have 1MHz instruction cycle though, and I'm attempting various goals from the venerable blinking LED, to shift register and 16x2 LCD control with these chips. I realize this is a small subset of processors that would happen to use the same timing, but it would help out my short term experiments quite a lot.
 

Offline luxfxTopic starter

  • Contributor
  • Posts: 36
  • Country: us
Re: making processor independent mpasm delay lib
« Reply #9 on: August 02, 2016, 12:30:49 am »
if you use xc8/16/32 there is already one.
you first need to define the mcu frequency by issuing
#define _XTAL_FREQ xxxxxxxx

Thanks! I'm planning on switching to xc8 eventually but want to get a good baseline understanding of the assembly behind it.
 

Offline luxfxTopic starter

  • Contributor
  • Posts: 36
  • Country: us
Re: making processor independent mpasm delay lib
« Reply #10 on: August 02, 2016, 12:33:43 am »
See http://www.microchip.com/forums/m617237.aspx for a 'rule of thumb' method of interpreting  8 bit PIC part numbers.

THANK YOU I've been trying to find a general understanding of those part numbers for a while now. So far I've only been able to locate echos of the first few replies in that thread to RTFM. That doesn't provide a general understanding though, for people that are looking for breadth first instead of depth first knowledge.
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4196
  • Country: us
Re: making processor independent mpasm delay lib
« Reply #11 on: August 02, 2016, 02:22:29 am »
Normally, code that wants to be processor-independent and cpu-speed independent will need to be implement partially as macros, rather than as library functions.  That's because various symbols/values (like the clock rather, or the cpu type, or the exact address of the XXX register are frequently available either only at compile time, or are use in a way that is much more easily optimized if known at compile time.  Or perhaps the instruction length changes between different processors (as it does between "baseline" and "enhanced" PICs.)  (that said, I'm pretty sure that STATUS is 3 on all the PIC10/12/16s (but they're different on PIC18.)

(A bunch of "higher performance" CPUs (ARM and PIC32) will do things like derive the CPU clock frequency in code.  So that if you initialize a UART at 9600bps, the code will go and read a bunch of CPU registers to figure out how fast the clock is running, taking hundreds of instructions instead of using a compile-time constant that would have worked fine.   Because...  I don't know why.  (actually, Microchip has pointed out that this makes it possible to distribute the libraries as binaries rather than source code.  Which opens up a bunch of different licensing possibilities.  But they do it code code that is already distributed in source form as well, and I don't like it!))
 

Offline luxfxTopic starter

  • Contributor
  • Posts: 36
  • Country: us
Re: making processor independent mpasm delay lib
« Reply #12 on: August 02, 2016, 03:34:04 pm »
Normally, code that wants to be processor-independent and cpu-speed independent will need to be implement partially as macros, rather than as library functions.

Thanks - I was trying to leverage the library project structure that MPLABx provided. What is the IDE friendly method for using macros where they're not tied to specific projects? I had briefly entertained the concept of snippets but prefer discrete code files not tied to the IDE that I could check into a file repository.
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4196
  • Country: us
Re: making processor independent mpasm delay lib
« Reply #13 on: August 02, 2016, 11:27:30 pm »
You should be able to put macro definition files (".inc", usually, I think) in the same sorts of places you'd put the .h files for user-created C libraries, and have the IDE either reference them (more efficient) or copy them into your individual projects (better source code control wise, sort-of.)  I'm not familiar enough with MPIDE to know the "preferred" details...
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12807
Re: making processor independent mpasm delay lib
« Reply #14 on: August 02, 2016, 11:46:01 pm »
You should be able to put macro definition files (".inc", usually, I think) in the same sorts of places you'd put the .h files for user-created C libraries, and have the IDE either reference them (more efficient)
No, you cant as Microchip's MPASM/MPASMWIN/MPASMX is brain-dead and doesn't have any concept of an include path.  It searches the current directory and the MPASM program directory only.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf