Skip to content

Commit 5606f82

Browse files
committed
Update driver code to accept point to application delay function
Updates the driver code to accept a function point to a delay function determined by the application. This allows the application to determine the delay implementation, for example, using a hardware timer. Also updates the test application to use new functionality.
1 parent d6125f1 commit 5606f82

File tree

4 files changed

+6
-32
lines changed

4 files changed

+6
-32
lines changed

libs/bme68x/Inc/bme68x_driver.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ typedef struct
9090
* @param[in,out] bme Pointer to newly initialized bme68x sensor interface
9191
* @param[in] i2c_handle Pointer to I2C handle.
9292
*/
93-
void bme_init(bme68x_sensor_t *bme, I2C_HandleTypeDef *i2c_handle);
93+
void bme_init(bme68x_sensor_t *bme, I2C_HandleTypeDef *i2c_handle, bme68x_delay_us_fptr_t delay_fn);
9494

9595
/**
9696
* @brief Returns basic status check
@@ -175,18 +175,6 @@ uint8_t bme_fetch_data(bme68x_sensor_t *bme);
175175
*/
176176
uint32_t bme_get_meas_dur(bme68x_sensor_t *bme, uint8_t opmode);
177177

178-
/**
179-
* @brief Implements the default microsecond delay callback
180-
*
181-
* Sets the delay_us function pointer on the bme68x_dev device struct.
182-
* This callback is used throughout the Bosch library whenever a microsecond
183-
* delay is necessary for proper device functioning.
184-
*
185-
* @param[in] period_us Duration of the delay in microseconds
186-
* @param[in] intf_ptr Pointer to the interface descriptor
187-
*/
188-
void bme_delay_us(uint32_t period_us, void *intf_ptr);
189-
190178
/**
191179
* @brief Implements the default I2C write transaction
192180
*

libs/bme68x/Src/bme68x_driver.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/* ========================== FUNCTION IMPLEMENTATIONS ========================== */
1717

1818
/** Initializes the BME68x sensor interface */
19-
void bme_init(bme68x_sensor_t *bme, I2C_HandleTypeDef *i2c_handle)
19+
void bme_init(bme68x_sensor_t *bme, I2C_HandleTypeDef *i2c_handle, bme68x_delay_us_fptr_t delay_fn)
2020
{
2121
if (bme == NULL)
2222
{
@@ -36,7 +36,7 @@ void bme_init(bme68x_sensor_t *bme, I2C_HandleTypeDef *i2c_handle)
3636
/** @todo (maybe) Assign variant ID on bme struct */
3737
bme->device.read = &bme_read;
3838
bme->device.write = &bme_write;
39-
bme->device.delay_us = bme_delay_us;
39+
bme->device.delay_us = delay_fn;
4040
bme->status = BME68X_OK;
4141
// Typical ambient temperature in Celsius
4242
bme->device.amb_temp = 25;
@@ -119,21 +119,6 @@ uint32_t bme_get_meas_dur(bme68x_sensor_t *bme, uint8_t opmode)
119119
return bme68x_get_meas_dur(opmode, &bme->conf, &bme->device);
120120
}
121121

122-
/** Implements the default microsecond delay callback */
123-
void bme_delay_us(uint32_t period_us, void *intf_ptr)
124-
{
125-
// intf_ptr is not used, but it is part of the standard function signature
126-
// so cast to void to avoid compiler warning
127-
(void)intf_ptr;
128-
/** @todo: Implement a microsecond delay here, possibly with a DWT cycle counter, or an actual hardware timer */
129-
// FIXME: Short-term implementation of a blocking delay
130-
volatile uint32_t cycles = period_us * 20;
131-
while (cycles--)
132-
{
133-
__NOP();
134-
}
135-
}
136-
137122
/** Implements the default microsecond delay callback */
138123
int8_t bme_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length, void *intf_ptr)
139124
{

targets/stm32L4xx/L476RG/Src/tim.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
113113
/** Example implementation for a microsecond delay callback */
114114
void delay_us_timer(uint32_t us, void *intf_ptr)
115115
{
116+
(void)intf_ptr;
116117
/* Reset completion flag */
117118
delay_complete = 0;
118119

tests/test_bme688/test_bme688.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ int main(void)
109109

110110
// Create bme interface struct and initialize it
111111
bme68x_sensor_t bme;
112-
bme_init(&bme, &hi2c1);
112+
bme_init(&bme, &hi2c1, &delay_us_timer);
113113
// Check status, should be 0 for OK
114114
int bme_status = bme_check_status(&bme);
115115
{
@@ -145,7 +145,7 @@ int main(void)
145145
// Set to forced mode, which takes a single sample and returns to sleep mode
146146
bme_set_opmode(&bme, BME68X_FORCED_MODE);
147147
/** @todo: May adjust the specific timing function called here, but it should be based on bme_get_meas_dur */
148-
bme_delay_us(bme_get_meas_dur(&bme, BME68X_SLEEP_MODE), &hi2c1);
148+
delay_us_timer(bme_get_meas_dur(&bme, BME68X_SLEEP_MODE), &hi2c1);
149149
// Fetch data
150150
int fetch_success = bme_fetch_data(&bme);
151151
if (fetch_success)

0 commit comments

Comments
 (0)