├── .cproject ├── .gitignore ├── .project ├── README.md ├── configuration_tool ├── configuration_tool-sources │ ├── build.xml │ ├── manifest.mf │ ├── nbproject │ │ ├── build-impl.xml │ │ ├── genfiles.properties │ │ ├── private │ │ │ ├── config.properties │ │ │ ├── private.properties │ │ │ └── private.xml │ │ ├── project.properties │ │ └── project.xml │ └── src │ │ └── Flexible_OpenSource_EBike_firmware_configuration_tool │ │ ├── Configuration_tool.form │ │ └── Configuration_tool.java ├── linux_scripts │ ├── build_and_flash.sh │ ├── option_bytes_pwm_n_channels_enabled.bin │ └── write_option_bytes.sh └── windows_scripts │ ├── Start_Compiling.bat │ ├── WriteOptionBytes.bat │ ├── clean.bat │ ├── libiconv2.dll │ ├── libintl3.dll │ ├── make.exe │ └── optionbytes.hex ├── firmware ├── .cproject ├── .project ├── .settings │ ├── SDCC Batch.launch │ ├── language.settings.xml │ ├── org.eclipse.cdt.core.prefs │ ├── org.eclipse.cdt.managedbuilder.core.prefs │ └── org.eclipse.cdt.ui.prefs ├── Flash_Debug-STM8.launch ├── Makefile_linux ├── Makefile_windows ├── OSEC Parameter Configurator.jar ├── OpenOCD-v0.10.launch ├── Start_Compiling.bat ├── StdPeriphLib │ ├── README.md │ ├── inc │ │ ├── stm8s.h │ │ ├── stm8s_adc1.h │ │ ├── stm8s_adc2.h │ │ ├── stm8s_awu.h │ │ ├── stm8s_beep.h │ │ ├── stm8s_can.h │ │ ├── stm8s_clk.h │ │ ├── stm8s_conf.h │ │ ├── stm8s_exti.h │ │ ├── stm8s_flash.h │ │ ├── stm8s_gpio.h │ │ ├── stm8s_i2c.h │ │ ├── stm8s_it.h │ │ ├── stm8s_itc.h │ │ ├── stm8s_iwdg.h │ │ ├── stm8s_rst.h │ │ ├── stm8s_spi.h │ │ ├── stm8s_tim1.h │ │ ├── stm8s_tim2.h │ │ ├── stm8s_tim3.h │ │ ├── stm8s_tim4.h │ │ ├── stm8s_tim5.h │ │ ├── stm8s_tim6.h │ │ ├── stm8s_uart1.h │ │ ├── stm8s_uart2.h │ │ ├── stm8s_uart3.h │ │ ├── stm8s_uart4.h │ │ └── stm8s_wwdg.h │ └── src │ │ ├── stm8s_adc1.c │ │ ├── stm8s_adc2.c │ │ ├── stm8s_awu.c │ │ ├── stm8s_beep.c │ │ ├── stm8s_can.c │ │ ├── stm8s_clk.c │ │ ├── stm8s_exti.c │ │ ├── stm8s_flash.c │ │ ├── stm8s_gpio.c │ │ ├── stm8s_i2c.c │ │ ├── stm8s_it.c │ │ ├── stm8s_itc.c │ │ ├── stm8s_iwdg.c │ │ ├── stm8s_rst.c │ │ ├── stm8s_spi.c │ │ ├── stm8s_tim1.c │ │ ├── stm8s_tim2.c │ │ ├── stm8s_tim3.c │ │ ├── stm8s_tim4.c │ │ ├── stm8s_tim5.c │ │ ├── stm8s_tim6.c │ │ ├── stm8s_uart1.c │ │ ├── stm8s_uart2.c │ │ ├── stm8s_uart3.c │ │ ├── stm8s_uart4.c │ │ └── stm8s_wwdg.c ├── WriteOptionBytes.bat ├── adc.c ├── adc.h ├── brake.c ├── brake.h ├── clean.bat ├── config-example.h ├── config.h ├── docs │ ├── Tutorial for seting up the toolchain in windows environment.pdf │ ├── firmware_structure.odg │ └── firmware_structure.png ├── ebike_app.c ├── ebike_app.h ├── eeprom.c ├── eeprom.h ├── gpio.c ├── gpio.h ├── interrupts.h ├── main.c ├── main.h ├── motor.c ├── motor.h ├── optionbytes.hex ├── pas.c ├── pas.h ├── pwm.c ├── pwm.h ├── timers.c ├── timers.h ├── tools │ ├── 60-st_link_v2.rules │ ├── BLDC_SPWM_Lookup_tables.ods │ ├── KT-LCD data packet CRC calc.ods │ ├── OSEC.java │ ├── cygwin │ │ ├── bin │ │ │ ├── cyggcc_s-seh-1.dll │ │ │ ├── cygiconv-2.dll │ │ │ ├── cygintl-8.dll │ │ │ ├── cygncursesw-10.dll │ │ │ ├── cygreadline7.dll │ │ │ ├── cygstdc++-6.dll │ │ │ ├── cygwin1.dll │ │ │ ├── libiconv2.dll │ │ │ ├── libintl3.dll │ │ │ ├── make.exe │ │ │ └── sh.exe │ │ └── tmp │ │ │ └── dummy_file.txt │ ├── motor_profile.ods │ └── openocd-v0.10.0-scripts │ │ ├── stlink-v2.cfg │ │ ├── stm8s003.cfg │ │ └── stm8s105.cfg ├── uart.c ├── uart.h ├── utils.c ├── utils.h ├── watchdog.c ├── watchdog.h ├── wheel_speed_sensor.c └── wheel_speed_sensor.h ├── firmware_configuration_tool.jar └── lib └── swing-layout-1.0.4.jar /.cproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | firmware/*.asm 2 | firmware/*.bin 3 | firmware/*.elf 4 | firmware/*.ihx 5 | firmware/*.cdb 6 | firmware/*.lk 7 | firmware/*.map 8 | firmware/*.adb 9 | firmware/*.lst 10 | firmware/*.rel 11 | firmware/*.rst 12 | firmware/*.sym 13 | firmware/StdPeriphLib/src/*.asm 14 | firmware/StdPeriphLib/src/*.bin 15 | firmware/StdPeriphLib/src/*.elf 16 | firmware/StdPeriphLib/src/*.ihx 17 | firmware/StdPeriphLib/src/*.cdb 18 | firmware/StdPeriphLib/src/*.lk 19 | firmware/StdPeriphLib/src/*.map 20 | firmware/StdPeriphLib/src/*.adb 21 | firmware/StdPeriphLib/src/*.lst 22 | firmware/StdPeriphLib/src/*.rel 23 | firmware/StdPeriphLib/src/*.rst 24 | firmware/StdPeriphLib/src/*.sym 25 | firmware/Debug 26 | StdPeriphLib/src/ 27 | configuration_tool/configuration_tool-sources/build 28 | configuration_tool/configuration_tool-sources/dist 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | BMSBattery_S_controllers_firmware 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.ui.externaltools.ExternalToolBuilder 10 | full,incremental, 11 | 12 | 13 | LaunchConfigHandle 14 | <project>/.externalToolBuilders/org.eclipse.cdt.managedbuilder.core.genmakebuilder.launch 15 | 16 | 17 | 18 | 19 | org.eclipse.ui.externaltools.ExternalToolBuilder 20 | full,incremental, 21 | 22 | 23 | LaunchConfigHandle 24 | <project>/.externalToolBuilders/org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder.launch 25 | 26 | 27 | 28 | 29 | org.eclipse.ui.externaltools.ExternalToolBuilder 30 | full,incremental, 31 | 32 | 33 | LaunchConfigHandle 34 | <project>/.externalToolBuilders/SCDD.launch 35 | 36 | 37 | 38 | 39 | 40 | org.eclipse.cdt.core.cnature 41 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 42 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 43 | 44 | 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | THIS CODE IS NOT BEING DEVELOPED NOR MANTAINED ANYMORE. 2 | 3 | Please use instead the Stancecoke firmware: 4 | - https://opensourceebikefirmware.bitbucket.io/windows_instructions 5 | - https://github.com/stancecoke/BMSBattery_S_controllers_firmware 6 | 7 | Casainho was developing and mantaining this code up to June 2018 but stopped because he started using on his ebikes the alternative TSDZ2 mide drive motor that uses another motor controller. Not having the hardware, it would be impossible to keep developing and testing the firmware. 8 | 9 | TSDZ2 mid drive motor controller uses the same microcontroller STM8 as KT motor controllers and so Casainho started to use the same base code developed for KT motor controllers firmware, on the TSDZ2 motor controllers -- see here: https://github.com/OpenSource-EBike-firmware/TongSheng_TSDZ2_motor_controller_firmware 10 | 11 | Original documentation, outdated: 12 | - user documentation: https://opensourceebikefirmware.bitbucket.io 13 | - development documentation: https://opensourceebikefirmware.bitbucket.io/development 14 | -------------------------------------------------------------------------------- /configuration_tool/configuration_tool-sources/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Builds, tests, and runs the project Flexible_OpenSource_EBike_motor_controller_firmware-Configuration_tool. 12 | 13 | 73 | 74 | -------------------------------------------------------------------------------- /configuration_tool/configuration_tool-sources/manifest.mf: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | X-COMMENT: Main-Class will be added automatically by build 3 | 4 | -------------------------------------------------------------------------------- /configuration_tool/configuration_tool-sources/nbproject/genfiles.properties: -------------------------------------------------------------------------------- 1 | build.xml.data.CRC32=1be2725f 2 | build.xml.script.CRC32=bcc43c73 3 | build.xml.stylesheet.CRC32=8064a381@1.79.1.48 4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. 5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. 6 | nbproject/build-impl.xml.data.CRC32=1be2725f 7 | nbproject/build-impl.xml.script.CRC32=0c4159a1 8 | nbproject/build-impl.xml.stylesheet.CRC32=05530350@1.79.1.48 9 | -------------------------------------------------------------------------------- /configuration_tool/configuration_tool-sources/nbproject/private/config.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/configuration_tool/configuration_tool-sources/nbproject/private/config.properties -------------------------------------------------------------------------------- /configuration_tool/configuration_tool-sources/nbproject/private/private.properties: -------------------------------------------------------------------------------- 1 | do.depend=false 2 | do.jar=true 3 | javac.debug=true 4 | #Mon Dec 25 12:17:40 WET 2017 5 | javadoc.preview=true 6 | compile.on.save=true 7 | user.properties.file=/home/cas/.netbeans/8.1/build.properties 8 | -------------------------------------------------------------------------------- /configuration_tool/configuration_tool-sources/nbproject/private/private.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | src/Flexible_OpenSource_EBike_firmware_configuration_tool/Configuration_tool.java 6 | 7 | 8 | 965 9 | 10 | 11 | 12 | 13 | 14 | 15 | file:/home/cas/OpenSource-EBike-firmware/BMSBattery_S_controllers_firmware/configuration_tool/configuration_tool-sources/src/Flexible_OpenSource_EBike_firmware_configuration_tool/Configuration_tool.java 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /configuration_tool/configuration_tool-sources/nbproject/project.properties: -------------------------------------------------------------------------------- 1 | annotation.processing.enabled=true 2 | annotation.processing.enabled.in.editor=false 3 | annotation.processing.processors.list= 4 | annotation.processing.run.all.processors=true 5 | application.title=Flexible_OpenSource_EBike_motor_controller_firmware-Configuration_tool 6 | application.vendor=Sun Microsystems Inc 7 | build.classes.dir=${build.dir}/classes 8 | build.classes.excludes=**/*.java,**/*.form 9 | # This directory is removed when the project is cleaned: 10 | build.dir=build 11 | build.generated.dir=${build.dir}/generated 12 | build.generated.sources.dir=${build.dir}/generated-sources 13 | # Only compile against the classpath explicitly listed here: 14 | build.sysclasspath=ignore 15 | build.test.classes.dir=${build.dir}/test/classes 16 | build.test.results.dir=${build.dir}/test/results 17 | debug.classpath=\ 18 | ${run.classpath} 19 | debug.test.classpath=\ 20 | ${run.test.classpath} 21 | # This directory is removed when the project is cleaned: 22 | dist.dir=dist 23 | dist.jar=${dist.dir}/Flexible_OpenSource_EBike_motor_controller_firmware-Configuration_tool.jar 24 | dist.javadoc.dir=${dist.dir}/javadoc 25 | endorsed.classpath= 26 | excludes= 27 | includes=** 28 | jar.compress=false 29 | javac.classpath=\ 30 | ${libs.swing-layout.classpath} 31 | # Space-separated list of extra javac options 32 | javac.compilerargs= 33 | javac.deprecation=false 34 | javac.external.vm=false 35 | javac.processorpath=\ 36 | ${javac.classpath} 37 | javac.source=1.8 38 | javac.target=1.8 39 | javac.test.classpath=\ 40 | ${javac.classpath}:\ 41 | ${build.classes.dir} 42 | javadoc.additionalparam= 43 | javadoc.author=false 44 | javadoc.encoding=${source.encoding} 45 | javadoc.noindex=false 46 | javadoc.nonavbar=false 47 | javadoc.notree=false 48 | javadoc.private=false 49 | javadoc.splitindex=true 50 | javadoc.use=true 51 | javadoc.version=false 52 | javadoc.windowtitle= 53 | main.class=Flexible_OpenSource_EBike_firmware_configuration_tool.Configuration_tool 54 | manifest.file=manifest.mf 55 | meta.inf.dir=${src.dir}/META-INF 56 | mkdist.disabled=false 57 | platform.active=default_platform 58 | run.classpath=\ 59 | ${javac.classpath}:\ 60 | ${build.classes.dir} 61 | # Space-separated list of JVM arguments used when running the project 62 | # (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value 63 | # or test-sys-prop.name=value to set system properties for unit tests): 64 | run.jvmargs= 65 | run.test.classpath=\ 66 | ${javac.test.classpath}:\ 67 | ${build.test.classes.dir} 68 | source.encoding=UTF-8 69 | src.dir=src 70 | test.src.dir=test 71 | -------------------------------------------------------------------------------- /configuration_tool/configuration_tool-sources/nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.java.j2seproject 4 | 5 | 6 | Flexible_OpenSource_EBike_motor_controller_firmware-Configuration_tool 7 | 1.6.5 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /configuration_tool/linux_scripts/build_and_flash.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | make -C './firmware' -f Makefile_linux clean 3 | make -C './firmware' -f Makefile_linux 4 | gksu 'stm8flash -c stlinkv2 -p stm8s105?6 -w ./firmware/main.bin' 5 | 6 | -------------------------------------------------------------------------------- /configuration_tool/linux_scripts/option_bytes_pwm_n_channels_enabled.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/configuration_tool/linux_scripts/option_bytes_pwm_n_channels_enabled.bin -------------------------------------------------------------------------------- /configuration_tool/linux_scripts/write_option_bytes.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gksu 'stm8flash -c stlinkv2 -p stm8s105?6 -s opt -w configuration_tool/linux_scripts/option_bytes_pwm_n_channels_enabled.bin' 3 | -------------------------------------------------------------------------------- /configuration_tool/windows_scripts/Start_Compiling.bat: -------------------------------------------------------------------------------- 1 | PATH = %PATH%;C:\Program Files (x86)\STMicroelectronics\st_toolset\stvp 2 | 3 | cd firmware 4 | 5 | ..\configuration_tool\windows_scripts\make -f Makefile_windows clean 6 | ..\configuration_tool\windows_scripts\make -f Makefile_windows 7 | 8 | STVP_CmdLine -BoardName=ST-LINK -ProgMode=SWIM -Port=USB -Device=STM8S105x6 -FileProg=main.ihx -verbose -no_loop 9 | 10 | pause 11 | exit 12 | -------------------------------------------------------------------------------- /configuration_tool/windows_scripts/WriteOptionBytes.bat: -------------------------------------------------------------------------------- 1 | PATH = %PATH%;C:\Program Files (x86)\STMicroelectronics\st_toolset\stvp 2 | 3 | cd configuration_tool\windows_scripts 4 | 5 | STVP_CmdLine -BoardName=ST-LINK -ProgMode=SWIM -Port=USB -Device=STM8S105x6 -FileOption=optionbytes.hex -verbose -no_loop 6 | 7 | exit 8 | -------------------------------------------------------------------------------- /configuration_tool/windows_scripts/clean.bat: -------------------------------------------------------------------------------- 1 | cd stdperiphlib\src 2 | del *.asm 3 | del *.rel 4 | del *.lk 5 | del *.lst 6 | del *.rst 7 | del *.sym 8 | del *.cdb 9 | del *.map 10 | del *.elf 11 | del *.bin 12 | cd.. 13 | cd.. -------------------------------------------------------------------------------- /configuration_tool/windows_scripts/libiconv2.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/configuration_tool/windows_scripts/libiconv2.dll -------------------------------------------------------------------------------- /configuration_tool/windows_scripts/libintl3.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/configuration_tool/windows_scripts/libintl3.dll -------------------------------------------------------------------------------- /configuration_tool/windows_scripts/make.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/configuration_tool/windows_scripts/make.exe -------------------------------------------------------------------------------- /configuration_tool/windows_scripts/optionbytes.hex: -------------------------------------------------------------------------------- 1 | :0148000000B7 2 | :0148010000B6 3 | :014803002094 4 | :0148050000B2 5 | :0148070000B0 6 | :0148090000AE 7 | :01480B0000AC 8 | :01480D0000AA 9 | :01487E000039 10 | :00000001FF 11 | -------------------------------------------------------------------------------- /firmware/.cproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | make 36 | 37 | STM8 38 | true 39 | false 40 | true 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | make 66 | 67 | all 68 | true 69 | false 70 | true 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /firmware/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | BMSBattery_S_controllers_firmware 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.ui.externaltools.ExternalToolBuilder 10 | full,incremental, 11 | 12 | 13 | LaunchConfigHandle 14 | <project>/.externalToolBuilders/org.eclipse.cdt.managedbuilder.core.genmakebuilder.launch 15 | 16 | 17 | 18 | 19 | org.eclipse.ui.externaltools.ExternalToolBuilder 20 | full,incremental, 21 | 22 | 23 | LaunchConfigHandle 24 | <project>/.externalToolBuilders/org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder.launch 25 | 26 | 27 | 28 | 29 | org.eclipse.ui.externaltools.ExternalToolBuilder 30 | full,incremental, 31 | 32 | 33 | LaunchConfigHandle 34 | <project>/.externalToolBuilders/SCDD.launch 35 | 36 | 37 | 38 | 39 | 40 | org.eclipse.cdt.core.cnature 41 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 42 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 43 | 44 | 45 | -------------------------------------------------------------------------------- /firmware/.settings/SDCC Batch.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /firmware/.settings/language.settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /firmware/.settings/org.eclipse.cdt.managedbuilder.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.2042742946/CPATH/delimiter=\: 3 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.2042742946/CPATH/operation=remove 4 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.2042742946/C_INCLUDE_PATH/delimiter=\: 5 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.2042742946/C_INCLUDE_PATH/operation=remove 6 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.2042742946/append=true 7 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.2042742946/appendContributed=true 8 | environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.cross.exe.debug.2042742946/LIBRARY_PATH/delimiter=\: 9 | environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.cross.exe.debug.2042742946/LIBRARY_PATH/operation=remove 10 | environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.cross.exe.debug.2042742946/append=true 11 | environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.cross.exe.debug.2042742946/appendContributed=true 12 | -------------------------------------------------------------------------------- /firmware/.settings/org.eclipse.cdt.ui.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | formatter_profile=org.eclipse.cdt.ui.default.gnu_profile 3 | formatter_settings_version=1 4 | -------------------------------------------------------------------------------- /firmware/Flash_Debug-STM8.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /firmware/Makefile_linux: -------------------------------------------------------------------------------- 1 | #Makefile for STM8 Examples with SDCC compiler 2 | #Author: Saeid Yazdani 3 | #Website: WWW.EMBEDONIX.COM 4 | #Copyright 2016 5 | #LICENSE: GNU-LGPL 6 | 7 | .PHONY: all clean 8 | 9 | #Compiler 10 | CC = sdcc 11 | OBJCOPY = stm8-objcopy 12 | SIZE = stm8-size 13 | 14 | #Platform 15 | PLATFORM = stm8 16 | 17 | #Product name 18 | PNAME = main 19 | 20 | #Directory for helpers 21 | IDIR = StdPeriphLib/inc 22 | SDIR = StdPeriphLib/src 23 | 24 | # In case you ever want a different name for the main source file 25 | MAINSRC = $(PNAME).c 26 | 27 | ELF_SECTIONS_TO_REMOVE = -R DATA -R INITIALIZED -R SSEG -R .debug_line -R .debug_loc -R .debug_abbrev -R .debug_info -R .debug_pubnames -R .debug_frame 28 | 29 | # These are the sources that must be compiled to .rel files: 30 | EXTRASRCS = \ 31 | $(SDIR)/stm8s_itc.c \ 32 | $(SDIR)/stm8s_clk.c \ 33 | $(SDIR)/stm8s_iwdg.c \ 34 | $(SDIR)/stm8s_gpio.c \ 35 | $(SDIR)/stm8s_exti.c \ 36 | $(SDIR)/stm8s_uart2.c \ 37 | $(SDIR)/stm8s_tim1.c \ 38 | $(SDIR)/stm8s_tim2.c \ 39 | $(SDIR)/stm8s_adc1.c \ 40 | $(SDIR)/stm8s_flash.c \ 41 | watchdog.c \ 42 | gpio.c \ 43 | utils.c \ 44 | uart.c \ 45 | adc.c \ 46 | brake.c \ 47 | pas.c \ 48 | wheel_speed_sensor.c \ 49 | timers.c \ 50 | pwm.c \ 51 | eeprom.c \ 52 | motor.c \ 53 | ebike_app.c \ 54 | 55 | HEADERS = watchdog.h adc.h brake.h gpio.h interrupts.h main.h config.h pwm.h timers.h uart.h utils.h motor.h ebike_app.h eeprom.h pas.h wheel_speed_sensor.h 56 | 57 | # The list of .rel files can be derived from the list of their source files 58 | RELS = $(EXTRASRCS:.c=.rel) 59 | 60 | INCLUDES = -I$(IDIR) -I. 61 | CFLAGS = -m$(PLATFORM) -Ddouble=float --std-c99 --nolospre 62 | ELF_FLAGS = --out-fmt-elf --debug 63 | LIBS = 64 | 65 | # This just provides the conventional target name "all"; it is optional 66 | # Note: I assume you set PNAME via some means not exhibited in your original file 67 | all: $(PNAME) 68 | 69 | # How to build the overall program 70 | $(PNAME): $(MAINSRC) $(RELS) 71 | $(CC) $(INCLUDES) $(CFLAGS) $(ELF_FLAGS) $(LIBS) $(MAINSRC) $(RELS) 72 | $(SIZE) $(PNAME).elf -A 73 | $(OBJCOPY) -O binary $(ELF_SECTIONS_TO_REMOVE) $(PNAME).elf $(PNAME).bin 74 | # $(OBJCOPY) -O ihex $(ELF_SECTIONS_TO_REMOVE) $(PNAME).elf $(PNAME).hex 75 | 76 | # How to build any .rel file from its corresponding .c file 77 | # GNU would have you use a pattern rule for this, but that's GNU-specific 78 | %.rel: %.c $(HEADERS) 79 | $(CC) -c $(INCLUDES) $(CFLAGS) $(ELF_FLAGS) $(LIBS) -o$< $< 80 | 81 | # Suffixes appearing in suffix rules we care about. 82 | # Necessary because .rel is not one of the standard suffixes. 83 | .SUFFIXES: .c .rel 84 | 85 | hex: 86 | $(OBJCOPY) -O ihex $(ELF_SECTIONS_TO_REMOVE) $(PNAME).elf $(PNAME).ihx 87 | 88 | flash: 89 | stm8flash -cstlinkv2 -pstm8s105?6 -w$(PNAME).ihx 90 | 91 | clean: 92 | @echo "Cleaning files..." 93 | @rm -rf $(SDIR)/*.asm 94 | @rm -rf $(SDIR)/*.rel 95 | @rm -rf $(SDIR)/*.lk 96 | @rm -rf $(SDIR)/*.lst 97 | @rm -rf $(SDIR)/*.rst 98 | @rm -rf $(SDIR)/*.sym 99 | @rm -rf $(SDIR)/*.cdb 100 | @rm -rf $(SDIR)/*.map 101 | @rm -rf $(SDIR)/*.elf 102 | @rm -rf $(SDIR)/*.bin 103 | @rm -rf $(SDIR)/*.adb 104 | @rm -rf *.asm 105 | @rm -rf *.rel 106 | @rm -rf *.lk 107 | @rm -rf *.lst 108 | @rm -rf *.rst 109 | @rm -rf *.sym 110 | @rm -rf *.cdb 111 | @rm -rf *.map 112 | @rm -rf *.adb 113 | @rm -rf *.elf 114 | @rm -rf main.bin 115 | @rm -rf *.ihx 116 | @echo "Done." 117 | 118 | -------------------------------------------------------------------------------- /firmware/Makefile_windows: -------------------------------------------------------------------------------- 1 | #Makefile for STM8 Examples with SDCC compiler 2 | #Author: Saeid Yazdani 3 | #Website: WWW.EMBEDONIX.COM 4 | #Copyright 2016 5 | #LICENSE: GNU-LGPL 6 | 7 | .PHONY: all clean 8 | 9 | #Compiler 10 | CC = sdcc 11 | OBJCOPY = stm8-objcopy 12 | SIZE = stm8-size 13 | 14 | #Platform 15 | PLATFORM = stm8 16 | 17 | #Product name 18 | PNAME = main 19 | 20 | #Directory for helpers 21 | IDIR = StdPeriphLib/inc 22 | SDIR = StdPeriphLib/src 23 | 24 | # In case you ever want a different name for the main source file 25 | MAINSRC = $(PNAME).c 26 | 27 | ELF_SECTIONS_TO_REMOVE = -R DATA -R INITIALIZED -R SSEG -R .debug_line -R .debug_loc -R .debug_abbrev -R .debug_info -R .debug_pubnames -R .debug_frame 28 | 29 | # These are the sources that must be compiled to .rel files: 30 | EXTRASRCS = \ 31 | $(SDIR)/stm8s_itc.c \ 32 | $(SDIR)/stm8s_clk.c \ 33 | $(SDIR)/stm8s_iwdg.c \ 34 | $(SDIR)/stm8s_gpio.c \ 35 | $(SDIR)/stm8s_exti.c \ 36 | $(SDIR)/stm8s_uart2.c \ 37 | $(SDIR)/stm8s_tim1.c \ 38 | $(SDIR)/stm8s_tim2.c \ 39 | $(SDIR)/stm8s_adc1.c \ 40 | $(SDIR)/stm8s_flash.c \ 41 | watchdog.c \ 42 | gpio.c \ 43 | utils.c \ 44 | uart.c \ 45 | adc.c \ 46 | brake.c \ 47 | pas.c \ 48 | wheel_speed_sensor.c \ 49 | timers.c \ 50 | pwm.c \ 51 | eeprom.c \ 52 | motor.c \ 53 | ebike_app.c \ 54 | 55 | HEADERS = watchdog.h adc.h brake.h gpio.h interrupts.h main.h config.h pwm.h timers.h uart.h utils.h motor.h ebike_app.h eeprom.h pas.h wheel_speed_sensor.h 56 | 57 | # The list of .rel files can be derived from the list of their source files 58 | RELS = $(EXTRASRCS:.c=.rel) 59 | 60 | INCLUDES = -I$(IDIR) -I. 61 | CFLAGS = -m$(PLATFORM) -Ddouble=float --std-c99 --nolospre 62 | ELF_FLAGS = --out-fmt-ihx --debug 63 | LIBS = 64 | 65 | # This just provides the conventional target name "all"; it is optional 66 | # Note: I assume you set PNAME via some means not exhibited in your original file 67 | all: $(PNAME) 68 | 69 | # How to build the overall program 70 | $(PNAME): $(MAINSRC) $(RELS) 71 | $(CC) $(INCLUDES) $(CFLAGS) $(ELF_FLAGS) $(LIBS) $(MAINSRC) $(RELS) 72 | # $(SIZE) $(PNAME).elf 73 | # $(OBJCOPY) -O binary $(ELF_SECTIONS_TO_REMOVE) $(PNAME).elf $(PNAME).bin 74 | # $(OBJCOPY) -O ihex $(ELF_SECTIONS_TO_REMOVE) $(PNAME).elf $(PNAME).hex 75 | 76 | # How to build any .rel file from its corresponding .c file 77 | # GNU would have you use a pattern rule for this, but that's GNU-specific 78 | %.rel: %.c $(HEADERS) 79 | $(CC) -c $(INCLUDES) $(CFLAGS) $(ELF_FLAGS) $(LIBS) -o$< $< 80 | 81 | # Suffixes appearing in suffix rules we care about. 82 | # Necessary because .rel is not one of the standard suffixes. 83 | .SUFFIXES: .c .rel 84 | 85 | hex: 86 | $(OBJCOPY) -O ihex $(ELF_SECTIONS_TO_REMOVE) $(PNAME).elf $(PNAME).ihx 87 | 88 | # flash: 89 | # stm8flash -cstlinkv2 -pstm8s105?6 -w$(PNAME).ihx 90 | 91 | ENTF = cmd /C del 92 | 93 | clean: 94 | @echo "Cleaning files..." 95 | @clean.bat 96 | @$(ENTF) *.asm 97 | @$(ENTF) *.rel 98 | @$(ENTF) *.lk 99 | @$(ENTF) *.lst 100 | @$(ENTF) *.rst 101 | @$(ENTF) *.sym 102 | @$(ENTF) *.cdb 103 | @$(ENTF) *.map 104 | @$(ENTF) *.elf 105 | @$(ENTF) *.adb 106 | @echo "Done." 107 | 108 | -------------------------------------------------------------------------------- /firmware/OSEC Parameter Configurator.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/OSEC Parameter Configurator.jar -------------------------------------------------------------------------------- /firmware/OpenOCD-v0.10.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /firmware/Start_Compiling.bat: -------------------------------------------------------------------------------- 1 | PATH = %PATH%;C:\Program Files (x86)\STMicroelectronics\st_toolset\stvp;C:\SDCC\usr\local\bin;%~p0\tools\cygwin\bin 2 | 3 | del main.ihx 4 | make -f Makefile_windows clean 5 | make -f Makefile_windows 6 | 7 | STVP_CmdLine -BoardName=ST-LINK -ProgMode=SWIM -Port=USB -Device=STM8S105x6 -FileProg=main.ihx -verbose -no_loop 8 | 9 | 10 | exit 11 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/README.md: -------------------------------------------------------------------------------- 1 | STM8S Std. Peripheral Library for SDCC. 2 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/inc/stm8s_awu.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm8s_awu.h 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all functions prototype and macros for the AWU peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Define to prevent recursive inclusion -------------------------------------*/ 29 | #ifndef __STM8S_AWU_H 30 | #define __STM8S_AWU_H 31 | 32 | /* Includes ------------------------------------------------------------------*/ 33 | #include "stm8s.h" 34 | 35 | /* Exported types ------------------------------------------------------------*/ 36 | 37 | /** @addtogroup AWU_Exported_Types 38 | * @{ 39 | */ 40 | 41 | /** 42 | * @brief AWU TimeBase selection 43 | */ 44 | 45 | typedef enum 46 | { 47 | AWU_TIMEBASE_NO_IT = (uint8_t)0, /*!< No AWU interrupt selected */ 48 | AWU_TIMEBASE_250US = (uint8_t)1, /*!< AWU Timebase equals 0.25 ms */ 49 | AWU_TIMEBASE_500US = (uint8_t)2, /*!< AWU Timebase equals 0.5 ms */ 50 | AWU_TIMEBASE_1MS = (uint8_t)3, /*!< AWU Timebase equals 1 ms */ 51 | AWU_TIMEBASE_2MS = (uint8_t)4, /*!< AWU Timebase equals 2 ms */ 52 | AWU_TIMEBASE_4MS = (uint8_t)5, /*!< AWU Timebase equals 4 ms */ 53 | AWU_TIMEBASE_8MS = (uint8_t)6, /*!< AWU Timebase equals 8 ms */ 54 | AWU_TIMEBASE_16MS = (uint8_t)7, /*!< AWU Timebase equals 16 ms */ 55 | AWU_TIMEBASE_32MS = (uint8_t)8, /*!< AWU Timebase equals 32 ms */ 56 | AWU_TIMEBASE_64MS = (uint8_t)9, /*!< AWU Timebase equals 64 ms */ 57 | AWU_TIMEBASE_128MS = (uint8_t)10, /*!< AWU Timebase equals 128 ms */ 58 | AWU_TIMEBASE_256MS = (uint8_t)11, /*!< AWU Timebase equals 256 ms */ 59 | AWU_TIMEBASE_512MS = (uint8_t)12, /*!< AWU Timebase equals 512 ms */ 60 | AWU_TIMEBASE_1S = (uint8_t)13, /*!< AWU Timebase equals 1 s */ 61 | AWU_TIMEBASE_2S = (uint8_t)14, /*!< AWU Timebase equals 2 s */ 62 | AWU_TIMEBASE_12S = (uint8_t)15, /*!< AWU Timebase equals 12 s */ 63 | AWU_TIMEBASE_30S = (uint8_t)16 /*!< AWU Timebase equals 30 s */ 64 | } AWU_Timebase_TypeDef; 65 | 66 | /** 67 | * @} 68 | */ 69 | 70 | /* Exported constants --------------------------------------------------------*/ 71 | 72 | /** @addtogroup AWU_Exported_Constants 73 | * @{ 74 | */ 75 | 76 | #define LSI_FREQUENCY_MIN ((uint32_t)110000) /*!< LSI minimum value in Hertz */ 77 | #define LSI_FREQUENCY_MAX ((uint32_t)150000) /*!< LSI maximum value in Hertz */ 78 | 79 | /** 80 | * @} 81 | */ 82 | 83 | /* Exported macros ------------------------------------------------------------*/ 84 | 85 | /* Private macros ------------------------------------------------------------*/ 86 | 87 | /** @addtogroup AWU_Private_Macros 88 | * @{ 89 | */ 90 | 91 | /** 92 | * @brief Macro used by the assert function to check the different functions parameters. 93 | */ 94 | 95 | /** 96 | * @brief Macro used by the assert function to check the AWU timebases 97 | */ 98 | #define IS_AWU_TIMEBASE_OK(TB) \ 99 | (((TB) == AWU_TIMEBASE_NO_IT) || \ 100 | ((TB) == AWU_TIMEBASE_250US) || \ 101 | ((TB) == AWU_TIMEBASE_500US) || \ 102 | ((TB) == AWU_TIMEBASE_1MS) || \ 103 | ((TB) == AWU_TIMEBASE_2MS) || \ 104 | ((TB) == AWU_TIMEBASE_4MS) || \ 105 | ((TB) == AWU_TIMEBASE_8MS) || \ 106 | ((TB) == AWU_TIMEBASE_16MS) || \ 107 | ((TB) == AWU_TIMEBASE_32MS) || \ 108 | ((TB) == AWU_TIMEBASE_64MS) || \ 109 | ((TB) == AWU_TIMEBASE_128MS) || \ 110 | ((TB) == AWU_TIMEBASE_256MS) || \ 111 | ((TB) == AWU_TIMEBASE_512MS) || \ 112 | ((TB) == AWU_TIMEBASE_1S) || \ 113 | ((TB) == AWU_TIMEBASE_2S) || \ 114 | ((TB) == AWU_TIMEBASE_12S) || \ 115 | ((TB) == AWU_TIMEBASE_30S)) 116 | 117 | /** 118 | * @brief Macro used by the assert function to check the LSI frequency (in Hz) 119 | */ 120 | #define IS_LSI_FREQUENCY_OK(FREQ) \ 121 | (((FREQ) >= LSI_FREQUENCY_MIN) && \ 122 | ((FREQ) <= LSI_FREQUENCY_MAX)) 123 | 124 | /** 125 | * @} 126 | */ 127 | 128 | /* Exported functions ------------------------------------------------------- */ 129 | 130 | /** @addtogroup AWU_Exported_Functions 131 | * @{ 132 | */ 133 | void AWU_DeInit(void); 134 | void AWU_Init(AWU_Timebase_TypeDef AWU_TimeBase); 135 | void AWU_Cmd(FunctionalState NewState); 136 | void AWU_LSICalibrationConfig(uint32_t LSIFreqHz); 137 | void AWU_IdleModeEnable(void); 138 | FlagStatus AWU_GetFlagStatus(void); 139 | 140 | /** 141 | * @} 142 | */ 143 | 144 | #endif /* __STM8S_AWU_H */ 145 | 146 | 147 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 148 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/inc/stm8s_beep.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm8s_beep.h 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all functions prototype and macros for the BEEP peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __STM8S_BEEP_H 31 | #define __STM8S_BEEP_H 32 | 33 | /* Includes ------------------------------------------------------------------*/ 34 | #include "stm8s.h" 35 | 36 | /* Exported types ------------------------------------------------------------*/ 37 | 38 | /** @addtogroup BEEP_Exported_Types 39 | * @{ 40 | */ 41 | 42 | /** 43 | * @brief BEEP Frequency selection 44 | */ 45 | typedef enum { 46 | BEEP_FREQUENCY_1KHZ = (uint8_t)0x00, /*!< Beep signal output frequency equals to 1 KHz */ 47 | BEEP_FREQUENCY_2KHZ = (uint8_t)0x40, /*!< Beep signal output frequency equals to 2 KHz */ 48 | BEEP_FREQUENCY_4KHZ = (uint8_t)0x80 /*!< Beep signal output frequency equals to 4 KHz */ 49 | } BEEP_Frequency_TypeDef; 50 | 51 | /** 52 | * @} 53 | */ 54 | 55 | /* Exported constants --------------------------------------------------------*/ 56 | 57 | /** @addtogroup BEEP_Exported_Constants 58 | * @{ 59 | */ 60 | 61 | #define BEEP_CALIBRATION_DEFAULT ((uint8_t)0x0B) /*!< Default value when calibration is not done */ 62 | 63 | #define LSI_FREQUENCY_MIN ((uint32_t)110000) /*!< LSI minimum value in Hertz */ 64 | #define LSI_FREQUENCY_MAX ((uint32_t)150000) /*!< LSI maximum value in Hertz */ 65 | 66 | /** 67 | * @} 68 | */ 69 | 70 | /* Exported macros -----------------------------------------------------------*/ 71 | /* Private macros ------------------------------------------------------------*/ 72 | 73 | /** @addtogroup BEEP_Private_Macros 74 | * @{ 75 | */ 76 | 77 | /** 78 | * @brief Macro used by the assert function to check the different functions parameters. 79 | */ 80 | 81 | /** 82 | * @brief Macro used by the assert function to check the BEEP frequencies. 83 | */ 84 | #define IS_BEEP_FREQUENCY_OK(FREQ) \ 85 | (((FREQ) == BEEP_FREQUENCY_1KHZ) || \ 86 | ((FREQ) == BEEP_FREQUENCY_2KHZ) || \ 87 | ((FREQ) == BEEP_FREQUENCY_4KHZ)) 88 | 89 | /** 90 | * @brief Macro used by the assert function to check the LSI frequency (in Hz). 91 | */ 92 | #define IS_LSI_FREQUENCY_OK(FREQ) \ 93 | (((FREQ) >= LSI_FREQUENCY_MIN) && \ 94 | ((FREQ) <= LSI_FREQUENCY_MAX)) 95 | 96 | /** 97 | * @} 98 | */ 99 | 100 | /* Exported functions ------------------------------------------------------- */ 101 | 102 | /** @addtogroup BEEP_Exported_Functions 103 | * @{ 104 | */ 105 | 106 | void BEEP_DeInit(void); 107 | void BEEP_Init(BEEP_Frequency_TypeDef BEEP_Frequency); 108 | void BEEP_Cmd(FunctionalState NewState); 109 | void BEEP_LSICalibrationConfig(uint32_t LSIFreqHz); 110 | 111 | 112 | /** 113 | * @} 114 | */ 115 | 116 | #endif /* __STM8S_BEEP_H */ 117 | 118 | 119 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 120 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/inc/stm8s_conf.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm8s_conf.h 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file is used to configure the Library. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Define to prevent recursive inclusion -------------------------------------*/ 29 | #ifndef __STM8S_CONF_H 30 | #define __STM8S_CONF_H 31 | 32 | /* Includes ------------------------------------------------------------------*/ 33 | #include "stm8s.h" 34 | 35 | /* Uncomment the line below to enable peripheral header file inclusion */ 36 | #if defined(STM8S105) || defined(STM8S005) || defined(STM8S103) || defined(STM8S003) ||\ 37 | defined(STM8S903) || defined (STM8AF626x) || defined (STM8AF622x) 38 | #include "stm8s_adc1.h" 39 | #endif /* (STM8S105) ||(STM8S103) || (STM8S903) || (STM8AF626x) || (STM8AF622x) */ 40 | #if defined(STM8S208) || defined(STM8S207) || defined(STM8S007) || defined (STM8AF52Ax) ||\ 41 | defined (STM8AF62Ax) 42 | #include "stm8s_adc2.h" 43 | #endif /* (STM8S208) || (STM8S207) || (STM8AF62Ax) || (STM8AF52Ax) */ 44 | #include "stm8s_awu.h" 45 | #include "stm8s_beep.h" 46 | #if defined (STM8S208) || defined (STM8AF52Ax) 47 | #include "stm8s_can.h" 48 | #endif /* (STM8S208) || (STM8AF52Ax) */ 49 | #include "stm8s_clk.h" 50 | #include "stm8s_exti.h" 51 | #include "stm8s_flash.h" 52 | #include "stm8s_gpio.h" 53 | #include "stm8s_i2c.h" 54 | #include "stm8s_itc.h" 55 | #include "stm8s_iwdg.h" 56 | #include "stm8s_rst.h" 57 | #include "stm8s_spi.h" 58 | #include "stm8s_tim1.h" 59 | #if !defined(STM8S903) || !defined(STM8AF622x) 60 | #include "stm8s_tim2.h" 61 | #endif /* (STM8S903) || (STM8AF622x) */ 62 | #if defined(STM8S208) || defined(STM8S207) || defined(STM8S007) ||defined(STM8S105) ||\ 63 | defined(STM8S005) || defined (STM8AF52Ax) || defined (STM8AF62Ax) || defined (STM8AF626x) 64 | #include "stm8s_tim3.h" 65 | #endif /* (STM8S208) ||defined(STM8S207) || defined(STM8S007) ||defined(STM8S105) */ 66 | #if !defined(STM8S903) || !defined(STM8AF622x) 67 | #include "stm8s_tim4.h" 68 | #endif /* (STM8S903) || (STM8AF622x) */ 69 | #if defined(STM8S903) || defined(STM8AF622x) 70 | #include "stm8s_tim5.h" 71 | #include "stm8s_tim6.h" 72 | #endif /* (STM8S903) || (STM8AF622x) */ 73 | #if defined(STM8S208) ||defined(STM8S207) || defined(STM8S007) ||defined(STM8S103) ||\ 74 | defined(STM8S003) || defined(STM8S903) || defined (STM8AF52Ax) || defined (STM8AF62Ax) 75 | #include "stm8s_uart1.h" 76 | #endif /* (STM8S208) || (STM8S207) || (STM8S103) || (STM8S903) || (STM8AF52Ax) || (STM8AF62Ax) */ 77 | #if defined(STM8S105) || defined(STM8S005) || defined (STM8AF626x) 78 | #include "stm8s_uart2.h" 79 | #endif /* (STM8S105) || (STM8AF626x) */ 80 | #if defined(STM8S208) ||defined(STM8S207) || defined(STM8S007) || defined (STM8AF52Ax) ||\ 81 | defined (STM8AF62Ax) 82 | #include "stm8s_uart3.h" 83 | #endif /* STM8S208 || STM8S207 || STM8AF52Ax || STM8AF62Ax */ 84 | #if defined(STM8AF622x) 85 | #include "stm8s_uart4.h" 86 | #endif /* (STM8AF622x) */ 87 | #include "stm8s_wwdg.h" 88 | 89 | /* Exported types ------------------------------------------------------------*/ 90 | /* Exported constants --------------------------------------------------------*/ 91 | /* Uncomment the line below to expanse the "assert_param" macro in the 92 | Standard Peripheral Library drivers code */ 93 | //#define USE_FULL_ASSERT (1) 94 | 95 | /* Exported macro ------------------------------------------------------------*/ 96 | #ifdef USE_FULL_ASSERT 97 | 98 | /** 99 | * @brief The assert_param macro is used for function's parameters check. 100 | * @param expr: If expr is false, it calls assert_failed function 101 | * which reports the name of the source file and the source 102 | * line number of the call that failed. 103 | * If expr is true, it returns no value. 104 | * @retval : None 105 | */ 106 | #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) 107 | /* Exported functions ------------------------------------------------------- */ 108 | void assert_failed(uint8_t* file, uint32_t line); 109 | #else 110 | #define assert_param(expr) ((void)0) 111 | #endif /* USE_FULL_ASSERT */ 112 | 113 | #endif /* __STM8S_CONF_H */ 114 | 115 | 116 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 117 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/inc/stm8s_exti.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm8s_exti.h 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all functions prototype and macros for the EXTI peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Define to prevent recursive inclusion -------------------------------------*/ 29 | #ifndef __STM8S_EXTI_H 30 | #define __STM8S_EXTI_H 31 | 32 | /* Includes ------------------------------------------------------------------*/ 33 | #include "stm8s.h" 34 | 35 | /* Exported types ------------------------------------------------------------*/ 36 | 37 | /** @addtogroup EXTI_Exported_Types 38 | * @{ 39 | */ 40 | 41 | /** 42 | * @brief EXTI Sensitivity values for PORTA to PORTE 43 | */ 44 | typedef enum { 45 | EXTI_SENSITIVITY_FALL_LOW = (uint8_t)0x00, /*!< Interrupt on Falling edge and Low level */ 46 | EXTI_SENSITIVITY_RISE_ONLY = (uint8_t)0x01, /*!< Interrupt on Rising edge only */ 47 | EXTI_SENSITIVITY_FALL_ONLY = (uint8_t)0x02, /*!< Interrupt on Falling edge only */ 48 | EXTI_SENSITIVITY_RISE_FALL = (uint8_t)0x03 /*!< Interrupt on Rising and Falling edges */ 49 | } EXTI_Sensitivity_TypeDef; 50 | 51 | /** 52 | * @brief EXTI Sensitivity values for TLI 53 | */ 54 | typedef enum { 55 | EXTI_TLISENSITIVITY_FALL_ONLY = (uint8_t)0x00, /*!< Top Level Interrupt on Falling edge only */ 56 | EXTI_TLISENSITIVITY_RISE_ONLY = (uint8_t)0x04 /*!< Top Level Interrupt on Rising edge only */ 57 | } EXTI_TLISensitivity_TypeDef; 58 | 59 | /** 60 | * @brief EXTI PortNum possible values 61 | */ 62 | typedef enum { 63 | EXTI_PORT_GPIOA = (uint8_t)0x00, /*!< GPIO Port A */ 64 | EXTI_PORT_GPIOB = (uint8_t)0x01, /*!< GPIO Port B */ 65 | EXTI_PORT_GPIOC = (uint8_t)0x02, /*!< GPIO Port C */ 66 | EXTI_PORT_GPIOD = (uint8_t)0x03, /*!< GPIO Port D */ 67 | EXTI_PORT_GPIOE = (uint8_t)0x04 /*!< GPIO Port E */ 68 | } EXTI_Port_TypeDef; 69 | 70 | /** 71 | * @} 72 | */ 73 | 74 | /* Private macros ------------------------------------------------------------*/ 75 | 76 | /** @addtogroup EXTI_Private_Macros 77 | * @{ 78 | */ 79 | 80 | /** 81 | * @brief Macro used by the assert function in order to check the different sensitivity values for PORTA to PORTE. 82 | */ 83 | #define IS_EXTI_SENSITIVITY_OK(SensitivityValue) \ 84 | (((SensitivityValue) == EXTI_SENSITIVITY_FALL_LOW) || \ 85 | ((SensitivityValue) == EXTI_SENSITIVITY_RISE_ONLY) || \ 86 | ((SensitivityValue) == EXTI_SENSITIVITY_FALL_ONLY) || \ 87 | ((SensitivityValue) == EXTI_SENSITIVITY_RISE_FALL)) 88 | 89 | /** 90 | * @brief Macro used by the assert function in order to check the different sensitivity values for TLI. 91 | */ 92 | #define IS_EXTI_TLISENSITIVITY_OK(SensitivityValue) \ 93 | (((SensitivityValue) == EXTI_TLISENSITIVITY_FALL_ONLY) || \ 94 | ((SensitivityValue) == EXTI_TLISENSITIVITY_RISE_ONLY)) 95 | 96 | /** 97 | * @brief Macro used by the assert function in order to check the different Port values 98 | */ 99 | #define IS_EXTI_PORT_OK(PORT) \ 100 | (((PORT) == EXTI_PORT_GPIOA) ||\ 101 | ((PORT) == EXTI_PORT_GPIOB) ||\ 102 | ((PORT) == EXTI_PORT_GPIOC) ||\ 103 | ((PORT) == EXTI_PORT_GPIOD) ||\ 104 | ((PORT) == EXTI_PORT_GPIOE)) 105 | 106 | /** 107 | * @brief Macro used by the assert function in order to check the different values of the EXTI PinMask 108 | */ 109 | #define IS_EXTI_PINMASK_OK(PinMask) ((((PinMask) & (uint8_t)0x00) == (uint8_t)0x00) && ((PinMask) != (uint8_t)0x00)) 110 | 111 | /** 112 | * @} 113 | */ 114 | 115 | /* Exported functions ------------------------------------------------------- */ 116 | 117 | /** @addtogroup EXTI_Exported_Functions 118 | * @{ 119 | */ 120 | 121 | void EXTI_DeInit(void); 122 | void EXTI_SetExtIntSensitivity(EXTI_Port_TypeDef Port, EXTI_Sensitivity_TypeDef SensitivityValue); 123 | void EXTI_SetTLISensitivity(EXTI_TLISensitivity_TypeDef SensitivityValue); 124 | EXTI_Sensitivity_TypeDef EXTI_GetExtIntSensitivity(EXTI_Port_TypeDef Port); 125 | EXTI_TLISensitivity_TypeDef EXTI_GetTLISensitivity(void); 126 | 127 | /** 128 | * @} 129 | */ 130 | 131 | #endif /* __STM8S_EXTI_H */ 132 | 133 | 134 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 135 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/inc/stm8s_gpio.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm8s_gpio.h 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all functions prototype and macros for the GPIO peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Define to prevent recursive inclusion -------------------------------------*/ 29 | #ifndef __STM8S_GPIO_H 30 | #define __STM8S_GPIO_H 31 | 32 | /* Includes ------------------------------------------------------------------*/ 33 | #include "stm8s.h" 34 | 35 | /* Exported variables ------------------------------------------------------- */ 36 | /* Exported types ------------------------------------------------------------*/ 37 | 38 | /** @addtogroup GPIO_Exported_Types 39 | * @{ 40 | */ 41 | 42 | /** 43 | * @brief GPIO modes 44 | * 45 | * Bits definitions: 46 | * - Bit 7: 0 = INPUT mode 47 | * 1 = OUTPUT mode 48 | * 1 = PULL-UP (input) or PUSH-PULL (output) 49 | * - Bit 5: 0 = No external interrupt (input) or No slope control (output) 50 | * 1 = External interrupt (input) or Slow control enabled (output) 51 | * - Bit 4: 0 = Low level (output) 52 | * 1 = High level (output push-pull) or HI-Z (output open-drain) 53 | */ 54 | typedef enum 55 | { 56 | GPIO_MODE_IN_FL_NO_IT = (uint8_t)0x00, /*!< Input floating, no external interrupt */ 57 | GPIO_MODE_IN_PU_NO_IT = (uint8_t)0x40, /*!< Input pull-up, no external interrupt */ 58 | GPIO_MODE_IN_FL_IT = (uint8_t)0x20, /*!< Input floating, external interrupt */ 59 | GPIO_MODE_IN_PU_IT = (uint8_t)0x60, /*!< Input pull-up, external interrupt */ 60 | GPIO_MODE_OUT_OD_LOW_FAST = (uint8_t)0xA0, /*!< Output open-drain, low level, 10MHz */ 61 | GPIO_MODE_OUT_PP_LOW_FAST = (uint8_t)0xE0, /*!< Output push-pull, low level, 10MHz */ 62 | GPIO_MODE_OUT_OD_LOW_SLOW = (uint8_t)0x80, /*!< Output open-drain, low level, 2MHz */ 63 | GPIO_MODE_OUT_PP_LOW_SLOW = (uint8_t)0xC0, /*!< Output push-pull, low level, 2MHz */ 64 | GPIO_MODE_OUT_OD_HIZ_FAST = (uint8_t)0xB0, /*!< Output open-drain, high-impedance level,10MHz */ 65 | GPIO_MODE_OUT_PP_HIGH_FAST = (uint8_t)0xF0, /*!< Output push-pull, high level, 10MHz */ 66 | GPIO_MODE_OUT_OD_HIZ_SLOW = (uint8_t)0x90, /*!< Output open-drain, high-impedance level, 2MHz */ 67 | GPIO_MODE_OUT_PP_HIGH_SLOW = (uint8_t)0xD0 /*!< Output push-pull, high level, 2MHz */ 68 | }GPIO_Mode_TypeDef; 69 | 70 | /** 71 | * @brief Definition of the GPIO pins. Used by the @ref GPIO_Init function in 72 | * order to select the pins to be initialized. 73 | */ 74 | 75 | typedef enum 76 | { 77 | GPIO_PIN_0 = ((uint8_t)0x01), /*!< Pin 0 selected */ 78 | GPIO_PIN_1 = ((uint8_t)0x02), /*!< Pin 1 selected */ 79 | GPIO_PIN_2 = ((uint8_t)0x04), /*!< Pin 2 selected */ 80 | GPIO_PIN_3 = ((uint8_t)0x08), /*!< Pin 3 selected */ 81 | GPIO_PIN_4 = ((uint8_t)0x10), /*!< Pin 4 selected */ 82 | GPIO_PIN_5 = ((uint8_t)0x20), /*!< Pin 5 selected */ 83 | GPIO_PIN_6 = ((uint8_t)0x40), /*!< Pin 6 selected */ 84 | GPIO_PIN_7 = ((uint8_t)0x80), /*!< Pin 7 selected */ 85 | GPIO_PIN_LNIB = ((uint8_t)0x0F), /*!< Low nibble pins selected */ 86 | GPIO_PIN_HNIB = ((uint8_t)0xF0), /*!< High nibble pins selected */ 87 | GPIO_PIN_ALL = ((uint8_t)0xFF) /*!< All pins selected */ 88 | }GPIO_Pin_TypeDef; 89 | 90 | /** 91 | * @} 92 | */ 93 | 94 | /* Exported constants --------------------------------------------------------*/ 95 | /* Exported macros -----------------------------------------------------------*/ 96 | /* Private macros ------------------------------------------------------------*/ 97 | 98 | /** @addtogroup GPIO_Private_Macros 99 | * @{ 100 | */ 101 | 102 | /** 103 | * @brief Macro used by the assert function to check the different functions parameters. 104 | */ 105 | 106 | /** 107 | * @brief Macro used by the assert function in order to check the different 108 | * values of GPIOMode_TypeDef. 109 | */ 110 | #define IS_GPIO_MODE_OK(MODE) \ 111 | (((MODE) == GPIO_MODE_IN_FL_NO_IT) || \ 112 | ((MODE) == GPIO_MODE_IN_PU_NO_IT) || \ 113 | ((MODE) == GPIO_MODE_IN_FL_IT) || \ 114 | ((MODE) == GPIO_MODE_IN_PU_IT) || \ 115 | ((MODE) == GPIO_MODE_OUT_OD_LOW_FAST) || \ 116 | ((MODE) == GPIO_MODE_OUT_PP_LOW_FAST) || \ 117 | ((MODE) == GPIO_MODE_OUT_OD_LOW_SLOW) || \ 118 | ((MODE) == GPIO_MODE_OUT_PP_LOW_SLOW) || \ 119 | ((MODE) == GPIO_MODE_OUT_OD_HIZ_FAST) || \ 120 | ((MODE) == GPIO_MODE_OUT_PP_HIGH_FAST) || \ 121 | ((MODE) == GPIO_MODE_OUT_OD_HIZ_SLOW) || \ 122 | ((MODE) == GPIO_MODE_OUT_PP_HIGH_SLOW)) 123 | 124 | /** 125 | * @brief Macro used by the assert function in order to check the different 126 | * values of GPIO_Pins. 127 | */ 128 | #define IS_GPIO_PIN_OK(PIN) ((PIN) != (uint8_t)0x00) 129 | 130 | /** 131 | * @} 132 | */ 133 | 134 | /* Exported functions ------------------------------------------------------- */ 135 | /** @addtogroup GPIO_Exported_Functions 136 | * @{ 137 | */ 138 | 139 | void GPIO_DeInit(GPIO_TypeDef* GPIOx); 140 | void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin, GPIO_Mode_TypeDef GPIO_Mode); 141 | void GPIO_Write(GPIO_TypeDef* GPIOx, uint8_t PortVal); 142 | void GPIO_WriteHigh(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef PortPins); 143 | void GPIO_WriteLow(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef PortPins); 144 | void GPIO_WriteReverse(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef PortPins); 145 | uint8_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx); 146 | uint8_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx); 147 | BitStatus GPIO_ReadInputPin(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin); 148 | void GPIO_ExternalPullUpConfig(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin, FunctionalState NewState); 149 | /** 150 | * @} 151 | */ 152 | 153 | #endif /* __STM8L_GPIO_H */ 154 | 155 | 156 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 157 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/inc/stm8s_it.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm8s_it.h 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains the headers of the interrupt handlers 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Define to prevent recursive inclusion -------------------------------------*/ 29 | #ifndef __STM8S_IT_H 30 | #define __STM8S_IT_H 31 | 32 | /* Includes ------------------------------------------------------------------*/ 33 | #include "stm8s.h" 34 | 35 | /* Exported types ------------------------------------------------------------*/ 36 | /* Exported constants --------------------------------------------------------*/ 37 | /* Exported macro ------------------------------------------------------------*/ 38 | /* Exported functions ------------------------------------------------------- */ 39 | #ifdef _COSMIC_ 40 | void _stext(void); /* RESET startup routine */ 41 | INTERRUPT void NonHandledInterrupt(void); 42 | #endif /* _COSMIC_ */ 43 | 44 | #if defined(_IAR_) 45 | INTERRUPT void TRAP_IRQHandler(void); /* TRAP */ 46 | INTERRUPT void TLI_IRQHandler(void); /* TLI */ 47 | INTERRUPT void AWU_IRQHandler(void); /* AWU */ 48 | INTERRUPT void CLK_IRQHandler(void); /* CLOCK */ 49 | INTERRUPT void EXTI_PORTA_IRQHandler(void); /* EXTI PORTA */ 50 | INTERRUPT void EXTI_PORTB_IRQHandler(void); /* EXTI PORTB */ 51 | INTERRUPT void EXTI_PORTC_IRQHandler(void); /* EXTI PORTC */ 52 | INTERRUPT void EXTI_PORTD_IRQHandler(void); /* EXTI PORTD */ 53 | INTERRUPT void EXTI_PORTE_IRQHandler(void); /* EXTI PORTE */ 54 | #elif defined(_SDCC_) 55 | INTERRUPT_HANDLER_TRAP(TRAP_IRQHandler); 56 | INTERRUPT_HANDLER(TLI_IRQHandler, 0); 57 | INTERRUPT_HANDLER(AWU_IRQHandler, 1); 58 | INTERRUPT_HANDLER(CLK_IRQHandler, 2); 59 | INTERRUPT_HANDLER(EXTI_PORTA_IRQHandler, 3); 60 | INTERRUPT_HANDLER(EXTI_PORTB_IRQHandler, 4); 61 | INTERRUPT_HANDLER(EXTI_PORTC_IRQHandler, 5); 62 | INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 6); 63 | INTERRUPT_HANDLER(EXTI_PORTE_IRQHandler, 7); 64 | #endif 65 | 66 | #if defined(STM8S903) || defined(STM8AF622x) 67 | #if defined(_IAR_) 68 | INTERRUPT void EXTI_PORTF_IRQHandler(void); /* EXTI PORTF */ 69 | #elif defined(_SDCC_) 70 | INTERRUPT_HANDLER(EXTI_PORTF_IRQHandler, 8); 71 | #endif 72 | #endif /* (STM8S903) || (STM8AF622x) */ 73 | 74 | #if defined (STM8S208) || defined (STM8AF52Ax) 75 | #if defined(_IAR_) 76 | INTERRUPT void CAN_RX_IRQHandler(void); /* CAN RX */ 77 | INTERRUPT void CAN_TX_IRQHandler(void); /* CAN TX/ER/SC */ 78 | #elif defined(_SDCC_) 79 | INTERRUPT_HANDLER(CAN_RX_IRQHandler, 8); 80 | INTERRUPT_HANDLER(CAN_TX_IRQHandler, 9); 81 | #endif 82 | #endif /* (STM8S208) || (STM8AF52Ax) */ 83 | 84 | #if defined(_IAR_) 85 | INTERRUPT void SPI_IRQHandler(void); /* SPI */ 86 | INTERRUPT void TIM1_CAP_COM_IRQHandler(void); /* TIM1 CAP/COM */ 87 | INTERRUPT void TIM1_UPD_OVF_TRG_BRK_IRQHandler(void); /* TIM1 UPD/OVF/TRG/BRK */ 88 | #elif defined(_SDCC_) 89 | INTERRUPT_HANDLER(SPI_IRQHandler, 10); 90 | INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11); 91 | INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler, 12); 92 | #endif 93 | 94 | #if defined(STM8S903) || defined(STM8AF622x) 95 | #if defined(_IAR_) 96 | INTERRUPT void TIM5_UPD_OVF_BRK_TRG_IRQHandler(void); /* TIM5 UPD/OVF/BRK/TRG */ 97 | INTERRUPT void TIM5_CAP_COM_IRQHandler(void); /* TIM5 CAP/COM */ 98 | #elif defined(_SDCC_) 99 | INTERRUPT_HANDLER(TIM5_UPD_OVF_BRK_TRG_IRQHandler, 13); 100 | INTERRUPT_HANDLER(TIM5_CAP_COM_IRQHandler, 14); 101 | #endif 102 | #else /* (STM8S208) || (STM8S207) || (STM8S105) || (STM8S103) || (STM8AF52Ax) || (STM8AF62Ax) || (STM8A626x) */ 103 | #if defined(_IAR_) 104 | INTERRUPT void TIM2_UPD_OVF_BRK_IRQHandler(void); /* TIM2 UPD/OVF/BRK */ 105 | INTERRUPT void TIM2_CAP_COM_IRQHandler(void); /* TIM2 CAP/COM */ 106 | #elif defined(_SDCC_) 107 | INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13); 108 | INTERRUPT_HANDLER(TIM2_CAP_COM_IRQHandler, 14); 109 | #endif 110 | #endif /* (STM8S903) || (STM8AF622x) */ 111 | 112 | #if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \ 113 | defined(STM8S005) || defined (STM8AF52Ax) || defined (STM8AF62Ax) || defined (STM8AF626x) 114 | #if defined(_IAR_) 115 | INTERRUPT void TIM3_UPD_OVF_BRK_IRQHandler(void); /* TIM3 UPD/OVF/BRK */ 116 | INTERRUPT void TIM3_CAP_COM_IRQHandler(void); /* TIM3 CAP/COM */ 117 | #elif defined(_SDCC_) 118 | INTERRUPT_HANDLER(TIM3_UPD_OVF_BRK_IRQHandler, 15); 119 | INTERRUPT_HANDLER(TIM3_CAP_COM_IRQHandler, 16); 120 | #endif 121 | #endif /* (STM8S208) || (STM8S207) || (STM8S105) || (STM8AF52Ax) || (STM8AF62Ax) || (STM8A626x) */ 122 | 123 | #if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S103) || \ 124 | defined(STM8S003) || defined (STM8AF52Ax) || defined (STM8AF62Ax) || defined (STM8S903) 125 | #if defined(_IAR_) 126 | INTERRUPT void UART1_TX_IRQHandler(void); /* UART1 TX */ 127 | INTERRUPT void UART1_RX_IRQHandler(void); /* UART1 RX */ 128 | #elif defined(_SDCC_) 129 | INTERRUPT_HANDLER(UART1_TX_IRQHandler, 17); 130 | INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18); 131 | #endif 132 | #endif /* (STM8S208) || (STM8S207) || (STM8S903) || (STM8S103) || (STM8AF52Ax) || (STM8AF62Ax) */ 133 | 134 | #if defined (STM8AF622x) 135 | #if defined(_IAR_) 136 | INTERRUPT void UART4_TX_IRQHandler(void); /* UART4 TX */ 137 | INTERRUPT void UART4_RX_IRQHandler(void); /* UART4 RX */ 138 | #elif defined(_SDCC_) 139 | INTERRUPT_HANDLER(UART4_TX_IRQHandler, 17); 140 | INTERRUPT_HANDLER(UART4_RX_IRQHandler, 18); 141 | #endif 142 | #endif /* (STM8AF622x) */ 143 | 144 | #if defined(_IAR_) 145 | INTERRUPT void I2C_IRQHandler(void); /* I2C */ 146 | #elif defined(_SDCC_) 147 | INTERRUPT_HANDLER(I2C_IRQHandler, 19); 148 | #endif 149 | 150 | #if defined(STM8S105) || defined(STM8S005) || defined (STM8AF626x) 151 | #if defined(_IAR_) 152 | INTERRUPT void UART2_RX_IRQHandler(void); /* UART2 RX */ 153 | INTERRUPT void UART2_TX_IRQHandler(void); /* UART2 TX */ 154 | #elif defined(_SDCC_) 155 | INTERRUPT_HANDLER(UART2_TX_IRQHandler, 20); 156 | INTERRUPT_HANDLER(UART2_RX_IRQHandler, 21); 157 | #endif 158 | #endif /* (STM8S105) || (STM8AF626x) */ 159 | 160 | #if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax) 161 | #if defined(_IAR_) 162 | INTERRUPT void UART3_RX_IRQHandler(void); /* UART3 RX */ 163 | INTERRUPT void UART3_TX_IRQHandler(void); /* UART3 TX */ 164 | #elif defined(_SDCC_) 165 | INTERRUPT_HANDLER(UART3_TX_IRQHandler, 20); 166 | INTERRUPT_HANDLER(UART3_RX_IRQHandler, 21); 167 | #endif 168 | #endif /* (STM8S207) || (STM8S208) || (STM8AF62Ax) || (STM8AF52Ax) */ 169 | 170 | #if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax) 171 | #if defined(_IAR_) 172 | INTERRUPT void ADC2_IRQHandler(void); /* ADC2 */ 173 | #elif defined(_SDCC_) 174 | INTERRUPT_HANDLER(ADC2_IRQHandler, 22); 175 | #endif 176 | #else /* (STM8S105) || (STM8S103) || (STM8S903) || (STM8AF622x) */ 177 | #if defined(_IAR_) 178 | INTERRUPT void ADC1_IRQHandler(void); /* ADC1 */ 179 | #elif defined(_SDCC_) 180 | INTERRUPT_HANDLER(ADC1_IRQHandler, 22); 181 | #endif 182 | #endif /* (STM8S207) || (STM8S208) || (STM8AF62Ax) || (STM8AF52Ax) */ 183 | 184 | #if defined(STM8S903) || defined(STM8AF622x) 185 | #if defined(_IAR_) 186 | INTERRUPT void TIM6_UPD_OVF_TRG_IRQHandler(void); /* TIM6 UPD/OVF/TRG */ 187 | #elif defined(_SDCC_) 188 | INTERRUPT_HANDLER(TIM6_UPD_OVF_TRG_IRQHandler, 23); 189 | #endif 190 | #else /* (STM8S208) || (STM8S207) || (STM8S105) || (STM8S103) || (STM8AF62Ax) || (STM8AF52Ax) || (STM8AF626x) */ 191 | #if defined(_IAR_) 192 | INTERRUPT void TIM4_UPD_OVF_IRQHandler(void); /* TIM4 UPD/OVF */ 193 | #elif defined(_SDCC_) 194 | INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23); 195 | #endif 196 | #endif /* (STM8S903) || (STM8AF622x) */ 197 | #if defined(_IAR_) 198 | INTERRUPT void EEPROM_EEC_IRQHandler(void); /* EEPROM ECC CORRECTION */ 199 | #elif defined(_SDCC_) 200 | INTERRUPT_HANDLER(EEPROM_EEC_IRQHandler, 24); 201 | #endif 202 | 203 | #endif /* __STM8S_IT_H */ 204 | 205 | 206 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 207 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/inc/stm8s_itc.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm8s_itc.h 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all functions prototype and macros for the ITC peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Define to prevent recursive inclusion -------------------------------------*/ 29 | #ifndef __STM8S_ITC_H 30 | #define __STM8S_ITC_H 31 | 32 | /* Includes ------------------------------------------------------------------*/ 33 | #include "stm8s.h" 34 | 35 | /* Exported types ------------------------------------------------------------*/ 36 | 37 | /** @addtogroup ITC_Exported_Types 38 | * @{ 39 | */ 40 | 41 | /** 42 | * @brief ITC Interrupt Lines selection 43 | */ 44 | typedef enum { 45 | ITC_IRQ_TLI = (uint8_t)0, /*!< Software interrupt */ 46 | ITC_IRQ_AWU = (uint8_t)1, /*!< Auto wake up from halt interrupt */ 47 | ITC_IRQ_CLK = (uint8_t)2, /*!< Clock controller interrupt */ 48 | ITC_IRQ_PORTA = (uint8_t)3, /*!< Port A external interrupts */ 49 | ITC_IRQ_PORTB = (uint8_t)4, /*!< Port B external interrupts */ 50 | ITC_IRQ_PORTC = (uint8_t)5, /*!< Port C external interrupts */ 51 | ITC_IRQ_PORTD = (uint8_t)6, /*!< Port D external interrupts */ 52 | ITC_IRQ_PORTE = (uint8_t)7, /*!< Port E external interrupts */ 53 | 54 | #if defined(STM8S208) || defined(STM8AF52Ax) 55 | ITC_IRQ_CAN_RX = (uint8_t)8, /*!< beCAN RX interrupt */ 56 | ITC_IRQ_CAN_TX = (uint8_t)9, /*!< beCAN TX/ER/SC interrupt */ 57 | #endif /*STM8S208 or STM8AF52Ax */ 58 | 59 | #if defined(STM8S903) || defined(STM8AF622x) 60 | ITC_IRQ_PORTF = (uint8_t)8, /*!< Port F external interrupts */ 61 | #endif /*STM8S903 or STM8AF622x */ 62 | 63 | ITC_IRQ_SPI = (uint8_t)10, /*!< SPI interrupt */ 64 | ITC_IRQ_TIM1_OVF = (uint8_t)11, /*!< TIM1 update/overflow/underflow/trigger/ 65 | break interrupt*/ 66 | ITC_IRQ_TIM1_CAPCOM = (uint8_t)12, /*!< TIM1 capture/compare interrupt */ 67 | 68 | #if defined(STM8S903) || defined(STM8AF622x) 69 | ITC_IRQ_TIM5_OVFTRI = (uint8_t)13, /*!< TIM5 update/overflow/underflow/trigger/ 70 | interrupt */ 71 | ITC_IRQ_TIM5_CAPCOM = (uint8_t)14, /*!< TIM5 capture/compare interrupt */ 72 | #else 73 | ITC_IRQ_TIM2_OVF = (uint8_t)13, /*!< TIM2 update /overflow interrupt */ 74 | ITC_IRQ_TIM2_CAPCOM = (uint8_t)14, /*!< TIM2 capture/compare interrupt */ 75 | #endif /*STM8S903 or STM8AF622x */ 76 | 77 | ITC_IRQ_TIM3_OVF = (uint8_t)15, /*!< TIM3 update /overflow interrupt*/ 78 | ITC_IRQ_TIM3_CAPCOM = (uint8_t)16, /*!< TIM3 update /overflow interrupt */ 79 | 80 | #if defined(STM8S208) ||defined(STM8S207) || defined (STM8S007) || defined(STM8S103) || \ 81 | defined(STM8S003) ||defined(STM8S903) || defined (STM8AF52Ax) || defined (STM8AF62Ax) 82 | ITC_IRQ_UART1_TX = (uint8_t)17, /*!< UART1 TX interrupt */ 83 | ITC_IRQ_UART1_RX = (uint8_t)18, /*!< UART1 RX interrupt */ 84 | #endif /*STM8S208 or STM8S207 or STM8S007 or STM8S103 or STM8S003 or STM8S903 or STM8AF52Ax or STM8AF62Ax */ 85 | #if defined(STM8AF622x) 86 | ITC_IRQ_UART4_TX = (uint8_t)17, /*!< UART4 TX interrupt */ 87 | ITC_IRQ_UART4_RX = (uint8_t)18, /*!< UART4 RX interrupt */ 88 | #endif /*STM8AF622x */ 89 | 90 | ITC_IRQ_I2C = (uint8_t)19, /*!< I2C interrupt */ 91 | 92 | #if defined(STM8S105) || defined(STM8S005) || defined(STM8AF626x) 93 | ITC_IRQ_UART2_TX = (uint8_t)20, /*!< USART2 TX interrupt */ 94 | ITC_IRQ_UART2_RX = (uint8_t)21, /*!< USART2 RX interrupt */ 95 | #endif /*STM8S105 or STM8AF626x */ 96 | 97 | #if defined(STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8AF52Ax) || defined(STM8AF62Ax) 98 | ITC_IRQ_UART3_TX = (uint8_t)20, /*!< USART3 TX interrupt */ 99 | ITC_IRQ_UART3_RX = (uint8_t)21, /*!< USART3 RX interrupt */ 100 | ITC_IRQ_ADC2 = (uint8_t)22, /*!< ADC2 interrupt */ 101 | #endif /*STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax */ 102 | 103 | #if defined(STM8S105) || defined(STM8S005) || defined(STM8S103) || defined(STM8S003) || defined(STM8S903) || defined(STM8AF626x) || defined(STM8AF622x) 104 | ITC_IRQ_ADC1 = (uint8_t)22, /*!< ADC2 interrupt */ 105 | #endif /*STM8S105 or STM8S005 or STM8S003 or STM8S103 or STM8S903 or STM8AF626x or STM8AF622x */ 106 | 107 | #if defined(STM8S903) || defined(STM8AF622x) 108 | ITC_IRQ_TIM6_OVFTRI = (uint8_t)23, /*!< TIM6 update/overflow/underflow/trigger/ 109 | interrupt */ 110 | #else 111 | ITC_IRQ_TIM4_OVF = (uint8_t)23, /*!< TIM4 update /overflow interrupt */ 112 | #endif /*STM8S903 or STM8AF622x */ 113 | 114 | ITC_IRQ_EEPROM_EEC = (uint8_t)24 /*!< Flash interrupt */ 115 | } ITC_Irq_TypeDef; 116 | 117 | /** 118 | * @brief ITC Priority Levels selection 119 | */ 120 | typedef enum { 121 | ITC_PRIORITYLEVEL_0 = (uint8_t)0x02, /*!< Software priority level 0 (cannot be written) */ 122 | ITC_PRIORITYLEVEL_1 = (uint8_t)0x01, /*!< Software priority level 1 */ 123 | ITC_PRIORITYLEVEL_2 = (uint8_t)0x00, /*!< Software priority level 2 */ 124 | ITC_PRIORITYLEVEL_3 = (uint8_t)0x03 /*!< Software priority level 3 */ 125 | } ITC_PriorityLevel_TypeDef; 126 | 127 | /** 128 | * @} 129 | */ 130 | 131 | /* Exported constants --------------------------------------------------------*/ 132 | 133 | /** @addtogroup ITC_Exported_Constants 134 | * @{ 135 | */ 136 | #define CPU_SOFT_INT_DISABLED ((uint8_t)0x28) /*!< Mask for I1 and I0 bits in CPU_CC register */ 137 | 138 | /** 139 | * @} 140 | */ 141 | 142 | /* Private macros ------------------------------------------------------------*/ 143 | 144 | /** 145 | * @brief Macros used by the assert function in order to check the different functions parameters. 146 | * @addtogroup ITC_Private_Macros 147 | * @{ 148 | */ 149 | 150 | /* Used by assert function */ 151 | #define IS_ITC_IRQ_OK(IRQ) ((IRQ) <= (uint8_t)24) 152 | 153 | /* Used by assert function */ 154 | #define IS_ITC_PRIORITY_OK(PriorityValue) \ 155 | (((PriorityValue) == ITC_PRIORITYLEVEL_0) || \ 156 | ((PriorityValue) == ITC_PRIORITYLEVEL_1) || \ 157 | ((PriorityValue) == ITC_PRIORITYLEVEL_2) || \ 158 | ((PriorityValue) == ITC_PRIORITYLEVEL_3)) 159 | 160 | /* Used by assert function */ 161 | #define IS_ITC_INTERRUPTS_DISABLED (ITC_GetSoftIntStatus() == CPU_SOFT_INT_DISABLED) 162 | 163 | /** 164 | * @} 165 | */ 166 | 167 | /* Exported functions ------------------------------------------------------- */ 168 | 169 | /** @addtogroup ITC_Exported_Functions 170 | * @{ 171 | */ 172 | 173 | uint8_t ITC_GetCPUCC(void); 174 | void ITC_DeInit(void); 175 | uint8_t ITC_GetSoftIntStatus(void); 176 | void ITC_SetSoftwarePriority(ITC_Irq_TypeDef IrqNum, ITC_PriorityLevel_TypeDef PriorityValue); 177 | ITC_PriorityLevel_TypeDef ITC_GetSoftwarePriority(ITC_Irq_TypeDef IrqNum); 178 | 179 | /** 180 | * @} 181 | */ 182 | 183 | #endif /* __STM8S_ITC_H */ 184 | 185 | 186 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 187 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/inc/stm8s_iwdg.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm8s_iwdg.h 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all functions prototypes and macros for the IWDG peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Define to prevent recursive inclusion -------------------------------------*/ 29 | #ifndef __STM8S_IWDG_H 30 | #define __STM8S_IWDG_H 31 | 32 | /* Includes ------------------------------------------------------------------*/ 33 | #include "stm8s.h" 34 | 35 | /** @addtogroup STM8S_StdPeriph_Driver 36 | * @{ 37 | */ 38 | 39 | /** @addtogroup IWDG_Private_Define 40 | * @{ 41 | */ 42 | 43 | /** 44 | * @brief Define used to prevent watchdog reset 45 | */ 46 | #define IWDG_KEY_REFRESH ((uint8_t)0xAA) /*!< This value written in the Key register prevent the watchdog reset */ 47 | 48 | /** 49 | * @brief Define used to start the watchdog counter down 50 | */ 51 | #define IWDG_KEY_ENABLE ((uint8_t)0xCC) /*!< This value written in the Key register start the watchdog counting down*/ 52 | 53 | /** 54 | * @} 55 | */ 56 | 57 | /** @addtogroup IWDG_Private_Macros 58 | * @{ 59 | */ 60 | 61 | /** 62 | * @brief Macro used by the assert function in order to check the different 63 | * values of the prescaler. 64 | */ 65 | #define IS_IWDG_PRESCALER_OK(VALUE) (((VALUE) == IWDG_Prescaler_4 ) || \ 66 | ((VALUE) == IWDG_Prescaler_8 ) || \ 67 | ((VALUE) == IWDG_Prescaler_16 ) || \ 68 | ((VALUE) == IWDG_Prescaler_32 ) || \ 69 | ((VALUE) == IWDG_Prescaler_64 ) || \ 70 | ((VALUE) == IWDG_Prescaler_128 ) || \ 71 | ((VALUE) == IWDG_Prescaler_256)) 72 | 73 | /** 74 | * @brief Macro used by the assert function in order to check the different 75 | * values of the counter register. 76 | */ 77 | #define IS_IWDG_WRITEACCESS_MODE_OK(MODE) (((MODE) == IWDG_WriteAccess_Enable) || ((MODE) == IWDG_WriteAccess_Disable)) 78 | 79 | /** 80 | * @} 81 | */ 82 | 83 | /** @addtogroup IWDG_Exported_Types 84 | * @{ 85 | */ 86 | 87 | /** IWDG write access enumeration */ 88 | typedef enum 89 | { 90 | IWDG_WriteAccess_Enable = (uint8_t)0x55, /*!< Code 0x55 in Key register, allow write access to Prescaler and Reload registers */ 91 | IWDG_WriteAccess_Disable = (uint8_t)0x00 /*!< Code 0x00 in Key register, not allow write access to Prescaler and Reload registers */ 92 | } IWDG_WriteAccess_TypeDef; 93 | 94 | /** IWDG prescaler enumaration */ 95 | typedef enum 96 | { 97 | IWDG_Prescaler_4 = (uint8_t)0x00, /*!< Used to set prescaler register to 4 */ 98 | IWDG_Prescaler_8 = (uint8_t)0x01, /*!< Used to set prescaler register to 8 */ 99 | IWDG_Prescaler_16 = (uint8_t)0x02, /*!< Used to set prescaler register to 16 */ 100 | IWDG_Prescaler_32 = (uint8_t)0x03, /*!< Used to set prescaler register to 32 */ 101 | IWDG_Prescaler_64 = (uint8_t)0x04, /*!< Used to set prescaler register to 64 */ 102 | IWDG_Prescaler_128 = (uint8_t)0x05, /*!< Used to set prescaler register to 128 */ 103 | IWDG_Prescaler_256 = (uint8_t)0x06 /*!< Used to set prescaler register to 256 */ 104 | } IWDG_Prescaler_TypeDef; 105 | 106 | /** 107 | * @} 108 | */ 109 | 110 | /** @addtogroup IWDG_Exported_Functions 111 | * @{ 112 | */ 113 | 114 | void IWDG_WriteAccessCmd(IWDG_WriteAccess_TypeDef IWDG_WriteAccess); 115 | void IWDG_SetPrescaler(IWDG_Prescaler_TypeDef IWDG_Prescaler); 116 | void IWDG_SetReload(uint8_t IWDG_Reload); 117 | void IWDG_ReloadCounter(void); 118 | void IWDG_Enable(void); 119 | 120 | /** 121 | * @} 122 | */ 123 | 124 | #endif /* __STM8S_IWDG_H */ 125 | 126 | /** 127 | * @} 128 | */ 129 | 130 | 131 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 132 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/inc/stm8s_rst.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm8s_rst.h 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all functions prototype and macros for the RST peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | /* Define to prevent recursive inclusion -------------------------------------*/ 28 | #ifndef __STM8S_RST_H 29 | #define __STM8S_RST_H 30 | 31 | /* Includes ------------------------------------------------------------------*/ 32 | #include "stm8s.h" 33 | 34 | /** @addtogroup STM8S_StdPeriph_Driver 35 | * @{ 36 | */ 37 | 38 | /** @addtogroup RST_Exported_Types 39 | * @{ 40 | */ 41 | typedef enum { 42 | RST_FLAG_EMCF = (uint8_t)0x10, /*!< EMC reset flag */ 43 | RST_FLAG_SWIMF = (uint8_t)0x08, /*!< SWIM reset flag */ 44 | RST_FLAG_ILLOPF = (uint8_t)0x04, /*!< Illigal opcode reset flag */ 45 | RST_FLAG_IWDGF = (uint8_t)0x02, /*!< Independent watchdog reset flag */ 46 | RST_FLAG_WWDGF = (uint8_t)0x01 /*!< Window watchdog reset flag */ 47 | }RST_Flag_TypeDef; 48 | 49 | /** 50 | * @} 51 | */ 52 | 53 | /* Exported constants --------------------------------------------------------*/ 54 | /* Exported macros -----------------------------------------------------------*/ 55 | 56 | /** @addtogroup RST_Private_Macros 57 | * @{ 58 | */ 59 | 60 | /** 61 | * @brief Macro used by the assert function to check the different functions parameters. 62 | */ 63 | /** 64 | * @brief Macro used by the assert function to check the different RST flags. 65 | */ 66 | #define IS_RST_FLAG_OK(FLAG) (((FLAG) == RST_FLAG_EMCF) || \ 67 | ((FLAG) == RST_FLAG_SWIMF) ||\ 68 | ((FLAG) == RST_FLAG_ILLOPF) ||\ 69 | ((FLAG) == RST_FLAG_IWDGF) ||\ 70 | ((FLAG) == RST_FLAG_WWDGF)) 71 | 72 | /** 73 | * @} 74 | */ 75 | 76 | /** @addtogroup RST_Exported_functions 77 | * @{ 78 | */ 79 | FlagStatus RST_GetFlagStatus(RST_Flag_TypeDef RST_Flag); 80 | void RST_ClearFlag(RST_Flag_TypeDef RST_Flag); 81 | 82 | /** 83 | * @} 84 | */ 85 | 86 | #endif /* __STM8S_RST_H */ 87 | 88 | /** 89 | * @} 90 | */ 91 | 92 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 93 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/inc/stm8s_tim4.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm8s_tim4.h 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all functions prototype and macros for the TIM4 peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Define to prevent recursive inclusion -------------------------------------*/ 29 | #ifndef __STM8S_TIM4_H 30 | #define __STM8S_TIM4_H 31 | 32 | /* Includes ------------------------------------------------------------------*/ 33 | #include "stm8s.h" 34 | 35 | /** @addtogroup STM8S_StdPeriph_Driver 36 | * @{ 37 | */ 38 | 39 | /* Exported types ------------------------------------------------------------*/ 40 | 41 | /** @addtogroup TIM4_Exported_Types 42 | * @{ 43 | */ 44 | 45 | 46 | 47 | /** TIM4 Prescaler */ 48 | typedef enum 49 | { 50 | TIM4_PRESCALER_1 = ((uint8_t)0x00), 51 | TIM4_PRESCALER_2 = ((uint8_t)0x01), 52 | TIM4_PRESCALER_4 = ((uint8_t)0x02), 53 | TIM4_PRESCALER_8 = ((uint8_t)0x03), 54 | TIM4_PRESCALER_16 = ((uint8_t)0x04), 55 | TIM4_PRESCALER_32 = ((uint8_t)0x05), 56 | TIM4_PRESCALER_64 = ((uint8_t)0x06), 57 | TIM4_PRESCALER_128 = ((uint8_t)0x07) 58 | } TIM4_Prescaler_TypeDef; 59 | 60 | #define IS_TIM4_PRESCALER_OK(PRESCALER) (((PRESCALER) == TIM4_PRESCALER_1 ) || \ 61 | ((PRESCALER) == TIM4_PRESCALER_2 ) || \ 62 | ((PRESCALER) == TIM4_PRESCALER_4 ) || \ 63 | ((PRESCALER) == TIM4_PRESCALER_8 ) || \ 64 | ((PRESCALER) == TIM4_PRESCALER_16 ) || \ 65 | ((PRESCALER) == TIM4_PRESCALER_32 ) || \ 66 | ((PRESCALER) == TIM4_PRESCALER_64 ) || \ 67 | ((PRESCALER) == TIM4_PRESCALER_128 ) ) 68 | 69 | /** TIM4 One Pulse Mode */ 70 | typedef enum 71 | { 72 | TIM4_OPMODE_SINGLE = ((uint8_t)0x01), 73 | TIM4_OPMODE_REPETITIVE = ((uint8_t)0x00) 74 | } TIM4_OPMode_TypeDef; 75 | 76 | #define IS_TIM4_OPM_MODE_OK(MODE) (((MODE) == TIM4_OPMODE_SINGLE) || \ 77 | ((MODE) == TIM4_OPMODE_REPETITIVE)) 78 | 79 | /** TIM4 Prescaler Reload Mode */ 80 | typedef enum 81 | { 82 | TIM4_PSCRELOADMODE_UPDATE = ((uint8_t)0x00), 83 | TIM4_PSCRELOADMODE_IMMEDIATE = ((uint8_t)0x01) 84 | } TIM4_PSCReloadMode_TypeDef; 85 | 86 | #define IS_TIM4_PRESCALER_RELOAD_OK(RELOAD) (((RELOAD) == TIM4_PSCRELOADMODE_UPDATE) || \ 87 | ((RELOAD) == TIM4_PSCRELOADMODE_IMMEDIATE)) 88 | 89 | /** TIM4 Update Source */ 90 | typedef enum 91 | { 92 | TIM4_UPDATESOURCE_GLOBAL = ((uint8_t)0x00), 93 | TIM4_UPDATESOURCE_REGULAR = ((uint8_t)0x01) 94 | } TIM4_UpdateSource_TypeDef; 95 | 96 | #define IS_TIM4_UPDATE_SOURCE_OK(SOURCE) (((SOURCE) == TIM4_UPDATESOURCE_GLOBAL) || \ 97 | ((SOURCE) == TIM4_UPDATESOURCE_REGULAR)) 98 | 99 | /** TIM4 Event Source */ 100 | typedef enum 101 | { 102 | TIM4_EVENTSOURCE_UPDATE = ((uint8_t)0x01) 103 | }TIM4_EventSource_TypeDef; 104 | 105 | #define IS_TIM4_EVENT_SOURCE_OK(SOURCE) (((SOURCE) == 0x01)) 106 | 107 | /** TIM4 Flags */ 108 | typedef enum 109 | { 110 | TIM4_FLAG_UPDATE = ((uint8_t)0x01) 111 | }TIM4_FLAG_TypeDef; 112 | 113 | #define IS_TIM4_GET_FLAG_OK(FLAG) ((FLAG) == TIM4_FLAG_UPDATE) 114 | 115 | 116 | 117 | /** TIM4 interrupt sources */ 118 | typedef enum 119 | { 120 | TIM4_IT_UPDATE = ((uint8_t)0x01) 121 | }TIM4_IT_TypeDef; 122 | 123 | #define IS_TIM4_IT_OK(IT) ((IT) == TIM4_IT_UPDATE) 124 | 125 | 126 | 127 | /** 128 | * @} 129 | */ 130 | 131 | /* Exported macro ------------------------------------------------------------*/ 132 | 133 | /* Exported functions --------------------------------------------------------*/ 134 | 135 | /** @addtogroup TIM4_Exported_Functions 136 | * @{ 137 | */ 138 | void TIM4_DeInit(void); 139 | void TIM4_TimeBaseInit(TIM4_Prescaler_TypeDef TIM4_Prescaler, uint8_t TIM4_Period); 140 | void TIM4_Cmd(FunctionalState NewState); 141 | void TIM4_ITConfig(TIM4_IT_TypeDef TIM4_IT, FunctionalState NewState); 142 | void TIM4_UpdateDisableConfig(FunctionalState NewState); 143 | void TIM4_UpdateRequestConfig(TIM4_UpdateSource_TypeDef TIM4_UpdateSource); 144 | void TIM4_SelectOnePulseMode(TIM4_OPMode_TypeDef TIM4_OPMode); 145 | void TIM4_PrescalerConfig(TIM4_Prescaler_TypeDef Prescaler, TIM4_PSCReloadMode_TypeDef TIM4_PSCReloadMode); 146 | void TIM4_ARRPreloadConfig(FunctionalState NewState); 147 | void TIM4_GenerateEvent(TIM4_EventSource_TypeDef TIM4_EventSource); 148 | void TIM4_SetCounter(uint8_t Counter); 149 | void TIM4_SetAutoreload(uint8_t Autoreload); 150 | uint8_t TIM4_GetCounter(void); 151 | TIM4_Prescaler_TypeDef TIM4_GetPrescaler(void); 152 | FlagStatus TIM4_GetFlagStatus(TIM4_FLAG_TypeDef TIM4_FLAG); 153 | void TIM4_ClearFlag(TIM4_FLAG_TypeDef TIM4_FLAG); 154 | ITStatus TIM4_GetITStatus(TIM4_IT_TypeDef TIM4_IT); 155 | void TIM4_ClearITPendingBit(TIM4_IT_TypeDef TIM4_IT); 156 | 157 | 158 | /** 159 | * @} 160 | */ 161 | 162 | #endif /* __STM8S_TIM4_H */ 163 | 164 | /** 165 | * @} 166 | */ 167 | 168 | 169 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 170 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/inc/stm8s_wwdg.h: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************** 3 | * @file stm8s_wwdg.h 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all functions prototype and macros for the WWDG peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Define to prevent recursive inclusion -------------------------------------*/ 29 | #ifndef __STM8S_WWDG_H 30 | #define __STM8S_WWDG_H 31 | 32 | /* Includes ------------------------------------------------------------------*/ 33 | #include "stm8s.h" 34 | 35 | /** @addtogroup STM8S_StdPeriph_Driver 36 | * @{ 37 | */ 38 | 39 | /* Private macros ------------------------------------------------------------*/ 40 | 41 | /** @addtogroup WWDG_Private_Macros 42 | * @{ 43 | */ 44 | 45 | /** 46 | * @brief Macro used by the assert function in order to check the 47 | * values of the window register. 48 | */ 49 | #define IS_WWDG_WINDOWLIMITVALUE_OK(WindowLimitValue) ((WindowLimitValue) <= 0x7F) 50 | 51 | /** 52 | * @brief Macro used by the assert function in order to check the different 53 | * values of the counter register. 54 | */ 55 | #define IS_WWDG_COUNTERVALUE_OK(CounterValue) ((CounterValue) <= 0x7F) 56 | 57 | /** 58 | * @} 59 | */ 60 | 61 | /* Exported types ------------------------------------------------------------*/ 62 | 63 | /* Exported functions ------------------------------------------------------- */ 64 | 65 | /** @addtogroup WWDG_Exported_Functions 66 | * @{ 67 | */ 68 | 69 | void WWDG_Init(uint8_t Counter, uint8_t WindowValue); 70 | void WWDG_SetCounter(uint8_t Counter); 71 | uint8_t WWDG_GetCounter(void); 72 | void WWDG_SWReset(void); 73 | void WWDG_SetWindowValue(uint8_t WindowValue); 74 | 75 | 76 | /** 77 | * @} 78 | */ 79 | 80 | #endif /* __STM8S_WWDG_H */ 81 | 82 | /** 83 | * @} 84 | */ 85 | 86 | 87 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 88 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_awu.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm8s_awu.c 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all the functions for the AWU peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm8s_awu.h" 30 | 31 | /** @addtogroup STM8S_StdPeriph_Driver 32 | * @{ 33 | */ 34 | /* Private typedef -----------------------------------------------------------*/ 35 | /* Private define ------------------------------------------------------------*/ 36 | /* Private macro -------------------------------------------------------------*/ 37 | /* Private variables ---------------------------------------------------------*/ 38 | /* Private function prototypes -----------------------------------------------*/ 39 | /* Private functions ---------------------------------------------------------*/ 40 | 41 | /* See also AWU_Timebase_TypeDef structure in stm8s_awu.h file : 42 | N 2 5 1 2 4 8 1 3 6 1 2 5 1 2 1 3 43 | O 5 0 m m m m 6 2 4 2 5 1 s s 2 0 44 | I 0 0 s s s s m m m 8 6 2 s s 45 | T u u s s s m m m 46 | s s s s s 47 | */ 48 | /** Contains the different values to write in the APR register (used by AWU_Init function) */ 49 | CONST uint8_t APR_Array[17] = 50 | { 51 | 0, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 61, 23, 23, 62 52 | }; 53 | 54 | /** Contains the different values to write in the TBR register (used by AWU_Init function) */ 55 | CONST uint8_t TBR_Array[17] = 56 | { 57 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 14, 15, 15 58 | }; 59 | 60 | /* Public functions ----------------------------------------------------------*/ 61 | 62 | /** 63 | * @addtogroup AWU_Public_Functions 64 | * @{ 65 | */ 66 | 67 | /** 68 | * @brief Deinitializes the AWU peripheral registers to their default reset 69 | * values. 70 | * @param None 71 | * @retval None 72 | */ 73 | void AWU_DeInit(void) 74 | { 75 | AWU->CSR = AWU_CSR_RESET_VALUE; 76 | AWU->APR = AWU_APR_RESET_VALUE; 77 | AWU->TBR = AWU_TBR_RESET_VALUE; 78 | } 79 | 80 | /** 81 | * @brief Initializes the AWU peripheral according to the specified parameters. 82 | * @param AWU_TimeBase : Time base selection (interval between AWU interrupts). 83 | * can be one of the values of @ref AWU_Timebase_TypeDef. 84 | * @retval None 85 | * @par Required preconditions: 86 | * The LS RC calibration must be performed before calling this function. 87 | */ 88 | void AWU_Init(AWU_Timebase_TypeDef AWU_TimeBase) 89 | { 90 | /* Check parameter */ 91 | assert_param(IS_AWU_TIMEBASE_OK(AWU_TimeBase)); 92 | 93 | /* Enable the AWU peripheral */ 94 | AWU->CSR |= AWU_CSR_AWUEN; 95 | 96 | /* Set the TimeBase */ 97 | AWU->TBR &= (uint8_t)(~AWU_TBR_AWUTB); 98 | AWU->TBR |= TBR_Array[(uint8_t)AWU_TimeBase]; 99 | 100 | /* Set the APR divider */ 101 | AWU->APR &= (uint8_t)(~AWU_APR_APR); 102 | AWU->APR |= APR_Array[(uint8_t)AWU_TimeBase]; 103 | } 104 | 105 | /** 106 | * @brief Enable or disable the AWU peripheral. 107 | * @param NewState Indicates the new state of the AWU peripheral. 108 | * @retval None 109 | * @par Required preconditions: 110 | * Initialisation of AWU and LS RC calibration must be done before. 111 | */ 112 | void AWU_Cmd(FunctionalState NewState) 113 | { 114 | if (NewState != DISABLE) 115 | { 116 | /* Enable the AWU peripheral */ 117 | AWU->CSR |= AWU_CSR_AWUEN; 118 | } 119 | else 120 | { 121 | /* Disable the AWU peripheral */ 122 | AWU->CSR &= (uint8_t)(~AWU_CSR_AWUEN); 123 | } 124 | } 125 | 126 | /** 127 | * @brief Update APR register with the measured LSI frequency. 128 | * @par Note on the APR calculation: 129 | * A is the integer part of lsifreqkhz/4 and x the decimal part. 130 | * x <= A/(1+2A) is equivalent to A >= x(1+2A) and also to 4A >= 4x(1+2A) [F1] 131 | * but we know that A + x = lsifreqkhz/4 ==> 4x = lsifreqkhz-4A 132 | * so [F1] can be written : 133 | * 4A >= (lsifreqkhz-4A)(1+2A) 134 | * @param LSIFreqHz Low Speed RC frequency measured by timer (in Hz). 135 | * @retval None 136 | * @par Required preconditions: 137 | * - AWU must be disabled to avoid unwanted interrupts. 138 | */ 139 | void AWU_LSICalibrationConfig(uint32_t LSIFreqHz) 140 | { 141 | uint16_t lsifreqkhz = 0x0; 142 | uint16_t A = 0x0; 143 | 144 | /* Check parameter */ 145 | assert_param(IS_LSI_FREQUENCY_OK(LSIFreqHz)); 146 | 147 | lsifreqkhz = (uint16_t)(LSIFreqHz / 1000); /* Converts value in kHz */ 148 | 149 | /* Calculation of AWU calibration value */ 150 | 151 | A = (uint16_t)(lsifreqkhz >> 2U); /* Division by 4, keep integer part only */ 152 | 153 | if ((4U * A) >= ((lsifreqkhz - (4U * A)) * (1U + (2U * A)))) 154 | { 155 | AWU->APR = (uint8_t)(A - 2U); 156 | } 157 | else 158 | { 159 | AWU->APR = (uint8_t)(A - 1U); 160 | } 161 | } 162 | 163 | /** 164 | * @brief Configures AWU in Idle mode to reduce power consumption. 165 | * @param None 166 | * @retval None 167 | */ 168 | void AWU_IdleModeEnable(void) 169 | { 170 | /* Disable AWU peripheral */ 171 | AWU->CSR &= (uint8_t)(~AWU_CSR_AWUEN); 172 | 173 | /* No AWU timebase */ 174 | AWU->TBR = (uint8_t)(~AWU_TBR_AWUTB); 175 | } 176 | 177 | /** 178 | * @brief Returns status of the AWU peripheral flag. 179 | * @param None 180 | * @retval FlagStatus : Status of the AWU flag. 181 | * This parameter can be any of the @ref FlagStatus enumeration. 182 | */ 183 | FlagStatus AWU_GetFlagStatus(void) 184 | { 185 | return((FlagStatus)(((uint8_t)(AWU->CSR & AWU_CSR_AWUF) == (uint8_t)0x00) ? RESET : SET)); 186 | } 187 | 188 | 189 | /** 190 | * @} 191 | */ 192 | 193 | /** 194 | * @} 195 | */ 196 | 197 | 198 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 199 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_beep.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm8s_beep.c 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all the functions for the BEEP peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm8s_beep.h" 30 | 31 | /** @addtogroup STM8S_StdPeriph_Driver 32 | * @{ 33 | */ 34 | /* Private typedef -----------------------------------------------------------*/ 35 | /* Private define ------------------------------------------------------------*/ 36 | /* Private macro -------------------------------------------------------------*/ 37 | /* Private variables ---------------------------------------------------------*/ 38 | /* Private function prototypes -----------------------------------------------*/ 39 | /* Private functions ---------------------------------------------------------*/ 40 | 41 | /* Public functions ----------------------------------------------------------*/ 42 | 43 | /** 44 | * @addtogroup BEEP_Public_Functions 45 | * @{ 46 | */ 47 | 48 | /** 49 | * @brief Deinitializes the BEEP peripheral registers to their default reset 50 | * values. 51 | * @param None 52 | * @retval None 53 | */ 54 | void BEEP_DeInit(void) 55 | { 56 | BEEP->CSR = BEEP_CSR_RESET_VALUE; 57 | } 58 | 59 | /** 60 | * @brief Initializes the BEEP function according to the specified parameters. 61 | * @param BEEP_Frequency Frequency selection. 62 | * can be one of the values of @ref BEEP_Frequency_TypeDef. 63 | * @retval None 64 | * @par Required preconditions: 65 | * The LS RC calibration must be performed before calling this function. 66 | */ 67 | void BEEP_Init(BEEP_Frequency_TypeDef BEEP_Frequency) 68 | { 69 | /* Check parameter */ 70 | assert_param(IS_BEEP_FREQUENCY_OK(BEEP_Frequency)); 71 | 72 | /* Set a default calibration value if no calibration is done */ 73 | if ((BEEP->CSR & BEEP_CSR_BEEPDIV) == BEEP_CSR_BEEPDIV) 74 | { 75 | BEEP->CSR &= (uint8_t)(~BEEP_CSR_BEEPDIV); /* Clear bits */ 76 | BEEP->CSR |= BEEP_CALIBRATION_DEFAULT; 77 | } 78 | 79 | /* Select the output frequency */ 80 | BEEP->CSR &= (uint8_t)(~BEEP_CSR_BEEPSEL); 81 | BEEP->CSR |= (uint8_t)(BEEP_Frequency); 82 | } 83 | 84 | /** 85 | * @brief Enable or disable the BEEP function. 86 | * @param NewState Indicates the new state of the BEEP function. 87 | * @retval None 88 | * @par Required preconditions: 89 | * Initialisation of BEEP and LS RC calibration must be done before. 90 | */ 91 | void BEEP_Cmd(FunctionalState NewState) 92 | { 93 | if (NewState != DISABLE) 94 | { 95 | /* Enable the BEEP peripheral */ 96 | BEEP->CSR |= BEEP_CSR_BEEPEN; 97 | } 98 | else 99 | { 100 | /* Disable the BEEP peripheral */ 101 | BEEP->CSR &= (uint8_t)(~BEEP_CSR_BEEPEN); 102 | } 103 | } 104 | 105 | /** 106 | * @brief Update CSR register with the measured LSI frequency. 107 | * @par Note on the APR calculation: 108 | * A is the integer part of LSIFreqkHz/4 and x the decimal part. 109 | * x <= A/(1+2A) is equivalent to A >= x(1+2A) and also to 4A >= 4x(1+2A) [F1] 110 | * but we know that A + x = LSIFreqkHz/4 ==> 4x = LSIFreqkHz-4A 111 | * so [F1] can be written : 112 | * 4A >= (LSIFreqkHz-4A)(1+2A) 113 | * @param LSIFreqHz Low Speed RC frequency measured by timer (in Hz). 114 | * @retval None 115 | * @par Required preconditions: 116 | * - BEEP must be disabled to avoid unwanted interrupts. 117 | */ 118 | void BEEP_LSICalibrationConfig(uint32_t LSIFreqHz) 119 | { 120 | uint16_t lsifreqkhz; 121 | uint16_t A; 122 | 123 | /* Check parameter */ 124 | assert_param(IS_LSI_FREQUENCY_OK(LSIFreqHz)); 125 | 126 | lsifreqkhz = (uint16_t)(LSIFreqHz / 1000); /* Converts value in kHz */ 127 | 128 | /* Calculation of BEEPER calibration value */ 129 | 130 | BEEP->CSR &= (uint8_t)(~BEEP_CSR_BEEPDIV); /* Clear bits */ 131 | 132 | A = (uint16_t)(lsifreqkhz >> 3U); /* Division by 8, keep integer part only */ 133 | 134 | if ((8U * A) >= ((lsifreqkhz - (8U * A)) * (1U + (2U * A)))) 135 | { 136 | BEEP->CSR |= (uint8_t)(A - 2U); 137 | } 138 | else 139 | { 140 | BEEP->CSR |= (uint8_t)(A - 1U); 141 | } 142 | } 143 | 144 | /** 145 | * @} 146 | */ 147 | 148 | /** 149 | * @} 150 | */ 151 | 152 | 153 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 154 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_can.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/StdPeriphLib/src/stm8s_can.c -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_clk.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/StdPeriphLib/src/stm8s_clk.c -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_exti.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm8s_exti.c 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all the functions for the EXTI peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm8s_exti.h" 30 | 31 | /** @addtogroup STM8S_StdPeriph_Driver 32 | * @{ 33 | */ 34 | /* Private typedef -----------------------------------------------------------*/ 35 | /* Private define ------------------------------------------------------------*/ 36 | /* Private macro -------------------------------------------------------------*/ 37 | /* Private variables ---------------------------------------------------------*/ 38 | /* Private function prototypes -----------------------------------------------*/ 39 | /* Private functions ---------------------------------------------------------*/ 40 | 41 | /* Public functions ----------------------------------------------------------*/ 42 | 43 | /** 44 | * @addtogroup EXTI_Public_Functions 45 | * @{ 46 | */ 47 | 48 | /** 49 | * @brief Deinitializes the external interrupt control registers to their default reset value. 50 | * @param None 51 | * @retval None 52 | */ 53 | void EXTI_DeInit(void) 54 | { 55 | EXTI->CR1 = EXTI_CR1_RESET_VALUE; 56 | EXTI->CR2 = EXTI_CR2_RESET_VALUE; 57 | } 58 | 59 | /** 60 | * @brief Set the external interrupt sensitivity of the selected port. 61 | * @warning 62 | * - The modification of external interrupt sensitivity is only possible when the interrupts are disabled. 63 | * - The normal behavior is to disable the interrupts before calling this function, and re-enable them after. 64 | * @param Port The port number to access. 65 | * @param SensitivityValue The external interrupt sensitivity value to set. 66 | * @retval None 67 | * @par Required preconditions: 68 | * Global interrupts must be disabled before calling this function. 69 | */ 70 | void EXTI_SetExtIntSensitivity(EXTI_Port_TypeDef Port, EXTI_Sensitivity_TypeDef SensitivityValue) 71 | { 72 | /* Check function parameters */ 73 | assert_param(IS_EXTI_PORT_OK(Port)); 74 | assert_param(IS_EXTI_SENSITIVITY_OK(SensitivityValue)); 75 | 76 | /* Set external interrupt sensitivity */ 77 | switch (Port) 78 | { 79 | case EXTI_PORT_GPIOA: 80 | EXTI->CR1 &= (uint8_t)(~EXTI_CR1_PAIS); 81 | EXTI->CR1 |= (uint8_t)(SensitivityValue); 82 | break; 83 | case EXTI_PORT_GPIOB: 84 | EXTI->CR1 &= (uint8_t)(~EXTI_CR1_PBIS); 85 | EXTI->CR1 |= (uint8_t)((uint8_t)(SensitivityValue) << 2); 86 | break; 87 | case EXTI_PORT_GPIOC: 88 | EXTI->CR1 &= (uint8_t)(~EXTI_CR1_PCIS); 89 | EXTI->CR1 |= (uint8_t)((uint8_t)(SensitivityValue) << 4); 90 | break; 91 | case EXTI_PORT_GPIOD: 92 | EXTI->CR1 &= (uint8_t)(~EXTI_CR1_PDIS); 93 | EXTI->CR1 |= (uint8_t)((uint8_t)(SensitivityValue) << 6); 94 | break; 95 | case EXTI_PORT_GPIOE: 96 | EXTI->CR2 &= (uint8_t)(~EXTI_CR2_PEIS); 97 | EXTI->CR2 |= (uint8_t)(SensitivityValue); 98 | break; 99 | default: 100 | break; 101 | } 102 | } 103 | 104 | /** 105 | * @brief Set the TLI interrupt sensitivity. 106 | * @param SensitivityValue The TLI interrupt sensitivity value. 107 | * @retval None 108 | * @par Required preconditions: 109 | * Global interrupts must be disabled before calling this function. 110 | */ 111 | void EXTI_SetTLISensitivity(EXTI_TLISensitivity_TypeDef SensitivityValue) 112 | { 113 | /* Check function parameters */ 114 | assert_param(IS_EXTI_TLISENSITIVITY_OK(SensitivityValue)); 115 | 116 | /* Set TLI interrupt sensitivity */ 117 | EXTI->CR2 &= (uint8_t)(~EXTI_CR2_TLIS); 118 | EXTI->CR2 |= (uint8_t)(SensitivityValue); 119 | } 120 | 121 | /** 122 | * @brief Get the external interrupt sensitivity of the selected port. 123 | * @param Port The port number to access. 124 | * @retval EXTI_Sensitivity_TypeDef The external interrupt sensitivity of the selected port. 125 | */ 126 | EXTI_Sensitivity_TypeDef EXTI_GetExtIntSensitivity(EXTI_Port_TypeDef Port) 127 | { 128 | uint8_t value = 0; 129 | 130 | /* Check function parameters */ 131 | assert_param(IS_EXTI_PORT_OK(Port)); 132 | 133 | switch (Port) 134 | { 135 | case EXTI_PORT_GPIOA: 136 | value = (uint8_t)(EXTI->CR1 & EXTI_CR1_PAIS); 137 | break; 138 | case EXTI_PORT_GPIOB: 139 | value = (uint8_t)((uint8_t)(EXTI->CR1 & EXTI_CR1_PBIS) >> 2); 140 | break; 141 | case EXTI_PORT_GPIOC: 142 | value = (uint8_t)((uint8_t)(EXTI->CR1 & EXTI_CR1_PCIS) >> 4); 143 | break; 144 | case EXTI_PORT_GPIOD: 145 | value = (uint8_t)((uint8_t)(EXTI->CR1 & EXTI_CR1_PDIS) >> 6); 146 | break; 147 | case EXTI_PORT_GPIOE: 148 | value = (uint8_t)(EXTI->CR2 & EXTI_CR2_PEIS); 149 | break; 150 | default: 151 | break; 152 | } 153 | 154 | return((EXTI_Sensitivity_TypeDef)value); 155 | } 156 | 157 | /** 158 | * @brief Get the TLI interrupt sensitivity. 159 | * @param None 160 | * @retval EXTI_TLISensitivity_TypeDef The TLI interrupt sensitivity read. 161 | */ 162 | EXTI_TLISensitivity_TypeDef EXTI_GetTLISensitivity(void) 163 | { 164 | uint8_t value = 0; 165 | 166 | /* Get TLI interrupt sensitivity */ 167 | value = (uint8_t)(EXTI->CR2 & EXTI_CR2_TLIS); 168 | 169 | return((EXTI_TLISensitivity_TypeDef)value); 170 | } 171 | 172 | /** 173 | * @} 174 | */ 175 | 176 | /** 177 | * @} 178 | */ 179 | 180 | 181 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 182 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_flash.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/StdPeriphLib/src/stm8s_flash.c -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_gpio.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm8s_gpio.c 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all the functions for the GPIO peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm8s_gpio.h" 30 | 31 | /** @addtogroup STM8S_StdPeriph_Driver 32 | * @{ 33 | */ 34 | /* Private typedef -----------------------------------------------------------*/ 35 | /* Private define ------------------------------------------------------------*/ 36 | /* Private macro -------------------------------------------------------------*/ 37 | /* Private variables ---------------------------------------------------------*/ 38 | /* Private function prototypes -----------------------------------------------*/ 39 | /* Private functions ---------------------------------------------------------*/ 40 | 41 | /* Public functions ----------------------------------------------------------*/ 42 | 43 | /** 44 | * @addtogroup GPIO_Public_Functions 45 | * @{ 46 | */ 47 | 48 | /** 49 | * @brief Deinitializes the GPIOx peripheral registers to their default reset values. 50 | * @param GPIOx: Select the GPIO peripheral number (x = A to I). 51 | * @retval None 52 | */ 53 | void GPIO_DeInit(GPIO_TypeDef* GPIOx) 54 | { 55 | GPIOx->ODR = GPIO_ODR_RESET_VALUE; /* Reset Output Data Register */ 56 | GPIOx->DDR = GPIO_DDR_RESET_VALUE; /* Reset Data Direction Register */ 57 | GPIOx->CR1 = GPIO_CR1_RESET_VALUE; /* Reset Control Register 1 */ 58 | GPIOx->CR2 = GPIO_CR2_RESET_VALUE; /* Reset Control Register 2 */ 59 | } 60 | 61 | /** 62 | * @brief Initializes the GPIOx according to the specified parameters. 63 | * @param GPIOx : Select the GPIO peripheral number (x = A to I). 64 | * @param GPIO_Pin : This parameter contains the pin number, it can be any value 65 | * of the @ref GPIO_Pin_TypeDef enumeration. 66 | * @param GPIO_Mode : This parameter can be a value of the 67 | * @Ref GPIO_Mode_TypeDef enumeration. 68 | * @retval None 69 | */ 70 | 71 | void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin, GPIO_Mode_TypeDef GPIO_Mode) 72 | { 73 | /* Reset corresponding bit to GPIO_Pin in CR2 register */ 74 | GPIOx->CR2 &= (uint8_t)(~(GPIO_Pin)); 75 | 76 | /*-----------------------------*/ 77 | /* Input/Output mode selection */ 78 | /*-----------------------------*/ 79 | 80 | if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x80) != (uint8_t)0x00) /* Output mode */ 81 | { 82 | if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x10) != (uint8_t)0x00) /* High level */ 83 | { 84 | GPIOx->ODR |= (uint8_t)GPIO_Pin; 85 | } 86 | else /* Low level */ 87 | { 88 | GPIOx->ODR &= (uint8_t)(~(GPIO_Pin)); 89 | } 90 | /* Set Output mode */ 91 | GPIOx->DDR |= (uint8_t)GPIO_Pin; 92 | } 93 | else /* Input mode */ 94 | { 95 | /* Set Input mode */ 96 | GPIOx->DDR &= (uint8_t)(~(GPIO_Pin)); 97 | } 98 | 99 | /*------------------------------------------------------------------------*/ 100 | /* Pull-Up/Float (Input) or Push-Pull/Open-Drain (Output) modes selection */ 101 | /*------------------------------------------------------------------------*/ 102 | 103 | if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x40) != (uint8_t)0x00) /* Pull-Up or Push-Pull */ 104 | { 105 | GPIOx->CR1 |= (uint8_t)GPIO_Pin; 106 | } 107 | else /* Float or Open-Drain */ 108 | { 109 | GPIOx->CR1 &= (uint8_t)(~(GPIO_Pin)); 110 | } 111 | 112 | /*-----------------------------------------------------*/ 113 | /* Interrupt (Input) or Slope (Output) modes selection */ 114 | /*-----------------------------------------------------*/ 115 | 116 | if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x20) != (uint8_t)0x00) /* Interrupt or Slow slope */ 117 | { 118 | GPIOx->CR2 |= (uint8_t)GPIO_Pin; 119 | } 120 | else /* No external interrupt or No slope control */ 121 | { 122 | GPIOx->CR2 &= (uint8_t)(~(GPIO_Pin)); 123 | } 124 | } 125 | 126 | /** 127 | * @brief Writes data to the specified GPIO data port. 128 | * @note The port must be configured in output mode. 129 | * @param GPIOx : Select the GPIO peripheral number (x = A to I). 130 | * @param GPIO_PortVal : Specifies the value to be written to the port output 131 | * data register. 132 | * @retval None 133 | */ 134 | void GPIO_Write(GPIO_TypeDef* GPIOx, uint8_t PortVal) 135 | { 136 | GPIOx->ODR = PortVal; 137 | } 138 | 139 | /** 140 | * @brief Writes high level to the specified GPIO pins. 141 | * @note The port must be configured in output mode. 142 | * @param GPIOx : Select the GPIO peripheral number (x = A to I). 143 | * @param PortPins : Specifies the pins to be turned high to the port output. 144 | * data register. 145 | * @retval None 146 | */ 147 | void GPIO_WriteHigh(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef PortPins) 148 | { 149 | GPIOx->ODR |= (uint8_t)PortPins; 150 | } 151 | 152 | /** 153 | * @brief Writes low level to the specified GPIO pins. 154 | * @note The port must be configured in output mode. 155 | * @param GPIOx : Select the GPIO peripheral number (x = A to I). 156 | * @param PortPins : Specifies the pins to be turned low to the port output. 157 | * data register. 158 | * @retval None 159 | */ 160 | void GPIO_WriteLow(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef PortPins) 161 | { 162 | GPIOx->ODR &= (uint8_t)(~PortPins); 163 | } 164 | 165 | /** 166 | * @brief Writes reverse level to the specified GPIO pins. 167 | * @note The port must be configured in output mode. 168 | * @param GPIOx : Select the GPIO peripheral number (x = A to I). 169 | * @param PortPins : Specifies the pins to be reversed to the port output. 170 | * data register. 171 | * @retval None 172 | */ 173 | void GPIO_WriteReverse(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef PortPins) 174 | { 175 | GPIOx->ODR ^= (uint8_t)PortPins; 176 | } 177 | 178 | /** 179 | * @brief Reads the specified GPIO output data port. 180 | * @note The port must be configured in input mode. 181 | * @param GPIOx : Select the GPIO peripheral number (x = A to I). 182 | * @retval GPIO output data port value. 183 | */ 184 | uint8_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx) 185 | { 186 | return ((uint8_t)GPIOx->ODR); 187 | } 188 | 189 | /** 190 | * @brief Reads the specified GPIO input data port. 191 | * @note The port must be configured in input mode. 192 | * @param GPIOx : Select the GPIO peripheral number (x = A to I). 193 | * @retval GPIO input data port value. 194 | */ 195 | uint8_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx) 196 | { 197 | return ((uint8_t)GPIOx->IDR); 198 | } 199 | 200 | /** 201 | * @brief Reads the specified GPIO input data pin. 202 | * @param GPIOx : Select the GPIO peripheral number (x = A to I). 203 | * @param GPIO_Pin : Specifies the pin number. 204 | * @retval BitStatus : GPIO input pin status. 205 | */ 206 | BitStatus GPIO_ReadInputPin(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin) 207 | { 208 | return ((BitStatus)(GPIOx->IDR & (uint8_t)GPIO_Pin)); 209 | } 210 | 211 | /** 212 | * @brief Configures the external pull-up on GPIOx pins. 213 | * @param GPIOx : Select the GPIO peripheral number (x = A to I). 214 | * @param GPIO_Pin : Specifies the pin number 215 | * @param NewState : The new state of the pull up pin. 216 | * @retval None 217 | */ 218 | void GPIO_ExternalPullUpConfig(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin, FunctionalState NewState) 219 | { 220 | /* Check the parameters */ 221 | assert_param(IS_GPIO_PIN_OK(GPIO_Pin)); 222 | assert_param(IS_FUNCTIONALSTATE_OK(NewState)); 223 | 224 | if (NewState != DISABLE) /* External Pull-Up Set*/ 225 | { 226 | GPIOx->CR1 |= (uint8_t)GPIO_Pin; 227 | } else /* External Pull-Up Reset*/ 228 | { 229 | GPIOx->CR1 &= (uint8_t)(~(GPIO_Pin)); 230 | } 231 | } 232 | 233 | /** 234 | * @} 235 | */ 236 | 237 | /** 238 | * @} 239 | */ 240 | 241 | 242 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 243 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_i2c.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/StdPeriphLib/src/stm8s_i2c.c -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_iwdg.c: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************** 3 | * @file stm8s_iwdg.c 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all the functions for the IWDG peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm8s_iwdg.h" 30 | 31 | /* Private define ------------------------------------------------------------*/ 32 | /* Private macro -------------------------------------------------------------*/ 33 | /* Private variables ---------------------------------------------------------*/ 34 | /* Private function prototypes -----------------------------------------------*/ 35 | /* Private functions ---------------------------------------------------------*/ 36 | /* Public functions ----------------------------------------------------------*/ 37 | 38 | /** @addtogroup IWDG_Public_Functions 39 | * @{ 40 | */ 41 | 42 | /** 43 | * @brief Enables or disables write access to Prescaler and Reload registers. 44 | * @param IWDG_WriteAccess : New state of write access to Prescaler and Reload 45 | * registers. This parameter can be a value of @ref IWDG_WriteAccess_TypeDef. 46 | * @retval None 47 | */ 48 | void IWDG_WriteAccessCmd(IWDG_WriteAccess_TypeDef IWDG_WriteAccess) 49 | { 50 | /* Check the parameters */ 51 | assert_param(IS_IWDG_WRITEACCESS_MODE_OK(IWDG_WriteAccess)); 52 | 53 | IWDG->KR = (uint8_t)IWDG_WriteAccess; /* Write Access */ 54 | } 55 | 56 | /** 57 | * @brief Sets IWDG Prescaler value. 58 | * @note Write access should be enabled 59 | * @param IWDG_Prescaler : Specifies the IWDG Prescaler value. 60 | * This parameter can be a value of @ref IWDG_Prescaler_TypeDef. 61 | * @retval None 62 | */ 63 | void IWDG_SetPrescaler(IWDG_Prescaler_TypeDef IWDG_Prescaler) 64 | { 65 | /* Check the parameters */ 66 | assert_param(IS_IWDG_PRESCALER_OK(IWDG_Prescaler)); 67 | 68 | IWDG->PR = (uint8_t)IWDG_Prescaler; 69 | } 70 | 71 | /** 72 | * @brief Sets IWDG Reload value. 73 | * @note Write access should be enabled 74 | * @param IWDG_Reload : Reload register value. 75 | * This parameter must be a number between 0 and 0xFF. 76 | * @retval None 77 | */ 78 | void IWDG_SetReload(uint8_t IWDG_Reload) 79 | { 80 | IWDG->RLR = IWDG_Reload; 81 | } 82 | 83 | /** 84 | * @brief Reloads IWDG counter 85 | * @note Write access should be enabled 86 | * @param None 87 | * @retval None 88 | */ 89 | void IWDG_ReloadCounter(void) 90 | { 91 | IWDG->KR = IWDG_KEY_REFRESH; 92 | } 93 | 94 | /** 95 | * @brief Enables IWDG. 96 | * @param None 97 | * @retval None 98 | */ 99 | void IWDG_Enable(void) 100 | { 101 | IWDG->KR = IWDG_KEY_ENABLE; 102 | } 103 | 104 | /** 105 | * @} 106 | */ 107 | 108 | /** 109 | * @} 110 | */ 111 | 112 | 113 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 114 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_rst.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm8s_rst.c 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all the functions for the RST peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | 30 | #include "stm8s_rst.h" 31 | 32 | /** @addtogroup STM8S_StdPeriph_Driver 33 | * @{ 34 | */ 35 | /* Private typedef -----------------------------------------------------------*/ 36 | /* Private define ------------------------------------------------------------*/ 37 | /* Private macro -------------------------------------------------------------*/ 38 | /* Private variables ---------------------------------------------------------*/ 39 | /* Private function prototypes -----------------------------------------------*/ 40 | /* Private Constants ---------------------------------------------------------*/ 41 | /* Public functions ----------------------------------------------------------*/ 42 | /** 43 | * @addtogroup RST_Public_Functions 44 | * @{ 45 | */ 46 | 47 | 48 | /** 49 | * @brief Checks whether the specified RST flag is set or not. 50 | * @param RST_Flag : specify the reset flag to check. 51 | * This parameter can be a value of @ref RST_FLAG_TypeDef. 52 | * @retval FlagStatus: status of the given RST flag. 53 | */ 54 | FlagStatus RST_GetFlagStatus(RST_Flag_TypeDef RST_Flag) 55 | { 56 | /* Check the parameters */ 57 | assert_param(IS_RST_FLAG_OK(RST_Flag)); 58 | 59 | /* Get flag status */ 60 | return((FlagStatus)(((uint8_t)(RST->SR & RST_Flag) == (uint8_t)0x00) ? RESET : SET)); 61 | } 62 | 63 | /** 64 | * @brief Clears the specified RST flag. 65 | * @param RST_Flag : specify the reset flag to clear. 66 | * This parameter can be a value of @ref RST_FLAG_TypeDef. 67 | * @retval None 68 | */ 69 | void RST_ClearFlag(RST_Flag_TypeDef RST_Flag) 70 | { 71 | /* Check the parameters */ 72 | assert_param(IS_RST_FLAG_OK(RST_Flag)); 73 | 74 | RST->SR = (uint8_t)RST_Flag; 75 | } 76 | 77 | /** 78 | * @} 79 | */ 80 | 81 | /** 82 | * @} 83 | */ 84 | 85 | 86 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 87 | -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_tim2.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/StdPeriphLib/src/stm8s_tim2.c -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_tim3.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/StdPeriphLib/src/stm8s_tim3.c -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_tim4.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/StdPeriphLib/src/stm8s_tim4.c -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_tim5.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/StdPeriphLib/src/stm8s_tim5.c -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_tim6.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/StdPeriphLib/src/stm8s_tim6.c -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_uart1.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/StdPeriphLib/src/stm8s_uart1.c -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_uart2.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/StdPeriphLib/src/stm8s_uart2.c -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_uart4.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/StdPeriphLib/src/stm8s_uart4.c -------------------------------------------------------------------------------- /firmware/StdPeriphLib/src/stm8s_wwdg.c: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************** 3 | * @file stm8s_wwdg.c 4 | * @author MCD Application Team 5 | * @version V2.2.0 6 | * @date 30-September-2014 7 | * @brief This file contains all the functions for the WWDG peripheral. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2014 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm8s_wwdg.h" 30 | 31 | /** @addtogroup STM8S_StdPeriph_Driver 32 | * @{ 33 | */ 34 | /* Private define ------------------------------------------------------------*/ 35 | #define BIT_MASK ((uint8_t)0x7F) 36 | /* Private macro -------------------------------------------------------------*/ 37 | /* Private variables ---------------------------------------------------------*/ 38 | /* Private function prototypes -----------------------------------------------*/ 39 | /* Private functions ---------------------------------------------------------*/ 40 | 41 | /** @addtogroup WWDG_Public_Functions 42 | * @{ 43 | */ 44 | 45 | /** 46 | * @brief Initializes the WWDG peripheral. 47 | * This function set Window Register = WindowValue, Counter Register 48 | * according to Counter and \b ENABLE \b WWDG 49 | * @param Counter : WWDG counter value 50 | * @param WindowValue : specifies the WWDG Window Register, range is 0x00 to 0x7F. 51 | * @retval None 52 | */ 53 | void WWDG_Init(uint8_t Counter, uint8_t WindowValue) 54 | { 55 | /* Check the parameters */ 56 | assert_param(IS_WWDG_WINDOWLIMITVALUE_OK(WindowValue)); 57 | 58 | WWDG->WR = WWDG_WR_RESET_VALUE; 59 | WWDG->CR = (uint8_t)((uint8_t)(WWDG_CR_WDGA | WWDG_CR_T6) | (uint8_t)Counter); 60 | WWDG->WR = (uint8_t)((uint8_t)(~WWDG_CR_WDGA) & (uint8_t)(WWDG_CR_T6 | WindowValue)); 61 | } 62 | 63 | /** 64 | * @brief Refreshes the WWDG peripheral. 65 | * @param Counter : WWDG Counter Value 66 | * This parameter must be a number between 0x40 and 0x7F. 67 | * @retval None 68 | */ 69 | void WWDG_SetCounter(uint8_t Counter) 70 | { 71 | /* Check the parameters */ 72 | assert_param(IS_WWDG_COUNTERVALUE_OK(Counter)); 73 | 74 | /* Write to T[6:0] bits to configure the counter value, no need to do 75 | a read-modify-write; writing a 0 to WDGA bit does nothing */ 76 | WWDG->CR = (uint8_t)(Counter & (uint8_t)BIT_MASK); 77 | } 78 | 79 | /** 80 | * @brief Gets the WWDG Counter Value. 81 | * This value could be used to check if WWDG is in the window, where 82 | * refresh is allowed. 83 | * @param None 84 | * @retval WWDG Counter Value 85 | */ 86 | uint8_t WWDG_GetCounter(void) 87 | { 88 | return(WWDG->CR); 89 | } 90 | 91 | /** 92 | * @brief Generates immediate WWDG RESET. 93 | * @param None 94 | * @retval None 95 | */ 96 | void WWDG_SWReset(void) 97 | { 98 | WWDG->CR = WWDG_CR_WDGA; /* Activate WWDG, with clearing T6 */ 99 | } 100 | 101 | /** 102 | * @brief Sets the WWDG window value. 103 | * @param WindowValue: specifies the window value to be compared to the 104 | * downcounter. 105 | * This parameter value must be lower than 0x80. 106 | * @retval None 107 | */ 108 | void WWDG_SetWindowValue(uint8_t WindowValue) 109 | { 110 | /* Check the parameters */ 111 | assert_param(IS_WWDG_WINDOWLIMITVALUE_OK(WindowValue)); 112 | 113 | WWDG->WR = (uint8_t)((uint8_t)(~WWDG_CR_WDGA) & (uint8_t)(WWDG_CR_T6 | WindowValue)); 114 | } 115 | 116 | /** 117 | * @} 118 | */ 119 | 120 | /** 121 | * @} 122 | */ 123 | 124 | 125 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 126 | -------------------------------------------------------------------------------- /firmware/WriteOptionBytes.bat: -------------------------------------------------------------------------------- 1 | PATH = %PATH%;C:\Program Files (x86)\STMicroelectronics\st_toolset\stvp 2 | 3 | STVP_CmdLine -BoardName=ST-LINK -ProgMode=SWIM -Port=USB -Device=STM8S105x6 -FileOption=optionbytes.hex -verbose -no_loop 4 | 5 | exit 6 | -------------------------------------------------------------------------------- /firmware/adc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * EGG OpenSource EBike firmware 3 | * 4 | * Copyright (C) Casainho, 2015, 2106, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #include 10 | #include 11 | #include "stm8s.h" 12 | #include "gpio.h" 13 | #include "stm8s_adc1.h" 14 | #include "stm8s_tim2.h" 15 | #include "adc.h" 16 | #include "ebike_app.h" 17 | 18 | void adc_trigger (void); 19 | 20 | void adc_init (void) 21 | { 22 | uint8_t ui8_i; 23 | uint16_t ui16_counter; 24 | 25 | //init GPIO for the used ADC pins 26 | GPIO_Init(GPIOB, 27 | (THROTTLE__PIN || PHASE_B_CURRENT__PIN || MOTOR_CURRENT__PIN), 28 | GPIO_MODE_IN_FL_NO_IT); 29 | 30 | GPIO_Init(GPIOE, 31 | (BATTERY_CURRENT__PIN), 32 | GPIO_MODE_IN_FL_NO_IT); 33 | 34 | //de-Init ADC peripheral 35 | ADC1_DeInit(); 36 | 37 | //init ADC1 peripheral 38 | ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, 39 | ADC1_CHANNEL_9, 40 | ADC1_PRESSEL_FCPU_D2, 41 | ADC1_EXTTRIG_TIM, 42 | DISABLE, 43 | ADC1_ALIGN_LEFT, 44 | (ADC1_SCHMITTTRIG_CHANNEL4 || ADC1_SCHMITTTRIG_CHANNEL5 || ADC1_SCHMITTTRIG_CHANNEL6 || ADC1_SCHMITTTRIG_CHANNEL8 || ADC1_SCHMITTTRIG_CHANNEL9), 45 | DISABLE); 46 | 47 | ADC1_ScanModeCmd (ENABLE); 48 | ADC1_Cmd (ENABLE); 49 | 50 | //******************************************************************************** 51 | // next code is for "calibrating" the offset value of ADC battery current 52 | // read and discard few samples of ADC, to make sure the next samples are ok 53 | for (ui8_i = 0; ui8_i < 4; ui8_i++) 54 | { 55 | ui16_counter = TIM2_GetCounter () + 10; 56 | while (TIM2_GetCounter () < ui16_counter) ; // delay ~10ms 57 | adc_trigger (); 58 | while (!ADC1_GetFlagStatus (ADC1_FLAG_EOC)) ; // wait for end of conversion 59 | ui8_adc_motor_current_offset = ui8_adc_read_battery_current (); 60 | } 61 | 62 | // read and average a few values of ADC battery current 63 | ui16_adc_battery_current_offset_10b = 0; 64 | for (ui8_i = 0; ui8_i < 16; ui8_i++) 65 | { 66 | ui16_counter = TIM2_GetCounter () + 10; // delay ~10ms 67 | adc_trigger (); 68 | while (!ADC1_GetFlagStatus (ADC1_FLAG_EOC)) ; // wait for end of conversion 69 | ui16_adc_battery_current_offset_10b += ui16_adc_read_battery_current_10b (); 70 | } 71 | ui16_adc_battery_current_offset_10b >>= 4; 72 | ui8_adc_battery_current_offset = ui16_adc_battery_current_offset_10b >> 2; 73 | 74 | // read and average a few values of ADC motor current 75 | ui16_adc_motor_current_offset = 0; 76 | for (ui8_i = 0; ui8_i < 16; ui8_i++) 77 | { 78 | ui16_counter = TIM2_GetCounter () + 10; // delay ~10ms 79 | adc_trigger (); 80 | while (!ADC1_GetFlagStatus (ADC1_FLAG_EOC)) ; // wait for end of conversion 81 | ui16_adc_motor_current_offset += ui8_adc_read_motor_current (); 82 | } 83 | ui16_adc_motor_current_offset >>= 4; 84 | ui8_adc_motor_current_offset = ui16_adc_motor_current_offset; 85 | 86 | // read and average a few values of ADC throttle 87 | ui16_adc_throttle_offset = 0; 88 | for (ui8_i = 0; ui8_i < 16; ui8_i++) 89 | { 90 | ui16_counter = TIM2_GetCounter () + 10; // delay ~10ms 91 | adc_trigger (); 92 | while (!ADC1_GetFlagStatus (ADC1_FLAG_EOC)) ; // wait for end of conversion 93 | ui16_adc_throttle_offset += UI8_ADC_THROTTLE; 94 | } 95 | ui16_adc_throttle_offset >>= 4; 96 | ui8_adc_throttle_offset = ui16_adc_throttle_offset; 97 | } 98 | 99 | void adc_trigger (void) 100 | { 101 | // trigger ADC conversion of all channels (scan conversion, buffered) 102 | ADC1->CSR &= 0x09; // clear EOC flag first (selected also channel 9) 103 | ADC1->CR1 |= ADC1_CR1_ADON; // Start ADC1 conversion 104 | } 105 | 106 | uint8_t ui8_adc_read_phase_B_current (void) 107 | { 108 | // /* Read LSB first */ 109 | // templ = *(uint8_t*)(uint16_t)((uint16_t)ADC1_BaseAddress + (uint8_t)(Buffer << 1) + 1); 110 | // /* Then read MSB */ 111 | // temph = *(uint8_t*)(uint16_t)((uint16_t)ADC1_BaseAddress + (uint8_t)(Buffer << 1)); 112 | //#define ADC1_BaseAddress 0x53E0 113 | //phase_B_current --> ADC_AIN5 114 | // 0x53E0 + 2*5 = 0x53EA 115 | return *(uint8_t*)(0x53EA); 116 | } 117 | 118 | uint16_t ui16_adc_read_phase_B_current (void) 119 | { 120 | uint16_t temph; 121 | uint8_t templ; 122 | 123 | templ = *(uint8_t*)(0x53EB); 124 | temph = *(uint8_t*)(0x53EA); 125 | 126 | return ((uint16_t) temph) << 2 | ((uint16_t) templ); 127 | } 128 | 129 | uint8_t ui8_adc_read_throttle (void) 130 | { 131 | // 0x53E0 + 2*4 = 0x53E8 132 | // return *(uint8_t*)(0x53E8); 133 | return *(uint8_t*)(0x53E8); 134 | } 135 | 136 | uint8_t ui8_adc_read_battery_current (void) 137 | { 138 | // 0x53E0 + 2*8 = 0x53F0 139 | return *(uint8_t*)(0x53F0); 140 | } 141 | 142 | uint16_t ui16_adc_read_battery_current_10b (void) 143 | { 144 | uint16_t temph; 145 | uint8_t templ; 146 | 147 | templ = *(uint8_t*)(0x53F1); 148 | temph = *(uint8_t*)(0x53F0); 149 | 150 | return ((uint16_t) temph) << 2 | ((uint16_t) templ); 151 | } 152 | 153 | uint8_t ui8_adc_read_motor_current (void) 154 | { 155 | // 0x53E0 + 2*6 = 0x53EC 156 | return *(uint8_t*)(0x53EC); 157 | } 158 | 159 | uint16_t ui16_adc_read_motor_current_10b (void) 160 | { 161 | uint16_t temph; 162 | uint8_t templ; 163 | 164 | templ = *(uint8_t*)(0x53ED); 165 | temph = *(uint8_t*)(0x53EC); 166 | 167 | return ((uint16_t) temph) << 2 | ((uint16_t) templ); 168 | } 169 | 170 | uint8_t ui8_adc_read_battery_voltage (void) 171 | { 172 | // 0x53E0 + 2*9 = 0x53F2 173 | return *(uint8_t*)(0x53F2); 174 | } 175 | -------------------------------------------------------------------------------- /firmware/adc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EGG OpenSource EBike firmware 3 | * 4 | * Copyright (C) Casainho, 2015, 2106, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #ifndef _ADC_H 10 | #define _ADC_H 11 | 12 | #include "main.h" 13 | 14 | #define ADC1_CHANNEL_MOTOR_PHASE_B_CURRENT ADC1_CHANNEL_5 15 | #define ADC1_CHANNEL_MOTOR_CURRENT ADC1_CHANNEL_6 16 | #define ADC1_CHANNEL_BATTERY_CURRENT ADC1_CHANNEL_8 17 | #define ADC1_CHANNEL_BATTERY_VOLTAGE ADC1_CHANNEL_9 18 | #define ADC1_CHANNEL_THROTTLE ADC1_CHANNEL_4 19 | 20 | //phase_B_current --> ADC_AIN5 21 | // 0x53E0 + 2*5 = 0x53EA 22 | 23 | #define UI8_ADC_BATTERY_VOLTAGE (*(uint8_t*)(0x53F2)) 24 | #define UI8_ADC_BATTERY_CURRENT (*(uint8_t*)(0x53F0)) 25 | #define UI8_ADC_MOTOR_CURRENT (*(uint8_t*)(0x53EC)) 26 | #define UI8_ADC_MOTOR_PHASE_B_CURRENT (*(uint8_t*)(0x53EA)) 27 | #define UI8_ADC_THROTTLE (*(uint8_t*)(0x53E8)) 28 | 29 | extern uint8_t adc_throttle_busy_flag; 30 | extern uint8_t ui8_adc_throttle_value; 31 | extern uint8_t ui8_adc_motor_current_offset; 32 | extern uint16_t ui16_adc_motor_current_offset; 33 | 34 | void adc_init (void); 35 | inline void adc_trigger (void); 36 | uint8_t ui8_adc_read_phase_B_current (void); 37 | uint16_t ui16_adc_read_phase_B_current (void); 38 | uint8_t ui8_adc_read_throttle (void); 39 | uint8_t ui8_adc_read_battery_current (void); 40 | uint16_t ui16_adc_read_battery_current_10b (void); 41 | uint8_t ui8_adc_read_motor_current (void); 42 | uint16_t ui16_adc_read_motor_current_10b (void); 43 | uint8_t ui8_adc_read_battery_voltage (void); 44 | 45 | #endif /* _ADC_H */ 46 | -------------------------------------------------------------------------------- /firmware/brake.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #include 10 | #include "stm8s.h" 11 | #include "stm8s_it.h" 12 | #include "gpio.h" 13 | #include "main.h" 14 | #include "interrupts.h" 15 | #include "brake.h" 16 | #include "ebike_app.h" 17 | #include "motor.h" 18 | #include "pwm.h" 19 | 20 | // Brake signal 21 | void EXTI_PORTA_IRQHandler(void) __interrupt(EXTI_PORTA_IRQHANDLER) 22 | { 23 | if (brake_is_set()) 24 | { 25 | motor_controller_set_state (MOTOR_CONTROLLER_STATE_BRAKE); 26 | ebike_app_battery_set_regen_current_max (ADC_BATTERY_REGEN_CURRENT_MAX); 27 | motor_set_regen_current_max (ADC_MOTOR_REGEN_CURRENT_MAX); 28 | motor_set_pwm_duty_cycle_target (0); 29 | ebike_app_cruise_control_stop (); 30 | ebike_app_set_state (EBIKE_APP_STATE_MOTOR_STOP); 31 | } 32 | else 33 | { 34 | motor_set_regen_current_max (0); 35 | ebike_app_battery_set_regen_current_max (0); 36 | ebike_app_set_state (EBIKE_APP_STATE_MOTOR_STOP); 37 | motor_controller_reset_state (MOTOR_CONTROLLER_STATE_BRAKE); 38 | #if defined (EBIKE_REGEN_EBRAKE_LIKE_COAST_BRAKES) 39 | motor_reset_regen_ebrake_like_coast_brakes (); 40 | #endif 41 | } 42 | } 43 | 44 | void brake_init (void) 45 | { 46 | //brake pin as external input pin interrupt 47 | GPIO_Init(BRAKE__PORT, 48 | BRAKE__PIN, 49 | GPIO_MODE_IN_FL_IT); // with external interrupt 50 | 51 | //initialize the Interrupt sensitivity 52 | EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOA, 53 | EXTI_SENSITIVITY_RISE_FALL); 54 | } 55 | 56 | BitStatus brake_is_set (void) 57 | { 58 | if (GPIO_ReadInputPin(BRAKE__PORT, BRAKE__PIN) == 0) 59 | return 1; 60 | else 61 | return 0; 62 | } 63 | 64 | -------------------------------------------------------------------------------- /firmware/brake.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #ifndef _BRAKE_H 10 | #define _BRAKE_H 11 | 12 | #include "main.h" 13 | 14 | void brake_init (void); 15 | BitStatus brake_is_set (void); 16 | 17 | #endif /* _BRAKE_H */ 18 | -------------------------------------------------------------------------------- /firmware/clean.bat: -------------------------------------------------------------------------------- 1 | cd stdperiphlib\src 2 | del *.asm 3 | del *.rel 4 | del *.lk 5 | del *.lst 6 | del *.rst 7 | del *.sym 8 | del *.cdb 9 | del *.map 10 | del *.elf 11 | del *.bin 12 | cd.. 13 | cd.. -------------------------------------------------------------------------------- /firmware/config-example.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #ifndef CONFIG_H_ 10 | #define CONFIG_H_ 11 | 12 | // This file is the firmware configuration for the following hardware combination: 13 | // - throttle and PAS; using motor current/torque and/or wheel speed controller 14 | // - PAS with 8 magnets and with configured max cadence of 90 RPM 15 | // - LCD3 16 | // - battery pack Li-ion 24V 17 | // - BMSBattery S06S 24/36V motor controller 18 | // - BMSBattery Q85 motor 328RPM (direct drive motor) 19 | // 20 | // You should adapt the configurations for your hardware combination, by commenting/uncommenting the needed lines and values 21 | 22 | 23 | // *************************************************************************** // 24 | // THROTTLE 25 | // 26 | // choose between throttle and/or pas or torque sensor 27 | #define EBIKE_THROTTLE_TYPE EBIKE_THROTTLE_TYPE_THROTTLE_PAS 28 | //#define EBIKE_THROTTLE_TYPE EBIKE_THROTTLE_TYPE_TORQUE_SENSOR 29 | 30 | // next, choose one of the both (only apply to throttle and/or PAS) 31 | //#define EBIKE_THROTTLE_TYPE_THROTTLE_PAS_PWM_DUTY_CYCLE // direct PWM duty_cycle control 32 | #define EBIKE_THROTTLE_TYPE_THROTTLE_PAS_CURRENT_SPEED // control using motor current/torque and/or wheel speed 33 | 34 | // in the case of EBIKE_THROTTLE_TYPE = EBIKE_THROTTLE_TYPE_THROTTLE_PAS, LCD assist level will apply only to PAS 35 | #define EBIKE_THROTTLE_TYPE_THROTTLE_PAS_ASSIST_LEVEL_PAS_ONLY 36 | 37 | // next, if enabled, output of torque sensor algorithm is the human power (torque * cadence) other way will be the same as the torque signal 38 | // (only apply to torque sensor) 39 | #define EBIKE_THROTTLE_TYPE_TORQUE_SENSOR_HUMAN_POWER 40 | 41 | // PAS configurations 42 | // 43 | #define PAS_NUMBER_MAGNETS 8 44 | #define PAS_MAX_CADENCE_RPM 90 45 | 46 | #define PAS_THRESHOLD 1.7 47 | 48 | // if your motor accelerates while rotating backwards the pedals instead of rotating forward, select the other option 49 | #define PAS_DIRECTION PAS_DIRECTION_LEFT 50 | //#define PAS_DIRECTION PAS_DIRECTION_RIGHT 51 | 52 | // this is an advanced feature, please read bout it here: 53 | // https://opensourceebikefirmware.bitbucket.io/development/Various--2017.12.01_-_Regen_ebrake_like_coast_brakes.html 54 | //#define EBIKE_REGEN_EBRAKE_LIKE_COAST_BRAKES 55 | // *************************************************************************** // 56 | 57 | // *************************************************************************** // 58 | // LCD 59 | 60 | // the firmware supports automatically both LCD3 and LCD5 61 | 62 | // you can tune LCD assist level 63 | #define ASSIST_LEVEL_0 0.00 64 | #define ASSIST_LEVEL_1 0.44 65 | #define ASSIST_LEVEL_2 0.66 66 | #define ASSIST_LEVEL_3 1.00 67 | #define ASSIST_LEVEL_4 1.5 68 | #define ASSIST_LEVEL_5 2.25 69 | 70 | // S06S motor controller: 1.50 71 | // KT36/48SVPR: ?? 72 | #define LCD_BATTERY_CURRENT_FACTOR 1.50 // each unit = 0.35A | in this example, 42 * 0.35 ~= 15 amps 73 | // *************************************************************************** // 74 | 75 | 76 | // *************************************************************************** // 77 | // BATTERY 78 | // Choose your battery pack voltage 79 | 80 | // the cells number can be a custom value that the controller supports, like 8 for 8S battery pack 81 | #define BATTERY_LI_ION_CELLS_NUMBER 7 // 7S = 24V battery pack 82 | //#define BATTERY_LI_ION_CELLS_NUMBER 10 // 10S = 36V battery pack 83 | //#define BATTERY_LI_ION_CELLS_NUMBER 13 // 13S = 48V battery pack 84 | 85 | #define ADC_BATTERY_CURRENT_MAX 42 // each unit = 0.35A | in this example, 42 * 0.35 ~= 15 amps 86 | #define ADC_BATTERY_REGEN_CURRENT_MAX 5 // CAN'T be more than 66 units!! each unit = 0.35A 87 | 88 | // Considering the follow voltage values for each li-ion battery cell 89 | // State of charge | voltage 90 | #define LI_ION_CELL_VOLTS_MAX 4.25 // this value is used to protect battery from overcharge on regeneration mode, mainly for direct drive motors 91 | #define LI_ION_CELL_VOLTS_100 4.06 // this value is used to help finding the battery SOC 92 | #define LI_ION_CELL_VOLTS_80 3.93 // this value is used to help finding the battery SOC 93 | #define LI_ION_CELL_VOLTS_60 3.78 // this value is used to help finding the battery SOC 94 | #define LI_ION_CELL_VOLTS_40 3.60 // this value is used to help finding the battery SOC 95 | #define LI_ION_CELL_VOLTS_20 3.38 // this value is used to help finding the battery SOC 96 | #define LI_ION_CELL_VOLTS_10 3.25 // this value is used to help finding the battery SOC 97 | #define LI_ION_CELL_VOLTS_0 3.00 // this value is used to help finding the battery SOC and is the minimum value after which the firmware don't ask more current to battery, to run the motor 98 | // *************************************************************************** // 99 | 100 | // *************************************************************************** // 101 | // MOTOR CONTROLLER 102 | 103 | // Choose your controller max current and max regen current 104 | // 105 | // S06S controller holds 15 amps as max current 106 | #define ADC_MOTOR_CURRENT_MAX 43 // each unit = 0.35A; 43 = 15A (tested on S06S motor controller) 107 | #define ADC_MOTOR_REGEN_CURRENT_MAX 28 // CAN'T be more than 66 units!! each unit = 0.35A; 20 = 10A but the brake/regen must be only for a few seconds!! 108 | 109 | // Choose PWM ramp up/down step (higher value will make the motor acceleration slower) 110 | // 111 | // For a 24V battery, 25 for ramp up seems ok. For an higher voltage battery, this values should be higher 112 | #define PWM_DUTY_CYCLE_RAMP_UP_INVERSE_STEP 25 113 | #define PWM_DUTY_CYCLE_RAMP_DOWN_INVERSE_STEP 10 114 | // *************************************************************************** // 115 | 116 | // *************************************************************************** // 117 | // MOTOR 118 | 119 | // Choose some parameters for your motor (if you don't know, just keep the following original values because they should work ok) 120 | // 121 | // This value should be near 0. When measured with an oscilloscope, for a Q85 motor, the offset is 5 122 | // You can try to tune with the whell on the air, full throttle and look at batttery current: adjust for lower battery current 123 | #define MOTOR_ROTOR_OFFSET_ANGLE 0 124 | 125 | #define MOTOR_ROTOR_ANGLE_STARTUP 0 126 | 127 | // This value is ERPS speed after which a transition happens from sinewave no interpolation to have 128 | // interpolation 60 degrees and must be found experimentally but a value of 25 may be good 129 | #define MOTOR_ROTOR_ERPS_START_INTERPOLATION_60_DEGREES 25 130 | 131 | // For some motors with not very well placed mosfets at 120 degrees between each of them. May be easier to keep this option disabled 132 | //#define DO_SINEWAVE_INTERPOLATION_360_DEGREES 133 | // *************************************************************************** // 134 | 135 | #endif /* CONFIG_H_ */ 136 | -------------------------------------------------------------------------------- /firmware/config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * config.h 3 | * 4 | * Automatically created by Flexible OpenSource firmware - Configuration tool 5 | * Author: stancecoke 6 | * Author: casainho 7 | */ 8 | 9 | #ifndef _CONFIG_H_ 10 | #define _CONFIG_H_ 11 | 12 | #define EBIKE_THROTTLE_TYPE EBIKE_THROTTLE_TYPE_TORQUE_SENSOR 13 | #define EBIKE_THROTTLE_TYPE_THROTTLE_PAS_CURRENT_SPEED 14 | #define EBIKE_THROTTLE_TYPE_THROTTLE_PAS_ASSIST_LEVEL_PAS_ONLY 15 | #define PAS_NUMBER_MAGNETS 12 16 | #define PAS_MAX_CADENCE_RPM 80 17 | #define PAS_THRESHOLD 1.7 18 | #define PAS_DIRECTION PAS_DIRECTION_RIGHT 19 | #define ASSIST_LEVEL_0 0.0 20 | #define ASSIST_LEVEL_1 0.25 21 | #define ASSIST_LEVEL_2 0.5 22 | #define ASSIST_LEVEL_3 1.0 23 | #define ASSIST_LEVEL_4 2.0 24 | #define ASSIST_LEVEL_5 4.0 25 | #define LCD_BATTERY_CURRENT_FACTOR 1.50 26 | #define BATTERY_LI_ION_CELLS_NUMBER 7 27 | #define ADC_BATTERY_CURRENT_MAX 35 28 | #define ADC_BATTERY_REGEN_CURRENT_MAX 35 29 | #define MOTOR_ROTOR_OFFSET_ANGLE 0 30 | #define MOTOR_ROTOR_ERPS_START_INTERPOLATION_60_DEGREES 20 31 | #define ADC_MOTOR_CURRENT_MAX 120 32 | #define ADC_MOTOR_REGEN_CURRENT_MAX 66 33 | #define PWM_DUTY_CYCLE_RAMP_UP_INVERSE_STEP 35 34 | #define PWM_DUTY_CYCLE_RAMP_DOWN_INVERSE_STEP 35 35 | #define LI_ION_CELL_VOLTS_MAX 4.25 36 | #define LI_ION_CELL_VOLTS_100 4.06 37 | #define LI_ION_CELL_VOLTS_80 3.93 38 | #define LI_ION_CELL_VOLTS_60 3.78 39 | #define LI_ION_CELL_VOLTS_40 3.60 40 | #define LI_ION_CELL_VOLTS_20 3.38 41 | #define LI_ION_CELL_VOLTS_10 3.25 42 | #define LI_ION_CELL_VOLTS_0 3.00 43 | 44 | #endif /* _CONFIG_H_ */ 45 | -------------------------------------------------------------------------------- /firmware/docs/Tutorial for seting up the toolchain in windows environment.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/docs/Tutorial for seting up the toolchain in windows environment.pdf -------------------------------------------------------------------------------- /firmware/docs/firmware_structure.odg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/docs/firmware_structure.odg -------------------------------------------------------------------------------- /firmware/docs/firmware_structure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/docs/firmware_structure.png -------------------------------------------------------------------------------- /firmware/ebike_app.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EGG OpenSource EBike firmware 3 | * 4 | * Copyright (C) Casainho, 2015, 2106, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #ifndef _EBIKE_APP_H_ 10 | #define _EBIKE_APP_H_ 11 | 12 | #include 13 | #include "main.h" 14 | 15 | #define EBIKE_APP_STATE_MOTOR_COAST 0 16 | #define EBIKE_APP_STATE_MOTOR_STOP 1 17 | #define EBIKE_APP_STATE_MOTOR_STARTUP 2 18 | #define EBIKE_APP_STATE_MOTOR_COOL 3 19 | #define EBIKE_APP_STATE_MOTOR_RUNNING 4 20 | 21 | #define EBIKE_APP_ERROR_EMPTY 0x0 22 | #define EBIKE_APP_ERROR_01_THROTTLE 0x22 23 | #define EBIKE_APP_ERROR_02 0x23 24 | #define EBIKE_APP_ERROR_03 0x24 25 | #define EBIKE_APP_ERROR_04 0x26 26 | #define EBIKE_APP_ERROR_05 0x28 27 | #define EBIKE_APP_ERROR_06_SHORT_CIRCUIT 0x21 28 | #define EBIKE_APP_ERROR_91_BATTERY_UNDER_VOLTAGE 0x91 29 | 30 | typedef struct _lcd_configuration_variables 31 | { 32 | uint8_t ui8_assist_level; 33 | uint8_t ui8_motor_characteristic; 34 | uint8_t ui8_wheel_size; 35 | uint8_t ui8_max_speed; 36 | uint8_t ui8_power_assist_control_mode; 37 | uint8_t ui8_controller_max_current; 38 | } struct_lcd_configuration_variables; 39 | 40 | extern volatile uint16_t ui16_pas1_pwm_cycles_ticks; 41 | extern volatile uint16_t ui16_pas1_pwm_cycles_on_ticks; 42 | extern volatile uint8_t ui8_pas1_direction; 43 | extern uint8_t ui8_pas_flag; 44 | extern uint8_t PAS_act; 45 | 46 | extern volatile uint8_t ui8_ebike_app_state; 47 | 48 | extern volatile uint16_t ui16_wheel_speed_sensor_pwm_cycles_ticks; 49 | extern volatile uint8_t ui8_wheel_speed_sensor_is_disconnected; 50 | 51 | extern uint8_t ui8_throttle_value; 52 | extern uint8_t ui8_adc_throttle_value; 53 | extern volatile uint16_t ui16_throttle_value_accumulated; 54 | 55 | extern uint16_t ui16_adc_battery_current_offset_10b; 56 | extern volatile uint8_t ui8_adc_target_battery_current_max; 57 | extern volatile uint8_t ui8_adc_target_battery_regen_current_max; 58 | 59 | extern volatile uint8_t ui8_log_pi_battery_current_value; 60 | extern volatile uint8_t ui8_log_pi_battery_target_current_value; 61 | extern volatile uint8_t ui8_adc_battery_current_offset; 62 | 63 | extern uint8_t ui8_adc_throttle_offset; 64 | extern uint16_t ui16_adc_throttle_offset; 65 | 66 | void ebike_app_init (void); 67 | void ebike_app_controller (void); 68 | void ebike_app_cruise_control_stop (void); 69 | uint8_t ebike_app_get_adc_throttle_value_cruise_control (void); 70 | struct_lcd_configuration_variables *ebike_app_get_lcd_configuration_variables (void); 71 | uint8_t ebike_app_throttle_is_released (void); 72 | uint8_t ui8_ebike_app_get_wheel_speed (void); 73 | void ebike_app_set_state (uint8_t ui8_state); 74 | void ebike_app_set_error (uint8_t error); 75 | void ebike_app_clear_error (void); 76 | uint8_t ebike_app_get_error (void); 77 | void ebike_app_battery_set_regen_current_max (uint8_t ui8_value); 78 | void battery_protect_over_voltage (void); 79 | 80 | #endif /* _EBIKE_APP_H_ */ 81 | -------------------------------------------------------------------------------- /firmware/eeprom.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #include 10 | #include "stm8s.h" 11 | #include "stm8s_flash.h" 12 | #include "eeprom.h" 13 | #include "ebike_app.h" 14 | 15 | void eeprom_read_values_to_variables (void); 16 | void eeprom_read_values_to_variables (void); 17 | void eeprom_write_array (uint8_t *array_values); 18 | 19 | void eeprom_init (void) 20 | { 21 | uint8_t array_default_values [7] = { 22 | KEY, 23 | DEFAULT_VALUE_ASSIST_LEVEL, 24 | DEFAULT_VALUE_MOTOR_CHARACTARISTIC, 25 | DEFAULT_VALUE_WHEEL_SIZE, 26 | DEFAULT_VALUE_MAX_SPEED, 27 | DEFAULT_VALUE_POWER_ASSIST_CONTROL_MODE, 28 | DEFAULT_VALUE_CONTROLLER_MAX_CURRENT 29 | }; 30 | uint8_t ui8_data; 31 | 32 | // start by reading address 0 and see if value is different from our key, 33 | // if so mean that eeprom memory is clean and we need to populate: should happen after erasing the microcontroller 34 | ui8_data = FLASH_ReadByte (ADDRESS_KEY); 35 | if (ui8_data != KEY) // verify if our key exist 36 | { 37 | eeprom_write_array (array_default_values); 38 | eeprom_read_values_to_variables (); 39 | } 40 | else // values on eeprom memory should be ok, now use them 41 | { 42 | eeprom_read_values_to_variables (); 43 | } 44 | } 45 | 46 | void eeprom_read_values_to_variables (void) 47 | { 48 | struct_lcd_configuration_variables *p_lcd_configuration_variables = ebike_app_get_lcd_configuration_variables (); 49 | 50 | p_lcd_configuration_variables->ui8_assist_level = FLASH_ReadByte (ADDRESS_ASSIST_LEVEL); 51 | p_lcd_configuration_variables->ui8_motor_characteristic = FLASH_ReadByte (ADDRESS_MOTOR_CHARACTARISTIC); 52 | p_lcd_configuration_variables->ui8_wheel_size = FLASH_ReadByte (ADDRESS_WHEEL_SIZE); 53 | p_lcd_configuration_variables->ui8_max_speed = FLASH_ReadByte (ADDRESS_MAX_SPEED); 54 | p_lcd_configuration_variables->ui8_power_assist_control_mode = FLASH_ReadByte (ADDRESS_POWER_ASSIST_CONTROL_MODE); 55 | p_lcd_configuration_variables->ui8_controller_max_current = FLASH_ReadByte (ADDRESS_CONTROLLER_MAX_CURRENT); 56 | } 57 | 58 | void eeprom_write_if_values_changed (void) 59 | { 60 | struct_lcd_configuration_variables *p_lcd_configuration_variables = ebike_app_get_lcd_configuration_variables (); 61 | static uint8_t array_values [7]; 62 | 63 | // see if the values differ from the ones on EEPROM and if so, write all of them to EEPROM 64 | if ((p_lcd_configuration_variables->ui8_assist_level != FLASH_ReadByte (ADDRESS_ASSIST_LEVEL)) || 65 | (p_lcd_configuration_variables->ui8_motor_characteristic != FLASH_ReadByte (ADDRESS_MOTOR_CHARACTARISTIC)) || 66 | (p_lcd_configuration_variables->ui8_wheel_size != FLASH_ReadByte (ADDRESS_WHEEL_SIZE)) || 67 | (p_lcd_configuration_variables->ui8_max_speed != FLASH_ReadByte (ADDRESS_MAX_SPEED)) || 68 | (p_lcd_configuration_variables->ui8_power_assist_control_mode != FLASH_ReadByte (ADDRESS_POWER_ASSIST_CONTROL_MODE)) || 69 | (p_lcd_configuration_variables->ui8_controller_max_current != FLASH_ReadByte (ADDRESS_CONTROLLER_MAX_CURRENT))) 70 | { 71 | array_values [0] = KEY; 72 | array_values [1] = p_lcd_configuration_variables->ui8_assist_level; 73 | array_values [2] = p_lcd_configuration_variables->ui8_motor_characteristic; 74 | array_values [3] = p_lcd_configuration_variables->ui8_wheel_size; 75 | array_values [4] = p_lcd_configuration_variables->ui8_max_speed; 76 | array_values [5] = p_lcd_configuration_variables->ui8_power_assist_control_mode; 77 | array_values [6] = p_lcd_configuration_variables->ui8_controller_max_current; 78 | 79 | eeprom_write_array (array_values); 80 | } 81 | } 82 | 83 | void eeprom_write_array (uint8_t *array_values) 84 | { 85 | uint8_t ui8_i; 86 | 87 | FLASH_SetProgrammingTime (FLASH_PROGRAMTIME_STANDARD); 88 | FLASH_Unlock (FLASH_MEMTYPE_DATA); 89 | while (!FLASH_GetFlagStatus (FLASH_FLAG_DUL)) ; 90 | 91 | for (ui8_i = 0; ui8_i < 7; ui8_i++) 92 | { 93 | FLASH_ProgramByte (EEPROM_BASE_ADDRESS + ui8_i, *array_values++); 94 | while (!FLASH_GetFlagStatus (FLASH_FLAG_EOP)) ; 95 | } 96 | FLASH_Lock (FLASH_MEMTYPE_DATA); 97 | } 98 | -------------------------------------------------------------------------------- /firmware/eeprom.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #ifndef _EEPROM_H_ 10 | #define _EEPROM_H_ 11 | 12 | #include "main.h" 13 | 14 | #define KEY 0xca 15 | 16 | #define EEPROM_BASE_ADDRESS 0x4000 17 | #define ADDRESS_KEY EEPROM_BASE_ADDRESS 18 | #define ADDRESS_ASSIST_LEVEL 1 + EEPROM_BASE_ADDRESS 19 | #define ADDRESS_MOTOR_CHARACTARISTIC 2 + EEPROM_BASE_ADDRESS 20 | #define ADDRESS_WHEEL_SIZE 3 + EEPROM_BASE_ADDRESS 21 | #define ADDRESS_MAX_SPEED 4 + EEPROM_BASE_ADDRESS 22 | #define ADDRESS_POWER_ASSIST_CONTROL_MODE 5 + EEPROM_BASE_ADDRESS 23 | #define ADDRESS_CONTROLLER_MAX_CURRENT 6 + EEPROM_BASE_ADDRESS 24 | 25 | void eeprom_init (void); 26 | void eeprom_write_if_values_changed (void); 27 | 28 | #endif /* _EEPROM_H_ */ 29 | -------------------------------------------------------------------------------- /firmware/gpio.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #include "stm8s.h" 10 | #include "stm8s_gpio.h" 11 | #include "gpio.h" 12 | 13 | void gpio_init (void) 14 | { 15 | 16 | } 17 | 18 | void debug_pin_init (void) 19 | { 20 | GPIO_Init(DEBUG__PORT, 21 | DEBUG__PIN, 22 | GPIO_MODE_OUT_PP_HIGH_FAST); 23 | } 24 | 25 | void debug_pin_set (void) 26 | { 27 | GPIO_WriteHigh(DEBUG__PORT, DEBUG__PIN); 28 | } 29 | 30 | void debug_pin_reset (void) 31 | { 32 | GPIO_WriteLow(DEBUG__PORT, DEBUG__PIN); 33 | } 34 | 35 | 36 | -------------------------------------------------------------------------------- /firmware/gpio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | /* Connections: 10 | * 11 | * Motor PHASE_A: yellow wire 12 | * Motor PHASE_B: green wire 13 | * Motor PHASE_C: blue wire 14 | * 15 | * 16 | * PIN | IN/OUT|Function 17 | * ---------------------------------------------------------- 18 | * PB5 (ADC_AIN5) | in | phase_B_current 19 | * PB6 (ADC_AIN6) | in | motor_total_current 20 | * PE7 (ADC_AIN8) | in | motor_total_current_filtered 21 | * PD7 | in | motor_total_over_current 22 | * 23 | * PE6 (ADC_AIN9) | in | battery_voltage 24 | * 25 | * PE0 | in | Hall_sensor_A 26 | * PE1 | in | Hall_sensor_B 27 | * PE2 | in | Hall_sensor_C 28 | * 29 | * PB2 (TIM1_CH3N) | out | PWM_phase_A_low 30 | * PB1 (TIM1_CH2N) | out | PWM_phase_B_low 31 | * PB0 (TIM1_CH1N) | out | PWM_phase_C_low 32 | * PC3 (TIM1_CH3) | out | PWM_phase_A_high 33 | * PC2 (TIM1_CH2) | out | PWM_phase_B_high 34 | * PC1 (TIM1_CH1) | out | PWM_phase_C_high 35 | * 36 | * PD5 (UART2_TX) | out | usart_tx 37 | * PD6 (UART2_RX) | out | usart_rx 38 | * 39 | * PA4 | in | brake 40 | * PB4 (ADC_AIN4) | in | throttle 41 | * PD0 | in | PAS1 42 | * PB7 | in | PAS2 43 | * PC5 | in | wheel speed 44 | * 45 | * 46 | */ 47 | 48 | #ifndef _GPIO_H_ 49 | #define _GPIO_H_ 50 | 51 | #include "main.h" 52 | #include "stm8s_gpio.h" 53 | 54 | #define PHASE_B_CURRENT__PIN GPIO_PIN_5 55 | #define CURRENT_PHASE_B__PORT GPIOB 56 | #define MOTOR_CURRENT__PIN GPIO_PIN_6 57 | #define CURRENT_MOTOR_TOTAL__PORT GPIOB 58 | #define BATTERY_CURRENT__PIN GPIO_PIN_7 59 | #define CURRENT_MOTOR_TOTAL_FILTRED__PORT GPIOE 60 | #define CURRENT_MOTOR_TOTAL_OVER__PIN GPIO_PIN_7 61 | #define CURRENT_MOTOR_TOTAL_OVER__PORT GPIOD 62 | 63 | #define BATTERY_VOLTAGE__PIN GPIO_PIN_6 64 | #define BATTERY_VOLTAGE__PORT GPIOE 65 | 66 | #define HALL_SENSOR_A__PIN GPIO_PIN_0 67 | #define HALL_SENSOR_B__PIN GPIO_PIN_1 68 | #define HALL_SENSOR_C__PIN GPIO_PIN_2 69 | 70 | #define HALL_SENSOR_A__PORT GPIOE 71 | #define HALL_SENSOR_B__PORT GPIOE 72 | #define HALL_SENSOR_C__PORT GPIOE 73 | #define HALL_SENSORS__PORT GPIOE 74 | #define HALL_SENSORS_MASK (HALL_SENSOR_A__PIN | HALL_SENSOR_B__PIN | HALL_SENSOR_C__PIN) 75 | 76 | #define PMW_PHASE_A_LOW__PIN GPIO_PIN_2 77 | #define PMW_PHASE_A_LOW__PORT GPIOB 78 | #define PMW_PHASE_B_LOW__PIN GPIO_PIN_1 79 | #define PMW_PHASE_B_LOW__PORT GPIOB 80 | #define PMW_PHASE_C_LOW__PIN GPIO_PIN_0 81 | #define PMW_PHASE_C_LOW__PORT GPIOB 82 | #define PMW_PHASE_A_HIGH__PIN GPIO_PIN_3 83 | #define PMW_PHASE_A_HIGH__PORT GPIOC 84 | #define PMW_PHASE_B_HIGH__PIN GPIO_PIN_2 85 | #define PMW_PHASE_B_HIGH__PORT GPIOC 86 | #define PMW_PHASE_C_HIGH__PIN GPIO_PIN_1 87 | #define PMW_PHASE_C_HIGH__PORT GPIOC 88 | 89 | #define UART2_TX__PIN GPIO_PIN_5 90 | #define UART2_TX__PORT GPIOD 91 | #define UART2_RX__PIN GPIO_PIN_6 92 | #define UART2_RX__PORT GPIOD 93 | 94 | #define BRAKE__PIN GPIO_PIN_4 95 | #define BRAKE__PORT GPIOA 96 | 97 | #define THROTTLE__PIN GPIO_PIN_4 98 | #define THROTTLE__PORT GPIOB 99 | 100 | #define PAS1__PIN GPIO_PIN_0 101 | #define PAS1__PORT GPIOD 102 | 103 | #define PAS2__PIN GPIO_PIN_7 104 | #define PAS2__PORT GPIOB 105 | 106 | #define WHEEL_SPEED_SENSOR__PIN GPIO_PIN_5 107 | #define WHEEL_SPEED_SENSOR__PORT GPIOC 108 | 109 | #define DEBUG__PIN GPIO_PIN_7 110 | #define DEBUG__PORT GPIOC 111 | 112 | 113 | void gpio_init (void); 114 | void debug_pin_init (void); 115 | void debug_pin_set (void); 116 | void debug_pin_reset (void); 117 | void PAS_init (void); 118 | void SPEED_init (void); 119 | 120 | #endif /* GPIO_H_ */ 121 | -------------------------------------------------------------------------------- /firmware/interrupts.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #ifndef _INTERRUPTS_H_ 10 | #define _INTERRUPTS_H_ 11 | 12 | #define EXTI_PORTA_IRQHANDLER 3 13 | #define EXTI_PORTC_IRQHANDLER 5 14 | #define EXTI_PORTD_IRQHANDLER 6 15 | #define EXTI_PORTE_IRQHANDLER 7 16 | #define TIM1_CAP_COM_IRQHANDLER 12 17 | #define TIM2_UPD_OVF_TRG_BRK_IRQHANDLER 13 18 | #define UART2_IRQHANDLER 21 19 | #define ADC1_IRQHANDLER 22 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /firmware/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #include 10 | #include 11 | #include "stm8s.h" 12 | #include "gpio.h" 13 | #include "stm8s_itc.h" 14 | #include "stm8s_gpio.h" 15 | #include "interrupts.h" 16 | #include "stm8s_tim2.h" 17 | #include "watchdog.h" 18 | #include "uart.h" 19 | #include "adc.h" 20 | #include "brake.h" 21 | #include "utils.h" 22 | #include "timers.h" 23 | #include "pwm.h" 24 | #include "config.h" 25 | #include "ebike_app.h" 26 | #include "eeprom.h" 27 | #include "motor.h" 28 | #include "pas.h" 29 | #include "wheel_speed_sensor.h" 30 | 31 | uint16_t ui16_TIM2_counter = 0; 32 | uint16_t ui16_ebike_app_controller_counter = 0; 33 | uint16_t ui16_debug_uart_counter = 0; 34 | 35 | ///////////////////////////////////////////////////////////////////////////////////////////// 36 | //// Functions prototypes 37 | 38 | // main -- start of firmware and main loop 39 | int main (void); 40 | 41 | // With SDCC, interrupt service routine function prototypes must be placed in the file that contains main () 42 | // in order for an vector for the interrupt to be placed in the the interrupt vector space. It's acceptable 43 | // to place the function prototype in a header file as long as the header file is included in the file that 44 | // contains main (). SDCC will not generate any warnings or errors if this is not done, but the vector will 45 | // not be in place so the ISR will not be executed when the interrupt occurs. 46 | 47 | // Calling a function from interrupt not always works, SDCC manual says to avoid it. Maybe the best is to put 48 | // all the code inside the interrupt 49 | 50 | // Local VS global variables 51 | // Sometimes I got the following error when compiling the firmware: motor.asm:750: Error: relocation error 52 | // when I have this code inside a function: "static uint8_t ui8_cruise_counter = 0;" 53 | // and the solution was define the variable as global instead 54 | // Another error example: 55 | // *** buffer overflow detected ***: sdcc terminated 56 | // Caught signal 6: SIGABRT 57 | 58 | // Brake signal interrupt 59 | void EXTI_PORTA_IRQHandler(void) __interrupt(EXTI_PORTA_IRQHANDLER); 60 | 61 | // Motor overcurrent interrupt 62 | void EXTI_PORTD_IRQHandler(void) __interrupt(EXTI_PORTD_IRQHANDLER); 63 | 64 | // Timer1/PWM period interrupt 65 | void TIM1_CAP_COM_IRQHandler(void) __interrupt(TIM1_CAP_COM_IRQHANDLER); 66 | 67 | // UART2 Receive interrupt 68 | void UART2_IRQHandler(void) __interrupt(UART2_IRQHANDLER); 69 | 70 | ///////////////////////////////////////////////////////////////////////////////////////////// 71 | ///////////////////////////////////////////////////////////////////////////////////////////// 72 | 73 | int main (void) 74 | { 75 | //set clock at the max 16MHz 76 | CLK_HSIPrescalerConfig (CLK_PRESCALER_HSIDIV1); 77 | 78 | gpio_init (); 79 | brake_init (); 80 | while (brake_is_set()) ; // hold here while brake is pressed -- this is a protection for development 81 | debug_pin_init (); 82 | timer2_init (); 83 | uart_init (); 84 | pwm_init_bipolar_4q (); 85 | hall_sensor_init (); 86 | adc_init (); 87 | battery_protect_over_voltage (); // will stop the motor if battery over voltage 88 | eeprom_init (); 89 | motor_init (); 90 | pas1_init (); 91 | pas2_init (); 92 | wheel_speed_sensor_init (); 93 | ebike_app_init (); 94 | enableInterrupts (); 95 | 96 | while (1) 97 | { 98 | // because of continue; at the end of each if code block that will stop the while (1) loop there, 99 | // the first if block code will have the higher priority over any others 100 | ui16_TIM2_counter = TIM2_GetCounter (); 101 | if ((ui16_TIM2_counter - ui16_ebike_app_controller_counter) > 100) // every 100ms 102 | { 103 | ui16_ebike_app_controller_counter = ui16_TIM2_counter; 104 | // ebike_app_controller() takes about 13ms (measured at 2018.03) 105 | ebike_app_controller (); 106 | continue; 107 | } 108 | 109 | #ifdef DEBUG_UART 110 | ui16_TIM2_counter = TIM2_GetCounter (); 111 | if ((ui16_TIM2_counter - ui16_debug_uart_counter) > 20) // every 20ms 112 | { 113 | ui16_debug_uart_counter = ui16_TIM2_counter; 114 | 115 | // sugestion: no more than 6 variables printed (takes about 3ms to printf 6 variables) 116 | printf ("%d,%d,%d,%d,%d\n", 117 | ui8_duty_cycle_target, 118 | ui8_duty_cycle, 119 | ui16_motor_get_motor_speed_erps(), 120 | UI8_ADC_BATTERY_CURRENT, 121 | ui8_angle_correction); 122 | continue; 123 | } 124 | #endif 125 | } 126 | 127 | return 0; 128 | } 129 | 130 | -------------------------------------------------------------------------------- /firmware/main.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #ifndef _MAIN_H_ 10 | #define _MAIN_H_ 11 | 12 | #include "config.h" 13 | 14 | //#define DEBUG_UART 15 | 16 | #define MOTOR_TYPE_Q85 1 17 | #define MOTOR_TYPE_Q100 2 18 | #define MOTOR_TYPE_Q11 3 19 | 20 | // Choose your motor type 21 | // 22 | // the following motors were tested, if you have a different motor, try to choose MOTOR_TYPE_Q85 23 | #define MOTOR_TYPE MOTOR_TYPE_Q85 // geared motor 24 | //#define MOTOR_TYPE MOTOR_TYPE_Q100 // geared motor 25 | //#define MOTOR_TYPE MOTOR_TYPE_Q11 // direct drive motor 26 | 27 | 28 | #define CONTROLLER_TYPE_S06S 1 29 | #define CONTROLLER_TYPE_S12S 2 30 | 31 | // Choose your controller type 32 | // 33 | #define CONTROLLER_TYPE CONTROLLER_TYPE_S06S 34 | //#define CONTROLLER_TYPE CONTROLLER_TYPE_S12S 35 | 36 | // *************************************************************************** // 37 | // Throotle and PAS 38 | 39 | #define EBIKE_THROTTLE_TYPE_THROTTLE_PAS 1 40 | #define EBIKE_THROTTLE_TYPE_TORQUE_SENSOR 2 41 | // *************************************************************************** // 42 | 43 | // enable to get Regen ebrake like coast brakes, read more here: 44 | // https://opensourceebikefirmware.bitbucket.io/development/Various--2017.12.01_-_Regen_ebrake_like_coast_brakes.html 45 | #if defined(EBIKE_REGEN_EBRAKE_LIKE_COAST_BRAKES) 46 | //#undef EBIKE_THROTTLE_TYPE 47 | //#define EBIKE_THROTTLE_TYPE EBIKE_THROTTLE_TYPE_TORQUE_SENSOR 48 | #define ADC_BATTERY_REGEN_CURRENT_MAX_1_5 (ADC_BATTERY_REGEN_CURRENT_MAX / 5) 49 | #endif 50 | 51 | // Phase current: max of +-15.5 amps 52 | // 512 --> 15.5 amps 53 | // 1 ADC increment --> 0.030A 54 | // RMS value --> max value * 0.707 55 | #define ADC_PHASE_B_CURRENT_ZERO_AMPS_FOC_MAX 125 // for phase B current sensor that outputs 2.5V at zero amps (ACS711) 56 | #define ADC_PHASE_B_CURRENT_ZERO_AMPS_FOC_MIN 123 // for phase B current sensor that outputs 2.5V at zero amps (ACS711) 57 | 58 | #if defined (DO_SINEWAVE_INTERPOLATION_360_DEGREES) 59 | // This value is ERPS speed after which a transition happens from sinewave 60 degrees to have 60 | // interpolation 360 degrees and must be found experimentally but a value of 100 may be good 61 | #define MOTOR_ROTOR_ERPS_START_INTERPOLATION_360_DEGREES 150 62 | #endif 63 | 64 | #if MOTOR_TYPE == MOTOR_TYPE_Q85 65 | #define PWM_CYCLES_COUNTER_MAX 3125 // 5 erps minimum speed; 1/5 = 200ms; 200ms/64us = 3125 66 | #elif MOTOR_TYPE == MOTOR_TYPE_Q100 67 | #define PWM_CYCLES_COUNTER_MAX 1041 // 15 erps minimum speed 68 | #endif 69 | 70 | #define PWM_CYCLES_SECOND 15625L // 1 / 64us(PWM period) 71 | 72 | #define PWM_DUTY_CYCLE_MAX 250 73 | #define PWM_DUTY_CYCLE_MIN 20 74 | #define MIDDLE_PWM_DUTY_CYCLE_MAX (PWM_DUTY_CYCLE_MAX/2) 75 | 76 | #define MOTOR_ROTOR_ANGLE_90 (63 + MOTOR_ROTOR_OFFSET_ANGLE) 77 | #define MOTOR_ROTOR_ANGLE_150 (106 + MOTOR_ROTOR_OFFSET_ANGLE) 78 | #define MOTOR_ROTOR_ANGLE_210 (148 + MOTOR_ROTOR_OFFSET_ANGLE) 79 | #define MOTOR_ROTOR_ANGLE_270 (191 + MOTOR_ROTOR_OFFSET_ANGLE) 80 | #define MOTOR_ROTOR_ANGLE_330 (233 + MOTOR_ROTOR_OFFSET_ANGLE) 81 | #define MOTOR_ROTOR_ANGLE_30 (20 + MOTOR_ROTOR_OFFSET_ANGLE) 82 | 83 | 84 | #define MOTOR_ROTOR_ANGLE_STARTUP 0 85 | 86 | // this value of 127 is equal to 180 degrees 87 | #define MOTOR_ROTOR_ANGLE_FOC (127 + MOTOR_ROTOR_OFFSET_ANGLE) 88 | 89 | 90 | #define MOTOR_OVER_SPEED_ERPS 520 // motor max speed, protection max value | 30 points for the sinewave at max speed 91 | 92 | #if CONTROLLER_TYPE == CONTROLLER_TYPE_S06S 93 | #define WHEEL_SPEED_PI_CONTROLLER_KP_DIVIDEND 100 94 | #define WHEEL_SPEED_PI_CONTROLLER_KP_DIVISOR 4 95 | #define WHEEL_SPEED_PI_CONTROLLER_KI_DIVIDEND 40 96 | #define WHEEL_SPEED_PI_CONTROLLER_KI_DIVISOR 6 97 | #elif CONTROLLER_TYPE == CONTROLLER_TYPE_S12S 98 | #define WHEEL_SPEED_PI_CONTROLLER_KP_DIVIDEND 100 99 | #define WHEEL_SPEED_PI_CONTROLLER_KP_DIVISOR 4 100 | #define WHEEL_SPEED_PI_CONTROLLER_KI_DIVIDEND 40 101 | #define WHEEL_SPEED_PI_CONTROLLER_KI_DIVISOR 6 102 | #endif 103 | 104 | // Possible values: 0, 1, 2, 3, 4, 5, 6 105 | // 0 equal to no filtering and no delay, higher values will increase filtering but will also add bigger delay 106 | #define THROTTLE_FILTER_COEFFICIENT 1 107 | #define ADC_THROTTLE_THRESHOLD 4 // value in ADC 8 bits step 108 | 109 | #define CRUISE_CONTROL_MIN 20 110 | 111 | // Max voltage value for throttle and torque sensor signals, in ADC 8 bits step 112 | // each ADC 8 bits step = (5V / 256) = 0.0195 113 | #if (EBIKE_THROTTLE_TYPE == EBIKE_THROTTLE_TYPE_THROTTLE_PAS) 114 | #define ADC_THROTTLE_MAX_VALUE 229 115 | #elif (EBIKE_THROTTLE_TYPE == EBIKE_THROTTLE_TYPE_TORQUE_SENSOR) 116 | #define ADC_THROTTLE_MAX_VALUE 183 117 | #endif 118 | 119 | // *************************************************************************** // 120 | // PAS 121 | #define PAS_DIRECTION_RIGHT 0 122 | #define PAS_DIRECTION_LEFT 1 123 | 124 | // (1/(150RPM/60)) / (PAS_NUMBER_MAGNETS * 0.000064) 125 | #define PAS_ABSOLUTE_MAX_CADENCE_PWM_CYCLE_TICKS (6250 / PAS_NUMBER_MAGNETS) // max hard limit to 150RPM PAS cadence 126 | #define PAS_ABSOLUTE_MIN_CADENCE_PWM_CYCLE_TICKS (156250 / PAS_NUMBER_MAGNETS) // min hard limit to 6RPM PAS cadence 127 | 128 | #define PAS_NUMBER_MAGNETS_X2 (PAS_NUMBER_MAGNETS * 2) 129 | // *************************************************************************** // 130 | 131 | // *************************************************************************** // 132 | // Wheel speed sensor 133 | #define WHEEL_SPEED_SENSOR_MAX_PWM_CYCLE_TICKS 135 // something like 200m/h with a 6'' wheel 134 | #define WHEEL_SPEED_SENSOR_MIN_PWM_CYCLE_TICKS 32767 // could be a bigger number but will make slow detecting wheel stopped 135 | // *************************************************************************** // 136 | 137 | // *************************************************************************** // 138 | // EEPROM memory variables default values 139 | #define DEFAULT_VALUE_ASSIST_LEVEL 2 140 | #define DEFAULT_VALUE_MOTOR_CHARACTARISTIC 202 // for Q85 motor (12.6 * 16) 141 | #define DEFAULT_VALUE_WHEEL_SIZE 20 // 26'' 142 | #define DEFAULT_VALUE_MAX_SPEED 25 143 | #define DEFAULT_VALUE_POWER_ASSIST_CONTROL_MODE 1 144 | #define DEFAULT_VALUE_CONTROLLER_MAX_CURRENT 10 145 | // *************************************************************************** // 146 | 147 | // *************************************************************************** // 148 | // BATTERY 149 | 150 | // ADC Battery voltage 151 | // 29.8V --> 110 (8bits ADC) 152 | // 22.1V --> 81 (8bits ADC) 153 | // 1 ADC step 8 bits --> 0.287 volts 154 | #if CONTROLLER_TYPE == CONTROLLER_TYPE_S06S 155 | #define ADC_BATTERY_VOLTAGE_PER_ADC_STEP 0.2652 // S06S controller | this value was found experimentaly, to beter represent the real value 156 | #define ADC_BATTERY_VOLTAGE_K 68 // S06S | 0.26252 << 8 157 | #elif CONTROLLER_TYPE == CONTROLLER_TYPE_S12S 158 | #define ADC_BATTERY_VOLTAGE_PER_ADC_STEP 0.2652 // S12S controller | this value was found experimentaly, to beter represent the real value 159 | #define ADC_BATTERY_VOLTAGE_K 68 // S12S | 0.26252 << 8 160 | #endif 161 | 162 | #define COMMUNICATIONS_BATTERY_VOLTAGE (uint8_t) (((float) BATTERY_LI_ION_CELLS_NUMBER) * 3.45) // example: 7S battery, should be = 24 163 | #define ADC_BATTERY_VOLTAGE_MAX (uint8_t) ((float) (BATTERY_LI_ION_CELLS_NUMBER * LI_ION_CELL_VOLTS_MAX) / ADC_BATTERY_VOLTAGE_PER_ADC_STEP) 164 | #define ADC_BATTERY_VOLTAGE_10 (uint8_t) ((float) (BATTERY_LI_ION_CELLS_NUMBER * LI_ION_CELL_VOLTS_10) / ADC_BATTERY_VOLTAGE_PER_ADC_STEP) 165 | #define ADC_BATTERY_VOLTAGE_MIN (uint8_t) ((float) (BATTERY_LI_ION_CELLS_NUMBER * LI_ION_CELL_VOLTS_0) / ADC_BATTERY_VOLTAGE_PER_ADC_STEP) 166 | 167 | #define BATTERY_PACK_VOLTS_100 ((float) LI_ION_CELL_VOLTS_100 * (BATTERY_LI_ION_CELLS_NUMBER << 8)) 168 | #define BATTERY_PACK_VOLTS_80 ((float) LI_ION_CELL_VOLTS_80 * (BATTERY_LI_ION_CELLS_NUMBER << 8)) 169 | #define BATTERY_PACK_VOLTS_60 ((float) LI_ION_CELL_VOLTS_60 * (BATTERY_LI_ION_CELLS_NUMBER << 8)) 170 | #define BATTERY_PACK_VOLTS_40 ((float) LI_ION_CELL_VOLTS_40 * (BATTERY_LI_ION_CELLS_NUMBER << 8)) 171 | #define BATTERY_PACK_VOLTS_20 ((float) LI_ION_CELL_VOLTS_20 * (BATTERY_LI_ION_CELLS_NUMBER << 8)) 172 | #define BATTERY_PACK_VOLTS_10 ((float) LI_ION_CELL_VOLTS_10 * (BATTERY_LI_ION_CELLS_NUMBER << 8)) 173 | #define BATTERY_PACK_VOLTS_0 ((float) LI_ION_CELL_VOLTS_0 * (BATTERY_LI_ION_CELLS_NUMBER << 8)) 174 | 175 | // Possible values: 0, 1, 2, 3, 4, 5, 6 176 | // 0 equal to no filtering and no delay, higher values will increase filtering but will also add bigger delay 177 | #define READ_BATTERY_CURRENT_FILTER_COEFFICIENT 3 178 | #define READ_BATTERY_VOLTAGE_FILTER_COEFFICIENT 5 179 | // *************************************************************************** // 180 | 181 | #endif // _MAIN_H_ 182 | -------------------------------------------------------------------------------- /firmware/motor.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EGG OpenSource EBike firmware 3 | * 4 | * Copyright (C) Casainho, 2015, 2106, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #ifndef _MOTOR_H_ 10 | #define _MOTOR_H_ 11 | 12 | #include 13 | 14 | // motor states 15 | #define BLOCK_COMMUTATION 1 16 | #define SINEWAVE_INTERPOLATION_60_DEGREES 2 17 | #define SINEWAVE_INTERPOLATION_360_DEGREES 3 18 | 19 | #define MOTOR_CONTROLLER_STATE_OK 1 20 | #define MOTOR_CONTROLLER_STATE_BRAKE 2 21 | #define MOTOR_CONTROLLER_STATE_BRAKE_LIKE_COAST_BRAKES 4 22 | #define MOTOR_CONTROLLER_STATE_OVER_CURRENT 8 23 | #define MOTOR_CONTROLLER_STATE_UNDER_VOLTAGE 16 24 | #define MOTOR_CONTROLLER_STATE_OVER_VOLTAGE 32 25 | #define MOTOR_CONTROLLER_STATE_THROTTLE_ERROR 64 26 | #define MOTOR_CONTROLLER_STATE_MOTOR_BLOCKED 128 27 | 28 | extern uint8_t ui8_motor_commutation_type; 29 | extern volatile uint8_t ui8_angle_correction; 30 | extern volatile uint8_t ui8_adc_motor_current; 31 | extern uint8_t ui8_adc_motor_current_offset; 32 | extern volatile uint8_t ui8_duty_cycle; 33 | extern uint8_t ui8_duty_cycle_target; 34 | extern volatile uint8_t ui8_duty_cycle; 35 | extern uint16_t ui16_PWM_cycles_counter_total; 36 | extern uint8_t ui8_pwm_duty_cycle_duty_cycle_controller; 37 | extern uint8_t ui8_pas_state; 38 | extern volatile uint8_t ui8_torque_sensor_throttle_processed_value; 39 | extern volatile uint8_t ui8_adc_target_motor_regen_current_max; 40 | extern volatile uint8_t ui8_adc_target_motor_current_max; 41 | 42 | /***************************************************************************************/ 43 | // Motor interface 44 | void hall_sensor_init (void); // must be called before using the motor 45 | void motor_init (void); // must be called before using the motor 46 | void motor_controller (void); 47 | void motor_enable_PWM (void); 48 | void motor_disable_PWM (void); 49 | void motor_set_pwm_duty_cycle_target (uint8_t value); 50 | void motor_set_current_max (uint8_t value); // steps of 0.25A each step 51 | void motor_set_regen_current_max (uint8_t value); // steps of 0.25A each step 52 | void motor_set_pwm_duty_cycle_ramp_up_inverse_step (uint16_t value); // each step = 64us 53 | void motor_set_pwm_duty_cycle_ramp_down_inverse_step (uint16_t value); // each step = 64us 54 | uint16_t ui16_motor_get_motor_speed_erps (void); 55 | void motor_controller_set_wheel_speed_max (uint8_t ui8_wheel_speed); 56 | uint16_t motor_get_er_PWM_ticks (void); // PWM ticks per electronic rotation 57 | void motor_controller_set_state (uint8_t state); 58 | void motor_controller_reset_state (uint8_t state); 59 | uint8_t motor_controller_get_state (void); 60 | uint8_t motor_controller_state_is_set (uint8_t state); 61 | void motor_controller_set_target_speed_erps (uint16_t ui16_erps); 62 | void motor_controller_set_speed_erps_max (uint16_t ui16_erps); 63 | uint16_t motor_controller_get_target_speed_erps_max (void); 64 | void motor_controller_set_target_current_10b (uint16_t ui16_current); 65 | void motor_controller_set_error (uint8_t ui8_error); 66 | void motor_controller_clear_error (void); 67 | uint8_t motor_controller_get_error (void); 68 | void motor_set_pwm_duty_cycle (uint8_t ui8_value); 69 | void motor_set_pwm_duty_cycle_target (uint8_t ui8_value); 70 | uint8_t motor_get_pwm_duty_cycle_target (void); 71 | int8_t motor_get_current_filtered_10b (void); 72 | void motor_reset_regen_ebrake_like_coast_brakes (void); 73 | /***************************************************************************************/ 74 | 75 | #endif /* _MOTOR_H_ */ 76 | -------------------------------------------------------------------------------- /firmware/optionbytes.hex: -------------------------------------------------------------------------------- 1 | :0148000000B7 2 | :0148010000B6 3 | :014803002094 4 | :0148050000B2 5 | :0148070000B0 6 | :0148090000AE 7 | :01480B0000AC 8 | :01480D0000AA 9 | :01487E000039 10 | :00000001FF 11 | -------------------------------------------------------------------------------- /firmware/pas.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #include 10 | #include "stm8s.h" 11 | #include "stm8s_it.h" 12 | #include "gpio.h" 13 | 14 | void pas1_init (void) 15 | { 16 | //PAS1 pin as external input pin 17 | GPIO_Init(PAS1__PORT, 18 | PAS1__PIN, 19 | GPIO_MODE_IN_PU_NO_IT); // input pull-up, no external interrupt 20 | } 21 | 22 | void pas2_init (void) 23 | { 24 | //PAS2 pin as external input pin interrupt 25 | GPIO_Init(PAS2__PORT, 26 | PAS2__PIN, 27 | GPIO_MODE_IN_PU_NO_IT); // input pull-up, no external interrupt 28 | } 29 | 30 | -------------------------------------------------------------------------------- /firmware/pas.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #ifndef _PAS_H_ 10 | #define _PAS_H_ 11 | 12 | #include "main.h" 13 | 14 | void pas1_init (void); 15 | void pas2_init (void); 16 | 17 | #endif /* _PAS_H_ */ 18 | -------------------------------------------------------------------------------- /firmware/pwm.c: -------------------------------------------------------------------------------- 1 | /* 2 | * EGG OpenSource EBike firmware 3 | * 4 | * Copyright (C) Casainho, 2015, 2106, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #include 10 | #include 11 | #include "interrupts.h" 12 | #include "stm8s_tim1.h" 13 | #include "gpio.h" 14 | #include "pwm.h" 15 | #include "motor.h" 16 | 17 | void pwm_init_bipolar_4q (void) 18 | { 19 | TIM1_TimeBaseInit(0, // TIM1_Prescaler = 0 20 | TIM1_COUNTERMODE_CENTERALIGNED1, 21 | (512 - 1), // clock = 16MHz; counter period = 1024; PWM freq = 16MHz / 1024 = 15.625kHz; 22 | //(BUT PWM center aligned mode needs twice the frequency) 23 | 1); // will fire the TIM1_IT_UPDATE at every PWM period cycle 24 | 25 | 26 | //#define DISABLE_PWM_CHANNELS_1_3 27 | 28 | TIM1_OC1Init(TIM1_OCMODE_PWM1, 29 | #ifdef DISABLE_PWM_CHANNELS_1_3 30 | TIM1_OUTPUTSTATE_DISABLE, 31 | TIM1_OUTPUTNSTATE_DISABLE, 32 | #else 33 | TIM1_OUTPUTSTATE_ENABLE, 34 | TIM1_OUTPUTNSTATE_ENABLE, 35 | #endif 36 | 255, // initial duty_cycle value 37 | TIM1_OCPOLARITY_HIGH, 38 | TIM1_OCNPOLARITY_LOW, 39 | TIM1_OCIDLESTATE_RESET, 40 | TIM1_OCNIDLESTATE_SET); 41 | 42 | TIM1_OC2Init(TIM1_OCMODE_PWM1, 43 | TIM1_OUTPUTSTATE_ENABLE, 44 | TIM1_OUTPUTNSTATE_ENABLE, 45 | 255, // initial duty_cycle value 46 | TIM1_OCPOLARITY_HIGH, 47 | TIM1_OCNPOLARITY_LOW, 48 | TIM1_OCIDLESTATE_RESET, 49 | TIM1_OCIDLESTATE_SET); 50 | 51 | TIM1_OC3Init(TIM1_OCMODE_PWM1, 52 | #ifdef DISABLE_PWM_CHANNELS_1_3 53 | TIM1_OUTPUTSTATE_DISABLE, 54 | TIM1_OUTPUTNSTATE_DISABLE, 55 | #else 56 | TIM1_OUTPUTSTATE_ENABLE, 57 | TIM1_OUTPUTNSTATE_ENABLE, 58 | #endif 59 | 255, // initial duty_cycle value 60 | TIM1_OCPOLARITY_HIGH, 61 | TIM1_OCNPOLARITY_LOW, 62 | TIM1_OCIDLESTATE_RESET, 63 | TIM1_OCNIDLESTATE_SET); 64 | 65 | // OC4 is being used only to fire interrupt at a specific time (middle of DC link current pulses) 66 | // OC4 is always ssyncronized with PWM 67 | TIM1_OC4Init(TIM1_OCMODE_PWM1, 68 | TIM1_OUTPUTSTATE_DISABLE, 69 | 285, // timming for interrupt firing (hand adjusted) 70 | TIM1_OCPOLARITY_HIGH, 71 | TIM1_OCIDLESTATE_RESET); 72 | 73 | // break, dead time and lock configuration 74 | TIM1_BDTRConfig(TIM1_OSSISTATE_ENABLE, 75 | TIM1_LOCKLEVEL_OFF, 76 | // hardware nees a dead time of 1us 77 | 16, // DTG = 0; dead time in 62.5 ns steps; 1us/62.5ns = 16 78 | TIM1_BREAK_DISABLE, 79 | TIM1_BREAKPOLARITY_LOW, 80 | TIM1_AUTOMATICOUTPUT_DISABLE); 81 | 82 | TIM1_ITConfig(TIM1_IT_CC4, ENABLE); 83 | TIM1_Cmd(ENABLE); // TIM1 counter enable 84 | TIM1_CtrlPWMOutputs(ENABLE); 85 | } 86 | -------------------------------------------------------------------------------- /firmware/pwm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EGG OpenSource EBike firmware 3 | * 4 | * Copyright (C) Casainho, 2015, 2106, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #ifndef _PWM_H 10 | #define _PWM_H 11 | 12 | void pwm_init_bipolar_4q (void); 13 | void pwm_phase_a_disable (void); 14 | void pwm_phase_a_enable_pwm (void); 15 | void pwm_phase_a_enable_low (void); 16 | void pwm_phase_b_disable (void); 17 | void pwm_phase_b_enable_pwm (void); 18 | void pwm_phase_b_enable_low (void); 19 | void pwm_phase_c_disable (void); 20 | void pwm_phase_c_enable_pwm (void); 21 | void pwm_phase_c_enable_low (void); 22 | 23 | #endif /* _PWM_H_ */ 24 | -------------------------------------------------------------------------------- /firmware/timers.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #include "stm8s.h" 10 | #include "stm8s_tim2.h" 11 | 12 | void timer2_init (void) 13 | { 14 | uint16_t ui16_i; 15 | 16 | // TIM2 Peripheral Configuration 17 | TIM2_DeInit(); 18 | TIM2_TimeBaseInit(TIM2_PRESCALER_16384, 0xffff); // each incremment at every ~1ms 19 | TIM2_Cmd(ENABLE); // TIM2 counter enable 20 | 21 | // IMPORTANT: this software delay is needed so timer2 work after this 22 | for(ui16_i = 0; ui16_i < (29000); ui16_i++) { ; } 23 | } 24 | -------------------------------------------------------------------------------- /firmware/timers.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #ifndef _TIMERS_H_ 10 | #define _TIMERS_H_ 11 | 12 | void timer2_init (void); 13 | 14 | #endif /* _TIMERS_H_ */ 15 | -------------------------------------------------------------------------------- /firmware/tools/60-st_link_v2.rules: -------------------------------------------------------------------------------- 1 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3748", MODE="0666" 2 | -------------------------------------------------------------------------------- /firmware/tools/BLDC_SPWM_Lookup_tables.ods: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/tools/BLDC_SPWM_Lookup_tables.ods -------------------------------------------------------------------------------- /firmware/tools/KT-LCD data packet CRC calc.ods: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/tools/KT-LCD data packet CRC calc.ods -------------------------------------------------------------------------------- /firmware/tools/OSEC.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/tools/OSEC.java -------------------------------------------------------------------------------- /firmware/tools/cygwin/bin/cyggcc_s-seh-1.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/tools/cygwin/bin/cyggcc_s-seh-1.dll -------------------------------------------------------------------------------- /firmware/tools/cygwin/bin/cygiconv-2.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/tools/cygwin/bin/cygiconv-2.dll -------------------------------------------------------------------------------- /firmware/tools/cygwin/bin/cygintl-8.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/tools/cygwin/bin/cygintl-8.dll -------------------------------------------------------------------------------- /firmware/tools/cygwin/bin/cygncursesw-10.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/tools/cygwin/bin/cygncursesw-10.dll -------------------------------------------------------------------------------- /firmware/tools/cygwin/bin/cygreadline7.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/tools/cygwin/bin/cygreadline7.dll -------------------------------------------------------------------------------- /firmware/tools/cygwin/bin/cygstdc++-6.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/tools/cygwin/bin/cygstdc++-6.dll -------------------------------------------------------------------------------- /firmware/tools/cygwin/bin/cygwin1.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/tools/cygwin/bin/cygwin1.dll -------------------------------------------------------------------------------- /firmware/tools/cygwin/bin/libiconv2.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/tools/cygwin/bin/libiconv2.dll -------------------------------------------------------------------------------- /firmware/tools/cygwin/bin/libintl3.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/tools/cygwin/bin/libintl3.dll -------------------------------------------------------------------------------- /firmware/tools/cygwin/bin/make.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/tools/cygwin/bin/make.exe -------------------------------------------------------------------------------- /firmware/tools/cygwin/bin/sh.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/tools/cygwin/bin/sh.exe -------------------------------------------------------------------------------- /firmware/tools/cygwin/tmp/dummy_file.txt: -------------------------------------------------------------------------------- 1 | This folder can't be empty -------------------------------------------------------------------------------- /firmware/tools/motor_profile.ods: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware/tools/motor_profile.ods -------------------------------------------------------------------------------- /firmware/tools/openocd-v0.10.0-scripts/stlink-v2.cfg: -------------------------------------------------------------------------------- 1 | # 2 | # STMicroelectronics ST-LINK/V2 in-circuit debugger/programmer 3 | # 4 | 5 | interface hla 6 | hla_layout stlink 7 | hla_device_desc "ST-LINK/V2" 8 | hla_vid_pid 0x0483 0x3748 9 | 10 | # Optionally specify the serial number of ST-LINK/V2 usb device. ST-LINK/V2 11 | # devices seem to have serial numbers with unreadable characters. ST-LINK/V2 12 | # firmware version >= V2.J21.S4 recommended to avoid issues with adapter serial 13 | # number reset issues. 14 | # eg. 15 | #hla_serial "\xaa\xbc\x6e\x06\x50\x75\xff\x55\x17\x42\x19\x3f" 16 | 17 | -------------------------------------------------------------------------------- /firmware/tools/openocd-v0.10.0-scripts/stm8s003.cfg: -------------------------------------------------------------------------------- 1 | # script for stm8 family 2 | 3 | # 4 | # stm8 devices support SWIM transports only. 5 | # 6 | 7 | transport select stlink_swim 8 | 9 | if { [info exists CHIPNAME] } { 10 | set _CHIPNAME $CHIPNAME 11 | } else { 12 | set _CHIPNAME stm8 13 | } 14 | 15 | # Work-area is a space in RAM used for flash programming 16 | # By default use 1kB 17 | if { [info exists WORKAREASIZE] } { 18 | set _WORKAREASIZE $WORKAREASIZE 19 | } else { 20 | set _WORKAREASIZE 0x400 21 | } 22 | 23 | if { [info exists FLASHSTART] } { 24 | set _FLASHSTART $FLASHSTART 25 | } else { 26 | set _FLASHSTART 0x8000 27 | } 28 | 29 | if { [info exists FLASHEND] } { 30 | set _FLASHEND $FLASHEND 31 | } else { 32 | set _FLASHEND 0xffff 33 | } 34 | 35 | if { [info exists EEPROMSTART] } { 36 | set _EEPROMSTART $EEPROMSTART 37 | } else { 38 | set _EEPROMSTART 0x4000 39 | } 40 | 41 | if { [info exists EEPROMEND] } { 42 | set _EEPROMEND $EEPROMEND 43 | } else { 44 | set _EEPROMEND 0x43ff 45 | } 46 | 47 | if { [info exists OPTIONSTART] } { 48 | set _OPTIONSTART $OPTIONSTART 49 | } else { 50 | set _OPTIONSTART 0x4800 51 | } 52 | 53 | if { [info exists OPTIONEND] } { 54 | set _OPTIONEND $OPTIONEND 55 | } else { 56 | set _OPTIONEND 0x487f 57 | } 58 | 59 | if { [info exists BLOCKSIZE] } { 60 | set _BLOCKSIZE $BLOCKSIZE 61 | } else { 62 | set _BLOCKSIZE 0x40 63 | } 64 | 65 | hla newtap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id 0 66 | 67 | set _TARGETNAME $_CHIPNAME.cpu 68 | 69 | target create $_TARGETNAME stm8 -chain-position stm8.cpu 70 | 71 | $_TARGETNAME configure -work-area-phys 0x0 -work-area-size $_WORKAREASIZE -work-area-backup 1 72 | $_TARGETNAME configure -flashstart $_FLASHSTART -flashend $_FLASHEND -eepromstart $_EEPROMSTART -eepromend $_EEPROMEND -optionstart $_OPTIONSTART -optionend $_EEPROMEND -blocksize $_BLOCKSIZE 73 | 74 | if { [info exists ENABLE_STEP_IRQ] } { 75 | $_TARGETNAME configure -enable_step_irq 76 | } 77 | 78 | adapter_khz 0 79 | 80 | reset_config srst_only 81 | 82 | -------------------------------------------------------------------------------- /firmware/tools/openocd-v0.10.0-scripts/stm8s105.cfg: -------------------------------------------------------------------------------- 1 | # script for stm8 family 2 | 3 | # 4 | # stm8 devices support SWIM transports only. 5 | # 6 | 7 | transport select stlink_swim 8 | 9 | if { [info exists CHIPNAME] } { 10 | set _CHIPNAME $CHIPNAME 11 | } else { 12 | set _CHIPNAME stm8 13 | } 14 | 15 | # Work-area is a space in RAM used for flash programming 16 | # By default use 1kB 17 | if { [info exists WORKAREASIZE] } { 18 | set _WORKAREASIZE $WORKAREASIZE 19 | } else { 20 | set _WORKAREASIZE 0x400 21 | } 22 | 23 | if { [info exists FLASHSTART] } { 24 | set _FLASHSTART $FLASHSTART 25 | } else { 26 | set _FLASHSTART 0x8000 27 | } 28 | 29 | if { [info exists FLASHEND] } { 30 | set _FLASHEND $FLASHEND 31 | } else { 32 | set _FLASHEND 0xffff 33 | } 34 | 35 | if { [info exists EEPROMSTART] } { 36 | set _EEPROMSTART $EEPROMSTART 37 | } else { 38 | set _EEPROMSTART 0x4000 39 | } 40 | 41 | if { [info exists EEPROMEND] } { 42 | set _EEPROMEND $EEPROMEND 43 | } else { 44 | set _EEPROMEND 0x43ff 45 | } 46 | 47 | if { [info exists OPTIONSTART] } { 48 | set _OPTIONSTART $OPTIONSTART 49 | } else { 50 | set _OPTIONSTART 0x4800 51 | } 52 | 53 | if { [info exists OPTIONEND] } { 54 | set _OPTIONEND $OPTIONEND 55 | } else { 56 | set _OPTIONEND 0x487f 57 | } 58 | 59 | if { [info exists BLOCKSIZE] } { 60 | set _BLOCKSIZE $BLOCKSIZE 61 | } else { 62 | set _BLOCKSIZE 0x80 63 | } 64 | 65 | hla newtap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id 0 66 | 67 | set _TARGETNAME $_CHIPNAME.cpu 68 | 69 | target create $_TARGETNAME stm8 -chain-position stm8.cpu 70 | 71 | $_TARGETNAME configure -work-area-phys 0x0 -work-area-size $_WORKAREASIZE -work-area-backup 1 72 | $_TARGETNAME configure -flashstart $_FLASHSTART -flashend $_FLASHEND -eepromstart $_EEPROMSTART -eepromend $_EEPROMEND -optionstart $_OPTIONSTART -optionend $_EEPROMEND -blocksize $_BLOCKSIZE 73 | 74 | if { [info exists ENABLE_STEP_IRQ] } { 75 | $_TARGETNAME configure -enable_step_irq 76 | } 77 | 78 | adapter_khz 0 79 | 80 | reset_config srst_only 81 | 82 | -------------------------------------------------------------------------------- /firmware/uart.c: -------------------------------------------------------------------------------- 1 | /* 2 | * EGG OpenSource EBike firmware 3 | * 4 | * Copyright (C) Casainho, 2015, 2106, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | #include "motor.h" 13 | #include "stm8s.h" 14 | #include "stm8s_uart2.h" 15 | #include "main.h" 16 | 17 | void uart_init (void) 18 | { 19 | UART2_DeInit(); 20 | #ifdef DEBUG_UART 21 | UART2_Init((uint32_t) 115200, 22 | #else 23 | UART2_Init((uint32_t) 9600, 24 | #endif 25 | UART2_WORDLENGTH_8D, 26 | UART2_STOPBITS_1, 27 | UART2_PARITY_NO, 28 | UART2_SYNCMODE_CLOCK_DISABLE, 29 | UART2_MODE_TXRX_ENABLE); 30 | 31 | UART2_ITConfig(UART2_IT_RXNE_OR, ENABLE); 32 | } 33 | 34 | #if __SDCC_REVISION < 9624 35 | void putchar(char c) 36 | { 37 | //Write a character to the UART2 38 | UART2_SendData8(c); 39 | 40 | //Loop until the end of transmission 41 | while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET); 42 | } 43 | #else 44 | int putchar(int c) 45 | { 46 | //Write a character to the UART2 47 | UART2_SendData8(c); 48 | 49 | //Loop until the end of transmission 50 | while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET); 51 | 52 | return((unsigned char)c); 53 | } 54 | #endif 55 | 56 | #if __SDCC_REVISION < 9989 57 | char getchar(void) 58 | #else 59 | int getchar(void) 60 | #endif 61 | { 62 | uint8_t c = 0; 63 | 64 | /* Loop until the Read data register flag is SET */ 65 | while (UART2_GetFlagStatus(UART2_FLAG_RXNE) == RESET) ; 66 | 67 | c = UART2_ReceiveData8(); 68 | 69 | return (c); 70 | } 71 | -------------------------------------------------------------------------------- /firmware/uart.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EGG OpenSource EBike firmware 3 | * 4 | * Copyright (C) Casainho, 2015, 2106, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #ifndef _UART_H 10 | #define _UART_H 11 | 12 | #include "main.h" 13 | 14 | void uart_init (void); 15 | 16 | #if __SDCC_REVISION < 9624 17 | void putchar(char c); 18 | #else 19 | int putchar(int c); 20 | #endif 21 | 22 | #if __SDCC_REVISION < 9989 23 | char getchar(void); 24 | #else 25 | int getchar(void); 26 | #endif 27 | 28 | #endif /* _UART_H */ 29 | 30 | -------------------------------------------------------------------------------- /firmware/utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * EGG OpenSource EBike firmware 3 | * 4 | * Copyright (C) Casainho, 2015, 2106, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #include 10 | #include 11 | #include "stm8s.h" 12 | #include "utils.h" 13 | 14 | int32_t map (int32_t x, int32_t in_min, int32_t in_max, int32_t out_min, int32_t out_max) 15 | { 16 | // if input is smaller/bigger than expected return the min/max out ranges value 17 | if (x < in_min) 18 | return out_min; 19 | else if (x > in_max) 20 | return out_max; 21 | 22 | // map the input to the output range. 23 | // round up if mapping bigger ranges to smaller ranges 24 | else if ((in_max - in_min) > (out_max - out_min)) 25 | return (x - in_min) * (out_max - out_min + 1) / (in_max - in_min + 1) + out_min; 26 | // round down if mapping smaller ranges to bigger ranges 27 | else 28 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; 29 | } 30 | 31 | uint8_t ui8_min (uint8_t value_a, uint8_t value_b) 32 | { 33 | if (value_a < value_b) return value_a; 34 | else return value_b; 35 | } 36 | 37 | uint8_t ui8_max (uint8_t value_a, uint8_t value_b) 38 | { 39 | if (value_a > value_b) return value_a; 40 | else return value_b; 41 | } 42 | 43 | void pi_controller (struct_pi_controller_state *pi_controller) 44 | { 45 | int16_t i16_error; 46 | int16_t i16_p_term; 47 | int16_t i16_temp; 48 | 49 | i16_error = pi_controller->ui8_target_value - pi_controller->ui8_current_value; // 255-0 or 0-255 --> [-255 ; 255] 50 | i16_p_term = (i16_error * pi_controller->ui8_kp_dividend) >> pi_controller->ui8_kp_divisor; 51 | 52 | pi_controller->i16_i_term += (i16_error * pi_controller->ui8_ki_dividend) >> pi_controller->ui8_ki_divisor; 53 | if (pi_controller->i16_i_term > 255) { pi_controller->i16_i_term = 255; } 54 | if (pi_controller->i16_i_term < 0) { pi_controller->i16_i_term = 0; } 55 | 56 | i16_temp = i16_p_term + pi_controller->i16_i_term; 57 | // limit to [0 ; 255] as duty_cycle that will be controlled can't have other values than that ones 58 | if (i16_temp > 255) { i16_temp = 255; } 59 | if (i16_temp < 0) { i16_temp = 0; } 60 | pi_controller->ui8_controller_output_value = (uint8_t) i16_temp; 61 | } 62 | 63 | void pi_controller_reset (struct_pi_controller_state *pi_controller) 64 | { 65 | pi_controller->i16_i_term = 0; 66 | } 67 | -------------------------------------------------------------------------------- /firmware/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EGG OpenSource EBike firmware 3 | * 4 | * Copyright (C) Casainho, 2015, 2106, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #ifndef _UTILS_H 10 | #define _UTILS_H 11 | 12 | #include "main.h" 13 | 14 | typedef struct pi_controller_state 15 | { 16 | uint8_t ui8_current_value; 17 | uint8_t ui8_target_value; 18 | uint8_t ui8_controller_output_value; 19 | uint8_t ui8_kp_dividend; 20 | uint8_t ui8_kp_divisor; 21 | uint8_t ui8_ki_dividend; 22 | uint8_t ui8_ki_divisor; 23 | int16_t i16_i_term; 24 | } struct_pi_controller_state; 25 | 26 | int32_t map (int32_t x, int32_t in_min, int32_t in_max, int32_t out_min, int32_t out_max); 27 | uint8_t ui8_max (uint8_t value_a, uint8_t value_b); 28 | uint8_t ui8_min (uint8_t value_a, uint8_t value_b); 29 | void pi_controller (struct_pi_controller_state *pi_controller_state); 30 | void pi_controller_reset (struct_pi_controller_state *pi_controller); 31 | 32 | #endif /* _UTILS_H */ 33 | -------------------------------------------------------------------------------- /firmware/watchdog.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #include "stm8s.h" 10 | #include "stm8s_clk.h" 11 | #include "stm8s_iwdg.h" 12 | 13 | // PLEASE NOTE: while debuging using STLinkV2, watchdog seems to be disable and to test, you will need to run without the debugger 14 | void watchdog_init (void) 15 | { 16 | IWDG_Enable (); 17 | IWDG_WriteAccessCmd (IWDG_WriteAccess_Enable); 18 | IWDG_SetPrescaler (IWDG_Prescaler_4); 19 | 20 | // Timeout period 21 | // The timeout period can be configured through the IWDG_PR and IWDG_RLR registers. It 22 | // is determined by the following equation: 23 | // T = 2 * T LSI * P * R 24 | // where: 25 | // T = Timeout period 26 | // T LSI = 1/f LSI 27 | // P = 2 (PR[2:0] + 2) 28 | // R = RLR[7:0]+1 29 | // 30 | // 0.0001 = 2 * (1 / 128000) * 4 * R 31 | // R = 1.6 ; rounding to R = 2 32 | // R = 2 means a value of reload register = 1 33 | IWDG_SetReload (2); // 187.5us; for some reason, a value of 1 don't work, only 2 34 | IWDG_ReloadCounter (); 35 | } 36 | -------------------------------------------------------------------------------- /firmware/watchdog.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #ifndef _WATCHDOG_H_ 10 | #define _WATCHDOG_H_ 11 | 12 | void watchdog_init (void); 13 | 14 | #endif /* _WATCHDOG_H_ */ 15 | -------------------------------------------------------------------------------- /firmware/wheel_speed_sensor.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #include 10 | #include "stm8s.h" 11 | #include "stm8s_it.h" 12 | #include "gpio.h" 13 | 14 | void wheel_speed_sensor_init (void) 15 | { 16 | //whell speed sensor pin as input 17 | GPIO_Init(WHEEL_SPEED_SENSOR__PORT, 18 | WHEEL_SPEED_SENSOR__PIN, 19 | GPIO_MODE_IN_PU_NO_IT); // input pull-up, no external interrupt 20 | } 21 | -------------------------------------------------------------------------------- /firmware/wheel_speed_sensor.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BMSBattery S series motor controllers firmware 3 | * 4 | * Copyright (C) Casainho, 2017. 5 | * 6 | * Released under the GPL License, Version 3 7 | */ 8 | 9 | #ifndef _WHELL_SPEED_SENSOR_H_ 10 | #define _PAS_H_ 11 | 12 | #include "main.h" 13 | 14 | void wheel_speed_sensor_init (void); 15 | 16 | #endif /* _WHELL_SPEED_SENSOR_H_ */ 17 | -------------------------------------------------------------------------------- /firmware_configuration_tool.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/firmware_configuration_tool.jar -------------------------------------------------------------------------------- /lib/swing-layout-1.0.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenSourceEBike/BMSBattery_S_controllers_firmware/5451cfb11e8553a982d9cc9b37ddb72a83983558/lib/swing-layout-1.0.4.jar --------------------------------------------------------------------------------