Skip to content

Commit

Permalink
Adapt the code to RIOT's coding conventions.
Browse files Browse the repository at this point in the history
Format the code with the uncrustify utility.
  • Loading branch information
dpproto committed Feb 19, 2024
1 parent b66d155 commit 1a59f06
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 38 deletions.
2 changes: 1 addition & 1 deletion drivers/abp2/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ config MODULE_ABP2_I2C
depends on HAS_PERIPH_I2C
depends on !MODULE_ABP2_SPI
select MODULE_PERIPH_I2C

comment "Select either the SPI or I2C interface"
depends on !MODULE_ABP2_SPI
depends on !MODULE_ABP2_I2C
Expand Down
2 changes: 0 additions & 2 deletions drivers/abp2/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

ifneq (,$(filter abp2_spi,$(USEMODULE)))
FEATURES_REQUIRED += periph_spi
endif

ifneq (,$(filter abp2_i2c,$(USEMODULE)))
FEATURES_REQUIRED += periph_i2c
endif

37 changes: 23 additions & 14 deletions drivers/abp2/abp2.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ int abp2_init(abp2_t *dev, const abp2_params_t *params)
#else
#pragma message("implement I2C code here")
#endif
if(res)
if (res) {
return ABP2_ERRBUS;
}

/* test status byte: the busy flag should clear in about 5ms */
while( (status & ABP2_STATUS_BUSY) && count)
{
while ((status & ABP2_STATUS_BUSY) && count) {
#if defined(MODULE_ABP2_SPI)
spi_acquire(params->spi, params->cs, SPI_MODE_0, params->clk);
status = spi_transfer_byte(params->spi, params->cs, false, ABP2_CMD_NOP);
Expand All @@ -84,8 +84,9 @@ int abp2_init(abp2_t *dev, const abp2_params_t *params)
count--;
}
/* the busy flag should be clear */
if(status & ABP2_STATUS_BUSY)
if (status & ABP2_STATUS_BUSY) {
res = ABP2_ERRSENS;
}

return res;
}
Expand All @@ -102,8 +103,9 @@ int abp2_measure(const abp2_t *dev)
#else
#pragma message("implement I2C code here")
#endif
if(res)
if (res) {
return ABP2_ERRBUS;
}
#if defined(MODULE_ABP2_SPI)
spi_acquire(dev->params->spi, dev->params->cs, SPI_MODE_0, dev->params->clk);
spi_transfer_bytes(dev->params->spi, dev->params->cs, false, data_tx_meas_start, data_rx, len);
Expand All @@ -112,8 +114,9 @@ int abp2_measure(const abp2_t *dev)
#pragma message("implement I2C code here")
#endif
status = data_rx[0];
if(status & ABP2_STATUS_MASK)
if (status & ABP2_STATUS_MASK) {
return ABP2_ERRSENS;
}

return res;
}
Expand All @@ -128,8 +131,9 @@ uint8_t abp2_getstatus(const abp2_t *dev)
#else
#pragma message("implement I2C code here")
#endif
if(res)
if (res) {
return ABP2_ERRBUS;
}
#if defined(MODULE_ABP2_SPI)
spi_acquire(dev->params->spi, dev->params->cs, SPI_MODE_0, dev->params->clk);
status = spi_transfer_byte(dev->params->spi, dev->params->cs, false, ABP2_CMD_NOP);
Expand All @@ -153,8 +157,9 @@ int abp2_read_raw(const abp2_t *dev, abp2_raw_t *raw_values)
#else
#pragma message("implement I2C code here")
#endif
if(res)
if (res) {
return ABP2_ERRBUS;
}
#if defined(MODULE_ABP2_SPI)
spi_acquire(dev->params->spi, dev->params->cs, SPI_MODE_0, dev->params->clk);
spi_transfer_bytes(dev->params->spi, dev->params->cs, false, data_tx_nop_read, data_rx, len);
Expand All @@ -163,17 +168,18 @@ int abp2_read_raw(const abp2_t *dev, abp2_raw_t *raw_values)
#pragma message("implement I2C code here")
#endif
status = data_rx[0];
if(status & ABP2_STATUS_MASK)
if (status & ABP2_STATUS_MASK) {
return ABP2_ERRSENS;
}
raw_values->cntPress = 0x00FFFFFF & (data_rx[1] << 16 | data_rx[2] << 8 | data_rx[3]);
raw_values->cntTemp= 0x00FFFFFF & (data_rx[4] << 16 | data_rx[5] << 8 | data_rx[6]);
raw_values->cntTemp = 0x00FFFFFF & (data_rx[4] << 16 | data_rx[5] << 8 | data_rx[6]);

return res;
}

int abp2_measure_read(const abp2_t *dev, abp2_raw_t *raw_values)
{
uint8_t status = 0xFF; /* sensor status byte */
uint8_t status = 0xFF; /* sensor status byte */
int res = ABP2_OK;
int len = sizeof(data_tx_meas_read) / sizeof(data_tx_meas_read[0]);
uint8_t data_rx[ABP2_RX_BUF_LEN];
Expand All @@ -183,8 +189,9 @@ int abp2_measure_read(const abp2_t *dev, abp2_raw_t *raw_values)
#else
#pragma message("implement I2C code here")
#endif
if(res)
if (res) {
return ABP2_ERRBUS;
}
#if defined(MODULE_ABP2_SPI)
spi_acquire(dev->params->spi, dev->params->cs, SPI_MODE_0, dev->params->clk);
spi_transfer_bytes(dev->params->spi, dev->params->cs, false, data_tx_meas_read, data_rx, len);
Expand All @@ -193,17 +200,19 @@ int abp2_measure_read(const abp2_t *dev, abp2_raw_t *raw_values)
#pragma message("implement I2C code here")
#endif
status = data_rx[0];
if(status & ABP2_STATUS_MASK)
if (status & ABP2_STATUS_MASK) {
return ABP2_ERRSENS;
}
raw_values->cntPress = 0x00FFFFFF & (data_rx[1] << 16 | data_rx[2] << 8 | data_rx[3]);
raw_values->cntTemp= 0x00FFFFFF & (data_rx[4] << 16 | data_rx[5] << 8 | data_rx[6]);
raw_values->cntTemp = 0x00FFFFFF & (data_rx[4] << 16 | data_rx[5] << 8 | data_rx[6]);

return res;
}

float abp2_pressure(const abp2_t *dev, const abp2_raw_t *raw_values)
{
float press = 0.0f;

press = (raw_values->cntPress - cntPMin);
press /= (cntPMax - cntPMin);
press *= (dev->params->rangeMax - dev->params->rangeMin);
Expand Down
11 changes: 7 additions & 4 deletions drivers/abp2/abp2_saul.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
static int read_press(const void *dev, phydat_t *press)
{
abp2_raw_t rawData;
if(abp2_measure_read((abp2_t *)dev, &rawData)) {

if (abp2_measure_read((abp2_t *)dev, &rawData)) {
return -ECANCELED;
}
float p_mbar = abp2_pressure((abp2_t *)dev, &rawData); /* pressure in mbar */
press->val[0] = p_mbar * 1000.0f; /* convert to ubar */

press->val[0] = p_mbar * 1000.0f; /* convert to ubar */
press->scale = -6;
press->unit = UNIT_BAR;

Expand All @@ -41,11 +42,13 @@ static int read_press(const void *dev, phydat_t *press)
static int read_temp(const void *dev, phydat_t *temp)
{
abp2_raw_t rawData;

if (abp2_measure_read((abp2_t *)dev, &rawData)) {
return -ECANCELED;
}
float t_degc = abp2_temperature((abp2_t *)dev, &rawData); /* temperature in Celsius */
temp->val[0] = t_degc * 1000.0f; /* convert to mdegc */

temp->val[0] = t_degc * 1000.0f; /* convert to mdegc */
temp->scale = -3;
temp->unit = UNIT_TEMP_C;

Expand Down
34 changes: 17 additions & 17 deletions drivers/include/abp2.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@
* ## Usage
*
* See `RIOT/tests/drivers/abp2` for an example application using this driver.
*
*
* Add `USEMODULE += abp2` and `USEMODULE += abp2_spi` to the Makefile.
* When the I2C version is supported, the latter can be replaced by
* `USEMODULE += abp2_i2c`.
*
*
* When a measurement is started on the sensor, its internal ADC converts
* both pressure and temperature.
* These two values are retrieved in a single bus transfer.
*
*
* There are two ways to use the driver: either wait for the conversion to
* complete, or start a measurement while reading previous values.
* The first one is best suited for infrequent, non-periodic measurements,
* while the second one produces very compact code, but requires at least 5ms
* between two measurements.
* In any case, the raw values can be converted to
* physical quantities with #abp2_pressure() and #abp2_temperature().
*
*
* ### Poll sensor until the end of conversion
*
*
* -# Start a measurement with #abp2_measure().
* -# Poll the busy flag until it clears with #abp2_getstatus().
* -# Retrive the results of the conversion with #abp2_read_raw().
*
*
* ### Start measurement and read previous values
*
*
* A single call to #abp2_measure_read() will start a conversion of the ADC
* and retrieve the results of the \b previous measurement.
*
Expand Down Expand Up @@ -105,7 +105,7 @@ extern "C" {
* 2 | 1 : integrity test failed | Checksum error at power-up. |
* 1 | always 0 | |
* 0 | 1 : internal math saturation | |
*/
*/
#define ABP2_STATUS_BUSY 0x20 /**< Sensor is busy. */
#define ABP2_STATUS_MEMERR 0x04 /**< Memory integrity error. */
#define ABP2_STATUS_MATHSAT 0x01 /**< Math saturation error. */
Expand Down Expand Up @@ -178,7 +178,7 @@ extern const saul_driver_t abp2_saul_driver_temp; /**< SAUL driver to real tem
*
* @note The bus is expected to be initialized when calling this function.
* @note \a params sets the sensor range.
*
*
* @return 0 on success
* @return <0 on error
*/
Expand All @@ -202,11 +202,11 @@ int abp2_measure(const abp2_t *dev);

/**
* @brief Read the status byte of the sensor.
*
*
* @param[in] dev Sensor device descriptor.
*
*
* \sa \ref abp2_statusbits
*
*
* @return The status byte.
*/
uint8_t abp2_getstatus(const abp2_t *dev);
Expand Down Expand Up @@ -243,7 +243,7 @@ int abp2_read_raw(const abp2_t *dev, abp2_raw_t *raw_values);
*
* \pre A measurement must have been initiated by #abp2_measure()
* or a previous call to this function, in order to retrieve valid data.
*
*
* @note This function leads to concise user code. However, in order
* to get valid data, it must be called periodically, and at short intervals.
* If the application favors power savings i.e. low frequency measurements, then
Expand All @@ -256,20 +256,20 @@ int abp2_measure_read(const abp2_t *dev, abp2_raw_t *raw_values);

/**
* @brief Convert ADC counts to pressure.
*
*
* @param[in] dev Sensor device descriptor.
* @param[in] raw_values Raw values read from the sensor.
*
*
* @return The pressure in user units.
*/
float abp2_pressure(const abp2_t *dev, const abp2_raw_t *raw_values);

/**
* @brief Convert ADC counts to temperature .
*
*
* @param[in] dev Sensor device descriptor.
* @param[in] raw_values Raw values read from the sensor.
*
*
* @return The temperature in degrees Celsius.
*/
float abp2_temperature(const abp2_t *dev, const abp2_raw_t *raw_values);
Expand Down

0 comments on commit 1a59f06

Please sign in to comment.