Nucleo_f303k8 with Wire.h library problem

Hi everyone,
I’m having problem uploading my code to Nucleo F303K8. The code compiles, but, failed to upload.
Here’s my code:

#include <Arduino.h>
#include <Wire.h>


void setup() {
  Wire.begin();

  Serial.begin(9600);
  while (!Serial)
     delay(10);
  Serial.println("\nI2C Scanner");
}


void loop() {
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(15000);           // wait 5 seconds for next scan
}

.ini

[env:nucleo_f303k8]
platform = ststm32
board = nucleo_f303k8
framework = arduino

I’m using stlink2 (clone) on Windows 11 Enterprise, 24H2

The error:

xPack Open On-Chip Debugger 0.12.0-01004-g9ea7f3d64-dirty (2023-01-30-15:04)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 1

srst_only separate srst_nogate srst_open_drain connect_deassert_srst

[stm32f3x.cpu] halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
** Programming Started **
Warn : Adding extra erase range, 0x08005cf8 .. 0x08005fff
Error: error writing to flash at address 0x08000000 at offset 0x00000000
embedded:startup.tcl:1516: Error: ** Programming Failed **
in procedure 'program'
in procedure 'program_error' called at file "embedded:startup.tcl", line 1581
at file "embedded:startup.tcl", line 1516
*** [upload] Error 1
======================================================================================= [FAILED] Took 4.36 seconds =======================================================================================

What i tried:
-Uninstalled vs code
-Uninstalled platformio
-Using wire as reference in .ini file.
Any ideas?
Thanks in advanced :slight_smile:

That is very unusual. But why do you need a STLinkV2 Clone if you’re uploading to a “Nucleo F303K8”? It has a STLINK V2.1 on-board. Are you using a custom board?

Can you connect to chip using the official STM32CubeProgrammer tool and erase the flash?

Sorry I said wrong. I’m working on two boards in the same time. To Necleo I don’t use STLINK V2, because the board has it’s own. I just use the USB port to upload.
And yes, with ST utility tool I can connect to the board and erase it for example. I tested another brand new board on two win11 laptops and got the same error. Both boards from Mouser.

Can you check in the STM32CubeProgrammer or ST-Link utilty whether the flash is write-protected via the option bytes? Is erasing the flash possiible?

I forgot to mention that it worked to upload any other code which is not related to I2C. So ya, it works with something else

There are some specialites when it comes to I2C and the solder bridges on the Nucleo F303 board. The default SCL and SDA pins are

The user manual


But anyways, that should not cause a straightup flashing failure.

Can you take the binary from .pio/build/<env>/firmware.bin and use ST-Util to flash it to address 0x08000000?

Done!


17:23:10 : V2J37M0
17:23:10 : Connected via SWD.
17:23:10 : SWD Frequency = 4,0 MHz.
17:23:10 : Connection mode : Normal.
17:23:10 : Debug in Low Power mode enabled.
17:23:10 : Device ID:0x438 
17:23:10 : Device flash Size : 64KBytes
17:23:10 : Device family :STM32F303x4-x6-x8/F328xx/F334xx
17:31:02 : [firmware.bin] opened successfully.
17:31:02 : [firmware.bin] checksum : 0x00253847 
17:31:19 : Memory programmed in 1s and 766ms.
17:31:19 : Verification...OK
17:31:19 : Programmed memory Checksum: 0x00253847

And then when you use the “Monitor” task in PlatformIO and press the reset button on the board, does it show any serial output?

And then when you use the “Monitor” task in PlatformIO and press the reset button on the board, does it show any serial output?

Yes.

--- Terminal on COM8 | 9600 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H

I2C Scanner
Scanning...
No I2C devices found

After now having flashed the firmware once with a other tool, does PlatformIO’s “Upload” task work withot error?

After now having flashed the firmware once with a other tool, does PlatformIO’s “Upload” task work withot error?

No, still the same

Mhm. No idea why OpenOCD fails to upload then. Maybe try opening an issue at https://github.com/platformio/platform-ststm32/issues.