Codegen Tooling
We no longer use this codegen tooling to generate can packing functions. We instead generate the messages as part of scons build script, which creates getters and setters for each field of the can message. https://uwmidsun.atlassian.net/l/cp/z0ruBZP3
Introduction
Preface
Note: codegen tooling has been merged into firmware_xiv, so some of this write-up might be outdated. See https://uwmidsun.atlassian.net/l/c/HUVz1yu7 for info about the transition.
This page is more or less a write-up of a video by @Arshan Khanifar that can be found here. If you prefer video to text, please watch the video.
What is codegen?
Code generation is the generation of code so that humans don’t have to write repetitive code. For MSXIV, we store our code generation tools in the repo codegen-tooling-msxiv.
Why do we use codegen?
We use tools to generate code to make our code more clear when it comes to CAN messages. A CAN message has a number of fields packed into 64 bits, e.g. 2 x uint32_t, 1 x uint64_t, or 2 x uint8_t. To facilitate packing these fields, we generate CAN_PACK_… and CAN_UNPACK… functions for each message depending on the contents. This way we don’t have to write custom message packing every time we want to transmit a message. We also generate CAN_TRANSMIT_… functions to facilitate transmitting messages.
How does codegen work?
At a high level, we define our message layouts in the file can_messages.asciipb. Then, we use python scripts to generate the necessary code.
Tutorials
How do you set up codegen?
Follow the instructions on the github repo readme. In short, run these commands after you’ve cloned the readme.
# add the protobuf repo
sudo add-apt-repository ppa:maarten-fonville/protobuf
sudo apt-get update
# install protobuf compiler
sudo apt-get install protobuf-compiler
go get -u github.com/golang/protobuf/protoc-gen-go
# create a virtual environment using conda or virtual env
# virtualenv demonstrated here
virtualenv .venv
source .venv/bin/activate
pip install -r requirements.txt
You should now be set up to generate messages.
How do you add new messages?
In a text editor of your choice, open up
can_messages.asciipb
Choose a non-used message id, and fill out a message definition, following the format of the other messages
Note: messages 0-9 are high priority messages, don’t use these.
msg { id: 99 source: EXAMPLE # target: EXAMPLE msg_name: "descriptive name" can_data { u8 { field_name_1: "blablabla" field_name_2: "albalbalb" } } }
If you need enums to go along with your messages, don’t forget to add them to exported_enums.h in the firmware_xiv repo.
Run
make gen
in the root of the repo.The generated files will be placed in the
codegen-tooling-msxiv/out
directory.
Copy the generated files into your firmware repo at
firmware-xiv/libraries/codegen-tooling/inc
.Check your messages were added correctly with
git diff
or other, then commit and push your changes.