/* Host unit test for the lk2nd SPI panel init byte-stream walker. */ #include #include #include "spi-panel-init.h" #include "panel_streams.h" static unsigned char cmds[64]; static unsigned char data[64][16]; static unsigned int data_len[64]; static unsigned int cmd_count, delay_total, delay_count; static int fail_write; static int rec_cmd(const unsigned char *cmd) { if(fail_write) return -5; if(cmd_count < 64) cmds[cmd_count] = cmd[0]; cmd_count++; return 0; } static int rec_data(const unsigned char *buf, unsigned int len) { if(fail_write) return -5; if(cmd_count > 0 && cmd_count <= 64 && len <= 16) { memcpy(data[cmd_count - 1], buf, len); data_len[cmd_count - 1] = len; } return 0; } static void rec_delay(unsigned int ms) { delay_total += ms; delay_count++; } static const struct lk2nd_spi_panel_init_ops ops = { .write_cmd = rec_cmd, .write_data = rec_data, .delay = rec_delay, }; static void reset(void) { cmd_count = delay_total = delay_count = 0; fail_write = 0; memset(cmds, 0, sizeof(cmds)); memset(data_len, 0, sizeof(data_len)); } #define CHECK(cond, msg) do { \ if(!(cond)) { \ printf("[fail] %s (line %d)\n", (msg), __LINE__); \ return 1; \ } \ } while(0) static int has_cmd(const unsigned char *cmd, unsigned int len) { unsigned int i; for(i = 0; i < cmd_count; i++) { if(cmds[i] != cmd[0]) continue; if(len == 1) return 1; if(data_len[i] == len - 1 && memcmp(data[i], cmd + 1, len - 1) == 0) return 1; } return 0; } int main(void) { static const unsigned char rgb565[] = {0x3a, 0x05}; static const unsigned char window_x[] = {0x2a, 0x00, 0x00, 0x00, 0xef}; static const unsigned char window_y[] = {0x2b, 0x00, 0x00, 0x01, 0x3f}; static const unsigned char zero_dlen[] = {0x00, 0x00}; static const unsigned char trailing[] = {0x00, 0x01, 0x29, 0x00}; static const unsigned char truncated[] = {0x00, 0x05, 0x2a, 0x00}; reset(); CHECK(lk2nd_spi_panel_init_run(stream_v2, sizeof(stream_v2), &ops) == 0, "v2 stream rejected"); CHECK(cmd_count == 10, "v2 stream must have 10 commands"); CHECK(delay_total == 120 && delay_count == 1, "v2 stream must wait 120 ms once after sleep-out"); CHECK(cmds[0] == 0xfe && cmds[1] == 0xef, "v2 stream must start with the GC9305 page select"); CHECK(has_cmd(rgb565, sizeof(rgb565)), "missing RGB565 COLMOD"); CHECK(has_cmd(window_x, sizeof(window_x)), "missing 240 column window"); CHECK(has_cmd(window_y, sizeof(window_y)), "missing 320 row window"); CHECK(has_cmd((const unsigned char *)"\x11", 1), "missing sleep-out"); CHECK(has_cmd((const unsigned char *)"\x29", 1), "missing display-on"); CHECK(cmds[cmd_count - 1] == 0x2c, "stream must end with RAMWR (0x2c)"); reset(); CHECK(lk2nd_spi_panel_init_run(zero_dlen, sizeof(zero_dlen), &ops) < 0, "zero dlen accepted"); reset(); CHECK(lk2nd_spi_panel_init_run(trailing, sizeof(trailing), &ops) < 0, "trailing garbage accepted"); reset(); CHECK(lk2nd_spi_panel_init_run(truncated, sizeof(truncated), &ops) < 0, "truncated payload accepted"); reset(); fail_write = 1; CHECK(lk2nd_spi_panel_init_run(stream_v2, sizeof(stream_v2), &ops) == -5, "write error not propagated"); printf("[ok] SPI panel init walker: GC9305 v2 stream, framing, RGB565, " "240x320 window, delays and error handling\n"); return 0; }