8000 Merge pull request #2 from pcbreflux/pcbreflux_custom · mikepang/esp32-snippets@a7ea154 · GitHub
[go: up one dir, main page]

Skip to content

Commit a7ea154

Browse files
authored
Merge pull request nkolban#2 from pcbreflux/pcbreflux_custom
I²C for u8g2 with ssd1306
2 parents 7ddb504 + 99b99ff commit a7ea154

File tree

3 files changed

+248
-129
lines changed

3 files changed

+248
-129
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <driver/gpio.h>
2+
#include <driver/spi_master.h>
3+
#include <esp_log.h>
4+
#include <freertos/FreeRTOS.h>
5+
#include <freertos/task.h>
6+
#include <stdio.h>
7+
#include <string.h>
8+
#include <u8g2.h>
9+
10+
#include "sdkconfig.h"
11+
#include "u8g2_esp32_hal.h"
12+
13+
// SDA - GPIO21
14+
#define PIN_SDA 21
15+
16+
// SCL - GPIO22
17+
#define PIN_SCL 22
18+
19+
static const char *TAG = "ssd1306";
20+
21+
void task_test_SSD1306i2c(void *ignore) {
22+
u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
23+
u8g2_esp32_hal.sda = PIN_SDA;
24+
u8g2_esp32_hal.scl = PIN_SCL;
25+
u8g2_esp32_hal_init(u8g2_esp32_hal);
26+
27+
28+
u8g2_t u8g2; // a structure which will contain all the data for one display
29+
u8g2_Setup_ssd1306_128x32_univision_f(
30+
&u8g2,
31+
U8G2_R0,
32+
//u8x8_byte_sw_i2c,
33+
u8g2_esp32_msg_i2c_cb,
34+
u8g2_esp32_msg_i2c_and_delay_cb); // init u8g2 structure
35+
u8x8_SetI2CAddress(&u8g2.u8x8,0x78);
36+
37+
ESP_LOGI(TAG, "u8g2_InitDisplay");
38+
u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this,
39+
40+
ESP_LOGI(TAG, "u8g2_SetPowerSave");
41+
u8g2_SetPowerSave(&u8g2, 0); // wake up display
42+
ESP_LOGI(TAG, "u8g2_ClearBuffer");
43+
u8g2_ClearBuffer(&u8g2);
44+
ESP_LOGI(TAG, "u8g2_DrawBox");
45+
u8g2_DrawBox(&u8g2, 0, 26, 80,6);
46+
u8g2_DrawFrame(&u8g2, 0,26,100,6);
47+
48+
ESP_LOGI(TAG, "u8g2_SetFont");
49+
u8g2_SetFont(&u8g2, u8g2_font_ncenB14_tr);
50+
ESP_LOGI(TAG, "u8g2_DrawStr");
51+
u8g2_DrawStr(&u8g2, 2,17,"Hi nkolban!");
52+
ESP_LOGI(TAG, "u8g2_SendBuffer");
53+
u8g2_SendBuffer(&u8g2);
54+
55+
ESP_LOGI(TAG, "All done!");
56+
57+
vTaskDelete(NULL);
58+
}
59+
Lines changed: 172 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,29 @@
1-
#include <driver/gpio.h>
2-
#include <driver/spi_master.h>
3-
#include <esp_log.h>
4-
#include <freertos/FreeRTOS.h>
5-
#include <freertos/task.h>
61
#include <stdio.h>
72
#include <string.h>
8-
#include <u8g2.h>
93

10< A3D4 /code>4
#include "sdkconfig.h"
5+
#include "esp_log.h"
6+
7+
#include "freertos/FreeRTOS.h"
8+
#include "freertos/task.h"
9+
1110
#include "u8g2_esp32_hal.h"
1211

13-
//static char tag[] = "u8g2_esp32_hal";
12+
static const char *TAG = "u8g2_hal";
1413

1514
static spi_device_handle_t handle; // SPI handle.
1615
static u8g2_esp32_hal_t u8g2_esp32_hal; // HAL state data.
1716

17+
#undef ESP_ERROR_CHECK
18+
#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);
19+
1820
/*
1921
* Initialze the ESP32 HAL.
2022
*/
2123
void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param) {
2224
u8g2_esp32_hal = u8g2_esp32_hal_param;
2325
} // u8g2_esp32_hal_init
2426

25-
26-
/*
27-
static char *bytesToString(int length, char *data) {
28-
static char ret[1024];
29-
strcpy(ret, "");
30-
int i;
31 F438 -
char temp[10];
32-
for (i=0; i<length; i++) {
33-
sprintf(temp, "%.2x ", data[i]);
34-
strcat(ret, temp);
35-
}
36-
return ret;
37-
}
38-
39-
40-
static char *msgToString(uint8_t msg, uint8_t arg_int, void *arg_ptr) {
41-
static char buf[1024];
42-
switch(msg)
43-
{
44-
case U8X8_MSG_GPIO_AND_DELAY_INIT: // called once during init phase of u8g2/u8x8
45-
return "U8X8_MSG_GPIO_AND_DELAY_INIT"; // can be used to setup pins
46-
47-
case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second
48-
sprintf(buf, "U8X8_MSG_DELAY_NANO: %d ns", arg_int);
49-
return buf;
50-
51-
case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
52-
return "U8X8_MSG_DELAY_100NANO";
53-
case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
54-
return "U8X8_MSG_DELAY_10MICRO";
55-
56-
case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
57-
sprintf(buf, "U8X8_MSG_DELAY_MILLI: %d ms", arg_int);
58-
return buf;
59-
60-
case U8X8_MSG_DELAY_I2C: // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
61-
return "U8X8_MSG_DELAY_I2C";
62-
break; // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
63-
case U8X8_MSG_GPIO_D0: // D0 or SPI clock pin: Output level in arg_int
64-
//case U8X8_MSG_GPIO_SPI_CLOCK:
65-
return "U8X8_MSG_GPIO_D0";
66-
case U8X8_MSG_GPIO_D1: // D1 or SPI data pin: Output level in arg_int
67-
//case U8X8_MSG_GPIO_SPI_DATA:
68-
return "U8X8_MSG_GPIO_D1";
69-
case U8X8_MSG_GPIO_D2: // D2 pin: Output level in arg_int
70-
return "U8X8_MSG_GPIO_D2";
71-
case U8X8_MSG_GPIO_D3: // D3 pin: Output level in arg_int
72-
return "U8X8_MSG_GPIO_D3";
73-
case U8X8_MSG_GPIO_D4: // D4 pin: Output level in arg_int
74-
return "U8X8_MSG_GPIO_D4";
75-
case U8X8_MSG_GPIO_D5: // D5 pin: Output level in arg_int
76-
return "U8X8_MSG_GPIO_D5";
77-
case U8X8_MSG_GPIO_D6: // D6 pin: Output level in arg_int
78-
return "U8X8_MSG_GPIO_D6";
79-
case U8X8_MSG_GPIO_D7: // D7 pin: Output level in arg_int
80-
return "U8X8_MSG_GPIO_D7";
81-
case U8X8_MSG_GPIO_E: // E/WR pin: Output level in arg_int
82-
return "U8X8_MSG_GPIO_E";
83-
case U8X8_MSG_GPIO_CS: // CS (chip select) pin: Output level in arg_int
84-
return "U8X8_MSG_GPIO_CS";
85-
case U8X8_MSG_GPIO_DC: // DC (data/cmd, A0, register select) pin: Output level in arg_int
86-
return "U8X8_MSG_GPIO_DC";
87-
case U8X8_MSG_GPIO_RESET: // Reset pin: Output level in arg_int
88-
sprintf(buf, "U8X8_MSG_GPIO_RESET -> %d", arg_int);
89-
return buf;
90-
91-
case U8X8_MSG_GPIO_CS1: // CS1 (chip select) pin: Output level in arg_int
92-
return "U8X8_MSG_GPIO_CS1";
93-
case U8X8_MSG_GPIO_CS2: // CS2 (chip select) pin: Output level in arg_int
94-
return "U8X8_MSG_GPIO_CS2";
95-
case U8X8_MSG_GPIO_I2C_CLOCK: // arg_int=0: Output low at I2C clock pin
96-
return "U8X8_MSG_GPIO_I2C_CLOCK"; // arg_int=1: Input dir with pullup high for I2C clock pin
97-
case U8X8_MSG_GPIO_I2C_DATA: // arg_int=0: Output low at I2C data pin
98-
return "U8X8_MSG_GPIO_I2C_DATA"; // arg_int=1: Input dir with pullup high for I2C data pin
99-
case U8X8_MSG_GPIO_MENU_SELECT:
100-
return "U8X8_MSG_GPIO_MENU_SELECT";
101-
case U8X8_MSG_GPIO_MENU_NEXT:
102-
return "U8X8_MSG_GPIO_MENU_NEXT";
103-
case U8X8_MSG_GPIO_MENU_PREV:
104-
return "U8X8_MSG_GPIO_MENU_PREV";
105-
case U8X8_MSG_GPIO_MENU_HOME:
106-
return "U8X8_MSG_GPIO_MENU_HOME";
107-
case U8X8_MSG_BYTE_SEND:
108-
sprintf(buf, "U8X8_MSG_BYTE_SEND: length=%d %s", arg_int, bytesToString(arg_int, arg_ptr));
109-
return buf;
110-
case U8X8_MSG_BYTE_INIT:
111-
return "U8X8_MSG_BYTE_INIT";
112-
case U8X8_MSG_BYTE_END_TRANSFER:
113-
return "U8X8_MSG_BYTE_END_TRANSFER";
114-
case U8X8_MSG_BYTE_START_TRANSFER:
115-
return "U8X8_MSG_BYTE_START_TRANSFER";
116-
case U8X8_MSG_BYTE_SET_DC:
117-
sprintf(buf, "U8X8_MSG_BYTE_SET_DC -> %d", arg_int);
118-
return buf;
119-
default:
120-
return "Unknown";
121-
}
122-
}
123-
*/
124-
125-
12627
/*
12728
* HAL callback function as prescribed by the U8G2 library. This callback is invoked
12829
* to handle callbacks for communications.
@@ -152,24 +53,7 @@ uint8_t u8g2_esp32_msg_comms_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void
15253
//ESP_LOGI(tag, "... Initializing bus.");
15354
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &bus_config, 1));
15455

155-
156-
spi_device_interface_config_t dev_config;
157-
dev_config.address_bits = 0;
158-
dev_config.command_bits = 0;
159-
dev_config.dummy_bits = 0;
160-
dev_config.mode = 0;
161-
dev_config.duty_cycle_pos = 0;
162-
dev_config.cs_ena_posttrans = 0;
163-
dev_config.cs_ena_pretrans = 0;
164-
dev_config.clock_speed_hz = 10000;
165-
dev_config.spics_io_num = u8g2_esp32_hal.cs;
166-
dev_config.flags = 0;
167-
dev_config.queue_size = 200;
168-
dev_config.pre_cb = NULL;
169-
dev_config.post_cb = NULL;
170-
//ESP_LOGI(tag, "... Adding device bus.");
171-
ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &dev_config, &handle));
172-
break;
56+
break;
17357
}
17458

17559
case U8X8_MSG_BYTE_SEND: {
@@ -190,6 +74,92 @@ uint8_t u8g2_esp32_msg_comms_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void
19074
return 0;
19175
} // u8g2_esp32_msg_comms_cb
19276

77+
/*
78+
* HAL callback function as prescribed by the U8G2 library. This callback is invoked
79+
* to handle callbacks for communications.
80+
*/
81+
uint8_t u8g2_esp32_msg_i2c_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
82+
83+
ESP_LOGD(TAG, "msg_i2c_cb: Received a msg: %d %d", msg, arg_int);
84+
//ESP_LOGD(tag, "msg_i2c_cb: Received a msg: %d: %s", msg, msgToString(msg, arg_int, arg_ptr));
85+
86+
switch(msg) {
87+
case U8X8_MSG_BYTE_SET_DC:
88+
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) {
89+
gpio_set_level(u8g2_esp32_hal.dc, arg_int);
90+
}
91+
break;
92+
93+
case U8X8_MSG_BYTE_INIT: {
94+
if (u8g2_esp32_hal.sda == U8G2_ESP32_HAL_UNDEFINED ||
95+
u8g2_esp32_hal.scl == U8G2_ESP32_HAL_UNDEFINED) {
96+
break;
97+
}
98+
99+
i2c_config_t conf;
100+
conf.mode = I2C_MODE_MASTER;
101+
ESP_LOGI(TAG, "sda_io_num %d", u8g2_esp32_hal.sda);
102+
conf.sda_io_num = u8g2_esp32_hal.sda;
103+
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
104+
ESP_LOGI(TAG, "scl_io_num %d", u8g2_esp32_hal.scl);
105+
conf.scl_io_num = u8g2_esp32_hal.scl;
106+
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
107+
ESP_LOGI(TAG, "clk_speed %d", I2C_MASTER_FREQ_HZ);
108+
conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
109+
ESP_LOGI(TAG, "i2c_param_config %d", conf.mode);
110+
ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf));
111+
ESP_LOGI(TAG, "i2c_driver_install %d", I2C_MASTER_NUM);
112+
ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0));
113+
/*
114+
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); // dummy write
115+
ESP_ERROR_CHECK(i2c_master_start(cmd));
116+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, 0x00 | I2C_MASTER_WRITE, ACK_CHECK_DIS));
117+
ESP_ERROR_CHECK(i2c_master_stop(cmd));
118+
ESP_LOGI(TAG, "i2c_master_cmd_begin %d", I2C_MASTER_NUM);
119+
ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS));
120+
i2c_cmd_link_delete(cmd);
121+
*/
122+
123+
break;
124+
}
125+
126+
case U8X8_MSG_BYTE_SEND: {
127+
uint8_t *data;
128+
uint8_t cmddata;
129+
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
130+
ESP_ERROR_CHECK(i2c_master_start(cmd));
131+
// ESP_LOGI(TAG, "I2CAddress %02X", u8x8_GetI2CAddress(u8x8)>>1);
132+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, u8x8_GetI2CAddress(u8x8) | I2C_MASTER_WRITE, ACK_CHECK_EN));
133+
data = (uint8_t *)arg_ptr;
134+
if (arg_int==1) {
135+
cmddata=0;
136+
ESP_ERROR_CHECK(i2c_master_write(cmd, &cmddata, 1, ACK_CHECK_EN));
137+
// printf("0x%02X ",zerodata);
138+
} else {
139+
cmddata=0x40;
140+
ESP_ERROR_CHECK(i2c_master_write(cmd, &cmddata, 1, ACK_CHECK_EN));
141+
//bzero(arg_ptr,arg_int);
142+
//*data=0x40;
143+
}
144+
//ESP_ERROR_CHECK(i2c_master_write(cmd, arg_ptr, arg_int, ACK_CHECK_EN));
145+
146+
while( arg_int > 0 ) {
147+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, *data, ACK_CHECK_EN));
148+
// printf("0x%02X ",*data);
149+
data++;
150+
arg_int--;
151+
}
152+
// printf("\n");
153+
154+
ESP_ERROR_CHECK(i2c_master_stop(cmd));
155+
// ESP_LOGI(TAG, "i2c_master_cmd_begin %d", I2C_MASTER_NUM);
156+
ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS));
157+
i2c_cmd_link_delete(cmd);
158+
break;
159+
}
160+
}
161+
return 0;
162+
} // u8g2_esp32_msg_i2c_cb
193163

194164
/*
195165
* HAL callback function as prescribed by the U8G2 library. This callback is invoked
@@ -234,3 +204,79 @@ uint8_t u8g2_esp32_msg_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_
234204
}
235205
return 0;
236206
} // u8g2_esp32_msg_gpio_and_delay_cb
207+
208+
/*
209+
* HAL callback function as prescribed by the U8G2 library. This callback is invoked
210+
* to handle callbacks for I²C and delay functions.
211+
*/
212+
uint8_t u8g2_esp32_msg_i2c_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
213+
214+
ESP_LOGD(TAG, "msg_i2c_and_delay_cb: Received a msg: %d", msg);
215+
216+
switch(msg) {
217+
// Initialize the GPIO and DELAY HAL functions. If the pins for DC and RESET have been
218+
// specified then we define those pins as GPIO outputs.
219+
case U8X8_MSG_GPIO_AND_DELAY_INIT: {
220+
uint64_t bitmask = 0;
221+
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) {
222+
bitmask = bitmask | (1<<u8g2_esp32_hal.dc);
223+
}
224+
if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED) {
225+
bitmask = bitmask | (1<<u8g2_esp32_hal.reset);
226+
}
227+
if (u8g2_esp32_hal.cs != U8G2_ESP32_HAL_UNDEFINED) {
228+
bitmask = bitmask | (1<<u8g2_esp32_hal.cs);
229+
}
230+
if (u8g2_esp32_hal.sda != U8G2_ESP32_HAL_UNDEFINED) {
231+
//bitmask = bitmask | (1<<u8g2_esp32_hal.sda);
232+
}
233+
if (u8g2_esp32_hal.scl != U8G2_ESP32_HAL_UNDEFINED) {
234+
//bitmask = bitmask | (1<<u8g2_esp32_hal.scl);
235+
}
236+
if (bitmask==0) {
237+
break;
238+
}
239+
gpio_config_t gpioConfig;
240+
gpioConfig.pin_bit_mask = bitmask;
241+
gpioConfig.mode = GPIO_MODE_OUTPUT;
242+
gpioConfig.pull_up_en = GPIO_PULLUP_DISABLE;
243+
gpioConfig.pull_down_en = GPIO_PULLDOWN_ENABLE;
244+
gpioConfig.intr_type = GPIO_INTR_DISABLE;
245+
gpio_config(&gpioConfig);
246+
break;
247+
}
248+
249+
// Set the GPIO reset pin to the value passed in through arg_int.
250+
case U8X8_MSG_GPIO_RESET:
251+
if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED) {
252+
gpio_set_level(u8g2_esp32_hal.reset, arg_int);
253+
}
254+
break;
255+
// Set the GPIO client select pin to the value passed in through arg_int.
256+
case U8X8_MSG_GPIO_CS:
257+
if (u8g2_esp32_hal.cs != U8G2_ESP32_HAL_UNDEFINED) {
258+
gpio_set_level(u8g2_esp32_hal.cs, arg_int);
259+
}
260+
break;
261+
// Set the Software I²C pin to the value passed in through arg_int.
262+
case U8X8_MSG_GPIO_I2C_CLOCK:
263+
if (u8g2_esp32_hal.scl != U8G2_ESP32_HAL_UNDEFINED) {
264+
gpio_set_level(u8g2_esp32_hal.scl, arg_int);
265+
// printf("%c",(arg_int==1?'C':'c'));
266+
}
267+
break;
268+
// Set the Software I²C pin to the value passed in through arg_int.
269+
case U8X8_MSG_GPIO_I2C_DATA:
270+
if (u8g2_esp32_hal.sda != U8G2_ESP32_HAL_UNDEFINED) {
271+
gpio_set_level(u8g2_esp32_hal.sda, arg_int);
272+
// printf("%c",(arg_int==1?'D':'d'));
273+
}
274+
break;
275+
276+
// Delay for the number of milliseconds passed in through arg_int.
277+
case U8X8_MSG_DELAY_MILLI:
278+
vTaskDelay(arg_int/portTICK_PERIOD_MS);
279+
break;
280+
}
281+
return 0;
282+
} // u8g2_esp32_msg_gpio_and_delay_cb

0 commit comments

Comments
 (0)
0