Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] STM32 F1 Cube Support #13

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
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
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
load("@rules_cc//cc:defs.bzl", "cc_library")


INCLUDE_DIRECTORIES = [
"Drivers/CMSIS/Core_A/Include",
"Drivers/CMSIS/Core/Include",
"Drivers/CMSIS/Device/ST/STM32F1xx/Include",
"Drivers/CMSIS/DSP/Include",
"Drivers/CMSIS/Include",
"Drivers/CMSIS/NN/Include",
"Drivers/CMSIS/RTOS2/Include",
"Drivers/STM32F1xx_HAL_Driver/Inc",
"Drivers/STM32F1xx_HAL_Driver/Inc/Legacy",
"Middlewares/ST/STM32_Network_Library/Includes",
"Middlewares/ST/STM32_USB_Device_Library/Class/AUDIO/Inc",
"Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc",
"Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Inc",
"Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Inc",
"Middlewares/ST/STM32_USB_Device_Library/Class/HID/Inc",
"Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc",
"Middlewares/ST/STM32_USB_Device_Library/Core/Inc",
"Middlewares/ST/STM32_USB_Host_Library/Class/AUDIO/Inc",
"Middlewares/ST/STM32_USB_Host_Library/Class/CDC/Inc",
"Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc",
"Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Inc",
"Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Inc",
"Middlewares/ST/STM32_USB_Host_Library/Core/Inc",
"Middlewares/Third_Party/FatFs/src",
"Middlewares/Third_Party/FatFs/src/drivers",
"Middlewares/Third_Party/LwIP/system/arch",
"Middlewares/Third_Party/mbedTLS/include/mbedtls",
"Middlewares/Third_Party/mbedTLS/include",
]

CMSIS_GLOB_EXCLUSIONS = [
"**/IAR/**",
"Drivers/CMSIS/Core_A/**",
"Drivers/CMSIS/NN/**",
"Drivers/CMSIS/DSP/**",
"Drivers/CMSIS/RTOS2/**",
]

STM32_DEFINES = {
"stm32f103xx" : [
"USE_HAL_DRIVER",
"STM32F103xx"
]

}


def stm32f1_startup_file(device):
return "@stm32f1cube//Drivers/CMSIS/Device/ST/STM32F1xx/Source/Templates/gcc/startup_" + device + ".s"

def stm32f1_project(name, config):
include_directories = INCLUDE_DIRECTORIES
defines = []
glob_excludes = ["**/*template.c"]

cc_library(
name = name + "_hal_driver",
srcs = native.glob(["Drivers/STM32F1xx_HAL_Driver/**/*.c"], exclude = glob_excludes),
hdrs = native.glob(["**/*.h"]),
copts = ["-include stdint.h"],
defines = defines,
includes = include_directories,
deps = [config],
tags = ["manual"],
alwayslink = True,
)

cc_library(
name = name + "_stm32_network_library",
srcs = native.glob(["Middlewares/ST/STM32_Network_Library/**/*.c"], exclude = glob_excludes),
hdrs = native.glob(["**/*.h"]),
defines = defines,
includes = include_directories,
deps = [config],
tags = ["manual"],
alwayslink = True,
)

cc_library(
name = name + "_stm32_usb_device_library",
srcs = native.glob(["MiddleWares/ST/STM32_USB_Device_Library/**/*.c"], exclude = glob_excludes),
hdrs = native.glob(["**/*.h"]),
defines = defines,
includes = include_directories,
deps = [config],
tags = ["manual"],
alwayslink = True,
)

cc_library(
name = name + "_stm32_usb_host_library",
srcs = native.glob(["MiddleWares/ST/STM32_USB_Host_Library/**/*.c"], exclude = glob_excludes),
hdrs = native.glob(["**/*.h"]),
defines = defines,
includes = include_directories,
deps = [config],
tags = ["manual"],
alwayslink = True,
)

cc_library(
name = name + "_st_middlewares",
deps = [
":" + name + "_stm32_network_library",
":" + name + "_stm32_usb_device_library",
":" + name + "_stm32_usb_host_library",
],
tags = ["manual"],
alwayslink = True,
)

cc_library(
name = name + "_fatfs",
srcs = native.glob(["Middlewares/Third_Party/FatFs/src/*.c"], exclude = glob_excludes),
hdrs = native.glob(["Middlewares/Third_Party/FatFs/src/*.h"]),
includes = include_directories,
deps = [config],
tags = ["manual"],
alwayslink = True,
)

cc_library(
name = name + "_mbed_tls",
srcs = native.glob(
["Middlewares/Third_Party/mbedTLS/library/*.c"],
exclude = glob_excludes,
),
hdrs = native.glob(["Middlewares/Third_Party/mbedTLS/include/mbedtls/*.h"]),
copts = [
'-DMBEDTLS_CONFIG_FILE=\\"mbedtls_conf.h\\"',
],
includes = include_directories,
deps = [config],
tags = ["manual"],
alwayslink = True,
)

cc_library(
name = name + "_third_party_middlewares",
deps = [
":" + name + "_fatfs",
":" + name + "_mbed_tls",
],
tags = ["manual"],
alwayslink = True,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
load(":stm32f1_defs.bzl", "STM32_DEFINES")

VERSIONS = {
"1.8.2" : {
"commit" : "441b2cbdc25aa50437a59c4bffe22b88e78942c9",
"sha256" : ""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add a complete sha256 just to confirm determinism. If you leave this empty it should print out the sha256 when it fetches the remote reposistory.

Copy link
Author

Choose a reason for hiding this comment

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

Got it. Will finish up the example this weekend.

}
}


def _stm32f1xx_repository_impl(rctx):
REPOSITORY_NAME = "STM32CubeF1"
BASE_URL = "https://github.com/STMicroelectronics/" + REPOSITORY_NAME + "/archive/"

version_info = VERSIONS[rctx.attr.version]
rctx.download_and_extract(
url = BASE_URL + version_info["commit"] + ".zip",
stripPrefix = REPOSITORY_NAME + "-" + version_info["commit"],
sha256 = version_info["sha256"],
)
rctx.symlink(Label("//third_party_libs/devices/arm/st_microelectronics/stm32f1cube:stm32f1_defs.bzl"), "stm32f1_defs.bzl")
target_template = """
stm32f1_project(
name = "{name}",
config = "{config}",
)
"""

startup_template = """
cc_library(
name = "startup_{device}",
srcs = ["Drivers/CMSIS/Device/ST/STM32F1xx/Source/Templates/gcc/startup_{device}.s"],
alwayslink = True,
tags = ["manual"],
)
"""
tartup_targets = [startup_template.format(device = device) for device in STM32_DEFINES.keys()]
startup_targets_str = "\n".join(startup_targets)
targets = [target_template.format(config = config, name = name) for name, config in rctx.attr.project_configs.items()]
targets_str = "\n".join(targets)
rctx.file("BUILD", """
package(default_visibility = ["//visibility:public"])
exports_files(glob(["**/*.s"]))
load(":stm32f1_defs.bzl","stm32f1_project")
""" + targets_str + startup_targets_str)

stm32f1_repository_simple = repository_rule(
implementation = _stm32f1xx_repository_impl,
attrs = {
"version": attr.string(
doc = "Release version of repository",
default = "v1.7.0",
values = VERSIONS.keys(),
),
"project_configs": attr.string_dict(
doc = "The key as the named prefix for this project and the value as the Label for your projects config cc_library",
mandatory = True,
),
},
)

def stm32f1_repository(project_configs, version = "v1.7.0"):
stm32f1_repository_simple(name = "stm32f1cube", project_configs = project_configs, version = version)