Stm8s compiling or even just setting up to work VScode... ! Help!

Hi I am new to VScode and ProgramIO is there a site that describes how to setup ProgramIO to compile c code or even assembler for the STM8S003F3 ?

Many Thanks
David_1

1 Like

Example projects are there, documentation exists, but no board definition for STM8S003F3 exists yet.

You would have to follow docs on how to create a board definition, e.g. based on the stm8s103f3 definition. If you have problems with that let me know, but basically copying that file as boards/stm8s003f3.json in the project folder and changing up f_cpu to 16000000L, mcu to stm8s103f3p6, maximum_ram to 1024, maximum_size to 8192 and the macro STM8S103 to STM8S003 should work. With that file created you can use board = stm8s003f3 in the platformio.ini of the project to use the file.

There’s also currently a caveat that the SDCC compiler packaged by PlatformIO needs to be exchanged for the current 4.0 version in order to successfully compile code on Windows (Error 309 - "arduino-fade-all-pins" for the platform "ST STM8" - #18 by vampir8).

For assembly, just try and put .S (or .s or .asm) in the src/ folder. PlatformIO should attempt to assemble these with the configured assembler, which is

and then link them into the project.

2 Likes

Thanks @maxgerhardt for the detailed anwser.

I’ve added boards/stm8s003f3.json board definition to my local .platformio as follow, just not sure about what macro is and where to change it…

{
  "build": {
"core": "sduino",
"extra_flags": "-DSTM8S_003 -DSTM8S003",
"f_cpu": "16000000L",
"cpu": "stm8",
"mcu": " stm8s103f3p6",
"variant": "standard"
  },
  "frameworks": [
"arduino",
"spl"
  ],
  "upload": {
"maximum_ram_size": 1024,
"maximum_size": 8192,
"protocol": "serial",
"protocols": [
    "serial",
    "stlinkv2"
]
  },
  "name": "ST STM8S003F3 Board",
  "url": "https://tenbaht.github.io/sduino/hardware/stm8blue/",
  "vendor": "ST"
}

I’ve not been able to compile anything yet. It would be nice to have a PR with appropriate board and changes. If you can check content of the board definition file above, it can do it.

Thank in advance for the help

1 Like

I thought you wanted to do a stm8s003f3p6 board definition? If I change it to

        "mcu": "stm8s003f3p6",

and use the platformio.ini

[env:stm8s003f3]
platform = ststm8
board = stm8s003f3
framework = arduino

with the src\main.c code from platform-ststm8/examples/arduino-fade-all-pins/src/fade-all-pins.c at develop · platformio/platform-ststm8 · GitHub I get

Checking size .pio\build\stm8s003f3\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   7.3% (used 75 bytes from 1024 bytes)
Flash: [===       ]  34.3% (used 2813 bytes from 8192 bytes)
========================== [SUCCESS] Took 2.14 seconds ==========================

So that all works nicely.

Note: I’ve also pre-emptively replaced the toolchain in C:\Users\<user>\.platformio\packages\toolchain-sdcc with the v4.0 version, as noted in Error 309 - "arduino-fade-all-pins" for the platform "ST STM8" - #18 by vampir8.

Edit:

To program the chip for the first time, its flash needs to be unlocked – see post below

cd .platformio\packages\tool-stm8tools
stm8flash -c stlinkv2 -u -p stm8s003?3
3 Likes

Indeed this was a mistake on my side.
Replaced sdcc to v4.0 and finaly managed to make it compile with you help, thank you.

I was struggling because of some identations problem in board definition json, it was making the compilation fail.

Now the upload creates problems, i use STlinkv2, with default driver (also tried ST driver):

With "protocol": "stlinkv2 ",

    Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
    RAM:   [=         ]   7.3% (used 75 bytes from 1024 bytes)
    Flash: [===       ]  34.3% (used 2813 bytes from 8192 bytes)
    Configuring upload protocol...
    AVAILABLE: serial, stlinkv2
    CURRENT: upload_protocol = stlinkv2
    Uploading .pio\build\stm8s003f3\firmware.ihx
    Determine FLASH area

    libusb: error [init_device] device '\\.\USB#VID_1532&PID_007B&MI_01#9&129E28B5&0&0001' is no longer connected!
    libusb: warning [init_device] could not get node connection information for device '\\.\USB#VID_1532&PID_007A&MI_02#8&3360FDAD&1&0002': [87] Paramètre incorrect.
    Due to its file extension (or lack thereof), ".pio\build\stm8s003f3\firmware.ihx" is considered as INTEL HEX format!
    2913 bytes at 0x8000... Tries exceeded
    *** [upload] Error -1
    ======================================================= [FAILED] Took 1.17 seconds =======================================================
    The terminal process "C:\Users\neroc\.platformio\penv\Scripts\pio.exe 'run', '--target', 'upload'" terminated with exit code: 1.

With "protocol": "serial",

    Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
    RAM:   [=         ]   7.3% (used 75 bytes from 1024 bytes)
    Flash: [===       ]  34.3% (used 2813 bytes from 8192 bytes)
    Configuring upload protocol...
    AVAILABLE: serial, stlinkv2
    CURRENT: upload_protocol = serial
    Looking for upload port...
    Error: Please specify `upload_port` for environment or use global `--upload-port` option.
    For some development platforms it can be a USB flash drive (i.e. /media/<user>/<device name>)
    *** [upload] Explicit exit, status 1
    ================================================== [FAILED] Took 1.04 seconds ==================================================
    The terminal process "C:\Users\neroc\.platformio\penv\Scripts\pio.exe 'run', '--target', 'upload'" terminated with exit code: 1.

I’m looking arround what i can find to solve this.

1 Like

That space doesn’t look right

I’ve had this problem too until I unlocked the flash of my MCU.

Try opening a new terminal (Windows+R → cmd.exe) and

cd .platformio\packages\tool-stm8tools
stm8flash -c stlinkv2 -u -p stm8s003?3

To unlock the flash.

For the serial upload you would need to have a USB-serial adapter connected to the target board and I guess put the board in bootloader mode have a serial bootloader on the board (?) somehow.

Remember to power up the target board first, then the STLinkV2 programmer. If done right, it should display

C:\Users\Max\.platformio\packages\tool-stm8tools>stm8flash -c stlinkv2 -u -p stm8s003?3
Determine OPT area
libusb: error [init_device] device '\\.\USB#VID_062A&PID_5918&MI_01#9&1953ACC1&0&0001' is no longer connected!
Due to its file extension (or lack thereof), "Workaround" is considered as RAW BINARY format!
Unlocked device. Option bytes reset to default state.
Bytes written: 11

after that flashing should work.

2 Likes

Awesome @maxgerhardt, the unlocking procedure work using the stlinkv2 protocol

I’ll finally be able to write some code for https://github.com/nerocide/jhe42b_buzzer_firmware :partying_face:

Many thanks to you.

2 Likes

sorry all I got side tracked on some pcb Gerber problems… Rabbit hole back on this tonight after work :slight_smile:

Stil have some issue while using standard function from arduino.h, like tone()

#include <Arduino.h>
int BUZZER=13;
int buttonState = 0;

void setup()  {
  pinMode(BUZZER,OUTPUT);
} 

void loop()  {
  //delay(100);
  tone(BUZZER,266,200);
}

Compiler goes

ASlink-Warning-No definition of area SSEG <== only shows when delay is commented
?ASlink-Warning-Undefined Global '_tone' referenced by module 'main'
*** [.pio\build\stm8s003f3\firmware.elf] Error 1
================================================ [FAILED] Took 1.28 seconds ===============================================

Only if “standard” pinMode, digitalWrite, for, defay, if… are used , everything is fine.

1 Like

The Arduino core implementation (“SDuino”) is a stripped-down C reimplementation of the original one, but missing some featuers. tone() is one of the 6 things / modules not implemented in that core, as documented in sduino/status-todo.md at development · tenbaht/sduino · GitHub.

The Arduino-avr implementation looks rather complex with direct access to the TIMER1/TIMER0. But maybe it can be ported to STM8?

Other PWM functions like analogWrite() and the Servo library are available though.

Note that the original analogWrite() implementation is here. Maybe you can modify some timer register / prescaler to get it the frequency you want. Per docs a tone(pin, freq, duration) is equivalent to a PWM signal with duty cycle 50% and a variable PWM frequency. So an analogWrite() with a value of 128 and a change of the timer’s prescaler might do the trick? :slight_smile:

Edit: Depending on which pin you start the PWM outupt you’ll be using a different base timer. The Servo library actually has code

that referes to setting up the prescaler. With the help of the datasheet and the knowledge of some clock values one should be able to figure out what the input clock for the PWM peripheral is and what prescaler value is needed to get to something in between 100 and 2000 Hz, and then write that as high-low value (16 bit into two 8-bit values) into the prescalars.

Per

these are the possible PWM pins. SUPPORT_ALTERNATE_MAPPINGS is disabled by default so you have pin 2, 5, 6 and 12, which correspond to these pin names in the array at index 2, 5 etc.

so PA3 would be PWM capable pin e.g. and would use

Timer 2 channel 3 (“TIMER23”).

2 Likes

@jean.mich.c I’ve edited the post above regarding additional info for the timer registers that need to modified here.

As a sidenote, one can also do this in a blocking way, see e.g.

but for a non-blocking way as per above, a hardware timer must be used.

Edit: Even tested the tetris sketch above with output on PD4 (which is pin 13), and it works nicely after prefixing all the arrays with static const (so that they end up in flash and not overflow RAM…) and using a NPN transistor as an applifier for louder sound :slight_smile:

Edit 2: Also got this theme working by deleting all Serial.println() lines, the digitalWrite(13.. lines and changing melodyPin to 13 to get again output on PD4. Sounds good :smiley:.

1 Like

Well, again thanks @maxgerhardt, every steps of this project got me closer to the backhole :smiley:

I got Mario theme working has POC :partying_face: :partying_face: :partying_face: it’s perfect and the method i’ll use.

The timer method you described in previous post is just out of reach with my current knowledge (i might dig it a bit for learning purpose) and because i prefer a KISS approch.

What started as a drone finder buzzer firmware rewrite finally got me to learn (in pain^^) quite a lot on STM8 and platformIO (The ST IDE was also pain to setup, license stuff etc)

Edit: I created a PR for new definition board on STM8 repo https://github.com/platformio/platform-ststm8/pull/28
Code is now going on well, i’ll publish it when ready.

1 Like