USART Library query

Hi I am starting to program an atmega4809 chip and new to bare bones, My application uses 3 USARTS, and it occurs to me having started to explore the Microchip code examples, that functions such as transmitChar, receiveChar, transmitString etc are going to be highly reusable across projects, so I’ll write them and place in a library. (never built a lib before).

However. it also occurs to me that such a library already exists for the 4809 chip perhaps? Is there a goto place for such things? I had a quick google but didn’t see anything obvious.

thanks for help,
Paul

The U(S)ART functionality would be expected to be inside the framework that one is using – for the ATMega4809 I only know of the Arduino core and what Atmel START framework (ASF) provides. Note that however the ASF is not a supported framework in PlatformIO.

The Arduino core implementations for the ATMega4809 (e.g. the MegaCoreX) all just use the pre-given avr/io.h header and then start using the registers themselves. See e.g. the UART library / module part in MegaCoreX/megaavr/cores/coreX-corefiles/UART.cpp at master · MCUdude/MegaCoreX · GitHub (and the .h file).

So, since the only supported framework for the ATMega4809 is Arduino, and the Arduino core already has the HardwareSerial library / class that provides the USART functionality, it’s not really useful to provide another library for it, except that library has some feature that the core deosn’t have.

For barematel cases however, sure, you can write a library that implements U(S)ART functionality. The code in e.g. TB3216_Getting_Started_with_USART/Send_Hello_World/main.c at master · MicrochipTech/TB3216_Getting_Started_with_USART · GitHub does not rely on any ASF-specific headers as I see it, so it should directly compile in a bare-metal project.

So your main search points for a pre-made library for something would be the Atmel START framework and the Arduino core implementations / libraries, if you want to create new libraries for bare-metal programming cases.

E.g., many other examples can be searched through at Microchip Technology · GitHub (and Ctrl+F “ATmega4809”, “Highlight all”). There are minimal examples for RTC, ADC, TCA/TCB (=timers, PWM?), AC (analog comparator), CCL (Configurable Custom Logic) and SPI.

Thanks very much for all this info Max, much appreciated. I’ve read some of the MegaCorex info and it looks brilliant. It’s been very interesting reading AVR programming books which get you directly in touch with the hardware, and that’s useful to me, but I don’t want to go reinventing core code functions such as transmitChar etc. I’ll explore some more and come back if I have any further quetions.
Paul

I have downloaded and unzipped the MegaCoreX repository and used the PIO.ini template from the MegaCoreX site. However, I am not sure how platformio will find what it needs from MegaCoreX. Do I need to install MegaCoreX in a particular place for it to work with platformio or specify a path somewhere?

Also, I have read the readme.md of the megacoreX repo, but couldn’t find function lists. Do they exist somewhere or do I have to explore .h files to find out about functionality? Sorry for basic questions.
Paul

answering my own question, I have found the folder C:\users\paul.platformio\packages which already includes MegaCoreX. Fine.

When I compile, I’m now getting this:

```

compilation terminated.
In file included from C:\Users\Paul.platformio\packages\framework-arduino-megaavr-megacorex\cores\MegaCoreX\wiring_private.h:31:0,
from C:\Users\Paul.platformio\packages\framework-arduino-megaavr-megacorex\cores\MegaCoreX\WInterrupts.c:31:
C:\Users\Paul.platformio\packages\framework-arduino-megaavr-megacorex\cores\MegaCoreX\Arduino.h:154:10: fatal error: pins_arduino.h: No such file or directory


  • Looking for pins_arduino.h dependency? Check our library registry!

thanks very much for help.
Paul

If you just want to use the pre-existing Arduino core (in the MegaCoreX variant) for the ATMega4809 chip, you just have to tell PlatformIO to include that as your framework.

See board docs.

So you would do a normal platformio.ini

[env:ATmega4809]
platform = atmelmegaavr
board = ATmega4809
framework = arduino

and then create a src\main.cpp where you would start using the Arduino core functions (and intended function structure)

#include <Arduino.h>

const int blinky_pin = PIN_PC3; 

void setup() {
  //UART init @ 9600 baud
  Serial.begin(9600);
  //print on UART with line ending
  Serial.println("Hello, world!!");
  //set GPIO pin to OUTPUT (push-pull)
  pinMode(blinky_pin, OUTPUT);
}

void loop() {
  //print on UART with line ending
  Serial.println("Hello there");
  //delay, set high, delay, set low
  delay(1000);
  digitalWrite(blinky_pin, HIGH);
  delay(1000);
  digitalWrite(blinky_pin, LOW);
}

Then when you build it you can also see the packages that are being used

CONFIGURATION: https://docs.platformio.org/page/boards/atmelmegaavr/ATmega4809.html
PLATFORM: Atmel megaAVR (1.3.0) > ATmega4809   
HARDWARE: ATMEGA4809 16MHz, 6KB RAM, 48KB Flash
PACKAGES:
 - framework-arduino-megaavr-megacorex 1.0.7   
 - toolchain-atmelavr 2.70300.201015 (7.3.0)   
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 12 compatible libraries
Scanning dependencies...
No dependencies
...
Building .pio\build\ATmega4809\firmware.hex
Checking size .pio\build\ATmega4809\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   2.6% (used 161 bytes from 6144 bytes)
Flash: [=         ]   5.9% (used 2894 bytes from 49152 bytes)
==================== [SUCCESS] Took 1.88 seconds ====================

(notice framework-arduino-megaavr-megacorex 1.0.7)

That’s how you use the existing UART functionality in the Arduino core the ATMega4809 (note that Serial is an instance of the UartClass class instantiated with the argument UART0 and some other pins, see here).

That of course builds the whole Arduino core with the firmware, too, once we set framework = arduino.

Or am I misunderstanding your question?

Note: If you’re using a board like the Arduino Nano Every, that is a seperate board (Arduino Nano Every — PlatformIO latest documentation) and also uses a different Arduino core implementaiton. Note MegaCoreX but GitHub - arduino/ArduinoCore-megaavr: Arduino Core for the ATMEGA4809 CPU.