diff --git a/examples/pcf8591/Makefile b/examples/pcf8591/Makefile new file mode 100644 index 0000000..f9d5768 --- /dev/null +++ b/examples/pcf8591/Makefile @@ -0,0 +1,3 @@ +PROGRAM = pcf8591 +EXTRA_COMPONENTS = extras/i2c extras/pcf8591 +include ../../common.mk diff --git a/examples/pcf8591/main.c b/examples/pcf8591/main.c new file mode 100644 index 0000000..7547c75 --- /dev/null +++ b/examples/pcf8591/main.c @@ -0,0 +1,36 @@ +#include + +#include "espressif/esp_common.h" +#include "esp/uart.h" + +#include "FreeRTOS.h" +#include "task.h" + +#include "i2c/i2c.h" +#include "pcf8591/pcf8591.h" + +#define SCL_PIN 5 +#define SDA_PIN 4 + +static void measure(void *pvParameters) +{ + while (1) + { + vTaskDelay(1000 / portTICK_PERIOD_MS); + printf("Value: %d\n", pcf8591_read(PCF8591_DEFAULT_ADDRESS, 0x03)); + } +} + +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/pcf8591/component.mk b/extras/pcf8591/component.mk new file mode 100644 index 0000000..5e09035 --- /dev/null +++ b/extras/pcf8591/component.mk @@ -0,0 +1,6 @@ +# Component makefile for extras/pcf8591 + +INC_DIRS += $(pcf8591_ROOT).. +pcf8591_SRC_DIR = $(pcf8591_ROOT) + +$(eval $(call component_compile_rules,pcf8591)) diff --git a/extras/pcf8591/pcf8591.c b/extras/pcf8591/pcf8591.c new file mode 100644 index 0000000..42b9df4 --- /dev/null +++ b/extras/pcf8591/pcf8591.c @@ -0,0 +1,22 @@ +#include +#include + +#include +#include "pcf8591.h" + +/** + * CAUTION: PLEASE SET I2C_FREQUENCY_400K IS 'false' IN 'i2c.h' FILE + */ + +#define PCF8591_CTRL_REG_READ 0x03 + +uint8_t +pcf8591_read(uint8_t addr, uint8_t analog_pin) +{ + uint8_t res = 0; + uint8_t control_reg = PCF8591_CTRL_REG_READ & analog_pin; + + i2c_slave_read(addr, &control_reg, &res, 1); + + return res; +} diff --git a/extras/pcf8591/pcf8591.h b/extras/pcf8591/pcf8591.h new file mode 100644 index 0000000..3deac57 --- /dev/null +++ b/extras/pcf8591/pcf8591.h @@ -0,0 +1,42 @@ +/** + * Driver for 8-bit analog-to-digital conversion and + * an 8-bit digital-to-analog conversion PCF8591 + * + * Part of esp-open-rtos + * Copyright (C) 2017 Pham Ngoc Thanh + * BSD Licensed as described in the file LICENSE + */ +#ifndef _EXTRAS_PCF8591_H_ +#define _EXTRAS_PCF8591_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/** + * CAUTION: PLEASE SET I2C_FREQUENCY_400K IS 'false' IN 'i2c.h' FILE + */ + +#define PCF8591_DEFAULT_ADDRESS 0x48 + +void pcf8591_init(void); + +/** + * Read input value of an analog pin. + * @param[in] addr Pointer to device + * @param[in] analog_pin pin number: + * 0 - AIN0 + * 1 - AIN1 + * 2 - AIN2 + * 3 - AIN3 + * @return analog value + */ +uint8_t pcf8591_read(uint8_t addr, uint8_t analog_pin); + + +#ifdef __cplusplus +} +#endif + +#endif /* _EXTRAS_PCF8591_H_ */