Easiest Blinky Ever. Making a mega4809-NANO Blink using Codevision.

Go To Last Post
22 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

So I've just taken a delivery of a pile of ATmega4809-Nanos. I use CVAVR as my compiler of choice which already has the 4809 in its project wizard.

 

The nano has an LED on PF5. Run the project wizard and select PF5 as an output. Drop the following code at the top of the source code...

 

#include <mega4809.h>
#include <delay.h>
#include <iobits.h>

 

And the following at the bottom...

 

while (1) {
    delay_ms(250);
    TGLBIT(PORTF.OUT,5);
}

And compile it.

 

Now, how to program the 4809? The nano has an on-board debugger but while looking in the manual I spotted that it will also emulate a USB mass storage device so that you just drag-and-drop a .hex file onto it and it will program the 4809.

 

So plug in a USB lead, let it install drivers, and lo' it appears as a disk drive. Drop the main.hex onto it and I now have a blinking LED.

 

How easy was that?

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

Last Edited: Thu. Oct 18, 2018 - 07:38 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I first saw the concept of an "MSD bootloader" in Dean's LUFA - the idea of just being able to drag/drop code images onto a "drive" always seemed like a very neat idea. In Dean's case I think he needs BIN not HEX though as putting an iHex decoder into an AVR could be a bit "fraught".

 

PS Dean's "maddest" idea for a bootloader is one that implements USB "Printer" class - you "print" the new firmware to it! surprise

Last Edited: Mon. Oct 15, 2018 - 02:28 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Now with added printf();

 

The nano also has an on-board CDC COM port which appears on the PC.

 

Use the Code Wizard to initialise the USART and I can print strings over USB to the terminal built into CVAVR.

 

A slight gotcha...

 

even though I told the Wizard I wanted to run at 16MHz, the chip was actually running at 20MHz. I need to read up on clock selection of the in-built RC oscillator.

 

Aha, my bad. The actual clock frequency is set by a fuse and cannot be changed at run time. I was confused by the wording of the wizard.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

Last Edited: Mon. Oct 15, 2018 - 04:07 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
Aha, my bad. The actual clock frequency is set by a fuse and cannot be changed at run time. I was confused by the wording of the wizard.

 

Actually, a fuse selects between 16MHz and 20MHz base clock, true, but you can select a divisor by software.

 

edit: and yes, programming by drag and drop is interesting. I see from the schematic that the debug chip is a SAMD21, I thought Microchip might use a PIC as debug chip, like the other curiosity boards have, but they decided to keep things separated.

Last Edited: Mon. Oct 15, 2018 - 05:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Worth documenting that to get the CDC channel to work it needed a driver. I did this by installing MPLABX on this machine, which worked, but I also sent an email to microchip to see if there was any other way to get the driver. For reference, here is there reply...

 

Quote:

Our comm drivers are normally distributed with our IDE's  or our MDBCore layers so there are a few options.

 

- Download and Install MPLAB X, or

- Download the SDK from www.opensource4pic.org  (It contains MPLABComm which is the layer for all the drivers. or

- Download and install Atmel Studio, or

- Download http://studio.download.atmel.com/7.0.1931/driver-atmel-installer-x64-7.0.1931.msi , or

- Download http://studio.download.atmel.com/7.0.1931/driver-atmel-installer-x86-7.0.1931.msi

 

Hopefully one of these will provide what you need.

 

"Use the force"  http://microchip.wikidot.com/mplabx:start

 

 

 

A few interesting links in there.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
A few interesting links in there.
Indeed

- Download the SDK from www.opensource4pic.org  (It contains MPLABComm which is the layer for all the drivers. or

http://www.opensource4pic.org/content/content/mplab-x-sdk-mplab-x-ide

...

Welcome to the MPLAB X SDK! This software development kit contains documentation, sample code, and precompiled libraries for developing a Plugin for MPLAB X IDE, or a standalone application of the MDBCore API. Please use developers forum on Microchip Website for any help/suggestions on SDK.

...

Microchip

Developer Help

MDB: The Microchip Debugger

http://microchipdeveloper.com/mplabx:mdb

(why - MDB extension to Visual Studio Code?) 

"Use the force"

laugh

 

Edit: why

 

"Dare to be naïve." - Buckminster Fuller

Last Edited: Tue. Oct 16, 2018 - 03:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

More progress.

 

I can now program the 4809 without using the drag and drop method. ATprogram can directly talk to the onboard nEDBG chip and take in .hex files to flash the 4809.

 

Nest step...integrate atprogram into CVAVR

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Brian Fairchild wrote:

Next step...integrate atprogram into CVAVR

 

And done.

 

So I can now code a working program and program it into a chip all within CVAVR with no external hardware. I just use an ATmega4809-NANO.

 

For simple debugging the nano has an onboard LED and switch; for slightly more useful debugging I can printf out to the virtual COM port and see the text within CVAVRs built-in terminal.

 

I haven't had this much micro fun for ages.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And the same technique works on...

 

ATtiny416 Xplained Nano

ATtiny817 Xplained Mini

 

I'll get this written up as a tutorial.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How can that compile properly without a main() ???

Without further examination I have no Idea what at "TGLBIT" does. Is it a macro or a function?

How do I even know that the port is configured as an output properly?

 

For all I know you led might be blinking because it is configured as an input and the internal pullup resistor is toggled.

 

Dragging and dropping some file ( Where do you get it from ?) seems cumbersome.

I will stick to my old fashioned makefiles.

A bit of work in the beginning to set them up properly, but it is platform independent, and you can use any decent IDE to call a make target that compiles and flashes your code with a single key press, so I don't even have to move my hands from the keyboard or go search for my mouse.

 

I was a bit curious about that 4809 thing, so I found a link:

https://www.microchip.com/Developmenttools/ProductDetails/DM320115

 

Built-in debugger is nice for a starter kit, even though I prefer an external programmer/debugger with a serial connection like the ST-Link V2 for the STM32 processors.

That EUR3 programmer/debugger was one of the reasons that peaqued my interest in the STM32.

But for prices around EUR 10 it seems a nice way to start.

https://octopart.com/search?q=DM320115

 

Is this thing compatible with a breadboard if you solder in some headers?

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:
I have no Idea what at "TGLBIT" does. Is it a macro or a function?
Given that the 4809 have ports with:

/* I/O Ports */
typedef struct PORT_struct
{
    register8_t DIR;  /* Data Direction */
    register8_t DIRSET;  /* Data Direction Set */
    register8_t DIRCLR;  /* Data Direction Clear */
    register8_t DIRTGL;  /* Data Direction Toggle */
    register8_t OUT;  /* Output Value */
    register8_t OUTSET;  /* Output Value Set */
    register8_t OUTCLR;  /* Output Value Clear */
    register8_t OUTTGL;  /* Output Value Toggle */
    register8_t IN;  /* Input Value */
    register8_t INTFLAGS;  /* Interrupt Flags */
    register8_t PORTCTRL;  /* Port Control */
    register8_t reserved_0x0B;
    register8_t reserved_0x0C;
    register8_t reserved_0x0D;
    register8_t reserved_0x0E;
    register8_t reserved_0x0F;
    register8_t PIN0CTRL;  /* Pin 0 Control */
    register8_t PIN1CTRL;  /* Pin 1 Control */
    register8_t PIN2CTRL;  /* Pin 2 Control */
    register8_t PIN3CTRL;  /* Pin 3 Control */
    register8_t PIN4CTRL;  /* Pin 4 Control */
    register8_t PIN5CTRL;  /* Pin 5 Control */
    register8_t PIN6CTRL;  /* Pin 6 Control */
    register8_t PIN7CTRL;  /* Pin 7 Control */
    register8_t reserved_0x18;
    register8_t reserved_0x19;
    register8_t reserved_0x1A;
    register8_t reserved_0x1B;
    register8_t reserved_0x1C;
    register8_t reserved_0x1D;
    register8_t reserved_0x1E;
    register8_t reserved_0x1F;
} PORT_t;

I do kind of wonder why it wouldn't have simply used:

PORTF.OUTTGL = (1 << 5);

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:
How can that compile properly without a main() ???

Without further examination I have no Idea what at "TGLBIT" does. Is it a macro or a function?

How do I even know that the port is configured as an output properly?

 

TBH, I had these questions too, but didn't ask because I just assumed it was a combination of incomplete code and codevision differences from avr-gcc.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Of course it doesn't compile without a main().

It also doesn't work without setting up the peripherals.

Which is why I said to run Code Wizard which generates all the initialisation for you.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
Nest step...integrate atprogram into CVAVR

CodeVisionAVR V3 Revision History

http://hpinfotech.ro/cvavr_revision3.html

...

V3.33 Commercial Release

...

  • Chip Programmer
    • Added support for the Microchip EDBG programmer

...

Am assuming that nEDBG is in the set of EDBG.

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The problem at the moment is that CV requires studio to be installed. My interest was to bypass that.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

FAQ: atprogram standalone utility

https://microchip.secure.force.com/microchipknowledge/articles/en_US/FAQ/atprogram-standalone-utility/

Question

How to use atprogram.exe as a standalone utility without installing Atmel Studio? 

Answer

Atprogram utility can be used as a standalone utility, but unfortunately we do not have standalone installer for “atprogram.exe”. Following procedure is a ‘workaround’ to run atprogram.exe as standalone application. ...

Maybe by the MPLAB X 5.05 SDK would be easier.

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian, does the drag and drop programming technique really work for the ATtiny416 Xplained Nano?
 
The ATmega4809-NANO document talks about the virtual disk, but the manual for the ATtiny416 Xplained Nano (which has a DAC which I want to use) mentions only the on-board debugger and the virtual COM port.

 

Thanks!

Mike

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mha11 wrote:
Brian, does the drag and drop programming technique really work for the ATtiny416 Xplained Nano?

 

Sorry, when I wrote...

 

Brian Fairchild wrote:

ATtiny416 Xplained Nano

ATtiny817 Xplained Mini

 

...I was referring to using ATprogram to program the chip via EDBG.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just curious:

Is that file system write only, or can you also read from it?

 

With micro Python ( or circuit Python) there is often something similar, note that Python is an interpreter, not a compiler.

The workflow is to fire up your favourite editor, open the source file from your "mass storage device" and start programming.

Each time you save the file, the Python Device restarts with the new file.

 

With such an interpreter I can understand the benefit, especially if your "mass storage device" is implemented robust enough that it "always just works".

But once you have to go through the pain of compiling and linking a bunch of source files the elegance is already gone and for me, using an external programmer seems more convenient than struggling with a bootloader.

 

About that reliability issue:

DFU (Device Firmware Update) is a standarized protocol.

I got (slightly) aquainted with it with my Logic Analyser, which uses a Cypress CY7C68013A.

This chip has no FLASH, only ram. Every time it is plugged into USB it requests for firmware, and the driver on the PC is supposed to provide it via the DFU protocol.

(I think firmware can also be read from an external I2C EEPROM).

 

On my Linux Box I have vague memories that for some Atmel device (Can't remember which) a custom DFU driver was neccasarry, because Atmel thought itself smarter then the rest of the world and had to change a few bits here and there so the standard DFU did not work any more with Atmell stuff. Yuch.

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yet another way I thought of:

 

The Beagle Bone Black has a low enough current consumption that you can power it from an USB port.

If you have a new BBB and plug it in an USB port, you can fire up a web browser, type in it's ISP address and you get an IDE in which you can write and run "node.js"

 

On Linux and the Fuit brand drivers for TCP/IP over USB are installed by default, no extra steps required (maybe an udev rule).

On Windoze you have to install some driver as always, or else it does not understand what to do with an USB device.

 

The BBB also hosts its own website, which is a mirror of

http://beagleboard.org/getting-started

with instuctions on getting started.

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:
using an external programmer seems more convenient than struggling with a bootloader.
Then I guess you haven't seen this in action. Say you have built three AVR programs blinky.hex, alarm.hex, adc_test.hex what simpler way could there be than drag/drop each in turn onto the "AVR drive" and it just starts running a very short while later. Digging out your programmer and then coming up with some tortuous avrdude command to achieve the same will never feel as "slick".

 

Almost as slick is an SD based bootloader I did. You just plug in the SD card with the hex you want to use. Turn on the AVR and it reads/programs/starts the new program - again no messy / convoluted avrdude commands. The only caveat here is that the name of the file on the card has to be set to a higher number than the code already loaded into the AVR.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:

... using an external programmer seems more convenient than struggling with a bootloader.

 

For field upgrades, and upgrades performed by an end-user, a bootloader makes life sooooooo much easier.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss