Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RFM = 69
LORA = 0

MAIN = avr64ea.c
SRC = rfm.c spi.c usart.c
SRC = rfm.c spi.c usart.c bme68x/bme68x.c bme688.c

CC = avr-gcc
OBJCOPY = avr-objcopy
Expand All @@ -38,11 +38,13 @@ CFLAGS += -std=gnu99
TARGET = $(strip $(basename $(MAIN)))
SRC += $(TARGET).c
SRC += ../librfm$(RFM)/librfm$(RFM).a
SRC += ../bsec/AVR8_XMEGA/libalgobsec.a

OBJ = $(SRC:.c=.o)
OBJ = $(SRC:.S=.o)

$(TARGET).elf: spi.h usart.h utils.h Makefile
$(TARGET).elf: spi.h usart.h utils.h bme68x/bme68x.h bme68x/bme68x_defs.h \
bme688.h Makefile

all: $(TARGET).hex

Expand Down
13 changes: 12 additions & 1 deletion avr64ea.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#if RFM == 95
#include "librfm95/librfm95.h"
#endif
#include "bme688.h"

/* Timebase used for timing internal delays */
#define TIMEBASE_VALUE ((uint8_t) ceil(F_CPU * 0.000001))
Expand Down Expand Up @@ -91,7 +92,7 @@ ISR(ADC0_RESRDY_vect) {
EMPTY_INTERRUPT(ADC0_RESRDY_vect);

/**
* Radio module DIO and DIO4 (FSK)/DIO1 (LoRa) interrupt.
* Radio module DIO0 and DIO4 (FSK)/DIO1 (LoRa) interrupt.
*/
ISR(PORTD_PORT_vect) {
if (PORTD_INTFLAGS & PORT_INT_2_bm) {
Expand Down Expand Up @@ -134,6 +135,10 @@ static void initPins(void) {
// PD1 is radio module CS pin (output pin + pullup)
PORTD_DIRSET = (1 << PD1);
PORTD_PIN1CTRL |= PORT_PULLUPEN_bm;

// PD2 is BME68x CS pin (output pin + pullup)
PORTD_DIRSET = (1 << PD2);
PORTD_PIN2CTRL |= PORT_PULLUPEN_bm;
}

/* Sets CPU and peripherals clock speed */
Expand Down Expand Up @@ -312,6 +317,12 @@ int main(void) {
printString("Radio init failed!\r\n");
}

int8_t bme688 = initBME68x();
if (bme688 < 0 && USART) {
printString("BME688 init failed!\r\n");
printInt(bme688);
}

// enable global interrupts
sei();

Expand Down
144 changes: 144 additions & 0 deletions bme688.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* File: bme688.c
* Author: [email protected]
*
* Thanks to https://github.com/boschsensortec/BME68x_SensorAPI/tree/master/examples
*
* Created on 4. Oktober 2025, 20:31
*/

#include "bme688.h"

/**
* Writes given register - data pairs to respective register(s).
*
* @param reg register of first register - data pair
* @param data array with data of first register - data pair as first element,
* with remaining register - data pairs following
* @param len (number of register - data pairs) - 1
* @param intfPtr
* @return success
*/
static BME68X_INTF_RET_TYPE bme68xWrite(uint8_t reg,
const uint8_t *data,
uint32_t len,
void *intfPtr) {
PORTD_OUTCLR = (1 << PD4);
transmit(reg);
for (uint32_t i = 0; i < len; i++) {
transmit(data[i]);
}
PORTD_OUTSET = (1 << PD4);

// TODO
return 0;
}

/**
* Reads from register(s) into given data array starting with
* given register auto-incrementing.
*
* @param reg start register
* @param data array with data to be read from consecutive registers
* @param len number of registers to read from
* @param intfPtr
* @return success
*/
static BME68X_INTF_RET_TYPE bme68xRead(uint8_t reg,
uint8_t *data,
uint32_t len,
void *intfPtr) {
PORTD_OUTCLR = (1 << PD4);
transmit(reg);
for (uint32_t i = 0; i < len; i++) {
data[i] = transmit(0x00);
}
PORTD_OUTSET = (1 << PD4);

// TODO
return 0;
}

/**
* Delay function for BME68x - requires __DELAY_BACKWARD_COMPATIBLE__
* so delay does not have to be a compile time constant.
*
* @param period
* @param intfPtr
*/
static void bme68xDelayUs(uint32_t period, void *intfPtr) {
_delay_us(period);
}

int8_t initBME68x() {
struct bme68x_dev dev;
struct bme68x_conf conf;
struct bme68x_heatr_conf heater_conf;
struct bme68x_data data;
int8_t result;

uint8_t pin = PD4;

dev.intf = BME68X_SPI_INTF;
dev.write = bme68xWrite;
dev.read = bme68xRead;
dev.delay_us = bme68xDelayUs;
dev.intf_ptr = &pin; // TODO not used
dev.amb_temp = 20; // could use temp measured with thermistor ;-)

result = bme68x_init(&dev);
// TODO properly check result
if (result < 0) {
return result;
}

conf.filter = BME68X_FILTER_OFF;
conf.odr = BME68X_ODR_NONE;
conf.os_hum = BME68X_OS_8X;
conf.os_pres = BME68X_OS_8X;
conf.os_temp = BME68X_OS_8X;
result = bme68x_set_conf(&conf, &dev);
// TODO properly check result
if (result < 0) {
return result;
}

heater_conf.enable = BME68X_ENABLE;
heater_conf.heatr_temp = 300;
heater_conf.heatr_dur = 100;
result = bme68x_set_heatr_conf(BME68X_FORCED_MODE, &heater_conf, &dev);
// TODO properly check result
if (result < 0) {
return result;
}

result = bme68x_set_op_mode(BME68X_FORCED_MODE, &dev);
// TODO properly check result
if (result < 0) {
return result;
}

uint32_t meas_dur = bme68x_get_meas_dur(BME68X_FORCED_MODE, &conf, &dev) +
(heater_conf.heatr_dur * 1000);
dev.delay_us(meas_dur, dev.intf_ptr);

uint8_t n_data;
result = bme68x_get_data(BME68X_FORCED_MODE, &data, &n_data, &dev);
// TODO properly check result
if (result < 0) {
return result;
}

div_t tdiv = div(data.temperature, 100);

char buf[70];
snprintf(buf, sizeof (buf), "%c%d.%d°, %ld Pa, %ld %%, %ld Ohm, %d\r\n",
data.temperature < 0 ? '-' : ' ', abs(tdiv.quot), abs(tdiv.rem),
(uint32_t)data.pressure,
(uint32_t)(data.humidity / 1000),
(uint32_t)data.gas_resistance,
data.status);
printString(buf);

return true;
}
32 changes: 32 additions & 0 deletions bme688.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* File: bme688.h
* Author: [email protected]
*
* Created on 4. Oktober 2025, 20:31
*/

#ifndef BME688_H
#define BME688_H

/* Allow passing a variable to _delay_* functions */
#define __DELAY_BACKWARD_COMPATIBLE__

#include <stdlib.h>
#include <util/delay.h>

#include "pins.h"
#include "spi.h"
#include "usart.h"
#include "bme68x/bme68x.h"

/**
* Initializes the BME68x sensor.
*
* @return error code
*/
int8_t initBME68x(void);

#undef __DELAY_BACKWARD_COMPATIBLE__

#endif /* BME688_H */

Loading