21 static spi_device_handle_t handle_spi; ///< SPI handle. |
21 static spi_device_handle_t handle_spi; ///< SPI handle. |
22 static i2c_cmd_handle_t handle_i2c; ///< I2C handle. |
22 static i2c_cmd_handle_t handle_i2c; ///< I2C handle. |
23 static u8g2_esp32_hal_t u8g2_esp32_hal; ///< HAL state data. |
23 static u8g2_esp32_hal_t u8g2_esp32_hal; ///< HAL state data. |
24 static bool initialized = false; |
24 static bool initialized = false; |
25 |
25 |
26 #undef ESP_ERROR_CHECK |
|
27 #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); |
|
28 |
26 |
29 /* |
27 /* |
30 * Initialze the ESP32 HAL. |
28 * Initialze the ESP32 HAL. |
31 */ |
29 */ |
32 void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param) { |
30 void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param) { |
122 if (u8g2_esp32_hal.sda == U8G2_ESP32_HAL_UNDEFINED || u8g2_esp32_hal.scl == U8G2_ESP32_HAL_UNDEFINED || initialized) { |
120 if (u8g2_esp32_hal.sda == U8G2_ESP32_HAL_UNDEFINED || u8g2_esp32_hal.scl == U8G2_ESP32_HAL_UNDEFINED || initialized) { |
123 // here i test if already initialized, if it's the case don't do it again! |
121 // here i test if already initialized, if it's the case don't do it again! |
124 break; |
122 break; |
125 } |
123 } |
126 |
124 |
127 i2c_config_t conf; |
125 i2c_config_t conf = { |
128 conf.mode = I2C_MODE_MASTER; |
126 .mode = I2C_MODE_MASTER, |
129 conf.sda_io_num = u8g2_esp32_hal.sda; |
127 .sda_io_num = u8g2_esp32_hal.sda, |
130 conf.sda_pullup_en = GPIO_PULLUP_ENABLE; |
128 .sda_pullup_en = GPIO_PULLUP_ENABLE, |
131 conf.scl_io_num = u8g2_esp32_hal.scl; |
129 .scl_io_num = u8g2_esp32_hal.scl, |
132 conf.scl_pullup_en = GPIO_PULLUP_ENABLE; |
130 .scl_pullup_en = GPIO_PULLUP_ENABLE, |
133 conf.master.clk_speed = I2C_MASTER_FREQ_HZ; |
131 .master.clk_speed = I2C_MASTER_FREQ_HZ, |
|
132 }; |
134 ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf)); |
133 ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf)); |
135 ESP_LOGI(TAG, "I2C gpio_sda: %d gpio_scl: %d clk_speed: %d master_num: %d", |
134 ESP_LOGI(TAG, "I2C gpio_sda: %d gpio_scl: %d clk_speed: %d master_num: %d", |
136 u8g2_esp32_hal.sda, u8g2_esp32_hal.scl, I2C_MASTER_FREQ_HZ, I2C_MASTER_NUM); |
135 u8g2_esp32_hal.sda, u8g2_esp32_hal.scl, I2C_MASTER_FREQ_HZ, I2C_MASTER_NUM); |
137 ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0)); |
136 ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0)); |
138 initialized = true; |
137 initialized = true; |
161 } |
160 } |
162 |
161 |
163 case U8X8_MSG_BYTE_END_TRANSFER: { |
162 case U8X8_MSG_BYTE_END_TRANSFER: { |
164 ESP_LOGD(TAG, "End I2C transfer."); |
163 ESP_LOGD(TAG, "End I2C transfer."); |
165 ESP_ERROR_CHECK(i2c_master_stop(handle_i2c)); |
164 ESP_ERROR_CHECK(i2c_master_stop(handle_i2c)); |
166 ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, handle_i2c, I2C_TIMEOUT_MS / portTICK_RATE_MS)); |
165 i2c_master_cmd_begin(I2C_MASTER_NUM, handle_i2c, I2C_TIMEOUT_MS / portTICK_PERIOD_MS); |
167 i2c_cmd_link_delete(handle_i2c); |
166 i2c_cmd_link_delete(handle_i2c); |
168 break; |
167 break; |
169 } |
168 } |
170 } |
169 } |
171 return 0; |
170 return 0; |