upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/components/axs15231b/axs15231b.c
diff options
context:
space:
mode:
authorYour Name <you@example.com>2026-05-19 02:31:19 +0530
committerYour Name <you@example.com>2026-05-19 02:32:41 +0530
commit81f2dc52dc42d01c89dff45a5407ec40b8863052 (patch)
tree15018c2438639ca89dc6d33a5144c10d0b1c2af0 /components/axs15231b/axs15231b.c
parent75688d55b3c8d13c8c9a50da9668ec408f684cb3 (diff)
feat: local Nostr relay with relay selection, sync, and integration tests
Local Nostr relay (NIP-01) on port 4869 with LittleFS 4MB storage. All events published locally first, then synced to public relays via REQ-diff. Relay selection via NIP-11 HTTP probing with NIP-77 scoring and auto-failover. Components: - wisp_relay: 16-file local relay (ws_server, storage_engine, sub_manager, broadcaster, relay_validator, router, handlers, rate_limiter, nip11, deletion, flash_monitor, relay_types) - esp_littlefs: LittleFS VFS integration (git submodule) - negentropy: for future NIP-77 binary sync (git submodule) New source files: - local_relay.c/h: thin wrapper for relay init/start/publish - relay_selector.c/h: NIP-11 probe + scoring + auto-failover - sync_manager.c/h: REQ-diff sync (primary 30min, fallback 6h) Bug fixes: - config.c: use-after-free (cJSON_Delete before seed_relays/sync parsing) - local_relay: moved init to app_main for boot-time start (not gated on STA IP) Flash layout: 4MB LittleFS partition at 0x500000 for relay_store Test results (Board B, live hardware): - Smoke: ping + HTTP 4869 + NIP-11: PASS - NIP-11 info document: 10/11 PASS - WS pub/sub (connect, REQ/EOSE, EVENT/OK, CLOSE, concurrent): 6/6 PASS - Unit tests (relay_validator + relay_selector): 13/13 PASS Hardware test make targets in physical-router-test-automation/: - make relay-build, relay-flash-b, relay-test-smoke/nip11/pubsub/sync/full
Diffstat (limited to 'components/axs15231b/axs15231b.c')
-rw-r--r--components/axs15231b/axs15231b.c282
1 files changed, 282 insertions, 0 deletions
diff --git a/components/axs15231b/axs15231b.c b/components/axs15231b/axs15231b.c
new file mode 100644
index 0000000..dd7145a
--- /dev/null
+++ b/components/axs15231b/axs15231b.c
@@ -0,0 +1,282 @@
1#include "axs15231b.h"
2#include "driver/spi_master.h"
3#include "driver/gpio.h"
4#include "esp_log.h"
5#include "esp_timer.h"
6#include "freertos/FreeRTOS.h"
7#include "freertos/task.h"
8#include <string.h>
9#include <stdlib.h>
10#include <esp_heap_caps.h>
11
12static const char *TAG = "axs15231b";
13
14#define SWRESET 0x01
15#define SLPIN 0x10
16#define SLPOUT 0x11
17#define INVOFF 0x20
18#define INVON 0x21
19#define DISPOFF 0x28
20#define DISPON 0x29
21#define CASET 0x2A
22#define RASET 0x2B
23#define RAMWR 0x2C
24#define COLMOD 0x3A
25#define MADCTL 0x36
26
27#define MADCTL_MY 0x80
28#define MADCTL_MX 0x40
29#define MADCTL_MV 0x20
30#define MADCTL_RGB 0x00
31
32static spi_device_handle_t s_spi = NULL;
33static uint16_t *s_fb = NULL;
34static int s_width = AXS15231B_WIDTH;
35static int s_height = AXS15231B_HEIGHT;
36
37typedef struct {
38 uint8_t cmd;
39 uint8_t data_len;
40 const uint8_t *data;
41 uint16_t delay_ms;
42} init_cmd_t;
43
44static esp_err_t send_cmd(uint8_t cmd) {
45 spi_transaction_t t = {0};
46 t.length = 8;
47 t.tx_data[0] = cmd;
48 t.flags = SPI_TRANS_USE_TXDATA;
49 return spi_device_polling_transmit(s_spi, &t);
50}
51
52static esp_err_t send_data(const uint8_t *data, int len) {
53 if (len == 0) return ESP_OK;
54 spi_transaction_t t = {0};
55 t.length = len * 8;
56 t.tx_buffer = data;
57 t.flags = 0;
58 return spi_device_polling_transmit(s_spi, &t);
59}
60
61static esp_err_t send_cmd_data(uint8_t cmd, const uint8_t *data, int len) {
62 esp_err_t ret = send_cmd(cmd);
63 if (ret != ESP_OK) return ret;
64 if (len > 0) ret = send_data(data, len);
65 return ret;
66}
67
68static const uint8_t init_bb[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x5A,0xA5};
69static const uint8_t init_a0[] = {0xC0,0x10,0x00,0x02,0x00,0x00,0x04,0x3F,0x20,0x05,0x3F,0x3F,0x00,0x00,0x00,0x00,0x00};
70static const uint8_t init_a2[] = {0x30,0x3C,0x24,0x14,0xD0,0x20,0xFF,0xE0,0x40,0x19,0x80,0x80,0x80,0x20,0xF9,0x10,0x02,0xFF,0xFF,0xF0,0x90,0x01,0x32,0xA0,0x91,0xE0,0x20,0x7F,0xFF,0x00,0x5A};
71static const uint8_t init_d0[] = {0xE0,0x40,0x51,0x24,0x08,0x05,0x10,0x01,0x20,0x15,0xC2,0x42,0x22,0x22,0xAA,0x03,0x10,0x12,0x60,0x14,0x1E,0x51,0x15,0x00,0x8A,0x20,0x00,0x03,0x3A,0x12};
72static const uint8_t init_a3[] = {0xA0,0x06,0xAA,0x00,0x08,0x02,0x0A,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,0x55,0x55};
73static const uint8_t init_c1[] = {0x31,0x04,0x02,0x02,0x71,0x05,0x24,0x55,0x02,0x00,0x41,0x00,0x53,0xFF,0xFF,0xFF,0x4F,0x52,0x00,0x4F,0x52,0x00,0x45,0x3B,0x0B,0x02,0x0D,0x00,0xFF,0x40};
74static const uint8_t init_c3[] = {0x00,0x00,0x00,0x50,0x03,0x00,0x00,0x00,0x01,0x80,0x01};
75static const uint8_t init_c4[] = {0x00,0x24,0x33,0x80,0x00,0xEA,0x64,0x32,0xC8,0x64,0xC8,0x32,0x90,0x90,0x11,0x06,0xDC,0xFA,0x00,0x00,0x80,0xFE,0x10,0x10,0x00,0x0A,0x0A,0x44,0x50};
76static const uint8_t init_c5[] = {0x18,0x00,0x00,0x03,0xFE,0x3A,0x4A,0x20,0x30,0x10,0x88,0xDE,0x0D,0x08,0x0F,0x0F,0x01,0x3A,0x4A,0x20,0x10,0x10,0x00};
77static const uint8_t init_c6[] = {0x05,0x0A,0x05,0x0A,0x00,0xE0,0x2E,0x0B,0x12,0x22,0x12,0x22,0x01,0x03,0x00,0x3F,0x6A,0x18,0xC8,0x22};
78static const uint8_t init_c7[] = {0x50,0x32,0x28,0x00,0xA2,0x80,0x8F,0x00,0x80,0xFF,0x07,0x11,0x9C,0x67,0xFF,0x24,0x0C,0x0D,0x0E,0x0F};
79static const uint8_t init_c9[] = {0x33,0x44,0x44,0x01};
80static const uint8_t init_cf[] = {0x2C,0x1E,0x88,0x58,0x13,0x18,0x56,0x18,0x1E,0x68,0x88,0x00,0x65,0x09,0x22,0xC4,0x0C,0x77,0x22,0x44,0xAA,0x55,0x08,0x08,0x12,0xA0,0x08};
81static const uint8_t init_d5[] = {0x40,0x8E,0x8D,0x01,0x35,0x04,0x92,0x74,0x04,0x92,0x74,0x04,0x08,0x6A,0x04,0x46,0x03,0x03,0x03,0x03,0x82,0x01,0x03,0x00,0xE0,0x51,0xA1,0x00,0x00,0x00};
82static const uint8_t init_d6[] = {0x10,0x32,0x54,0x76,0x98,0xBA,0xDC,0xFE,0x93,0x00,0x01,0x83,0x07,0x07,0x00,0x07,0x07,0x00,0x03,0x03,0x03,0x03,0x03,0x03,0x00,0x84,0x00,0x20,0x01,0x00};
83static const uint8_t init_d7[] = {0x03,0x01,0x0B,0x09,0x0F,0x0D,0x1E,0x1F,0x18,0x1D,0x1F,0x19,0x40,0x8E,0x04,0x00,0x20,0xA0,0x1F};
84static const uint8_t init_d8[] = {0x02,0x00,0x0A,0x08,0x0E,0x0C,0x1E,0x1F,0x18,0x1D,0x1F,0x19};
85static const uint8_t init_d9[] = {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F};
86static const uint8_t init_dd[] = {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F};
87static const uint8_t init_df[] = {0x44,0x73,0x4B,0x69,0x00,0x0A,0x02,0x90};
88static const uint8_t init_e0[] = {0x3B,0x28,0x10,0x16,0x0C,0x06,0x11,0x28,0x5C,0x21,0x0D,0x35,0x13,0x2C,0x33,0x28,0x0D};
89static const uint8_t init_e1[] = {0x37,0x28,0x10,0x16,0x0B,0x06,0x11,0x28,0x5C,0x21,0x0D,0x35,0x14,0x2C,0x33,0x28,0x0F};
90static const uint8_t init_e2[] = {0x3B,0x07,0x12,0x18,0x0E,0x0D,0x17,0x35,0x44,0x32,0x0C,0x14,0x14,0x36,0x3A,0x2F,0x0D};
91static const uint8_t init_e3[] = {0x37,0x07,0x12,0x18,0x0E,0x0D,0x17,0x35,0x44,0x32,0x0C,0x14,0x14,0x36,0x32,0x2F,0x0F};
92static const uint8_t init_e4[] = {0x3B,0x07,0x12,0x18,0x0E,0x0D,0x17,0x39,0x44,0x2E,0x0C,0x14,0x14,0x36,0x3A,0x2F,0x0D};
93static const uint8_t init_e5[] = {0x37,0x07,0x12,0x18,0x0E,0x0D,0x17,0x39,0x44,0x2E,0x0C,0x14,0x14,0x36,0x3A,0x2F,0x0F};
94static const uint8_t init_a4_1[] = {0x85,0x85,0x95,0x82,0xAF,0xAA,0xAA,0x80,0x10,0x30,0x40,0x40,0x20,0xFF,0x60,0x30};
95static const uint8_t init_a4_2[] = {0x85,0x85,0x95,0x85};
96static const uint8_t init_bb2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
97
98static const init_cmd_t s_init_cmds[] = {
99 {0xBB, sizeof(init_bb), init_bb, 0},
100 {0xA0, sizeof(init_a0), init_a0, 0},
101 {0xA2, sizeof(init_a2), init_a2, 0},
102 {0xD0, sizeof(init_d0), init_d0, 0},
103 {0xA3, sizeof(init_a3), init_a3, 0},
104 {0xC1, sizeof(init_c1), init_c1, 0},
105 {0xC3, sizeof(init_c3), init_c3, 0},
106 {0xC4, sizeof(init_c4), init_c4, 0},
107 {0xC5, sizeof(init_c5), init_c5, 0},
108 {0xC6, sizeof(init_c6), init_c6, 0},
109 {0xC7, sizeof(init_c7), init_c7, 0},
110 {0xC9, sizeof(init_c9), init_c9, 0},
111 {0xCF, sizeof(init_cf), init_cf, 0},
112 {0xD5, sizeof(init_d5), init_d5, 0},
113 {0xD6, sizeof(init_d6), init_d6, 0},
114 {0xD7, sizeof(init_d7), init_d7, 0},
115 {0xD8, sizeof(init_d8), init_d8, 0},
116 {0xD9, sizeof(init_d9), init_d9, 0},
117 {0xDD, sizeof(init_dd), init_dd, 0},
118 {0xDF, sizeof(init_df), init_df, 0},
119 {0xE0, sizeof(init_e0), init_e0, 0},
120 {0xE1, sizeof(init_e1), init_e1, 0},
121 {0xE2, sizeof(init_e2), init_e2, 0},
122 {0xE3, sizeof(init_e3), init_e3, 0},
123 {0xE4, sizeof(init_e4), init_e4, 0},
124 {0xE5, sizeof(init_e5), init_e5, 0},
125 {0xA4, sizeof(init_a4_1), init_a4_1, 0},
126 {0xA4, sizeof(init_a4_2), init_a4_2, 0},
127 {0xBB, sizeof(init_bb2), init_bb2, 0},
128 {SLPOUT, 0, NULL, 200},
129 {DISPON, 0, NULL, 100},
130};
131#define INIT_CMD_COUNT (sizeof(s_init_cmds) / sizeof(s_init_cmds[0]))
132
133esp_err_t axs15231b_init(void) {
134 ESP_LOGI(TAG, "Initializing AXS15231B display...");
135
136 esp_err_t ret;
137
138 spi_bus_config_t buscfg = {
139 .mosi_io_num = AXS15231B_PIN_D0,
140 .sclk_io_num = AXS15231B_PIN_CLK,
141 .miso_io_num = -1,
142 .quadwp_io_num = -1,
143 .quadhd_io_num = -1,
144 .max_transfer_sz = 32768,
145 };
146
147 spi_device_interface_config_t devcfg = {
148 .clock_speed_hz = 40 * 1000 * 1000,
149 .mode = 0,
150 .spics_io_num = AXS15231B_PIN_CS,
151 .queue_size = 7,
152 .flags = 0,
153 };
154
155 ret = spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO);
156 if (ret != ESP_OK) {
157 ESP_LOGE(TAG, "Failed to init SPI bus: %s", esp_err_to_name(ret));
158 return ret;
159 }
160
161 ret = spi_bus_add_device(SPI2_HOST, &devcfg, &s_spi);
162 if (ret != ESP_OK) {
163 ESP_LOGE(TAG, "Failed to add SPI device: %s", esp_err_to_name(ret));
164 return ret;
165 }
166
167 size_t fb_size = (size_t)s_width * s_height * 2;
168 s_fb = heap_caps_malloc(fb_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
169 if (!s_fb) {
170 ESP_LOGE(TAG, "Failed to allocate framebuffer (%zu bytes)", fb_size);
171 return ESP_ERR_NO_MEM;
172 }
173 memset(s_fb, 0, fb_size);
174 ESP_LOGI(TAG, "Framebuffer allocated: %zu bytes in PSRAM", fb_size);
175
176 gpio_config_t bl_cfg = {
177 .pin_bit_mask = (1ULL << AXS15231B_PIN_BL),
178 .mode = GPIO_MODE_OUTPUT,
179 .pull_up_en = GPIO_PULLUP_DISABLE,
180 .pull_down_en = GPIO_PULLDOWN_DISABLE,
181 .intr_type = GPIO_INTR_DISABLE,
182 };
183 gpio_config(&bl_cfg);
184
185 send_cmd(SWRESET);
186 vTaskDelay(pdMS_TO_TICKS(200));
187
188 for (int i = 0; i < INIT_CMD_COUNT; i++) {
189 ret = send_cmd_data(s_init_cmds[i].cmd, s_init_cmds[i].data, s_init_cmds[i].data_len);
190 if (ret != ESP_OK) {
191 ESP_LOGE(TAG, "Init cmd 0x%02X failed: %s", s_init_cmds[i].cmd, esp_err_to_name(ret));
192 return ret;
193 }
194 if (s_init_cmds[i].delay_ms > 0) {
195 vTaskDelay(pdMS_TO_TICKS(s_init_cmds[i].delay_ms));
196 }
197 }
198
199 uint8_t madctl_val = MADCTL_MX | MADCTL_MV | MADCTL_RGB;
200 ret = send_cmd_data(MADCTL, &madctl_val, 1);
201 if (ret != ESP_OK) {
202 ESP_LOGE(TAG, "Failed to set rotation: %s", esp_err_to_name(ret));
203 return ret;
204 }
205
206 uint8_t colmod_val = 0x55;
207 ret = send_cmd_data(COLMOD, &colmod_val, 1);
208 if (ret != ESP_OK) {
209 ESP_LOGE(TAG, "Failed to set pixel format: %s", esp_err_to_name(ret));
210 return ret;
211 }
212
213 axs15231b_fill_screen(0x0000);
214 axs15231b_flush();
215
216 axs15231b_set_backlight(true);
217
218 ESP_LOGI(TAG, "AXS15231B initialized: %dx%d landscape", s_width, s_height);
219 return ESP_OK;
220}
221
222void axs15231b_set_backlight(bool on) {
223 gpio_set_level(AXS15231B_PIN_BL, on ? 1 : 0);
224}
225
226void axs15231b_fill_screen(uint16_t color) {
227 uint32_t pixels = (uint32_t)s_width * s_height;
228 for (uint32_t i = 0; i < pixels; i++) {
229 s_fb[i] = color;
230 }
231}
232
233void axs15231b_fill_rect(int x, int y, int w, int h, uint16_t color) {
234 if (x < 0 || y < 0 || x + w > s_width || y + h > s_height) return;
235 for (int row = y; row < y + h; row++) {
236 for (int col = x; col < x + w; col++) {
237 s_fb[row * s_width + col] = color;
238 }
239 }
240}
241
242void axs15231b_flush(void) {
243 if (!s_spi || !s_fb) return;
244
245 uint8_t buf[4];
246 buf[0] = 0;
247 buf[1] = 0;
248 buf[2] = (s_width - 1) >> 8;
249 buf[3] = (s_width - 1) & 0xFF;
250 send_cmd_data(CASET, buf, 4);
251
252 buf[0] = 0;
253 buf[1] = 0;
254 buf[2] = (s_height - 1) >> 8;
255 buf[3] = (s_height - 1) & 0xFF;
256 send_cmd_data(RASET, buf, 4);
257
258 send_cmd(RAMWR);
259
260 int total_bytes = s_width * s_height * 2;
261 int chunk_size = 32768;
262 int offset = 0;
263 uint8_t *fb_bytes = (uint8_t *)s_fb;
264
265 while (offset < total_bytes) {
266 int remaining = total_bytes - offset;
267 int this_chunk = remaining < chunk_size ? remaining : chunk_size;
268
269 spi_transaction_t t = {0};
270 t.length = this_chunk * 8;
271 t.tx_buffer = fb_bytes + offset;
272 esp_err_t ret = spi_device_polling_transmit(s_spi, &t);
273 if (ret != ESP_OK) {
274 ESP_LOGE(TAG, "Flush transfer failed at offset %d: %s", offset, esp_err_to_name(ret));
275 return;
276 }
277 offset += this_chunk;
278 }
279}
280
281int axs15231b_get_width(void) { return s_width; }
282int axs15231b_get_height(void) { return s_height; }