Wednesday, January 24, 2024

Defining F_CPU in Microchip Studio

The macro F_CPU specifies the CPU frequency and is used by libraries such as <delay.h> to calculate the delay based on the frequency of the processor or by UART libraries to calculate baud rate.

Up until now I used to define F_CPU like this:

#define F_CPU    16000000UL

Defining this way will work but it could lead to issues and confusion when you have multiple files that define this macro. The ideal way is to define it in a single place. This could be a Makefile if you are using custom Makefiles or in the IDE project configuration.

Defining F_CPU in Microchip Studio

Open the project properties window in Project -> Properties (Alt+F7) then in the left panel select the Toolchain menu. Next in AVR/GNU C Compiler -> Symbols use the green + button to define a new symbol F_CPU=16000000UL. Of course the F_CPU value depends on your CPU frequency that in this example is 16000000 Hertz or 16MHz. The UL at the end stands for unsigned integer.

Defining F_CPU symbol in Microchip Studio
click to enlarge

That's it. Now all included files in your project will use a single F_CPU value without giving errors such as "F_CPU is not defined", "F_CPU redefined", etc.

Since this method of defining is not as obvious as the first one, it is easy to forged to do it when starting a new project but there is a simple solution for that - using preprocessor conditionals.

#ifndef F_CPU
	#warning	"F_CPU not defined. Define it in project properties."
	
#elif F_CPU != 16000000
	#warning	"Wrong F_CPU frequency!"
#endif

This macro can be placed at the beginning of the main.c file before any type of code. If the F_CPU is not defined it will output a warning. If the F_CPU is defined but is not the value specified in the macro, it will also output a warning. This is also helpful to see what the CPU frequency is and to ensure that the defined value in project properties is correct.

Thursday, January 4, 2024

How to program an UPDI AVR microcontroller using avrdude and USB to serial programmer

After buying some ATtiny402 microcontrollers I've noticed that newer AVR models from Microchip are now using the UPDI interface for programming. Previously the programming was done using SPI or UART if a bootloader was present.

How to program an UPDI AVR microcontroller using avrdude and USB to serial programmer

What is UPDI and how do I use it?

UPDI stands for Unified Program and Debug Interface and is proprietary to Microchip. In many ways is similar to 1 wire UART. The main advantage is that it can be used for programming and also for debugging. Now there is no need for SPI, bootloader, debugWire... It's all Unified in one pin and one interface.

On certain devices such as ATtiny, the UPDI and Reset are on the same pin. In each case, the UPDI pin can also be used as a GPIO pin. When UPDI and Reset share the same pin, the functionality can be selected using the specific fuse. By default the fuse is set to select UPDI as a pin function. If you change the fuse and enable the Reset then you will need a 12V programmer to be able to program the microcontroller.

To prevent false triggering when the line is idle, it is recommended to have a pull-up resistor of at least 10k on the UPDI pin. Although some say that it works without problems even without a pull-up resistor so if you are using the UPDI pin also as a GPIO pin, you might consider not placing the resistor.

Source: https://microchip.my.site.com/s/article/AVR---Hardware-Design-Considerations-for-UPDI-pin.

Can i manually reset the microcontroller when UPDI is shared with RESET? 

Wednesday, December 20, 2023

Library for TMC2209 driver - AVR

This library is used to control one or more TMC2209 modules via UART using an AVR microcontroller. Apart from reading and writing the TMC2209 registers, this library can also be used to drive a stepper motor by using the stepperCon library in the background. The stepperCon library provides non blocking functions by using an interrupt, to drive multiple stepper motors with custom acceleration and speed, to keep track of motor positions, 3-axis motor coordination, as well as other useful functions. UART library is also provided that can use up to 2 USART peripherals at the same time: one for interfacing with the driver IC and one for debugging (assuming the microcontroller has two USART peripherals). 

The serial communication has some extra features for checkup such as:

  • reads the interface transmission counter (IFCNT) register after every write to ensure the IC received the correct data.
  • compares CRCs.
  • checks if correct number of bytes has been received.
  • if an error occurs it retries 2 times then sets a communication error flag that the user can check after each function is executed and take the appropriate action if an error occurs. 

Library for TMC2209 driver - AVR
The principle of operation is read-modify-write so there is no need to keep all driver IC registers in memory. The library only uses an 8 bytes union structure called replyDatagram where received and data to send is stored temporary.

If you wish to learn on how to wire the TMC2209 driver, you can find a tutorial here.

Contents

Thursday, December 14, 2023

TMC2209 stepper driver module tutorial

My first stepper driver module was A4988 and recently I have decided to give TMC2209 a try hearing how silent and energy efficient it is. Driving the A4988 is simple - just set direction, pulse the step pin and the motor moves. You can do the same thing with TMC2209 but this thing can do much more. It has UART interface, registers, algorithms and an 83 pages datasheet. I might be slow but I had to read the datasheet 3 times to wrap my head around on what things can this thing do. In my opinion it is a very good stepper driver so I made a library for it and also this tutorial to better help in using the library. Although the IC presented in this tutorial is TMC2209, most information can still be applied to other TMC ICs.

Contents

 

TMC2209 description

The TMC2209 is an ultra-silent motor driver IC for two phase stepper motors, developed by trinamic. TMC2209 pinning is compatible to other drivers as well as to the TMC2208. 

TMC2209 stepper driver module tutorial
Multiple versions of modules can be found that are using this IC, however the stand-alone chip can be used for a DIY module that could provide some cost savings.

Features:

  • 2-phase stepper motors up to 2.8A coil current (peak), 2A RMS
  • STEP/DIR Interface with 8, 16, 32 or 64 microstep pin setting
  • Smooth Running 256 microsteps by MicroPlyer™ interpolation
  • StealthChop2™ silent motor operation
  • SpreadCycle™ highly dynamic motor control chopper
  • StallGuard4™ load and stall detection for StealthChop
  • CoolStep™ current control for energy savings up to 75%
  • Low RDSon, Low Heat-Up LS 170mΩ & HS 170mΩ (typ. at 25°C)
  • Voltage Range 4.75… 29V DC
  • Internal Sense Resistor option (no sense resistors required)
  • Passive Braking, Freewheeling, and automatic power down
  • Single Wire UART & OTP for advanced configuration options
  • Integrated Pulse Generator for standalone motion
 

Monday, October 23, 2023

Pin interrupt library for AVR microcontrollers

This library can help to easily configure a pin interrupt on AVR microcontrollers without the need to check the datasheet every time. At the moment only PCINT pins are supported.

Pin interrupt library for AVR devices

In this blog post, I will explain how to use interrupts on AVR microcontrollers. Interrupts are a powerful feature that allow the microcontroller to respond to external or internal events without constantly polling for them. Interrupts can improve the performance and efficiency of your code, as well as enable new functionalities.

What are interrupts?

An interrupt is a signal that causes the microcontroller to temporarily stop its current execution and jump to a special function called an interrupt service routine (ISR). The ISR performs the necessary actions to handle the interrupt, and then returns to the original program flow. The ISR can be triggered by various sources, such as:

  • External pins (EXTINT)
  • Pin changes (PCINT)
  • Timers/Counters (TIMER)
  • Serial communication (USART, SPI, TWI)
  • Analog-to-digital conversion (ADC)
  • Analog comparator (AC)
  • Watchdog timer (WDT)
  • EEPROM ready (EE READY)
  • Store program memory ready (SPM READY)

Tuesday, October 3, 2023

stepperCon library for controlling stepper motor drivers using timer interrupt on AVR devices

The stepperCon library allows you to control multiple stepper motors at the same time. It can interface with any stepper motor driver that can take inputs such as step and direction. The library also has some extra useful functionalities such as motor coordination using Bresenham line algorithm - useful for controlling 3 axis motors for example, free spin - where the motor keeps spinning, and positioning the motor at a certain degree from 1 to 360 by finding the shortest path.

The maximum speed (step rate) on a 16MHz microcontroller is 25kHz, however the maximum practical speed is 6.25kHz or 8.33kHz depending on the motor, load, supply voltage and micro-step resolution. A higher voltage can yield higher RPM and with 1/32 micro-step resolution the step rate can be 25kHz as oppose to 6.25kHz when using 1/8 micro-step resolution.

Even with multiple motors running at the same time, the speed can be maintained and that is because the speed profile is segmented and calculated while the interrupt routine (ISR) is not executing steps. So the time spent inside the ISR is low since the calculations for acceleration and deceleration are not done there. Only the stepping is done inside the ISR. Having an interrupt to generate the steps is preferred over a function in the main loop where other code could delay the stepping function thus causing motor stuttering.

The stepper library includes the micros library that is used to trigger a timer interrupt every 40us and as a perk you also have a way to keep track of time in your project with a 40us time resolution.

 

stepperCon library characteristics

  • interface with multiple stepper motor drivers
  • up to 3 coordinated motors for X, Y and Z (can be extended for more)
  • maximum stepping rate speed of 6.25kHz (or 8.33kHz if the motor can have high acceleration). Speeds are given for a 16MHz CPU. With higher CPU clock the stepping rate will also be higher.
  • angular positioning function that finds the shortest path from current angle to target angle
  • perpetual motion
  • timer interrupt driven
  • individual settings for acceleration and deceleration

Monday, August 7, 2023

Plotting data from a logic analyzer - CSV and LibreOffice Calc

Sometimes it is useful to visualize data from a logic analyzer in a graphical way but unfortunately not many applications can do that so I came up with a solution involving CSV data and LibreOffice Calc. Here I am using Logic from Saleae but any software that can export the data to a CSV file can work.

Plotting data from a logic analyzer

Usage

First thing to do is to export the data from the logic analyzer software to a CSV file format. In Logic 2 this can be easily achieved by going to File -> Export Data or by pressing CTRL + SHIFT + S.

Exporting CSV in Logic 2 from Saleae