atk_dnesp32s3_box.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "wifi_board.h"
  2. #include "codecs/es8311_audio_codec.h"
  3. #include "codecs/no_audio_codec.h"
  4. #include "display/lcd_display.h"
  5. #include "application.h"
  6. #include "button.h"
  7. #include "config.h"
  8. #include "led/single_led.h"
  9. #include "i2c_device.h"
  10. #include <wifi_station.h>
  11. #include <esp_log.h>
  12. #include <driver/i2c_master.h>
  13. #include <freertos/FreeRTOS.h>
  14. #include <freertos/task.h>
  15. #include <freertos/timers.h>
  16. #include <esp_lcd_panel_io.h>
  17. #include <esp_lcd_panel_vendor.h>
  18. #include <esp_lcd_panel_ops.h>
  19. #define TAG "atk_dnesp32s3_box"
  20. LV_FONT_DECLARE(font_puhui_20_4);
  21. LV_FONT_DECLARE(font_awesome_20_4);
  22. class ATK_NoAudioCodecDuplex : public NoAudioCodec {
  23. public:
  24. ATK_NoAudioCodecDuplex(int input_sample_rate, int output_sample_rate, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din) {
  25. duplex_ = true;
  26. input_sample_rate_ = input_sample_rate;
  27. output_sample_rate_ = output_sample_rate;
  28. i2s_chan_config_t chan_cfg = {
  29. .id = I2S_NUM_0,
  30. .role = I2S_ROLE_MASTER,
  31. .dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
  32. .dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
  33. .auto_clear_after_cb = true,
  34. .auto_clear_before_cb = false,
  35. .intr_priority = 0,
  36. };
  37. ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle_, &rx_handle_));
  38. i2s_std_config_t std_cfg = {
  39. .clk_cfg = {
  40. .sample_rate_hz = (uint32_t)output_sample_rate_,
  41. .clk_src = I2S_CLK_SRC_DEFAULT,
  42. .mclk_multiple = I2S_MCLK_MULTIPLE_256,
  43. #ifdef I2S_HW_VERSION_2
  44. .ext_clk_freq_hz = 0,
  45. #endif
  46. },
  47. .slot_cfg = {
  48. .data_bit_width = I2S_DATA_BIT_WIDTH_16BIT,
  49. .slot_bit_width = I2S_SLOT_BIT_WIDTH_AUTO,
  50. .slot_mode = I2S_SLOT_MODE_STEREO,
  51. .slot_mask = I2S_STD_SLOT_BOTH,
  52. .ws_width = I2S_DATA_BIT_WIDTH_16BIT,
  53. .ws_pol = false,
  54. .bit_shift = true,
  55. #ifdef I2S_HW_VERSION_2
  56. .left_align = true,
  57. .big_endian = false,
  58. .bit_order_lsb = false
  59. #endif
  60. },
  61. .gpio_cfg = {
  62. .mclk = I2S_GPIO_UNUSED,
  63. .bclk = bclk,
  64. .ws = ws,
  65. .dout = dout,
  66. .din = din,
  67. .invert_flags = {
  68. .mclk_inv = false,
  69. .bclk_inv = false,
  70. .ws_inv = false
  71. }
  72. }
  73. };
  74. ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
  75. ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle_, &std_cfg));
  76. ESP_LOGI(TAG, "Duplex channels created");
  77. }
  78. };
  79. class XL9555_IN : public I2cDevice {
  80. public:
  81. XL9555_IN(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
  82. WriteReg(0x06, 0x3B);
  83. WriteReg(0x07, 0xFE);
  84. }
  85. void xl9555_cfg(void) {
  86. WriteReg(0x06, 0x1B);
  87. WriteReg(0x07, 0xFE);
  88. }
  89. void SetOutputState(uint8_t bit, uint8_t level) {
  90. uint16_t data;
  91. int index = bit;
  92. if (bit < 8) {
  93. data = ReadReg(0x02);
  94. } else {
  95. data = ReadReg(0x03);
  96. index -= 8;
  97. }
  98. data = (data & ~(1 << index)) | (level << index);
  99. if (bit < 8) {
  100. WriteReg(0x02, data);
  101. } else {
  102. WriteReg(0x03, data);
  103. }
  104. }
  105. int GetPingState(uint16_t pin) {
  106. uint8_t data;
  107. if (pin <= 0x0080) {
  108. data = ReadReg(0x00);
  109. return (data & (uint8_t)(pin & 0xFF)) ? 1 : 0;
  110. } else {
  111. data = ReadReg(0x01);
  112. return (data & (uint8_t)((pin >> 8) & 0xFF )) ? 1 : 0;
  113. }
  114. return 0;
  115. }
  116. };
  117. class atk_dnesp32s3_box : public WifiBoard {
  118. private:
  119. i2c_master_bus_handle_t i2c_bus_;
  120. i2c_master_dev_handle_t xl9555_handle_;
  121. Button boot_button_;
  122. LcdDisplay* display_;
  123. XL9555_IN* xl9555_in_;
  124. bool es8311_detected_ = false;
  125. void InitializeI2c() {
  126. // Initialize I2C peripheral
  127. i2c_master_bus_config_t i2c_bus_cfg = {
  128. .i2c_port = (i2c_port_t)0,
  129. .sda_io_num = GPIO_NUM_48,
  130. .scl_io_num = GPIO_NUM_45,
  131. .clk_source = I2C_CLK_SRC_DEFAULT,
  132. .glitch_ignore_cnt = 7,
  133. .intr_priority = 0,
  134. .trans_queue_depth = 0,
  135. .flags = {
  136. .enable_internal_pullup = 1,
  137. },
  138. };
  139. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  140. // Initialize XL9555
  141. xl9555_in_ = new XL9555_IN(i2c_bus_, 0x20);
  142. if (xl9555_in_->GetPingState(0x0020) == 1) {
  143. es8311_detected_ = true; /* 音频设备标志位,SPK_CTRL_IO为高电平时,该标志位置1,且判定为ES8311 */
  144. } else {
  145. es8311_detected_ = false; /* 音频设备标志位,SPK_CTRL_IO为低电平时,该标志位置0,且判定为NS4168 */
  146. }
  147. xl9555_in_->xl9555_cfg();
  148. }
  149. void InitializeATK_ST7789_80_Display() {
  150. esp_lcd_panel_io_handle_t panel_io = nullptr;
  151. esp_lcd_panel_handle_t panel = nullptr;
  152. /* 配置RD引脚 */
  153. gpio_config_t gpio_init_struct;
  154. gpio_init_struct.intr_type = GPIO_INTR_DISABLE;
  155. gpio_init_struct.mode = GPIO_MODE_INPUT_OUTPUT;
  156. gpio_init_struct.pin_bit_mask = 1ull << LCD_NUM_RD;
  157. gpio_init_struct.pull_down_en = GPIO_PULLDOWN_DISABLE;
  158. gpio_init_struct.pull_up_en = GPIO_PULLUP_ENABLE;
  159. gpio_config(&gpio_init_struct);
  160. gpio_set_level(LCD_NUM_RD, 1);
  161. esp_lcd_i80_bus_handle_t i80_bus = NULL;
  162. esp_lcd_i80_bus_config_t bus_config = {
  163. .dc_gpio_num = LCD_NUM_DC,
  164. .wr_gpio_num = LCD_NUM_WR,
  165. .clk_src = LCD_CLK_SRC_DEFAULT,
  166. .data_gpio_nums = {
  167. GPIO_LCD_D0,
  168. GPIO_LCD_D1,
  169. GPIO_LCD_D2,
  170. GPIO_LCD_D3,
  171. GPIO_LCD_D4,
  172. GPIO_LCD_D5,
  173. GPIO_LCD_D6,
  174. GPIO_LCD_D7,
  175. },
  176. .bus_width = 8,
  177. .max_transfer_bytes = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t),
  178. .psram_trans_align = 64,
  179. .sram_trans_align = 4,
  180. };
  181. ESP_ERROR_CHECK(esp_lcd_new_i80_bus(&bus_config, &i80_bus));
  182. esp_lcd_panel_io_i80_config_t io_config = {
  183. .cs_gpio_num = LCD_NUM_CS,
  184. .pclk_hz = (10 * 1000 * 1000),
  185. .trans_queue_depth = 10,
  186. .on_color_trans_done = nullptr,
  187. .user_ctx = nullptr,
  188. .lcd_cmd_bits = 8,
  189. .lcd_param_bits = 8,
  190. .dc_levels = {
  191. .dc_idle_level = 0,
  192. .dc_cmd_level = 0,
  193. .dc_dummy_level = 0,
  194. .dc_data_level = 1,
  195. },
  196. .flags = {
  197. .swap_color_bytes = 0,
  198. },
  199. };
  200. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i80(i80_bus, &io_config, &panel_io));
  201. esp_lcd_panel_dev_config_t panel_config = {
  202. .reset_gpio_num = LCD_NUM_RST,
  203. .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
  204. .bits_per_pixel = 16,
  205. };
  206. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel));
  207. esp_lcd_panel_reset(panel);
  208. esp_lcd_panel_init(panel);
  209. esp_lcd_panel_invert_color(panel, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  210. esp_lcd_panel_set_gap(panel, 0, 0);
  211. uint8_t data0[] = {0x00};
  212. uint8_t data1[] = {0x65};
  213. esp_lcd_panel_io_tx_param(panel_io, 0x36, data0, 1);
  214. esp_lcd_panel_io_tx_param(panel_io, 0x3A, data1, 1);
  215. esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
  216. esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
  217. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel, true));
  218. display_ = new SpiLcdDisplay(panel_io, panel,
  219. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  220. {
  221. .text_font = &font_puhui_20_4,
  222. .icon_font = &font_awesome_20_4,
  223. #if CONFIG_USE_WECHAT_MESSAGE_STYLE
  224. .emoji_font = font_emoji_32_init(),
  225. #else
  226. .emoji_font = DISPLAY_HEIGHT >= 240 ? font_emoji_64_init() : font_emoji_32_init(),
  227. #endif
  228. });
  229. }
  230. void InitializeButtons() {
  231. boot_button_.OnClick([this]() {
  232. auto& app = Application::GetInstance();
  233. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  234. ResetWifiConfiguration();
  235. }
  236. app.ToggleChatState();
  237. });
  238. }
  239. public:
  240. atk_dnesp32s3_box() : boot_button_(BOOT_BUTTON_GPIO) {
  241. InitializeI2c();
  242. InitializeATK_ST7789_80_Display();
  243. xl9555_in_->SetOutputState(5, 1);
  244. xl9555_in_->SetOutputState(7, 1);
  245. InitializeButtons();
  246. }
  247. virtual AudioCodec* GetAudioCodec() override {
  248. /* 根据探测结果初始化编解码器 */
  249. if (es8311_detected_) {
  250. /* 使用ES8311 驱动 */
  251. static Es8311AudioCodec audio_codec(
  252. i2c_bus_,
  253. I2C_NUM_0,
  254. AUDIO_INPUT_SAMPLE_RATE,
  255. AUDIO_OUTPUT_SAMPLE_RATE,
  256. GPIO_NUM_NC,
  257. AUDIO_I2S_GPIO_BCLK,
  258. AUDIO_I2S_GPIO_WS,
  259. AUDIO_I2S_GPIO_DOUT,
  260. AUDIO_I2S_GPIO_DIN,
  261. GPIO_NUM_NC,
  262. AUDIO_CODEC_ES8311_ADDR,
  263. false);
  264. return &audio_codec;
  265. } else {
  266. static ATK_NoAudioCodecDuplex audio_codec(
  267. AUDIO_INPUT_SAMPLE_RATE,
  268. AUDIO_OUTPUT_SAMPLE_RATE,
  269. AUDIO_I2S_GPIO_BCLK,
  270. AUDIO_I2S_GPIO_WS,
  271. AUDIO_I2S_GPIO_DOUT,
  272. AUDIO_I2S_GPIO_DIN);
  273. return &audio_codec;
  274. }
  275. return NULL;
  276. }
  277. virtual Display* GetDisplay() override {
  278. return display_;
  279. }
  280. };
  281. DECLARE_BOARD(atk_dnesp32s3_box);