esp32s3_korvo2_v3_board.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "wifi_board.h"
  2. #include "codecs/box_audio_codec.h"
  3. #include "display/lcd_display.h"
  4. #include "application.h"
  5. #include "button.h"
  6. #include "config.h"
  7. #include "i2c_device.h"
  8. #include <esp_log.h>
  9. #include <esp_lcd_panel_vendor.h>
  10. #include <esp_io_expander_tca9554.h>
  11. #include <esp_lcd_ili9341.h>
  12. #include <driver/i2c_master.h>
  13. #include <driver/spi_common.h>
  14. #include <wifi_station.h>
  15. #include "esp32_camera.h"
  16. #define TAG "esp32s3_korvo2_v3"
  17. LV_FONT_DECLARE(font_puhui_20_4);
  18. LV_FONT_DECLARE(font_awesome_20_4);
  19. // Init ili9341 by custom cmd
  20. static const ili9341_lcd_init_cmd_t vendor_specific_init[] = {
  21. {0xC8, (uint8_t []){0xFF, 0x93, 0x42}, 3, 0},
  22. {0xC0, (uint8_t []){0x0E, 0x0E}, 2, 0},
  23. {0xC5, (uint8_t []){0xD0}, 1, 0},
  24. {0xC1, (uint8_t []){0x02}, 1, 0},
  25. {0xB4, (uint8_t []){0x02}, 1, 0},
  26. {0xE0, (uint8_t []){0x00, 0x03, 0x08, 0x06, 0x13, 0x09, 0x39, 0x39, 0x48, 0x02, 0x0a, 0x08, 0x17, 0x17, 0x0F}, 15, 0},
  27. {0xE1, (uint8_t []){0x00, 0x28, 0x29, 0x01, 0x0d, 0x03, 0x3f, 0x33, 0x52, 0x04, 0x0f, 0x0e, 0x37, 0x38, 0x0F}, 15, 0},
  28. {0xB1, (uint8_t []){00, 0x1B}, 2, 0},
  29. {0x36, (uint8_t []){0x08}, 1, 0},
  30. {0x3A, (uint8_t []){0x55}, 1, 0},
  31. {0xB7, (uint8_t []){0x06}, 1, 0},
  32. {0x11, (uint8_t []){0}, 0x80, 0},
  33. {0x29, (uint8_t []){0}, 0x80, 0},
  34. {0, (uint8_t []){0}, 0xff, 0},
  35. };
  36. class Esp32S3Korvo2V3Board : public WifiBoard {
  37. private:
  38. Button boot_button_;
  39. i2c_master_bus_handle_t i2c_bus_;
  40. LcdDisplay* display_;
  41. esp_io_expander_handle_t io_expander_ = NULL;
  42. Esp32Camera* camera_;
  43. void InitializeI2c() {
  44. // Initialize I2C peripheral
  45. i2c_master_bus_config_t i2c_bus_cfg = {
  46. .i2c_port = (i2c_port_t)1,
  47. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  48. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  49. .clk_source = I2C_CLK_SRC_DEFAULT,
  50. .glitch_ignore_cnt = 7,
  51. .intr_priority = 0,
  52. .trans_queue_depth = 0,
  53. .flags = {
  54. .enable_internal_pullup = 1,
  55. },
  56. };
  57. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  58. }
  59. void I2cDetect() {
  60. uint8_t address;
  61. printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
  62. for (int i = 0; i < 128; i += 16) {
  63. printf("%02x: ", i);
  64. for (int j = 0; j < 16; j++) {
  65. fflush(stdout);
  66. address = i + j;
  67. esp_err_t ret = i2c_master_probe(i2c_bus_, address, pdMS_TO_TICKS(200));
  68. if (ret == ESP_OK) {
  69. printf("%02x ", address);
  70. } else if (ret == ESP_ERR_TIMEOUT) {
  71. printf("UU ");
  72. } else {
  73. printf("-- ");
  74. }
  75. }
  76. printf("\r\n");
  77. }
  78. }
  79. void InitializeTca9554() {
  80. esp_err_t ret = esp_io_expander_new_i2c_tca9554(i2c_bus_, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000, &io_expander_);
  81. if(ret != ESP_OK) {
  82. ret = esp_io_expander_new_i2c_tca9554(i2c_bus_, ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_000, &io_expander_);
  83. if(ret != ESP_OK) {
  84. ESP_LOGE(TAG, "TCA9554 create returned error");
  85. return;
  86. }
  87. }
  88. // 配置IO0-IO3为输出模式
  89. ESP_ERROR_CHECK(esp_io_expander_set_dir(io_expander_,
  90. IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 |
  91. IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3,
  92. IO_EXPANDER_OUTPUT));
  93. // 复位LCD和TouchPad
  94. ESP_ERROR_CHECK(esp_io_expander_set_level(io_expander_,
  95. IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 | IO_EXPANDER_PIN_NUM_2, 1));
  96. vTaskDelay(pdMS_TO_TICKS(300));
  97. ESP_ERROR_CHECK(esp_io_expander_set_level(io_expander_,
  98. IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 | IO_EXPANDER_PIN_NUM_2, 0));
  99. vTaskDelay(pdMS_TO_TICKS(300));
  100. ESP_ERROR_CHECK(esp_io_expander_set_level(io_expander_,
  101. IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 | IO_EXPANDER_PIN_NUM_2, 1));
  102. }
  103. void EnableLcdCs() {
  104. if(io_expander_ != NULL) {
  105. esp_io_expander_set_level(io_expander_, IO_EXPANDER_PIN_NUM_3, 0);// 置低 LCD CS
  106. }
  107. }
  108. void InitializeSpi() {
  109. spi_bus_config_t buscfg = {};
  110. buscfg.mosi_io_num = GPIO_NUM_0;
  111. buscfg.miso_io_num = GPIO_NUM_NC;
  112. buscfg.sclk_io_num = GPIO_NUM_1;
  113. buscfg.quadwp_io_num = GPIO_NUM_NC;
  114. buscfg.quadhd_io_num = GPIO_NUM_NC;
  115. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  116. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  117. }
  118. void InitializeButtons() {
  119. boot_button_.OnClick([this]() {
  120. auto& app = Application::GetInstance();
  121. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  122. ResetWifiConfiguration();
  123. }
  124. app.ToggleChatState();
  125. });
  126. #if CONFIG_USE_DEVICE_AEC
  127. boot_button_.OnDoubleClick([this]() {
  128. auto& app = Application::GetInstance();
  129. if (app.GetDeviceState() == kDeviceStateIdle) {
  130. app.SetAecMode(app.GetAecMode() == kAecOff ? kAecOnDeviceSide : kAecOff);
  131. }
  132. });
  133. #endif
  134. }
  135. void InitializeIli9341Display() {
  136. esp_lcd_panel_io_handle_t panel_io = nullptr;
  137. esp_lcd_panel_handle_t panel = nullptr;
  138. // 液晶屏控制IO初始化
  139. ESP_LOGD(TAG, "Install panel IO");
  140. esp_lcd_panel_io_spi_config_t io_config = {};
  141. io_config.cs_gpio_num = GPIO_NUM_NC;
  142. io_config.dc_gpio_num = GPIO_NUM_2;
  143. io_config.spi_mode = 0;
  144. io_config.pclk_hz = 40 * 1000 * 1000;
  145. io_config.trans_queue_depth = 10;
  146. io_config.lcd_cmd_bits = 8;
  147. io_config.lcd_param_bits = 8;
  148. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
  149. // 初始化液晶屏驱动芯片
  150. ESP_LOGD(TAG, "Install LCD driver");
  151. const ili9341_vendor_config_t vendor_config = {
  152. .init_cmds = &vendor_specific_init[0],
  153. .init_cmds_size = sizeof(vendor_specific_init) / sizeof(ili9341_lcd_init_cmd_t),
  154. };
  155. esp_lcd_panel_dev_config_t panel_config = {};
  156. panel_config.reset_gpio_num = GPIO_NUM_NC;
  157. // panel_config.flags.reset_active_high = 0,
  158. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  159. panel_config.bits_per_pixel = 16;
  160. panel_config.vendor_config = (void *)&vendor_config;
  161. ESP_ERROR_CHECK(esp_lcd_new_panel_ili9341(panel_io, &panel_config, &panel));
  162. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel));
  163. EnableLcdCs();
  164. ESP_ERROR_CHECK(esp_lcd_panel_init(panel));
  165. ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY));
  166. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
  167. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel, false));
  168. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel, true));
  169. display_ = new SpiLcdDisplay(panel_io, panel,
  170. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  171. {
  172. .text_font = &font_puhui_20_4,
  173. .icon_font = &font_awesome_20_4,
  174. .emoji_font = font_emoji_64_init(),
  175. });
  176. }
  177. void InitializeSt7789Display() {
  178. esp_lcd_panel_io_handle_t panel_io = nullptr;
  179. esp_lcd_panel_handle_t panel = nullptr;
  180. // 液晶屏控制IO初始化
  181. ESP_LOGD(TAG, "Install panel IO");
  182. esp_lcd_panel_io_spi_config_t io_config = {};
  183. io_config.cs_gpio_num = GPIO_NUM_46;
  184. io_config.dc_gpio_num = GPIO_NUM_2;
  185. io_config.spi_mode = 0;
  186. io_config.pclk_hz = 60 * 1000 * 1000;
  187. io_config.trans_queue_depth = 10;
  188. io_config.lcd_cmd_bits = 8;
  189. io_config.lcd_param_bits = 8;
  190. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
  191. // 初始化液晶屏驱动芯片ST7789
  192. ESP_LOGD(TAG, "Install LCD driver");
  193. esp_lcd_panel_dev_config_t panel_config = {};
  194. panel_config.reset_gpio_num = GPIO_NUM_NC;
  195. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  196. panel_config.bits_per_pixel = 16;
  197. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel));
  198. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel));
  199. EnableLcdCs();
  200. ESP_ERROR_CHECK(esp_lcd_panel_init(panel));
  201. ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY));
  202. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
  203. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel, true));
  204. display_ = new SpiLcdDisplay(panel_io, panel,
  205. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  206. {
  207. .text_font = &font_puhui_20_4,
  208. .icon_font = &font_awesome_20_4,
  209. .emoji_font = font_emoji_64_init(),
  210. });
  211. }
  212. void InitializeCamera() {
  213. // Open camera power
  214. camera_config_t config = {};
  215. config.ledc_channel = LEDC_CHANNEL_2; // LEDC通道选择 用于生成XCLK时钟 但是S3不用
  216. config.ledc_timer = LEDC_TIMER_2; // LEDC timer选择 用于生成XCLK时钟 但是S3不用
  217. config.pin_d0 = CAMERA_PIN_D0;
  218. config.pin_d1 = CAMERA_PIN_D1;
  219. config.pin_d2 = CAMERA_PIN_D2;
  220. config.pin_d3 = CAMERA_PIN_D3;
  221. config.pin_d4 = CAMERA_PIN_D4;
  222. config.pin_d5 = CAMERA_PIN_D5;
  223. config.pin_d6 = CAMERA_PIN_D6;
  224. config.pin_d7 = CAMERA_PIN_D7;
  225. config.pin_xclk = CAMERA_PIN_XCLK;
  226. config.pin_pclk = CAMERA_PIN_PCLK;
  227. config.pin_vsync = CAMERA_PIN_VSYNC;
  228. config.pin_href = CAMERA_PIN_HREF;
  229. config.pin_sccb_sda = -1; // 这里写-1 表示使用已经初始化的I2C接口
  230. config.pin_sccb_scl = CAMERA_PIN_SIOC;
  231. config.sccb_i2c_port = 1;
  232. config.pin_pwdn = CAMERA_PIN_PWDN;
  233. config.pin_reset = CAMERA_PIN_RESET;
  234. config.xclk_freq_hz = XCLK_FREQ_HZ;
  235. config.pixel_format = PIXFORMAT_RGB565;
  236. config.frame_size = FRAMESIZE_VGA;
  237. config.jpeg_quality = 12;
  238. config.fb_count = 1;
  239. config.fb_location = CAMERA_FB_IN_PSRAM;
  240. config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
  241. camera_ = new Esp32Camera(config);
  242. }
  243. public:
  244. Esp32S3Korvo2V3Board() : boot_button_(BOOT_BUTTON_GPIO) {
  245. ESP_LOGI(TAG, "Initializing esp32s3_korvo2_v3 Board");
  246. InitializeI2c();
  247. I2cDetect();
  248. InitializeTca9554();
  249. InitializeCamera();
  250. InitializeSpi();
  251. InitializeButtons();
  252. #ifdef LCD_TYPE_ILI9341_SERIAL
  253. InitializeIli9341Display();
  254. #else
  255. InitializeSt7789Display();
  256. #endif
  257. }
  258. virtual AudioCodec* GetAudioCodec() override {
  259. static BoxAudioCodec audio_codec(
  260. i2c_bus_,
  261. AUDIO_INPUT_SAMPLE_RATE,
  262. AUDIO_OUTPUT_SAMPLE_RATE,
  263. AUDIO_I2S_GPIO_MCLK,
  264. AUDIO_I2S_GPIO_BCLK,
  265. AUDIO_I2S_GPIO_WS,
  266. AUDIO_I2S_GPIO_DOUT,
  267. AUDIO_I2S_GPIO_DIN,
  268. AUDIO_CODEC_PA_PIN,
  269. AUDIO_CODEC_ES8311_ADDR,
  270. AUDIO_CODEC_ES7210_ADDR,
  271. AUDIO_INPUT_REFERENCE);
  272. return &audio_codec;
  273. }
  274. virtual Display *GetDisplay() override {
  275. return display_;
  276. }
  277. virtual Camera* GetCamera() override {
  278. return camera_;
  279. }
  280. };
  281. DECLARE_BOARD(Esp32S3Korvo2V3Board);