2025-06-27 7:10 AM
Hello,
i'm using arduino to code a STM32C011J6M6 and I want to use the Energy harvesting pin of ST25DV04K to source the IC when the VCC be off and the NFC Field be on.
But with this code the EH is on just when VCC and NFC Field are on.
if I remove all the EH_MODE code part, the circuit continues to works, so i believe that I'm coding this wrong. Someone already works with this kind of programming?
Best Regards
#include <Wire.h>
#define LED PC13
#define SCL_PIN PB6
#define SDA_PIN PB7
#define TILT PA13
#define ST25DV64K_ADDR 0x53
int memAddr = 0;
int counter = 0;
#define PTR_EH_MODE 0x0002
#define PTR_EH_CTRL_DYN 0x2002
void setup() {
pinMode(LED, OUTPUT);
pinMode(TILT, INPUT);
Wire.begin();
configureEnergyHarvesting();
}
void loop() {
digitalWrite(LED, HIGH);
delay(500);
uint8_t tiltCount = countTiltEvents();
writeEEPROM(memAddr++, 5);
writeEEPROM(memAddr++, (uint8_t)26);
writeEEPROM(memAddr++, 52);
writeEEPROM(memAddr, (uint8_t)counter);
counter = (counter + 1) % 256;
uint8_t readData = readEEPROM(memAddr++);
if (memAddr >= 1000) { memAddr = 0; }
digitalWrite(LED, LOW);
}
void configureEnergyHarvesting() {
Wire.beginTransmission(ST25DV64K_ADDR);
Wire.write((uint8_t)(PTR_EH_MODE >> 8));
Wire.write((uint8_t)(PTR_EH_MODE & 0xFF));
Wire.write(0x00);
Wire.endTransmission();
delay(10);
Wire.beginTransmission(ST25DV64K_ADDR);
Wire.write((uint8_t)(PTR_EH_CTRL_DYN >> 8));
Wire.write((uint8_t)(PTR_EH_CTRL_DYN & 0xFF));
Wire.write(0x01);
Wire.endTransmission();
delay(10);
}
uint8_t countTiltEvents() {
uint8_t count = 0;
unsigned long startTime = millis();
bool lastState = digitalRead(TILT);
while (millis() - startTime < 1000) {
bool currentState = digitalRead(TILT);
if (currentState && !lastState) {
count++;
if (count == 255) break;
}
lastState = currentState;
delay(1);
}
return count;
}
void writeEEPROM(uint16_t addr, uint8_t data) {
Wire.beginTransmission(ST25DV64K_ADDR);
Wire.write((uint8_t)(addr >> 8));
Wire.write((uint8_t)(addr & 0xFF));
Wire.write(data);
Wire.endTransmission();
delay(10);
}
uint8_t readEEPROM(uint16_t addr) {
Wire.beginTransmission(ST25DV64K_ADDR);
Wire.write((uint8_t)(addr >> 8));
Wire.write((uint8_t)(addr & 0xFF));
Wire.endTransmission();
Wire.requestFrom(ST25DV64K_ADDR, (uint8_t)1);
if (Wire.available()) {
return Wire.read();
}
return 0xFF;
}
2025-06-30 8:04 AM
Hi GabrielGranzotto,
when you want to change the static configuration registers, you need to open a secure session through the I²C bus. To do that you need to present the password before writing value to PTR_EH_MODE register. Factory default, the password is filled with 0.
I hope this can help you.
Kind Regards.
2025-06-30 8:09 AM
Hello René,
Did you have some example of how to open a secure session?
Best regards
2025-07-11 12:59 AM
Hi GabrielGranzotto,
You can find some simple example in the X-CUBE-NFC4 package (source code provided for ST25DV04K expansion board), the example shows how to enable or disable the Energy Harvesting and provides the code to open a secure session on I²C..
The example is in the folder Projects\NUCLEO-L476RG\Examples\EH of this package.
I hope this can help you.
Kind Regards.