Skip to content

Commit 3a117c5

Browse files
committed
feat(hal): update lcd functions
* Make transfer callbacks IRAM_ATTR * Make lcd_write function use queued transactions instead of polling transactions so that all lcd comms is using the same mechanisms * Change the max transfer size to be more representative of what is actually being sent
1 parent 02f6ad7 commit 3a117c5

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

components/box-emu-hal/src/spi_lcd.cpp

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using namespace box_hal;
1111

1212
static spi_device_handle_t spi;
13+
static spi_device_interface_config_t devcfg;
1314

1415
static constexpr size_t pixel_buffer_size = display_width*NUM_ROWS_IN_FRAME_BUFFER;
1516
std::shared_ptr<espp::Display> display;
@@ -24,12 +25,10 @@ static uint8_t *frame_buffer1;
2425
static constexpr int FLUSH_BIT = (1 << (int)espp::display_drivers::Flags::FLUSH_BIT);
2526
static constexpr int DC_LEVEL_BIT = (1 << (int)espp::display_drivers::Flags::DC_LEVEL_BIT);
2627

27-
static void lcd_wait_lines();
28-
2928
// This function is called (in irq context!) just before a transmission starts.
3029
// It will set the D/C line to the value indicated in the user field
3130
// (DC_LEVEL_BIT).
32-
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)
3332
{
3433
uint32_t user_flags = (uint32_t)(t->user);
3534
bool dc_level = user_flags & DC_LEVEL_BIT;
@@ -39,7 +38,7 @@ static void lcd_spi_pre_transfer_callback(spi_transaction_t *t)
3938
// This function is called (in irq context!) just after a transmission ends. It
4039
// will indicate to lvgl that the next flush is ready to be done if the
4140
// FLUSH_BIT is set.
42-
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)
4342
{
4443
uint16_t user_flags = (uint32_t)(t->user);
4544
bool should_flush = user_flags & FLUSH_BIT;
@@ -48,24 +47,6 @@ static void lcd_spi_post_transfer_callback(spi_transaction_t *t)
4847
lv_disp_flush_ready(disp->driver);
4948
}
5049
}
51-
52-
extern "C" void lcd_write(const uint8_t *data, size_t length, uint32_t user_data) {
53-
if (length == 0) {
54-
return;
55-
}
56-
lcd_wait_lines();
57-
esp_err_t ret;
58-
static spi_transaction_t t;
59-
memset(&t, 0, sizeof(t));
60-
t.length = length * 8;
61-
t.tx_buffer = data;
62-
t.user = (void*)user_data;
63-
ret = spi_device_polling_transmit(spi, &t);
64-
if (ret != ESP_OK) {
65-
fmt::print("Could not transmit: {} '{}'\n", ret, esp_err_to_name(ret));
66-
}
67-
}
68-
6950
// Transaction descriptors. Declared static so they're not allocated on the
7051
// stack; we need this memory even when this function is finished because the
7152
// SPI driver needs access to it even while we're already calculating the next
@@ -80,7 +61,7 @@ static void lcd_wait_lines() {
8061
// fmt::print("Waiting for {} queued transactions\n", num_queued_trans);
8162
// Wait for all transactions to be done and get back the results.
8263
while (num_queued_trans) {
83-
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);
8465
if (ret != ESP_OK) {
8566
fmt::print("Could not get trans result: {} '{}'\n", ret, esp_err_to_name(ret));
8667
}
@@ -89,6 +70,27 @@ static void lcd_wait_lines() {
8970
}
9071
}
9172

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+
9294
extern "C" void lcd_send_lines(int xs, int ys, int xe, int ye, const uint8_t *data, uint32_t user_data) {
9395
// if we haven't waited by now, wait here...
9496
lcd_wait_lines();
@@ -125,7 +127,7 @@ extern "C" void lcd_send_lines(int xs, int ys, int xe, int ye, const uint8_t *da
125127
trans[5].tx_buffer = data;
126128
trans[5].length = length*8;
127129
// undo SPI_TRANS_USE_TXDATA flag
128-
trans[5].flags = 0; // SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL;
130+
trans[5].flags = SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL;
129131
// we need to keep the dc bit set, but also add our flags
130132
trans[5].user = (void*)(DC_LEVEL_BIT | user_data);
131133
//Queue all transactions.
@@ -194,9 +196,8 @@ extern "C" void lcd_init() {
194196
buscfg.sclk_io_num = lcd_sclk;
195197
buscfg.quadwp_io_num = -1;
196198
buscfg.quadhd_io_num = -1;
197-
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;
198200

199-
static spi_device_interface_config_t devcfg;
200201
memset(&devcfg, 0, sizeof(devcfg));
201202
devcfg.mode = 0;
202203
// devcfg.flags = SPI_DEVICE_NO_RETURN_RESULT;

0 commit comments

Comments
 (0)