Skip to content

Commit 0a17d6e

Browse files
committed
HAL updated
Updated the HAL by N. Kolban to be compatible with framework v3
1 parent 43c5d62 commit 0a17d6e

File tree

3 files changed

+57
-123
lines changed

3 files changed

+57
-123
lines changed

18_u8g2/main/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ void app_main() {
2626
u8g2_Setup_ssd1306_128x64_noname_f(
2727
&u8g2,
2828
U8G2_R0,
29-
u8g2_esp32_msg_i2c_cb,
30-
u8g2_esp32_msg_i2c_and_delay_cb);
29+
u8g2_esp32_i2c_byte_cb,
30+
u8g2_esp32_gpio_and_delay_cb);
3131

3232
// set the display address
3333
u8x8_SetI2CAddress(&u8g2.u8x8, 0x78);

18_u8g2/main/u8g2_esp32_hal.c

Lines changed: 46 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include "u8g2_esp32_hal.h"
1111

1212
static const char *TAG = "u8g2_hal";
13+
static const unsigned int I2C_TIMEOUT_MS = 1000;
1314

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.
1618

1719
#undef ESP_ERROR_CHECK
1820
#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) {
2628

2729
/*
2830
* HAL callback function as prescribed by the U8G2 library. This callback is invoked
29-
* to handle callbacks for communications.
31+
* to handle SPI communications.
3032
*/
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);
3335
switch(msg) {
3436
case U8X8_MSG_BYTE_SET_DC:
3537
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
5052
bus_config.miso_io_num = -1; // MISO
5153
bus_config.quadwp_io_num = -1; // Not used
5254
bus_config.quadhd_io_num = -1; // Not used
53-
//ESP_LOGI(tag, "... Initializing bus.");
55+
//ESP_LOGI(TAG, "... Initializing bus.");
5456
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &bus_config, 1));
5557

5658
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
6769
dev_config.queue_size = 200;
6870
dev_config.pre_cb = NULL;
6971
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));
7274

7375
break;
7476
}
7577

7678
case U8X8_MSG_BYTE_SEND: {
7779
spi_transaction_t trans_desc;
7880
trans_desc.addr = 0;
79-
trans_desc.command = 0;
81+
trans_desc.cmd = 0;
8082
trans_desc.flags = 0;
8183
trans_desc.length = 8 * arg_int; // Number of bits NOT number of bytes.
8284
trans_desc.rxlength = 0;
8385
trans_desc.tx_buffer = arg_ptr;
8486
trans_desc.rx_buffer = NULL;
8587

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));
8890
break;
8991
}
9092
}
9193
return 0;
92-
} // u8g2_esp32_msg_comms_cb
94+
} // u8g2_esp32_spi_byte_cb
9395

9496
/*
9597
* HAL callback function as prescribed by the U8G2 library. This callback is invoked
96-
* to handle callbacks for communications.
98+
* to handle I2C communications.
9799
*/
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);
102102

103103
switch(msg) {
104-
case U8X8_MSG_BYTE_SET_DC:
104+
case U8X8_MSG_BYTE_SET_DC: {
105105
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) {
106106
gpio_set_level(u8g2_esp32_hal.dc, arg_int);
107107
}
108108
break;
109+
}
109110

110111
case U8X8_MSG_BYTE_INIT: {
111112
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 *
127128
ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf));
128129
ESP_LOGI(TAG, "i2c_driver_install %d", I2C_MASTER_NUM);
129130
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-
140131
break;
141132
}
142133

143134
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);
162137

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++;
167141
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);
198142
}
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);
207143
break;
208144
}
209145

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));
215152
break;
153+
}
216154

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);
220160
break;
161+
}
221162
}
222163
return 0;
223-
} // u8g2_esp32_msg_gpio_and_delay_cb
164+
} // u8g2_esp32_i2c_byte_cb
224165

225166
/*
226167
* 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.
228169
*/
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);
232172

233173
switch(msg) {
234174
// Initialize the GPIO and DELAY HAL functions. If the pins for DC and RESET have been
235175
// specified then we define those pins as GPIO outputs.
236176
case U8X8_MSG_GPIO_AND_DELAY_INIT: {
237177
uint64_t bitmask = 0;
238178
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);
240180
}
241181
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);
243183
}
244184
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);
252186
}
187+
253188
if (bitmask==0) {
254189
break;
255190
}
@@ -296,4 +231,4 @@ uint8_t u8g2_esp32_msg_i2c_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_i
296231
break;
297232
}
298233
return 0;
299-
} // u8g2_esp32_msg_gpio_and_delay_cb
234+
} // u8g2_esp32_gpio_and_delay_cb

18_u8g2/main/u8g2_esp32_hal.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
#define U8G2_ESP32_HAL_UNDEFINED (-1)
1717

18-
#define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */
19-
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
20-
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
21-
#define I2C_MASTER_FREQ_HZ 50000 /*!< I2C master clock frequency */
22-
#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
23-
#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
18+
#define I2C_MASTER_NUM I2C_NUM_1 // I2C port number for master dev
19+
#define I2C_MASTER_TX_BUF_DISABLE 0 // I2C master do not need buffer
20+
#define I2C_MASTER_RX_BUF_DISABLE 0 // I2C master do not need buffer
21+
#define I2C_MASTER_FREQ_HZ 50000 // I2C master clock frequency
22+
#define ACK_CHECK_EN 0x1 // I2C master will check ack from slave
23+
#define ACK_CHECK_DIS 0x0 // I2C master will not check ack from slave
2424

2525
typedef struct {
2626
gpio_num_t clk;
@@ -35,8 +35,7 @@ typedef struct {
3535
#define U8G2_ESP32_HAL_DEFAULT {U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED }
3636

3737
void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param);
38-
uint8_t u8g2_esp32_msg_comms_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
39-
uint8_t u8g2_esp32_msg_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
40-
uint8_t u8g2_esp32_msg_i2c_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
41-
uint8_t u8g2_esp32_msg_i2c_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
38+
uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
39+
uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
40+
uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
4241
#endif /* U8G2_ESP32_HAL_H_ */

0 commit comments

Comments
 (0)