From 6f7940a443325e78e37c96e14a14645696f306d5 Mon Sep 17 00:00:00 2001 From: Christian Loch Date: Mon, 10 Feb 2020 19:14:18 +0100 Subject: [PATCH] Imported u8g2 library and added example to main Removed old display library code --- components/u8g2 | 1 + main/CMakeLists.txt | 2 +- main/main.c | 459 ++++++++++-------------------------------- main/u8g2_esp32_hal.c | 166 +++++++++++++++ main/u8g2_esp32_hal.h | 39 ++++ 5 files changed, 309 insertions(+), 358 deletions(-) create mode 160000 components/u8g2 create mode 100644 main/u8g2_esp32_hal.c create mode 100644 main/u8g2_esp32_hal.h diff --git a/components/u8g2 b/components/u8g2 new file mode 160000 index 0000000..12ed2e4 --- /dev/null +++ b/components/u8g2 @@ -0,0 +1 @@ +Subproject commit 12ed2e48354566216bfeee77449fc7ea41e5df2c diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index e6c20ac..f7fb560 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -2,7 +2,7 @@ set(COMPONENT_REQUIRES ) set(COMPONENT_PRIV_REQUIRES ) -set(COMPONENT_SRCS "main.c") +set(COMPONENT_SRCS "main.c" "u8g2_esp32_hal.c") set(COMPONENT_ADD_INCLUDEDIRS "") register_component() diff --git a/main/main.c b/main/main.c index 3799913..5674926 100644 --- a/main/main.c +++ b/main/main.c @@ -1,374 +1,119 @@ -#include "freertos/FreeRTOS.h" -#include "esp_wifi.h" -#include "esp_system.h" -#include "esp_event.h" -#include "esp_event_loop.h" -#include "nvs_flash.h" -#include "driver/gpio.h" +//#include "freertos/FreeRTOS.h" +//#include "esp_wifi.h" +//#include "esp_system.h" +//#include "esp_event.h" +//#include "esp_event_loop.h" +//#include "nvs_flash.h" +//#include "driver/gpio.h" #include "bme280.h" +//#include "ssd1366.h" #include "driver/i2c.h" +#include +#include "u8g2_esp32_hal.h" +#include -#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ -#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ -#define WRITE_BIT I2C_MASTER_WRITE /*!< I2C master write */ -#define READ_BIT I2C_MASTER_READ /*!< I2C master read */ -#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/ -#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */ -#define ACK_VAL 0x0 /*!< I2C ack value */ -#define NACK_VAL 0x1 /*!< I2C nack value */ - -static struct { - struct arg_int *port; - struct arg_int *freq; - struct arg_int *sda; - struct arg_int *scl; - struct arg_end *end; -} i2cconfig_args; - -static struct { - struct arg_int *chip_address; - struct arg_int *register_address; - struct arg_int *data_length; - struct arg_end *end; -} i2cget_args; - -static struct { - struct arg_int *chip_address; - struct arg_int *register_address; - struct arg_int *data; - struct arg_end *end; -} i2cset_args; - -static int do_i2cget_cmd(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **)&i2cget_args); - if (nerrors != 0) { - arg_print_errors(stderr, i2cget_args.end, argv[0]); - return 0; - } - - /* Check chip address: "-c" option */ - int chip_addr = i2cget_args.chip_address->ival[0]; - /* Check register address: "-r" option */ - int data_addr = -1; - if (i2cget_args.register_address->count) { - data_addr = i2cget_args.register_address->ival[0]; - } - /* Check data length: "-l" option */ - int len = 1; - if (i2cget_args.data_length->count) { - len = i2cget_args.data_length->ival[0]; - } - uint8_t *data = malloc(len); - - i2c_master_driver_initialize(); - i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - if (data_addr != -1) { - i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN); - i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN); - i2c_master_start(cmd); - } - i2c_master_write_byte(cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN); - if (len > 1) { - i2c_master_read(cmd, data, len - 1, ACK_VAL); - } - i2c_master_read_byte(cmd, data + len - 1, NACK_VAL); - i2c_master_stop(cmd); - esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS); - i2c_cmd_link_delete(cmd); - if (ret == ESP_OK) { - for (int i = 0; i < len; i++) { - printf("0x%02x ", data[i]); - if ((i + 1) % 16 == 0) { - printf("\r\n"); - } - } - if (len % 16) { - printf("\r\n"); - } - } else if (ret == ESP_ERR_TIMEOUT) { - ESP_LOGW(TAG, "Bus is busy"); - } else { - ESP_LOGW(TAG, "Read failed"); - } - free(data); - i2c_driver_delete(i2c_port); - return 0; -} - -static int do_i2cset_cmd(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **)&i2cset_args); - if (nerrors != 0) { - arg_print_errors(stderr, i2cset_args.end, argv[0]); - return 0; - } - - /* Check chip address: "-c" option */ - int chip_addr = i2cset_args.chip_address->ival[0]; - /* Check register address: "-r" option */ - int data_addr = 0; - if (i2cset_args.register_address->count) { - data_addr = i2cset_args.register_address->ival[0]; - } - /* Check data: "-d" option */ - int len = i2cset_args.data->count; - - i2c_master_driver_initialize(); - i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN); - if (i2cset_args.register_address->count) { - i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN); - } - for (int i = 0; i < len; i++) { - i2c_master_write_byte(cmd, i2cset_args.data->ival[i], ACK_CHECK_EN); - } - i2c_master_stop(cmd); - esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS); - i2c_cmd_link_delete(cmd); - if (ret == ESP_OK) { - ESP_LOGI(TAG, "Write OK"); - } else if (ret == ESP_ERR_TIMEOUT) { - ESP_LOGW(TAG, "Bus is busy"); - } else { - ESP_LOGW(TAG, "Write Failed"); - } - i2c_driver_delete(i2c_port); - return 0; -} - -static int do_i2cconfig_cmd(int argc, char **argv) -{ - int nerrors = arg_parse(argc, argv, (void **)&i2cconfig_args); - if (nerrors != 0) { - arg_print_errors(stderr, i2cconfig_args.end, argv[0]); - return 0; - } - - /* Check "--port" option */ - if (i2cconfig_args.port->count) { - if (i2c_get_port(i2cconfig_args.port->ival[0], &i2c_port) != ESP_OK) { - return 1; - } - } - /* Check "--freq" option */ - if (i2cconfig_args.freq->count) { - i2c_frequency = i2cconfig_args.freq->ival[0]; - } - /* Check "--sda" option */ - i2c_gpio_sda = i2cconfig_args.sda->ival[0]; - /* Check "--scl" option */ - i2c_gpio_scl = i2cconfig_args.scl->ival[0]; - return 0; -} - -int fd; - -void user_delay_ms(uint32_t period); - -void print_sensor_data(struct bme280_data *comp_data); - -int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len); - -int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len); - -int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev); - -/*! - * @brief This function reading the sensor's registers through I2C bus. - */ -int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len) +void i2c_setup() { - write(fd, ®_addr, 1); - read(fd, data, len); - - return 0; + printf("Setting up I²C driver... "); + //i2c_driver_install(0, I2C_MODE_MASTER, 0, 0, 0); + i2c_config_t config; + config.mode = I2C_MODE_MASTER; + config.sda_io_num = 18; + config.sda_pullup_en = GPIO_PULLUP_ENABLE; + config.scl_io_num = 19; + config.scl_pullup_en = GPIO_PULLUP_ENABLE; + config.master.clk_speed = 100000; + i2c_param_config(I2C_NUM_0, &config); + printf("Set driver parameters... "); + esp_err_t err = i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0); + if (err == ESP_OK) + printf("Driver installed!\n"); + else if (err == ESP_ERR_INVALID_ARG) + printf("Driver install failed, invalid arguments!\n"); + else + printf("Driver install failed!\n"); } -/*! - * @brief This function provides the delay for required time (Microseconds) as per the input provided in some of the - * APIs - */ -void user_delay_ms(uint32_t period) +void i2c_detect() { - /* Milliseconds convert to microseconds */ - usleep(period * 1000); + printf("Scanning I²C bus:\n"); + uint8_t address; + printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n"); + for (int i = 0; i < 128; i += 16) { + printf("%02x: ", i); + for (int j = 0; j < 16; j++) { + fflush(stdout); + address = i + j; + i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + i2c_master_start(cmd); + i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, 0x1); + i2c_master_stop(cmd); + esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 50 / portTICK_RATE_MS); + i2c_cmd_link_delete(cmd); + if (ret == ESP_OK) { + printf("%02x ", address); + } else if (ret == ESP_ERR_TIMEOUT) { + printf("UU "); + } else { + printf("-- "); + } + } + printf("\r\n"); + } } -/*! - * @brief This function for writing the sensor's registers through I2C bus. - */ -int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len) +void i2c_shutdown() { - int8_t *buf; - - buf = malloc(len + 1); - buf[0] = reg_addr; - memcpy(buf + 1, data, len); - if (write(fd, buf, len + 1) < len) - { - return BME280_E_COMM_FAIL; - } - - free(buf); - - return BME280_OK; -} - -/*! - * @brief This API used to print the sensor temperature, pressure and humidity data. - */ -void print_sensor_data(struct bme280_data *comp_data) -{ - float temp, press, hum; - -#ifdef BME280_FLOAT_ENABLE - temp = comp_data->temperature; - press = 0.01 * comp_data->pressure; - hum = comp_data->humidity; -#else -#ifdef BME280_64BIT_ENABLE - temp = 0.01f * comp_data->temperature; - press = 0.0001f * comp_data->pressure; - hum = 1.0f / 1024.0f * comp_data->humidity; -#else - temp = 0.01f * comp_data->temperature; - press = 0.01f * comp_data->pressure; - hum = 1.0f / 1024.0f * comp_data->humidity; -#endif -#endif - printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum); -} - -/*! - * @brief This API reads the sensor temperature, pressure and humidity data in forced mode. - */ -int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev) -{ - /* Variable to define the result */ - int8_t rslt = BME280_OK; - - /* Variable to define the selecting sensors */ - uint8_t settings_sel = 0; - - /* Variable to store minimum wait time between consecutive measurement in force mode */ - uint32_t req_delay; - - /* Structure to get the pressure, temperature and humidity values */ - struct bme280_data comp_data; - - /* Recommended mode of operation: Indoor navigation */ - dev->settings.osr_h = BME280_OVERSAMPLING_1X; - dev->settings.osr_p = BME280_OVERSAMPLING_16X; - dev->settings.osr_t = BME280_OVERSAMPLING_2X; - dev->settings.filter = BME280_FILTER_COEFF_16; - - settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL; - - /* Set the sensor settings */ - rslt = bme280_set_sensor_settings(settings_sel, dev); - if (rslt != BME280_OK) - { - fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt); - - return rslt; - } - - printf("Temperature, Pressure, Humidity\n"); - - /*Calculate the minimum delay required between consecutive measurement based upon the sensor enabled - * and the oversampling configuration. */ - req_delay = bme280_cal_meas_delay(&dev->settings); - - /* Continuously stream sensor data */ - while (1) - { - /* Set the sensor to forced mode */ - rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev); - if (rslt != BME280_OK) - { - fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt); - break; - } - - /* Wait for the measurement to complete and print data */ - dev->delay_ms(req_delay); - rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev); - if (rslt != BME280_OK) - { - fprintf(stderr, "Failed to get sensor data (code %+d).", rslt); - break; - } - - print_sensor_data(&comp_data); - } - - return rslt; -} - -static esp_err_t i2c_master_driver_initialize() -{ - i2c_config_t conf = { - .mode = I2C_MODE_MASTER, - .sda_io_num = i2c_gpio_sda, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_io_num = i2c_gpio_scl, - .scl_pullup_en = GPIO_PULLUP_ENABLE, - .master.clk_speed = i2c_frequency - }; - return i2c_param_config(i2c_port, &conf); + printf("Shutting down I²C bus... "); + esp_err_t err = i2c_driver_delete(I2C_NUM_0); + if (err == ESP_ERR_INVALID_ARG) + printf("Failed, invalid arguments!\n"); + else + printf("Success!\n"); } void app_main(void) { + i2c_setup(); + i2c_detect(); + i2c_shutdown(); + u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT; + u8g2_esp32_hal.sda = 18; + u8g2_esp32_hal.scl = 19; + u8g2_esp32_hal_init(u8g2_esp32_hal); + + u8g2_t u8g2; + u8g2_Setup_ssd1306_i2c_128x64_vcomh0_f(&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb, + u8g2_esp32_gpio_and_delay_cb); + + //u8x8_SetI2CAddress(&u8g2.u8x8,0x78); + u8x8_SetI2CAddress(&u8g2.u8x8,0x3C << 1); + u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this, + u8g2_SetPowerSave(&u8g2, 0); // wake up display + + u8g2_SetContrast(&u8g2, 50); + + u8g2_uint_t r = 10; + u8g2_uint_t x = r+1; + u8g2_uint_t y = r+1; + int8_t dx = 2; + int8_t dy = 5; + + while (1) { + u8g2_ClearBuffer(&u8g2); + u8g2_DrawDisc(&u8g2, x, y, r, U8G2_DRAW_ALL); + u8g2_DrawFrame(&u8g2, 0, 0, 128, 64); + u8g2_SendBuffer(&u8g2); + x = x + dx; + y = y + dy; + if (x <= 0+r+1 || x >= 127-r-1) { + dx = dx * -1; + } + if (y <= 0+r+1 || y >= 63-r-1) { + dy = dy * -1; + vTaskDelay(10 / portTICK_PERIOD_MS); + } + } - struct bme280_dev dev; - - /* Variable to define the result */ - int8_t rslt = BME280_OK; - - /* Make sure to select BME280_I2C_ADDR_PRIM or BME280_I2C_ADDR_SEC as needed */ - dev.dev_id = BME280_I2C_ADDR_PRIM; - - /* dev.dev_id = BME280_I2C_ADDR_SEC; */ - dev.intf = BME280_I2C_INTF; - dev.read = user_i2c_read; - dev.write = user_i2c_write; - dev.delay_ms = user_delay_ms; - - if ((fd = open(argv[1], O_RDWR)) < 0) - { - fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]); - exit(1); - } - - #ifdef __KERNEL__ - if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0) - { - fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n"); - exit(1); - } - #endif - - /* Initialize the bme280 */ - rslt = bme280_init(&dev); - if (rslt != BME280_OK) - { - fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt); - exit(1); - } - - rslt = stream_sensor_data_forced_mode(&dev); - if (rslt != BME280_OK) - { - fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt); - exit(1); - } - - return ; } diff --git a/main/u8g2_esp32_hal.c b/main/u8g2_esp32_hal.c new file mode 100644 index 0000000..72ae016 --- /dev/null +++ b/main/u8g2_esp32_hal.c @@ -0,0 +1,166 @@ +#include +#include + +#include "sdkconfig.h" +#include "esp_log.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +#include "u8g2_esp32_hal.h" + +static const char *TAG = "u8g2_hal"; +static const unsigned int I2C_TIMEOUT_MS = 1000; + +static i2c_cmd_handle_t handle_i2c; // I2C handle. +static u8g2_esp32_hal_t u8g2_esp32_hal; // HAL state data. + +#undef ESP_ERROR_CHECK +#define ESP_ERROR_CHECK(x) do { esp_err_t rc = (x); if (rc != ESP_OK) { ESP_LOGE("err", "esp_err_t = %d", rc); assert(0 && #x);} } while(0); + +/* + * Initialze the ESP32 HAL. + */ +void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param) { + u8g2_esp32_hal = u8g2_esp32_hal_param; +} // u8g2_esp32_hal_init + +/* + * HAL callback function as prescribed by the U8G2 library. This callback is invoked + * to handle I2C communications. + */ +uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) { + //ESP_LOGD(TAG, "i2c_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr); + + switch(msg) { + case U8X8_MSG_BYTE_SET_DC: { + if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) { + gpio_set_level(u8g2_esp32_hal.dc, arg_int); + } + break; + } + + case U8X8_MSG_BYTE_INIT: { + if (u8g2_esp32_hal.sda == U8G2_ESP32_HAL_UNDEFINED || + u8g2_esp32_hal.scl == U8G2_ESP32_HAL_UNDEFINED) { + break; + } + + i2c_config_t conf; + conf.mode = I2C_MODE_MASTER; + ESP_LOGI(TAG, "sda_io_num %d", u8g2_esp32_hal.sda); + conf.sda_io_num = u8g2_esp32_hal.sda; + conf.sda_pullup_en = GPIO_PULLUP_ENABLE; + ESP_LOGI(TAG, "scl_io_num %d", u8g2_esp32_hal.scl); + conf.scl_io_num = u8g2_esp32_hal.scl; + conf.scl_pullup_en = GPIO_PULLUP_ENABLE; + ESP_LOGI(TAG, "clk_speed %d", I2C_MASTER_FREQ_HZ); + conf.master.clk_speed = I2C_MASTER_FREQ_HZ; + ESP_LOGI(TAG, "i2c_param_config %d", conf.mode); + ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf)); + ESP_LOGI(TAG, "i2c_driver_install %d", I2C_MASTER_NUM); + ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0)); + break; + } + + case U8X8_MSG_BYTE_SEND: { + uint8_t* data_ptr = (uint8_t*)arg_ptr; + ESP_LOG_BUFFER_HEXDUMP(TAG, data_ptr, arg_int, ESP_LOG_VERBOSE); + + while( arg_int > 0 ) { + ESP_ERROR_CHECK(i2c_master_write_byte(handle_i2c, *data_ptr, ACK_CHECK_EN)); + data_ptr++; + arg_int--; + } + break; + } + + case U8X8_MSG_BYTE_START_TRANSFER: { + uint8_t i2c_address = u8x8_GetI2CAddress(u8x8); + handle_i2c = i2c_cmd_link_create(); + ESP_LOGD(TAG, "Start I2C transfer to %02X.", i2c_address>>1); + ESP_ERROR_CHECK(i2c_master_start(handle_i2c)); + ESP_ERROR_CHECK(i2c_master_write_byte(handle_i2c, i2c_address | I2C_MASTER_WRITE, ACK_CHECK_EN)); + break; + } + + case U8X8_MSG_BYTE_END_TRANSFER: { + ESP_LOGD(TAG, "End I2C transfer."); + ESP_ERROR_CHECK(i2c_master_stop(handle_i2c)); + ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, handle_i2c, I2C_TIMEOUT_MS / portTICK_RATE_MS)); + i2c_cmd_link_delete(handle_i2c); + break; + } + } + return 0; +} // u8g2_esp32_i2c_byte_cb + +/* + * HAL callback function as prescribed by the U8G2 library. This callback is invoked + * to handle callbacks for GPIO and delay functions. + */ +uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) { + ESP_LOGD(TAG, "gpio_and_delay_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr); + + switch(msg) { + // Initialize the GPIO and DELAY HAL functions. If the pins for DC and RESET have been + // specified then we define those pins as GPIO outputs. + case U8X8_MSG_GPIO_AND_DELAY_INIT: { + uint64_t bitmask = 0; + if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) { + bitmask = bitmask | (1ull<