main/u8g2_esp32_hal.c

changeset 0
88d965579617
child 37
358bbd5b608e
equal deleted inserted replaced
-1:000000000000 0:88d965579617
1 #include <stdio.h>
2 #include <string.h>
3
4 #include "sdkconfig.h"
5 #include "esp_log.h"
6
7 #include "freertos/FreeRTOS.h"
8 #include "freertos/task.h"
9
10 #include "u8g2_esp32_hal.h"
11
12 static const char *TAG = "u8g2_hal";
13 static const unsigned int I2C_TIMEOUT_MS = 1000;
14
15 static spi_device_handle_t handle_spi; // SPI handle.
16 static i2c_cmd_handle_t handle_i2c; // I2C handle.
17 static u8g2_esp32_hal_t u8g2_esp32_hal; // HAL state data.
18
19 #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
22 /*
23 * Initialze the ESP32 HAL.
24 */
25 void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param) {
26 u8g2_esp32_hal = u8g2_esp32_hal_param;
27 } // u8g2_esp32_hal_init
28
29 /*
30 * HAL callback function as prescribed by the U8G2 library. This callback is invoked
31 * to handle SPI communications.
32 */
33 uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
34 ESP_LOGD(TAG, "spi_byte_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr);
35 switch(msg) {
36 case U8X8_MSG_BYTE_SET_DC:
37 if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) {
38 gpio_set_level(u8g2_esp32_hal.dc, arg_int);
39 }
40 break;
41
42 case U8X8_MSG_BYTE_INIT: {
43 if (u8g2_esp32_hal.clk == U8G2_ESP32_HAL_UNDEFINED ||
44 u8g2_esp32_hal.mosi == U8G2_ESP32_HAL_UNDEFINED ||
45 u8g2_esp32_hal.cs == U8G2_ESP32_HAL_UNDEFINED) {
46 break;
47 }
48
49 spi_bus_config_t bus_config;
50 memset(&bus_config, 0, sizeof(spi_bus_config_t));
51 bus_config.sclk_io_num = u8g2_esp32_hal.clk; // CLK
52 bus_config.mosi_io_num = u8g2_esp32_hal.mosi; // MOSI
53 bus_config.miso_io_num = -1; // MISO
54 bus_config.quadwp_io_num = -1; // Not used
55 bus_config.quadhd_io_num = -1; // Not used
56 //ESP_LOGI(TAG, "... Initializing bus.");
57 ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &bus_config, 1));
58
59 spi_device_interface_config_t dev_config;
60 dev_config.address_bits = 0;
61 dev_config.command_bits = 0;
62 dev_config.dummy_bits = 0;
63 dev_config.mode = 0;
64 dev_config.duty_cycle_pos = 0;
65 dev_config.cs_ena_posttrans = 0;
66 dev_config.cs_ena_pretrans = 0;
67 dev_config.clock_speed_hz = 10000;
68 dev_config.spics_io_num = u8g2_esp32_hal.cs;
69 dev_config.flags = 0;
70 dev_config.queue_size = 200;
71 dev_config.pre_cb = NULL;
72 dev_config.post_cb = NULL;
73 //ESP_LOGI(TAG, "... Adding device bus.");
74 ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &dev_config, &handle_spi));
75
76 break;
77 }
78
79 case U8X8_MSG_BYTE_SEND: {
80 spi_transaction_t trans_desc;
81 trans_desc.addr = 0;
82 trans_desc.cmd = 0;
83 trans_desc.flags = 0;
84 trans_desc.length = 8 * arg_int; // Number of bits NOT number of bytes.
85 trans_desc.rxlength = 0;
86 trans_desc.tx_buffer = arg_ptr;
87 trans_desc.rx_buffer = NULL;
88
89 //ESP_LOGI(TAG, "... Transmitting %d bytes.", arg_int);
90 ESP_ERROR_CHECK(spi_device_transmit(handle_spi, &trans_desc));
91 break;
92 }
93 }
94 return 0;
95 } // u8g2_esp32_spi_byte_cb
96
97 /*
98 * HAL callback function as prescribed by the U8G2 library. This callback is invoked
99 * to handle I2C communications.
100 */
101 uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
102 ESP_LOGD(TAG, "i2c_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr);
103
104 switch(msg) {
105 case U8X8_MSG_BYTE_SET_DC: {
106 if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) {
107 gpio_set_level(u8g2_esp32_hal.dc, arg_int);
108 }
109 break;
110 }
111
112 case U8X8_MSG_BYTE_INIT: {
113 if (u8g2_esp32_hal.sda == U8G2_ESP32_HAL_UNDEFINED ||
114 u8g2_esp32_hal.scl == U8G2_ESP32_HAL_UNDEFINED) {
115 break;
116 }
117
118 i2c_config_t conf;
119 conf.mode = I2C_MODE_MASTER;
120 ESP_LOGI(TAG, "sda_io_num %d", u8g2_esp32_hal.sda);
121 conf.sda_io_num = u8g2_esp32_hal.sda;
122 conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
123 ESP_LOGI(TAG, "scl_io_num %d", u8g2_esp32_hal.scl);
124 conf.scl_io_num = u8g2_esp32_hal.scl;
125 conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
126 ESP_LOGI(TAG, "clk_speed %d", I2C_MASTER_FREQ_HZ);
127 conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
128 ESP_LOGI(TAG, "i2c_param_config %d", conf.mode);
129 ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf));
130 ESP_LOGI(TAG, "i2c_driver_install %d", I2C_MASTER_NUM);
131 ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0));
132 break;
133 }
134
135 case U8X8_MSG_BYTE_SEND: {
136 uint8_t* data_ptr = (uint8_t*)arg_ptr;
137 ESP_LOG_BUFFER_HEXDUMP(TAG, data_ptr, arg_int, ESP_LOG_VERBOSE);
138
139 while( arg_int > 0 ) {
140 ESP_ERROR_CHECK(i2c_master_write_byte(handle_i2c, *data_ptr, ACK_CHECK_EN));
141 data_ptr++;
142 arg_int--;
143 }
144 break;
145 }
146
147 case U8X8_MSG_BYTE_START_TRANSFER: {
148 uint8_t i2c_address = u8x8_GetI2CAddress(u8x8);
149 handle_i2c = i2c_cmd_link_create();
150 ESP_LOGD(TAG, "Start I2C transfer to %02X.", i2c_address>>1);
151 ESP_ERROR_CHECK(i2c_master_start(handle_i2c));
152 ESP_ERROR_CHECK(i2c_master_write_byte(handle_i2c, i2c_address | I2C_MASTER_WRITE, ACK_CHECK_EN));
153 break;
154 }
155
156 case U8X8_MSG_BYTE_END_TRANSFER: {
157 ESP_LOGD(TAG, "End I2C transfer.");
158 ESP_ERROR_CHECK(i2c_master_stop(handle_i2c));
159 ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, handle_i2c, I2C_TIMEOUT_MS / portTICK_RATE_MS));
160 i2c_cmd_link_delete(handle_i2c);
161 break;
162 }
163 }
164 return 0;
165 } // u8g2_esp32_i2c_byte_cb
166
167 /*
168 * HAL callback function as prescribed by the U8G2 library. This callback is invoked
169 * to handle callbacks for GPIO and delay functions.
170 */
171 uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
172 ESP_LOGD(TAG, "gpio_and_delay_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr);
173
174 switch(msg) {
175 // Initialize the GPIO and DELAY HAL functions. If the pins for DC and RESET have been
176 // specified then we define those pins as GPIO outputs.
177 case U8X8_MSG_GPIO_AND_DELAY_INIT: {
178 uint64_t bitmask = 0;
179 if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) {
180 bitmask = bitmask | (1ull<<u8g2_esp32_hal.dc);
181 }
182 if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED) {
183 bitmask = bitmask | (1ull<<u8g2_esp32_hal.reset);
184 }
185 if (u8g2_esp32_hal.cs != U8G2_ESP32_HAL_UNDEFINED) {
186 bitmask = bitmask | (1ull<<u8g2_esp32_hal.cs);
187 }
188
189 if (bitmask==0) {
190 break;
191 }
192 gpio_config_t gpioConfig;
193 gpioConfig.pin_bit_mask = bitmask;
194 gpioConfig.mode = GPIO_MODE_OUTPUT;
195 gpioConfig.pull_up_en = GPIO_PULLUP_DISABLE;
196 gpioConfig.pull_down_en = GPIO_PULLDOWN_ENABLE;
197 gpioConfig.intr_type = GPIO_INTR_DISABLE;
198 gpio_config(&gpioConfig);
199 break;
200 }
201
202 // Set the GPIO reset pin to the value passed in through arg_int.
203 case U8X8_MSG_GPIO_RESET:
204 if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED) {
205 gpio_set_level(u8g2_esp32_hal.reset, arg_int);
206 }
207 break;
208 // Set the GPIO client select pin to the value passed in through arg_int.
209 case U8X8_MSG_GPIO_CS:
210 if (u8g2_esp32_hal.cs != U8G2_ESP32_HAL_UNDEFINED) {
211 gpio_set_level(u8g2_esp32_hal.cs, arg_int);
212 }
213 break;
214 // Set the Software I²C pin to the value passed in through arg_int.
215 case U8X8_MSG_GPIO_I2C_CLOCK:
216 if (u8g2_esp32_hal.scl != U8G2_ESP32_HAL_UNDEFINED) {
217 gpio_set_level(u8g2_esp32_hal.scl, arg_int);
218 // printf("%c",(arg_int==1?'C':'c'));
219 }
220 break;
221 // Set the Software I²C pin to the value passed in through arg_int.
222 case U8X8_MSG_GPIO_I2C_DATA:
223 if (u8g2_esp32_hal.sda != U8G2_ESP32_HAL_UNDEFINED) {
224 gpio_set_level(u8g2_esp32_hal.sda, arg_int);
225 // printf("%c",(arg_int==1?'D':'d'));
226 }
227 break;
228
229 // Delay for the number of milliseconds passed in through arg_int.
230 case U8X8_MSG_DELAY_MILLI:
231 vTaskDelay(arg_int/portTICK_PERIOD_MS);
232 break;
233 }
234 return 0;
235 } // u8g2_esp32_gpio_and_delay_cb

mercurial