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