Skip to content

Commit

Permalink
Add initial generated driver
Browse files Browse the repository at this point in the history
  • Loading branch information
rnestler committed Jan 22, 2021
1 parent f1e2af0 commit a31a8f3
Show file tree
Hide file tree
Showing 39 changed files with 5,537 additions and 2 deletions.
14 changes: 14 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 4
AlignAfterOpenBracket: Align
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
IndentCaseLabels: true
SpacesBeforeTrailingComments: 2
PointerAlignment: Left
AlignEscapedNewlines: Left
ForEachMacros: ['TEST_GROUP', 'TEST']
...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/scd4x_i2c_example_usage
38 changes: 38 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
stages:
- validate
- build
- test

syntax_check:
stage: validate
image:
name: registry.gitlab.sensirion.lokal/mso-sw/drivers/docker-driver-generator:0.1.0
tags: [linux, docker]
script:
- find . -type f -iregex ".*\.\(c\|h\|cpp\|ino\)" -exec clang-format-6.0 -i -style=file {} \; && git diff --exit-code


cppcheck:
stage: validate
image:
name: registry.gitlab.sensirion.lokal/mso-sw/drivers/docker-driver-generator:0.1.0
tags: [linux, docker]
script:
- cppcheck --std=c89 --language=c --error-exitcode=1 --enable=warning,style,performance,portability -i sample-implementations/ .

build:
stage: build
image:
name: sensirion/embedded-ci:2.3.0
tags: [linux, docker]
script:
- make

test:
stage: test
image: registry.gitlab.sensirion.lokal/sensirion/docker/docker-rpi-testbed:1.2.0
tags: [mso-sw-testbed-2]
script:
- git clone -b 3.0.0 https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab/MSO-SW/drivers/testbed/i2c-mux-testbed.git
- cd tests/
- make test
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2021, Sensirion AG
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
common_sources = sensirion_config.h sensirion_common.h sensirion_common.c
i2c_sources = sensirion_i2c_hal.h sensirion_i2c.h sensirion_i2c.c
scd4x_sources = scd4x_i2c.h scd4x_i2c.c

i2c_implementation ?= sensirion_i2c_hal.c

CFLAGS = -Os -Wall -fstrict-aliasing -Wstrict-aliasing=1 -Wsign-conversion -fPIC -I.

ifdef CI
CFLAGS += -Werror
endif

.PHONY: all clean

all: scd4x_i2c_example_usage

scd4x_i2c_example_usage: clean
$(CC) $(CFLAGS) -o $@ ${scd4x_sources} ${i2c_sources} \
${i2c_implementation} ${common_sources} scd4x_i2c_example_usage.c

clean:
$(RM) scd4x_i2c_example_usage
100 changes: 98 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,99 @@
# embedded-scd4x
# Getting started

Embedded driver for the SCD4x sensor family.
To get the embedded driver for your Sensirion Sensor working there are now
only a few steps missing. These steps are needed since our driver is made
in a way that it is possible to run it on different hardware platforms.
So we need to adjust two files according to your platform.
The first one is `sensirion_i2c_hal.c`.
This file contains the implementation of the sensor communication
(how to send requests to it). Therefore, how this is done depends on your
hardware platform. For this reason we can't provide just one version of this
code. The default version of this file is an unimplemented template, where you
can implement it yourself. For some common platforms we provide an
implementation in the `sample-implementations` folder.
If you are using for example a Linux based platform like Raspberry Pi
you can just replace the unimplemented HAL template with the provided
implementation in `sample-implementations/linux_user_space/` by replacing
the template with the actual implementation:

```
cp sample-implementations/linux_user_space/sensirion_i2c_hal.c ./
```

If you are on a different platform just use one of the other sample
implementations or implement `sensirion_i2c_hal.c` yourself.
The second file is `sensirion_config.h`.
If you are on a Linux based platform you can skip this part since
everything is already correctly setup for you.
Otherwise you need to check if the libraries `<stdint.h>` and
`<stdlib.h>` are provided by your toolchain, compiler or are available
on your system. If you have no idea on how to do that you can skip this
step for now and come back when you get errors including these names when
trying to compile your driver.
The features we use out of those libraries is standard integer sizes
from `<stdint.h>` and `NULL` from `<stdlib.h>`. If they are not available
you need to specify the following integer types yourself:
* `int64_t` = signed 64bit integer
* `uint64_t` = unsigned 64bit integer
* `int32_t` = signed 32bit integer
* `uint32_t` = unsigned 32bit integer
* `int16_t` = signed 16bit integer
* `uint16_t` = unsigned 16bit integer
* `int8_t` = signed 8bit integer
* `uint8_t` = unsigned 8bit integer

In addition to that you will need to specify `NULL`.
For both we have a commented template where just need to fill in
your system specific values.
Now we are ready to compile and run the example usage for your sensor.
This is again specific to your platform. Since this step can vary quite
a lot we here just explain how to do it on Linux based platforms. In
general you now just need to take all the `.c` and `.h` files directly in
this folder pass them to your favorite C compiler and run the resulting
binary. For Linux systems this is done by following these steps:

1. Open up a terminal.
2. Navigate the directory where this README is located.
3. Run `make`, this compiles all the code here to one binary.
4. Run `./[SENSORNAME]_example_usage`, where you replace `[SENSORNAME]`
with your sensors name. This will run your newly compiled binary.
5. Now you should see the first measurement values appear in your terminal.
As a next step you can adjust the example usage file or write your own
main function to use the sensor.

We hope this tutorial was helpful and you are now able to use the embedded drivers
to communicate with your Sensirion sensor.

# Background

## Files

### sensirion\_i2c.[ch]

In these files you can find the implementation of the I2C protocol used by Sensirion
sensors. The functions in these files are used by the embedded driver to build the
correct frame out of data to be sent to the sensor or receive a frame of data from
the sensor and convert is back to data readable by your machine. The functions in
here calculate and check CRCs, reorder bytes for different byte orders and build the
correct formatted frame for your sensor.

### sensirion\_i2c\_hal.[ch]

In these files you can find the implementation of the hardware abstraction layer used
by Sensirion I2C embedded drivers. This part of the code is specific to the underlying
hardware platform, therefore multiple implementations exist. We also provide an
unimplemented template for the user to implement.
In the `sample-implementations/` folder we provide implementations for the most common
platforms.

### sensirion\_config.h

In this file we keep all the included libraries for our drivers and global defines.
Next to `sensirion_i2c_hal.c` *it's the only file you should need to edit to get your
driver working.*

### sensirion\_common.[ch]

In these files you can find some helper functions used by Sensirion embedded drivers.
It mostly contains byte order conversions for different variable types. These functions
are also used by the UART embedded drivers therefore they are keep in their own file.
85 changes: 85 additions & 0 deletions sample-implementations/Atmel_SAMD2_series/sensirion_i2c_hal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2018, Sensirion AG
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Sensirion AG nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <delay.h>
#include <i2c_master.h>

#include "sensirion_common.h"
#include "sensirion_config.h"
#include "sensirion_i2c_hal.h"

struct i2c_master_module i2c_master_instance;

void sensirion_i2c_hal_init(void) {
/* initialize config structure and software module */
struct i2c_master_config config_i2c_master;
i2c_master_get_config_defaults(&config_i2c_master);

/* change buffer timeout to something longer */
config_i2c_master.buffer_timeout = 10000;

/* initialize and enable device with config. */
i2c_master_init(&i2c_master_instance, SERCOM2, &config_i2c_master);
i2c_master_enable(&i2c_master_instance);
}

/**
* Release all resources initialized by sensirion_i2c_hal_init().
*/
void sensirion_i2c_hal_free(void) {
}

int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint16_t count) {
struct i2c_master_packet packet = {
.address = address,
.data_length = count,
.data = (uint8_t*)data,
.ten_bit_address = false,
.high_speed = false,
};
return i2c_master_read_packet_wait(&i2c_master_instance, &packet);
}

int8_t sensirion_i2c_hal_write(uint8_t address, const uint8_t* data,
uint16_t count) {
struct i2c_master_packet packet = {
.address = address,
.data_length = count,
.data = (uint8_t*)data,
.ten_bit_address = false,
.high_speed = false,
};
return i2c_master_write_packet_wait_no_stop(&i2c_master_instance, &packet);
}

void sensirion_i2c_hal_sleep_usec(uint32_t useconds) {
delay_us(useconds);
}
19 changes: 19 additions & 0 deletions sample-implementations/GPIO_bit_banging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# GPIO based Software I2C

This implementation of the I2C HAL uses bit banging on GPIOs to run
the I2C communication. It is different from other sample-implementations.
It uses an additional pair of `.c` and `.h` file to make the implementation
generic, in a way that GPIO bitbanging can be performed on any system providing
access to GPIOs. Note that in this case the pull-up resistors in the electric
circuit are still needed, or need to be enabled manually if your microprocessor
provides this option. These files are called `sensirion_i2c_gpio.[ch]`.
Similar to the I2C HAL, we provide an unimplemented template in this in this
folder to implement yourself for your platform and sample implementations in the
so called folder.

## Getting started

To use this I2C HAL copy `sensirion_i2c_hal.c` and `sensirion_i2c_gpio.h`
from this folder to the main folder of your driver. Then either choose a
sample implementation or implement `sensirion_i2c_gpio.c` and copy
it to the main driver folder as well.
Loading

0 comments on commit a31a8f3

Please sign in to comment.