Skip to content

Commit

Permalink
https://github.com/rusefi/rusefi/issues/6997
Browse files Browse the repository at this point in the history
extracting class
  • Loading branch information
rusefillc committed Oct 23, 2024
1 parent d57d64d commit 80c3d52
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 61 deletions.
3 changes: 3 additions & 0 deletions SENT-box/firmware/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ CPPSRC = $(ALLCPPSRC) \
main.cpp \
can.cpp \
uart.cpp \
sent_gm_fuel_sensor.cpp \
sent.cpp \
sent_hw_icu.cpp \
sent_hw_pal.cpp \
Expand All @@ -151,6 +152,8 @@ CWARN = -Wall -Wextra -Wundef -Wstrict-prototypes
# Define C++ warning options here.
CPPWARN = -Wall -Wextra -Wundef

CPPWARN += -Werror=undef

#
# Project, target, sources and paths
##############################################################################
Expand Down
56 changes: 9 additions & 47 deletions SENT-box/firmware/sent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "hal.h"

#include "sent.h"
#include "sent_gm_fuel_sensor.h"

struct sent_channel {
SM_SENT_enum state;
Expand Down Expand Up @@ -43,10 +44,7 @@ static struct sent_channel channels[SENT_CHANNELS_NUM];
int32_t si7215_magnetic[SENT_CHANNELS_NUM];
int32_t si7215_counter[SENT_CHANNELS_NUM];

/* GM DI fuel pressure, temperature sensor decoded data */
int32_t gm_sig0[SENT_CHANNELS_NUM];
int32_t gm_sig1[SENT_CHANNELS_NUM];
int32_t gm_stat[SENT_CHANNELS_NUM];
SentGmFuelSensor sentGmFuelSensor;

#if SENT_DEV == SENT_GM_ETB

Expand Down Expand Up @@ -439,42 +437,6 @@ int32_t Si7215_GetCounter(uint32_t n)
return si7215_counter[n];
}

/* GM DI fuel pressure, temperature sensor data */
int32_t gm_GetSig0(uint32_t n)
{
return gm_sig0[n];
}

int32_t gm_GetSig1(uint32_t n)
{
return gm_sig1[n];
}

int32_t gm_GetStat(uint32_t n)
{
return gm_stat[n];
}

int32_t gm_GetPressure(uint32_t n)
{
/* two pressure signals:
* Sig0 occupie 3 first nibbles in MSB..LSB order
* Sig1 occupit next 3 nibbles in LSB..MSB order
* Signals are close, but not identical.
* Sig0 shows about 197..198 at 1 Atm (open air) and 282 at 1000 KPa (9.86 Atm)
* Sig1 shows abour 202..203 at 1 Atm (open air) and 283 at 1000 KPa (9.86 Atm)
* So for 8.86 Atm delta there are:
* 84..85 units for sig0
* 80..81 units for sig1
* Measurements are not ideal, so lets ASSUME 10 units per 1 Atm
* Offset is 187..188 for Sig0 and 192..193 for Sig1.
* Average offset is 180.
*/

/* in 0.001 Atm */
return ((gm_GetSig0(n) - 198 + 10 + gm_GetSig1(n) - 202 + 10) * 100 / 2);
}

/* 4 per channel should be enougth */
#define SENT_MB_SIZE (4 * SENT_CH_MAX)

Expand All @@ -501,33 +463,33 @@ static void SentDecoderThread(void*)
/* TODO: handle ret */
if (ret == MSG_OK) {
uint16_t tick = msg & 0xffff;
uint8_t n = (msg >> 16) & 0xff;
struct sent_channel *ch = &channels[n];
uint8_t channleIndex = (msg >> 16) & 0xff;
struct sent_channel *ch = &channels[channleIndex];

if (SENT_Decoder(ch, tick) > 0) {
/* decode Si7215 packet */
if (((~ch->nibbles[1 + 5]) & 0x0f) == ch->nibbles[1 + 0]) {
si7215_magnetic[n] =
si7215_magnetic[channleIndex] =
((ch->nibbles[1 + 0] << 8) |
(ch->nibbles[1 + 1] << 4) |
(ch->nibbles[1 + 2] << 0)) - 2048;
si7215_counter[n] =
si7215_counter[channleIndex] =
(ch->nibbles[1 + 3] << 4) |
(ch->nibbles[1 + 4] << 0);
}
/* decode GM DI fuel pressure, temperature sensor */
if (1) {
/* Sig0 occupie first 3 nibbles in MSB..LSB order
* Sig1 occupit next 3 nibbles in LSB..MSB order */
gm_sig0[n] =
sentGmFuelSensor.gm_sig0[channleIndex] =
(ch->nibbles[1 + 0] << 8) |
(ch->nibbles[1 + 1] << 4) |
(ch->nibbles[1 + 2] << 0);
gm_sig1[n] =
sentGmFuelSensor.gm_sig1[channleIndex] =
(ch->nibbles[1 + 3] << 0) |
(ch->nibbles[1 + 4] << 4) |
(ch->nibbles[1 + 5] << 8);
gm_stat[n] =
sentGmFuelSensor.gm_stat[channleIndex] =
ch->nibbles[0];
}
}
Expand Down
13 changes: 1 addition & 12 deletions SENT-box/firmware/sent.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@

#define SENT_DEV SENT_GM_ETB

#if SENT_DEV == SENT_GM_ETB
#define SENT_CHANNELS_NUM 2 // Number of sent channels
#elif SENT_DEV == SENT_SILABS_SENS
#define SENT_CHANNELS_NUM 4 // Number of sent channels
#endif
#include "sent_conf.h"

#define SENT_OFFSET_INTERVAL 12
#define SENT_SYNC_INTERVAL (56 - SENT_OFFSET_INTERVAL) // 56 ticks - 12
Expand Down Expand Up @@ -103,10 +99,3 @@ uint16_t SENT_GetSlowMessageID(uint32_t n, uint32_t i);
/* Si7215 decoded data */
int32_t Si7215_GetMagneticField(uint32_t n);
int32_t Si7215_GetCounter(uint32_t n);

/* GM DI fuel pressure, temperature sensor data */
int32_t gm_GetSig0(uint32_t n);
int32_t gm_GetSig1(uint32_t n);
int32_t gm_GetStat(uint32_t n);
int32_t gm_GetPressure(uint32_t n);

9 changes: 9 additions & 0 deletions SENT-box/firmware/sent_conf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "sent.h"

#if SENT_DEV == SENT_GM_ETB
#define SENT_CHANNELS_NUM 2 // Number of sent channels
#elif SENT_DEV == SENT_SILABS_SENS
#define SENT_CHANNELS_NUM 4 // Number of sent channels
#endif
2 changes: 2 additions & 0 deletions SENT-box/firmware/sent_gm_fuel_sensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

#include "sent_gm_fuel_sensor.h"
51 changes: 51 additions & 0 deletions SENT-box/firmware/sent_gm_fuel_sensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include "ch.h"
#include "hal.h"
#include "sent_conf.h"

class SentGmFuelSensor {
public:

/* GM DI fuel pressure, temperature sensor decoded data */
int32_t gm_sig0[SENT_CHANNELS_NUM];
int32_t gm_sig1[SENT_CHANNELS_NUM];
int32_t gm_stat[SENT_CHANNELS_NUM];

/* GM DI fuel pressure, temperature sensor data */
int32_t gm_GetSig0(uint32_t n)
{
return gm_sig0[n];
}

int32_t gm_GetSig1(uint32_t n)
{
return gm_sig1[n];
}

int32_t gm_GetStat(uint32_t n)
{
return gm_stat[n];
}

int32_t gm_GetPressure(uint32_t n)
{
/* two pressure signals:
* Sig0 occupie 3 first nibbles in MSB..LSB order
* Sig1 occupit next 3 nibbles in LSB..MSB order
* Signals are close, but not identical.
* Sig0 shows about 197..198 at 1 Atm (open air) and 282 at 1000 KPa (9.86 Atm)
* Sig1 shows abour 202..203 at 1 Atm (open air) and 283 at 1000 KPa (9.86 Atm)
* So for 8.86 Atm delta there are:
* 84..85 units for sig0
* 80..81 units for sig1
* Measurements are not ideal, so lets ASSUME 10 units per 1 Atm
* Offset is 187..188 for Sig0 and 192..193 for Sig1.
* Average offset is 180.
*/

/* in 0.001 Atm */
return ((gm_GetSig0(n) - 198 + 10 + gm_GetSig1(n) - 202 + 10) * 100 / 2);
}

};
7 changes: 5 additions & 2 deletions SENT-box/firmware/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "sent.h"
#include "io_pins.h"
#include "mcu-util.h"
#include "sent_gm_fuel_sensor.h"

extern SentGmFuelSensor sentGmFuelSensor;

static const UARTConfig uartCfg =
{
Expand Down Expand Up @@ -68,10 +71,10 @@ static void UartThread(void*)
SENT_GetFrameCnt(0)
);
#else
uint32_t gm_pressure = gm_GetPressure(0);
uint32_t gm_pressure = sentGmFuelSensor.gm_GetPressure(0);
writeCount += chsnprintf(printBuffer + writeCount, BUFFER_SIZE - writeCount,
" GM: St %x, Sig0 %04d, Sig1 %04d, %d.%03d Atm Tick = %04d nS.\r\n",
gm_GetStat(0), gm_GetSig0(0), gm_GetSig1(0), gm_pressure / 1000, gm_pressure % 1000,
sentGmFuelSensor.gm_GetStat(0), sentGmFuelSensor.gm_GetSig0(0), sentGmFuelSensor.gm_GetSig1(0), gm_pressure / 1000, gm_pressure % 1000,
SENT_GetTickTimeNs()
);

Expand Down

0 comments on commit 80c3d52

Please sign in to comment.