Skip to main content
Associate II
February 28, 2025
Solved

Programming STM32C011J6M6 with Arduino IDE

  • February 28, 2025
  • 2 replies
  • 711 views

Hello,

I'm using an STM32C011J6M6 with the Arduino IDE to register with theST25DV64KC-IE6S3 's EEPROM.

I previously used the ATTiny85 and would like to replace it with the STM32C0 to maintain the entire solution with ST components.

I made some changes to the code but I can't register with the SDA/SCL of the STM32C0.

According to the datasheet, I should use pin 8 as SCL and pin 1 as SDA, but the registration with the EEPROM isn't working, I tested the eeprom address with ATTiny85 and it is 0x53

In the project with the ATTiny85, I didn't need to declare the terminal to perform this registration. Does anyone have a programming example that communicates via I2C?

#include <Wire.h>
#include <EEPROM.h>

#define EEPROM_ADDRESS 0x53
#define START_ADDRESS 0 

int currentAddress = START_ADDRESS;

int counter = 0;

void setup() {
 Wire.begin();
}

void loop() {
 writeToEEPROM(currentAddress, currentAddress);

 currentAddress++;

 counter = (counter > 499) ? 0 : counter + 1;

 currentAddress = (currentAddress >= 999) ? START_ADDRESS : currentAddress;

...

void writeToEEPROM(int address, uint8_t value) {
 Wire.beginTransmission(EEPROM_ADDRESS);
 Wire.write((address >> ‌‌8) & 0xFF); // High address byte
 Wire.write(address & 0xFF); // Low address byte
 Wire.write(value);
 Wire.endTransmission();
}

 

Best answer by GabrielGranzotto

I found the problem, the Wire.h library does not work on the STM32C011J6M6 in the SO8N footprint for some reason that I could not identify.

I changed it to an I²C communication code without the library and reading the datasheet I got to the terminals that are specified as SDA and SCL in the code 

#define LED PA8
#define SCL_PIN 7
#define SDA_PIN PB7
#define ST25DV64K_ADDR 0x53

void i2c_init();
void i2c_start();
void i2c_stop();
void i2c_write_byte(uint8_t data);
void writeEEPROM(uint16_t memAddr, uint8_t data);

int memAddr = 0;
int counter = 0;

void setup() {
 pinMode(LED, OUTPUT);
 i2c_init();
}

void loop() {
 digitalWrite(LED, HIGH);
 delay(500);

 writeEEPROM(memAddr, counter & 0xFF);
 memAddr++;

 counter++;
 if (counter > 255) {
 counter = 0;
 }
 if (memAddr >= 1000) {
 memAddr = 0;
 }
 digitalWrite(LED, LOW);
}

void i2c_init() {
 pinMode(SCL_PIN, OUTPUT_OPEN_DRAIN);
 pinMode(SDA_PIN, OUTPUT_OPEN_DRAIN);
 digitalWrite(SCL_PIN, HIGH);
 digitalWrite(SDA_PIN, HIGH);
}

void i2c_start() {
 digitalWrite(SDA_PIN, HIGH);
 digitalWrite(SCL_PIN, HIGH);
 delayMicroseconds(5);
 digitalWrite(SDA_PIN, LOW);
 delayMicroseconds(5);
 digitalWrite(SCL_PIN, LOW);
 delayMicroseconds(5);
}

void i2c_stop() {
 digitalWrite(SCL_PIN, LOW);
 digitalWrite(SDA_PIN, LOW);
 delayMicroseconds(5);
 digitalWrite(SCL_PIN, HIGH);
 delayMicroseconds(5);
 digitalWrite(SDA_PIN, HIGH);
 delayMicroseconds(5);
}

void i2c_write_byte(uint8_t data) {
 for (int8_t i = 7; i >= 0; i--) {
 digitalWrite(SDA_PIN, (data & (1 << i)) ? HIGH : LOW);
 delayMicroseconds(2);
 digitalWrite(SCL_PIN, HIGH);
 delayMicroseconds(5);
 digitalWrite(SCL_PIN, LOW);
 delayMicroseconds(3);
 }
 
 digitalWrite(SDA_PIN, HIGH);
 digitalWrite(SCL_PIN, HIGH);
 delayMicroseconds(5);
 digitalWrite(SCL_PIN, LOW);
 delayMicroseconds(2);
}

void writeEEPROM(int memAddr, int data) {
 i2c_start();
 i2c_write_byte(ST25DV64K_ADDR << 1);
 i2c_write_byte(memAddr >> 8);
 i2c_write_byte(memAddr & 0xFF);
 i2c_write_byte(data & 0xFF);
 i2c_stop();
 delay(50);
}

 

2 replies

Tesla DeLorean
Guru
February 28, 2025

Please use the code pasting tool "</>" icon rather than dump partial source in-line. This way the formatting/indentation won't be lost.

You can also EDIT your own posts via the "V" to the upper right of your post(s)

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
GabrielGranzottoAuthorBest answer
Associate II
April 3, 2025

I found the problem, the Wire.h library does not work on the STM32C011J6M6 in the SO8N footprint for some reason that I could not identify.

I changed it to an I²C communication code without the library and reading the datasheet I got to the terminals that are specified as SDA and SCL in the code 

#define LED PA8
#define SCL_PIN 7
#define SDA_PIN PB7
#define ST25DV64K_ADDR 0x53

void i2c_init();
void i2c_start();
void i2c_stop();
void i2c_write_byte(uint8_t data);
void writeEEPROM(uint16_t memAddr, uint8_t data);

int memAddr = 0;
int counter = 0;

void setup() {
 pinMode(LED, OUTPUT);
 i2c_init();
}

void loop() {
 digitalWrite(LED, HIGH);
 delay(500);

 writeEEPROM(memAddr, counter & 0xFF);
 memAddr++;

 counter++;
 if (counter > 255) {
 counter = 0;
 }
 if (memAddr >= 1000) {
 memAddr = 0;
 }
 digitalWrite(LED, LOW);
}

void i2c_init() {
 pinMode(SCL_PIN, OUTPUT_OPEN_DRAIN);
 pinMode(SDA_PIN, OUTPUT_OPEN_DRAIN);
 digitalWrite(SCL_PIN, HIGH);
 digitalWrite(SDA_PIN, HIGH);
}

void i2c_start() {
 digitalWrite(SDA_PIN, HIGH);
 digitalWrite(SCL_PIN, HIGH);
 delayMicroseconds(5);
 digitalWrite(SDA_PIN, LOW);
 delayMicroseconds(5);
 digitalWrite(SCL_PIN, LOW);
 delayMicroseconds(5);
}

void i2c_stop() {
 digitalWrite(SCL_PIN, LOW);
 digitalWrite(SDA_PIN, LOW);
 delayMicroseconds(5);
 digitalWrite(SCL_PIN, HIGH);
 delayMicroseconds(5);
 digitalWrite(SDA_PIN, HIGH);
 delayMicroseconds(5);
}

void i2c_write_byte(uint8_t data) {
 for (int8_t i = 7; i >= 0; i--) {
 digitalWrite(SDA_PIN, (data & (1 << i)) ? HIGH : LOW);
 delayMicroseconds(2);
 digitalWrite(SCL_PIN, HIGH);
 delayMicroseconds(5);
 digitalWrite(SCL_PIN, LOW);
 delayMicroseconds(3);
 }
 
 digitalWrite(SDA_PIN, HIGH);
 digitalWrite(SCL_PIN, HIGH);
 delayMicroseconds(5);
 digitalWrite(SCL_PIN, LOW);
 delayMicroseconds(2);
}

void writeEEPROM(int memAddr, int data) {
 i2c_start();
 i2c_write_byte(ST25DV64K_ADDR << 1);
 i2c_write_byte(memAddr >> 8);
 i2c_write_byte(memAddr & 0xFF);
 i2c_write_byte(data & 0xFF);
 i2c_stop();
 delay(50);
}