Compare commits

...

3 Commits

4 changed files with 138 additions and 38 deletions

View File

@ -0,0 +1,71 @@
/**
* @file bma456_wrapper.c
* @author Anatole SCHRAMM-HENRY
* @brief Source file containing custom helper functions that are not part of the Bosh official driver
* @version 0.1
* @date 2024-01-05
*
* @copyright MIT
*
*/
#include "bma456_wrapper.h"
#include "i2c.h"
#include "app_utils.h"
static BMA4_INTF_RET_TYPE _bma456_i2c_read(uint8_t reg_addr, uint8_t *read_data, uint32_t len, void *intf_ptr)
{
uint8_t dev_address = *(uint8_t*)intf_ptr;
return !i2c_read(dev_address, reg_addr, read_data, len);
}
static BMA4_INTF_RET_TYPE _bma456_i2c_write(uint8_t reg_addr, const uint8_t *read_data, uint32_t len, void *intf_ptr)
{
uint8_t dev_address = *(uint8_t*)intf_ptr;
return !i2c_write(dev_address, reg_addr, read_data, len);
}
static void _bma456_delay_us(uint32_t period, void *intf_ptr)
{
(void) intf_ptr;
us_delay(period);
}
bool bma456_wrapper_interface_init(struct bma4_dev *bma, uint8_t *dev_addr_ptr)
{
if(!bma || !dev_addr_ptr) return false;
/* Init the BMA456 */
bma->intf = BMA4_I2C_INTF;
bma->intf_ptr = dev_addr_ptr;
bma->bus_read = &(_bma456_i2c_read);
bma->bus_write = &(_bma456_i2c_write);
bma->variant = BMA45X_VARIANT;
bma->delay_us = &(_bma456_delay_us);
bma->read_write_len = 500; // Default was 46
bma->perf_mode_status = BMA4_DISABLE;
return true;
}
bool bma456_wrapper_is_config_file_already_loaded(struct bma4_dev *bma)
{
uint8_t status = 0;
if(!bma) return false;
/* Read the status of BMA4 accelerometer to know if the ASIC is running or not */
if(bma4_read_regs(BMA4_INTERNAL_STAT, &status, 1, bma) == BMA4_OK);
{
/* Only get the message bits */
status &= BMA4_CONFIG_STREAM_MESSAGE_MSK;
return status == BMA4_ASIC_INITIALIZED;
}
/* If we failed to read the register, just say it is not loaded */
return false;
}

View File

@ -0,0 +1,37 @@
/**
* @file bma456_wrapper.h
* @author Anatole SCHRAMM-HENRY
* @brief Header file containing custom helper functions that are not part of the Bosh official driver
* @version 0.1
* @date 2024-01-05
*
* @copyright MIT
*
*/
#ifndef BMA456_WRAPPER_H
#define BMA456_WRAPPER_H
#include "wm_type_def.h"
#include "bma456w.h"
/**
* @brief Helper function used to initialize the BMA456 driver interface (i2c read and write functions, us_delay functions and other settings).
*
* @param bma a pointer to a user allocated @ref bma4_dev object
* @param dev_addr_ptr a pointer to an uint8_t containing the device's i2c address
* @return true on initialization success
* @return false on failure
*/
bool bma456_wrapper_interface_init(struct bma4_dev *bma, uint8_t *dev_addr_ptr);
/**
* @brief Helper function returning the state of the ASIC's config file loading.
*
* @param bma a pointer to a user allocated and initialized @ref bma4_dev object
* @return true if the config file was already loaded in the BMA456
* @return false if a config file is not loaded in the BMA456 ie ASIC is not started
*/
bool bma456_wrapper_is_config_file_already_loaded(struct bma4_dev *bma);
#endif //BMA456_WRAPPER_H

View File

@ -9,6 +9,7 @@
#include "i2c.h"
#include "BMP280.h"
#include "bma456w.h"
#include "bma456_wrapper.h"
#include "CST816D.h"
#include "app_utils.h"
#include "watch_settings.h"
@ -48,8 +49,8 @@ static BatteryControllerStatusChangeCb_t _BatteryControllerStatusChangeCb = NULL
static bool _wakeup_is_io = false;
static bool _wakeup_is_timer = false;
/* BMA456 strcture */
struct
/* BMA456 structure */
static struct
{
uint8_t dev_addr;
struct bma4_dev bma;
@ -564,7 +565,7 @@ bool watch_peripherals_pressure_sensor_init(void)
else
APP_LOG_INFO("Inited BMP280");
if(!BMP280_configure(BMP280_Forced, BMP280_Oversampling_x16, BMP280_Oversampling_x16, BMP280_Filter_x16, BMP280_Standby_4000MS))
if(!BMP280_configure(BMP280_Forced, BMP280_Oversampling_x2, BMP280_Oversampling_x16, BMP280_Filter_x4, BMP280_Standby_4000MS))
{
APP_LOG_ERROR("Failed to configure BMP280");
return false;
@ -584,38 +585,15 @@ float watch_peripherals_pressure_sensor_get_pressure(float * const temperature)
return BMP280_get_pressure(temperature);
}
static BMA4_INTF_RET_TYPE _bma4_i2c_read(uint8_t reg_addr, uint8_t *read_data, uint32_t len, void *intf_ptr)
{
uint8_t dev_address = *(uint8_t*)intf_ptr;
return !i2c_read(dev_address, reg_addr, read_data, len);
}
static BMA4_INTF_RET_TYPE _bma4_i2c_write(uint8_t reg_addr, const uint8_t *read_data, uint32_t len, void *intf_ptr)
{
uint8_t dev_address = *(uint8_t*)intf_ptr;
return !i2c_write(dev_address, reg_addr, read_data, len);
}
static void _bma4_delay_us(uint32_t period, void *intf_ptr)
{
(void) intf_ptr;
us_delay(period);
}
bool watch_peripherals_accelerometer_init(void)
{
/* Init the BMA456 */
_bma456.bma.intf = BMA4_I2C_INTF;
_bma456.bma.intf_ptr = &_bma456.dev_addr;
_bma456.bma.bus_read = &(_bma4_i2c_read);
_bma456.bma.bus_write = &(_bma4_i2c_write);
_bma456.bma.variant = BMA45X_VARIANT;
_bma456.bma.delay_us = &(_bma4_delay_us);
_bma456.bma.read_write_len = 46;
_bma456.bma.perf_mode_status = BMA4_DISABLE;
if(bma456_wrapper_interface_init(&_bma456.bma, &_bma456.dev_addr))
APP_LOG_INFO("BMA456 interface init ok");
else
{
APP_LOG_ERROR("Failed to init BMA456 interface");
return false;
}
if(bma456w_init(&_bma456.bma) == BMA4_OK)
APP_LOG_INFO("BMA456 init");
@ -624,6 +602,14 @@ bool watch_peripherals_accelerometer_init(void)
APP_LOG_ERROR("Failed to init BMA456");
return false;
}
/* Before we do a soft reset of the sensor, let's check if the config file was not already loaded ... */
if(bma456_wrapper_is_config_file_already_loaded(&_bma456.bma))
{
APP_LOG_INFO("BMA456 config file already loaded");
return true;
}
else APP_LOG_INFO("BMA456 config file needs loading");
if(bma4_soft_reset(&_bma456.bma) == BMA4_OK)
APP_LOG_INFO("BMA456 soft reset");
@ -754,10 +740,10 @@ bool watch_peripherals_accelerometer_step_counter_enable(bool enable)
_bma456.step_counter_enable = enable;
if(bma456w_feature_enable(BMA456W_STEP_CNTR, BMA4_ENABLE, &_bma456.bma) == BMA4_OK)
APP_LOG_INFO("BMA456 step cnter feature enable ok");
APP_LOG_INFO("BMA456 step counter feature enable ok");
else
{
APP_LOG_ERROR("BMA456 step cnter feature enable failed");
APP_LOG_ERROR("BMA456 step counter feature enable failed");
return false;
}
@ -791,11 +777,10 @@ bool watch_peripherals_accelerometer_step_count_reset(void)
void watch_peripherals_watch_sleep(void)
{
extern LCDConfig_t LCDConfig;
// First, we disable the display backlight and we set all the peripherals in their low power mode
/* First, we disable the display backlight and we set all the peripherals in their low power mode */
lcd_set_backlight(&LCDConfig, 0);
lcd_sleep(&LCDConfig, true);
// Set a restore function to apply the correct power mode when we leave sleep
//watch_peripherals_magnetometer_power_mode_set(QMC5883L_Mode_Control_Standby);
/* Set a restore function to apply the correct power mode when we leave sleep */
if(CST816D_sleep())
APP_LOG_DEBUG("CST816D Sleep cmd ok");
else

View File

@ -402,6 +402,13 @@ static void saveSettingsToFlashCb(void)
static void performFactoryResetCb()
{
// Reset the BMA456 internal step counter
if(!watch_peripherals_accelerometer_step_count_reset())
{
APP_LOG_ERROR("Failed to reset the step counter");
return;
}
// Reload factory settings
persistency_factory_reset();