summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoey Castillo <jose.castillo@gmail.com>2021-08-04 18:35:58 -0400
committerJoey Castillo <jose.castillo@gmail.com>2021-08-04 18:43:52 -0400
commit67e9d173ca0b504305a3fd3d8d1ad7b64e2d23dd (patch)
tree9e73b6af76f84b77d664d2cb303d09e89de9ac40
parent021a901bc19f55a26062439885f6f9ea8b875524 (diff)
downloadSensor-Watch-67e9d173ca0b504305a3fd3d8d1ad7b64e2d23dd.tar.gz
Sensor-Watch-67e9d173ca0b504305a3fd3d8d1ad7b64e2d23dd.tar.bz2
Sensor-Watch-67e9d173ca0b504305a3fd3d8d1ad7b64e2d23dd.zip
new project: sensor watch environment
-rw-r--r--Sensor Watch BME280 Project/app.c184
-rw-r--r--Sensor Watch BME280 Project/bme280.h85
-rwxr-xr-xSensor Watch BME280 Project/make/.gitignore1
-rwxr-xr-xSensor Watch BME280 Project/make/Makefile151
4 files changed, 421 insertions, 0 deletions
diff --git a/Sensor Watch BME280 Project/app.c b/Sensor Watch BME280 Project/app.c
new file mode 100644
index 00000000..f2605070
--- /dev/null
+++ b/Sensor Watch BME280 Project/app.c
@@ -0,0 +1,184 @@
+#include <stdio.h>
+#include <string.h>
+#include "watch.h"
+#include "app.h"
+#include "bme280.h"
+
+typedef enum ApplicationMode {
+ MODE_TEMPERATURE = 0,
+ MODE_HUMIDITY,
+ MODE_OFF,
+} ApplicationMode;
+
+typedef struct ApplicationState {
+ ApplicationMode mode;
+ bool light_on;
+ uint16_t dig_T1;
+ int16_t dig_T2;
+ int16_t dig_T3;
+ uint8_t dig_H1;
+ int16_t dig_H2;
+ uint8_t dig_H3;
+ int16_t dig_H4;
+ int16_t dig_H5;
+ int8_t dig_H6;
+} ApplicationState;
+
+ApplicationState application_state;
+
+
+void cb_mode_pressed();
+void cb_tick();
+
+float read_temperature(int32_t *p_t_fine);
+float read_humidity(int32_t t_fine);
+
+/**
+ * @brief Zeroes out the application state struct.
+ */
+void app_init() {
+ memset(&application_state, 0, sizeof(application_state));
+}
+
+/**
+ * @todo stash the BME280's calibration values in backup memory so we don't have to ask again
+ */
+void app_wake_from_deep_sleep() {
+}
+
+/**
+ * Enables the MODE button, the display and the sensor, and grabs calibration data.
+ */
+void app_setup() {
+ watch_enable_buttons();
+ watch_register_button_callback(BTN_MODE, cb_mode_pressed);
+
+ // pin A0 powers the sensor on this board.
+ watch_enable_digital_output(A0);
+ watch_set_pin_level(A0, true);
+ delay_ms(10);
+
+ watch_enable_i2c();
+
+ watch_i2c_write8(BME280_ADDRESS, BME280_REGISTER_SOFTRESET, BME280_SOFT_RESET_CODE);
+ delay_ms(10);
+ application_state.dig_T1 = watch_i2c_read16(BME280_ADDRESS, BME280_REGISTER_DIG_T1);
+ application_state.dig_T2 = (int16_t)watch_i2c_read16(BME280_ADDRESS, BME280_REGISTER_DIG_T2);
+ application_state.dig_T3 = (int16_t)watch_i2c_read16(BME280_ADDRESS, BME280_REGISTER_DIG_T3);
+ application_state.dig_H1 = watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_DIG_H1);
+ application_state.dig_H2 = (int16_t)watch_i2c_read16(BME280_ADDRESS, BME280_REGISTER_DIG_H2);
+ application_state.dig_H3 = watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_DIG_H3);
+ application_state.dig_H4 = ((int8_t)watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_DIG_H4) << 4) |
+ (watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_DIG_H4 + 1) & 0xF);
+ application_state.dig_H5 = ((int8_t)watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_DIG_H5 + 1) << 4) |
+ (watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_DIG_H5) >> 4);
+ application_state.dig_H6 = (int8_t)watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_DIG_H6);
+
+ watch_i2c_write8(BME280_ADDRESS, BME280_REGISTER_CONTROL_HUMID, BME280_CONTROL_HUMID_SAMPLING_X16);
+ watch_i2c_write8(BME280_ADDRESS, BME280_REGISTER_CONTROL, BME280_CONTROL_TEMPERATURE_SAMPLING_X16 |
+ BME280_CONTROL_PRESSURE_SAMPLING_NONE |
+ BME280_CONTROL_MODE_NORMAL);
+
+ watch_enable_display();
+
+ watch_register_tick_callback(cb_tick);
+}
+
+/**
+ * Nothing to do here.
+ */
+void app_prepare_for_sleep() {
+}
+
+/**
+ * @todo restore the BME280's calibration values from backup memory
+ */
+void app_wake_from_sleep() {
+}
+
+/**
+ * Displays the temperature and humidity on screen, or a string indicating no measurements are being taken.
+ */
+bool app_loop() {
+ int32_t t_fine;
+ float temperature;
+ float humidity;
+ char buf[11] = {0};
+
+ switch (application_state.mode) {
+ case MODE_TEMPERATURE:
+ temperature = read_temperature(NULL);
+ sprintf(buf, "TE %4.1f#C", temperature);
+ watch_display_string(buf, 0);
+ watch_clear_pixel(1, 16);
+ break;
+ case MODE_HUMIDITY:
+ temperature = read_temperature(&t_fine);
+ humidity = read_humidity(t_fine);
+ sprintf(buf, "HU rH %3d", (int)humidity);
+ watch_display_string(buf, 0);
+ watch_set_pixel(1, 16);
+ break;
+ case MODE_OFF:
+ watch_display_string(" Sleep ", 0);
+ watch_clear_pixel(1, 16);
+ }
+
+ return true;
+}
+
+/**
+ * Reads the temperature from the BME280
+ * @param p_t_fine - an optional pointer to an int32_t; if provided, the t_fine measurement
+ * (required for humidity calculation) will be returned by reference.
+ * Pass in NULL if you do not care about this value.
+ * @return a float indicating the temperature in degrees celsius.
+ */
+float read_temperature(int32_t *p_t_fine) {
+ // read24 reads the bytes into a uint32 which works for little-endian values (MSB is 0)
+ uint32_t raw_data = watch_i2c_read24(BME280_ADDRESS, BME280_REGISTER_TEMP_DATA) >> 8;
+ // alas the sensor's register layout is big-endian-ish, with a nibble of zeroes at the end of the LSB.
+ // this line shuffles everything back into place (swaps LSB and MSB and shifts the zeroes off the end)
+ int32_t adc_value = (((raw_data >> 16) | (raw_data & 0xFF00) | (raw_data << 16)) & 0xFFFFFF) >> 4;
+
+ // this bit is cribbed from Adafruit's BME280 driver. support their open source efforts by buying some stuff!
+ int32_t var1 = ((((adc_value >> 3) - ((int32_t)application_state.dig_T1 << 1))) * ((int32_t)application_state.dig_T2)) >> 11;
+ int32_t var2 = (((((adc_value >> 4) - ((int32_t)application_state.dig_T1)) * ((adc_value >> 4) - ((int32_t)application_state.dig_T1))) >> 12) * ((int32_t)application_state.dig_T3)) >> 14;
+ int32_t t_fine = var1 + var2;
+
+ // if we got a pointer to a t_fine, return it by reference (for humidity calculation).
+ if (p_t_fine != NULL) *p_t_fine = t_fine;
+
+ return ((t_fine * 5 + 128) >> 8) / 100.0;
+}
+
+/**
+ * Reads the humidity from the BME280
+ * @param t_fine - the t_fine measurement from a call to read_temperature
+ * @return a float indicating the temperature in degrees celsius.
+ */
+float read_humidity(int32_t t_fine) {
+ int32_t adc_value = watch_i2c_read16(BME280_ADDRESS, BME280_REGISTER_HUMID_DATA);
+ // the sensor returns this as big-endian, so swap the bytes.
+ adc_value = (adc_value >> 8) | (adc_value << 8);
+
+ // again, cribbed from Adafruit's BME280 driver. they sell a great breakout board for this sensor!
+ int32_t v_x1_u32r = v_x1_u32r = (t_fine - ((int32_t)76800));
+ v_x1_u32r = (((((adc_value << 14) - (((int32_t)application_state.dig_H4) << 20) - (((int32_t)application_state.dig_H5) * v_x1_u32r)) +
+ ((int32_t)16384)) >> 15) * (((((((v_x1_u32r * ((int32_t)application_state.dig_H6)) >> 10) * (((v_x1_u32r * ((int32_t)application_state.dig_H3)) >> 11) +
+ ((int32_t)32768))) >> 10) + ((int32_t)2097152)) * ((int32_t)application_state.dig_H2) + 8192) >> 14));
+ v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * ((int32_t)application_state.dig_H1)) >> 4));
+ v_x1_u32r = (v_x1_u32r < 0) ? 0 : v_x1_u32r;
+ v_x1_u32r = (v_x1_u32r > 419430400) ? 419430400 : v_x1_u32r;
+ float h = (v_x1_u32r >> 12);
+
+ return h / 1024.0;
+}
+
+void cb_mode_pressed() {
+ application_state.mode = (application_state.mode + 1) % 3;
+}
+
+void cb_tick() {
+}
+
diff --git a/Sensor Watch BME280 Project/bme280.h b/Sensor Watch BME280 Project/bme280.h
new file mode 100644
index 00000000..ba142582
--- /dev/null
+++ b/Sensor Watch BME280 Project/bme280.h
@@ -0,0 +1,85 @@
+#define BME280_ADDRESS (0x77)
+#define BME280_SOFT_RESET_CODE (0xB6)
+#define BME280_STATUS_UPDATING_MASK (1 << 3)
+
+typedef enum BME280Register {
+ BME280_REGISTER_DIG_T1 = 0x88,
+ BME280_REGISTER_DIG_T2 = 0x8A,
+ BME280_REGISTER_DIG_T3 = 0x8C,
+
+ BME280_REGISTER_DIG_P1 = 0x8E,
+ BME280_REGISTER_DIG_P2 = 0x90,
+ BME280_REGISTER_DIG_P3 = 0x92,
+ BME280_REGISTER_DIG_P4 = 0x94,
+ BME280_REGISTER_DIG_P5 = 0x96,
+ BME280_REGISTER_DIG_P6 = 0x98,
+ BME280_REGISTER_DIG_P7 = 0x9A,
+ BME280_REGISTER_DIG_P8 = 0x9C,
+ BME280_REGISTER_DIG_P9 = 0x9E,
+
+ BME280_REGISTER_DIG_H1 = 0xA1,
+ BME280_REGISTER_DIG_H2 = 0xE1,
+ BME280_REGISTER_DIG_H3 = 0xE3,
+ BME280_REGISTER_DIG_H4 = 0xE4,
+ BME280_REGISTER_DIG_H5 = 0xE5,
+ BME280_REGISTER_DIG_H6 = 0xE7,
+
+ BME280_REGISTER_CHIPID = 0xD0,
+ BME280_REGISTER_VERSION = 0xD1,
+ BME280_REGISTER_SOFTRESET = 0xE0,
+
+ BME280_REGISTER_CAL26 = 0xE1,
+
+ BME280_REGISTER_CONTROL_HUMID = 0xF2,
+ BME280_REGISTER_STATUS = 0XF3,
+ BME280_REGISTER_CONTROL = 0xF4,
+ BME280_REGISTER_CONFIG = 0xF5,
+ BME280_REGISTER_PRESSURE_DATA = 0xF7,
+ BME280_REGISTER_TEMP_DATA = 0xFA,
+ BME280_REGISTER_HUMID_DATA = 0xFD
+} BME280Register;
+
+typedef enum BME280Control {
+ BME280_CONTROL_MODE_SLEEP = 0b00,
+ BME280_CONTROL_MODE_FORCED = 0b01,
+ BME280_CONTROL_MODE_NORMAL = 0b11,
+ BME280_CONTROL_PRESSURE_SAMPLING_NONE = 0b000 << 2,
+ BME280_CONTROL_PRESSURE_SAMPLING_X1 = 0b001 << 2,
+ BME280_CONTROL_PRESSURE_SAMPLING_X2 = 0b010 << 2,
+ BME280_CONTROL_PRESSURE_SAMPLING_X4 = 0b011 << 2,
+ BME280_CONTROL_PRESSURE_SAMPLING_X8 = 0b100 << 2,
+ BME280_CONTROL_PRESSURE_SAMPLING_X16 = 0b101 << 2,
+ BME280_CONTROL_TEMPERATURE_SAMPLING_NONE = 0b000 << 5,
+ BME280_CONTROL_TEMPERATURE_SAMPLING_X1 = 0b001 << 5,
+ BME280_CONTROL_TEMPERATURE_SAMPLING_X2 = 0b010 << 5,
+ BME280_CONTROL_TEMPERATURE_SAMPLING_X4 = 0b011 << 5,
+ BME280_CONTROL_TEMPERATURE_SAMPLING_X8 = 0b100 << 5,
+ BME280_CONTROL_TEMPERATURE_SAMPLING_X16 = 0b101 << 5
+} BME280Control;
+
+typedef enum BME280ControlHumidity {
+ BME280_CONTROL_HUMID_SAMPLING_NONE = 0b000,
+ BME280_CONTROL_HUMID_SAMPLING_X1 = 0b001,
+ BME280_CONTROL_HUMID_SAMPLING_X2 = 0b010,
+ BME280_CONTROL_HUMID_SAMPLING_X4 = 0b011,
+ BME280_CONTROL_HUMID_SAMPLING_X8 = 0b100,
+ BME280_CONTROL_HUMID_SAMPLING_X16 = 0b101
+} BME280ControlHumidity;
+
+typedef enum BME280Filter {
+ BME280_CONFIG_FILTER_OFF = 0b000 << 2,
+ BME280_CONFIG_FILTER_X2 = 0b001 << 2,
+ BME280_CONFIG_FILTER_X4 = 0b010 << 2,
+ BME280_CONFIG_FILTER_X8 = 0b011 << 2,
+ BME280_CONFIG_FILTER_X16 = 0b10 << 2,
+ BME280_CONFIG_STANDBY_MS_0_5 = 0b000 << 5,
+ BME280_CONFIG_STANDBY_MS_10 = 0b110 << 5,
+ BME280_CONFIG_STANDBY_MS_20 = 0b111 << 5,
+ BME280_CONFIG_STANDBY_MS_62_5 = 0b001 << 5,
+ BME280_CONFIG_STANDBY_MS_125 = 0b010 << 5,
+ BME280_CONFIG_STANDBY_MS_250 = 0b011 << 5,
+ BME280_CONFIG_STANDBY_MS_500 = 0b100 << 5,
+ BME280_CONFIG_STANDBY_MS_1000 = 0b101 << 5
+} BME280Filter;
+
+inline uint16_t make_le_16(uint16_t val) { return (val >> 8) | (val << 8); }
diff --git a/Sensor Watch BME280 Project/make/.gitignore b/Sensor Watch BME280 Project/make/.gitignore
new file mode 100755
index 00000000..3722ac63
--- /dev/null
+++ b/Sensor Watch BME280 Project/make/.gitignore
@@ -0,0 +1 @@
+build/
diff --git a/Sensor Watch BME280 Project/make/Makefile b/Sensor Watch BME280 Project/make/Makefile
new file mode 100755
index 00000000..9b8d25c0
--- /dev/null
+++ b/Sensor Watch BME280 Project/make/Makefile
@@ -0,0 +1,151 @@
+##############################################################################
+BUILD = build
+BIN = watch
+
+##############################################################################
+.PHONY: all directory clean size
+
+CC = arm-none-eabi-gcc
+OBJCOPY = arm-none-eabi-objcopy
+SIZE = arm-none-eabi-size
+UF2 = python ../../utils/uf2conv.py
+
+ifeq ($(OS), Windows_NT)
+ MKDIR = gmkdir
+else
+ MKDIR = mkdir
+endif
+
+CFLAGS += -W -Wall --std=gnu99 -Os
+CFLAGS += -fno-diagnostics-show-caret
+CFLAGS += -fdata-sections -ffunction-sections
+CFLAGS += -funsigned-char -funsigned-bitfields
+CFLAGS += -mcpu=cortex-m0plus -mthumb
+CFLAGS += -MD -MP -MT $(BUILD)/$(*F).o -MF $(BUILD)/$(@F).d
+
+LDFLAGS += -mcpu=cortex-m0plus -mthumb
+LDFLAGS += -Wl,--gc-sections
+LDFLAGS += -Wl,--script=../../watch-library/linker/saml22j18.ld
+
+# If you add any additional directories with headers, add them to this list, e.g.
+# ../drivers/
+INCLUDES += \
+ -I../ \
+ -I../../watch-library/include \
+ -I../../watch-library/hal/ \
+ -I../../watch-library/hal/documentation/ \
+ -I../../watch-library/hal/include/ \
+ -I../../watch-library/hal/src/ \
+ -I../../watch-library/hal/utils/ \
+ -I../../watch-library/hal/utils/include/ \
+ -I../../watch-library/hal/utils/src/ \
+ -I../../watch-library/hpl/ \
+ -I../../watch-library/hpl/adc/ \
+ -I../../watch-library/hpl/core/ \
+ -I../../watch-library/hpl/dmac/ \
+ -I../../watch-library/hpl/eic/ \
+ -I../../watch-library/hpl/gclk/ \
+ -I../../watch-library/hpl/mclk/ \
+ -I../../watch-library/hpl/osc32kctrl/ \
+ -I../../watch-library/hpl/oscctrl/ \
+ -I../../watch-library/hpl/pm/ \
+ -I../../watch-library/hpl/port/ \
+ -I../../watch-library/hpl/rtc/ \
+ -I../../watch-library/hpl/sercom/ \
+ -I../../watch-library/hpl/slcd/ \
+ -I../../watch-library/hpl/systick/ \
+ -I../../watch-library/hpl/tcc/ \
+ -I../../watch-library/hpl/tc/ \
+ -I../../watch-library/hri/ \
+ -I../../watch-library/config/ \
+ -I../../watch-library/hw/ \
+ -I../../watch-library/watch/ \
+ -I../../watch-library
+
+# If you add any additional C files to your project, add them each to this list, e.g.
+# ../drivers/st25dv.c
+SRCS += \
+ ../app.c \
+ ../../watch-library/main.c \
+ ../../watch-library/startup_saml22.c \
+ ../../watch-library/hw/driver_init.c \
+ ../../watch-library/watch/watch.c \
+ ../../watch-library/hal/src/hal_adc_sync.c \
+ ../../watch-library/hal/src/hal_atomic.c \
+ ../../watch-library/hal/src/hal_calendar.c \
+ ../../watch-library/hal/src/hal_delay.c \
+ ../../watch-library/hal/src/hal_ext_irq.c \
+ ../../watch-library/hal/src/hal_gpio.c \
+ ../../watch-library/hal/src/hal_i2c_m_sync.c \
+ ../../watch-library/hal/src/hal_init.c \
+ ../../watch-library/hal/src/hal_io.c \
+ ../../watch-library/hal/src/hal_pwm.c \
+ ../../watch-library/hal/src/hal_slcd_sync.c \
+ ../../watch-library/hal/src/hal_sleep.c \
+ ../../watch-library/hal/utils/src/utils_assert.c \
+ ../../watch-library/hal/utils/src/utils_event.c \
+ ../../watch-library/hal/utils/src/utils_list.c \
+ ../../watch-library/hal/utils/src/utils_syscalls.c \
+ ../../watch-library/hpl/adc/hpl_adc.c \
+ ../../watch-library/hpl/core/hpl_core_m0plus_base.c \
+ ../../watch-library/hpl/core/hpl_init.c \
+ ../../watch-library/hpl/dmac/hpl_dmac.c \
+ ../../watch-library/hpl/eic/hpl_eic.c \
+ ../../watch-library/hpl/gclk/hpl_gclk.c \
+ ../../watch-library/hpl/mclk/hpl_mclk.c \
+ ../../watch-library/hpl/osc32kctrl/hpl_osc32kctrl.c \
+ ../../watch-library/hpl/oscctrl/hpl_oscctrl.c \
+ ../../watch-library/hpl/pm/hpl_pm.c \
+ ../../watch-library/hpl/rtc/hpl_rtc.c \
+ ../../watch-library/hpl/sercom/hpl_sercom.c \
+ ../../watch-library/hpl/slcd/hpl_slcd.c \
+ ../../watch-library/hpl/systick/hpl_systick.c \
+ ../../watch-library/hpl/tcc/hpl_tcc.c \
+ ../../watch-library/hpl/tc/hpl_tc.c
+
+DEFINES += \
+ -D__SAML22J18A__ \
+ -DDONT_USE_CMSIS_INIT
+
+CFLAGS += $(INCLUDES) $(DEFINES)
+
+OBJS = $(addprefix $(BUILD)/, $(notdir %/$(subst .c,.o, $(SRCS))))
+
+all: directory $(BUILD)/$(BIN).elf $(BUILD)/$(BIN).hex $(BUILD)/$(BIN).bin $(BUILD)/$(BIN).uf2 size
+
+$(BUILD)/$(BIN).elf: $(OBJS)
+ @echo LD $@
+ @$(CC) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
+
+$(BUILD)/$(BIN).hex: $(BUILD)/$(BIN).elf
+ @echo OBJCOPY $@
+ @$(OBJCOPY) -O ihex $^ $@
+
+$(BUILD)/$(BIN).bin: $(BUILD)/$(BIN).elf
+ @echo OBJCOPY $@
+ @$(OBJCOPY) -O binary $^ $@
+
+$(BUILD)/$(BIN).uf2: $(BUILD)/$(BIN).bin
+ @echo UF2CONV $@
+ @$(UF2) $^ -co $@
+
+install:
+ @$(UF2) -D $(BUILD)/$(BIN).uf2
+
+%.o:
+ @echo CC $@
+ @$(CC) $(CFLAGS) $(filter %/$(subst .o,.c,$(notdir $@)), $(SRCS)) -c -o $@
+
+directory:
+ @$(MKDIR) -p $(BUILD)
+
+size: $(BUILD)/$(BIN).elf
+ @echo size:
+ @$(SIZE) -t $^
+
+clean:
+ @echo clean
+ @-rm -rf $(BUILD)
+
+-include $(wildcard $(BUILD)/*.d)
+