AVR 32u4 programming

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

Post Reply
mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

AVR 32u4 programming

Post by mnf »

Attempting to test a Atmega 32u4 program (an Adafruit Feather with Lora)

How is the program uploaded to the board? - Under project options there is no option for setting the USB port.
Compiling 'to chip' gives a 'cannot find usbtiny' error - without pausing. (I do have a licence for the AVR processor pack)

I can upload code from the Arduino IDE. I do have usbtiny ISP programmer (or at least a cheapo clone) - though programming through USB would be easier especially as the Feather board doesn't have a header for connection.

On a related subject - how can the 32u4 be programmed (preferably easily:-) ) I have one board that breaks out the 32u4 pins - but I haven't been able to find any documentation on how to program it.

Martin

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: AVR 32u4 programming

Post by LeighM »

What target chip have you chosen for this project?
If the Adafruit Feather has a bootloader, then chosing "Arduino/Leonardo" might work via USB

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: AVR 32u4 programming

Post by mnf »

Thanks Leigh - I chose 32u4. I'll have a try using Leonardo.

- No joy using the Leonardo settings. Pressing 'reset' a couple of times cleared the dialog - but gave 'can't find com5' and uploading failed.

Later:

I did manage to get it to work using AVRDUDE and the following command:

Code: Select all

c:\Users\Martin\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino9/bin/avrdude -CC:\Users\Martin\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino9/etc/avrdude.conf -v -patmega32u4 -cavr109 -PCOM6 -b57600 -D -Uflash:w:./Flowcode1.hex:i
- pressing the reset just before executing the command. This is a little ungainly(!) - I copied the command across from the ArduinoIDE (and it could surely be tidied by moving the avrdude executable and conf file into the current directory or into a PATH directory at least)
Got a 'blink' working - take a deep breath and try the LORA radio module..

Then managed to get working using the 'Leonardo' option... Just needed to press 'reset' on the board during setting the project configuration - and choosing the correct com port (6 in my case) - before it is 'lost' (after the board goes out of the bootloader)


Martin

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: AVR 32u4 programming

Post by LeighM »

Hi Martin,
Thanks for the update.
Yes the Arduino boards with separate UART to USB device are easier to use, as the COM port is permanent and the connection automatically resets the board into bootloader mode. As usual, you get what you pay for :roll:

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: AVR 32u4 programming

Post by mnf »

Strange:

The Arduino IDE can find and program the board without a reset - it seems to 'hunt' through the serial ports (turn on the verbose output).

Flowcode seems problematic - sometimes it 'forgets' the Com port and defaults to com1 - otherwise it seems to take at least two presses of reset before it either works or gives an avrdude error. This may be connected to the following: (and also to using the Leonardo settings?)

Running a Flowcode sketch seems to 'kill' the connected com port (so com5 'disappears' from device manager) when running - and I need to use reset to get the Arduino IDE to reconnect. Using UART1 or an empty sketch seems to do this. (Serial.print in the Arduino code works aok)

I managed to get a simple program working(ish) with an i2c display - displayed 'Hello' briefly before the display cleared. Adding a 5s delay after the i2c display command didn't leave Hello on the display for any longer.

Uploading using AVRDUDE (as per previous message) gives the same problem

From within the Arduino IDE Avrdude reports the following:
Using Port : COM6
Using Programmer : avr109
Overriding Baud Rate : 57600
AVR Part : ATmega32U4
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PA0
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :

Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack
----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
eeprom 65 20 4 0 no 1024 4 0 9000 9000 0x00 0x00
flash 65 6 128 0 yes 32768 128 256 4500 4500 0x00 0x00
lfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
efuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
lock 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00

Programmer Type : butterfly
Description : Atmel AppNote AVR109 Boot Loader

Connecting to programmer: .
Found programmer: Id = "CATERIN"; type = S
Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.

Using AVRDude with a Flowcode hex file:
avrdude: Version 6.3, compiled on Jan 17 2017 at 12:00:53
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch

System wide configuration file is "C:\Users\Martin\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino9/etc/avrdude.conf"

Using Port : COM6
Using Programmer : avr109
Overriding Baud Rate : 57600
AVR Part : ATmega32U4
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PA0
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :

Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack
----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
eeprom 65 20 4 0 no 1024 4 0 9000 9000 0x00 0x00
flash 65 6 128 0 yes 32768 128 256 4500 4500 0x00 0x00
lfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
efuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
lock 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00

Programmer Type : butterfly
Description : Atmel AppNote AVR109 Boot Loader

Connecting to programmer: .
Found programmer: Id = "CATERIN"; type = S
Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.

Programmer supports the following devices:
Device code: 0x44

avrdude: devcode selected: 0x44
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9587 (probably m32u4)
avrdude: safemode: hfuse reads as D8
avrdude: safemode: efuse reads as CD
avrdude: reading input file "Flowcode2.hex"
avrdude: writing flash (464 bytes):

Writing | ################################################## | 100% 0.05s

avrdude: 464 bytes of flash written
avrdude: verifying flash memory against Flowcode2.hex:
avrdude: load data flash data from input file Flowcode2.hex:
avrdude: input file Flowcode2.hex contains 464 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.02s

avrdude: verifying ...
avrdude: 464 bytes of flash verified

avrdude: safemode: hfuse reads as D8
avrdude: safemode: efuse reads as CD
avrdude: safemode: Fuses OK (E:CD, H:D8, L:FF)

avrdude done. Thank you.
This using ATMega 32u4 as target (as an aside - a 'search' in the chip select dialog would be good - much easier than wading through dozens of similar chip names/descreiptions?)

Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: AVR 32u4 programming

Post by mnf »

Maybe I'm missing something but things don't seem to work so smoothly on the AVR 32u4.

UART component - doesn't seem to work: So something simple - output a string to the UART....
Micro UART.fcfx
(9.89 KiB) Downloaded 235 times
This using an Arduino micro.

Trying to use the USB HID (Initialise)
Target folder: C:\Users\Martin\DOWNLO~1
Source name: C:\Users\Martin\Downloads\Micro UART.fcfx
Title:
Description:
Device: AVR.ATMEGA.ATMEGA32U4
Generated by: Flowcode v7.3.0.6
Date: Tuesday, January 30, 2018 07:55:19
Users: 1
Registered to:
Licence key:
NOT FOR COMMERCIAL USE
http://www.matrixtsl.com
Launching the compiler...
C:\Program Files (x86)\Flowcode 7\compilers\avr\batchfiles\avra.bat atmega32u4 "C:\Users\Martin\DOWNLO~1\Micro UART.elf" "C:\Users\Martin\DOWNLO~1\Micro UART.c" "C:\Users\Martin\DOWNLO~1\Micro UART.lst"
Launching compiler...

C:\Users\Martin\DOWNLO~1>"C:\PROGRA~2\FLOWCO~1\compilers\avr\batchfiles\..\bin\avr-gcc.exe" -mmcu=atmega32u4 -Os -ffunction-sections -fdata-sections -funsigned-char -o "C:\Users\Martin\DOWNLO~1\Micro UART.elf" "C:\Users\Martin\DOWNLO~1\Micro UART.c" -lm -Wl,-gc-sections
C:\Users\Martin\DOWNLO~1\Micro UART.c:397:20: error: config.h: No such file or directory
C:\Users\Martin\DOWNLO~1\Micro UART.c:399:33: error: lib_mcu/usb/usb_drv.h: No such file or directory
C:\Users\Martin\DOWNLO~1\Micro UART.c:401:59: error: modules/usb/device_chap9/usb_standard_request.h: No such file or directory
C:\Users\Martin\DOWNLO~1\Micro UART.c:403:34: error: usb_specific_request.h: No such file or directory
C:\Users\Martin\DOWNLO~1\Micro UART.c:405:22: error: hid_task.h: No such file or directory
C:\Users\Martin\DOWNLO~1\Micro UART.c:409: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'wInterface'
C:\Users\Martin\DOWNLO~1\Micro UART.c: In function 'FCD_06fa1_USB_HID1__SendData':
C:\Users\Martin\DOWNLO~1\Micro UART.c:657: error: 'EP_HID_IN' undeclared (first use in this function)
C:\Users\Martin\DOWNLO~1\Micro UART.c:657: error: (Each undeclared identifier is reported only once
C:\Users\Martin\DOWNLO~1\Micro UART.c:657: error: for each function it appears in.)
C:\Users\Martin\DOWNLO~1\Micro UART.c: In function 'FCD_06fa1_USB_HID1__SendDataDirect':
C:\Users\Martin\DOWNLO~1\Micro UART.c:948: error: 'EP_HID_IN' undeclared (first use in this function)

Error returned from [avr-gcc.exe]
Any suggestions?


Martin

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: AVR 32u4 programming

Post by LeighM »

Hi Martin,
This using ATMega 32u4 as target (as an aside - a 'search' in the chip select dialog would be good - much easier than wading through dozens of similar chip names/descreiptions?)
Good idea, I'll pass it to the team.
UART component - doesn't seem to work: So something simple - output a string to the UART
Flowcode only supports the hardware UART on Port D pins 2 and 3.
Are you using those?
I've not got one of these boards, which looks very interesting!,
but as far a I can see, the Arduino IDE print port you refer to is a virtual port via the USB,
no doubt supported via an Arduino library.
Trying to use the USB HID (Initialise)
Yes, looks like there is something broken in AVR USB support, we'll investigate

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: AVR 32u4 programming

Post by LeighM »

Hi Martin,
Regarding the USB HID...
Go to Build->Compiler Options->Import and import the attached config file.
Click OK to exit and add a setting name e.g. AVR_USB_HID
Hopefully that should now build USB support.
Leigh
Attachments
AVR_USB_HID.FCS
(686 Bytes) Downloaded 213 times

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: AVR 32u4 programming

Post by mnf »

Thanks Leigh,

Got the HID working using the above with just a couple of queries/problems:

1) Flowcode now deletes 7 .o files and rebuilds all the USB support files on every compile - which isn't really a problem it just seems a little odd (I haven't noticed this behavior on other projects.

2) More importantly - the HID device doesn't seem to support ' ' (space) (or '!' (exclamation mark)) so from the USB_HID_Keyboard example the 'typed' string is:
HelloFromYourUSBKeyboardDevice12345


rather than
"Hello From Your USB Keyboard Device! 12345\n"
Edit - looking further I see the blame for this lies in the ASCIIToKeypress macro in the demo program.. Surely a case for a lookup table here?

The USB is connected to PD2/PD3 on the Arduino Micro board (and on the Feather see https://learn.adafruit.com/adafruit-fea ... le/pinouts) - so not sure why the UART component fails to work??

However - this does give me something to work with, so many thanks....

Martin

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: AVR 32u4 programming

Post by LeighM »

Hi Martin,
Thanks for the update.
Flowcode now deletes 7 .o files and rebuilds all the USB support files on every compile
This is done by the batch file avra_usb.bat, some "if exist" could be added :)
ASCIIToKeypress macro in the demo program
Yes, some demo programs tend to be thrown together as a get you started, if only we had the time to re-visit them all :wink:
The USB is connected to PD2/PD3 on the Arduino Micro board
Do you mean you have a UART to USB bridge virtual com port connected to PD2/PD3?

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: AVR 32u4 programming

Post by mnf »

Thanks Leigh,

I'll play a while with the AsciiToKeyPress - that is some mighty switch statement - I managed to save >300 bytes just by getting rid of the switch for 'a'..'z' (the scan codes are sequential so easily map to the calculation 0x4 + c - 'a').
Do you mean you have a UART to USB bridge virtual com port connected to PD2/PD3?
Yes - I think..... The USB port is connected to the TX/RX pins on the Avr32u4 (on the Arduino UNO it's acting as a dedicated USB host) but on these boards it's the only processor (as per Leonardo as well?) - at least that's what I've assumed is happening? So I should be able to connect to the relevant COM port using putty (or similar) and use UART for debug messages - if no, where does the UART component transmit/receive from??

Martin

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: AVR 32u4 programming

Post by LeighM »

Hi Martin,
The Flowcode component uses PD2/PD3, which are not connected anywhere on that board,
so you will need to hard wire them to say a UART to USB bridge
Leigh

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: AVR 32u4 programming

Post by mnf »

Had a play with the USB_HID_Keyboard demo (from the Wiki) and modified it slightly....

So now supports 'space' :) and also has a Ctrl function (demonstrated here with Ctrl-A, Ctrl-X and then Ctrl-V) which passes Ctrl+key.
USB_HID_Keyboard.fcfx
(23.4 KiB) Downloaded 204 times
I invented the Flowcode if .... else if ..... (he he)
It seems that is you open a code block in one C macro - you can close it in another! Excellent. I've left some of the shifted keys as a switch statement - and moved the rest of the keys to a C macro... Any ideas on a better way to encode the keys (the switch seemed rather clumsy in Flowcode - but else if statements get clumsy too, A lookup table would be another way - maybe a 'key' lookup that is searched for the ascii char and indexes into another array?? - or a 16 bit lookup table where top 8 bits are Ascii and bottom 8 the scan code - with bit 7 representing shift.

I think it would be useful to have some more 'special' keys. Backspace for example. How should they best be encoded? In one of the SendKeys programs in C(++) it would be {BS} in the sent string (similarly {ENTER} or even {ENTER 3} ) - however this seems excessive on a micro-controller.....
However having a separate macro Backspace (etc) doesn't feel quite right either.. Any ideas/suggestions?

Perhaps this could be 'baked' into the HID component - having it only work with scan-codes seems wrong too???

Note there is an excel file of scan codes that seems to be corrupted (at http://www.matrixtsl.com/wikiv7/index.p ... _Codes.xls) in the Wiki - could maybe get an update?

Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: AVR 32u4 programming

Post by mnf »

One further problem - I can now no longer compile anything without importing the USB_HID component and initializing it..
C:\Users\Martin\DOWNLO~1>"C:\PROGRA~2\FLOWCO~1\compilers\avr\batchfiles\..\bin\avr-gcc.exe" -mmcu=atmega32u4 "C:\Users\Martin\DOWNLO~1\bluetooth1.o" "C:\Users\Martin\DOWNLO~1\usb_specific_request.o" "C:\Users\Martin\DOWNLO~1\usb_descriptors.o" "C:\Users\Martin\DOWNLO~1\power_drv.o" "C:\Users\Martin\DOWNLO~1\usb_drv.o" "C:\Users\Martin\DOWNLO~1\usb_task.o" "C:\Users\Martin\DOWNLO~1\usb_standard_request.o" "C:\Users\Martin\DOWNLO~1\usb_device_task.o" "C:\Users\Martin\DOWNLO~1\hid_task.o" -o "C:\Users\Martin\DOWNLO~1\bluetooth1.elf"
C:\Users\Martin\DOWNLO~1\usb_specific_request.o: In function `hid_get_report':
usb_specific_request.c:(.text+0xa4): undefined reference to `wInterface'
usb_specific_request.c:(.text+0xac): undefined reference to `wInterface'
C:\Users\Martin\DOWNLO~1\usb_specific_request.o: In function `hid_set_report':
usb_specific_request.c:(.text+0x178): undefined reference to `HID_USB_Rx_String'
usb_specific_request.c:(.text+0x17e): undefined reference to `incoming'
C:\Users\Martin\DOWNLO~1\usb_specific_request.o: In function `usb_hid_set_idle':
usb_specific_request.c:(.text+0x1ae): undefined reference to `wInterface'
usb_specific_request.c:(.text+0x1b6): undefined reference to `wInterface'
C:\Users\Martin\DOWNLO~1\usb_specific_request.o: In function `usb_hid_get_interface':
usb_specific_request.c:(.text+0x1e4): undefined reference to `wInterface'
usb_specific_request.c:(.text+0x1ec): undefined reference to `wInterface'
C:\Users\Martin\DOWNLO~1\usb_specific_request.o: In function `hid_get_hid_descriptor':
usb_specific_request.c:(.text+0x21c): undefined reference to `wInterface'
C:\Users\Martin\DOWNLO~1\usb_specific_request.o:usb_specific_request.c:(.text+0x224): more undefined references to `wInterface' follow
Strangely the USB_HID::Initialise macro can be disabled and compilation still works...

- but only if target is Arduino micro. Nano / Uno etc fails....

Presumably I need to tweak the Compiler options back to a different configuration?? (& presumably I did something wrong when I imported the config file??)

Martin

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: AVR 32u4 programming

Post by LeighM »

Yes, if you are not using USB in a project you need to set the compiler option back to default Arduino build.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: AVR 32u4 programming

Post by mnf »

Hi Leigh,

Back working with this - and can't get it to work in v8..

If I change compiler options to use
$(appdir)compilers\avr\batchfiles\avra_usb.bat
Then I get a USB Target not recognized error.

A bit of fiddling with the above .fcs and I get
lots of erors along the lines of
avr-gcc.exe: E:\Flowcode\Common\Compilers\avr\batchfiles\..\..\..\CAL\AVR\USB\Series4\modules\usb\usb_task.c: No such file or directory
The search path seems to have tacked itself onto the location of the batchfiles - but I can't find the location to alter the directory path?

Strangely the program compiles without errors using the default toolchain parameters - doesn't work though?

Martin

Post Reply