Skip to content

Integrate latest version of edge-impulse-sdk with an updated model and add MicroMod support #11

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ add_executable(ei_rp2040_firmware
src/main.cpp
)

# Define options for different boards
OPTION(BOARD_MICROMOD "Select MicroMod board" OFF)

# Conditionally define macros based on the selected board
if (BOARD_MICROMOD)
target_compile_definitions(ei_rp2040_firmware PRIVATE BOARD_MICROMOD=1)
endif()

OPTION(DEFINE_PIN_UART
"Only use TX/RX pins and not USB UART"
OFF)
Expand Down Expand Up @@ -100,6 +108,7 @@ target_include_directories(ei_rp2040_firmware PRIVATE
ThirdParty/Seeed_Arduino_UltrasonicRanger
ThirdParty/Wire/src
ThirdParty/Arduino_LSM6DSOX/src
ThirdParty/LIS2DH12/src
ThirdParty/rp2040_DHT11_lib
ThirdParty/PDM/src/include
)
Expand Down
161 changes: 161 additions & 0 deletions ThirdParty/LIS2DH12/src/LIS2DH12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/* Include ----------------------------------------------------------------- */
#include "LIS2DH12.h"

/* Constant defines -------------------------------------------------------- */
// Registers
#define LIS2DH12_STATUS_REG_AUX 0x07U
#define LIS2DH12_OUT_TEMP_L 0x0CU
#define LIS2DH12_WHO_AM_I_REG 0X0FU
#define LIS2DH12_TEMP_CFG_REG 0x1FU
#define LIS2DH12_CTRL_REG1 0x20U
#define LIS2DH12_CTRL_REG4 0x23U
#define LIS2DH12_STATUS_REG 0x27U
#define LIS2DH12_OUT_X_L 0x28U

// Register masks
#define LIS2DH12_STATUS_REG_ZYXOR_MASK 0x80u
#define LIS2DH12_STATUS_REG_AUX_TDA_MASK 0x04u

LIS2DH12Class::LIS2DH12Class(TwoWire& wire, uint8_t slaveAddress) :
_wire(&wire),
_slaveAddress(slaveAddress)
{
}

LIS2DH12Class::~LIS2DH12Class() { /* Nothing to do */ }

int LIS2DH12Class::begin()
{
int8_t type;
uint8_t ret;
_wire->begin();

type = readRegister(LIS2DH12_WHO_AM_I_REG);
if (type != MOTION_SENSOR_LIS2DH12) {
return 0;
}

// Data rate: 100Hz, enable all axis, high-resolution mode
writeRegister(LIS2DH12_CTRL_REG1, 0x57);

//Enable block data update, full-scale 2g, hr 1
writeRegister(LIS2DH12_CTRL_REG4, 0x88);

//Enable temperature sensor
writeRegister(LIS2DH12_TEMP_CFG_REG, 0xC0);

return type;
}

void LIS2DH12Class::end()
{
_wire->end();
}

int LIS2DH12Class::readAcceleration(float& x, float& y, float& z)
{
int16_t data[3];

if (!readRegisters(LIS2DH12_OUT_X_L, (uint8_t*)data, sizeof(data))) {
x = NAN;
y = NAN;
z = NAN;

return 0;
}

/* First convert fs2 hr to mg and then to g */
x = ((data[0] / 16.0f) * 1.0f) / 1000.0f;
y = ((data[1] / 16.0f) * 1.0f) / 1000.0f;
z = ((data[2] / 16.0f) * 1.0f) / 1000.0f;

return 1;
}

int LIS2DH12Class::accelerationAvailable()
{
uint8_t data;
if (readRegisters(LIS2DH12_STATUS_REG, &data, 1) != 1) {
return 0;
}

return (data & LIS2DH12_STATUS_REG_ZYXOR_MASK) ? 1 : 0;
}

float LIS2DH12Class::accelerationSampleRate()
{
return 100.0F;
}

int LIS2DH12Class::readTemperature(int & temperature_deg)
{
/* Read the raw temperature from the sensor. */
int16_t temperature_raw = 0;

if (readRegisters(LIS2DH12_OUT_TEMP_L, reinterpret_cast<uint8_t*>(&temperature_raw), sizeof(temperature_raw)) != 1) {
return 0;
}

temperature_deg = (((float)temperature_raw / 64.0f ) / 4.0f ) + 25.0f;

return 1;
}

int LIS2DH12Class::temperatureAvailable()
{
uint8_t data;

if (readRegisters(LIS2DH12_STATUS_REG_AUX, &data, 1) != 1) {
return 0;
}
return (data & LIS2DH12_STATUS_REG_AUX_TDA_MASK) ? 1 : 0;
}

int LIS2DH12Class::readRegister(uint8_t address)
{
uint8_t value;

if (readRegisters(address, &value, sizeof(value)) != 1) {
return -1;
}

return value;
}

int LIS2DH12Class::readRegisters(uint8_t address, uint8_t* data, size_t length)
{
if (length > 1)
{
//For multi byte reads we must set the first bit to 1
address |= 0x80;
}

_wire->beginTransmission(_slaveAddress);
_wire->write(address);

if (_wire->endTransmission(false) != 0) {
return -1;
}

if (_wire->requestFrom(_slaveAddress, length) != length) {
return 0;
}

for (size_t i = 0; i < length; i++) {
*data++ = _wire->read();
}
return 1;
}

int LIS2DH12Class::writeRegister(uint8_t address, uint8_t value)
{
_wire->beginTransmission(_slaveAddress);
_wire->write(address);
_wire->write(value);
if (_wire->endTransmission() != 0) {
return 0;
}
return 1;
}

LIS2DH12Class MOTION(Wire, LIS2DH12_ADDRESS);
58 changes: 58 additions & 0 deletions ThirdParty/LIS2DH12/src/LIS2DH12.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
This file is part of the Arduino_LIS2DH12 library.
Copyright (c) 2021 Arduino SA. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <Wire.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define MOTION_SENSOR_LIS2DH12 0x33
#define LIS2DH12_ADDRESS 0x19U

class LIS2DH12Class {
public:
LIS2DH12Class(TwoWire& wire, uint8_t slaveAddress);
~LIS2DH12Class();

int begin();
void end();

// Accelerometer
int readAcceleration(float& x, float& y, float& z);
float accelerationSampleRate();
int accelerationAvailable();

// Temperature
int readTemperature(int & temperature_deg);
int temperatureAvailable();

private:
int readRegister(uint8_t address);
int readRegisters(uint8_t address, uint8_t* data, size_t length);
int writeRegister(uint8_t address, uint8_t value);


private:
TwoWire* _wire;
uint8_t _slaveAddress;
};

extern LIS2DH12Class MOTION;
7 changes: 7 additions & 0 deletions ThirdParty/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ constexpr uint32_t __bitset(const int (&a)[N], size_t i = 0U) {
#define WIRE_BUFFER_SIZE 128
#endif

#ifdef BOARD_MICROMOD
#define SDA 4
#define SCL 5
#define PIN_WIRE1_SDA 26
#define PIN_WIRE1_SCL 27
#else
// Wire
#define SDA 12
#define SCL 13

#define PIN_WIRE1_SDA 6
#define PIN_WIRE1_SCL 7
#endif

class TwoWire {
public:
Expand Down
4 changes: 4 additions & 0 deletions edge-impulse-sdk/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ utensor.lib
utensor/libutensor.a
*.o
*.d
doc/
node_modules/
package-lock.json
package.json
Loading