esp32-p4-wifi6-touch-lcd-xc.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "wifi_board.h"
  2. #include "codecs/box_audio_codec.h"
  3. #include "application.h"
  4. #include "display/lcd_display.h"
  5. // #include "display/no_display.h"
  6. #include "button.h"
  7. #include "esp_lcd_panel_ops.h"
  8. #include "esp_lcd_mipi_dsi.h"
  9. #include "esp_ldo_regulator.h"
  10. #include "esp_lcd_jd9365_10_1.h"
  11. #include "config.h"
  12. #include <wifi_station.h>
  13. #include <esp_log.h>
  14. #include <driver/i2c_master.h>
  15. #include <esp_lvgl_port.h>
  16. #include "esp_lcd_touch_gt911.h"
  17. #define TAG "WaveshareEsp32p4xc"
  18. LV_FONT_DECLARE(font_puhui_30_4);
  19. LV_FONT_DECLARE(font_awesome_30_4);
  20. class WaveshareEsp32p4xc : public WifiBoard {
  21. private:
  22. i2c_master_bus_handle_t i2c_bus_;
  23. Button boot_button_;
  24. LcdDisplay *display_;
  25. void InitializeCodecI2c() {
  26. // Initialize I2C peripheral
  27. i2c_master_bus_config_t i2c_bus_cfg = {
  28. .i2c_port = I2C_NUM_1,
  29. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  30. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  31. .clk_source = I2C_CLK_SRC_DEFAULT,
  32. .glitch_ignore_cnt = 7,
  33. .intr_priority = 0,
  34. .trans_queue_depth = 0,
  35. .flags = {
  36. .enable_internal_pullup = 1,
  37. },
  38. };
  39. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  40. }
  41. static esp_err_t bsp_enable_dsi_phy_power(void) {
  42. #if MIPI_DSI_PHY_PWR_LDO_CHAN > 0
  43. // Turn on the power for MIPI DSI PHY, so it can go from "No Power" state to "Shutdown" state
  44. static esp_ldo_channel_handle_t phy_pwr_chan = NULL;
  45. esp_ldo_channel_config_t ldo_cfg = {
  46. .chan_id = MIPI_DSI_PHY_PWR_LDO_CHAN,
  47. .voltage_mv = MIPI_DSI_PHY_PWR_LDO_VOLTAGE_MV,
  48. };
  49. esp_ldo_acquire_channel(&ldo_cfg, &phy_pwr_chan);
  50. ESP_LOGI(TAG, "MIPI DSI PHY Powered on");
  51. #endif // BSP_MIPI_DSI_PHY_PWR_LDO_CHAN > 0
  52. return ESP_OK;
  53. }
  54. void InitializeLCD() {
  55. bsp_enable_dsi_phy_power();
  56. esp_lcd_panel_io_handle_t io = NULL;
  57. esp_lcd_panel_handle_t disp_panel = NULL;
  58. esp_lcd_dsi_bus_handle_t mipi_dsi_bus = NULL;
  59. esp_lcd_dsi_bus_config_t bus_config = JD9365_PANEL_BUS_DSI_2CH_CONFIG();
  60. esp_lcd_new_dsi_bus(&bus_config, &mipi_dsi_bus);
  61. ESP_LOGI(TAG, "Install MIPI DSI LCD control panel");
  62. // we use DBI interface to send LCD commands and parameters
  63. esp_lcd_dbi_io_config_t dbi_config = JD9365_PANEL_IO_DBI_CONFIG();
  64. esp_lcd_new_panel_io_dbi(mipi_dsi_bus, &dbi_config, &io);
  65. esp_lcd_dpi_panel_config_t dpi_config = {
  66. .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
  67. .dpi_clock_freq_mhz = 46,
  68. .pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB565,
  69. .num_fbs = 1,
  70. .video_timing = {
  71. .h_size = DISPLAY_WIDTH,
  72. .v_size = DISPLAY_HEIGHT,
  73. .hsync_pulse_width = 20,
  74. .hsync_back_porch = 20,
  75. .hsync_front_porch = 40,
  76. .vsync_pulse_width = 4,
  77. .vsync_back_porch = 12,
  78. .vsync_front_porch = 24,
  79. },
  80. .flags = {
  81. .use_dma2d = true,
  82. },
  83. };
  84. jd9365_vendor_config_t vendor_config = {
  85. .init_cmds = lcd_init_cmds,
  86. .init_cmds_size = sizeof(lcd_init_cmds) / sizeof(lcd_init_cmds[0]),
  87. .mipi_config = {
  88. .dsi_bus = mipi_dsi_bus,
  89. .dpi_config = &dpi_config,
  90. .lane_num = 2,
  91. },
  92. .flags = {
  93. .use_mipi_interface = 1,
  94. },
  95. };
  96. const esp_lcd_panel_dev_config_t lcd_dev_config = {
  97. .reset_gpio_num = PIN_NUM_LCD_RST,
  98. .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
  99. .bits_per_pixel = 16,
  100. .vendor_config = &vendor_config,
  101. };
  102. esp_lcd_new_panel_jd9365(io, &lcd_dev_config, &disp_panel);
  103. esp_lcd_panel_reset(disp_panel);
  104. esp_lcd_panel_init(disp_panel);
  105. display_ = new MipiLcdDisplay(io, disp_panel, DISPLAY_WIDTH, DISPLAY_HEIGHT,
  106. DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  107. {
  108. .text_font = &font_puhui_30_4,
  109. .icon_font = &font_awesome_30_4,
  110. .emoji_font = font_emoji_64_init(),
  111. });
  112. }
  113. void InitializeTouch()
  114. {
  115. esp_lcd_touch_handle_t tp;
  116. esp_lcd_touch_config_t tp_cfg = {
  117. .x_max = DISPLAY_WIDTH,
  118. .y_max = DISPLAY_HEIGHT,
  119. .rst_gpio_num = GPIO_NUM_23,
  120. .int_gpio_num = GPIO_NUM_NC,
  121. .levels = {
  122. .reset = 0,
  123. .interrupt = 0,
  124. },
  125. .flags = {
  126. .swap_xy = 0,
  127. .mirror_x = 0,
  128. .mirror_y = 0,
  129. },
  130. };
  131. esp_lcd_panel_io_handle_t tp_io_handle = NULL;
  132. esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG();
  133. tp_io_config.scl_speed_hz = 400 * 1000;
  134. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(i2c_bus_, &tp_io_config, &tp_io_handle));
  135. ESP_LOGI(TAG, "Initialize touch controller");
  136. ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_gt911(tp_io_handle, &tp_cfg, &tp));
  137. const lvgl_port_touch_cfg_t touch_cfg = {
  138. .disp = lv_display_get_default(),
  139. .handle = tp,
  140. };
  141. lvgl_port_add_touch(&touch_cfg);
  142. ESP_LOGI(TAG, "Touch panel initialized successfully");
  143. }
  144. void InitializeButtons() {
  145. boot_button_.OnClick([this]() {
  146. auto& app = Application::GetInstance();
  147. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  148. ResetWifiConfiguration();
  149. }
  150. app.ToggleChatState(); });
  151. }
  152. public:
  153. WaveshareEsp32p4xc() :
  154. boot_button_(BOOT_BUTTON_GPIO) {
  155. InitializeCodecI2c();
  156. InitializeLCD();
  157. InitializeTouch();
  158. InitializeButtons();
  159. GetBacklight()->RestoreBrightness();
  160. }
  161. virtual AudioCodec* GetAudioCodec() override {
  162. static BoxAudioCodec audio_codec(
  163. i2c_bus_,
  164. AUDIO_INPUT_SAMPLE_RATE,
  165. AUDIO_OUTPUT_SAMPLE_RATE,
  166. AUDIO_I2S_GPIO_MCLK,
  167. AUDIO_I2S_GPIO_BCLK,
  168. AUDIO_I2S_GPIO_WS,
  169. AUDIO_I2S_GPIO_DOUT,
  170. AUDIO_I2S_GPIO_DIN,
  171. AUDIO_CODEC_PA_PIN,
  172. AUDIO_CODEC_ES8311_ADDR,
  173. AUDIO_CODEC_ES7210_ADDR,
  174. AUDIO_INPUT_REFERENCE);
  175. return &audio_codec;
  176. }
  177. virtual Display *GetDisplay() override {
  178. return display_;
  179. }
  180. virtual Backlight* GetBacklight() override {
  181. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  182. return &backlight;
  183. }
  184. };
  185. DECLARE_BOARD(WaveshareEsp32p4xc);