Skip to content

Commit

Permalink
Merge pull request #103 from rtlopez/develop
Browse files Browse the repository at this point in the history
prepare version v0.2.0-RC3
  • Loading branch information
rtlopez authored Dec 11, 2023
2 parents abb57ae + 8c731d3 commit 1789a52
Show file tree
Hide file tree
Showing 44 changed files with 831 additions and 757 deletions.
5 changes: 3 additions & 2 deletions docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ Logging using serial device is possible, like [D-ronin OpenLager](https://github
Recommended settings

- To log with 1k rate, you need device that is capable to receive data with 250kbps.
- To log with 2k rate, you need 500kbps
- To log with 1k rate with debug, baro and mag data, 500kbps is recommended.
- To log with 2k rate, you need 1Mbps

OpenLager can handle it easily. If you plan to use OpenLog, you might need to flash [blackbox-firmware](https://github.com/cleanflight/blackbox-firmware) to be able to handle more than 115.2kbps. Either 250kbps and 500kbps works well with modern SD cards up to 32GB.
OpenLager can handle it easily. If you plan to use OpenLog, you might need to flash [blackbox-firmware](https://github.com/cleanflight/blackbox-firmware) to be able to handle more than 115.2kbps. Either 250kbps and 500kbps works well with modern SD cards up to 32GB size.

## Limitations

Expand Down
20 changes: 4 additions & 16 deletions lib/Espfc/src/Blackbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ class BlackboxBuffer

size_t availableForWrite() const
{
//return _stream->availableForWrite();
return SIZE - _idx;
}

size_t isTxFifoEmpty() const
{
//return _stream->isTxFifoEmpty();
return _idx == 0;
}

static const size_t SIZE = 128;
static const size_t SIZE = SERIAL_TX_FIFO_SIZE;//128;

Espfc::Device::SerialDevice * _stream;
size_t _idx;
Expand Down Expand Up @@ -317,20 +319,6 @@ class Blackbox
return 1;
}

int onAppEvent(const Event& e)
{
switch(e.type)
{
case EVENT_MIXER_UPDATED:
update();
_model.state.appQueue.send(Event(EVENT_BBLOG_UPDATED));
return 1;
default:
break;
}
return 0;
}

int update()
{
if(!_model.blackboxEnabled()) return 0;
Expand Down Expand Up @@ -368,7 +356,7 @@ class Blackbox
acc.accADC[i] = _model.state.accel[i] * ACCEL_G_INV * acc.dev.acc_1G;
}
if(_model.magActive()) {
mag.magADC[i] = _model.state.mag[i];
mag.magADC[i] = _model.state.mag[i] * 1090;
}
if(_model.baroActive()) {
baro.altitude = lrintf(_model.state.baroAltitude * 100.f); // cm
Expand Down
27 changes: 21 additions & 6 deletions lib/Espfc/src/Cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -1200,14 +1200,29 @@ class Cli
s.println();
printStats(s);
s.println();
for(size_t i = 0; i < COUNTER_COUNT; ++i)
for(int i = 0; i < COUNTER_COUNT; ++i)
{
s.print(FPSTR(_model.state.stats.getName((StatCounter)i)));
StatCounter c = (StatCounter)i;
int time = lrintf(_model.state.stats.getTime(c));
float load = _model.state.stats.getLoad(c);
int freq = lrintf(_model.state.stats.getFreq(c));

s.print(FPSTR(_model.state.stats.getName(c)));
s.print(": ");
s.print((int)(_model.state.stats.getTime((StatCounter)i)), 1);
s.print("us, ");
s.print(_model.state.stats.getLoad((StatCounter)i), 1);
s.print("%");
if(time < 100) s.print(' ');
if(time < 10) s.print(' ');
s.print(time);
s.print("us, ");

if(load < 10) s.print(' ');
s.print(load, 1);
s.print("%, ");

if(freq < 1000) s.print(' ');
if(freq < 100) s.print(' ');
if(freq < 10) s.print(' ');
s.print(freq);
s.print(" Hz");
s.println();
}
s.print(F(" TOTAL: "));
Expand Down
21 changes: 0 additions & 21 deletions lib/Espfc/src/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,6 @@ class Controller
return 1;
}

int onAppEvent(const Event& e)
{
switch(e.type)
{
case EVENT_GYRO_READ:
_model.state.loopUpdate = true;
return 1;
case EVENT_SENSOR_READ:
if(_model.state.loopUpdate)
{
update();
_model.state.loopUpdate = false;
_model.state.appQueue.send(Event(EVENT_PID_UPDATED));
}
return 1;
default:
break;
}
return 0;
}

int update()
{
uint32_t startTime = 0;
Expand Down
9 changes: 6 additions & 3 deletions lib/Espfc/src/Device/BusAwareDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@ namespace Device {
class BusAwareDevice
{
public:
void setBus(BusDevice * bus, uint8_t addr, uint8_t masterAddr = 0)
void setBus(BusDevice * bus, uint8_t addr)
{
_bus = bus;
_addr = addr;
_masterAddr = masterAddr;
}

const BusDevice * getBus() const
{
return _bus;
}

uint8_t getAddress() const
{
return _addr;
}

protected:
BusDevice * _bus;
uint8_t _addr;
uint8_t _masterAddr;
};

}
Expand Down
38 changes: 12 additions & 26 deletions lib/Espfc/src/Device/BusDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <functional>
#include <Arduino.h>
#include "Math/Bits.h"

#define ESPFC_BUS_TIMEOUT 100

Expand All @@ -13,6 +14,7 @@ enum BusType {
BUS_AUTO,
BUS_I2C,
BUS_SPI,
BUS_SLV,
BUS_MAX
};

Expand Down Expand Up @@ -55,7 +57,7 @@ class BusDevice
{
uint8_t b;
uint8_t count = readByte(devAddr, regAddr, &b);
*data = b & (1 << bitNum);
*data = Math::getBit(b, bitNum);
return count;
}

Expand All @@ -64,23 +66,17 @@ class BusDevice
uint8_t count, b;
if ((count = readByte(devAddr, regAddr, &b)) != 0)
{
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
b &= mask;
b >>= (bitStart - length + 1);
*data = b;
*data = Math::getBitsMsb(b, bitStart, length);
}
return count;
}

int8_t readBitsBMI160(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data)
int8_t readBitsLsb(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data)
{
uint8_t count, b;
if ((count = readByte(devAddr, regAddr, &b)) != 0)
{
uint8_t mask = (1 << length) - 1;
b >>= bitStart;
b &= mask;
*data = b;
*data = Math::getBitsLsb(b, bitStart, length);
}
return count;
}
Expand All @@ -89,7 +85,7 @@ class BusDevice
{
uint8_t b;
readByte(devAddr, regAddr, &b);
b = (data != 0) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum));
b = Math::setBit(b, bitNum, data);
return writeByte(devAddr, regAddr, b);
}

Expand All @@ -98,27 +94,19 @@ class BusDevice
uint8_t b = 0;
if (readByte(devAddr, regAddr, &b) != 0)
{
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
data <<= (bitStart - length + 1); // shift data into correct position
data &= mask; // zero all non-important bits in data
b &= ~(mask); // zero all important bits in existing byte
b |= data; // combine data with existing byte
b = Math::setBitsMsb(b, bitStart, length, data);
return writeByte(devAddr, regAddr, b);
} else {
return false;
}
}

bool writeBitsBMI160(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data)
bool writeBitsLsb(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data)
{
uint8_t b = 0;
if (readByte(devAddr, regAddr, &b) != 0)
{
uint8_t mask = ((1 << length) - 1) << bitStart;
data <<= bitStart; // shift data into correct position
data &= mask; // zero all non-important bits in data
b &= ~(mask); // zero all important bits in existing byte
b |= data; // combine data with existing byte
b = Math::setBitsLsb(b, bitStart, length, data);
return writeByte(devAddr, regAddr, b);
} else {
return false;
Expand All @@ -130,9 +118,7 @@ class BusDevice
uint8_t b = 0;
if (readByte(devAddr, regAddr, &b) != 0)
{
data &= mask; // zero all non-important bits in data
b &= ~(mask); // zero all important bits in existing byte
b |= data; // combine data with existing byte
b = Math::setMasked(b, mask, data);
return writeByte(devAddr, regAddr, b);
} else {
return false;
Expand All @@ -141,7 +127,7 @@ class BusDevice

static const char ** getNames()
{
static const char* busDevChoices[] = { PSTR("NONE"), PSTR("AUTO"), PSTR("I2C"), PSTR("SPI"), NULL };
static const char* busDevChoices[] = { PSTR("NONE"), PSTR("AUTO"), PSTR("I2C"), PSTR("SPI"), PSTR("SLV"), NULL };
return busDevChoices;
}

Expand Down
96 changes: 96 additions & 0 deletions lib/Espfc/src/Device/BusSlave.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#pragma once

#define MPU6050_I2C_SLV0_ADDR 0x25
#define MPU6050_I2C_SLV0_REG 0x26
#define MPU6050_I2C_SLV0_DO 0x63
#define MPU6050_I2C_SLV0_CTRL 0x27
#define MPU6050_I2C_SLV0_EN 0x80
#define MPU6050_EXT_SENS_DATA_00 0x49

namespace Espfc {

namespace Device {

class BusSlave: public BusDevice, public BusAwareDevice
{
public:
BusSlave() {}

int begin(BusDevice * dev, int addr)
{
setBus(dev, addr);

return 1;
}

BusType getType() const override { return BUS_SLV; }

virtual int8_t read(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data) override
{
// set slave 0 to the AK8963 and set for read
if(!writeMaster(MPU6050_I2C_SLV0_ADDR, devAddr | 0x80)) {
return 0;
}
// set the register to the desired AK8963 sub address
if(!writeMaster(MPU6050_I2C_SLV0_REG, regAddr)) {
return 0;
}
// enable I2C and request the bytes
if(!writeMaster(MPU6050_I2C_SLV0_CTRL, MPU6050_I2C_SLV0_EN | length)) {
return 0;
}

// takes some time for these registers to fill
delay(1);

// read the bytes off the EXT_SENS_DATA registers
int8_t res = readMaster(MPU6050_EXT_SENS_DATA_00, length, data);

return res;
}

// readFast() ignores devAddr and regAddr args and read ext sensor data reg from master
virtual int8_t readFast(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data) override
{
return _bus->readFast(_addr, MPU6050_EXT_SENS_DATA_00, length, data);
}

// writes only one byte, length is ignored
virtual bool write(uint8_t devAddr, uint8_t regAddr, uint8_t length, const uint8_t* data) override
{
// set slave 0 to the AK8963 and set for write
if(!writeMaster(MPU6050_I2C_SLV0_ADDR, devAddr)) {
return false;
}
// set the register to the desired AK8963 sub address
if(!writeMaster(MPU6050_I2C_SLV0_REG, regAddr)) {
return false;
}
// store the data for write
if(!writeMaster(MPU6050_I2C_SLV0_DO, *data)) {
return false;
}
// enable I2C and send 1 byte
if(!writeMaster(MPU6050_I2C_SLV0_CTRL, MPU6050_I2C_SLV0_EN | 0x01)) {
return false;
}

return true;
}

int8_t writeMaster(uint8_t regAddr, uint8_t data)
{
int8_t res = _bus->write(_addr, regAddr, 1, &data);
delay(10);
return res;
}

int8_t readMaster(uint8_t regAddr, uint8_t length, uint8_t *data)
{
return _bus->read(_addr, regAddr, length, data);
}
};

}

}
Loading

0 comments on commit 1789a52

Please sign in to comment.