Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
LineEnding: LF
UseTab: Never
IndentWidth: 4
ColumnLimit: 120

IndentCaseLabels: false
BreakBeforeBraces: Allman
AllowShortIfStatementsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AlignConsecutiveMacros:
Enabled: true
AcrossEmptyLines: true
AcrossComments: false
6 changes: 3 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
root = true

[*]
end_of_line = crlf
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = tab
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
trim_trailing_whitespace = false
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Disable git line ending conversion
* -text
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
- id: check-added-large-files
- id: mixed-line-ending
args: [--fix=lf]
exclude: '^drawings/'
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v18.1.8
hooks:
- id: clang-format
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ If you find this project useful, consider sending a small [donation](https://www
* ⚠️ Unstable on BBS02 controllers!

## Highlights
* ✅ A bit more power without hardware modifications! (max 33A).
* ✅ A bit more power without hardware modifications! (max 33A).
* ✅ No upper voltage limit in software, can by default run up to 63V (maximum rating of components).
* ✅ Support lower voltage cutoff for use with e.g. 36V battery.
* ✅ Smooth Throttle/PAS override.
Expand Down Expand Up @@ -67,11 +67,11 @@ BBS02A - No idea, not tested, not recommended to try unless you have an already
### TSDZ2
Compatible with TSDZ2A/B using the STM microcontroller (which is nearly all off them).

### Displays and Controller
### Displays and Controller

Only displays with the Bafang display protocol can work.
Only displays with the Bafang display protocol can work.

Also the controllers need to be those, that are officially designed by Bafang, respectively Tongshen.
Also the controllers need to be those, that are officially designed by Bafang, respectively Tongshen.

Some shops sell kits with their own controller.

Expand Down
3 changes: 3 additions & 0 deletions code/firmware/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vs
.vscode
build
106 changes: 106 additions & 0 deletions code/firmware/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
cmake_minimum_required(VERSION 3.15)

project(bbs-fw C)

set(TARGET_CONTROLLER "BBSHD" CACHE STRING "Target controller: BBSHD,BBS02,TSDZ2,TSDZ8")

if (${TARGET_CONTROLLER} STREQUAL "BBSHD")
add_link_options(--xram-size 3840)
endif()

if (${TARGET_CONTROLLER} STREQUAL "BBS02")
add_link_options(--xram-size 1792)
endif()

if (${TARGET_CONTROLLER} STREQUAL "TSDZ2")
add_subdirectory(lib/stm8s)
endif()

set(SOURCE_FILES
"src/adc.h"
"src/app.c"
"src/app.h"
"src/battery.c"
"src/battery.h"
"src/cfgstore.c"
"src/cfgstore.h"
"src/eeprom.h"
"src/eventlog.c"
"src/eventlog.h"
"src/extcom.c"
"src/extcom.h"
"src/fwconfig.h"
"src/interrupt.h"
"src/lights.h"
"src/main.c"
"src/motor.h"
"src/sensors.h"
"src/system.h"
"src/throttle.c"
"src/throttle.h"
"src/timers.h"
"src/uart.h"
"src/util.h"
"src/version.h"
"src/watchdog.h"
)

if (${TARGET_CONTROLLER} STREQUAL "BBSHD" OR ${TARGET_CONTROLLER} STREQUAL "BBS02")
list(APPEND SOURCE_FILES
"src/bbsx/adc.c"
"src/bbsx/cpu.h"
"src/bbsx/eeprom.c"
"src/bbsx/interrupt.h"
"src/bbsx/lights.c"
"src/bbsx/motor.c"
"src/bbsx/pins.h"
"src/bbsx/sensors.c"
"src/bbsx/stc15.h"
"src/bbsx/system.c"
"src/bbsx/timers.c"
"src/bbsx/timers.h"
"src/bbsx/uart_motor.h"
"src/bbsx/uart.c"
"src/bbsx/watchdog.c"
)
endif()

if (${TARGET_CONTROLLER} STREQUAL "TSDZ2")
list(APPEND SOURCE_FILES
"src/tsdz2/adc.c"
"src/tsdz2/cpu.h"
"src/tsdz2/eeprom.c"
"src/tsdz2/interrupt.h"
"src/tsdz2/lights.c"
"src/tsdz2/motor.c"
"src/tsdz2/pins.h"
"src/tsdz2/sensors.c"
"src/tsdz2/stm8.h"
"src/tsdz2/system.c"
"src/tsdz2/timers.c"
"src/tsdz2/timers.h"
"src/tsdz2/torquesensor.c"
"src/tsdz2/uart.c"
"src/tsdz2/watchdog.c"
)
endif()

add_executable(bbs-fw ${SOURCE_FILES})

target_compile_definitions(bbs-fw PRIVATE ${TARGET_CONTROLLER})
target_include_directories(bbs-fw PRIVATE src)

if (${TARGET_CONTROLLER} STREQUAL "TSDZ2")
target_link_libraries(bbs-fw stm8s)
endif()


# Generate hex file (SDCC)
if (${CMAKE_C_COMPILER} STREQUAL "sdcc" OR ${TARGET_CONTROLLER} STREQUAL "BBS02" OR ${TARGET_CONTROLLER} STREQUAL "TSDZ2")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't look further into the file, but should this line have BBSHD too?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ye that's wrong, thanks!

add_custom_command(
POST_BUILD
COMMENT Generating bbs-fw.hex
TARGET bbs-fw
COMMAND packihx bbs-fw.ihx > bbs-fw.hex
)
endif()
65 changes: 65 additions & 0 deletions code/firmware/CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"version": 6,
"cmakeMinimumRequired": {
"major": 3,
"minor": 15,
"patch": 0
},
"configurePresets": [
{
"name": "bbshd-release",
"displayName": "BBSHD",
"description": "Build for BBSHD",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/bbshd-release",
"toolchainFile": "${sourceDir}/cmake/sdcc-8051-toolchain.cmake",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"TARGET_CONTROLLER": {
"type": "STRING",
"value": "BBSHD"
}
}
},
{
"name": "bbs02-release",
"displayName": "BBS02",
"description": "Build for BBS02",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/bbs02-release",
"toolchainFile": "${sourceDir}/cmake/sdcc-8051-toolchain.cmake",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"TARGET_CONTROLLER": {
"type": "STRING",
"value": "BBS02"
}
}
},
{
"name": "tsdz2-release",
"displayName": "TSDZ2",
"description": "Build for TSDZ2",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/tsdz2-release",
"toolchainFile": "${sourceDir}/cmake/sdcc-stm8-toolchain.cmake",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"TARGET_CONTROLLER": {
"type": "STRING",
"value": "TSDZ2"
}
}
}
],
"buildPresets": [
{
"name": "bbshd-release",
"configurePreset": "bbshd-release"
},
{
"name": "tsdz2-release",
"configurePreset": "tsdz2-release"
}
]
}
27 changes: 27 additions & 0 deletions code/firmware/cmake/sdcc-8051-toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# the name of the target operating system
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR 8051)

set(CMAKE_C_FLAGS_INIT "-mmcs51 --model-large --std-sdcc11 -Ddouble=float")
set(CMAKE_EXE_LINKER_FLAGS_INIT "")

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

# which compilers to use for C and ASM
set(CMAKE_C_COMPILER sdcc)

find_program (SDCC NAMES sdcc)
get_filename_component(SDCC_BIN_DIR ${SDCC} DIRECTORY)
get_filename_component(SDCC_PATH_DIR ${SDCC_BIN_DIR} DIRECTORY)

# here is the target environment is located
set(CMAKE_FIND_ROOT_PATH ${SDCC_PATH_DIR}/usr/share/sdcc)

# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search
# programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

set(CMAKE_ASM_OUTPUT_EXTENSION ".rel")
30 changes: 30 additions & 0 deletions code/firmware/cmake/sdcc-stm8-toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# the name of the target operating system
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR stm8)

set(CMAKE_C_FLAGS_INIT "-mstm8 --std-sdcc11 -Ddouble=float")
set(CMAKE_EXE_LINKER_FLAGS_INIT "")

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

# which compilers to use for C and ASM
set(CMAKE_C_COMPILER sdcc)
set(CMAKE_ASM_COMPILER sdasstm8)

find_program (SDCC NAMES sdcc)
get_filename_component(SDCC_BIN_DIR ${SDCC} DIRECTORY)
get_filename_component(SDCC_PATH_DIR ${SDCC_BIN_DIR} DIRECTORY)

# here is the target environment is located
set(CMAKE_FIND_ROOT_PATH ${SDCC_PATH_DIR}/usr/share/sdcc)

# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search
# programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

set(CMAKE_ASM_OUTPUT_EXTENSION ".rel")

set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_ASM_COMPILER} -o <OBJECT> <SOURCE>")
2 changes: 2 additions & 0 deletions code/firmware/lib/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DisableFormat: true
SortIncludes: Never
31 changes: 31 additions & 0 deletions code/firmware/lib/stm8s/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

add_library(stm8s INTERFACE
"include/stm8/stm8s_adc1.h"
"include/stm8/stm8s_adc2.h"
"include/stm8/stm8s_awu.h"
"include/stm8/stm8s_beep.h"
"include/stm8/stm8s_can.h"
"include/stm8/stm8s_clk.h"
"include/stm8/stm8s_exti.h"
"include/stm8/stm8s_flash.h"
"include/stm8/stm8s_gpio.h"
"include/stm8/stm8s_i2c.h"
"include/stm8/stm8s_itc.h"
"include/stm8/stm8s_iwdg.h"
"include/stm8/stm8s_rst.h"
"include/stm8/stm8s_spi.h"
"include/stm8/stm8s_tim1.h"
"include/stm8/stm8s_tim2.h"
"include/stm8/stm8s_tim3.h"
"include/stm8/stm8s_tim4.h"
"include/stm8/stm8s_tim5.h"
"include/stm8/stm8s_tim6.h"
"include/stm8/stm8s_uart1.h"
"include/stm8/stm8s_uart2.h"
"include/stm8/stm8s_uart3.h"
"include/stm8/stm8s_uart4.h"
"include/stm8/stm8s_wwdg.h"
"include/stm8/stm8s.h"
)

target_include_directories(stm8s INTERFACE include)
Loading