Lazarus

Programming => Embedded => Operating Systems => Embedded - AVR => Topic started by: diego bertotti on January 16, 2019, 01:43:19 pm

Title: avr hardware pins acces from pascal[SOLVED]
Post by: diego bertotti on January 16, 2019, 01:43:19 pm
hi, im trying fpc/lazarus to avr/arduino and it works! thank you. thank you thank you very very much!!!

but, if we don`t forget the atmega328 is a microcontroller, maybe a bit oriented view will be mandatory

when you need test a pin status , we need to read a whole port, and them mask the wanted bit, and the assembler generated don`t seems efficient (i guess).

when you need to output a bit, the assembler generated is good, but you may type things like that:

Code: Pascal  [Select][+][-]
  1.  
  2. PortB:= PortB AND %11111011;
  3.  
  4.  

then assembler generated is: cbi 5,0--its ok! but you must type a whole line!! >:D

or another example

Code: Pascal  [Select][+][-]
  1.  
  2. PORTB :=PORTB and not ( 1 shl 5 ) ;  // LED pin5 off
  3.  
  4.  

with a native new class "bit", may will can access bits more directly like turbo51 does for 8051 microcontroller:

example:

Code: Pascal  [Select][+][-]
  1.  
  2. P2.5:= true; //set bit 5 of port 2
  3.  
  4. if p2.5 then p2.4:= false; //if pin 5 of port 2 is set, then clear pin 4 of port 2
  5.  
  6.  

i looking the assembler instructions set of avr it can be do it (i guess)

again thank you very very very much!
Title: Re: avr hardware pins acces from pascal
Post by: ttomas on January 16, 2019, 01:53:46 pm
You can use bit packed record TByteBits
http://wiki.freepascal.org/Bit_manipulation
Title: Re: avr hardware pins acces from pascal
Post by: avra on January 16, 2019, 04:08:28 pm
Follow links from this post:
https://forum.lazarus.freepascal.org/index.php/topic,43923.msg308285.html#msg308285
Title: Re: avr hardware pins acces from pascal
Post by: diego bertotti on January 16, 2019, 07:04:35 pm
Thanks for replys

Maybe i don't be clear enough. My apologies. Its about assembler code generated by compiler, then only apply to avr.

And also to shortened the typed string. Im lazy

Maybe bitpacked will work. Ill try

I dont found documentation specific of avr feeepascal
Thanks again
Title: Re: avr hardware pins acces from pascal
Post by: diego bertotti on January 16, 2019, 10:03:28 pm
i Found IT!!

Code: Pascal  [Select][+][-]
  1. Var
  2. PuertoD: bitpacked array [0..7] of boolean absolute PORTD;
  3.  
  4.  
  5. begin
  6.  PuertoD[5]:= true;
  7.  PuertoD[5]:= false;
  8. end;  
  9.  
  10.  

and the generated assembler code was:
Code: Pascal  [Select][+][-]
  1.         sbi     11,5
  2.         cbi     11,5
  3.  
For output. Great news.. it work very well!! :)

remain found the way for read input bits!!
Title: Re: avr hardware pins acces from pascal
Post by: diego bertotti on January 16, 2019, 11:02:57 pm
and another aproach!


Code: Pascal  [Select][+][-]
  1. var
  2. PuertoD: bitpacked array [0..7] of boolean absolute PORTD;
  3. PinesD: bitpacked array [0..7] of boolean absolute PinD;  
  4.  
  5. begin
  6.  PuertoD[5]:= true;
  7.  PuertoD[5]:= False;
  8.  If PinesD[1] then
  9.   begin
  10.   PuertoD[5]:= False;
  11.   end;
  12. end.
  13.  

and assembler was....
Code: Pascal  [Select][+][-]
  1.         sbi     11,5
  2.         cbi     11,5
  3.         in      r18,9
  4.         sbrs    r18,1
  5.         rjmp    .Lj95
  6. .Lj94:
  7.         cbi     11,5
  8. .Lj95:
  9.        
  10.  

excellent! but for brach/ jump (if statement)
Title: Re: avr hardware pins acces from pascal[SOLVED]
Post by: diego bertotti on April 30, 2019, 02:10:15 pm
hi

im back

in last example, assembler use IN instruction to move port data to a r18 register, and then test bit. Will be better use branch instruction reading real port pin.

¿anybody know who can change this in the avr compiler?

where can i make contributions?

thanks
Title: Re: avr hardware pins acces from pascal[SOLVED]
Post by: Laksen on April 30, 2019, 07:00:45 pm
I just added that to trunk.
Title: Re: avr hardware pins acces from pascal[SOLVED]
Post by: diego bertotti on May 02, 2019, 01:44:37 pm
I just added that to trunk.
hi
thanks laksen!
how i can know when update/fix is online?
again, thanks
Title: Re: avr hardware pins acces from pascal[SOLVED]
Post by: sstvmaster on May 02, 2019, 04:45:54 pm
Here you can see the changes to trunk and so on: https://svn.freepascal.org/cgi-bin/viewvc.cgi/

Here is laksen's change/patch: https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/compiler/avr/aoptcpu.pas?view=log
Title: Re: avr hardware pins acces from pascal[SOLVED]
Post by: diego bertotti on May 03, 2019, 01:28:48 pm
thanks sstvmaster
Title: Re: avr hardware pins acces from pascal[SOLVED]
Post by: diego bertotti on May 03, 2019, 10:39:59 pm
hi

congratulations!!!

for this pascal code

Code: Pascal  [Select][+][-]
  1.    If Not PDin[2] Then PBout[0]:= True else PBout[0]:= False;
  2.    If Not PDin[4] Then PDout[7]:= True else PDout[7]:= False;
  3.    If Not PBin[5] Then PDout[6]:= True else PDout[6]:= False;
  4.    If Not PBin[4] Then PDout[5]:= True else PDout[5]:= False;
  5.    If Not PBin[3] Then PCout[0]:= True else PCout[0]:= False;
  6.    If Not PBin[2] Then PDout[3]:= True else PDout[3]:= False;
  7.  

now the compiler generated assembler is:

Code: Pascal  [Select][+][-]
  1. .Lj80:
  2.         sbic    9,2
  3.         rjmp    .Lj57
  4. .Lj58:
  5.         sbi     5,0
  6.         rjmp    .Lj59
  7. .Lj57:
  8.         cbi     5,0
  9. .Lj59:
  10.         sbic    9,4
  11.         rjmp    .Lj61
  12. .Lj62:
  13.         sbi     11,7
  14.         rjmp    .Lj63
  15. .Lj61:
  16.         cbi     11,7
  17. .Lj63:
  18.         sbic    3,5
  19.         rjmp    .Lj65
  20. .Lj66:
  21.         sbi     11,6
  22.         rjmp    .Lj67
  23. .Lj65:
  24.         cbi     11,6
  25. .Lj67:
  26.         sbic    3,4
  27.         rjmp    .Lj69
  28. .Lj70:
  29.         sbi     11,5
  30.         rjmp    .Lj71
  31. .Lj69:
  32.         cbi     11,5
  33. .Lj71:
  34.         sbic    3,3
  35.         rjmp    .Lj73
  36. .Lj74:
  37.         sbi     8,0
  38.         rjmp    .Lj75
  39. .Lj73:
  40.         cbi     8,0
  41. .Lj75:
  42.         sbic    3,2
  43.         rjmp    .Lj77
  44. .Lj78:
  45.         sbi     11,3
  46.         rjmp    .Lj50
  47. .Lj77:
  48.         cbi     11,3
  49.         rjmp    .Lj50      
  50.  
:D :D :D
Title: Re: avr hardware pins acces from pascal[SOLVED]
Post by: dseligo on January 17, 2022, 04:19:46 am
I know it is old thread, but why with this code bellow I don't get same results:

Code: Pascal  [Select][+][-]
  1. Var
  2.   PDin: bitpacked array [0..7] of boolean absolute PIND;
  3.   PBout: bitpacked array [0..7] of boolean absolute PORTB;
  4.   PDout: bitpacked array [0..7] of boolean absolute PORTD;
  5. ...
  6.   If Not PDin[2] Then PBout[0]:= True else PBout[0]:= False;
  7.   If Not PDin[4] Then PDout[7]:= True else PDout[7]:= False;

I get:
Code: ASM  [Select][+][-]
  1. # [109] If Not PDin[2] Then PBout[0]:= True else PBout[0]:= False;
  2.         lds     r18,(41)
  3.         sbrc    r18,2
  4.         rjmp    .Lj17
  5. .Lj18:
  6.         rjmp    .Lj16
  7. .Lj16:
  8.         lds     r18,(37)
  9.         ori     r18,1
  10.         sts     (37),r18
  11.         rjmp    .Lj19
  12. .Lj17:
  13.         lds     r18,(37)
  14.         ldi     r26,-2
  15.         and     r18,r26
  16.         sts     (37),r18
  17. .Lj19:
  18. # [110] If Not PDin[4] Then PDout[7]:= True else PDout[7]:= False;
  19.         lds     r18,(41)
  20.         sbrc    r18,4
  21.         rjmp    .Lj21
  22. .Lj22:
  23.         rjmp    .Lj20
  24. .Lj20:
  25.         lds     r18,(43)
  26.         ori     r18,-128
  27.         sts     (43),r18
  28.         rjmp    .Lj23
  29. .Lj21:
  30.         lds     r18,(43)
  31.         ldi     r26,127
  32.         and     r18,r26
  33.         sts     (43),r18
  34. .Lj23:

Version: Lazarus 2.3.0 (rev main-2_3-1122-g0a0b688587) FPC 3.3.1 i386-win32-win32/win64
Title: Re: avr hardware pins acces from pascal[SOLVED]
Post by: ccrause on January 17, 2022, 08:06:31 am
@dseligo: Try specifying optimization, such as -O1.  The compiler by default generates conservative instructions, which gets optimized by the peephole optimizer.
Title: Re: avr hardware pins acces from pascal[SOLVED]
Post by: avra on January 17, 2022, 09:18:34 am
Btw, bithelpers have been added to trunk FPC (sysutils and syshelpers), so now you should also be able to do something like this (not tested on AVR):
Code: Pascal  [Select][+][-]
  1. if not PIND.Bits[2] then PORTB.Bits[0] := true else PORTB.Bits[0] := false;

or even better:
Code: Pascal  [Select][+][-]
  1. PORTB.Bits[0] := not PIND.Bits[2];
Title: Re: avr hardware pins acces from pascal[SOLVED]
Post by: PascalDragon on January 17, 2022, 01:36:08 pm
Btw, bithelpers have been added to trunk FPC (sysutils and syshelpers), so now you should also be able to do something like this (not tested on AVR):

Two points:
Title: Re: avr hardware pins acces from pascal[SOLVED]
Post by: avra on January 17, 2022, 03:01:46 pm
Two points
Both valid  :D
Title: Re: avr hardware pins acces from pascal[SOLVED]
Post by: dseligo on January 17, 2022, 06:23:26 pm
@dseligo: Try specifying optimization, such as -O1.  The compiler by default generates conservative instructions, which gets optimized by the peephole optimizer.

Thanks, that was the cause.
I struggled to find it for a while. I tried to set optimization level in Lazarus' Project Options without success,
This was inserted in the top of the source when I created it with '[Embedded GUI] AVR-Project (Arduino)' in Lazarus:
Code: Pascal  [Select][+][-]
  1. {$H-,J-,O-}

Once I removed O- everything worked. :)
TinyPortal © 2005-2018