10
10
#include "u8g2_esp32_hal.h"
11
11
12
12
static const char * TAG = "u8g2_hal" ;
13
+ static const unsigned int I2C_TIMEOUT_MS = 1000 ;
13
14
14
- static spi_device_handle_t handle ; // SPI handle.
15
- static u8g2_esp32_hal_t u8g2_esp32_hal ; // HAL state data.
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.
16
18
17
19
#undef ESP_ERROR_CHECK
18
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);
@@ -26,10 +28,10 @@ void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param) {
26
28
27
29
/*
28
30
* HAL callback function as prescribed by the U8G2 library. This callback is invoked
29
- * to handle callbacks for communications.
31
+ * to handle SPI communications.
30
32
*/
31
- uint8_t u8g2_esp32_msg_comms_cb (u8x8_t * u8x8 , uint8_t msg , uint8_t arg_int , void * arg_ptr ) {
32
- // ESP_LOGD(tag , "msg_comms_cb : Received a msg: %d: %s", msg, msgToString( msg, arg_int, arg_ptr) );
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 );
33
35
switch (msg ) {
34
36
case U8X8_MSG_BYTE_SET_DC :
35
37
if (u8g2_esp32_hal .dc != U8G2_ESP32_HAL_UNDEFINED ) {
@@ -50,7 +52,7 @@ uint8_t u8g2_esp32_msg_comms_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void
50
52
bus_config .miso_io_num = -1 ; // MISO
51
53
bus_config .quadwp_io_num = -1 ; // Not used
52
54
bus_config .quadhd_io_num = -1 ; // Not used
53
- //ESP_LOGI(tag , "... Initializing bus.");
55
+ //ESP_LOGI(TAG , "... Initializing bus.");
54
56
ESP_ERROR_CHECK (spi_bus_initialize (HSPI_HOST , & bus_config , 1 ));
55
57
56
58
spi_device_interface_config_t dev_config ;
@@ -67,45 +69,44 @@ uint8_t u8g2_esp32_msg_comms_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void
67
69
dev_config .queue_size = 200 ;
68
70
dev_config .pre_cb = NULL ;
69
71
dev_config .post_cb = NULL ;
70
- //ESP_LOGI(tag , "... Adding device bus.");
71
- ESP_ERROR_CHECK (spi_bus_add_device (HSPI_HOST , & dev_config , & handle ));
72
+ //ESP_LOGI(TAG , "... Adding device bus.");
73
+ ESP_ERROR_CHECK (spi_bus_add_device (HSPI_HOST , & dev_config , & handle_spi ));
72
74
73
75
break ;
74
76
}
75
77
76
78
case U8X8_MSG_BYTE_SEND : {
77
79
spi_transaction_t trans_desc ;
78
80
trans_desc .addr = 0 ;
79
- trans_desc .command = 0 ;
81
+ trans_desc .cmd = 0 ;
80
82
trans_desc .flags = 0 ;
81
83
trans_desc .length = 8 * arg_int ; // Number of bits NOT number of bytes.
82
84
trans_desc .rxlength = 0 ;
83
85
trans_desc .tx_buffer = arg_ptr ;
84
86
trans_desc .rx_buffer = NULL ;
85
87
86
- //ESP_LOGI(tag , "... Transmitting %d bytes.", arg_int);
87
- ESP_ERROR_CHECK (spi_device_transmit (handle , & trans_desc ));
88
+ //ESP_LOGI(TAG , "... Transmitting %d bytes.", arg_int);
89
+ ESP_ERROR_CHECK (spi_device_transmit (handle_spi , & trans_desc ));
88
90
break ;
89
91
}
90
92
}
91
93
return 0 ;
92
- } // u8g2_esp32_msg_comms_cb
94
+ } // u8g2_esp32_spi_byte_cb
93
95
94
96
/*
95
97
* HAL callback function as prescribed by the U8G2 library. This callback is invoked
96
- * to handle callbacks for communications.
98
+ * to handle I2C communications.
97
99
*/
98
- uint8_t u8g2_esp32_msg_i2c_cb (u8x8_t * u8x8 , uint8_t msg , uint8_t arg_int , void * arg_ptr ) {
99
-
100
- ESP_LOGD (TAG , "msg_i2c_cb: Received a msg: %d %d" , msg , arg_int );
101
- //ESP_LOGD(tag, "msg_i2c_cb: Received a msg: %d: %s", msg, msgToString(msg, arg_int, arg_ptr));
100
+ uint8_t u8g2_esp32_i2c_byte_cb (u8x8_t * u8x8 , uint8_t msg , uint8_t arg_int , void * arg_ptr ) {
101
+ ESP_LOGD (TAG , "i2c_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p" , msg , arg_int , arg_ptr );
102
102
103
103
switch (msg ) {
104
- case U8X8_MSG_BYTE_SET_DC :
104
+ case U8X8_MSG_BYTE_SET_DC : {
105
105
if (u8g2_esp32_hal .dc != U8G2_ESP32_HAL_UNDEFINED ) {
106
106
gpio_set_level (u8g2_esp32_hal .dc , arg_int );
107
107
}
108
108
break ;
109
+ }
109
110
110
111
case U8X8_MSG_BYTE_INIT : {
111
112
if (u8g2_esp32_hal .sda == U8G2_ESP32_HAL_UNDEFINED ||
@@ -127,129 +128,63 @@ uint8_t u8g2_esp32_msg_i2c_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *
127
128
ESP_ERROR_CHECK (i2c_param_config (I2C_MASTER_NUM , & conf ));
128
129
ESP_LOGI (TAG , "i2c_driver_install %d" , I2C_MASTER_NUM );
129
130
ESP_ERROR_CHECK (i2c_driver_install (I2C_MASTER_NUM , conf .mode , I2C_MASTER_RX_BUF_DISABLE , I2C_MASTER_TX_BUF_DISABLE , 0 ));
130
- /*
131
- i2c_cmd_handle_t cmd = i2c_cmd_link_create(); // dummy write
132
- ESP_ERROR_CHECK(i2c_master_start(cmd));
133
- ESP_ERROR_CHECK(i2c_master_write_byte(cmd, 0x00 | I2C_MASTER_WRITE, ACK_CHECK_DIS));
134
- ESP_ERROR_CHECK(i2c_master_stop(cmd));
135
- ESP_LOGI(TAG, "i2c_master_cmd_begin %d", I2C_MASTER_NUM);
136
- ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS));
137
- i2c_cmd_link_delete(cmd);
138
- */
139
-
140
131
break ;
141
132
}
142
133
143
134
case U8X8_MSG_BYTE_SEND : {
144
- uint8_t * data ;
145
- uint8_t cmddata ;
146
- i2c_cmd_handle_t cmd = i2c_cmd_link_create ();
147
- ESP_ERROR_CHECK (i2c_master_start (cmd ));
148
- // ESP_LOGI(TAG, "I2CAddress %02X", u8x8_GetI2CAddress(u8x8)>>1);
149
- ESP_ERROR_CHECK (i2c_master_write_byte (cmd , u8x8_GetI2CAddress (u8x8 ) | I2C_MASTER_WRITE , ACK_CHECK_EN ));
150
- data = (uint8_t * )arg_ptr ;
151
- if (arg_int == 1 ) {
152
- cmddata = 0 ;
153
- ESP_ERROR_CHECK (i2c_master_write (cmd , & cmddata , 1 , ACK_CHECK_EN ));
154
- // printf("0x%02X ",zerodata);
155
- } else {
156
- cmddata = 0x40 ;
157
- ESP_ERROR_CHECK (i2c_master_write (cmd , & cmddata , 1 , ACK_CHECK_EN ));
158
- //bzero(arg_ptr,arg_int);
159
- //*data=0x40;
160
- }
161
- //ESP_ERROR_CHECK(i2c_master_write(cmd, arg_ptr, arg_int, ACK_CHECK_EN));
135
+ uint8_t * data_ptr = (uint8_t * )arg_ptr ;
136
+ ESP_LOG_BUFFER_HEXDUMP (TAG , data_ptr , arg_int , ESP_LOG_VERBOSE );
162
137
163
- while ( arg_int > 0 ) {
164
- ESP_ERROR_CHECK (i2c_master_write_byte (cmd , * data , ACK_CHECK_EN ));
165
- // printf("0x%02X ",*data);
166
- data ++ ;
138
+ while ( arg_int > 0 ) {
139
+ ESP_ERROR_CHECK (i2c_master_write_byte (handle_i2c , * data_ptr , ACK_CHECK_EN ));
140
+ data_ptr ++ ;
167
141
arg_int -- ;
168
- }
169
- // printf("\n");
170
-
171
- ESP_ERROR_CHECK (i2c_master_stop (cmd ));
172
- // ESP_LOGI(TAG, "i2c_master_cmd_begin %d", I2C_MASTER_NUM);
173
- ESP_ERROR_CHECK (i2c_master_cmd_begin (I2C_MASTER_NUM , cmd , 1000 / portTICK_RATE_MS ));
174
- i2c_cmd_link_delete (cmd );
175
- break ;
176
- }
177
- }
178
- return 0 ;
179
- } // u8g2_esp32_msg_i2c_cb
180
-
181
- /*
182
- * HAL callback function as prescribed by the U8G2 library. This callback is invoked
183
- * to handle callbacks for GPIO and delay functions.
184
- */
185
- uint8_t u8g2_esp32_msg_gpio_and_delay_cb (u8x8_t * u8x8 , uint8_t msg , uint8_t arg_int , void * arg_ptr ) {
186
- //ESP_LOGD(tag, "msg_gpio_and_delay_cb: Received a msg: %d: %s", msg, msgToString(msg, arg_int, arg_ptr));
187
- switch (msg ) {
188
-
189
- // Initialize the GPIO and DELAY HAL functions. If the pins for DC and RESET have been
190
- // specified then we define those pins as GPIO outputs.
191
- case U8X8_MSG_GPIO_AND_DELAY_INIT : {
192
- uint64_t bitmask = 0 ;
193
- if (u8g2_esp32_hal .dc != U8G2_ESP32_HAL_UNDEFINED ) {
194
- bitmask = bitmask | (1 <<u8g2_esp32_hal .dc );
195
- }
196
- if (u8g2_esp32_hal .reset != U8G2_ESP32_HAL_UNDEFINED ) {
197
- bitmask = bitmask | (1 <<u8g2_esp32_hal .reset );
198
142
}
199
-
200
- gpio_config_t gpioConfig ;
201
- gpioConfig .pin_bit_mask = bitmask ;
202
- gpioConfig .mode = GPIO_MODE_OUTPUT ;
203
- gpioConfig .pull_up_en = GPIO_PULLUP_DISABLE ;
204
- gpioConfig .pull_down_en = GPIO_PULLDOWN_ENABLE ;
205
- gpioConfig .intr_type = GPIO_INTR_DISABLE ;
206
- gpio_config (& gpioConfig );
207
143
break ;
208
144
}
209
145
210
- // Set the GPIO reset pin to the value passed in through arg_int.
211
- case U8X8_MSG_GPIO_RESET :
212
- if (u8g2_esp32_hal .reset != U8G2_ESP32_HAL_UNDEFINED ) {
213
- gpio_set_level (u8g2_esp32_hal .reset , arg_int );
214
- }
146
+ case U8X8_MSG_BYTE_START_TRANSFER : {
147
+ uint8_t i2c_address = u8x8_GetI2CAddress (u8x8 );
148
+ handle_i2c = i2c_cmd_link_create ();
149
+ ESP_LOGD (TAG , "Start I2C transfer to %02X." , i2c_address >>1 );
150
+ ESP_ERROR_CHECK (i2c_master_start (handle_i2c ));
151
+ ESP_ERROR_CHECK (i2c_master_write_byte (handle_i2c , i2c_address | I2C_MASTER_WRITE , ACK_CHECK_EN ));
215
152
break ;
153
+ }
216
154
217
- // Delay for the number of milliseconds passed in through arg_int.
218
- case U8X8_MSG_DELAY_MILLI :
219
- vTaskDelay (arg_int /portTICK_PERIOD_MS );
155
+ case U8X8_MSG_BYTE_END_TRANSFER : {
156
+ ESP_LOGD (TAG , "End I2C transfer." );
157
+ ESP_ERROR_CHECK (i2c_master_stop (handle_i2c ));
158
+ ESP_ERROR_CHECK (i2c_master_cmd_begin (I2C_MASTER_NUM , handle_i2c , I2C_TIMEOUT_MS / portTICK_RATE_MS ));
159
+ i2c_cmd_link_delete (handle_i2c );
220
160
break ;
161
+ }
221
162
}
222
163
return 0 ;
223
- } // u8g2_esp32_msg_gpio_and_delay_cb
164
+ } // u8g2_esp32_i2c_byte_cb
224
165
225
166
/*
226
167
* HAL callback function as prescribed by the U8G2 library. This callback is invoked
227
- * to handle callbacks for I²C and delay functions.
168
+ * to handle callbacks for GPIO and delay functions.
228
169
*/
229
- uint8_t u8g2_esp32_msg_i2c_and_delay_cb (u8x8_t * u8x8 , uint8_t msg , uint8_t arg_int , void * arg_ptr ) {
230
-
231
- ESP_LOGD (TAG , "msg_i2c_and_delay_cb: Received a msg: %d" , msg );
170
+ uint8_t u8g2_esp32_gpio_and_delay_cb (u8x8_t * u8x8 , uint8_t msg , uint8_t arg_int , void * arg_ptr ) {
171
+ ESP_LOGD (TAG , "gpio_and_delay_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p" , msg , arg_int , arg_ptr );
232
172
233
173
switch (msg ) {
234
174
// Initialize the GPIO and DELAY HAL functions. If the pins for DC and RESET have been
235
175
// specified then we define those pins as GPIO outputs.
236
176
case U8X8_MSG_GPIO_AND_DELAY_INIT : {
237
177
uint64_t bitmask = 0 ;
238
178
if (u8g2_esp32_hal .dc != U8G2_ESP32_HAL_UNDEFINED ) {
239
- bitmask = bitmask | (1 <<u8g2_esp32_hal .dc );
179
+ bitmask = bitmask | (1ull <<u8g2_esp32_hal .dc );
240
180
}
241
181
if (u8g2_esp32_hal .reset != U8G2_ESP32_HAL_UNDEFINED ) {
242
- bitmask = bitmask | (1 <<u8g2_esp32_hal .reset );
182
+ bitmask = bitmask | (1ull <<u8g2_esp32_hal .reset );
243
183
}
244
184
if (u8g2_esp32_hal .cs != U8G2_ESP32_HAL_UNDEFINED ) {
245
- bitmask = bitmask | (1 <<u8g2_esp32_hal .cs );
246
- }
247
- if (u8g2_esp32_hal .sda != U8G2_ESP32_HAL_UNDEFINED ) {
248
- //bitmask = bitmask | (1<<u8g2_esp32_hal.sda);
249
- }
250
- if (u8g2_esp32_hal .scl != U8G2_ESP32_HAL_UNDEFINED ) {
251
- //bitmask = bitmask | (1<<u8g2_esp32_hal.scl);
185
+ bitmask = bitmask | (1ull <<u8g2_esp32_hal .cs );
252
186
}
187
+
253
188
if (bitmask == 0 ) {
254
189
break ;
255
190
}
@@ -296,4 +231,4 @@ uint8_t u8g2_esp32_msg_i2c_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_i
296
231
break ;
297
232
}
298
233
return 0 ;
299
- } // u8g2_esp32_msg_gpio_and_delay_cb
234
+ } // u8g2_esp32_gpio_and_delay_cb
0 commit comments