Skip to content

Commit

Permalink
Feat support external tmp117 (#337)
Browse files Browse the repository at this point in the history
* feat: Support external TMP117

* Remove PVS

* fix: Enable external tmp117 by default

* Update tests with external tmp

* fix: Default to fastest I2C

* Measure for every tx in LL

* Lower measurement freq in LL

* Add flash protect function

* AStyle

* Add flash protection to tests

* Fix DF 5 rotating to DF 8
  • Loading branch information
ojousima authored Mar 13, 2024
1 parent 2778a3f commit b483257
Show file tree
Hide file tree
Showing 13 changed files with 621 additions and 297 deletions.
699 changes: 430 additions & 269 deletions Doxyfile

Large diffs are not rendered by default.

11 changes: 1 addition & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ SOURCES=${RUUVI_PRJ_SOURCES}
OBJECTS=$(SOURCES:.c=.o)
IOBJECTS=$(SOURCES:.c=.o.PVS-Studio.i)
POBJECTS=$(SOURCES:.c=.o.PVS-Studio.log)
EXECUTABLE=ruuvifw
SONAR=firmware_analysis

# Tag on this commit
Expand All @@ -242,22 +241,14 @@ VERSION := $(if $(TAG),$(TAG),$(COMMIT))

.PHONY: astyle clean doxygen sonar pvs

all: clean doxygen pvs $(SOURCES) $(EXECUTABLE)
all: clean doxygen $(SOURCES) $(EXECUTABLE)

pvs: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
# Converting
plog-converter -a 'GA:1,2,3;OP:1,2,3;CS:1,2,3;MISRA:1,2,3' -t $(LOG_FORMAT) $(POBJECTS) -o $(PVS_LOG)
plog-converter -a 'GA:1;OP:1;CS:1;MISRA:1' --excludedCodes=V1042 -t errorfile $(POBJECTS) -o ./pvs.error

.c.o:
# Build
$(CXX) $(CFLAGS) $< $(DFLAGS) $(INC_PARAMS) $(OFLAGS) -o $@
# Preprocessing
$(CXX) $(CFLAGS) $< $(DFLAGS) $(INC_PARAMS) -E -o $@.PVS-Studio.i
# Analysis
pvs-studio --cfg $(PVS_CFG) --source-file $< --i-file $@.PVS-Studio.i --output-file $@.PVS-Studio.log

sonar: $(SOURCES) $(SONAR)
$(SONAR): $(ANALYSIS)
Expand Down
4 changes: 2 additions & 2 deletions src/app_heartbeat.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#define U8_MASK (0xFFU)
#define APP_DF_3_ENABLED RE_3_ENABLED
#define APP_DF_5_ENABLED RE_5_ENABLED
#define APP_DF_8_ENABLED RE_5_ENABLED
#define APP_DF_8_ENABLED RE_8_ENABLED
#define APP_DF_C5_ENABLED RE_C5_ENABLED
#define APP_DF_FA_ENABLED RE_FA_ENABLED

Expand Down Expand Up @@ -101,7 +101,7 @@ void heartbeat (void * p_event, uint16_t event_size)
msg.data_length = (uint8_t) buffer_len;
err_code = send_adv (&msg);
// Advertising should always be successful
RD_ERROR_CHECK (err_code, ~RD_ERROR_FATAL);
RD_ERROR_CHECK (err_code, RD_SUCCESS);

if (RD_SUCCESS == err_code)
{
Expand Down
28 changes: 25 additions & 3 deletions src/app_sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ static rt_sensor_ctx_t shtcx = APP_SENSOR_SHTCX_DEFAULT_CFG;
static rt_sensor_ctx_t tmp117 = APP_SENSOR_TMP117_DEFAULT_CFG;
#endif

#if APP_SENSOR_TMP117EXT_ENABLED
static rt_sensor_ctx_t tmp117ext = APP_SENSOR_TMP117EXT_DEFAULT_CFG;
#endif

#if APP_SENSOR_PHOTO_ENABLED
static rt_sensor_ctx_t photo = APP_SENSOR_PHOTO_DEFAULT_CFG;
#endif
Expand All @@ -125,9 +129,13 @@ static
void
m_sensors_init (void)
{
// Due to TMP117 driver implementation, if there are many instances the last instance will be used.
#if APP_SENSOR_TMP117_ENABLED
m_sensors[TMP117_INDEX] = &tmp117;
#endif
#if APP_SENSOR_TMP117EXT_ENABLED
m_sensors[TMP117EXT_INDEX] = &tmp117ext;
#endif
#if APP_SENSOR_SHTCX_ENABLED
m_sensors[SHTCX_INDEX] = &shtcx;
#endif
Expand Down Expand Up @@ -254,7 +262,7 @@ static ri_spi_frequency_t rb_to_ri_spi_freq (unsigned int rb_freq)
return freq;
}

static rd_status_t app_sensor_buses_init (void)
static rd_status_t app_sensor_buses_init (ri_i2c_frequency_t i2c_freq)
{
rd_status_t err_code = RD_SUCCESS;
ri_gpio_id_t ss_pins[RB_SPI_SS_NUMBER] = RB_SPI_SS_LIST;
Expand All @@ -274,7 +282,7 @@ static rd_status_t app_sensor_buses_init (void)
.sda = RB_I2C_SDA_PIN,
.scl = RB_I2C_SCL_PIN,
.bus_pwr = RB_I2C_BUS_POWER_PIN,
.frequency = rb_to_ri_i2c_freq (RB_I2C_FREQ)
.frequency = i2c_freq
};

if ( (!ri_gpio_is_init()) || (!ri_gpio_interrupt_is_init()))
Expand Down Expand Up @@ -319,7 +327,11 @@ rd_status_t app_sensor_init (void)
{
rd_status_t err_code = RD_SUCCESS;
m_sensors_init();
err_code |= app_sensor_buses_init();
ri_i2c_frequency_t i2c_freq = rb_to_ri_i2c_freq (RB_I2C_FREQ);
// Initialize with slowest frequency supported by board to check all sensors
err_code |= app_sensor_buses_init (i2c_freq);
// Assume maximum speed board supports, if a sensor supports only lower speeds will get downgraded
i2c_freq = RB_I2C_MAX_SPD;

if (RD_SUCCESS == err_code)
{
Expand Down Expand Up @@ -365,6 +377,12 @@ rd_status_t app_sensor_init (void)
init_code = rt_sensor_configure (m_sensors[ii]);
rt_sensor_store (m_sensors[ii]);
}

// Update board max I2C speed
if (i2c_freq > m_sensors[ii]->i2c_max_speed)
{
i2c_freq = m_sensors[ii]->i2c_max_speed;
}
}
else if (RD_ERROR_SELFTEST == init_code)
{
Expand All @@ -376,6 +394,10 @@ rd_status_t app_sensor_init (void)
m_sensors[ii]->handle = APP_SENSOR_HANDLE_UNUSED;
}
}

// Reinit board with fastest speed supported by board + sensors.
err_code |= app_sensor_buses_uninit();
err_code |= app_sensor_buses_init (i2c_freq);
}

return err_code;
Expand Down
57 changes: 48 additions & 9 deletions src/app_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@

enum
{
// Due to TMP117 driver implementation, if there are many instances the last instance will be used.
#if APP_SENSOR_TMP117_ENABLED
TMP117_INDEX,
#endif
#if APP_SENSOR_TMP117EXT_ENABLED
TMP117EXT_INDEX,
#endif
#if APP_SENSOR_SHTCX_ENABLED
SHTCX_INDEX,
#endif
Expand Down Expand Up @@ -103,7 +107,8 @@ void m_sensors_init (void); //!< Give Ceedling a handle to initialize structs.
.pwr_pin = RB_BME280_SENSOR_POWER_PIN, \
.pwr_on = RI_GPIO_HIGH, \
.fifo_pin = RI_GPIO_ID_UNUSED, \
.level_pin = RI_GPIO_ID_UNUSED \
.level_pin = RI_GPIO_ID_UNUSED, \
.i2c_max_speed = RB_I2C_MAX_SPD \
}
#endif

Expand All @@ -127,7 +132,8 @@ void m_sensors_init (void); //!< Give Ceedling a handle to initialize structs.
.pwr_pin = RB_DPS310_SENSOR_POWER_PIN, \
.pwr_on = RI_GPIO_HIGH, \
.fifo_pin = RI_GPIO_ID_UNUSED, \
.level_pin = RI_GPIO_ID_UNUSED \
.level_pin = RI_GPIO_ID_UNUSED, \
.i2c_max_speed = RB_I2C_MAX_SPD \
}
#endif

Expand All @@ -151,7 +157,8 @@ void m_sensors_init (void); //!< Give Ceedling a handle to initialize structs.
.pwr_pin = RB_LIS2DH12_SENSOR_POWER_PIN, \
.pwr_on = RI_GPIO_HIGH, \
.fifo_pin = RB_INT_FIFO_PIN, \
.level_pin = RB_INT_LEVEL_PIN \
.level_pin = RB_INT_LEVEL_PIN, \
.i2c_max_speed = RB_I2C_MAX_SPD \
}
#endif

Expand All @@ -168,7 +175,8 @@ void m_sensors_init (void); //!< Give Ceedling a handle to initialize structs.
.pwr_pin = RI_GPIO_ID_UNUSED, \
.pwr_on = RI_GPIO_HIGH, \
.fifo_pin = RB_INT_ACC1_PIN, \
.level_pin = RB_INT_ACC2_PIN \
.level_pin = RB_INT_ACC2_PIN, \
.i2c_max_speed = RB_I2C_MAX_SPD \
}
#endif

Expand All @@ -192,7 +200,8 @@ void m_sensors_init (void); //!< Give Ceedling a handle to initialize structs.
.pwr_pin = RB_SHTCX_SENSOR_POWER_PIN, \
.pwr_on = RI_GPIO_HIGH, \
.fifo_pin = RI_GPIO_ID_UNUSED, \
.level_pin = RI_GPIO_ID_UNUSED \
.level_pin = RI_GPIO_ID_UNUSED, \
.i2c_max_speed = RB_SHTCX_I2C_MAX_SPD \
}
#endif

Expand All @@ -217,7 +226,34 @@ void m_sensors_init (void); //!< Give Ceedling a handle to initialize structs.
.pwr_pin = RB_TMP117_SENSOR_POWER_PIN, \
.pwr_on = RI_GPIO_HIGH, \
.fifo_pin = RI_GPIO_ID_UNUSED, \
.level_pin = RI_GPIO_ID_UNUSED \
.level_pin = RI_GPIO_ID_UNUSED, \
.i2c_max_speed = RB_TMP117_I2C_MAX_SPD \
}
#endif

#if APP_SENSOR_TMP117EXT_ENABLED
#define APP_SENSOR_TMP117EXT_DEFAULT_CFG \
{ \
.sensor = {0}, \
.init = &ri_tmp117_init, \
.configuration = \
{ \
.dsp_function = APP_SENSOR_TMP117_DSP_FUNC, \
.dsp_parameter = APP_SENSOR_TMP117_DSP_PARAM, \
.mode = APP_SENSOR_TMP117_MODE, \
.resolution = APP_SENSOR_TMP117_RESOLUTION, \
.samplerate = APP_SENSOR_TMP117_SAMPLERATE, \
.scale = APP_SENSOR_TMP117_SCALE \
}, \
.nvm_file = APP_FLASH_SENSOR_FILE, \
.nvm_record = APP_FLASH_SENSOR_TMP117_RECORD, \
.bus = RD_BUS_I2C, \
.handle = RB_TMP117EXT_I2C_ADDRESS, \
.pwr_pin = RB_TMP117_SENSOR_POWER_PIN, \
.pwr_on = RI_GPIO_HIGH, \
.fifo_pin = RI_GPIO_ID_UNUSED, \
.level_pin = RI_GPIO_ID_UNUSED, \
.i2c_max_speed = RB_TMP117EXT_I2C_MAX_SPD \
}
#endif

Expand All @@ -234,7 +270,8 @@ void m_sensors_init (void); //!< Give Ceedling a handle to initialize structs.
.pwr_pin = RB_PHOTO_PWR_PIN, \
.pwr_on = RB_PHOTO_ACTIVE, \
.fifo_pin = RI_GPIO_ID_UNUSED, \
.level_pin = RI_GPIO_ID_UNUSED \
.level_pin = RI_GPIO_ID_UNUSED, \
.i2c_max_speed = RB_I2C_MAX_SPD \
}
#endif

Expand All @@ -251,7 +288,8 @@ void m_sensors_init (void); //!< Give Ceedling a handle to initialize structs.
.pwr_pin = RB_NTC_PWR_PIN, \
.pwr_on = RB_NTC_ACTIVE, \
.fifo_pin = RI_GPIO_ID_UNUSED, \
.level_pin = RI_GPIO_ID_UNUSED \
.level_pin = RI_GPIO_ID_UNUSED, \
.i2c_max_speed = RB_I2C_MAX_SPD \
}
#endif

Expand All @@ -276,7 +314,8 @@ void m_sensors_init (void); //!< Give Ceedling a handle to initialize structs.
.pwr_pin = RI_GPIO_ID_UNUSED, \
.pwr_on = RI_GPIO_LOW, \
.fifo_pin = RI_GPIO_ID_UNUSED, \
.level_pin = RI_GPIO_ID_UNUSED \
.level_pin = RI_GPIO_ID_UNUSED, \
.i2c_max_speed = RB_I2C_MAX_SPD \
}
#endif

Expand Down
12 changes: 10 additions & 2 deletions src/application_config/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@
# define APP_SENSOR_TMP117_ENABLED RB_ENVIRONMENTAL_TMP117_PRESENT
#endif

/** @brief Enable External TMP117 temperature sensor */
#ifndef APP_SENSOR_TMP117EXT_ENABLED
# define APP_SENSOR_TMP117EXT_ENABLED RB_ENVIRONMENTAL_TMP117EXT_PRESENT
#endif

#ifndef APP_SENSOR_TMP117_DSP_FUNC
# define APP_SENSOR_TMP117_DSP_FUNC RD_SENSOR_DSP_LAST //!< Do not use DSP by default
#endif
Expand Down Expand Up @@ -407,9 +412,12 @@
* @brief Enable all possible dataformats for unit testing.
*/
#ifdef CEEDLING
# define ENABLE_ALL_DATAFORMATS (1U)
# define TEST_ALL_DATAFORMATS (1U)
#else
# define ENABLE_ALL_DATAFORMATS (0U)
# define TEST_ALL_DATAFORMATS (0U)
#endif
#ifndef ENABLE_ALL_DATAFORMATS
# define ENABLE_ALL_DATAFORMATS (TEST_ALL_DATAFORMATS)
#endif

/**
Expand Down
1 change: 1 addition & 0 deletions src/application_config/application_mode_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define RI_LOG_ENABLED 1
#define APP_LOG_LEVEL RI_LOG_LEVEL_INFO
#define APP_WDT_INTERVAL_MS (10U*60U*1000U)
#define ENABLE_ALL_DATAFORMATS (1U)

/** @brief Communicate sensor data at this interval. 221 matches Apple guideline. */
#define APP_BLE_INTERVAL_MS (221U)
Expand Down
4 changes: 4 additions & 0 deletions src/application_config/application_mode_longlife.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@

#define RT_FLASH_ENABLED (0U)

#define APP_SENSOR_LIS2DH12_SAMPLERATE (1U) //!< Hz

#define APP_SENSOR_TMP117_SAMPLERATE RD_SENSOR_CFG_CUSTOM_2 // 8000 ms conversion cycle.

#endif
26 changes: 26 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "main.h"
#include "run_integration_tests.h"
#include "ruuvi_interface_log.h"
#include "ruuvi_interface_flash.h"
#include "ruuvi_interface_power.h"
#include "ruuvi_interface_scheduler.h"
#include "ruuvi_interface_timer.h"
Expand Down Expand Up @@ -56,6 +57,30 @@ void app_on_error (const rd_status_t error,
}
}

#ifndef CEEDLING
static
#endif
rd_status_t protect_flash (void)
{
rd_status_t err_code = RD_SUCCESS;
#if RI_FLASH_ENABLED

// Protect softdevice
for (size_t page = 0; page < 0x26; page++)
{
err_code |= ri_flash_protect (page);
}

// Protect bootloader
for (size_t page = 0x75; page < 0x80; page++)
{
err_code |= ri_flash_protect (page);
}

#endif
return err_code;
}

/**
* @brief setup MCU peripherals and board peripherals.
*
Expand All @@ -68,6 +93,7 @@ void setup (void)
err_code |= ri_watchdog_init (APP_WDT_INTERVAL_MS, &on_wdt);
err_code |= ri_log_init (APP_LOG_LEVEL); // Logging to terminal.
# endif
err_code |= protect_flash();
err_code |= ri_yield_init();
err_code |= ri_timer_init();
err_code |= ri_scheduler_init();
Expand Down
2 changes: 1 addition & 1 deletion src/ruuvi.boards.c
2 changes: 1 addition & 1 deletion src/ruuvi.drivers.c
Loading

0 comments on commit b483257

Please sign in to comment.