Index

About the project

  1. about this documentation in HTML
  2. License

Development tools

  1. Linux
    1. Step-by-step tutorial development tools
      1. Hardware tools to flash and debug the firmware
      2. Tools to build the firmware
        1. stm8-binutils-gdb
      3. Tools to flash the firmware
      4. (optional) Tools do flash and debug the firmware
      5. (optional) Tools to debug using serial port
  2. Windows
  3. C library
  4. Various resources
    1. Printf example

Motor controllers

  1. BMSBattery S series
    1. LCD control panel
      1. LCD protocol
    2. BMSBattery S06S
      1. PWM signals
        1. very low speed - 6 steps
        2. low speed up to max speed - sineware
    3. BMSBattery S06P
      1. various info
        1. 01
        2. 02
    4. BMSBattery S12S
    5. BMSBattery bottle battery controller
  2. Other controllers
    1. Kunteng 18 mosfets motor controller
    2. Lishui motor controllers
      1. LSW-675
        1. Datasheets
        2. PWM signals
    3. JinHui motor controllers
  3. GreenEBikeKit

Motors

  1. BMSBattery Q75
  2. BMSBattery Q85
  3. BMSBattery Q100
  4. BMSBattery Q100C

Datasheets and application notes

  1. STM8S105C6T6
    1. Interrupts

Motor control

  1. Motor control scheme of S06S controller
  2. BLDC 6 steps
  3. PWM schemes
    1. So, Which PWM Technique is Best? (Part 1)
    2. So, Which PWM Technique is Best? (Part 2)
    3. So, Which PWM Technique is Best? (Part 3)
    4. So, Which PWM Technique is Best? (Part 4)
    5. So, Which PWM Technique is Best? (Part 5)
    6. So, Which PWM Technique is Best? (Part 6)
    7. So, Which PWM Technique is Best? (Part 7)
  4. PWM control and Dead Time Insertion
  5. Low inductance motors
  6. Throttle Control Modes
  7. Phase angle FOC
  8. PWM frequency VS motor eRPM
    1. Max motor speed using FOC
    2. Kelly controllers ultra high speed
  9. Sinusoidal Control of BLDCM with Hall Sensors Based
  10. Alternatives

Various

  1. Endless-sphere.com forum messages
    1. 2017.04.25 - Initial forum message
    2. 2017.05.08 - First flash and debug on a dev board
    3. 2017.05.18 - First code flashing and running
    4. 2017.05.20 - more new information
    5. 2017.08.23 - SxxP versus SxxS versus LSW-675
    6. 2017.09.01 - Trying to figure out an algorithm to automatically adjust ui8_position_correction_value
    7. 2017.09.02 - How to do FOC on the BMSBattery S06S/Kunteng STM8 motor controllers
    8. 2017.09.03 - more ideas about zero crossing for FOC
    9. 2017.09.05 - measuring IQ current and manually adjusting position_correction_value
    10. 2017.09.19 - measuring motor current
  2. How to unlock protected read memory
  3. STM8S003 board
  4. LOG
    1. 2017.06.19 - SVM current waveforms

Clipping

  1. 2017.05.22 - Hackaday Links: May 21, 2017

Smart BMS with bluetooth

Printf example

http://www.colecovision.eu/stm8/STM8S003F3%20Dev%20Board%20Power%20Input%20Onboard%203V3%20Regulator.shtml

Getting started with stm8 development using free software: Serial output on some cheap STM8S board


There are lots of very cheap STM8 development boards available from China. I bought one "STM8S003F3 Dev Board 5V/3.3V Power Input Onboard 3V3 Regulator" at 1.39€ including shipping. It turned out to have an STM8S103F3P6 µC though. This board is actually already one of the better ones, since it has pin headers already mounted. For many other cheap boards these are not included. While the boards themselves are very cheap, an stlink is not included. There are cheap ST-Link/V2 devices available from China, and I bought one at 4.49€, putting the total price at 5.88 €, which is not that far from some of ST's STM8-Discovery boards. This short tutorial presents a simple "Hello World" program for this setup, with an extra WaveShare RS232 board. I used a Debian GNU/Linux system, but the tutorial should work for other Linux distributions, *BSD or other Unices.
The tools we use are
• The SDCC compiler, version 3.5.0 or later to compile C programs for the stm8.
• stm8flash, to write programs onto devices.


Hardware setup


images/17-1.png To write our program to the board, we attach a an ST-Link/V2. The board does have an USB connector, which can be used to power it, but it should only be used when there is no ST-Link attached. We connect the RS232 board to power pins and for data to the I/O pins (TxIN to PC3, RxOUT to PC2); the serial cable is then attached to the RS232 board. On the other end the serial cable is attached to an RS232 port on a computer running a terminal program configured for 9600 baud, no parity, 8 bits, 1 stop bit and no flow control. We used a USB-to-serial converter and gtkterm.

Get SDCC


Depending on your operating system there might be an easy way to install SDCC 3.5.0 or newer using a package system or similar (e.g. apt-get install sdcc on Debian). While SDCC 3.4.0 should be sufficient for this tutorial, you might want to try a newer version in case you encounter any bugs. In particular, SDCC 3.4.0 has an issue with the library search path; this can be worked around by explicitly specifying the path to the standard library when linking.
SDCC binaries or a source tarball can be downloaded from its website.

Get stm8flash


The stm8flash source can be found at its GitHub location, where there is also a download link for a zip archive of the sources. To compile it, a C compiler, such as gcc, pkg-config and libusb need to be installed. Unzip the archive (e.g. using unzip stm8flash-master.zip) change into the directory stm8flash-master and type make. In case there are any errors, such as header files not found, check that pkg-config and development files for libusb are installed.

The Demo


We present a simple Demo that repeatedly outputs "Hello World!" on UART2 (the only UART the STM8S105 has). This demonstrates setting up and using the UART for serial I/O. Here is the C code:
// Source code under CC0 1.0
#include <stdint.h>
#include <stdio.h>

#define CLK_DIVR (*(volatile uint8_t *)0x50c6)
#define CLK_PCKENR1 (*(volatile uint8_t *)0x50c7)

#define UART1_SR (*(volatile uint8_t *)0x5230)
#define UART1_DR (*(volatile uint8_t *)0x5231)
#define UART1_BRR1 (*(volatile uint8_t *)0x5232)
#define UART1_BRR2 (*(volatile uint8_t *)0x5233)
#define UART1_CR2 (*(volatile uint8_t *)0x5235)
#define UART1_CR3 (*(volatile uint8_t *)0x5236)

#define UART_CR2_TEN (1 << 3)
#define UART_CR3_STOP2 (1 << 5)
#define UART_CR3_STOP1 (1 << 4)
#define UART_SR_TXE (1 << 7)

void putchar(char c)
{
while(!(UART1_SR & UART_SR_TXE));

UART1_DR = c;
}

void main(void)
{
unsigned long i = 0;

CLK_DIVR = 0x00; // Set the frequency to 16 MHz
CLK_PCKENR1 = 0xFF; // Enable peripherals

UART1_CR2 = UART_CR2_TEN; // Allow TX and RX
UART1_CR3 &= ~(UART_CR3_STOP1 | UART_CR3_STOP2); // 1 stop bit
UART1_BRR2 = 0x03; UART1_BRR1 = 0x68; // 9600 baud

for(;;)
{
printf("Hello World!\n");
for(i = 0; i < 147456; i++); // Sleep
}
}
SDCC is a freestanding, not a hosted implemenatation of C, and allows main to return void. Note that currently putchar() returns void. This is not standard compliant and might change to int in future sdcc versions. The printf() from the standard library uses putchar() for output. Since putchar() is device-specific we need to supply it. In this case we want it to output data using UART1.
The demo can be compiled simply by invocing SDCC using sdcc -mstm8 --std-c99 serial.c assuming the C code is in led.c. The option -mstm8 selects the target port (stm8). An .ihx file with a name corresponding to the source file will be generated.

Put the demo onto the board


Assuming stm8flash and serial.ihx are in the same directory, the board is attached through an stlinkv2 device, ./stm8flash -c stlinkv -p stm8s103f3 -w serial.ihx will write the demo onto the board. You can see the "Hello world" by attaching a serial cable to the DB9 connector on the RS232 board, and using a terminal program configured for 9600 baud, no parity, 8 bits, 1 stop bit and no flow control.

More about stm8flash


stm8flash was written by Valentin Dudouyt. It works both with stlink (including the one integrated on the discovery boards) and stlinkv2 devices. The programmer can be selected using -c stlink or -c stlinkv2. The target device is selected using the -p option (to get a list of target devices, use the -p option with an option argument that is not an stm8 device, e.g. -p help. stm8flash will treat filenames ending in .ihx a Intel hex, and other filenames as binaries.

More about SDCC


SDCC was initially written by Sandeep Dutta for the MCS-51, and has a relatively conservative architecture (see Sandeep Dutta, "Anatomy of a Compiler", 2000). It has been extended by various contributors and more recently, incorporated some cutting-edge technologies, in particular in register allocation (see Philipp Klaus Krause, "Optimal Register Allocation in Polynomial Time", 2013). The stm8 backend was mostly written by Philipp Klaus Krause for his research into bytewise register allocation and spilling (see Philipp Klaus Krause, "Bytewise Register Allocation", 2015).
SDCC is a C compiler that aims to be compliant with the C standards.
Important compiler options for STM8 developers include:
• -c to compile into object files to be linked later
• --std-c99 for compilation in C99 mode (some C99 features, e.g. variable-length arrays are not yet supported in sdcc though)
• --opt-code-size for optimization for code size
• --max-allocs-per-node to select the optimization level. the default value is 3000. Higher values result in more optimized code, longer compiler runtime, and higher memory usage during compilation.