Recent

Author Topic: AVRPascal – free code editor for FPC for AVR  (Read 43114 times)

ackarwow

  • Full Member
  • ***
  • Posts: 174
    • Andrzej Karwowski's Homepage
Re: AVRPascal – free code editor for FPC for AVR
« Reply #135 on: March 06, 2025, 07:13:32 pm »
Some new features that will be introduced in new AVRPascal 3.2 (work in progress):
- Changes in toolbar buttons: changed "New file" icon and added "Upload to device" button
- Added "New file" simple templates for program and unit
- And more... will be soon. :)

ackarwow

  • Full Member
  • ***
  • Posts: 174
    • Andrzej Karwowski's Homepage
Re: AVRPascal – free code editor for FPC for AVR
« Reply #136 on: March 19, 2025, 05:22:53 pm »
Another feature in the upcoming release - Serial Port Monitor inspired by RS232 Terminal program written by @Dzandaa and Arduino Serial Port Monitor.

ackarwow

  • Full Member
  • ***
  • Posts: 174
    • Andrzej Karwowski's Homepage
Re: AVRPascal – free code editor for FPC for AVR
« Reply #137 on: March 23, 2025, 07:55:51 am »
New AVRPascal version (3.2) is available at http://akarwowski.pl/index.php?page=electronics&lang=en

Improved FPC (fix by @ccrause in aasmcpu.pas)
New features:
- changed "New" icon, added "Upload" button on the toolbar (thanks to @Dzandaa for suggestions)
- added "New file" dialog window with the option to select a new document template
- added Serial Port Monitor inspired by "RS232 Terminal" by @Dzandaa and Arduino IDE serial port monitor; available in the View->Serial Port Monitor menu, configuration parameters in the Options window, Other tab
Bug fixes:
- MacOS: improved mechanisms using threads
- minor fixes in the About window
- removed minor memory leaks

Edit: AVRPascal manual is updated.
« Last Edit: April 04, 2025, 09:22:00 pm by ackarwow »

ccrause

  • Hero Member
  • *****
  • Posts: 1094
Re: AVRPascal – free code editor for FPC for AVR
« Reply #138 on: August 02, 2025, 08:44:08 am »
Great news, thank you!  I am interested in the Leonardo USB code (for later, I have other things occupying my time at the moment), so it would be good if that sits in its own repository.

I finally got round to testing the usb CDC serial code on a Leonardo. Receiving data works, but transmitting data doesn't work, I don't see any usb activity. Did anyone manage to send data using the CDC serial object in this repository?

Dzandaa

  • Hero Member
  • *****
  • Posts: 522
  • From C# to Lazarus
Re: AVRPascal – free code editor for FPC for AVR
« Reply #139 on: August 02, 2025, 01:02:59 pm »
Hi,

@ccrause:

Here are the programs I used to test communication between OS and an Arduino using lazarus and AVRPascal.

I don't know if that can be useful for you with Leonardo.

B->
Regards,
Dzandaa

ackarwow

  • Full Member
  • ***
  • Posts: 174
    • Andrzej Karwowski's Homepage
Re: AVRPascal – free code editor for FPC for AVR
« Reply #140 on: August 02, 2025, 04:27:46 pm »
Great news, thank you!  I am interested in the Leonardo USB code (for later, I have other things occupying my time at the moment), so it would be good if that sits in its own repository.

I finally got round to testing the usb CDC serial code on a Leonardo. Receiving data works, but transmitting data doesn't work, I don't see any usb activity. Did anyone manage to send data using the CDC serial object in this repository?

@ccrause,
Are you using the code for Arduino Leonardo from the UnoLib/extras folder? If I remember correctly, this code is used to "bring to life" the Arduino Leonardo after uploading some code otherwise, it would "hang." The point is that the Leonardo uses a microcontroller for USB communication, not a dedicated chip like the Uno. Unfortunatelly I don't remember all the details of my tests...

ackarwow

  • Full Member
  • ***
  • Posts: 174
    • Andrzej Karwowski's Homepage
Re: AVRPascal – free code editor for FPC for AVR
« Reply #141 on: August 02, 2025, 04:49:08 pm »
Hi,

@ccrause:

Here are the programs I used to test communication between OS and an Arduino using lazarus and AVRPascal.

I don't know if that can be useful for you with Leonardo.

B->

@Dzandaa,

Hi! I'm afraid your programs won't help. They're great for the Arduino Uno, but the Leonardo works differently. If I remember correctly, you have to program the USB communication yourself on Leonardo; the Uno uses a special chip (also an ATmega) to handle USB.

ackarwow

  • Full Member
  • ***
  • Posts: 174
    • Andrzej Karwowski's Homepage
Re: AVRPascal – free code editor for FPC for AVR
« Reply #142 on: August 02, 2025, 05:44:09 pm »
@ccrause, @Dzandaa,

I made a simple test on Windows (Virtual Machine) using ccrause_blink_leonardo.pas from UnoLib/extras. It seems to work, AVRDude can write and read from Arduino Leonardo flash:

Code: Text  [Select][+][-]
  1. Forcing reset using 1200bps open/close on port com5...
  2. Waiting for port...
  3. Found port COM4.
  4. "C:\Programs\avr\avrpascal\bin\x86_64-win64\avrdude.exe" "-CC:\Programs\avr\avrpascal\bin\x86_64-win64\avrdude.conf" -pm32u4 -PCOM4 -cavr109 -b57600 "-Uflash:w:C:\Programs\avr\avrpascal\examples\extras\ccrause_blink_leonardo.hex:i"
  5. Reading 3076 bytes for flash from input file ccrause_blink_leonardo.hex
  6. Writing 3076 bytes to flash
  7. Writing | ################################################## | 100% 0.41s
  8. Reading | ################################################## | 100% 0.12s
  9. 3076 bytes of flash verified
  10.  
  11. Avrdude done.  Thank you.
  12. Waiting for port...
  13. No port found.
  14.  

PS: Lines 1-3 and 12-13 (Forcing reset.../Waiting for port../No port found) are AVRPascal additions to reset and search Leonardo and on the finish I didn't turn on Leonardo USB device on my Windows Virtual Machine.

Dzandaa

  • Hero Member
  • *****
  • Posts: 522
  • From C# to Lazarus
Re: AVRPascal – free code editor for FPC for AVR
« Reply #143 on: August 02, 2025, 06:32:08 pm »
Hi,

@ackarwow:

You're right, the Leonardo use one ATMEGA32-U4 and the micro USB is connected to pin 3 (D-) , 4 (D+), and 5 (USB GND) of the AtMEGA32-U4.
But I've never had one in my hand.

B->
Regards,
Dzandaa

ackarwow

  • Full Member
  • ***
  • Posts: 174
    • Andrzej Karwowski's Homepage
Re: AVRPascal – free code editor for FPC for AVR
« Reply #144 on: August 02, 2025, 07:03:04 pm »
Hi,

@ackarwow:

You're right, the Leonardo use one ATMEGA32-U4 and the micro USB is connected to pin 3 (D-) , 4 (D+), and 5 (USB GND) of the AtMEGA32-U4.
But I've never had one in my hand.

B->

@Dzandaa,
Thanks for the info about Leonardo and USB :) That's more or less what I suspected. @ccrause's question is about virtual serial port emulation, which is supported by a converter based on the ATmega16U2 (or similar) on the Arduino Uno, but on the Leonardo, the emulation needs to be included in the program code. This is what I concluded from my "investigation" of the Arduino code, although many things are still unclear for me (for example - how Leonardo's bootloader works).

PS. I prepared the code for the Leonardo just to see if AVRPascal would work with it. I don't plan on writing programs for the Leonardo... This board is a bit strange  %)
« Last Edit: August 02, 2025, 07:06:45 pm by ackarwow »

ccrause

  • Hero Member
  • *****
  • Posts: 1094
Re: AVRPascal – free code editor for FPC for AVR
« Reply #145 on: August 02, 2025, 07:15:35 pm »
@ccrause, @Dzandaa,

I made a simple test on Windows (Virtual Machine) using ccrause_blink_leonardo.pas from UnoLib/extras. It seems to work, AVRDude can write and read from Arduino Leonardo flash:

Code: Text  [Select][+][-]
  1. Forcing reset using 1200bps open/close on port com5...
  2. Waiting for port...
  3. Found port COM4.
  4. "C:\Programs\avr\avrpascal\bin\x86_64-win64\avrdude.exe" "-CC:\Programs\avr\avrpascal\bin\x86_64-win64\avrdude.conf" -pm32u4 -PCOM4 -cavr109 -b57600 "-Uflash:w:C:\Programs\avr\avrpascal\examples\extras\ccrause_blink_leonardo.hex:i"
  5. Reading 3076 bytes for flash from input file ccrause_blink_leonardo.hex
  6. Writing 3076 bytes to flash
  7. Writing | ################################################## | 100% 0.41s
  8. Reading | ################################################## | 100% 0.12s
  9. 3076 bytes of flash verified
  10.  
  11. Avrdude done.  Thank you.
  12. Waiting for port...
  13. No port found.
  14.  

PS: Lines 1-3 and 12-13 (Forcing reset.../Waiting for port../No port found) are AVRPascal additions to reset and search Leonardo and on the finish I didn't turn on Leonardo USB device on my Windows Virtual Machine.

Yes it works well enough to trigger the bootloader when required. It also works for receiving data, but fails to transmit data.  Below a simple test program (with the uart unit from fpc-avr as backup) which toggles an LED when data is received.  The data is then echoed back.:
Code: Pascal  [Select][+][-]
  1. program usbserial;
  2.  
  3. // Simple uart/serial echo example
  4.  
  5. uses
  6.   cdc, usb, intrinsics, delay,
  7.   uart;
  8.  
  9. const
  10.   LEDpinMask = 1 shl 4;
  11.  
  12. var
  13.   serial: TCDCSerial;
  14.   c: byte;
  15.   LEDport: byte absolute PORTb;
  16.   LEDddr: byte absolute DDRB;
  17.  
  18. begin
  19.   LEDddr := LEDddr or LEDpinMask;
  20.   LEDport := LEDport or LEDpinMask;
  21.  
  22.   USBDevice.attach;
  23.   avr_sei;
  24.   serial.Start(9600);
  25.  
  26.   uart_init1(9600, true);
  27.  
  28.   repeat
  29.     uart_peek(c);
  30.     if c > 0 then
  31.     begin
  32.       LEDport := LEDport xor LEDpinMask;
  33.       uart_transmit(c);
  34.     end;
  35.  
  36.     if serial.Available > 0 then
  37.     begin
  38.       LEDport := LEDport xor LEDpinMask;
  39.       c := serial.Read;
  40.       serial.Write(c);
  41.     end;
  42.   until false;
  43. end.

Testing this requires an extra USB-serial connection to the hardware serial pins (if you want to test that interface). When writing a character to the USB-CDC port (/dev/ttyACM0) th e LED is togglrd, but no echo is received.

ackarwow

  • Full Member
  • ***
  • Posts: 174
    • Andrzej Karwowski's Homepage
Re: AVRPascal – free code editor for FPC for AVR
« Reply #146 on: August 02, 2025, 09:03:28 pm »
[...]Below a simple test program (with the uart unit from fpc-avr as backup) which toggles an LED when data is received.  The data is then echoed back.:
Code: Pascal  [Select][+][-]
  1. program usbserial;
  2.  
  3. // Simple uart/serial echo example
  4.  
  5. uses
  6.   cdc, usb, intrinsics, delay,
  7.   uart;
  8.  
  9. const
  10.   LEDpinMask = 1 shl 4;
  11.  
  12. var
  13.   serial: TCDCSerial;
  14.   c: byte;
  15.   LEDport: byte absolute PORTb;
  16.   LEDddr: byte absolute DDRB;
  17.  
  18. begin
  19.   LEDddr := LEDddr or LEDpinMask;
  20.   LEDport := LEDport or LEDpinMask;
  21.  
  22.   USBDevice.attach;
  23.   avr_sei;
  24.   serial.Start(9600);
  25.  
  26.   uart_init1(9600, true);
  27.  
  28.   repeat
  29.     uart_peek(c);
  30.     if c > 0 then
  31.     begin
  32.       LEDport := LEDport xor LEDpinMask;
  33.       uart_transmit(c);
  34.     end;
  35.  
  36.     if serial.Available > 0 then
  37.     begin
  38.       LEDport := LEDport xor LEDpinMask;
  39.       c := serial.Read;
  40.       serial.Write(c);
  41.     end;
  42.   until false;
  43. end.

Testing this requires an extra USB-serial connection to the hardware serial pins (if you want to test that interface). When writing a character to the USB-CDC port (/dev/ttyACM0) th e LED is togglrd, but no echo is received.

Another "investigation" of the Arduino code. The "Serial_::begin" method from CDC.cpp does nothing, at least not setting the BaudRate. (This is the source of TDCDSerial.Start, named that way to avoid conflict with the Pascal reserved word "begin"):

Code: C  [Select][+][-]
  1. void Serial_::begin(unsigned long /* baud_count */)
  2. {
  3.         peek_buffer = -1;
  4. }
  5.  
  6. void Serial_::begin(unsigned long /* baud_count */, byte /* config */)
  7. {
  8.         peek_buffer = -1;
  9. }
  10.  

I'm still looking for the cause. Maybe Arduino doesn't fully support CDC? Maybe TCDCSerial.AvailableForWrite should be used first... (?)

ackarwow

  • Full Member
  • ***
  • Posts: 174
    • Andrzej Karwowski's Homepage
Re: AVRPascal – free code editor for FPC for AVR
« Reply #147 on: August 02, 2025, 09:34:00 pm »
@ccrause
Next finding: TCDCSerial.Baud is a function, so can be only taken from _usbLineInfo variable:

Code: Pascal  [Select][+][-]
  1.   _usbLineInfo: TLineInfo=(dwDTERate: 57600; bCharFormat: $00; bParityType: $00; bDataBits: $00; lineState:  $00);
  2.  

Probably CDC_Setup uses these default values.Try to change your baud rate to 57600...

Edit: or change your code in this way:

Code: Pascal  [Select][+][-]
  1.     program usbserial;
  2.      
  3.     // Simple uart/serial echo example
  4.      
  5.     uses
  6.       cdc, usb, intrinsics, delay,
  7.       uart;
  8.      
  9.     const
  10.       LEDpinMask = 1 shl 4;
  11.      
  12.     var
  13.       serial: TCDCSerial;
  14.       c: byte;
  15.       LEDport: byte absolute PORTb;
  16.       LEDddr: byte absolute DDRB;
  17.      
  18.     begin
  19.       LEDddr := LEDddr or LEDpinMask;
  20.       LEDport := LEDport or LEDpinMask;
  21.      
  22.       USBDevice.attach;
  23.       avr_sei;
  24.       serial.Start(9600); //does nothing
  25.      
  26.       uart_init1({9600}serial.Baud, true);
  27.      
  28.       repeat
  29.         uart_peek(c);
  30.         if c > 0 then
  31.         begin
  32.           LEDport := LEDport xor LEDpinMask;
  33.           uart_transmit(c);
  34.         end;
  35.      
  36.         if serial.Available > 0 then
  37.         begin
  38.           LEDport := LEDport xor LEDpinMask;
  39.           c := serial.Read;
  40.           serial.Write(c);
  41.         end;
  42.       until false;
  43.     end.
  44.  
« Last Edit: August 02, 2025, 10:12:22 pm by ackarwow »

ccrause

  • Hero Member
  • *****
  • Posts: 1094
Re: AVRPascal – free code editor for FPC for AVR
« Reply #148 on: August 02, 2025, 11:11:18 pm »
@ccrause
Next finding: TCDCSerial.Baud is a function, so can be only taken from _usbLineInfo variable:

Code: Pascal  [Select][+][-]
  1.   _usbLineInfo: TLineInfo=(dwDTERate: 57600; bCharFormat: $00; bParityType: $00; bDataBits: $00; lineState:  $00);
  2.  

Probably CDC_Setup uses these default values.Try to change your baud rate to 57600...

The baud rate is not used for the USB layer data transfer, so I suspect that is why the Arduino code ignores the baud rate specified with Serial.begin.

Quote
Edit: or change your code in this way:

Code: Pascal  [Select][+][-]
  1.     program usbserial;
  2.      
  3.     // Simple uart/serial echo example
  4.      
  5.     uses
  6.       cdc, usb, intrinsics, delay,
  7.       uart;
  8.      
  9.     const
  10.       LEDpinMask = 1 shl 4;
  11.      
  12.     var
  13.       serial: TCDCSerial;
  14.       c: byte;
  15.       LEDport: byte absolute PORTb;
  16.       LEDddr: byte absolute DDRB;
  17.      
  18.     begin
  19.       LEDddr := LEDddr or LEDpinMask;
  20.       LEDport := LEDport or LEDpinMask;
  21.      
  22.       USBDevice.attach;
  23.       avr_sei;
  24.       serial.Start(9600); //does nothing
  25.      
  26.       uart_init1({9600}serial.Baud, true);
  27.      
  28.       repeat
  29.         uart_peek(c);
  30.         if c > 0 then
  31.         begin
  32.           LEDport := LEDport xor LEDpinMask;
  33.           uart_transmit(c);
  34.         end;
  35.      
  36.         if serial.Available > 0 then
  37.         begin
  38.           LEDport := LEDport xor LEDpinMask;
  39.           c := serial.Read;
  40.           serial.Write(c);
  41.         end;
  42.       until false;
  43.     end.
  44.  

Setting the baud rate for the hardware USART to be the same as for the USB-CDC interface does not affect the USB-CDC interface function.  In the code above the hardware serial and USB-CDC serial functions completely independent of each other so the change will not make a difference.

I suspect that there is a simple bug somewhere in the data send code path that is preventing the data from reaching the USB registers, but it is not that easy to identify. It should be simple bug since the Pascal code does follow the Arduino C++ code closely (at least as far as I understand C++).

ackarwow

  • Full Member
  • ***
  • Posts: 174
    • Andrzej Karwowski's Homepage
Re: AVRPascal – free code editor for FPC for AVR
« Reply #149 on: August 03, 2025, 10:11:14 am »
I suspect that there is a simple bug somewhere in the data send code path that is preventing the data from reaching the USB registers, but it is not that easy to identify. It should be simple bug since the Pascal code does follow the Arduino C++ code closely (at least as far as I understand C++).

@ccrause,
Yes, it is possible. I modified slightly usb.pas to be more compatible with Arduino source (attached) but I'm not sure if it will help. Have you tried writing a similar test program to the Arduino Sketch to make sure that Arduino code is working correctly? What are the connections in your test environment (I mean hardware and circuit)?

PS. I am using Arduino 1.8.19
« Last Edit: August 03, 2025, 10:12:57 am by ackarwow »

 

TinyPortal © 2005-2018