diff --git a/examples/bh1750/Makefile b/examples/bh1750/Makefile new file mode 100644 index 0000000..5abb97a --- /dev/null +++ b/examples/bh1750/Makefile @@ -0,0 +1,3 @@ +PROGRAM=bh1750_example +EXTRA_COMPONENTS = extras/i2c extras/bh1750 +include ../../common.mk diff --git a/examples/bh1750/bh1750_example.c b/examples/bh1750/bh1750_example.c new file mode 100644 index 0000000..fad67bd --- /dev/null +++ b/examples/bh1750/bh1750_example.c @@ -0,0 +1,41 @@ +#include + +#include "espressif/esp_common.h" +#include "esp/uart.h" + +#include "FreeRTOS.h" +#include "task.h" + +#include "i2c/i2c.h" +#include "bh1750/bh1750.h" + +#define SCL_PIN 5 +#define SDA_PIN 4 + +static void measure(void *pvParameters) +{ + bh1750_configure(BH1750_ADDR_LO, + BH1750_CONTINUOUS_MODE | BH1750_HIGH_RES_MODE); + + while (1) { + while(1) { + vTaskDelay(200 / portTICK_PERIOD_MS); + + printf("Lux: %d\n", bh1750_read(BH1750_ADDR_LO)); + } + } +} + +void user_init(void) +{ + uart_set_baud(0, 115200); + + // Just some information + printf("\n"); + printf("SDK version : %s\n", sdk_system_get_sdk_version()); + printf("GIT version : %s\n", GITSHORTREV); + + i2c_init(SCL_PIN, SDA_PIN); + + xTaskCreate(measure, "measure_task", 256, NULL, 2, NULL); +} diff --git a/extras/bh1750/bh1750.c b/extras/bh1750/bh1750.c new file mode 100644 index 0000000..68e623a --- /dev/null +++ b/extras/bh1750/bh1750.c @@ -0,0 +1,28 @@ +/* + * Driver for BH1750 light sensor + * + * Part of esp-open-rtos + * Copyright (C) 2017 Andrej Krutak + * BSD Licensed as described in the file LICENSE + */ +#include "bh1750.h" +#include +#include + +void bh1750_configure(uint8_t addr, uint8_t mode) +{ + i2c_slave_write(addr, NULL, &mode, 1); +} + +uint16_t bh1750_read(uint8_t addr) +{ + uint8_t buf[2]; + uint16_t level; + + i2c_slave_read(addr, NULL, buf, 2); + + level = buf[0] << 8 | buf[1]; + level = (level * 10) / 12; // convert to LUX + + return level; +} diff --git a/extras/bh1750/bh1750.h b/extras/bh1750/bh1750.h new file mode 100644 index 0000000..b280c45 --- /dev/null +++ b/extras/bh1750/bh1750.h @@ -0,0 +1,82 @@ +/* + * Driver for BH1750 light sensor + * + * Part of esp-open-rtos + * Copyright (C) 2017 Andrej Krutak + * BSD Licensed as described in the file LICENSE + * + * ROHM Semiconductor bh1750fvi-e.pdf + */ +#ifndef EXTRAS_BH1750_H_ +#define EXTRAS_BH1750_H_ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Possible chip addresses */ +#define BH1750_ADDR_LO 0x23 // ADDR pin floating/low +#define BH1750_ADDR_HI 0x5c + + +/* Configuration options */ + +// No active state +#define BH1750_POWER_DOWN 0x00 + +// Wating for measurement command +#define BH1750_POWER_ON 0x01 + +// Reset data register value - not accepted in POWER_DOWN mode +#define BH1750_RESET 0x07 + + +/* Measurement modes */ + +#define BH1750_CONTINUOUS_MODE 0x10 +#define BH1750_ONE_TIME_MODE 0x20 + +// Start measurement at 1 lx resolution (measurement time typically 120ms) +#define BH1750_HIGH_RES_MODE 0x00 +// Start measurement at 0.5 lx resolution (measurement time typically 120ms) +#define BH1750_HIGH_RES_MODE2 0x01 +// Start measurement at 4 lx resolution (measurement time typically 16ms) +#define BH1750_LOW_RES_MODE 0x03 + +/* Adjust measurement time to account for optical window size (see datasheet). + * Procedure from datasheet suggests order Hi, Low and finally measurement mode + */ +#define BH1750_MEASURE_TIME_HI(mt) (0x40 | (((mt) >> 5) & 0x7)) +#define BH1750_MEASURE_TIME_LO(mt) (0x60 | ((mt) & 0x1f)) +#define BH1750_DEFAULT_MEASURE_TIME 0x45 + + +/** + * Configure the device. + * @param addr Device address + * @param mode Combination of BH1750_* flags + * + * May be called multiple times e.g. to configure the measurement time and + * the readout mode afterwards - or if one time mode is used consecutively. + * + * Example: BH1750_ADDR_LO, BH1750_CONTINUOUS_MODE | BH1750_HIGH_RES_MODE + */ +void bh1750_configure(uint8_t addr, uint8_t mode); + +/** + * Read LUX value from the device. + * + * @param addr Device address + * @returns read value in lux units + */ +uint16_t bh1750_read(uint8_t addr); + +#ifdef __cplusplus +} +#endif + +#endif /* EXTRAS_BH1750_H_ */ diff --git a/extras/bh1750/component.mk b/extras/bh1750/component.mk new file mode 100644 index 0000000..0a57b31 --- /dev/null +++ b/extras/bh1750/component.mk @@ -0,0 +1,9 @@ +# Component makefile for extras/bh1750 + +# expected anyone using RTC driver includes it as 'bh1750/bh1750.h' +INC_DIRS += $(bh1750_ROOT).. + +# args for passing into compile rule generation +bh1750_SRC_DIR = $(bh1750_ROOT) + +$(eval $(call component_compile_rules,bh1750)) diff --git a/extras/pwm/pwm.c b/extras/pwm/pwm.c index 338dff0..792d899 100644 --- a/extras/pwm/pwm.c +++ b/extras/pwm/pwm.c @@ -29,7 +29,7 @@ typedef struct pwmInfoDefinition uint8_t running; uint16_t freq; - uint16_t dutyCicle; + uint16_t dutyCycle; /* private */ uint32_t _maxLoad; @@ -66,7 +66,7 @@ static void frc1_interrupt_handler(void) pwmInfo._step = step; } -void pwm_init(uint8_t npins, uint8_t* pins) +void pwm_init(uint8_t npins, const uint8_t* pins) { /* Assert number of pins is correct */ if (npins > MAX_PWM_PINS) @@ -127,10 +127,10 @@ void pwm_set_duty(uint16_t duty) { bool output; - pwmInfo.dutyCicle = duty; + pwmInfo.dutyCycle = duty; if (duty > 0 && duty < UINT16_MAX) { pwm_restart(); - return; + return; } // 0% and 100% duty cycle are special cases: constant output. @@ -139,7 +139,7 @@ void pwm_set_duty(uint16_t duty) output = (duty == UINT16_MAX); for (uint8_t i = 0; i < pwmInfo.usedPins; ++i) { - gpio_write(pwmInfo.pins[i].pin, output); + gpio_write(pwmInfo.pins[i].pin, output); } } @@ -154,7 +154,7 @@ void pwm_restart() void pwm_start() { - pwmInfo._onLoad = pwmInfo.dutyCicle * pwmInfo._maxLoad / UINT16_MAX; + pwmInfo._onLoad = pwmInfo.dutyCycle * pwmInfo._maxLoad / UINT16_MAX; pwmInfo._offLoad = pwmInfo._maxLoad - pwmInfo._onLoad; pwmInfo._step = PERIOD_ON; diff --git a/extras/pwm/pwm.h b/extras/pwm/pwm.h index 55b8761..e2e7d75 100644 --- a/extras/pwm/pwm.h +++ b/extras/pwm/pwm.h @@ -16,7 +16,7 @@ extern "C" { #endif -void pwm_init(uint8_t npins, uint8_t* pins); +void pwm_init(uint8_t npins, const uint8_t* pins); void pwm_set_freq(uint16_t freq); void pwm_set_duty(uint16_t duty);