blog.soch.cc

Nov 2025

Accidentally bricking an STM8S

The STM8S001J3 is an 8-bit microcontroller that’s dirt cheap. I recently bought some at 60 cents a piece. Alright, I admit, it’s not quite as cheap as dirt. I was looking for a cheaper alternative to the rather popular ATTINY85.

The STM8 chip has a few quirks. For one, it has 5 usable pins instead of 6, with the usual VDD and GND now accompanied by VCAP. It doesn’t have GCC support. It has no external reset pin (relevant for this story). It uses the SWIM protocol.

It’s also rather easy to brick. All you need to do is set PC6 to be an output. Once you flash some firmware that uses PC6 as an output to the chip, you can no longer flash it again.

stm8s001j3 pinout diagram

Since PC6 and SWIM share the same physical pin and we just overrided its behavior, that pin is forever fated to be an output.

Without an external reset pin, there’s no way to change this. In fact, the application note AN5047 even recommends adding a 5 second delay at the beginning of the firmware before assigning other functions to the SWIM pin.

I’m no amateur. I read datasheets, application notes and reference manuals. (cough) When I read that application note, I thanked ST Micro for such a great heads up. There was no way I was bricking the chip.


On this occasion, I needed some external clock signal so I could test a project I was working on. Instead of using some beefy ARM controllers, I decided to use the STM8S to generate a PWM signal. For some strange reason, which I myself cannot explain, I decided to use TIM2_CH1.

This was on physical pin 7. All good. Not a SWIM pin. But refer to the pinout diagram above. Do you see the square braces? It’s a remapped alternate function.

I thought: “Remapped? Probably just have to set some bit somewhere”. I was right. I had to set some bit somewhere. But not the usual somewhere in some memory mapped registers.

To enable the timer, I had to write an option byte which is located in EEPROM. You won’t be able to write the option bytes in firmware directly, unless some other bits are set. Instead of trying to figure out how to write the option bytes in firmware, I used stm8flash to write the bytes directly. How this was done is irrelevant anyways.

After writing the bytes, I no longer was able to program the chip. I got SWIM error 0x04 after SWIM error 0x04. I looked at the datasheet.

The bit I needed to write as AFR0, which I did. So why, in addition to the PWM not being generated, was I unable to write the chip again?

You already know.

I overrided the SWIM pin by accident. If I had really read the reference manual, I would have known that AFR0 not only enables TIM2_CH1, it also enables TIM1_CH1, which just so happens is on the SWIM pin. Gotcha!


It’s not a glorious story where I learned anything in particular, but I wanted to share my failure nonetheless. I’ll look back one day and either laugh or cringe or both. For now, I just need to remember:

  • read what happens when you write some hard to reach bit
  • have spares
  • it’s not the hardware, it’s you

Comments

Click to leave a comment on Mastodon. It'll show up here!