An5780 Migration of Applications From Atmega328 Family To Stm32c0 Series Microcontrollers Stmicroelectronics
An5780 Migration of Applications From Atmega328 Family To Stm32c0 Series Microcontrollers Stmicroelectronics
Application note
Introduction
This application note provides some guidelines and methodology to migrate easily an Arduino® application from the
ATMEGA328 family to the STM32C0 series platform. It groups together all the most important information and lists the main
aspects that must be addressed, providing guidelines on the hardware, firmware and peripheral migration.
For a better understanding, the user needs to be familiar with STM32 microcontrollers.
For additional information, refer to the documents in Table 1.
1 General information
This document applies to all STM32C0 series devices. All these products are Arm®-based microcontrollers.
Note: Arm® is a registered trademark of Arm® Limited (or its subsidiaries) in the US and/or elsewhere.
Reference Document
The STM32C0 series include all ATMEGA328 standard peripherals like SPI, UART (see Table 5 for more details).
But also a set of peripherals with advanced features and optimized power consumption level like:
The STM32C0 devices include a set of peripherals with advanced features and optimized power consumption
compared to the ATMEGA328:
• 32-bit CPU with CPU frequency at 48 MHz
• DMA
• 12-bit ADC
3 Hardware migration
Package pin count GPIO number in STM32C0 GPIO number in ATMEG328 Difference I/Os
28 26 23 +3
32 30 25 +5
8 SO8N -
12 WLCSP -
20 TSSOP/UFQFPN -
28 UFQFPN VQFPN / SDIP
32 UFQFPN / LQFP VQFPN / TQFN
48 UFQFPN / LQFP -
The two MCUs do not use the same pin name. Table 4 shows a comparison between the two architectures.
The biggest difference is concerning the ADC reference voltage pin. In the STM32 discovery board VREF is
connected to VDD, so it can be impossible to use it in the Arduino® board or homemade PCB. Otherwise, with the
help of STM32duino, the Arduino® core supports the STM32C0.
If the user use shield like Arduino® or ST extension board (compatible with Arduino® R3 connector), it is
necessary to raise the header with a shield-stacking header.
Note: If the application needs to use A4 and A5 as ADC input, it is mandatory to unsolder R8 and R9 resistors. These
pull-up resistors are used as I2C as default.
The boot configuration of the STM32C0 is based on the STM32 M0+ core products.
In the ATMEGA328 family the software can boot only from flash memory or bootloader while STM32C0 series
permits to locate the BOOT vector in flash memory, system memory (boot loader) or RAM, based on the Table 4.
In fact it relocates the boot memory start address, for example if the user chose to boot from the main flash
memory, this memory area is aliased in the boot memory space (0x0000 0000), but still accessible from its
original memory space (0x0800 0000). It is reciprocal to other boot area.
The ATMEGA328 does not integer a system bootloader like in the STM32C0 (bootloader programmed during
production) but something close to the open bootloader in other words the user need to add is own code. If the
user wants to know more about the open bootloarder on the STM32: Open Bootloader library
A feature to check if the device is virgin has been implemented on the STM32C0 series. If the BOOT0 pin is
defining main flash memory as the target boot area and after loading option byte, the flash memory interface
checks if the first location of the main memory is programmed and return the result on FLASH_ACR register.
5 Peripheral migration
I2C 1 1
I2S (Inter-IC-sound) 1 -
CRC X -
RTC X -
WWDG X X
IWDG X -
Comparotor -(2) X
1. Refer to AN4894
2. STM32G0 series support this feature.
5.3 Debug
In addition to use the Arduino® API, it can be used the STM32 family Arm® debug tool. Two wires are needed to
debug the MCU.
The new debug methodology allows:
• SW-DP: serial wire
• BPU: break point unit
• DWT: data watchpoint trigger
• Flexible debug pinout assignment
• NVIC debug
• MCU debug box (support for low-power modes, control over peripheral clocks, etc.)
Figure 3. Block diagram of STM32C0 MCU and Arm® Cortex®-M0+-level debug support
Debug AP
Bridge DBGMCU
SWDIO SW-DP
SWCLK Debug AP
NVIC
DWT
BPU
DT19240V1
• Sleep mode corresponds to the ATMEGA328 idle mode. The CPU is clocked off, but other peripherals and
interrupt controller continue to run. Moreover, in the STM32C0 is possible to have the memory.
• Stop mode is similar to the active power save mode. The high-speed clock is stopped, and the SRAM is
retained. In both products it is possible to wake-up the product with a timer.
• Standby mode is similar to the power-down mode. The high-speed clock is off, the low-speed clock can
be running if the application uses IWDG, but the main difference is that the RAM is powered off in the
STM32C0.
• Shutdown mode has no equivalent in the ATMEGA328. It Is the ultimate low-power mode. All clocks and
peripherals are off.
• Wake-up source: the Table 10 shows the wake-up source comparison.
Idle:
– Internal and external interrupts
– WDT (watch dog timer)
– Reset
ADC noise reduction:
– WDT
– Reset
– BOR reset
– Internal interrupt (TWI address
match, Timer/Counter2 SPM/
Sleep mode: EEPROM ready)
– Peripheral event/interrupt – External interrupt (INT0 or INT1)
– EXTI interrupt/event Power-down:
– NVIC IRQ interrupt – WDT
– IWDG – Reset
– nReset – BOR reset
Stop mode: – Internal interrupt (TWI address
– Peripheral event/interrupt match)
– EXTI interrupt/event – External interrupt (INT0 or INT1)
Power-save:
Low-power modes and wake-up sources – NVIC IRQ interrupt
– IWDG – WDT
– nReset – Reset
Standby mode: – BOR reset
– Wake-up pins – Internal interrupt (TWI address
match, Timer/Counter2)
– IWDG
– External interrupt (INT0 or INT1)
– nReset
Standby:
Shutdown mode:
– WDT
– Wakeup pins
– Reset
– nReset
– BOR reset
– Internal interrupt (TWI address
match)
– External interrupt (INT0 or INT1)
Extended standby:
– WDT
– Reset
– BOR reset
– Internal interrupt (TWI address
match, Timer/Counter2)
– External interrupt (INT0 or INT1)
5.6.1 Clocks
• Up to 48 MHz • Up to 16 MHz
System clock frequency
• 12 MHz after reset based on HSI • 1 MHz after reset
APB frequency Up to 24 MHz -
RTC clock source LSI, LSE or HSE clocks divided by 32 -
MCO (PIN1, PIN2): LSI, LSE, SYSCLK, HSI48, HSE LSCO (pin):
Clock output CLKO (PIN): System clock
LSI, LSE available in stop mode
Internal/external clock measurement inputs:
Internal oscillator
measurement and • TIM14 inputs: GPIO, RTC, HSE/32, MCO, MCO2 -
calibration • TIM16 inputs: GPIO, LSI, LSE, MCO2
• TIM17 inputs: GPIO, HSE/32, MCO, MCO2
The STM32C0 has a good clock accuracy compared to the ATMEGA328. This can permit to clock other
peripheral with the CCO output. HSI is used for USART communication.
5.6.2 Reset
In the STM32C0 series there are different types of resets.
The first one is the power reset, which set all register as their reset values excepted when exiting standby mode
where the register outside VCORE domain are not impacted (back up registers, IWDG, standby/shutdown mode
control).
The second one is the system reset, which reset all registers to their reset value excepted the reset flags, the
RTC registers and the back-up registers.
The last one is the RTC domain reset, which only affects the RTC domains (LSE oscillator, RTC and RCC_CSR1
register).
Through specific option bits, the STM32C0 has also the possibility to change the configuration the NRST pin as:
• Reset input/output
• Reset input
• GPIO
1. Power reset
2. System reset
The Cortex M0+ has six systems interrupt (five more than STM8). These interrupts are non-maskable, they are
always activated. The priority of Reset, NMI and HardFault are fixed, in contrast to SVC, PendSV and SysTick,
which are programmable.
5.8 DMA
Direct memory access (DMA) is a new IP in the STM32C0 compared to the ATMEGA328P. It is clearly a major
asset to improve the product consumption and the CPU bandwidth. Data transfer without the CPU is allowed. It is
between peripheral-to-memory, memory-to-peripheral, memory-to-memory and peripheral-to-peripheral.
In the STM32C0, the DMA has three different channels. Each channel has four levels of priority (very high,
high, medium, and low), the channel one has priority over a request of channel two. Different transfer sizes are
available (byte, half-word, and word). A circular buffer management can be used.
5.10 RTC
The real time clock (RTC) is a new feature in STM32C0 that measure the time increasing. It can be clocked
by LSE, LSI or the HSE (divided by a prescaler), but to have the best accuracy it needs to use the LSE clock
source with 32.768 kHz external crystal. The RTC can measure the calendar date: sub-second, seconds, minutes,
hours (12 or 24 format), weekday, date with an automatic correction for 28, 29 (leap year), 30, and 31 days of the
month. It has one programmable alarm.
The RTC also can be used as timer to automatically wake up the product.
5.11 USART
5.12 I2C
5.14 SRAM
STM32C011x4: 6 Kbytes
16 Kbytes -
STM32C031x4:12 Kbytes
STM32C011x6: 6 Kbytes
32 Kbytes 2 Kbytes
STM32C031x6:12 Kbytes
5.15 Timer
Capture/
Counter Counter DMA request Complementary
Timer type Timer Prescaler factor compare
resolution type generation outputs
channels
Capture/
Counter Counter DMA request Complementary
Timer type Timer Prescaler factor compare
resolution type generation outputs
channels
All timers in the STM32C0 are 16-bit and the maximum clock is now 48 MHz and one 24-bit timer inside the
Cortex M0+ core, which is generally used as 1 ms time base.
5.16 ADC
5.17 SPI
I2S X -
Moreover, the STM32C0 is I2S compatible. If the application needs an audio interphase.
5.18 Watchdog
The STM32C0 series has two different watchdogs: the independent watchdog (IWDG) and one the system
window watchdog (WWDG). It is one more compared to the ATMEGA328.
Register size 8x64-bits (32-bit option byte + 32-bit complemented option byte) 4x8-bits
• NRST pin, reset holder, BOR and low power mode entry
protection
• BOR
• Boot configuration
Peripheral • Boot size and configuration
• Multiple bonding
configuration • Watchdog selection
• Clock remapping
• Memory protection
• Watchdog selection and freeze option
• Flash protection (RDP, PCROP, WRP, SEC)
In addition to the option bytes, the STM32C0 has some engineering bytes that are visible to the user. They
contain some useful information that is written during production testing, such as:
• Unique device ID
• Flash size
• Package type
• Calibration value of internal voltage reference and temperature sensor
To migrate an Arduino® application from the ATMEGA328 family to the STM32C0 series, it is necessary to install
STM32 Arduino® core and select the appropriate board as explained in Getting started. This core supports all
Arduino® software APIs from ATMEGA328, and even more.
This means that all Arduino® build-in examples can be used, like blinky, AnalogReadSerial, toneMelody.
Furthermore, the external Arduino® libraries (for sensor, shields, ...) can be used if they respect Arduino® API's
recommendation. If the application or the external libraries do not respect Arduino® API (for example with direct
register access), then porting is necessary.
Firstly, the user must install STM32CubeProgrammer (available in STM32CubeProg).
To program the STM32C0 microcontrollers with the IDE and STM32CubeProgrammer the user needs to use the
STLINK. Thus, with STM32C0316-DX it is necessary to use an external module like STLINK-V3 (MB1762) with
the B2B connector.
To know more about the use of STM32C0 and Arduino® core see more details in STM32duino.
In addition to the Arduino® standard API, the STM32 Arduino® core provides other APIs to get access to some of
STM32 hardware.
If needed, it is also possible to access to the STM32Cube drivers (HAL and LL) within Arduino® so that the
application can access to full STM32 hardware capabilities.
Revision history
Table 27. Document revision history
Contents
1 General information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 STM32C0 series overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3 Hardware migration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.1 Pinout compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.2 Board migration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4 Boot mode selection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5 Peripheral migration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.1 STM32 product cross-compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.2 System architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.3 Debug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.4 Power control peripheral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.5 Power consumption mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.6 Reset and clock controller (RCC) interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.6.1 Clocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.6.2 Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.7 Nested vectored interrupt controller (NVIC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.8 DMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.9 GPIO interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.10 RTC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.11 USART . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.12 I2C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.13 Flash memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.14 SRAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.15 Timer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.16 ADC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.17 SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.18 Watchdog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.19 Option and engineering bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6 Firmware migration using the Arduino® IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21
List of tables
Table 1. Document and website references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Table 2. Additional IOs for STM32C0 vs ATMEG328. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Table 3. Package type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Table 4. Arduino® pin comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Table 5. Boot mode configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Table 6. Peripheral summary of STM32C0 series and ATMEGA328 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Table 7. Comparison of CPU core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Table 8. Power control peripheral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Table 9. Power consumption comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Table 10. Wake-up source comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Table 11. Clock configuration STM32C0 vs ATMEGA328 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Table 12. High and low speed clock accuracy comparison. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Table 13. Reset source comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Table 14. Interrupts features comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Table 15. System interrupts comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Table 16. GPIO input voltage comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Table 17. USART peripheral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Table 18. I2C configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Table 19. Flash memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Table 20. SRAM density comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Table 21. Timers available in STM32C0 series MCUs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Table 22. ADC differences between STM32C0 series and ATMEGA328 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Table 23. SPI comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Table 24. IWDG comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Table 25. WWDG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Table 26. Option bytes comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Table 27. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
List of figures
Figure 1. Arduino® uno board with STMC0316-DK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Figure 2. Arduino® shield-stacking header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Figure 3. Block diagram of STM32C0 MCU and Arm® Cortex®-M0+-level debug support. . . . . . . . . . . . . . . . . . . . . . . . 9
Figure 4. Tail chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Figure 5. Arduino® uno board with STM32C0316-DK and STLINK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20