programming/ library tips (new to AVR)

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

Hello,

Just at the outset to say thank you for reading this, it is a bit long, but it's because of context.

 

I have a fair bit of programming background, some in arduino ( In Arduino I wrote some control stuff for stepper motors and an interrupt driven shaft encoder), so that's roughly my level. For various reasons I thought I'd dip into 'direct' programming (using a mega4809 because it has the four USARTs), so I bought a nice book which I'm following. I've called it 'direct' programming, because in my head it would be 'non Arduino'. So whilst I'm following the concepts of setting bits in various registers to configure the avr peripherals and I'm ok with bit shifting and logical operations, I've come unstuck, perhaps in a concepts way, here's my problem:

 

Thinking about the USART chapter in the book, we've got to writing code for transmitChar and receiveChar and it's good to do this because you learn how stuff works. However, I'm thinking this is of course reusable code, so I'd want it in a library, but then I'm thinking why reinvent the wheel - this stuff must be in a library aklready? So this is where I'm stuck, I think there must be a usart library? If there is one, where would that be and how would I find out what functions it has?

 

The other question related to reusability of code is (I think), Arduino. I know Arduino has libraries and Arduino works with atmega4809, so should I be using Arduino libraries or (MegaCoreX is it the same thing)?

 

So if I use Aduino libraries my code is of the form Setup() and loop() rather than Main() is that right? and does this matter?

 

You can see I'm at a pretty basic level when it comes to avr, many thanks for help on this,

Paul

Paul

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

The 4809 is an Xmega. Most of the traditional "library code" written for AVR is actually for tiny/mega so you may have a job finding existing libraries for Xmega.
.
Having said that one thing in your favour is that for Xmega they chose to define register blocks for things like UART as C struct. So you should be able to write generic UART_init(), UART_sendchar() and so on where you simply pass the base address of the four identical UART and the same code will work for all.
.
Previously for tiny/mega you would have needed separate UART0_init(), UART1_init(), etc etc
.
Maybe look at start.atmel.com but, like Arduino if you use library code like that you simply "learn the library" not "learn the chip" so if you want to experience "bare metal programming" and learn about the chip itself you might be better writing from scratch.

Oh and the only real difference between Arduino and "normal" programs (apart from the fact that it is C++) is Arduino has a hidden main() that effectively says

int main(void) {
    setup();
    while(1) {
       loop();
    }
} 

 

 

Last Edited: Sun. Feb 7, 2021 - 01:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

paulskirk53 wrote:
I think there must be a usart library?
or source code snippets

paulskirk53 wrote:
If there is one, where would that be ...
Microchip Technology, AVR examples, GitHub

paulskirk53 wrote:
... and how would I find out what functions it has?
By reading the application note and browsing.

paulskirk53 wrote:
so should I be using Arduino libraries or (MegaCoreX is it the same thing)?
Your choice though please recognize copyright and license (right to copy, how to copy)

 


Code Snippet Manager | Microchip Studio

 

GitHub - microchip-pic-avr-examples/atmega4809-getting-started-with-usart-studio: This repository contains 5 bare metal code examples that show how to use the USART peripheral of the ATmega4809 device in different modes. It's a companion to the TB3216-Getting Started with Universal Synchronous/Asynchronous Receiver/Transmitter (USART)

via Getting Started with Universal Synchronous and Asynchronous Receiver and Transmitter (USART)

or TB3216 Getting Started with USART via ATMEGA4809 - 8-bit Microcontrollers (documents tab)

 

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

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

paulskirk53 wrote:
so I bought a nice book which I'm following.

which book, please provide link, so we know how good it is, or if it’s trash.

paulskirk53 wrote:
using a mega4809 because it has the four USARTs)

hopefully you have the 4809 curiosity board with built in programmer/debugger!

MPLABX or AS7 will recognize this board and guide you to resources, and also check out any app notes MC provides with sample code.

jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

Thanks very much for replies, much appreciated. I'm not a commercial programmer, just writing stuff to control my own telescope and observatory robotics. I also don't want to infinge any copyright, so presumably anything I find will come with terms and conditions of use and I'll abide by those.

 

I think the book is probably a very good one (recognising my 'new' status isn't necessarily a good judge) it's MAKE: AVR Programming by Elliot Williams. There are a few cliff edges in it (I realise that's subjective), but having a programming background and a bit of electronics has helped me through. Also the MCU context is tough (I find as a starter), so I thought Elliot's project based approach was useful. link https://www.amazon.co.uk/s?k=mak...

 

I got stuck around the USART chapter and need to (eventually) get my head around a non blocking serial transmission method, as my 4809s will need to comm with each other and also with clients via a C# driver which uses serial comms as per the astronomy ASCOM standards. Hence my interest in USARTs

 

Thanks for all the info about microchip avr examples and app notes, I'll have a look there.

 

I also found this Library which looks as though it supports 4 USARTs:

 

https://andygock.github.io/avr-u...

 

 

hopefully you have the 4809 curiosity board with built in programmer/debugger!

MPLABX or AS7 will recognize this board and guide you to resources, and also check out any app notes MC provides with sample code.

jim

 

I (perhaps foolishly from a startup perspective) bought the 4809 as a 40 pin DIP which I can breadboard. I was intending to follow the link below to program as UPDI or with a booloader with an FTDI I have:

 

http://www.technoblogy.com/show?...

 

However, taking on board (sorry!) about the 4809 curiosity board, if this is a good way to learn I'm happy to explore that too.

 

again thanks to everyone for your help,

Paul

Paul

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

I have been using a single library to operate all ports on chips such as mega64 with multiple USARTs for over 20 years.

For each port, store its base address and other information in something similar to the DCB structure of the WIN32API, and pass only the address of that structure to the function.

I'm already using all USARTs on mega4809 and AVR-Dx at the same time.

In tiny0 / 1, mega0, AVR-Dx, the register set of the port is defined as a structure, but even in the old chip, all the registers can be accessed as long as the base address is available.

 

Arduino supports various architectures, but the library for mega4809 can only be used on the same architecture.

To use it with "Arduino", you have to judge the architecture and select the appropriate library.

Last Edited: Sun. Feb 7, 2021 - 04:50 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I suggest getting an Arduino "Nano Every" development board.   It has an ATmega4809 and full support of the Xmega naming conventions that are used when programming on the "direct" (to peripheral control registers) level.   They only cost about $11 USD and plug into the PC's USB port.

 

  Also, download and review the ATmel Support Ap Note #1000.  This explains all the differences between the programming of the AVR peripherals in the "new" AVRs like the Mega4809 and the old AVRs like the Mega328P that is found in the Arduino UNO and Nano.

 

  Take a peek at the source code for the different USART libraries used by the UNO/Nano and the 4809-based Nano Every.  The source for the library code that runs the peripherals is located in the Arduino file structure>  arduino-n.n.nnnn\hardware\arduino\avr\libraries\

 

Arduino takes the setup() and loop() functions and turns them into a main() function, which then gets compiled like any other C++ code.   Arduino uses a "hardware abstraction language"  or HAL ("open the pod bay doors, HAL.  I'm sorry, Dave, I can't do that" showing my age here) to handle the USART, I2C, and SPI.   This allows the same more-or-less code to be used between the various CPUs that are supported by Arduiino.  Like the STM32F and the ESP8266.

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

Hello, I purchased an Arduino Nano Every board as a test platform. I have just started using platformio and vscode instead of the arduino IDE. However just as a start to check I could program the board, I've used the arduino ide. I picked the "nano every" board from the menu options and com3. The sketch is just an empty setup() and loop(). I get this error message from avrdude in the arduino IDE:

 

Sketch uses 802 bytes (1%) of program storage space. Maximum is 49152 bytes.
Global variables use 22 bytes (0%) of dynamic memory, leaving 6122 bytes for local variables. Maximum is 6144 bytes.
avrdude: jtagmkII_initialize(): Cannot locate "flash" and "boot" memories in description

 

The board has a green led lit (solid)

 

thanks for help with this.

Paul

Paul

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

the blink led sketch seems to work even though the error nessage appears at upload. So I think I'm ok....

 

thanks

Paul

Paul