Description
I'm working with the CH32V003 microcontroller and noticed that peripheral libraries (Wire for I2C, SPI, and UART) consume a significant amount of flash memory.
For example, just using the Wire library for I2C and Serial takes up about 73% of the available flash memory (12,018 bytes out of 16,384 bytes).
Here is a minimal example of my I2C code:
#include <Wire.h>
void setup() {
Wire.begin(8); // Initialize as I2C slave with address 8
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void loop() {
// Do nothing, waiting for data from master
}
void receiveEvent(int howMany) {
Serial.print("Received: ");
while (Wire.available()) {
char c = Wire.read();
Serial.print(c);
}
Serial.println();
}
Flash & RAM Usage After Compilation:
Sketch uses 12,108 bytes (73%) of program storage space. Maximum is 16,384 bytes.
Global variables use 680 bytes (33%) of dynamic memory, leaving 1,368 bytes for local variables. Maximum is 2,048 bytes.
I also noticed that SPI and UART libraries take up a lot of flash memory as well, making it difficult to fit multiple peripherals into the available 16 KB flash.
Comparison with MounRiver
When compiling a similar I2C example using MounRiver Studio, the flash usage is significantly lower:
text data bss dec hex filename
4196 64 260 4520 11A8 I2C_7bit_Mode.elf
This means that MounRiver only uses ~4.2 KB flash, while the Arduino Core version consumes ~12 KB, nearly 3 times more.
Questions:
- Are there any compiler flags or configuration options to disable unnecessary features and optimize resource usage?
- Is there any way to optimize flash usage while keeping I2C, SPI, and UART functionality?
Any suggestions for reducing flash usage would be greatly appreciated! 🚀