Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
aentinger committed May 23, 2023
0 parents commit ccb1482
Show file tree
Hide file tree
Showing 12 changed files with 421 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .codespellrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/spell-check/.codespellrc
# See: https://github.com/codespell-project/codespell#using-a-config-file
[codespell]
# In the event of a false positive, add the problematic word, in all lowercase, to a comma-separated list here:
ignore-words-list = te,
skip = ./.git,./.licenses,__pycache__,node_modules,./go.mod,./go.sum,./package-lock.json,./poetry.lock,./yarn.lock,./external
builtin = clear,informal,en-GB_to_en-US
check-filenames =
check-hidden =
13 changes: 13 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# See: https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#about-the-dependabotyml-file
version: 2

updates:
# Configure check for outdated GitHub Actions actions in workflows.
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/dependabot/README.md
# See: https://docs.github.com/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot
- package-ecosystem: github-actions
directory: / # Check the repository's workflows under /.github/workflows/
schedule:
interval: daily
labels:
- "topic: ci"
28 changes: 28 additions & 0 deletions .github/workflows/smoke-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Build

# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
on:
push:
pull_request:
schedule:
# Run every Tuesday at 8 AM UTC
- cron: "0 8 * * TUE"
workflow_dispatch:
repository_dispatch:

permissions:
contents: read

jobs:
smoke-test:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install CMake
run: sudo apt-get install cmake

- name: Create build directory, run CMake and Make
run: mkdir build && cd build && cmake .. && make
26 changes: 26 additions & 0 deletions .github/workflows/spell-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Source: https://github.com/per1234/.github/blob/main/workflow-templates/spell-check.md
name: Spell Check

# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
on:
push:
pull_request:
schedule:
# Run every Tuesday at 8 AM UTC to catch new misspelling detections resulting from dictionary updates.
- cron: "0 8 * * TUE"
workflow_dispatch:
repository_dispatch:

permissions:
contents: read

jobs:
spellcheck:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Spell check
uses: codespell-project/actions-codespell@master
133 changes: 133 additions & 0 deletions .github/workflows/sync-labels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/sync-labels.md
name: Sync Labels

# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
on:
push:
paths:
- ".github/workflows/sync-labels.ya?ml"
- ".github/label-configuration-files/*.ya?ml"
pull_request:
paths:
- ".github/workflows/sync-labels.ya?ml"
- ".github/label-configuration-files/*.ya?ml"
schedule:
# run every Tuesday at 3 AM UTC
- cron: "0 3 * * 2"
workflow_dispatch:
repository_dispatch:

env:
CONFIGURATIONS_FOLDER: .github/label-configuration-files
CONFIGURATIONS_ARTIFACT: label-configuration-files

jobs:
check:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Download JSON schema for labels configuration file
id: download-schema
uses: carlosperate/download-file-action@v2.0.0
with:
file-url: https://raw.githubusercontent.com/arduino/tooling-project-assets/main/workflow-templates/assets/sync-labels/arduino-tooling-gh-label-configuration-schema.json
location: ${{ runner.temp }}/label-configuration-schema

- name: Install JSON schema validator
run: |
sudo npm install \
--global \
ajv-cli \
ajv-formats
- name: Validate local labels configuration
run: |
# See: https://github.com/ajv-validator/ajv-cli#readme
ajv validate \
--all-errors \
-c ajv-formats \
-s "${{ steps.download-schema.outputs.file-path }}" \
-d "${{ env.CONFIGURATIONS_FOLDER }}/*.{yml,yaml}"
download:
needs: check
runs-on: ubuntu-latest

strategy:
matrix:
filename:
# Filenames of the shared configurations to apply to the repository in addition to the local configuration.
# https://github.com/107-systems/.github/blob/main/workflow-templates/assets/sync-labels
- universal.yml

steps:
- name: Download
uses: carlosperate/download-file-action@v2.0.0
with:
file-url: https://raw.githubusercontent.com/107-systems/.github/main/workflow-templates/assets/sync-labels/${{ matrix.filename }}

- name: Pass configuration files to next job via workflow artifact
uses: actions/upload-artifact@v3
with:
path: |
*.yaml
*.yml
if-no-files-found: error
name: ${{ env.CONFIGURATIONS_ARTIFACT }}

sync:
needs: download
runs-on: ubuntu-latest

steps:
- name: Set environment variables
run: |
# See: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
echo "MERGED_CONFIGURATION_PATH=${{ runner.temp }}/labels.yml" >> "$GITHUB_ENV"
- name: Determine whether to dry run
id: dry-run
if: >
github.event == 'pull_request' ||
github.ref != format('refs/heads/{0}', github.event.repository.default_branch)
run: |
# Use of this flag in the github-label-sync command will cause it to only check the validity of the
# configuration.
echo "::set-output name=flag::--dry-run"
- name: Checkout repository
uses: actions/checkout@v3

- name: Download configuration files artifact
uses: actions/download-artifact@v3
with:
name: ${{ env.CONFIGURATIONS_ARTIFACT }}
path: ${{ env.CONFIGURATIONS_FOLDER }}

- name: Remove unneeded artifact
uses: geekyeggo/delete-artifact@v2
with:
name: ${{ env.CONFIGURATIONS_ARTIFACT }}

- name: Merge label configuration files
run: |
# Merge all configuration files
shopt -s extglob
cat "${{ env.CONFIGURATIONS_FOLDER }}"/*.@(yml|yaml) > "${{ env.MERGED_CONFIGURATION_PATH }}"
- name: Install github-label-sync
run: sudo npm install --global github-label-sync

- name: Sync labels
env:
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
permissions: write-all
run: |
# See: https://github.com/Financial-Times/github-label-sync
github-label-sync \
--labels "${{ env.MERGED_CONFIGURATION_PATH }}" \
${{ steps.dry-run.outputs.flag }} \
${{ github.repository }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/
cmake-build-debug/
.idea/
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
##########################################################################
cmake_minimum_required(VERSION 3.15)
##########################################################################
project("pika-spark-bno085-driver")
##########################################################################
add_executable(${PROJECT_NAME}
main.cpp
spi.cpp
)
##########################################################################
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Werror -Wextra -Wpedantic)
endif()
##########################################################################
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_17)
##########################################################################
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Alexander Entinger, LXRobotics GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<a href="https://pika-spark.io/"><img align="right" src="https://raw.githubusercontent.com/pika-spark/.github/main/logo/logo-pika-spark-bg-white.png" width="15%"></a>
:sparkles: `pika-spark-bno085-driver`
=====================================
[![Smoke test status](https://github.com/pika-spark/pika-spark-bno085-driver/actions/workflows/smoke-test.yml/badge.svg)](https://github.com/pika-spark/pika-spark-bno085-driver/actions/workflows/smoke-test.yml)
[![Spell Check status](https://github.com/pika-spark/pika-spark-bno085-driver/actions/workflows/spell-check.yml/badge.svg)](https://github.com/pika-spark/pika-spark-bno085-driver/actions/workflows/spell-check.yml)

Linux user space driver for the [BNO085](https://www.ceva-dsp.com/wp-content/uploads/2019/10/BNO080_085-Datasheet.pdf) 9-DoF IMU driver.

<p align="center">
<a href="https://pika-spark.io/"><img src="https://raw.githubusercontent.com/pika-spark/.github/main/logo/logo-pika-spark-bg-white-github.png" width="40%"></a>
</p>

### How-to-build
* Enable [spidev](https://www.kernel.org/doc/Documentation/spi/spidev) to access SPI from userspace
```bash
modprobe spidev
sudo chmod ugo+rw /dev/spidev1.0
```
* Build user space driver within Docker container with access to SPI
```bash
docker pull alpine:latest
docker run -it -u 0 --device /dev/spidev1.0 alpine:latest sh
apk add git g++ make cmake linux-headers
cd /tmp
git clone https://github.com/107-systems/pika-spark-bno085-driver && cd pika-spark-bno085-driver
mkdir build && cd build
cmake ..
make
```
32 changes: 32 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* This software is distributed under the terms of the MIT License.
* Copyright (c) 2023 LXRobotics.
* Author: Alexander Entinger <alexander.entinger@lxrobotics.com>
* Contributors: https://github.com/107-systems/pika-spark-bno085-driver/graphs/contributors.
*/

#include <cstdlib>

#include <memory>
#include <iostream>
#include <stdexcept>

#include "spi.h"

int main(int /* argc */, char ** /* argv */) try
{
auto spi = std::make_shared<SPI>("/dev/spidev1.0", SPI_MODE_0, 8, 1000000);
return EXIT_SUCCESS;
}
catch (std::runtime_error const & err)
{
std::cerr << "Exception (std::runtime_error) caught: " << err.what() << std::endl;
std::cerr << "Terminating ..." << std::endl;
return EXIT_FAILURE;
}
catch (...)
{
std::cerr << "Unhandled exception caught." << std::endl;
std::cerr << "Terminating ..." << std::endl;
return EXIT_FAILURE;
}
72 changes: 72 additions & 0 deletions spi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* This software is distributed under the terms of the MIT License.
* Copyright (c) 2023 LXRobotics.
* Author: Alexander Entinger <alexander.entinger@lxrobotics.com>
* Contributors: https://github.com/107-systems/pika-spark-bno085-driver/graphs/contributors.
*/

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include "spi.h"

#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>

#include <stdexcept>

/**************************************************************************************
* CTOR/DTOR
**************************************************************************************/

SPI::SPI(std::string const & dev_name, uint8_t const mode, uint8_t const bits, uint32_t const speed_Hz)
: _bits{bits}
, _speed_Hz{speed_Hz}
{
_fd = open(dev_name.c_str(), O_RDWR);
if (_fd < 0)
throw std::runtime_error("could not open device \"" + dev_name + "\"");

if (ioctl(_fd, SPI_IOC_WR_MODE, &mode) < 0)
throw std::runtime_error("could not set SPI mode" + std::string(strerror(errno)));

if (ioctl(_fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0)
throw std::runtime_error("could not set SPI bits" + std::string(strerror(errno)));

if (ioctl(_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_Hz) < 0)
throw std::runtime_error("could not set SPI bit rate" + std::string(strerror(errno)));
}

SPI::~SPI()
{
close(_fd);
}

/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/

bool SPI::transfer(uint8_t const * tx_buf, uint8_t * rx_buf, size_t const num_bytes)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
struct spi_ioc_transfer tf =
{
/* .tx_buf = */(unsigned long)tx_buf,
/* .rx_buf = */(unsigned long)rx_buf,
/* .len = */static_cast<uint32_t >(num_bytes),
/* .speed_hz = */_speed_Hz,
/* .delay_usecs = */0,
/* .bits_per_word = */_bits,
};
#pragma GCC diagnostic pop

if (ioctl(_fd, SPI_IOC_MESSAGE(1), &tf) < 0)
return false;

return true;
}
Loading

0 comments on commit ccb1482

Please sign in to comment.