8 #include "freertos/task.h" |
8 #include "freertos/task.h" |
9 |
9 |
10 #include "u8g2_esp32_hal.h" |
10 #include "u8g2_esp32_hal.h" |
11 |
11 |
12 static const char *TAG = "u8g2_hal"; |
12 static const char *TAG = "u8g2_hal"; |
13 static const unsigned int I2C_TIMEOUT_MS = 5000; |
13 static const unsigned int I2C_TIMEOUT_MS = 1000; |
14 |
14 |
15 static spi_device_handle_t handle_spi; // SPI handle. |
15 static spi_device_handle_t handle_spi; // SPI handle. |
16 static i2c_cmd_handle_t handle_i2c; // I2C handle. |
16 static i2c_cmd_handle_t handle_i2c; // I2C handle. |
17 static u8g2_esp32_hal_t u8g2_esp32_hal; // HAL state data. |
17 static u8g2_esp32_hal_t u8g2_esp32_hal; // HAL state data. |
|
18 static bool initialized = false; |
18 |
19 |
19 #undef ESP_ERROR_CHECK |
20 #undef ESP_ERROR_CHECK |
20 #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); |
21 #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); |
21 |
22 |
22 /* |
23 /* |
109 break; |
110 break; |
110 } |
111 } |
111 |
112 |
112 case U8X8_MSG_BYTE_INIT: { |
113 case U8X8_MSG_BYTE_INIT: { |
113 if (u8g2_esp32_hal.sda == U8G2_ESP32_HAL_UNDEFINED || |
114 if (u8g2_esp32_hal.sda == U8G2_ESP32_HAL_UNDEFINED || |
114 u8g2_esp32_hal.scl == U8G2_ESP32_HAL_UNDEFINED) { |
115 u8g2_esp32_hal.scl == U8G2_ESP32_HAL_UNDEFINED || initialized) { // here i test if already initialized, if it's the case don't do it again! |
115 break; |
116 break; |
116 } |
117 } |
117 |
118 |
118 i2c_config_t conf; |
119 i2c_config_t conf; |
119 conf.mode = I2C_MODE_MASTER; |
120 conf.mode = I2C_MODE_MASTER; |
|
121 ESP_LOGI(TAG, "sda_io_num %d", u8g2_esp32_hal.sda); |
120 conf.sda_io_num = u8g2_esp32_hal.sda; |
122 conf.sda_io_num = u8g2_esp32_hal.sda; |
121 conf.sda_pullup_en = GPIO_PULLUP_ENABLE; |
123 conf.sda_pullup_en = GPIO_PULLUP_ENABLE; |
|
124 ESP_LOGI(TAG, "scl_io_num %d", u8g2_esp32_hal.scl); |
122 conf.scl_io_num = u8g2_esp32_hal.scl; |
125 conf.scl_io_num = u8g2_esp32_hal.scl; |
123 conf.scl_pullup_en = GPIO_PULLUP_ENABLE; |
126 conf.scl_pullup_en = GPIO_PULLUP_ENABLE; |
|
127 ESP_LOGI(TAG, "clk_speed %d", I2C_MASTER_FREQ_HZ); |
124 conf.master.clk_speed = I2C_MASTER_FREQ_HZ; |
128 conf.master.clk_speed = I2C_MASTER_FREQ_HZ; |
|
129 ESP_LOGI(TAG, "i2c_param_config %d", conf.mode); |
125 ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf)); |
130 ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf)); |
|
131 ESP_LOGI(TAG, "i2c_driver_install %d", I2C_MASTER_NUM); |
126 ESP_LOGI(TAG, "I2C gpio_sda: %d gpio_scl: %d clk_speed: %d master_num: %d", |
132 ESP_LOGI(TAG, "I2C gpio_sda: %d gpio_scl: %d clk_speed: %d master_num: %d", |
127 u8g2_esp32_hal.sda, u8g2_esp32_hal.scl, I2C_MASTER_FREQ_HZ, I2C_MASTER_NUM); |
133 u8g2_esp32_hal.sda, u8g2_esp32_hal.scl, I2C_MASTER_FREQ_HZ, I2C_MASTER_NUM); |
128 ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0)); |
134 ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0)); |
|
135 initialized=true; |
129 break; |
136 break; |
130 } |
137 } |
131 |
138 |
132 case U8X8_MSG_BYTE_SEND: { |
139 case U8X8_MSG_BYTE_SEND: { |
133 uint8_t* data_ptr = (uint8_t*)arg_ptr; |
140 uint8_t* data_ptr = (uint8_t*)arg_ptr; |