platformio / platform-nordicnrf51

Nordic nRF51: development platform for PlatformIO
https://registry.platformio.org/platforms/platformio/nordicnrf51
Apache License 2.0
20 stars 25 forks source link

Plateform.json should include framework-zephyr-hal-st to allows ST sensors drivers to work #36

Closed aburio closed 3 years ago

aburio commented 3 years ago

Bug

In order to use the magnetometer sensor of the BBC:Microbit v1.5 we need to compile the LIS2MDL driver.

To compile, this driver need a file named "lis2dh_reg.h" who is part of the zephyr-hal-st. This HAL is not releated to STM32 mcu but only to ST sensors (https://github.com/zephyrproject-rtos/hal_st)

Platformio should allows download of this module for the nordic platform.

Solution proposal

Updating the platform.json by adding the following line :

"framework-zephyr-hal-st": {
      "optional": true,
      "version": "0.0.0-alpha+sha.5b3ec3e182"
}

Project to reproduce

src/main.c

/*
 * Copyright (c) 2018, Yannis Damigos
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr.h>
#include <device.h>
#include <drivers/sensor.h>
#include <stdio.h>
#include <sys/util.h>

static s32_t read_sensor(struct device *sensor, enum sensor_channel channel)
{
    struct sensor_value val[3];
    s32_t ret = 0;

    ret = sensor_sample_fetch(sensor);
    if (ret < 0 && ret != -EBADMSG) {
        printf("Sensor sample update error\n");
        goto end;
    }

    ret = sensor_channel_get(sensor, channel, val);
    if (ret < 0) {
        printf("Cannot read sensor channels\n");
        goto end;
    }

    printf("( x y z ) = ( %f  %f  %f )\n", sensor_value_to_double(&val[0]),
                           sensor_value_to_double(&val[1]),
                           sensor_value_to_double(&val[2]));

end:
    return ret;
}

void main(void)
{
    struct device *accelerometer = device_get_binding(
                        DT_LABEL(DT_INST(0, st_lis2dh)));
    struct device *magnetometer = device_get_binding(
                        DT_LABEL(DT_INST(0, st_lis2mdl)));

    if (accelerometer == NULL) {
        printf("Could not get %s device\n",
                DT_LABEL(DT_INST(0, st_lis2dh)));
        return;
    }

    if (magnetometer == NULL) {
        printf("Could not get %s device\n",
                DT_LABEL(DT_INST(0, st_lis2mdl)));
        return;
    }

    while (1) {
        printf("Magnetometer data:\n");
        if (read_sensor(magnetometer, SENSOR_CHAN_MAGN_XYZ) < 0) {
            printf("Failed to read magnetometer data\n");
        }

        printf("Accelerometer data:\n");
        if (read_sensor(accelerometer, SENSOR_CHAN_ACCEL_XYZ) < 0) {
            printf("Failed to read accelerometer data\n");
        }

        k_sleep(K_MSEC(2000));
    }
}

zephyr/CMakeLists.txt

set(ZEPHYR_EXTRA_MODULES "/Users/username/.platformio/packages/framework-zephyr-hal-st")

cmake_minimum_required(VERSION 3.13.1)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(peripheral)

target_sources(app PRIVATE
  ../src/main.c
)

zephyr/prj.conf

CONFIG_STDOUT_CONSOLE=y
CONFIG_I2C=y
CONFIG_SENSOR=y
CONFIG_LIS2DH=y
CONFIG_LIS2MDL=y

zephyr/bbc_microbit.overlay

&i2c0 {
    status = "okay";
    clock-frequency = <I2C_BITRATE_FAST>;
    sda-pin = <30>;
    scl-pin = <0>;

    /* See https://tech.microbit.org/hardware/i2c/ for board variants */

    /* v1.3 MMA8653FC (= FXOS8700) + MAG3110 */
    mma8653fc@1d {
        compatible = "nxp,fxos8700", "nxp,mma8653fc";
        status = "okay";
        reg = <0x1d>;
        label = "MMA8653FC";
        int1-gpios = <&gpio0 28 GPIO_ACTIVE_LOW>;
        int2-gpios = <&gpio0 27 GPIO_ACTIVE_LOW>;
    };

    /* v1.5 variant 1 LSM303AGR */
    lsm303agr-magn@1e {
        compatible = "st,lis2mdl", "st,lsm303agr-magn";
        status = "okay";
        reg = <0x1e>;
        label = "LSM303AGR-MAGN";
        irq-gpios = <&gpio0 27 GPIO_ACTIVE_HIGH>;   /* A3 */
    };

    lsm303agr-accel@19 {
        compatible = "st,lis2dh", "st,lsm303agr-accel";
        status = "okay";
        reg = <0x19>;
        label = "LSM303AGR-ACCEL";
        irq-gpios = <&gpio0 28 GPIO_ACTIVE_HIGH>;
    };
};
aburio commented 3 years ago

@ivankravets should I made a pull request to solve this issue ?

aburio commented 3 years ago

Issue solved by the last release (version 6.0.0). Thank you @valeros 👍