1010using namespace box_hal ;
1111
1212static spi_device_handle_t spi;
13+ static spi_device_interface_config_t devcfg;
1314
1415static constexpr size_t pixel_buffer_size = display_width*NUM_ROWS_IN_FRAME_BUFFER;
1516std::shared_ptr<espp::Display> display;
@@ -27,7 +28,7 @@ static constexpr int DC_LEVEL_BIT = (1 << (int)espp::display_drivers::Flags::DC_
2728// This function is called (in irq context!) just before a transmission starts.
2829// It will set the D/C line to the value indicated in the user field
2930// (DC_LEVEL_BIT).
30- static void lcd_spi_pre_transfer_callback (spi_transaction_t *t)
31+ static void IRAM_ATTR lcd_spi_pre_transfer_callback (spi_transaction_t *t)
3132{
3233 uint32_t user_flags = (uint32_t )(t->user );
3334 bool dc_level = user_flags & DC_LEVEL_BIT;
@@ -37,7 +38,7 @@ static void lcd_spi_pre_transfer_callback(spi_transaction_t *t)
3738// This function is called (in irq context!) just after a transmission ends. It
3839// will indicate to lvgl that the next flush is ready to be done if the
3940// FLUSH_BIT is set.
40- static void lcd_spi_post_transfer_callback (spi_transaction_t *t)
41+ static void IRAM_ATTR lcd_spi_post_transfer_callback (spi_transaction_t *t)
4142{
4243 uint16_t user_flags = (uint32_t )(t->user );
4344 bool should_flush = user_flags & FLUSH_BIT;
@@ -46,20 +47,6 @@ static void lcd_spi_post_transfer_callback(spi_transaction_t *t)
4647 lv_disp_flush_ready (disp->driver );
4748 }
4849}
49-
50- extern " C" void lcd_write (const uint8_t *data, size_t length, uint32_t user_data) {
51- if (length == 0 ) {
52- return ;
53- }
54- esp_err_t ret;
55- static spi_transaction_t t;
56- memset (&t, 0 , sizeof (t));
57- t.length = length * 8 ;
58- t.tx_buffer = data;
59- t.user = (void *)user_data;
60- ret=spi_device_polling_transmit (spi, &t);
61- }
62-
6350// Transaction descriptors. Declared static so they're not allocated on the
6451// stack; we need this memory even when this function is finished because the
6552// SPI driver needs access to it even while we're already calculating the next
@@ -74,7 +61,7 @@ static void lcd_wait_lines() {
7461 // fmt::print("Waiting for {} queued transactions\n", num_queued_trans);
7562 // Wait for all transactions to be done and get back the results.
7663 while (num_queued_trans) {
77- ret= spi_device_get_trans_result (spi, &rtrans, 10 / portTICK_PERIOD_MS);
64+ ret = spi_device_get_trans_result (spi, &rtrans, 10 / portTICK_PERIOD_MS);
7865 if (ret != ESP_OK) {
7966 fmt::print (" Could not get trans result: {} '{}'\n " , ret, esp_err_to_name (ret));
8067 }
@@ -83,6 +70,27 @@ static void lcd_wait_lines() {
8370 }
8471}
8572
73+
74+ extern " C" void lcd_write (const uint8_t *data, size_t length, uint32_t user_data) {
75+ if (length == 0 ) {
76+ return ;
77+ }
78+ lcd_wait_lines ();
79+ esp_err_t ret;
80+ trans[0 ].length = length * 8 ;
81+ trans[0 ].user = (void *)user_data;
82+ trans[0 ].tx_buffer = data;
83+ trans[0 ].flags = 0 ; // maybe look at the length of data (<=32 bits) and see
84+ // if we should use SPI_TRANS_USE_TXDATA and copy the
85+ // data into the tx_data field
86+ ret = spi_device_queue_trans (spi, &trans[0 ], 10 / portTICK_PERIOD_MS);
87+ if (ret != ESP_OK) {
88+ fmt::print (" Couldn't queue trans: {} '{}'\n " , ret, esp_err_to_name (ret));
89+ } else {
90+ num_queued_trans++;
91+ }
92+ }
93+
8694extern " C" void lcd_send_lines (int xs, int ys, int xe, int ye, const uint8_t *data, uint32_t user_data) {
8795 // if we haven't waited by now, wait here...
8896 lcd_wait_lines ();
@@ -119,7 +127,7 @@ extern "C" void lcd_send_lines(int xs, int ys, int xe, int ye, const uint8_t *da
119127 trans[5 ].tx_buffer = data;
120128 trans[5 ].length = length*8 ;
121129 // undo SPI_TRANS_USE_TXDATA flag
122- trans[5 ].flags = 0 ; // SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL;
130+ trans[5 ].flags = SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL;
123131 // we need to keep the dc bit set, but also add our flags
124132 trans[5 ].user = (void *)(DC_LEVEL_BIT | user_data);
125133 // Queue all transactions.
@@ -188,9 +196,8 @@ extern "C" void lcd_init() {
188196 buscfg.sclk_io_num = lcd_sclk;
189197 buscfg.quadwp_io_num = -1 ;
190198 buscfg.quadhd_io_num = -1 ;
191- buscfg.max_transfer_sz = display_width * display_height * sizeof (lv_color_t ) + 8 ;
199+ buscfg.max_transfer_sz = pixel_buffer_size * sizeof (lv_color_t ) + 10 ;
192200
193- static spi_device_interface_config_t devcfg;
194201 memset (&devcfg, 0 , sizeof (devcfg));
195202 devcfg.mode = 0 ;
196203 // devcfg.flags = SPI_DEVICE_NO_RETURN_RESULT;
0 commit comments